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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #ifndef _Bnd_Box_HeaderFile
18 #define _Bnd_Box_HeaderFile
20 #include <Standard.hxx>
21 #include <Standard_DefineAlloc.hxx>
22 #include <Standard_Handle.hxx>
24 #include <Standard_Real.hxx>
25 #include <Standard_Integer.hxx>
26 #include <Standard_Boolean.hxx>
27 class Standard_ConstructionError;
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:
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.
68 //! Creates an empty Box.
69 //! The constructed box is qualified Void. Its gap is null.
70 Standard_EXPORT Bnd_Box();
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; }
76 //! Sets this bounding box so that it is empty. All points are outside a void box.
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);
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);
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);
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);
105 //! Returns the gap of this bounding box.
106 Standard_EXPORT Standard_Real GetGap() const;
108 //! Set the gap of this bounding box to abs(Tol).
109 Standard_EXPORT void SetGap (const Standard_Real Tol);
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);
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.
124 Standard_EXPORT void Get (Standard_Real& theXmin, Standard_Real& theYmin, Standard_Real& theZmin, Standard_Real& theXmax, Standard_Real& theYmax, Standard_Real& theZmax) const;
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.
131 Standard_EXPORT gp_Pnt CornerMin() const;
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.
138 Standard_EXPORT gp_Pnt CornerMax() const;
140 //! The Box will be infinitely long in the Xmin
142 void OpenXmin() { Flags |= XminMask; }
144 //! The Box will be infinitely long in the Xmax
146 void OpenXmax() { Flags |= XmaxMask; }
148 //! The Box will be infinitely long in the Ymin
150 void OpenYmin() { Flags |= YminMask; }
152 //! The Box will be infinitely long in the Ymax
154 void OpenYmax() { Flags |= YmaxMask; }
156 //! The Box will be infinitely long in the Zmin
158 void OpenZmin() { Flags |= ZminMask; }
160 //! The Box will be infinitely long in the Zmax
162 void OpenZmax() { Flags |= ZmaxMask; }
164 //! Returns true if this bounding box is open in the Xmin direction.
165 Standard_Boolean IsOpenXmin() const { return (Flags & XminMask) != 0; }
167 //! Returns true if this bounding box is open in the Xmax direction.
168 Standard_Boolean IsOpenXmax() const { return (Flags & XmaxMask) != 0; }
170 //! Returns true if this bounding box is open in the Ymix direction.
171 Standard_Boolean IsOpenYmin() const { return (Flags & YminMask) != 0; }
173 //! Returns true if this bounding box is open in the Ymax direction.
174 Standard_Boolean IsOpenYmax() const { return (Flags & YmaxMask) != 0; }
176 //! Returns true if this bounding box is open in the Zmin direction.
177 Standard_Boolean IsOpenZmin() const { return (Flags & ZminMask) != 0; }
179 //! Returns true if this bounding box is open in the Zmax direction.
180 Standard_Boolean IsOpenZmax() const { return (Flags & ZmaxMask) != 0; }
182 //! Returns true if this bounding box is infinite in all 6 directions (WholeSpace flag).
183 Standard_Boolean IsWhole() const { return (Flags & WholeMask) == WholeMask; }
185 //! Returns true if this bounding box is empty (Void flag).
186 Standard_Boolean IsVoid() const { return (Flags & VoidMask) != 0; }
188 //! true if xmax-xmin < tol.
189 Standard_EXPORT Standard_Boolean IsXThin (const Standard_Real tol) const;
191 //! true if ymax-ymin < tol.
192 Standard_EXPORT Standard_Boolean IsYThin (const Standard_Real tol) const;
194 //! true if zmax-zmin < tol.
195 Standard_EXPORT Standard_Boolean IsZThin (const Standard_Real tol) const;
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;
201 //! Returns a bounding box which is the result of applying the
202 //! transformation T to this bounding box.
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;
209 //! Adds the box <Other> to <me>.
210 Standard_EXPORT void Add (const Bnd_Box& Other);
212 //! Adds a Pnt to the box.
213 Standard_EXPORT void Add (const gp_Pnt& P);
215 //! Extends <me> from the Pnt <P> in the direction <D>.
216 Standard_EXPORT void Add (const gp_Pnt& P, const gp_Dir& D);
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);
223 //! Returns True if the Pnt is out the box.
224 Standard_EXPORT Standard_Boolean IsOut (const gp_Pnt& P) const;
226 //! Returns False if the line intersects the box.
227 Standard_EXPORT Standard_Boolean IsOut (const gp_Lin& L) const;
229 //! Returns False if the plane intersects the box.
230 Standard_EXPORT Standard_Boolean IsOut (const gp_Pln& P) const;
232 //! Returns False if the <Box> intersects or is inside <me>.
233 Standard_EXPORT Standard_Boolean IsOut (const Bnd_Box& Other) const;
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;
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;
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;
248 //! Computes the minimum distance between two boxes.
249 Standard_EXPORT Standard_Real Distance (const Bnd_Box& Other) const;
251 Standard_EXPORT void Dump() const;
253 //! Computes the squared diagonal of me.
254 Standard_Real SquareExtent() const
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;
291 Standard_Integer Flags;
295 #endif // _Bnd_Box_HeaderFile