IntersectionVisitor 11.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
/* -*-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_INTERSECTIONVISITOR
#define OSGUTIL_INTERSECTIONVISITOR 1

#include <osg/NodeVisitor>
#include <osg/Drawable>
#include <osgUtil/Export>

#include <list>

namespace osgUtil
{

// forward declare to allow Intersector to reference it.
class IntersectionVisitor;

/** Pure virtual base class for implementing custom intersection technique.
  * To implement a specific intersection technique on must override all
  * the pure virtue methods, concrete examples of how to do this can be seen in
  * the LineSegmentIntersector. */
class Intersector : public osg::Referenced
{
    public:

        enum CoordinateFrame
        {
            WINDOW,
            PROJECTION,
            VIEW,
            MODEL
        };

        enum IntersectionLimit
        {
            NO_LIMIT,
            LIMIT_ONE_PER_DRAWABLE,
            LIMIT_ONE,
            LIMIT_NEAREST
        };

        Intersector(CoordinateFrame cf=MODEL):
            _coordinateFrame(cf),
            _intersectionLimit(NO_LIMIT),
            _disabledCount(0) {}


        void setCoordinateFrame(CoordinateFrame cf) { _coordinateFrame = cf; }

        CoordinateFrame getCoordinateFrame() const { return _coordinateFrame; }

        void setIntersectionLimit(IntersectionLimit limit) { _intersectionLimit = limit; }

        IntersectionLimit getIntersectionLimit() const { return _intersectionLimit; }

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

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

        virtual void leave() = 0;

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

        virtual void reset() { _disabledCount = 0; }

        virtual bool containsIntersections() = 0;

        inline bool disabled() const { return _disabledCount!=0; }

        inline void incrementDisabledCount() { ++_disabledCount; }

        inline void decrementDisabledCount() { if (_disabledCount>0) --_disabledCount; }

        inline bool reachedLimit() { return _intersectionLimit == LIMIT_ONE && containsIntersections(); }

   protected:

        CoordinateFrame _coordinateFrame;
        IntersectionLimit _intersectionLimit;
        unsigned int    _disabledCount;

};


/** Concrete class for passing multiple intersectors through the scene graph.
  * To be used in conjunction with IntersectionVisitor. */
class OSGUTIL_EXPORT IntersectorGroup : public Intersector
{
    public:

        IntersectorGroup();

        /** Add an Intersector. */
        void addIntersector(Intersector* intersector);

        typedef std::vector< osg::ref_ptr<Intersector> > Intersectors;

        /** Get the list of intersector. */
        Intersectors& getIntersectors() { return _intersectors; }

        /** Clear the list of intersectors.*/
        void clear();

    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();

    protected:

        Intersectors _intersectors;

};

/** InteresectionVisitor is used to testing for intersections with the scene, traversing the scene using generic osgUtil::Intersector's to test against the scene.
  * To implement different types of intersection techniques, one implements custom versions of the osgUtil::Intersector, and then
  * pass the constructed intersector to the IntersectionVisitor.*/
class OSGUTIL_EXPORT IntersectionVisitor : public osg::NodeVisitor
{
    public:

        /** Callback used to implement the reading of external files, allowing support for paged databases to be
          * integrated with IntersectionVisitor.  A concrete implementation can be found in osgDB.
          * Note, this loose coupling approach is required as osgUtil is independent from osgDB where the file reading
          * is implemented, and osgDB itself is dependent upon osgUtil so a circular dependency would result from
          * tighter integration.*/
        struct ReadCallback : public osg::Referenced
        {
            virtual osg::Node* readNodeFile(const std::string& filename) = 0;
        };


        IntersectionVisitor(Intersector* intersector=0, ReadCallback* readCallback=0);

        META_NodeVisitor("osgUtil","IntersectionVisitor")

        virtual void reset();


        /** Set the intersector that will be used to intersect with the scene, and to store any hits that occur.*/
        void setIntersector(Intersector* intersector);

        /** Get the intersector that will be used to intersect with the scene, and to store any hits that occur.*/
        Intersector* getIntersector() { return _intersectorStack.empty() ? 0 : _intersectorStack.front().get(); }

        /** Get the const intersector that will be used to intersect with the scene, and to store any hits that occur.*/
        const Intersector* getIntersector() const { return _intersectorStack.empty() ? 0 : _intersectorStack.front().get(); }


        /** Set whether the intersectors should use KdTrees when they are found on the scene graph.*/
        void setUseKdTreeWhenAvailable(bool useKdTrees) { _useKdTreesWhenAvailable = useKdTrees; }

        /** Set whether the intersectors should use KdTrees.*/
        bool getUseKdTreeWhenAvailable() const { return _useKdTreesWhenAvailable; }

        void setDoDummyTraversal(bool dummy) { _dummyTraversal = dummy; }
        bool getDoDummyTraversal() const { return _dummyTraversal; }


        /** Set the read callback.*/
        void setReadCallback(ReadCallback* rc) { _readCallback = rc; }

        /** Get the read callback.*/
        ReadCallback* getReadCallback() { return _readCallback.get(); }

        /** Get the const read callback.*/
        const ReadCallback* getReadCallback() const { return _readCallback.get(); }


