1 // Created on: 2013-12-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _BVH_Box_Header
17 #define _BVH_Box_Header
19 #include <BVH_Constants.hxx>
20 #include <BVH_Types.hxx>
21 #include <Standard_ShortReal.hxx>
25 //! Defines axis aligned bounding box (AABB) based on BVH vectors.
26 //! \tparam T Numeric data type
27 //! \tparam N Vector dimension
28 template<class T, int N>
33 typedef typename BVH::VectorType<T, N>::Type BVH_VecNt;
37 //! Creates uninitialized bounding box.
38 BVH_Box() : myIsInited (Standard_False) {}
40 //! Creates bounding box of given point.
41 BVH_Box (const BVH_VecNt& thePoint)
42 : myMinPoint (thePoint),
43 myMaxPoint (thePoint),
44 myIsInited (Standard_True) {}
46 //! Creates copy of another bounding box.
47 BVH_Box (const BVH_Box& theBox)
48 : myMinPoint (theBox.myMinPoint),
49 myMaxPoint (theBox.myMaxPoint),
50 myIsInited (theBox.myIsInited) {}
52 //! Creates bounding box from corner points.
53 BVH_Box (const BVH_VecNt& theMinPoint,
54 const BVH_VecNt& theMaxPoint)
55 : myMinPoint (theMinPoint),
56 myMaxPoint (theMaxPoint),
57 myIsInited (Standard_True) {}
61 //! Clears bounding box.
62 void Clear() { myIsInited = Standard_False; }
64 //! Is bounding box valid?
65 Standard_Boolean IsValid() const { return myIsInited; }
67 //! Appends new point to the bounding box.
68 void Add (const BVH_VecNt& thePoint)
72 myMinPoint = thePoint;
73 myMaxPoint = thePoint;
74 myIsInited = Standard_True;
78 myMinPoint = myMinPoint.cwiseMin (thePoint);
79 myMaxPoint = myMaxPoint.cwiseMax (thePoint);
83 //! Combines bounding box with another one.
84 void Combine (const BVH_Box& theBox);
86 //! Returns minimum point of bounding box.
87 const BVH_VecNt& CornerMin() const { return myMinPoint; }
89 //! Returns maximum point of bounding box.
90 const BVH_VecNt& CornerMax() const { return myMaxPoint; }
92 //! Returns minimum point of bounding box.
93 BVH_VecNt& CornerMin() { return myMinPoint; }
95 //! Returns maximum point of bounding box.
96 BVH_VecNt& CornerMax() { return myMaxPoint; }
98 //! Returns surface area of bounding box.
99 //! If the box is degenerated into line, returns the perimeter instead.
102 //! Returns diagonal of bounding box.
103 BVH_VecNt Size() const { return myMaxPoint - myMinPoint; }
105 //! Returns center of bounding box.
106 BVH_VecNt Center() const { return (myMinPoint + myMaxPoint) * static_cast<T> (0.5); }
108 //! Returns center of bounding box along the given axis.
109 T Center (const Standard_Integer theAxis) const;
113 //! Checks if the Box is out of the other box.
114 Standard_Boolean IsOut (const BVH_Box<T, N>& theOther) const
116 if (!theOther.IsValid())
117 return Standard_True;
119 return IsOut (theOther.myMinPoint, theOther.myMaxPoint);
122 //! Checks if the Box is out of the other box defined by two points.
123 Standard_Boolean IsOut (const BVH_VecNt& theMinPoint,
124 const BVH_VecNt& theMaxPoint) const
127 return Standard_True;
130 for (int i = 0; i < n; ++i)
132 if (myMinPoint[i] > theMaxPoint[i] ||
133 myMaxPoint[i] < theMinPoint[i])
134 return Standard_True;
136 return Standard_False;
139 //! Checks if the Box fully contains the other box.
140 Standard_Boolean Contains (const BVH_Box<T, N>& theOther,
141 Standard_Boolean& hasOverlap) const
143 hasOverlap = Standard_False;
144 if (!theOther.IsValid())
145 return Standard_False;
147 return Contains (theOther.myMinPoint, theOther.myMaxPoint, hasOverlap);
150 //! Checks if the Box is fully contains the other box.
151 Standard_Boolean Contains (const BVH_VecNt& theMinPoint,
152 const BVH_VecNt& theMaxPoint,
153 Standard_Boolean& hasOverlap) const
155 hasOverlap = Standard_False;
157 return Standard_False;
159 Standard_Boolean isInside = Standard_True;
162 for (int i = 0; i < n; ++i)
164 hasOverlap = (myMinPoint[i] <= theMaxPoint[i] &&
165 myMaxPoint[i] >= theMinPoint[i]);
167 return Standard_False;
169 isInside = isInside && (myMinPoint[i] <= theMinPoint[i] &&
170 myMaxPoint[i] >= theMaxPoint[i]);
175 //! Checks if the Point is out of the box.
176 Standard_Boolean IsOut (const BVH_VecNt& thePoint) const
179 return Standard_True;
182 for (int i = 0; i < n; ++i)
184 if (thePoint[i] < myMinPoint[i] ||
185 thePoint[i] > myMaxPoint[i])
186 return Standard_True;
188 return Standard_False;
194 BVH_VecNt myMinPoint; //!< Minimum point of bounding box
195 BVH_VecNt myMaxPoint; //!< Maximum point of bounding box
196 Standard_Boolean myIsInited; //!< Is bounding box initialized?
202 //! Tool class for calculating box center along the given axis.
203 //! \tparam T Numeric data type
204 //! \tparam N Vector dimension
205 template<class T, int N>
212 struct CenterAxis<T, 2>
214 static T Center (const BVH_Box<T, 2>& theBox, const Standard_Integer theAxis)
218 return (theBox.CornerMin().x() + theBox.CornerMax().x()) * static_cast<T> (0.5);
220 else if (theAxis == 1)
222 return (theBox.CornerMin().y() + theBox.CornerMax().y()) * static_cast<T> (0.5);
224 return static_cast<T> (0.0);
229 struct CenterAxis<T, 3>
231 static T Center (const BVH_Box<T, 3>& theBox, const Standard_Integer theAxis)
235 return (theBox.CornerMin().x() + theBox.CornerMax().x()) * static_cast<T> (0.5);
237 else if (theAxis == 1)
239 return (theBox.CornerMin().y() + theBox.CornerMax().y()) * static_cast<T> (0.5);
241 else if (theAxis == 2)
243 return (theBox.CornerMin().z() + theBox.CornerMax().z()) * static_cast<T> (0.5);
245 return static_cast<T> (0.0);
250 struct CenterAxis<T, 4>
252 static T Center (const BVH_Box<T, 4>& theBox, const Standard_Integer theAxis)
256 return (theBox.CornerMin().x() + theBox.CornerMax().x()) * static_cast<T> (0.5);
258 else if (theAxis == 1)
260 return (theBox.CornerMin().y() + theBox.CornerMax().y()) * static_cast<T> (0.5);
262 else if (theAxis == 2)
264 return (theBox.CornerMin().z() + theBox.CornerMax().z()) * static_cast<T> (0.5);
266 return static_cast<T> (0.0);
270 //! Tool class for calculating surface area of the box.
271 //! \tparam T Numeric data type
272 //! \tparam N Vector dimension
273 template<class T, int N>
274 struct SurfaceCalculator
280 struct SurfaceCalculator<T, 2>
282 static T Area (const typename BVH_Box<T, 2>::BVH_VecNt& theSize)
284 const T anArea = theSize.x() * theSize.y();
286 if (anArea < std::numeric_limits<T>::epsilon())
288 return theSize.x() + theSize.y();
296 struct SurfaceCalculator<T, 3>
298 static T Area (const typename BVH_Box<T, 3>::BVH_VecNt& theSize)
300 const T anArea = ( theSize.x() * theSize.y() +
301 theSize.x() * theSize.z() +
302 theSize.z() * theSize.y() ) * static_cast<T> (2.0);
304 if (anArea < std::numeric_limits<T>::epsilon())
316 struct SurfaceCalculator<T, 4>
318 static T Area (const typename BVH_Box<T, 4>::BVH_VecNt& theSize)
320 const T anArea = ( theSize.x() * theSize.y() +
321 theSize.x() * theSize.z() +
322 theSize.z() * theSize.y() ) * static_cast<T> (2.0);
324 if (anArea < std::numeric_limits<T>::epsilon())
335 //! Tool class for calculate component-wise vector minimum
336 //! and maximum (optimized version).
337 //! \tparam T Numeric data type
338 //! \tparam N Vector dimension
339 template<class T, int N>
342 typedef typename BVH::VectorType<T, N>::Type BVH_VecNt;
344 static void CwiseMin (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
346 theVec1.x() = Min (theVec1.x(), theVec2.x());
347 theVec1.y() = Min (theVec1.y(), theVec2.y());
348 theVec1.z() = Min (theVec1.z(), theVec2.z());
351 static void CwiseMax (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
353 theVec1.x() = Max (theVec1.x(), theVec2.x());
354 theVec1.y() = Max (theVec1.y(), theVec2.y());
355 theVec1.z() = Max (theVec1.z(), theVec2.z());
360 struct BoxMinMax<T, 2>
362 typedef typename BVH::VectorType<T, 2>::Type BVH_VecNt;
364 static void CwiseMin (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
366 theVec1.x() = Min (theVec1.x(), theVec2.x());
367 theVec1.y() = Min (theVec1.y(), theVec2.y());
370 static void CwiseMax (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
372 theVec1.x() = Max (theVec1.x(), theVec2.x());
373 theVec1.y() = Max (theVec1.y(), theVec2.y());
378 // =======================================================================
379 // function : Combine
381 // =======================================================================
382 template<class T, int N>
383 void BVH_Box<T, N>::Combine (const BVH_Box& theBox)
385 if (theBox.myIsInited)
389 myMinPoint = theBox.myMinPoint;
390 myMaxPoint = theBox.myMaxPoint;
391 myIsInited = Standard_True;
395 BVH::BoxMinMax<T, N>::CwiseMin (myMinPoint, theBox.myMinPoint);
396 BVH::BoxMinMax<T, N>::CwiseMax (myMaxPoint, theBox.myMaxPoint);
401 // =======================================================================
404 // =======================================================================
405 template<class T, int N>
406 T BVH_Box<T, N>::Area() const
408 return !myIsInited ? static_cast<T> (0.0) : BVH::SurfaceCalculator<T, N>::Area (myMaxPoint - myMinPoint);
411 // =======================================================================
414 // =======================================================================
415 template<class T, int N>
416 T BVH_Box<T, N>::Center (const Standard_Integer theAxis) const
418 return BVH::CenterAxis<T, N>::Center (*this, theAxis);
421 #endif // _BVH_Box_Header