15 #define OSG_POLYTOPE 1
18 #include <osg/fast_back_stack>
38 _maskStack(cv._maskStack),
39 _resultMask(cv._resultMask),
40 _planeList(cv._planeList),
41 _referenceVertexList(cv._referenceVertexList) {}
43 inline Polytope(
const PlaneList& pl) : _planeList(pl) {setupMask();}
47 inline void clear() { _planeList.clear(); setupMask(); }
51 if (&cv==
this)
return *
this;
63 _planeList.push_back(
Plane(1.0,0.0,0.0,1.0));
64 _planeList.push_back(
Plane(-1.0,0.0,0.0,1.0));
65 _planeList.push_back(
Plane(0.0,1.0,0.0,1.0));
66 _planeList.push_back(
Plane(0.0,-1.0,0.0,1.0));
67 if (withNear) _planeList.push_back(
Plane(0.0,0.0,1.0,1.0));
68 if (withFar) _planeList.push_back(
Plane(0.0,0.0,-1.0,1.0));
76 _planeList.push_back(
Plane(1.0,0.0,0.0,-bb.
xMin()));
77 _planeList.push_back(
Plane(-1.0,0.0,0.0,bb.
xMax()));
78 _planeList.push_back(
Plane(0.0,1.0,0.0,-bb.
yMin()));
79 _planeList.push_back(
Plane(0.0,-1.0,0.0,bb.
yMax()));
80 _planeList.push_back(
Plane(0.0,0.0,1.0,-bb.
zMin()));
81 _planeList.push_back(
Plane(0.0,0.0,-1.0,bb.
zMax()));
92 _maskStack.back() = 0;
98 ClippingMask selector_mask = 0x1;
100 unsigned int numActivePlanes = 0;
103 PlaneList::const_iterator itr;
108 if (resultMask&selector_mask) ++numActivePlanes;
112 _planeList.resize(numActivePlanes);
115 unsigned int index = 0;
120 if (resultMask&selector_mask)
122 _planeList[index] = *itr;
123 _planeList[index++].transformProvidingInverse(matrix);
124 _resultMask = (_resultMask<<1) | 1;
129 _maskStack.back() = _resultMask;
132 inline void set(
const PlaneList& pl) { _planeList = pl; setupMask(); }
135 inline void add(
const osg::Plane& pl) { _planeList.push_back(pl); setupMask(); }
140 for(PlaneList::iterator itr=_planeList.begin();
141 itr!=_planeList.end();
148 inline bool empty()
const {
return _planeList.empty(); }
165 for(
unsigned int i=0;i<_planeList.size();++i)
167 _resultMask = (_resultMask<<1) | 1;
169 _maskStack.push_back(_resultMask);
187 _maskStack.push_back(_resultMask);
192 _maskStack.pop_back();
198 if (!_maskStack.back())
return true;
200 unsigned int selector_mask = 0x1;
201 for(PlaneList::const_iterator itr=_planeList.begin();
202 itr!=_planeList.end();
205 if ((_maskStack.back()&selector_mask) && (itr->distance(v)<0.0f))
return false;
212 inline bool contains(
const std::vector<Vec3>& vertices)
214 if (!_maskStack.back())
return true;
216 _resultMask = _maskStack.back();
218 for(std::vector<Vec3>::const_iterator vitr = vertices.begin();
219 vitr != vertices.end();
223 bool outside =
false;
224 ClippingMask selector_mask = 0x1;
225 for(PlaneList::const_iterator itr=_planeList.begin();
226 itr!=_planeList.end() && !outside;
229 if ((_maskStack.back()&selector_mask) && (itr->distance(v)<0.0f)) outside =
true;
233 if (!outside)
return true;
245 if (!_maskStack.back())
return true;
247 _resultMask = _maskStack.back();
248 ClippingMask selector_mask = 0x1;
250 for(PlaneList::const_iterator itr=_planeList.begin();
251 itr!=_planeList.end();
254 if (_resultMask&selector_mask)
256 int res=itr->intersect(bs);
257 if (res<0)
return false;
258 else if (res>0) _resultMask ^= selector_mask;
272 if (!_maskStack.back())
return true;
274 _resultMask = _maskStack.back();
275 ClippingMask selector_mask = 0x1;
277 for(PlaneList::const_iterator itr=_planeList.begin();
278 itr!=_planeList.end();
281 if (_resultMask&selector_mask)
283 int res=itr->intersect(bb);
284 if (res<0)
return false;
285 else if (res>0) _resultMask ^= selector_mask;
295 if (!_maskStack.back())
return false;
297 _resultMask = _maskStack.back();
298 ClippingMask selector_mask = 0x1;
300 for(PlaneList::const_iterator itr=_planeList.begin();
301 itr!=_planeList.end();
304 if (_resultMask&selector_mask)
306 int res=itr->intersect(vertices);
307 if (res<1)
return false;
308 _resultMask ^= selector_mask;
318 if (!_maskStack.back())
return false;
320 _resultMask = _maskStack.back();
321 ClippingMask selector_mask = 0x1;
323 for(PlaneList::const_iterator itr=_planeList.begin();
324 itr!=_planeList.end();
327 if (_resultMask&selector_mask)
329 int res=itr->intersect(bs);
330 if (res<1)
return false;
331 _resultMask ^= selector_mask;
341 if (!_maskStack.back())
return false;
343 _resultMask = _maskStack.back();
344 ClippingMask selector_mask = 0x1;
346 for(PlaneList::const_iterator itr=_planeList.begin();
347 itr!=_planeList.end();
350 if (_resultMask&selector_mask)
352 int res=itr->intersect(bb);
353 if (res<1)
return false;
354 _resultMask ^= selector_mask;
372 transformProvidingInverse(inverse);
379 if (!_maskStack.back())
return;
381 _resultMask = _maskStack.back();
382 ClippingMask selector_mask = 0x1;
383 for(PlaneList::iterator itr=_planeList.begin();
384 itr!=_planeList.end();
387 if (_resultMask&selector_mask)
389 itr->transformProvidingInverse(matrix);
bool containsAllOf(const osg::BoundingSphere &bs)
void setResultMask(ClippingMask mask)
unsigned int ClippingMask
PlaneList & getPlaneList()
void set(const PlaneList &pl)
bool contains(const osg::BoundingBox &bb)
Polytope(const Polytope &cv)
void transform(const osg::Matrix &matrix)
VertexList & getReferenceVertexList()
bool containsAllOf(const osg::BoundingBox &bb)
ClippingMask getResultMask() const
bool contains(const osg::Vec3 &v) const
const MaskStack & getMaskStack() const
bool containsAllOf(const std::vector< Vec3 > &vertices)
void setToBoundingBox(const BoundingBox &bb)
fast_back_stack< ClippingMask > MaskStack
void add(const osg::Plane &pl)
MaskStack & getMaskStack()
std::vector< Vec3 > VertexList
const PlaneList & getPlaneList() const
void transformProvidingInverse(const osg::Matrix &matrix)
Polytope(const PlaneList &pl)
void setAndTransformProvidingInverse(const Polytope &pt, const osg::Matrix &matrix)
const VertexList & getReferenceVertexList() const
bool contains(const std::vector< Vec3 > &vertices)
VertexList _referenceVertexList
ClippingMask & getCurrentMask()
A plane class. It can be used to represent an infinite plane.
void setReferenceVertexList(VertexList &vertices)
bool invert(const Matrixd &rhs)
ClippingMask getCurrentMask() const
void setToUnitFrustum(bool withNear=true, bool withFar=true)
std::vector< Plane > PlaneList
bool contains(const osg::BoundingSphere &bs)