OpenWalnut  1.3.1
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  * Destructs this instance.
87  */
88  virtual ~WBoundingBoxImpl();
89 
90  /**
91  * Resets this box to an initial state where max is FLT_MIN and min FLT_MAX.
92  *
93  * \note This is a wrapper call to osg::BoundingBoxImpl< VT >::init()
94  */
95  void reset();
96 
97  using osg::BoundingBoxImpl< VT >::valid;
98  using osg::BoundingBoxImpl< VT >::set;
99  using osg::BoundingBoxImpl< VT >::xMin;
100  using osg::BoundingBoxImpl< VT >::yMin;
101  using osg::BoundingBoxImpl< VT >::zMin;
102  using osg::BoundingBoxImpl< VT >::xMax;
103  using osg::BoundingBoxImpl< VT >::yMax;
104  using osg::BoundingBoxImpl< VT >::zMax;
105  using osg::BoundingBoxImpl< VT >::center;
106  using osg::BoundingBoxImpl< VT >::radius;
107 
108  /**
109  * Calculates and returns the squared length of the bounding box radius.
110  *
111  * \note This is a wrapper call to osg::BoundingBoxImpl< VT >::radius2()
112  *
113  * \return squared bbox radius
114  */
115  value_type radiusSquare() const;
116 
117  using osg::BoundingBoxImpl< VT >::corner;
118 
119  /**
120  * Explicit type conversion function to use a WBoundingBox as osg::BoundingBox.
121  *
122  * \return A copy of this bounding box as osg::BoundingBox.
123  */
124  osg::BoundingBox toOSGBB() const;
125 
126  using osg::BoundingBoxImpl< VT >::expandBy;
127 
128  /**
129  * Expands this bounding box to include the given bounding box.
130  *
131  * \param bb The other bounding box.
132  */
133  void expandBy( const WBoundingBoxImpl< VT > &bb );
134 
135  /**
136  * Checks for intersection of this bounding box with the specified bounding box.
137  *
138  * \param bb The other bouding box to tetst with.
139  *
140  * \return True if they intersect, false otherwise.
141  */
142  bool intersects( const WBoundingBoxImpl< VT > &bb ) const;
143 
144  /**
145  * Computes the minimal distance of tow axis parallel bounding boxes.
146  *
147  * \param bb The other bounding box.
148  *
149  * \return Zero if they intersect, otherwise their minimal distance.
150  */
151  value_type minDistance( const WBoundingBoxImpl< VT > &bb ) const;
152 
153  using osg::BoundingBoxImpl< VT >::contains;
154 
155  /**
156  * Gives the front lower left aka minimum corner.
157  *
158  * \return Minimum corner.
159  */
160  const vec_type& getMin() const;
161 
162  /**
163  * Gives the back upper right aka maximum corner.
164  *
165  * \return Maximum corner.
166  */
167  const vec_type& getMax() const;
168 
169 protected:
170 private:
171 };
172 
173 template< class VT >
175  : osg::BoundingBoxImpl< VT >()
176 {
177 }
178 
179 template< class VT >
180 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
181  : osg::BoundingBoxImpl< VT >( xmin, ymin, zmin, xmax, ymax, zmax )
182 {
183 }
184 
185 template< class VT >
187  : osg::BoundingBoxImpl< VT >( min, max )
188 {
189 }
190 
191 template< class VT >
193 {
194 }
195 
196 template< class VT >
198 {
199  this->init();
200 }
201 
202 template< class VT >
204 {
205  return this->raidus2();
206 }
207 
208 template< class VT >
209 inline osg::BoundingBox WBoundingBoxImpl< VT >::toOSGBB() const
210 {
211  return osg::BoundingBox( osg::BoundingBoxImpl< VT >::_min, osg::BoundingBoxImpl< VT >::_max );
212 }
213 
214 template< class VT >
216 {
217  osg::BoundingBoxImpl< VT >::expandBy( bb );
218 }
219 
220 template< class VT >
222 {
223  return osg::BoundingBoxImpl< VT >::intersects( bb );
224 }
225 
226 /**
227  * Anonymous namespace, just to be DRY in minDistance.
228  */
229 namespace
230 {
231  /**
232  * Checks if the two given intervals intersect and computes the distance between them.
233  *
234  * \param a0 lower bound of the first interval
235  * \param a1 upper bound of the first interval
236  * \param b0 lower bound of the second interval
237  * \param b1 upper bound if the second interval
238  *
239  * \return The distance between those intervals if they don't overlap, zero otherwise
240  */
241  inline double intervalDistance( double a0, double a1, double b0, double b1 )
242  {
243  if( a1 < b0 )
244  {
245  return b0 - a1;
246  }
247  else if( b1 < a0 )
248  {
249  return a0 - b1;
250  }
251  return 0.0;
252  }
253 }
254 
255 template< class VT >
257 {
258  // test if they are valid
259  if( !valid() || !bb.valid() )
260  {
261  throw WInvalidBoundingBox( "One of the both bounding boxes inside minDistance computation is not valid." );
262  }
263 
264  double dx = intervalDistance( xMin(), xMax(), bb.xMin(), bb.xMax() );
265  double dy = intervalDistance( yMin(), yMax(), bb.yMin(), bb.yMax() );
266  double dz = intervalDistance( zMin(), zMax(), bb.zMin(), bb.zMax() );
267  if( dx == 0.0 && dy == 0.0 && dz == 0.0 )
268  {
269  return 0.0;
270  }
271  return std::sqrt( dx * dx + dy * dy + dz * dz );
272 }
273 
274 /**
275  * Output operator for the WBoundingBoxImpl class.
276  *
277  * \param out Output stream operator
278  * \param bb The box which should be streamed out
279  *
280  * \return reference to the output stream
281  */
282 template< class VT >
283 inline std::ostream& operator<<( std::ostream& out, const WBoundingBoxImpl< VT >& bb )
284 {
285  out << std::scientific << std::setprecision( 16 );
286  out << "AABB( min: " << bb.xMin() << ", " << bb.yMin() << ", " << bb.zMin();
287  out << " max: " << bb.xMax() << ", " << bb.yMax() << ", " << bb.zMax() << " )";
288  return out;
289 }
290 
291 template< class VT >
293 {
294  return osg::BoundingBoxImpl< VT >::_min;
295 }
296 
297 template< class VT >
299 {
300  return osg::BoundingBoxImpl< VT >::_max;
301 }
302 
304 
305 #endif // WBOUNDINGBOX_H