Polytope 14.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_POLYTOPE
#define OSG_POLYTOPE 1

#include <osg/Plane>
#include <osg/fast_back_stack>

namespace osg {


/** A Polytope class for representing convex clipping volumes made up of a set of planes.
  * When adding planes, their normals should point inwards (into the volume) */
class OSG_EXPORT Polytope
{

    public:

        typedef unsigned int                    ClippingMask;
        typedef std::vector<Plane>              PlaneList;
        typedef std::vector<Vec3>               VertexList;
        typedef fast_back_stack<ClippingMask>   MaskStack;

        inline Polytope() {setupMask();}

        inline Polytope(const Polytope& cv) : 
            _maskStack(cv._maskStack),
            _resultMask(cv._resultMask),
            _planeList(cv._planeList),
            _referenceVertexList(cv._referenceVertexList) {}
        
        inline Polytope(const PlaneList& pl) : _planeList(pl) {setupMask();}

        inline ~Polytope() {}

        inline void clear() { _planeList.clear(); setupMask(); }

        inline Polytope& operator = (const Polytope& cv)
        {
            if (&cv==this) return *this;
            _maskStack = cv._maskStack;
            _resultMask = cv._resultMask;
            _planeList = cv._planeList;
            _referenceVertexList = cv._referenceVertexList;
            return *this;
        }
        
        /** Create a Polytope which is a cube, centered at 0,0,0, with sides of 2 units.*/
        void setToUnitFrustum(bool withNear=true, bool withFar=true)
        {
            _planeList.clear();
            _planeList.push_back(Plane(1.0,0.0,0.0,1.0)); // left plane.
            _planeList.push_back(Plane(-1.0,0.0,0.0,1.0)); // right plane.
            _planeList.push_back(Plane(0.0,1.0,0.0,1.0)); // bottom plane.
            _planeList.push_back(Plane(0.0,-1.0,0.0,1.0)); // top plane.
            if (withNear) _planeList.push_back(Plane(0.0,0.0,1.0,1.0)); // near plane
            if (withFar) _planeList.push_back(Plane(0.0,0.0,-1.0,1.0)); // far plane
            setupMask();
        }

        /** Create a Polytope which is a equivalent to BoundingBox.*/
        void setToBoundingBox(const BoundingBox& bb)
        {
            _planeList.clear();
            _planeList.push_back(Plane(1.0,0.0,0.0,-bb.xMin())); // left plane.
            _planeList.push_back(Plane(-1.0,0.0,0.0,bb.xMax())); // right plane.
            _planeList.push_back(Plane(0.0,1.0,0.0,-bb.yMin())); // bottom plane.
            _planeList.push_back(Plane(0.0,-1.0,0.0,bb.yMax())); // top plane.
            _planeList.push_back(Plane(0.0,0.0,1.0,-bb.zMin())); // near plane
            _planeList.push_back(Plane(0.0,0.0,-1.0,bb.zMax())); // far plane
            setupMask();
        }

        inline void setAndTransformProvidingInverse(const Polytope& pt, const osg::Matrix& matrix)
        {
            _referenceVertexList = pt._referenceVertexList;

            unsigned int resultMask = pt._maskStack.back();
            if (resultMask==0)
            {
                _maskStack.back() = 0;
                _resultMask = 0;
                _planeList.clear();
                return;
            }
            
            ClippingMask selector_mask = 0x1;

            unsigned int numActivePlanes = 0;

            // count number of active planes.
            PlaneList::const_iterator itr;
            for(itr=pt._planeList.begin();
                itr!=pt._planeList.end();
                ++itr)
            {
                if (resultMask&selector_mask) ++numActivePlanes;
                selector_mask <<= 1;
            }
            
            _planeList.resize(numActivePlanes);
            _resultMask = 0;
            selector_mask = 0x1;
            unsigned int index = 0;
            for(itr=pt._planeList.begin();
                itr!=pt._planeList.end();
                ++itr)
            {
                if (resultMask&selector_mask)
                {
                     _planeList[index] = *itr;
                     _planeList[index++].transformProvidingInverse(matrix);
                    _resultMask = (_resultMask<<1) | 1;
                }
                selector_mask <<= 1;
            }

            _maskStack.back() = _resultMask;
        }

