42cf5bc1 |
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(); |
dde68833 |
71 | |
42cf5bc1 |
72 | //! Sets this bounding box so that it covers the whole of 3D space. |
73 | //! It is infinitely long in all directions. |
dde68833 |
74 | void SetWhole() { Flags = WholeMask; } |
75 | |
42cf5bc1 |
76 | //! Sets this bounding box so that it is empty. All points are outside a void box. |
dde68833 |
77 | void SetVoid() |
78 | { |
04f0f1b0 |
79 | Xmin = RealLast(); |
80 | Xmax = -RealLast(); |
81 | Ymin = RealLast(); |
82 | Ymax = -RealLast(); |
83 | Zmin = RealLast(); |
84 | Zmax = -RealLast(); |
dde68833 |
85 | Flags = VoidMask; |
86 | Gap = 0.0; |
87 | } |
88 | |
42cf5bc1 |
89 | //! Sets this bounding box so that it bounds |
90 | //! - the point P. This involves first setting this bounding box |
91 | //! to be void and then adding the point P. |
92 | Standard_EXPORT void Set (const gp_Pnt& P); |
93 | |
94 | //! Sets this bounding box so that it bounds |
95 | //! the half-line defined by point P and direction D, i.e. all |
96 | //! points M defined by M=P+u*D, where u is greater than |
97 | //! or equal to 0, are inside the bounding volume. This |
98 | //! involves first setting this box to be void and then adding the half-line. |
99 | Standard_EXPORT void Set (const gp_Pnt& P, const gp_Dir& D); |
100 | |
101 | //! Enlarges this bounding box, if required, so that it |
102 | //! contains at least: |
103 | //! - interval [ aXmin,aXmax ] in the "X Direction", |
104 | //! - interval [ aYmin,aYmax ] in the "Y Direction", |
105 | //! - interval [ aZmin,aZmax ] in the "Z Direction"; |
106 | 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); |
107 | |
108 | //! Adds a point of coordinates (X,Y,Z) to this bounding box. |
109 | Standard_EXPORT void Update (const Standard_Real X, const Standard_Real Y, const Standard_Real Z); |
110 | |
111 | //! Returns the gap of this bounding box. |
112 | Standard_EXPORT Standard_Real GetGap() const; |
113 | |
114 | //! Set the gap of this bounding box to abs(Tol). |
115 | Standard_EXPORT void SetGap (const Standard_Real Tol); |
116 | |
117 | //! Enlarges the box with a tolerance value. |
118 | //! (minvalues-Abs(<tol>) and maxvalues+Abs(<tol>)) |
119 | //! This means that the minimum values of its X, Y and Z |
120 | //! intervals of definition, when they are finite, are reduced by |
121 | //! the absolute value of Tol, while the maximum values are |
122 | //! increased by the same amount. |
123 | Standard_EXPORT void Enlarge (const Standard_Real Tol); |
124 | |
125 | //! Returns the bounds of this bounding box. The gap is included. |
126 | //! If this bounding box is infinite (i.e. "open"), returned values |
127 | //! may be equal to +/- Precision::Infinite(). |
128 | //! Standard_ConstructionError exception will be thrown if the box is void. |
129 | //! if IsVoid() |
130 | Standard_EXPORT void Get (Standard_Real& theXmin, Standard_Real& theYmin, Standard_Real& theZmin, Standard_Real& theXmax, Standard_Real& theYmax, Standard_Real& theZmax) const; |
131 | |
132 | //! Returns the lower corner of this bounding box. The gap is included. |
133 | //! If this bounding box is infinite (i.e. "open"), returned values |
134 | //! may be equal to +/- Precision::Infinite(). |
135 | //! Standard_ConstructionError exception will be thrown if the box is void. |
136 | //! if IsVoid() |
137 | Standard_EXPORT gp_Pnt CornerMin() const; |
138 | |
139 | //! Returns the upper corner of this bounding box. The gap is included. |
140 | //! If this bounding box is infinite (i.e. "open"), returned values |
141 | //! may be equal to +/- Precision::Infinite(). |
142 | //! Standard_ConstructionError exception will be thrown if the box is void. |
143 | //! if IsVoid() |
144 | Standard_EXPORT gp_Pnt CornerMax() const; |
dde68833 |
145 | |
42cf5bc1 |
146 | //! The Box will be infinitely long in the Xmin |
147 | //! direction. |
dde68833 |
148 | void OpenXmin() { Flags |= XminMask; } |
149 | |
42cf5bc1 |
150 | //! The Box will be infinitely long in the Xmax |
151 | //! direction. |
dde68833 |
152 | void OpenXmax() { Flags |= XmaxMask; } |
153 | |
42cf5bc1 |
154 | //! The Box will be infinitely long in the Ymin |
155 | //! direction. |
dde68833 |
156 | void OpenYmin() { Flags |= YminMask; } |
157 | |
42cf5bc1 |
158 | //! The Box will be infinitely long in the Ymax |
159 | //! direction. |
dde68833 |
160 | void OpenYmax() { Flags |= YmaxMask; } |
161 | |
42cf5bc1 |
162 | //! The Box will be infinitely long in the Zmin |
163 | //! direction. |
dde68833 |
164 | void OpenZmin() { Flags |= ZminMask; } |
165 | |
42cf5bc1 |
166 | //! The Box will be infinitely long in the Zmax |
167 | //! direction. |
dde68833 |
168 | void OpenZmax() { Flags |= ZmaxMask; } |
169 | |
04f0f1b0 |
170 | //! Returns true if this bounding box has at least one open direction. |
171 | Standard_Boolean IsOpen() const { return (Flags & WholeMask) != 0; } |
172 | |
42cf5bc1 |
173 | //! Returns true if this bounding box is open in the Xmin direction. |
dde68833 |
174 | Standard_Boolean IsOpenXmin() const { return (Flags & XminMask) != 0; } |
175 | |
42cf5bc1 |
176 | //! Returns true if this bounding box is open in the Xmax direction. |
dde68833 |
177 | Standard_Boolean IsOpenXmax() const { return (Flags & XmaxMask) != 0; } |
178 | |
42cf5bc1 |
179 | //! Returns true if this bounding box is open in the Ymix direction. |
dde68833 |
180 | Standard_Boolean IsOpenYmin() const { return (Flags & YminMask) != 0; } |
181 | |
42cf5bc1 |
182 | //! Returns true if this bounding box is open in the Ymax direction. |
dde68833 |
183 | Standard_Boolean IsOpenYmax() const { return (Flags & YmaxMask) != 0; } |
184 | |
42cf5bc1 |
185 | //! Returns true if this bounding box is open in the Zmin direction. |
dde68833 |
186 | Standard_Boolean IsOpenZmin() const { return (Flags & ZminMask) != 0; } |
187 | |
42cf5bc1 |
188 | //! Returns true if this bounding box is open in the Zmax direction. |
dde68833 |
189 | Standard_Boolean IsOpenZmax() const { return (Flags & ZmaxMask) != 0; } |
190 | |
42cf5bc1 |
191 | //! Returns true if this bounding box is infinite in all 6 directions (WholeSpace flag). |
dde68833 |
192 | Standard_Boolean IsWhole() const { return (Flags & WholeMask) == WholeMask; } |
193 | |
42cf5bc1 |
194 | //! Returns true if this bounding box is empty (Void flag). |
dde68833 |
195 | Standard_Boolean IsVoid() const { return (Flags & VoidMask) != 0; } |
196 | |
42cf5bc1 |
197 | //! true if xmax-xmin < tol. |
198 | Standard_EXPORT Standard_Boolean IsXThin (const Standard_Real tol) const; |
199 | |
200 | //! true if ymax-ymin < tol. |
201 | Standard_EXPORT Standard_Boolean IsYThin (const Standard_Real tol) const; |
202 | |
203 | //! true if zmax-zmin < tol. |
204 | Standard_EXPORT Standard_Boolean IsZThin (const Standard_Real tol) const; |
205 | |
206 | //! Returns true if IsXThin, IsYThin and IsZThin are all true, |
207 | //! i.e. if the box is thin in all three dimensions. |
208 | Standard_EXPORT Standard_Boolean IsThin (const Standard_Real tol) const; |
209 | |
210 | //! Returns a bounding box which is the result of applying the |
211 | //! transformation T to this bounding box. |
212 | //! Warning |
213 | //! Applying a geometric transformation (for example, a |
214 | //! rotation) to a bounding box generally increases its |
215 | //! dimensions. This is not optimal for algorithms which use it. |
0be7dbe1 |
216 | Standard_EXPORT Standard_NODISCARD Bnd_Box Transformed (const gp_Trsf& T) const; |
42cf5bc1 |
217 | |
218 | //! Adds the box <Other> to <me>. |
219 | Standard_EXPORT void Add (const Bnd_Box& Other); |
220 | |
221 | //! Adds a Pnt to the box. |
222 | Standard_EXPORT void Add (const gp_Pnt& P); |
223 | |
224 | //! Extends <me> from the Pnt <P> in the direction <D>. |
225 | Standard_EXPORT void Add (const gp_Pnt& P, const gp_Dir& D); |
226 | |
227 | //! Extends the Box in the given Direction, i.e. adds |
228 | //! an half-line. The box may become infinite in |
229 | //! 1,2 or 3 directions. |
230 | Standard_EXPORT void Add (const gp_Dir& D); |
231 | |
232 | //! Returns True if the Pnt is out the box. |
233 | Standard_EXPORT Standard_Boolean IsOut (const gp_Pnt& P) const; |
234 | |
235 | //! Returns False if the line intersects the box. |
236 | Standard_EXPORT Standard_Boolean IsOut (const gp_Lin& L) const; |
237 | |
238 | //! Returns False if the plane intersects the box. |
239 | Standard_EXPORT Standard_Boolean IsOut (const gp_Pln& P) const; |
240 | |
241 | //! Returns False if the <Box> intersects or is inside <me>. |
242 | Standard_EXPORT Standard_Boolean IsOut (const Bnd_Box& Other) const; |
243 | |
244 | //! Returns False if the transformed <Box> intersects |
245 | //! or is inside <me>. |
246 | Standard_EXPORT Standard_Boolean IsOut (const Bnd_Box& Other, const gp_Trsf& T) const; |
247 | |
248 | //! Returns False if the transformed <Box> intersects |
249 | //! or is inside the transformed box <me>. |
250 | Standard_EXPORT Standard_Boolean IsOut (const gp_Trsf& T1, const Bnd_Box& Other, const gp_Trsf& T2) const; |
251 | |
252 | //! Returns False if the flat band lying between two parallel |
253 | //! lines represented by their reference points <P1>, <P2> and |
254 | //! direction <D> intersects the box. |
255 | Standard_EXPORT Standard_Boolean IsOut (const gp_Pnt& P1, const gp_Pnt& P2, const gp_Dir& D) const; |
256 | |
257 | //! Computes the minimum distance between two boxes. |
258 | Standard_EXPORT Standard_Real Distance (const Bnd_Box& Other) const; |
259 | |
260 | Standard_EXPORT void Dump() const; |
42cf5bc1 |
261 | |
dde68833 |
262 | //! Computes the squared diagonal of me. |
263 | Standard_Real SquareExtent() const |
264 | { |
265 | if (IsVoid()) |
266 | { |
267 | return 0.0; |
268 | } |
42cf5bc1 |
269 | |
dde68833 |
270 | const Standard_Real aDx = Xmax - Xmin + Gap + Gap; |
271 | const Standard_Real aDy = Ymax - Ymin + Gap + Gap; |
272 | const Standard_Real aDz = Zmax - Zmin + Gap + Gap; |
273 | return aDx * aDx + aDy * aDy + aDz * aDz; |
274 | } |
42cf5bc1 |
275 | |
04f0f1b0 |
276 | //! Returns a finite part of an infinite bounding box (returns self if this is already finite box). |
277 | //! This can be a Void box in case if its sides has been defined as infinite (Open) without adding any finite points. |
278 | //! WARNING! This method relies on Open flags, the infinite points added using Add() method will be returned as is. |
279 | Bnd_Box FinitePart() const |
280 | { |
281 | if (!HasFinitePart()) |
282 | { |
283 | return Bnd_Box(); |
284 | } |
285 | |
286 | Bnd_Box aBox; |
287 | aBox.Update (Xmin, Ymin, Zmin, Xmax, Ymax, Zmax); |
288 | aBox.SetGap (Gap); |
289 | return aBox; |
290 | } |
291 | |
292 | //! Returns TRUE if this box has finite part. |
293 | Standard_Boolean HasFinitePart() const |
294 | { |
295 | return !IsVoid() |
296 | && Xmax >= Xmin; |
297 | } |
298 | |
42cf5bc1 |
299 | protected: |
300 | |
dde68833 |
301 | //! Bit flags. |
302 | enum MaskFlags |
303 | { |
304 | VoidMask = 0x01, |
305 | XminMask = 0x02, |
306 | XmaxMask = 0x04, |
307 | YminMask = 0x08, |
308 | YmaxMask = 0x10, |
309 | ZminMask = 0x20, |
310 | ZmaxMask = 0x40, |
311 | WholeMask = 0x7e |
312 | }; |
42cf5bc1 |
313 | |
314 | private: |
315 | |
42cf5bc1 |
316 | Standard_Real Xmin; |
317 | Standard_Real Xmax; |
318 | Standard_Real Ymin; |
319 | Standard_Real Ymax; |
320 | Standard_Real Zmin; |
321 | Standard_Real Zmax; |
322 | Standard_Real Gap; |
323 | Standard_Integer Flags; |
324 | |
42cf5bc1 |
325 | }; |
326 | |
42cf5bc1 |
327 | #endif // _Bnd_Box_HeaderFile |