22 #include <osg/BoundingSphere>
23 #include <osg/BoundingBox>
38 #ifdef OSG_USE_FLOAT_PLANE
51 enum { num_components = 3 };
58 inline Plane() { _fv[0]=0.0; _fv[1]=0.0; _fv[2]=0.0; _fv[3]=0.0; _lowerBBCorner = 0; _upperBBCorner = 0; }
63 inline Plane(value_type a,value_type b,value_type c,value_type d) { set(a,b,c,d); }
79 inline Plane(
const Vec3_type& norm,value_type d) { set(norm,d); }
89 inline Plane(
const Vec3_type& v1,
const Vec3_type& v2,
const Vec3_type& v3) { set(v1,v2,v3); }
96 inline Plane(
const Vec3_type& norm,
const Vec3_type& point) { set(norm,point); }
100 if (&pl==
this)
return *
this;
105 inline void set(
const Plane& pl) { _fv[0]=pl.
_fv[0]; _fv[1]=pl.
_fv[1]; _fv[2]=pl.
_fv[2]; _fv[3]=pl.
_fv[3]; calculateUpperLowerBBCorners(); }
106 inline void set(value_type a, value_type b, value_type c, value_type d) { _fv[0]=a; _fv[1]=b; _fv[2]=c; _fv[3]=d; calculateUpperLowerBBCorners(); }
108 inline void set(
const Vec4f& vec) {
set(vec[0],vec[1],vec[2],vec[3]); }
109 inline void set(
const Vec4d& vec) {
set(vec[0],vec[1],vec[2],vec[3]); }
111 inline void set(
const Vec3_type& norm,
double d) {
set(norm[0],norm[1],norm[2],d); }
113 inline void set(
const Vec3_type& v1,
const Vec3_type& v2,
const Vec3_type& v3)
115 Vec3_type norm = (v2-v1)^(v3-v2);
116 value_type length = norm.
length();
117 if (length>1e-6) norm/= length;
118 else norm.
set(0.0,0.0,0.0);
119 set(norm[0],norm[1],norm[2],-(v1*norm));
122 inline void set(
const Vec3_type& norm,
const Vec3_type& point)
124 value_type d = -norm[0]*point[0] - norm[1]*point[1] - norm[2]*point[2];
125 set(norm[0],norm[1],norm[2],d);
135 calculateUpperLowerBBCorners();
142 value_type inv_length = 1.0 / sqrt(_fv[0]*_fv[0] + _fv[1]*_fv[1]+ _fv[2]*_fv[2]);
143 _fv[0] *= inv_length;
144 _fv[1] *= inv_length;
145 _fv[2] *= inv_length;
146 _fv[3] *= inv_length;
153 _upperBBCorner = (_fv[0]>=0.0?1:0) |
157 _lowerBBCorner = (~_upperBBCorner)&7;
168 inline bool operator == (
const Plane& plane)
const {
return _fv[0]==plane.
_fv[0] && _fv[1]==plane.
_fv[1] && _fv[2]==plane.
_fv[2] && _fv[3]==plane.
_fv[3]; }
170 inline bool operator != (
const Plane& plane)
const {
return _fv[0]!=plane.
_fv[0] || _fv[1]!=plane.
_fv[1] || _fv[2]!=plane.
_fv[2] || _fv[3]!=plane.
_fv[3]; }
174 inline bool operator < (
const Plane& plane)
const
176 if (_fv[0]<plane.
_fv[0])
return true;
177 else if (_fv[0]>plane.
_fv[0])
return false;
178 else if (_fv[1]<plane.
_fv[1])
return true;
179 else if (_fv[1]>plane.
_fv[1])
return false;
180 else if (_fv[2]<plane.
_fv[2])
return true;
181 else if (_fv[2]>plane.
_fv[2])
return false;
182 else return (_fv[3]<plane.
_fv[3]);
186 inline value_type*
ptr() {
return _fv; }
187 inline const value_type*
ptr()
const {
return _fv; }
189 inline Vec4_type
asVec4()
const {
return Vec4_type(_fv[0],_fv[1],_fv[2],_fv[3]); }
191 inline value_type& operator [] (
unsigned int i) {
return _fv[i]; }
192 inline value_type operator [] (
unsigned int i)
const {
return _fv[i]; }
195 inline Vec3_type
getNormal()
const {
return Vec3_type(_fv[0],_fv[1],_fv[2]); }
238 inline int intersect(
const std::vector<Vec3f>& vertices)
const
240 if (vertices.empty())
return -1;
245 for(std::vector<Vec3f>::const_iterator itr=vertices.begin();
246 itr != vertices.end();
249 float d = distance(*itr);
250 if (d>0.0f) ++noAbove;
251 else if (d<0.0f) ++noBelow;
257 if (noBelow>0)
return 0;
267 inline int intersect(
const std::vector<Vec3d>& vertices)
const
269 if (vertices.empty())
return -1;
274 for(std::vector<Vec3d>::const_iterator itr=vertices.begin();
275 itr != vertices.end();
278 double d = distance(*itr);
279 if (d>0.0) ++noAbove;
280 else if (d<0.0) ++noBelow;
286 if (noBelow>0)
return 0;
298 float d = distance(bs.
center());
300 if (d>bs.
radius())
return 1;
301 else if (d<-bs.
radius())
return -1;
313 if (distance(bb.
corner(_lowerBBCorner))>0.0f)
return 1;
316 if (distance(bb.
corner(_upperBBCorner))<0.0f)
return -1;
334 transformProvidingInverse(inverse);
342 Vec4_type vec(_fv[0],_fv[1],_fv[2],_fv[3]);
Plane(const Vec3_type &v1, const Vec3_type &v2, const Vec3_type &v3)
Constructor.
int intersect(const BoundingSphere &bs) const
void transform(const osg::Matrix &matrix)
void set(value_type a, value_type b, value_type c, value_type d)
Plane(value_type a, value_type b, value_type c, value_type d)
Constructor.
void transformProvidingInverse(const osg::Matrix &matrix)
void set(const Vec4d &vec)
Plane()
Default constructor.
void set(const Plane &pl)
unsigned int _upperBBCorner
float dotProductNormal(const osg::Vec3f &v) const
void set(const Vec3_type &norm, const Vec3_type &point)
void calculateUpperLowerBBCorners()
Plane(const Vec4f &vec)
Constructor.
Plane(const Vec3_type &norm, value_type d)
Constructor.
float distance(const osg::Vec3f &v) const
Plane(const Vec3_type &norm, const Vec3_type &point)
Constructor.
void set(value_type x, value_type y, value_type z)
Plane(const Vec4d &vec)
Constructor.
int intersect(const std::vector< Vec3d > &vertices) const
double dotProductNormal(const osg::Vec3d &v) const
void set(const Vec3_type &norm, double d)
int intersect(const BoundingBox &bb) const
int intersect(const std::vector< Vec3f > &vertices) const
Vec3_type getNormal() const
bool valid() const
Checks if all internal values describing the plane have valid numbers.
const vec_type corner(unsigned int pos) const
A plane class. It can be used to represent an infinite plane.
void set(const Vec3_type &v1, const Vec3_type &v2, const Vec3_type &v3)
unsigned int _lowerBBCorner
bool invert(const Matrixd &rhs)
value_type length() const
const value_type * ptr() const
void set(const Vec4f &vec)
double distance(const osg::Vec3d &v) const