        inline void set(const PlaneList& pl) { _planeList = pl; setupMask(); }
        
        
        inline void add(const osg::Plane& pl) { _planeList.push_back(pl); setupMask(); }

        /** flip/reverse the orientation of all the planes.*/
        inline void flip()
        {
            for(PlaneList::iterator itr=_planeList.begin();
                itr!=_planeList.end();
                ++itr)
            {
                itr->flip();
            }
        }


        inline PlaneList& getPlaneList() { return _planeList; }

        inline const PlaneList& getPlaneList() const { return _planeList; }


        inline void setReferenceVertexList(VertexList& vertices) { _referenceVertexList=vertices; }

        inline VertexList& getReferenceVertexList() { return _referenceVertexList; }
        
        inline const VertexList& getReferenceVertexList() const { return _referenceVertexList; }


        inline void setupMask()
        {
            _resultMask = 0;
            for(unsigned int i=0;i<_planeList.size();++i)
            {
                _resultMask = (_resultMask<<1) | 1;
            }
            _maskStack.push_back(_resultMask);
        }

        inline ClippingMask& getCurrentMask() { return _maskStack.back(); }

        inline ClippingMask getCurrentMask() const { return _maskStack.back(); }

        inline void setResultMask(ClippingMask mask) { _resultMask=mask; }

        inline ClippingMask getResultMask() const { return _resultMask; }
        
        MaskStack& getMaskStack() { return _maskStack; }
        
        const MaskStack& getMaskStack() const { return _maskStack; }
        
        
        inline void pushCurrentMask()
        {
            _maskStack.push_back(_resultMask);
        }

        inline void popCurrentMask()
        {
            _maskStack.pop_back();
        }

        /** Check whether a vertex is contained within clipping set.*/
        inline bool contains(const osg::Vec3& v) const
        {
            if (!_maskStack.back()) return true;
            
            unsigned int selector_mask = 0x1;
            for(PlaneList::const_iterator itr=_planeList.begin();
                itr!=_planeList.end();
                ++itr)
            {
                if ((_maskStack.back()&selector_mask) && (itr->distance(v)<0.0f)) return false;
                selector_mask <<= 1;
            }
            return true;
        }

        /** Check whether any part of vertex list is contained within clipping set.*/
        inline bool contains(const std::vector<Vec3>& vertices)
        {
            if (!_maskStack.back()) return true;

            _resultMask = _maskStack.back();

            for(std::vector<Vec3>::const_iterator vitr = vertices.begin();
                vitr != vertices.end();
                ++vitr)
            {
                const osg::Vec3& v = *vitr;
                bool outside = false;
                ClippingMask selector_mask = 0x1;
                for(PlaneList::const_iterator itr=_planeList.begin();
                    itr!=_planeList.end() && !outside;
                    ++itr)
                {
                    if ((_maskStack.back()&selector_mask) && (itr->distance(v)<0.0f)) outside = true;
                    selector_mask <<= 1;
                }
                
                if (!outside) return true;
            }
            return false;
        }

        /** Check whether any part of a bounding sphere is contained within clipping set.
            Using a mask to determine which planes should be used for the check, and
            modifying the mask to turn off planes which wouldn't contribute to clipping
            of any internal objects.  This feature is used in osgUtil::CullVisitor
            to prevent redundant plane checking.*/
        inline bool contains(const osg::BoundingSphere& bs)
        {
            if (!_maskStack.back()) return true;

            _resultMask = _maskStack.back();
            ClippingMask selector_mask = 0x1;

            for(PlaneList::const_iterator itr=_planeList.begin();
                itr!=_planeList.end();
                ++itr)
            {
                if (_resultMask&selector_mask)
                {
                    int res=itr->intersect(bs);
                    if (res<0) return false; // outside clipping set.
                    else if (res>0) _resultMask ^= selector_mask; // subsequent checks against this plane not required.
                }
                selector_mask <<= 1; 
            }
            return true;
        }
        