        void pushWindowMatrix(osg::RefMatrix* matrix) { _windowStack.push_back(matrix); _eyePointDirty = true; }
        void pushWindowMatrix(osg::Viewport* viewport) { _windowStack.push_back(new osg::RefMatrix( viewport->computeWindowMatrix()) ); _eyePointDirty = true; }
        void popWindowMatrix() { _windowStack.pop_back(); _eyePointDirty = true; }
        osg::RefMatrix* getWindowMatrix() { return _windowStack.empty() ? 0 :  _windowStack.back().get(); }
        const osg::RefMatrix* getWindowMatrix() const { return _windowStack.empty() ? 0 :  _windowStack.back().get(); }

        void pushProjectionMatrix(osg::RefMatrix* matrix) { _projectionStack.push_back(matrix); _eyePointDirty = true; }
        void popProjectionMatrix() { _projectionStack.pop_back(); _eyePointDirty = true; }
        osg::RefMatrix* getProjectionMatrix() { return _projectionStack.empty() ? 0 :  _projectionStack.back().get(); }
        const osg::RefMatrix* getProjectionMatrix() const { return _projectionStack.empty() ? 0 :  _projectionStack.back().get(); }

        void pushViewMatrix(osg::RefMatrix* matrix) { _viewStack.push_back(matrix); _eyePointDirty = true; }
        void popViewMatrix() { _viewStack.pop_back(); _eyePointDirty = true; }
        osg::RefMatrix* getViewMatrix() { return _viewStack.empty() ? 0 :  _viewStack.back().get(); }
        const osg::RefMatrix* getViewMatrix() const { return _viewStack.empty() ? 0 :  _viewStack.back().get(); }

        void pushModelMatrix(osg::RefMatrix* matrix) { _modelStack.push_back(matrix); _eyePointDirty = true; }
        void popModelMatrix() { _modelStack.pop_back(); _eyePointDirty = true; }
        osg::RefMatrix* getModelMatrix() { return _modelStack.empty() ? 0 :  _modelStack.back().get(); }
        const osg::RefMatrix* getModelMatrix() const { return _modelStack.empty() ? 0 :  _modelStack.back().get(); }


        /** Set the reference eye point that is used for nodes that require an eye point to position themselves, such as billboards.*/
        void setReferenceEyePoint(const osg::Vec3& ep) { _referenceEyePoint = ep; _eyePointDirty = true; }

        /** Get the reference eye point.*/
        const osg::Vec3& getReferenceEyePoint() const { return _referenceEyePoint; }

        /** Set the coordinate frame of the reference eye point.*/
        void setReferenceEyePointCoordinateFrame(Intersector::CoordinateFrame cf) { _referenceEyePointCoordinateFrame = cf; }

        /** Get the coordinate frame of the reference eye point.*/
        Intersector::CoordinateFrame getReferenceEyePointCoordinateFrame() const { return _referenceEyePointCoordinateFrame; }


        /** Get the eye point in the local coordinate frame a given traversal point.*/
        virtual osg::Vec3 getEyePoint() const;

        enum LODSelectionMode
        {
            USE_HIGHEST_LEVEL_OF_DETAIL,
            USE_EYE_POINT_FOR_LOD_LEVEL_SELECTION
        };

        /** Set the LOD selection scheme.*/
        void setLODSelectionMode(LODSelectionMode mode) { _lodSelectionMode = mode; }

        /** Get the LOD selection scheme.*/
        LODSelectionMode getLODSelectionMode() const { return _lodSelectionMode; }

         /** Get the distance from a point to the eye point, distance value in local coordinate system.
          * This is calculated using the pseudo-EyePoint (above) when doing LOD calculcations. */
        virtual float getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const;

    public:

        virtual void apply(osg::Node& node);
        virtual void apply(osg::Geode& geode);
        virtual void apply(osg::Billboard& geode);
        virtual void apply(osg::Group& group);
        virtual void apply(osg::LOD& lod);
        virtual void apply(osg::PagedLOD& lod);
        virtual void apply(osg::Transform& transform);
        virtual void apply(osg::Projection& projection);
        virtual void apply(osg::Camera& camera);

    protected:

        inline bool enter(const osg::Node& node) { return _intersectorStack.empty() ? false : _intersectorStack.back()->enter(node); }
        inline void leave() { _intersectorStack.back()->leave(); }
        inline void intersect(osg::Drawable* drawable) { _intersectorStack.back()->intersect(*this, drawable); }
        inline void push_clone() { _intersectorStack.push_back ( _intersectorStack.front()->clone(*this) ); }
        inline void pop_clone() { if (_intersectorStack.size()>=2) _intersectorStack.pop_back(); }

        typedef std::list< osg::ref_ptr<Intersector> > IntersectorStack;
        IntersectorStack _intersectorStack;

        bool _useKdTreesWhenAvailable;
        bool _dummyTraversal;

        osg::ref_ptr<ReadCallback> _readCallback;

        typedef std::list< osg::ref_ptr<osg::RefMatrix> > MatrixStack;
        MatrixStack _windowStack;
        MatrixStack _projectionStack;
        MatrixStack _viewStack;
        MatrixStack _modelStack;

        osg::Vec3                       _referenceEyePoint;
        Intersector::CoordinateFrame    _referenceEyePointCoordinateFrame;
        LODSelectionMode                _lodSelectionMode;

        mutable bool                    _eyePointDirty;
        mutable osg::Vec3               _eyePoint;
};

}

#endif