0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / src / Bnd / Bnd_Box.hxx
1 // Created on: 1991-01-28
2 // Created by: Remi Lequette
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _Bnd_Box_HeaderFile
18 #define _Bnd_Box_HeaderFile
19
20 #include <Standard.hxx>
21 #include <Standard_DefineAlloc.hxx>
22 #include <Standard_Handle.hxx>
23
24 #include <Standard_Real.hxx>
25 #include <Standard_Integer.hxx>
26 #include <Standard_Boolean.hxx>
27 class Standard_ConstructionError;
28 class gp_Pnt;
29 class gp_Dir;
30 class gp_Trsf;
31 class gp_Lin;
32 class gp_Pln;
33
34
35 //! Describes a bounding box in 3D space.
36 //! A bounding box is parallel to the axes of the coordinates
37 //! system. If it is finite, it is defined by the three intervals:
38 //! -   [ Xmin,Xmax ],
39 //! -   [ Ymin,Ymax ],
40 //! -   [ Zmin,Zmax ].
41 //! A bounding box may be infinite (i.e. open) in one or more
42 //! directions. It is said to be:
43 //! -   OpenXmin if it is infinite on the negative side of the   "X Direction";
44 //! -   OpenXmax if it is infinite on the positive side of the "X Direction";
45 //! -   OpenYmin if it is infinite on the negative side of the   "Y Direction";
46 //! -   OpenYmax if it is infinite on the positive side of the "Y Direction";
47 //! -   OpenZmin if it is infinite on the negative side of the   "Z Direction";
48 //! -   OpenZmax if it is infinite on the positive side of the "Z Direction";
49 //! -   WholeSpace if it is infinite in all six directions. In this
50 //! case, any point of the space is inside the box;
51 //! -   Void if it is empty. In this case, there is no point included in the box.
52 //! A bounding box is defined by:
53 //! -   six bounds (Xmin, Xmax, Ymin, Ymax, Zmin and
54 //! Zmax) which limit the bounding box if it is finite,
55 //! -   eight flags (OpenXmin, OpenXmax, OpenYmin,
56 //! OpenYmax, OpenZmin, OpenZmax,
57 //! WholeSpace and Void) which describe the
58 //! bounding box if it is infinite or empty, and
59 //! -   a gap, which is included on both sides in any direction
60 //! when consulting the finite bounds of the box.
61 class Bnd_Box 
62 {
63 public:
64
65   DEFINE_STANDARD_ALLOC
66
67   
68   //! Creates an empty Box.
69   //! The constructed box is qualified Void. Its gap is null.
70   Standard_EXPORT Bnd_Box();
71
72   //! Sets this bounding box so that it  covers the whole of 3D space.
73   //! It is infinitely  long in all directions.
74   void SetWhole() { Flags = WholeMask; }
75
76   //! Sets this bounding box so that it is empty. All points are outside a void box.
77   void SetVoid()
78   {
79     Flags = VoidMask;
80     Gap   = 0.0;
81   }
82
83   //! Sets this bounding box so that it bounds
84   //! -   the point P. This involves first setting this bounding box
85   //! to be void and then adding the point P.
86   Standard_EXPORT void Set (const gp_Pnt& P);
87   
88   //! Sets this bounding box so that it bounds
89   //! the half-line defined by point P and direction D, i.e. all
90   //! points M defined by M=P+u*D, where u is greater than
91   //! or equal to 0, are inside the bounding volume. This
92   //! involves first setting this box to be void and then adding   the half-line.
93   Standard_EXPORT void Set (const gp_Pnt& P, const gp_Dir& D);
94   
95   //! Enlarges this bounding box, if required, so that it
96   //! contains at least:
97   //! -   interval [ aXmin,aXmax ] in the "X Direction",
98   //! -   interval [ aYmin,aYmax ] in the "Y Direction",
99   //! -   interval [ aZmin,aZmax ] in the "Z Direction";
100   Standard_EXPORT void Update (const Standard_Real aXmin, const Standard_Real aYmin, const Standard_Real aZmin, const Standard_Real aXmax, const Standard_Real aYmax, const Standard_Real aZmax);
101   
102   //! Adds a point of coordinates (X,Y,Z) to this bounding box.
103   Standard_EXPORT void Update (const Standard_Real X, const Standard_Real Y, const Standard_Real Z);
104   
105   //! Returns the gap of this bounding box.
106   Standard_EXPORT Standard_Real GetGap() const;
107   
108   //! Set the gap of this bounding box to abs(Tol).
109   Standard_EXPORT void SetGap (const Standard_Real Tol);
110   
111   //! Enlarges the      box    with    a   tolerance   value.
112   //! (minvalues-Abs(<tol>) and maxvalues+Abs(<tol>))
113   //! This means that the minimum values of its X, Y and Z
114   //! intervals of definition, when they are finite, are reduced by
115   //! the absolute value of Tol, while the maximum values are
116   //! increased by the same amount.
117   Standard_EXPORT void Enlarge (const Standard_Real Tol);
118   
119   //! Returns the bounds of this bounding box. The gap is included.
120   //! If this bounding box is infinite (i.e. "open"), returned values
121   //! may be equal to +/- Precision::Infinite().
122   //! Standard_ConstructionError exception will be thrown if the box is void.
123   //! if IsVoid()
124   Standard_EXPORT void Get (Standard_Real& theXmin, Standard_Real& theYmin, Standard_Real& theZmin, Standard_Real& theXmax, Standard_Real& theYmax, Standard_Real& theZmax) const;
125   
126   //! Returns the lower corner of this bounding box. The gap is included.
127   //! If this bounding box is infinite (i.e. "open"), returned values
128   //! may be equal to +/- Precision::Infinite().
129   //! Standard_ConstructionError exception will be thrown if the box is void.
130   //! if IsVoid()
131   Standard_EXPORT gp_Pnt CornerMin() const;
132   
133   //! Returns the upper corner of this bounding box. The gap is included.
134   //! If this bounding box is infinite (i.e. "open"), returned values
135   //! may be equal to +/- Precision::Infinite().
136   //! Standard_ConstructionError exception will be thrown if the box is void.
137   //! if IsVoid()
138   Standard_EXPORT gp_Pnt CornerMax() const;
139
140   //! The   Box will be   infinitely   long  in the Xmin
141   //! direction.
142   void OpenXmin() { Flags |= XminMask; }
143
144   //! The   Box will be   infinitely   long  in the Xmax
145   //! direction.
146   void OpenXmax() { Flags |= XmaxMask; }
147
148   //! The   Box will be   infinitely   long  in the Ymin
149   //! direction.
150   void OpenYmin() { Flags |= YminMask; }
151
152   //! The   Box will be   infinitely   long  in the Ymax
153   //! direction.
154   void OpenYmax() { Flags |= YmaxMask; }
155
156   //! The   Box will be   infinitely   long  in the Zmin
157   //! direction.
158   void OpenZmin() { Flags |= ZminMask; }
159
160   //! The   Box will be   infinitely   long  in the Zmax
161   //! direction.
162   void OpenZmax() { Flags |= ZmaxMask; }
163
164   //! Returns true if this bounding box is open in the  Xmin direction.
165   Standard_Boolean IsOpenXmin() const { return (Flags & XminMask) != 0; }
166
167   //! Returns true if this bounding box is open in the  Xmax direction.
168   Standard_Boolean IsOpenXmax() const { return (Flags & XmaxMask) != 0; }
169
170   //! Returns true if this bounding box is open in the  Ymix direction.
171   Standard_Boolean IsOpenYmin() const { return (Flags & YminMask) != 0; }
172
173   //! Returns true if this bounding box is open in the  Ymax direction.
174   Standard_Boolean IsOpenYmax() const { return (Flags & YmaxMask) != 0; }
175
176   //! Returns true if this bounding box is open in the  Zmin direction.
177   Standard_Boolean IsOpenZmin() const { return (Flags & ZminMask) != 0; }
178
179   //! Returns true if this bounding box is open in the  Zmax  direction.
180   Standard_Boolean IsOpenZmax() const { return (Flags & ZmaxMask) != 0; }
181
182   //! Returns true if this bounding box is infinite in all 6 directions (WholeSpace flag).
183   Standard_Boolean IsWhole()    const { return (Flags & WholeMask) == WholeMask; }
184
185   //! Returns true if this bounding box is empty (Void flag).
186   Standard_Boolean IsVoid()     const { return (Flags & VoidMask) != 0; }
187
188   //! true if xmax-xmin < tol.
189   Standard_EXPORT Standard_Boolean IsXThin (const Standard_Real tol) const;
190   
191   //! true if ymax-ymin < tol.
192   Standard_EXPORT Standard_Boolean IsYThin (const Standard_Real tol) const;
193   
194   //! true if zmax-zmin < tol.
195   Standard_EXPORT Standard_Boolean IsZThin (const Standard_Real tol) const;
196   
197   //! Returns true if IsXThin, IsYThin and IsZThin are all true,
198   //! i.e. if the box is thin in all three dimensions.
199   Standard_EXPORT Standard_Boolean IsThin (const Standard_Real tol) const;
200   
201   //! Returns a bounding box which is the result of applying the
202   //! transformation T to this bounding box.
203   //! Warning
204   //! Applying a geometric transformation (for example, a
205   //! rotation) to a bounding box generally increases its
206   //! dimensions. This is not optimal for algorithms which use it.
207   Standard_EXPORT Bnd_Box Transformed (const gp_Trsf& T) const;
208   
209   //! Adds the box <Other> to <me>.
210   Standard_EXPORT void Add (const Bnd_Box& Other);
211   
212   //! Adds a Pnt to the box.
213   Standard_EXPORT void Add (const gp_Pnt& P);
214   
215   //! Extends  <me> from the Pnt <P> in the direction <D>.
216   Standard_EXPORT void Add (const gp_Pnt& P, const gp_Dir& D);
217   
218   //! Extends the Box  in the given Direction, i.e. adds
219   //! an  half-line. The   box  may become   infinite in
220   //! 1,2 or 3 directions.
221   Standard_EXPORT void Add (const gp_Dir& D);
222   
223   //! Returns True if the Pnt is out the box.
224   Standard_EXPORT Standard_Boolean IsOut (const gp_Pnt& P) const;
225   
226   //! Returns False if the line intersects the box.
227   Standard_EXPORT Standard_Boolean IsOut (const gp_Lin& L) const;
228   
229   //! Returns False if the plane intersects the box.
230   Standard_EXPORT Standard_Boolean IsOut (const gp_Pln& P) const;
231   
232   //! Returns False if the <Box> intersects or is inside <me>.
233   Standard_EXPORT Standard_Boolean IsOut (const Bnd_Box& Other) const;
234   
235   //! Returns False if  the transformed <Box> intersects
236   //! or  is inside <me>.
237   Standard_EXPORT Standard_Boolean IsOut (const Bnd_Box& Other, const gp_Trsf& T) const;
238   
239   //! Returns False  if the transformed <Box> intersects
240   //! or  is inside the transformed box <me>.
241   Standard_EXPORT Standard_Boolean IsOut (const gp_Trsf& T1, const Bnd_Box& Other, const gp_Trsf& T2) const;
242   
243   //! Returns False  if the flat band lying between two parallel
244   //! lines represented by their reference points <P1>, <P2> and
245   //! direction <D> intersects the box.
246   Standard_EXPORT Standard_Boolean IsOut (const gp_Pnt& P1, const gp_Pnt& P2, const gp_Dir& D) const;
247   
248   //! Computes the minimum distance between two boxes.
249   Standard_EXPORT Standard_Real Distance (const Bnd_Box& Other) const;
250   
251   Standard_EXPORT void Dump() const;
252
253   //! Computes the squared diagonal of me.
254   Standard_Real SquareExtent() const
255   {
256     if (IsVoid())
257     {
258       return 0.0;
259     }
260
261     const Standard_Real aDx = Xmax - Xmin + Gap + Gap;
262     const Standard_Real aDy = Ymax - Ymin + Gap + Gap;
263     const Standard_Real aDz = Zmax - Zmin + Gap + Gap;
264     return aDx * aDx + aDy * aDy + aDz * aDz;
265   }
266
267 protected:
268
269   //! Bit flags.
270   enum MaskFlags
271   {
272     VoidMask  = 0x01,
273     XminMask  = 0x02,
274     XmaxMask  = 0x04,
275     YminMask  = 0x08,
276     YmaxMask  = 0x10,
277     ZminMask  = 0x20,
278     ZmaxMask  = 0x40,
279     WholeMask = 0x7e
280   };
281
282 private:
283
284   Standard_Real Xmin;
285   Standard_Real Xmax;
286   Standard_Real Ymin;
287   Standard_Real Ymax;
288   Standard_Real Zmin;
289   Standard_Real Zmax;
290   Standard_Real Gap;
291   Standard_Integer Flags;
292
293 };
294
295 #endif // _Bnd_Box_HeaderFile