PolytopeIntersector 5.71 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
/* -*-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 OSGUTIL_POLYTOPEINTERSECTOR
#define OSGUTIL_POLYTOPEINTERSECTOR 1

#include <osgUtil/IntersectionVisitor>

namespace osgUtil
{

/** Concrete class for implementing polytope intersections with the scene graph.
  * To be used in conjunction with IntersectionVisitor. */
class OSGUTIL_EXPORT PolytopeIntersector : public Intersector
{
    public:
        /// dimension enum to specify primitive types to check.
        enum {
            DimZero = (1<<0),    ///< check for points
            DimOne = (1<<1),     ///< check for lines
            DimTwo = (1<<2),     ///< check for triangles, quad
            AllDims = (DimZero | DimOne | DimTwo)
        };

        /** Construct a PolytopeIntersector using specified polytope in MODEL coordinates.*/
        PolytopeIntersector(const osg::Polytope& polytope);

        /** Construct a PolytopeIntersector using specified polytope in specified coordinate frame.*/
        PolytopeIntersector(CoordinateFrame cf, const osg::Polytope& polytope);

        /** Convenience constructor for supporting picking in WINDOW, or PROJECTION coordinates
          * In WINDOW coordinates (clip space cube) creates a five sided polytope box that has a front face at 0.0 and sides around box xMin, yMin, xMax, yMax.
          * In PROJECTION coordinates (clip space cube) creates a five sided polytope box that has a front face at -1 and sides around box xMin, yMin, xMax, yMax.
          * In VIEW and MODEL coordinates (clip space cube) creates a five sided polytope box that has a front face at 0.0 and sides around box xMin, yMin, xMax, yMax.*/
        PolytopeIntersector(CoordinateFrame cf, double xMin, double yMin, double xMax, double yMax);

        struct Intersection
        {
            Intersection():
                distance(0.0),
                maxDistance(0.0),
                numIntersectionPoints(0),
                primitiveIndex(0) {}

            bool operator < (const Intersection& rhs) const
            {
                if (distance < rhs.distance) return true;
                if (rhs.distance < distance) return false;
                if (primitiveIndex < rhs.primitiveIndex) return true;
                if (rhs.primitiveIndex < primitiveIndex) return false;
                if (nodePath < rhs.nodePath) return true;
                if (rhs.nodePath < nodePath ) return false;
                return (drawable < rhs.drawable);
            }

            enum { MaxNumIntesectionPoints=6 };

            double                          distance;     ///< distance from reference plane
            double                          maxDistance;  ///< maximum distance of intersection points from reference plane
            osg::NodePath                   nodePath;
            osg::ref_ptr<osg::Drawable>     drawable;
            osg::ref_ptr<osg::RefMatrix>    matrix;
            osg::Vec3                       localIntersectionPoint;  ///< center of all intersection points
            unsigned int                    numIntersectionPoints;
            osg::Vec3                       intersectionPoints[MaxNumIntesectionPoints];
            unsigned int                    primitiveIndex; ///< primitive index
        };

        typedef std::set<Intersection> Intersections;

        inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); }

        inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }

        inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); }

        inline unsigned int getDimensionMask() const { return _dimensionMask; }

        /** set the dimension mask.
         * As polytope-triangle and polytope-quad intersections are expensive to compute
         * it is possible to turn them off by calling setDimensionMask( DimZero | DimOne )
         */
        inline void setDimensionMask(unsigned int dimensionMask) { _dimensionMask = dimensionMask; }

        inline const osg::Plane& getReferencePlane() const { return _referencePlane; }

        /** set the plane used to sort the intersections.
         * The intersections are sorted by the distance of the localIntersectionPoint
         * and the reference plane. The default for the reference plane is the
         * last plane of the polytope.
         */
        inline void setReferencePlane(const osg::Plane& plane) { _referencePlane = plane; }

    public:

        virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);

        virtual bool enter(const osg::Node& node);

        virtual void leave();

        virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);

        virtual void reset();

        virtual bool containsIntersections() { return !getIntersections().empty(); }

    protected:

        PolytopeIntersector* _parent;

        osg::Polytope _polytope;

        unsigned int _dimensionMask; ///< mask which dimensions should be checked
        osg::Plane _referencePlane; ///< plane to use for sorting intersections

        Intersections _intersections;

};

}

#endif