OpenWalnut  1.4.0
WBoundingBox.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WBOUNDINGBOX_H
26 #define WBOUNDINGBOX_H
27 
28 #include <ostream>
29 #include <iomanip> // for setprecision
30 #include <cmath> // std::sqrt
31 
32 #include <osg/BoundingBox>
33 
34 #include "exceptions/WInvalidBoundingBox.h"
35 //#include "math/linearAlgebra/WLinearAlgebra.h"
36 
37 /**
38  * Represents a \e axis \e parallel bounding box and provides some useful operations with them.
39  *
40  * \note Reason for subclassing: We don't want \c _min and \c _max member variables to be public.
41  * \note Reason for not having a \e private osg::BoundingBox member is, we don't have to wrap many
42  * member functions and can make use of the using directive. A downside on this is, we cannot
43  * automatical cast to osg::BoundingBox even if we provide a cast operator! Hence when we need this
44  * we will provide a toOsgBB() member function.
45  */
46 template< class VT >
47 class WBoundingBoxImpl : private osg::BoundingBoxImpl< VT >
48 {
49 public:
50  /**
51  * Vertex type for min and max positions of this box.
52  */
53  typedef typename osg::BoundingBoxImpl< VT >::vec_type vec_type;
54 
55  /**
56  * Value type of the vertex type for example double, float, etc.
57  */
58  typedef typename osg::BoundingBoxImpl< VT >::value_type value_type;
59 
60  /**
61  * Default constructor.
62  */
64 
65  /**
66  * Wrapps the component wise bounding box constructor from osg::BoundingBox.
67  *
68  * \param xmin Minimal x coordinate
69  * \param ymin Minimal y coordinate
70  * \param zmin Minimal z coordinate
71  * \param xmax Maximal x coordinate
72  * \param ymax Maximal y coordinate
73  * \param zmax Maximal z coordinate
74  */
75  WBoundingBoxImpl( value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax );
76 
77  /**
78  * Constructs a bounding box by min and max positions.
79  *
80  * \param min Position containing minx miny and minz coordinates.
81  * \param max Position containing maxx maxy and maxz coordinates.
82  */
83  WBoundingBoxImpl( const vec_type& min, const vec_type& max );
84 
85  /**
86  * Copy construct using a given bounding box
87  *
88  * \param bb the source bb
89  */
91 
92  /**
93  * Create BoundinmgBox using a given sphere.
94  *
95  * \param bs the sphere
96  */
97  explicit WBoundingBoxImpl( const osg::BoundingSphereImpl< VT >& bs );
98 
99  /**
100  * Destructs this instance.
101  */
102  virtual ~WBoundingBoxImpl();
103 
104  /**
105  * Resets this box to an initial state where max is FLT_MIN and min FLT_MAX.
106  *
107  * \note This is a wrapper call to osg::BoundingBoxImpl< VT >::init()
108  */
109  void reset();
110 
111  using osg::BoundingBoxImpl< VT >::valid;
112  using osg::BoundingBoxImpl< VT >::set;
113  using osg::BoundingBoxImpl< VT >::xMin;
114  using osg::BoundingBoxImpl< VT >::yMin;
115  using osg::BoundingBoxImpl< VT >::zMin;
116  using osg::BoundingBoxImpl< VT >::xMax;
117  using osg::BoundingBoxImpl< VT >::yMax;
118  using osg::BoundingBoxImpl< VT >::zMax;
119  using osg::BoundingBoxImpl< VT >::center;
120  using osg::BoundingBoxImpl< VT >::radius;
121 
122  /**
123  * Calculates and returns the squared length of the bounding box radius.
124  *
125  * \note This is a wrapper call to osg::BoundingBoxImpl< VT >::radius2()
126  *
127  * \return squared bbox radius
128  */
129  value_type radiusSquare() const;
130 
131  using osg::BoundingBoxImpl< VT >::corner;
132 
133  /**
134  * Explicit type conversion function to use a WBoundingBox as osg::BoundingBox.
135  *
136  * \return A copy of this bounding box as osg::BoundingBox.
137  */
138  osg::BoundingBox toOSGBB() const;
139 
140  using osg::BoundingBoxImpl< VT >::expandBy;
141 
142  /**
143  * Expands this bounding box to include the given bounding box.
144  *
145  * \param bb The other bounding box.
146  */
147  void expandBy( const WBoundingBoxImpl< VT > &bb );
148 
149  /**
150  * Checks for intersection of this bounding box with the specified bounding box.
151  *
152  * \param bb The other bouding box to tetst with.
153  *
154  * \return True if they intersect, false otherwise.
155  */
156  bool intersects( const WBoundingBoxImpl< VT > &bb ) const;
157 
158  /**
159  * Computes the minimal distance of tow axis parallel bounding boxes.
160  *
161  * \param bb The other bounding box.
162  *
163  * \return Zero if they intersect, otherwise their minimal distance.
164  */
165  value_type minDistance( const WBoundingBoxImpl< VT > &bb ) const;
166 
167  using osg::BoundingBoxImpl< VT >::contains;
168 
169  /**
170  * Gives the front lower left aka minimum corner.
171  *
172  * \return Minimum corner.
173  */
174  const vec_type& getMin() const;
175 
176  /**
177  * Gives the back upper right aka maximum corner.
178  *
179  * \return Maximum corner.
180  */
181  const vec_type& getMax() const;
182 
183 protected:
184 private:
185  /**
186  * Checks if the two given intervals intersect and computes the distance between them.
187  *
188  * \param a0 lower bound of the first interval
189  * \param a1 upper bound of the first interval
190  * \param b0 lower bound of the second interval
191  * \param b1 upper bound if the second interval
192  *
193  * \return The distance between those intervals if they don't overlap, zero otherwise
194  */
195  double intervalDistance( double a0, double a1, double b0, double b1 ) const;
196 };
197 
198 template< class VT >
200  : osg::BoundingBoxImpl< VT >()
201 {
202 }
203 
204 template< class VT >
205 inline WBoundingBoxImpl< VT >::WBoundingBoxImpl( value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax ) // NOLINT line length
206  : osg::BoundingBoxImpl< VT >( xmin, ymin, zmin, xmax, ymax, zmax )
207 {
208 }
209 
210 template< class VT >
212  : osg::BoundingBoxImpl< VT >( min, max )
213 {
214 }
215 
216 template< class VT >
217 inline WBoundingBoxImpl< VT >::WBoundingBoxImpl( const osg::BoundingSphereImpl< VT >& bs )
218  : osg::BoundingBoxImpl< VT >( bs.center() - VT( bs.radius(), bs.radius(), bs.radius() ) ,
219  bs.center() + VT( bs.radius(), bs.radius(), bs.radius() ) )
220 {
221 }
222 
223 template< class VT >
225  : osg::BoundingBoxImpl< VT >( bb )
226 {
227 }
228 
229 template< class VT >
231 {
232 }
233 
234 template< class VT >
236 {
237  this->init();
238 }
239 
240 template< class VT >
242 {
243  return this->radius2();
244 }
245 
246 template< class VT >
247 inline osg::BoundingBox WBoundingBoxImpl< VT >::toOSGBB() const
248 {
249  return osg::BoundingBox( osg::BoundingBoxImpl< VT >::_min, osg::BoundingBoxImpl< VT >::_max );
250 }
251 
252 template< class VT >
254 {
255  osg::BoundingBoxImpl< VT >::expandBy( bb );
256 }
257 
258 template< class VT >
260 {
261  return osg::BoundingBoxImpl< VT >::intersects( bb );
262 }
263 
264 template< class VT >
265 inline double WBoundingBoxImpl< VT >::intervalDistance( double a0, double a1, double b0, double b1 ) const
266 {
267  if( a1 < b0 )
268  {
269  return b0 - a1;
270  }
271  else if( b1 < a0 )
272  {
273  return a0 - b1;
274  }
275  return 0.0;
276 }
277 
278 template< class VT >
280 {
281  // test if they are valid
282  if( !valid() || !bb.valid() )
283  {
284  throw WInvalidBoundingBox( "One of the both bounding boxes inside minDistance computation is not valid." );
285  }
286 
287  double dx = intervalDistance( xMin(), xMax(), bb.xMin(), bb.xMax() );
288  double dy = intervalDistance( yMin(), yMax(), bb.yMin(), bb.yMax() );
289  double dz = intervalDistance( zMin(), zMax(), bb.zMin(), bb.zMax() );
290  if( dx == 0.0 && dy == 0.0 && dz == 0.0 )
291  {
292  return 0.0;
293  }
294  return std::sqrt( dx * dx + dy * dy + dz * dz );
295 }
296 
297 /**
298  * Output operator for the WBoundingBoxImpl class.
299  *
300  * \param out Output stream operator
301  * \param bb The box which should be streamed out
302  *
303  * \return reference to the output stream
304  */
305 template< class VT >
306 inline std::ostream& operator<<( std::ostream& out, const WBoundingBoxImpl< VT >& bb )
307 {
308  out << std::scientific << std::setprecision( 16 );
309  out << "AABB( min: " << bb.xMin() << ", " << bb.yMin() << ", " << bb.zMin();
310  out << " max: " << bb.xMax() << ", " << bb.yMax() << ", " << bb.zMax() << " )";
311  return out;
312 }
313 
314 template< class VT >
316 {
317  return osg::BoundingBoxImpl< VT >::_min;
318 }
319 
320 template< class VT >
322 {
323  return osg::BoundingBoxImpl< VT >::_max;
324 }
325 
327 
328 #endif // WBOUNDINGBOX_H