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