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_Types.hxx>
21 //! Defines axis aligned bounding box (AABB) based on BVH vectors.
22 //! \tparam T Numeric data type
23 //! \tparam N Vector dimension
24 template<class T, int N>
29 typedef typename BVH::VectorType<T, N>::Type BVH_VecNt;
33 //! Creates uninitialized bounding box.
34 BVH_Box() : myIsInited (Standard_False) {}
36 //! Creates bounding box of given point.
37 BVH_Box (const BVH_VecNt& thePoint)
38 : myMinPoint (thePoint),
39 myMaxPoint (thePoint),
40 myIsInited (Standard_True) {}
42 //! Creates copy of another bounding box.
43 BVH_Box (const BVH_Box& theBox)
44 : myMinPoint (theBox.myMinPoint),
45 myMaxPoint (theBox.myMaxPoint),
46 myIsInited (theBox.myIsInited) {}
48 //! Creates bounding box from corner points.
49 BVH_Box (const BVH_VecNt& theMinPoint,
50 const BVH_VecNt& theMaxPoint)
51 : myMinPoint (theMinPoint),
52 myMaxPoint (theMaxPoint),
53 myIsInited (Standard_True) {}
57 //! Clears bounding box.
60 //! Is bounding box valid?
61 Standard_Boolean IsValid() const;
63 //! Appends new point to the bounding box.
64 void Add (const BVH_VecNt& thePoint);
66 //! Combines bounding box with another one.
67 void Combine (const BVH_Box& theVolume);
69 //! Returns minimum point of bounding box.
70 const BVH_VecNt& CornerMin() const;
72 //! Returns maximum point of bounding box.
73 const BVH_VecNt& CornerMax() const;
75 //! Returns minimum point of bounding box.
76 BVH_VecNt& CornerMin();
78 //! Returns maximum point of bounding box.
79 BVH_VecNt& CornerMax();
81 //! Returns surface area of bounding box.
84 //! Returns diagonal of bounding box.
85 BVH_VecNt Size() const;
87 //! Returns center of bounding box.
88 BVH_VecNt Center() const;
90 //! Returns center of bounding box along the given axis.
91 T Center (const Standard_Integer theAxis) const;
95 BVH_VecNt myMinPoint; //!< Minimum point of bounding box
96 BVH_VecNt myMaxPoint; //!< Maximum point of bounding box
97 Standard_Boolean myIsInited; //!< Is bounding box initialized?
103 //! Tool class for calculating box center along the given axis.
104 //! \tparam T Numeric data type
105 //! \tparam N Vector dimension
106 template<class T, int N>
113 struct CenterAxis<T, 2>
115 static T Center (const BVH_Box<T, 2>& theBox, const Standard_Integer theAxis)
119 return (theBox.CornerMin().x() + theBox.CornerMax().x()) * static_cast<T> (0.5);
121 else if (theAxis == 1)
123 return (theBox.CornerMin().y() + theBox.CornerMax().y()) * static_cast<T> (0.5);
125 return static_cast<T> (0.0);
130 struct CenterAxis<T, 3>
132 static T Center (const BVH_Box<T, 3>& theBox, const Standard_Integer theAxis)
136 return (theBox.CornerMin().x() + theBox.CornerMax().x()) * static_cast<T> (0.5);
138 else if (theAxis == 1)
140 return (theBox.CornerMin().y() + theBox.CornerMax().y()) * static_cast<T> (0.5);
142 else if (theAxis == 2)
144 return (theBox.CornerMin().z() + theBox.CornerMax().z()) * static_cast<T> (0.5);
146 return static_cast<T> (0.0);
151 struct CenterAxis<T, 4>
153 static T Center (const BVH_Box<T, 4>& theBox, const Standard_Integer theAxis)
157 return (theBox.CornerMin().x() + theBox.CornerMax().x()) * static_cast<T> (0.5);
159 else if (theAxis == 1)
161 return (theBox.CornerMin().y() + theBox.CornerMax().y()) * static_cast<T> (0.5);
163 else if (theAxis == 2)
165 return (theBox.CornerMin().z() + theBox.CornerMax().z()) * static_cast<T> (0.5);
167 return static_cast<T> (0.0);
171 //! Tool class for calculating surface area of the box.
172 //! \tparam T Numeric data type
173 //! \tparam N Vector dimension
174 template<class T, int N>
175 struct SurfaceCalculator
181 struct SurfaceCalculator<T, 2>
183 static T Area (const typename BVH_Box<T, 2>::BVH_VecNt& theSize)
185 return theSize.x() * theSize.y();
190 struct SurfaceCalculator<T, 3>
192 static T Area (const typename BVH_Box<T, 3>::BVH_VecNt& theSize)
194 return ( theSize.x() * theSize.y() +
195 theSize.x() * theSize.z() +
196 theSize.z() * theSize.y() ) * static_cast<T> (2.0);
201 struct SurfaceCalculator<T, 4>
203 static T Area (const typename BVH_Box<T, 4>::BVH_VecNt& theSize)
205 return ( theSize.x() * theSize.y() +
206 theSize.x() * theSize.z() +
207 theSize.z() * theSize.y() ) * static_cast<T> (2.0);
211 //! Tool class for calculate component-wise vector minimum
212 //! and maximum (optimized version).
213 //! \tparam T Numeric data type
214 //! \tparam N Vector dimension
215 template<class T, int N>
218 typedef typename BVH::VectorType<T, N>::Type BVH_VecNt;
220 static void CwiseMin (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
222 theVec1.x() = Min (theVec1.x(), theVec2.x());
223 theVec1.y() = Min (theVec1.y(), theVec2.y());
224 theVec1.z() = Min (theVec1.z(), theVec2.z());
227 static void CwiseMax (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
229 theVec1.x() = Max (theVec1.x(), theVec2.x());
230 theVec1.y() = Max (theVec1.y(), theVec2.y());
231 theVec1.z() = Max (theVec1.z(), theVec2.z());
236 struct BoxMinMax<T, 2>
238 typedef typename BVH::VectorType<T, 2>::Type BVH_VecNt;
240 static void CwiseMin (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
242 theVec1.x() = Min (theVec1.x(), theVec2.x());
243 theVec1.y() = Min (theVec1.y(), theVec2.y());
246 static void CwiseMax (BVH_VecNt& theVec1, const BVH_VecNt& theVec2)
248 theVec1.x() = Max (theVec1.x(), theVec2.x());
249 theVec1.y() = Max (theVec1.y(), theVec2.y());
254 #include <BVH_Box.lxx>
256 #endif // _BVH_Box_Header