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 BVH_VecNt myMinPoint; //!< Minimum point of bounding box
114 BVH_VecNt myMaxPoint; //!< Maximum point of bounding box
115 Standard_Boolean myIsInited; //!< Is bounding box initialized?
121 //! Tool class for calculating box center along the given axis.
122 //! \tparam T Numeric data type
123 //! \tparam N Vector dimension
124 template<class T, int N>
131 struct CenterAxis<T, 2>
133 static T Center (const BVH_Box<T, 2>& theBox, const Standard_Integer theAxis)
137 return (theBox.CornerMin().x() + theBox.CornerMax().x()) * static_cast<T> (0.5);
139 else if (theAxis == 1)
141 return (theBox.CornerMin().y() + theBox.CornerMax().y()) * static_cast<T> (0.5);
143 return static_cast<T> (0.0);
148 struct CenterAxis<T, 3>
150 static T Center (const BVH_Box<T, 3>& theBox, const Standard_Integer theAxis)
154 return (theBox.CornerMin().x() + theBox.CornerMax().x()) * static_cast<T> (0.5);
156 else if (theAxis == 1)
158 return (theBox.CornerMin().y() + theBox.CornerMax().y()) * static_cast<T> (0.5);
160 else if (theAxis == 2)
162 return (theBox.CornerMin().z() + theBox.CornerMax().z()) * static_cast<T> (0.5);
164 return static_cast<T> (0.0);
169 struct CenterAxis<T, 4>
171 static T Center (const BVH_Box<T, 4>& theBox, const Standard_Integer theAxis)
175 return (theBox.CornerMin().x() + theBox.CornerMax().x()) * static_cast<T> (0.5);
177 else if (theAxis == 1)
179 return (theBox.CornerMin().y() + theBox.CornerMax().y()) * static_cast<T> (0.5);
181 else if (theAxis == 2)
183 return (theBox.CornerMin().z() + theBox.CornerMax().z()) * static_cast<T> (0.5);
185 return static_cast<T> (0.0);
189 //! Tool class for calculating surface area of the box.
190 //! \tparam T Numeric data type
191 //! \tparam N Vector dimension
192 template<class T, int N>
193 struct SurfaceCalculator
199 struct SurfaceCalculator<T, 2>
201 static T Area (const typename BVH_Box<T, 2>::BVH_VecNt& theSize)
203 const T anArea = theSize.x() * theSize.y();
205 if (anArea < std::numeric_limits<T>::epsilon())
207 return theSize.x() + theSize.y();
215 struct SurfaceCalculator<T, 3>
217 static T Area (const typename BVH_Box<T, 3>::BVH_VecNt& theSize)
219 const T anArea = ( theSize.x() * theSize.y() +
220 theSize.x() * theSize.z() +
221 theSize.z() * theSize.y() ) * static_cast<T> (2.0);
223 if (anArea < std::numeric_limits<T>::epsilon())
235 struct SurfaceCalculator<T, 4>
237 static T Area (const typename BVH_Box<T, 4>::BVH_VecNt& theSize)
239 const T anArea = ( theSize.x() * theSize.y() +
240 theSize.x() * theSize.z() +
241 theSize.z() * theSize.y() ) * static_cast<T> (2.0);
243 if (anArea < std::numeric_limits<T>::epsilon())
254 //! Tool class for calculate component-wise vector minimum
255 //! and maximum (optimized version).
256 //! \tparam T Numeric data type
257 //! \tparam N Vector dimension
258 template<class T, int N>
261 typedef typename BVH::VectorType<T, N>::Type BVH_VecNt;
263 static void CwiseMin (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
265 theVec1.x() = Min (theVec1.x(), theVec2.x());
266 theVec1.y() = Min (theVec1.y(), theVec2.y());
267 theVec1.z() = Min (theVec1.z(), theVec2.z());
270 static void CwiseMax (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
272 theVec1.x() = Max (theVec1.x(), theVec2.x());
273 theVec1.y() = Max (theVec1.y(), theVec2.y());
274 theVec1.z() = Max (theVec1.z(), theVec2.z());
279 struct BoxMinMax<T, 2>
281 typedef typename BVH::VectorType<T, 2>::Type BVH_VecNt;
283 static void CwiseMin (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
285 theVec1.x() = Min (theVec1.x(), theVec2.x());
286 theVec1.y() = Min (theVec1.y(), theVec2.y());
289 static void CwiseMax (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
291 theVec1.x() = Max (theVec1.x(), theVec2.x());
292 theVec1.y() = Max (theVec1.y(), theVec2.y());
297 // =======================================================================
298 // function : Combine
300 // =======================================================================
301 template<class T, int N>
302 void BVH_Box<T, N>::Combine (const BVH_Box& theBox)
304 if (theBox.myIsInited)
308 myMinPoint = theBox.myMinPoint;
309 myMaxPoint = theBox.myMaxPoint;
310 myIsInited = Standard_True;
314 BVH::BoxMinMax<T, N>::CwiseMin (myMinPoint, theBox.myMinPoint);
315 BVH::BoxMinMax<T, N>::CwiseMax (myMaxPoint, theBox.myMaxPoint);
320 // =======================================================================
323 // =======================================================================
324 template<class T, int N>
325 T BVH_Box<T, N>::Area() const
327 return !myIsInited ? static_cast<T> (0.0) : BVH::SurfaceCalculator<T, N>::Area (myMaxPoint - myMinPoint);
330 // =======================================================================
333 // =======================================================================
334 template<class T, int N>
335 T BVH_Box<T, N>::Center (const Standard_Integer theAxis) const
337 return BVH::CenterAxis<T, N>::Center (*this, theAxis);
340 #endif // _BVH_Box_Header