OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BoundingBox.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSG_BOUNDINGBOX
15 #define OSG_BOUNDINGBOX 1
16 
17 #include <osg/Config>
18 #include <osg/Export>
19 #include <osg/Vec3>
20 #include <osg/Vec3d>
21 #include <float.h>
22 
23 namespace osg {
24 
25 template<typename VT>
27 
32 template<typename VT>
34 {
35  public:
36  typedef VT vec_type;
37  typedef typename VT::value_type value_type;
38 
40  vec_type _min;
42  vec_type _max;
43 
45  inline BoundingBoxImpl() :
46  _min(FLT_MAX,
47  FLT_MAX,
48  FLT_MAX),
49  _max(-FLT_MAX,
50  -FLT_MAX,
51  -FLT_MAX)
52  {}
53 
54  template<typename BT>
56  _min(bb._min),
57  _max(bb._max)
58  {}
59 
61  inline BoundingBoxImpl(value_type xmin, value_type ymin, value_type zmin,
62  value_type xmax, value_type ymax, value_type zmax) :
63  _min(xmin,ymin,zmin),
64  _max(xmax,ymax,zmax) {}
65 
67  inline BoundingBoxImpl(const vec_type& min,const vec_type& max) :
68  _min(min),
69  _max(max) {}
70 
72  inline void init()
73  {
74  _min.set(FLT_MAX,
75  FLT_MAX,
76  FLT_MAX);
77  _max.set(-FLT_MAX,
78  -FLT_MAX,
79  -FLT_MAX);
80  }
81 
82  inline bool operator == (const BoundingBoxImpl& rhs) const { return _min==rhs._min && _max==rhs._max; }
83  inline bool operator != (const BoundingBoxImpl& rhs) const { return _min!=rhs._min || _max!=rhs._max; }
84 
86  inline bool valid() const
87  {
88  return _max.x()>=_min.x() && _max.y()>=_min.y() && _max.z()>=_min.z();
89  }
90 
92  inline void set (value_type xmin, value_type ymin, value_type zmin,
93  value_type xmax, value_type ymax, value_type zmax)
94  {
95  _min.set(xmin,ymin,zmin);
96  _max.set(xmax,ymax,zmax);
97  }
98 
100  inline void set(const vec_type& min,const vec_type& max)
101  {
102  _min = min;
103  _max = max;
104  }
105 
106 
107  inline value_type& xMin() { return _min.x(); }
108  inline value_type xMin() const { return _min.x(); }
109 
110  inline value_type& yMin() { return _min.y(); }
111  inline value_type yMin() const { return _min.y(); }
112 
113  inline value_type& zMin() { return _min.z(); }
114  inline value_type zMin() const { return _min.z(); }
115 
116  inline value_type& xMax() { return _max.x(); }
117  inline value_type xMax() const { return _max.x(); }
118 
119  inline value_type& yMax() { return _max.y(); }
120  inline value_type yMax() const { return _max.y(); }
121 
122  inline value_type& zMax() { return _max.z(); }
123  inline value_type zMax() const { return _max.z(); }
124 
126  inline const vec_type center() const
127  {
128  return (_min+_max)*0.5;
129  }
130 
132  inline value_type radius() const
133  {
134  return sqrt(radius2());
135  }
136 
139  inline value_type radius2() const
140  {
141  return 0.25*((_max-_min).length2());
142  }
143 
149  inline const vec_type corner(unsigned int pos) const
150  {
151  return vec_type(pos&1?_max.x():_min.x(),pos&2?_max.y():_min.y(),pos&4?_max.z():_min.z());
152  }
153 
156  inline void expandBy(const vec_type& v)
157  {
158  if(v.x()<_min.x()) _min.x() = v.x();
159  if(v.x()>_max.x()) _max.x() = v.x();
160 
161  if(v.y()<_min.y()) _min.y() = v.y();
162  if(v.y()>_max.y()) _max.y() = v.y();
163 
164  if(v.z()<_min.z()) _min.z() = v.z();
165  if(v.z()>_max.z()) _max.z() = v.z();
166  }
167 
171  inline void expandBy(value_type x,value_type y,value_type z)
172  {
173  if(x<_min.x()) _min.x() = x;
174  if(x>_max.x()) _max.x() = x;
175 
176  if(y<_min.y()) _min.y() = y;
177  if(y>_max.y()) _max.y() = y;
178 
179  if(z<_min.z()) _min.z() = z;
180  if(z>_max.z()) _max.z() = z;
181  }
182 
185  void expandBy(const BoundingBoxImpl& bb)
186  {
187  if (!bb.valid()) return;
188 
189  if(bb._min.x()<_min.x()) _min.x() = bb._min.x();
190  if(bb._max.x()>_max.x()) _max.x() = bb._max.x();
191 
192  if(bb._min.y()<_min.y()) _min.y() = bb._min.y();
193  if(bb._max.y()>_max.y()) _max.y() = bb._max.y();
194 
195  if(bb._min.z()<_min.z()) _min.z() = bb._min.z();
196  if(bb._max.z()>_max.z()) _max.z() = bb._max.z();
197  }
198 
201  template<typename BST>
203  {
204  if (!sh.valid()) return;
205 
206  if(sh._center.x()-sh._radius<_min.x()) _min.x() = sh._center.x()-sh._radius;
207  if(sh._center.x()+sh._radius>_max.x()) _max.x() = sh._center.x()+sh._radius;
208 
209  if(sh._center.y()-sh._radius<_min.y()) _min.y() = sh._center.y()-sh._radius;
210  if(sh._center.y()+sh._radius>_max.y()) _max.y() = sh._center.y()+sh._radius;
211 
212  if(sh._center.z()-sh._radius<_min.z()) _min.z() = sh._center.z()-sh._radius;
213  if(sh._center.z()+sh._radius>_max.z()) _max.z() = sh._center.z()+sh._radius;
214  }
215 
216 
219  { return BoundingBoxImpl(osg::maximum(xMin(),bb.xMin()),osg::maximum(yMin(),bb.yMin()),osg::maximum(zMin(),bb.zMin()),
220  osg::minimum(xMax(),bb.xMax()),osg::minimum(yMax(),bb.yMax()),osg::minimum(zMax(),bb.zMax()));
221 
222  }
223 
225  bool intersects(const BoundingBoxImpl& bb) const
226  { return osg::maximum(xMin(),bb.xMin()) <= osg::minimum(xMax(),bb.xMax()) &&
227  osg::maximum(yMin(),bb.yMin()) <= osg::minimum(yMax(),bb.yMax()) &&
228  osg::maximum(zMin(),bb.zMin()) <= osg::minimum(zMax(),bb.zMax());
229 
230  }
231 
233  inline bool contains(const vec_type& v) const
234  {
235  return valid() &&
236  (v.x()>=_min.x() && v.x()<=_max.x()) &&
237  (v.y()>=_min.y() && v.y()<=_max.y()) &&
238  (v.z()>=_min.z() && v.z()<=_max.z());
239  }
240 
242  inline bool contains(const vec_type& v, value_type epsilon) const
243  {
244  return valid() &&
245  ((v.x()+epsilon)>=_min.x() && (v.x()-epsilon)<=_max.x()) &&
246  ((v.y()+epsilon)>=_min.y() && (v.y()-epsilon)<=_max.y()) &&
247  ((v.z()+epsilon)>=_min.z() && (v.z()-epsilon)<=_max.z());
248  }
249 };
250 
253 
254 #ifdef OSG_USE_FLOAT_BOUNDINGBOX
255 typedef BoundingBoxf BoundingBox;
256 #else
257 typedef BoundingBoxd BoundingBox;
258 #endif
259 
260 }
261 
262 #endif
value_type zMax() const
Definition: BoundingBox.h:123
bool valid() const
Definition: BoundingBox.h:86
BoundingBoxImpl intersect(const BoundingBoxImpl &bb) const
Definition: BoundingBox.h:218
value_type radius() const
Definition: BoundingBox.h:132
bool contains(const vec_type &v, value_type epsilon) const
Definition: BoundingBox.h:242
bool operator!=(const BoundingBoxImpl &rhs) const
Definition: BoundingBox.h:83
BoundingBoxImpl< Vec3f > BoundingBoxf
Definition: BoundingBox.h:251
value_type & zMax()
Definition: BoundingBox.h:122
void expandBy(const BoundingSphereImpl< BST > &sh)
Definition: BoundingBox.h:202
value_type & zMin()
Definition: BoundingBox.h:113
value_type xMin() const
Definition: BoundingBox.h:108
value_type yMin() const
Definition: BoundingBox.h:111
T minimum(T lhs, T rhs)
Definition: Math.h:59
VT::value_type value_type
Definition: BoundingBox.h:37
void set(const vec_type &min, const vec_type &max)
Definition: BoundingBox.h:100
T maximum(T lhs, T rhs)
Definition: Math.h:66
BoundingBoxd BoundingBox
Definition: BoundingBox.h:257
value_type & yMax()
Definition: BoundingBox.h:119
void set(value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax)
Definition: BoundingBox.h:92
BoundingBoxImpl< Vec3d > BoundingBoxd
Definition: BoundingBox.h:252
void expandBy(const vec_type &v)
Definition: BoundingBox.h:156
void expandBy(const BoundingBoxImpl &bb)
Definition: BoundingBox.h:185
value_type & xMax()
Definition: BoundingBox.h:116
value_type radius2() const
Definition: BoundingBox.h:139
value_type & yMin()
Definition: BoundingBox.h:110
bool operator==(const BoundingBoxImpl &rhs) const
Definition: BoundingBox.h:82
value_type zMin() const
Definition: BoundingBox.h:114
const vec_type corner(unsigned int pos) const
Definition: BoundingBox.h:149
value_type xMax() const
Definition: BoundingBox.h:117
BoundingBoxImpl(value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax)
Definition: BoundingBox.h:61
value_type & xMin()
Definition: BoundingBox.h:107
BoundingBoxImpl(const vec_type &min, const vec_type &max)
Definition: BoundingBox.h:67
Definition: AlphaFunc.h:19
bool contains(const vec_type &v) const
Definition: BoundingBox.h:233
bool intersects(const BoundingBoxImpl &bb) const
Definition: BoundingBox.h:225
void expandBy(value_type x, value_type y, value_type z)
Definition: BoundingBox.h:171
value_type yMax() const
Definition: BoundingBox.h:120
const vec_type center() const
Definition: BoundingBox.h:126
BoundingBoxImpl(const BoundingBoxImpl< BT > &bb)
Definition: BoundingBox.h:55