openscenegraph
BoundingBox
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
23namespace osg {
24
25template<typename VT>
26class BoundingSphereImpl;
27
28/** General purpose axis-aligned bounding box class for enclosing objects/vertices.
29 * Bounds leaf objects in a scene such as osg::Drawable objects. Used for frustum
30 * culling etc.
31*/
32template<typename VT>
33class BoundingBoxImpl
34{
35 public:
36 typedef VT vec_type;
37 typedef typename VT::value_type value_type;
38
39 /** Minimum extent. (Smallest X, Y, and Z values of all coordinates.) */
40 vec_type _min;
41 /** Maximum extent. (Greatest X, Y, and Z values of all coordinates.) */
42 vec_type _max;
43
44 /** Creates an uninitialized bounding box. */
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>
55 inline BoundingBoxImpl(const BoundingBoxImpl<BT>& bb) :
56 _min(bb._min),
57 _max(bb._max)
58 {}
59
60 /** Creates a bounding box initialized to the given extents. */
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
66 /** Creates a bounding box initialized to the given extents. */
67 inline BoundingBoxImpl(const vec_type& min,const vec_type& max) :
68 _min(min),
69 _max(max) {}
70
71 /** Clear the bounding box. Erases existing minimum and maximum extents. */
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
85 /** Returns true if the bounding box extents are valid, false otherwise. */
86 inline bool valid() const
87 {
88 return _max.x()>=_min.x() && _max.y()>=_min.y() && _max.z()>=_min.z();
89 }
90
91 /** Sets the bounding box extents. */
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
99 /** Sets the bounding box extents. */
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
125 /** Calculates and returns the bounding box center. */
126 inline const vec_type center() const
127 {
128 return (_min+_max)*0.5;
129 }
130
131 /** Calculates and returns the bounding box radius. */
132 inline value_type radius() const
133 {
134 return sqrt(radius2());
135 }
136
137 /** Calculates and returns the squared length of the bounding box radius.
138 * Note, radius2() is faster to calculate than radius(). */
139 inline value_type radius2() const
140 {
141 return 0.25*((_max-_min).length2());
142 }
143
144 /** Returns a specific corner of the bounding box.
145 * pos specifies the corner as a number between 0 and 7.
146 * Each bit selects an axis, X, Y, or Z from least- to
147 * most-significant. Unset bits select the minimum value
148 * for that axis, and set bits select the maximum. */
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
154 /** Expands the bounding box to include the given coordinate.
155 * If the box is uninitialized, set its min and max extents to v. */
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
168 /** Expands the bounding box to include the given coordinate.
169 * If the box is uninitialized, set its min and max extents to
170 * Vec3(x,y,z). */
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
183 /** Expands this bounding box to include the given bounding box.
184 * If this box is uninitialized, set it equal to bb. */
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
199 /** Expands this bounding box to include the given sphere.
200 * If this box is uninitialized, set it to include sh. */
201 template<typename BST>
202 void expandBy(const BoundingSphereImpl<BST>& sh)
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
217 /** Returns the intersection of this bounding box and the specified bounding box. */
218 BoundingBoxImpl intersect(const BoundingBoxImpl& bb) const
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
224 /** Return true if this bounding box intersects the specified bounding box. */
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
232 /** Returns true if this bounding box contains the specified coordinate. */
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
241 /** Returns true if this bounding box contains the specified coordinate allowing for specific epsilon. */
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
251typedef BoundingBoxImpl<Vec3f> BoundingBoxf;
252typedef BoundingBoxImpl<Vec3d> BoundingBoxd;
253
254#ifdef OSG_USE_FLOAT_BOUNDINGBOX
255typedef BoundingBoxf BoundingBox;
256#else
257typedef BoundingBoxd BoundingBox;
258#endif
259
260}
261
262#endif