        /** Check whether any part of a bounding box is contained within clipping set.
            Using a mask to determine which planes should be used for the check, and
            modifying the mask to turn off planes which wouldn't contribute to clipping
            of any internal objects.  This feature is used in osgUtil::CullVisitor
            to prevent redundant plane checking.*/
        inline bool contains(const osg::BoundingBox& bb)
        {
            if (!_maskStack.back()) return true;

            _resultMask = _maskStack.back();
            ClippingMask selector_mask = 0x1;

            for(PlaneList::const_iterator itr=_planeList.begin();
                itr!=_planeList.end();
                ++itr)
            {
                if (_resultMask&selector_mask)
                {
                    int res=itr->intersect(bb);
                    if (res<0) return false; // outside clipping set.
                    else if (res>0) _resultMask ^= selector_mask; // subsequent checks against this plane not required.
                }
                selector_mask <<= 1; 
            }
            return true;
        }

        /** Check whether all of vertex list is contained with clipping set.*/
        inline bool containsAllOf(const std::vector<Vec3>& vertices)
        {
            if (!_maskStack.back()) return false;

            _resultMask = _maskStack.back();
            ClippingMask selector_mask = 0x1;

            for(PlaneList::const_iterator itr=_planeList.begin();
                itr!=_planeList.end();
                ++itr)
            {
                if (_resultMask&selector_mask)
                {
                    int res=itr->intersect(vertices);
                    if (res<1) return false;  // intersects, or is below plane.
                    _resultMask ^= selector_mask; // subsequent checks against this plane not required.
                }
                selector_mask <<= 1; 
            }
            return true;
        }

        /** Check whether the entire bounding sphere is contained within clipping set.*/
        inline bool containsAllOf(const osg::BoundingSphere& bs)
        {
            if (!_maskStack.back()) return false;

            _resultMask = _maskStack.back();
            ClippingMask selector_mask = 0x1;
            
            for(PlaneList::const_iterator itr=_planeList.begin();
                itr!=_planeList.end();
                ++itr)
            {
                if (_resultMask&selector_mask)
                {
                    int res=itr->intersect(bs);
                    if (res<1) return false;  // intersects, or is below plane.
                    _resultMask ^= selector_mask; // subsequent checks against this plane not required.
                }
                selector_mask <<= 1; 
            }
            return true;
        }
        
        /** Check whether the entire bounding box is contained within clipping set.*/
        inline bool containsAllOf(const osg::BoundingBox& bb)
        {
            if (!_maskStack.back()) return false;

            _resultMask = _maskStack.back();
            ClippingMask selector_mask = 0x1;

            for(PlaneList::const_iterator itr=_planeList.begin();
                itr!=_planeList.end();
                ++itr)
            {
                if (_resultMask&selector_mask)
                {
                    int res=itr->intersect(bb);
                    if (res<1) return false;  // intersects, or is below plane.
                    _resultMask ^= selector_mask; // subsequent checks against this plane not required.
                }
                selector_mask <<= 1; 
            }
            return true;
        }

        
        /** Transform the clipping set by matrix.  Note, this operations carries out
          * the calculation of the inverse of the matrix since a plane must 
          * be multiplied by the inverse transposed to transform it. This
          * makes this operation expensive.  If the inverse has been already
          * calculated elsewhere then use transformProvidingInverse() instead.
          * See http://www.worldserver.com/turk/computergraphics/NormalTransformations.pdf*/
        inline void transform(const osg::Matrix& matrix)
        {
            osg::Matrix inverse;
            inverse.invert(matrix);
            transformProvidingInverse(inverse);
        }
        
        /** Transform the clipping set by provide a pre inverted matrix.
          * see transform for details. */
        inline void transformProvidingInverse(const osg::Matrix& matrix)
        {
            if (!_maskStack.back()) return;

            _resultMask = _maskStack.back();
            ClippingMask selector_mask = 0x1;
            for(PlaneList::iterator itr=_planeList.begin();
                itr!=_planeList.end();
                ++itr)
            {
                if (_resultMask&selector_mask)
                {
                    itr->transformProvidingInverse(matrix);
                    selector_mask <<= 1; 
                }
            }
        }
        
    protected:


        MaskStack                           _maskStack;
        ClippingMask                        _resultMask;
        PlaneList                           _planeList;
        VertexList                          _referenceVertexList;

};

}    // end of namespace

#endif