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
32template<typename VT>
34{
35 public:
36 typedef VT vec_type;
37 typedef typename VT::value_type value_type;
38
43
45 inline BoundingBoxImpl() :
47 FLT_MAX,
48 FLT_MAX),
50 -FLT_MAX,
51 -FLT_MAX)
52 {}
53
54 template<typename BT>
56 _min(bb._min),
57 _max(bb._max)
58 {}
59
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
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
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
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
256#else
258#endif
259
260}
261
262#endif
Definition BoundingBox:34
value_type zMax() const
Definition BoundingBox:123
const vec_type center() const
Definition BoundingBox:126
VT::value_type value_type
Definition BoundingBox:37
bool intersects(const BoundingBoxImpl &bb) const
Definition BoundingBox:225
value_type & xMax()
Definition BoundingBox:116
value_type & yMin()
Definition BoundingBox:110
BoundingBoxImpl(const BoundingBoxImpl< BT > &bb)
Definition BoundingBox:55
value_type & xMin()
Definition BoundingBox:107
value_type & zMin()
Definition BoundingBox:113
value_type xMax() const
Definition BoundingBox:117
void expandBy(value_type x, value_type y, value_type z)
Definition BoundingBox:171
BoundingBoxImpl(value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax)
Definition BoundingBox:61
vec_type _max
Definition BoundingBox:42
bool contains(const vec_type &v, value_type epsilon) const
Definition BoundingBox:242
void expandBy(const vec_type &v)
Definition BoundingBox:156
bool operator==(const BoundingBoxImpl &rhs) const
Definition BoundingBox:82
value_type & zMax()
Definition BoundingBox:122
value_type xMin() const
Definition BoundingBox:108
void set(value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax)
Definition BoundingBox:92
BoundingBoxImpl intersect(const BoundingBoxImpl &bb) const
Definition BoundingBox:218
const vec_type corner(unsigned int pos) const
Definition BoundingBox:149
bool valid() const
Definition BoundingBox:86
vec_type _min
Definition BoundingBox:40
void expandBy(const BoundingBoxImpl &bb)
Definition BoundingBox:185
void expandBy(const BoundingSphereImpl< BST > &sh)
Definition BoundingBox:202
bool operator!=(const BoundingBoxImpl &rhs) const
Definition BoundingBox:83
BoundingBoxImpl()
Definition BoundingBox:45
bool contains(const vec_type &v) const
Definition BoundingBox:233
void set(const vec_type &min, const vec_type &max)
Definition BoundingBox:100
VT vec_type
Definition BoundingBox:36
value_type yMax() const
Definition BoundingBox:120
value_type zMin() const
Definition BoundingBox:114
void init()
Definition BoundingBox:72
value_type radius2() const
Definition BoundingBox:139
value_type & yMax()
Definition BoundingBox:119
value_type radius() const
Definition BoundingBox:132
value_type yMin() const
Definition BoundingBox:111
BoundingBoxImpl(const vec_type &min, const vec_type &max)
Definition BoundingBox:67
Definition BoundingSphere:35
author: Julien Valentin 2017 (mp3butcher@hotmail.com)
Definition AlphaFunc:19
BoundingBoxImpl< Vec3d > BoundingBoxd
Definition BoundingBox:252
BoundingBoxd BoundingBox
Definition BoundingBox:257
BoundingBoxImpl< Vec3f > BoundingBoxf
Definition BoundingBox:251
T maximum(T lhs, T rhs)
Definition Math:85
T minimum(T lhs, T rhs)
Definition Math:78