2011-03-12 01:20:24 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2006-10-27 00:03:17 +08:00
*
2011-03-12 01:20:24 +08:00
* 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
2006-10-27 00:03:17 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2011-03-12 01:20:24 +08:00
*
2006-10-27 00:03:17 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2011-03-12 01:20:24 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2006-10-27 00:03:17 +08:00
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGUTIL_INTERSECTIONVISITOR
#define OSGUTIL_INTERSECTIONVISITOR 1
#include <osg/NodeVisitor>
2006-10-31 04:29:06 +08:00
#include <osg/Drawable>
2006-10-27 00:03:17 +08:00
#include <osgUtil/Export>
#include <list>
namespace osgUtil
{
// forward declare to allow Intersector to reference it.
class IntersectionVisitor;
2007-12-11 01:30:18 +08:00
/** Pure virtual base class for implementing custom intersection technique.
2006-10-27 00:03:17 +08:00
* 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:
2011-03-12 01:20:24 +08:00
2006-11-01 22:41:32 +08:00
enum CoordinateFrame
{
WINDOW,
PROJECTION,
VIEW,
MODEL
};
2011-03-14 18:07:15 +08:00
enum IntersectionLimit
{
NO_LIMIT,
LIMIT_ONE_PER_DRAWABLE,
LIMIT_ONE,
LIMIT_NEAREST
};
2013-10-03 16:45:57 +08:00
Intersector(CoordinateFrame cf=MODEL, IntersectionLimit il=NO_LIMIT):
2006-11-02 01:18:45 +08:00
_coordinateFrame(cf),
2013-10-03 16:45:57 +08:00
_intersectionLimit(il),
2013-11-07 20:35:34 +08:00
_disabledCount(0),
_precisionHint(USE_DOUBLE_CALCULATIONS) {}
2011-03-12 01:20:24 +08:00
2006-11-01 22:41:32 +08:00
void setCoordinateFrame(CoordinateFrame cf) { _coordinateFrame = cf; }
2011-03-12 01:20:24 +08:00
2006-11-01 22:41:32 +08:00
CoordinateFrame getCoordinateFrame() const { return _coordinateFrame; }
2011-03-14 18:07:15 +08:00
void setIntersectionLimit(IntersectionLimit limit) { _intersectionLimit = limit; }
IntersectionLimit getIntersectionLimit() const { return _intersectionLimit; }
2006-10-27 00:03:17 +08:00
virtual Intersector* clone(osgUtil::IntersectionVisitor& iv) = 0;
2011-03-12 01:20:24 +08:00
2006-10-27 00:03:17 +08:00
virtual bool enter(const osg::Node& node) = 0;
2011-03-12 01:20:24 +08:00
2006-10-27 00:03:17 +08:00
virtual void leave() = 0;
2011-03-12 01:20:24 +08:00
2006-10-27 00:03:17 +08:00
virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable) = 0;
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
virtual void reset() { _disabledCount = 0; }
2011-03-12 01:20:24 +08:00
2006-10-27 00:03:17 +08:00
virtual bool containsIntersections() = 0;
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
inline bool disabled() const { return _disabledCount!=0; }
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
inline void incrementDisabledCount() { ++_disabledCount; }
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
inline void decrementDisabledCount() { if (_disabledCount>0) --_disabledCount; }
2011-03-14 18:07:15 +08:00
inline bool reachedLimit() { return _intersectionLimit == LIMIT_ONE && containsIntersections(); }
2013-11-07 20:35:34 +08:00
/** Hint to precision used in the internal intersections calculations.*/
enum PrecisionHint
{
USE_DOUBLE_CALCULATIONS,
USE_FLOAT_CALCULATIONS
};
/** Set the hint to what precision to use in the intersections calculations.*/
void setPrecisionHint(PrecisionHint hint) { _precisionHint = hint; }
/** Get the hint to what precision should be used in the intersections calculations.*/
PrecisionHint getPrecisionHint() const { return _precisionHint; }
2013-07-31 21:11:49 +08:00
protected:
CoordinateFrame _coordinateFrame;
2011-03-14 18:07:15 +08:00
IntersectionLimit _intersectionLimit;
2013-07-31 21:11:49 +08:00
unsigned int _disabledCount;
2013-11-07 20:35:34 +08:00
PrecisionHint _precisionHint;
2006-10-27 00:03:17 +08:00
};
2006-11-28 04:25:36 +08:00
2007-12-11 01:30:18 +08:00
/** Concrete class for passing multiple intersectors through the scene graph.
2006-10-27 23:11:17 +08:00
* To be used in conjunction with IntersectionVisitor. */
class OSGUTIL_EXPORT IntersectorGroup : public Intersector
{
public:
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
IntersectorGroup();
/** Add an Intersector. */
void addIntersector(Intersector* intersector);
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
typedef std::vector< osg::ref_ptr<Intersector> > Intersectors;
/** Get the list of intersector. */
Intersectors& getIntersectors() { return _intersectors; }
2011-03-12 01:20:24 +08:00
/** Clear the list of intersectors.*/
2006-10-27 23:11:17 +08:00
void clear();
public:
virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
virtual bool enter(const osg::Node& node);
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
virtual void leave();
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
virtual void reset();
virtual bool containsIntersections();
protected:
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
Intersectors _intersectors;
2011-03-12 01:20:24 +08:00
2006-10-27 23:11:17 +08:00
};
2006-10-27 00:03:17 +08:00
/** 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
2007-12-11 01:30:18 +08:00
* integrated with IntersectionVisitor. A concrete implementation can be found in osgDB.
2006-10-27 00:03:17 +08:00
* 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);
2011-03-12 01:20:24 +08:00
2013-01-25 02:48:34 +08:00
META_NodeVisitor(osgUtil, IntersectionVisitor)
2008-12-17 20:13:15 +08:00
2006-10-27 00:03:17 +08:00
virtual void reset();
2008-07-09 01:35:10 +08:00
2006-10-27 00:03:17 +08:00
/** Set the intersector that will be used to intersect with the scene, and to store any hits that occur.*/
void setIntersector(Intersector* intersector);
2011-03-12 01:20:24 +08:00
2006-10-27 00:03:17 +08:00
/** 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(); }
2008-07-09 01:35:10 +08:00
/** Set whether the intersectors should use KdTrees when they are found on the scene graph.*/
2008-07-09 02:02:09 +08:00
void setUseKdTreeWhenAvailable(bool useKdTrees) { _useKdTreesWhenAvailable = useKdTrees; }
2011-03-12 01:20:24 +08:00
2008-07-09 01:35:10 +08:00
/** Set whether the intersectors should use KdTrees.*/
bool getUseKdTreeWhenAvailable() const { return _useKdTreesWhenAvailable; }
2011-03-12 01:20:24 +08:00
2008-07-10 03:40:10 +08:00
void setDoDummyTraversal(bool dummy) { _dummyTraversal = dummy; }
bool getDoDummyTraversal() const { return _dummyTraversal; }
2008-07-09 01:35:10 +08:00
2006-10-27 00:03:17 +08:00
/** 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(); }
2011-03-12 01:20:24 +08:00
2008-08-14 22:22:39 +08:00
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; }
2006-10-27 00:03:17 +08:00
osg::RefMatrix* getWindowMatrix() { return _windowStack.empty() ? 0 : _windowStack.back().get(); }
2007-06-08 17:17:42 +08:00
const osg::RefMatrix* getWindowMatrix() const { return _windowStack.empty() ? 0 : _windowStack.back().get(); }
2006-11-01 22:41:32 +08:00
2008-08-14 22:22:39 +08:00
void pushProjectionMatrix(osg::RefMatrix* matrix) { _projectionStack.push_back(matrix); _eyePointDirty = true; }
void popProjectionMatrix() { _projectionStack.pop_back(); _eyePointDirty = true; }
2006-10-27 00:03:17 +08:00
osg::RefMatrix* getProjectionMatrix() { return _projectionStack.empty() ? 0 : _projectionStack.back().get(); }
2007-06-08 17:17:42 +08:00
const osg::RefMatrix* getProjectionMatrix() const { return _projectionStack.empty() ? 0 : _projectionStack.back().get(); }
2006-11-01 22:41:32 +08:00
2008-08-14 22:22:39 +08:00
void pushViewMatrix(osg::RefMatrix* matrix) { _viewStack.push_back(matrix); _eyePointDirty = true; }
void popViewMatrix() { _viewStack.pop_back(); _eyePointDirty = true; }
2006-10-27 00:03:17 +08:00
osg::RefMatrix* getViewMatrix() { return _viewStack.empty() ? 0 : _viewStack.back().get(); }
2007-06-08 17:17:42 +08:00
const osg::RefMatrix* getViewMatrix() const { return _viewStack.empty() ? 0 : _viewStack.back().get(); }
2006-10-27 00:03:17 +08:00
2008-08-14 22:22:39 +08:00
void pushModelMatrix(osg::RefMatrix* matrix) { _modelStack.push_back(matrix); _eyePointDirty = true; }
2011-03-12 01:20:24 +08:00
void popModelMatrix() { _modelStack.pop_back(); _eyePointDirty = true; }
2006-11-01 22:41:32 +08:00
osg::RefMatrix* getModelMatrix() { return _modelStack.empty() ? 0 : _modelStack.back().get(); }
2007-06-08 17:17:42 +08:00
const osg::RefMatrix* getModelMatrix() const { return _modelStack.empty() ? 0 : _modelStack.back().get(); }
2006-10-27 00:03:17 +08:00
2008-08-14 22:22:39 +08:00
/** 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; }
2011-03-12 01:20:24 +08:00
2008-08-14 22:22:39 +08:00
/** 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; }
2011-03-12 01:20:24 +08:00
2008-08-14 22:22:39 +08:00
/** 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
};
2011-03-12 01:20:24 +08:00
2008-08-14 22:22:39 +08:00
/** 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;
2006-10-27 00:03:17 +08:00
public:
virtual void apply(osg::Node& node);
2014-05-16 00:30:17 +08:00
virtual void apply(osg::Drawable& drawable);
2006-10-27 00:03:17 +08:00
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);
2006-11-27 22:52:07 +08:00
virtual void apply(osg::Camera& camera);
2011-03-12 01:20:24 +08:00
2006-10-27 00:03:17 +08:00
protected:
2011-03-12 01:20:24 +08:00
2006-10-27 00:03:17 +08:00
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) ); }
2006-10-27 23:11:17 +08:00
inline void pop_clone() { if (_intersectorStack.size()>=2) _intersectorStack.pop_back(); }
2006-10-27 00:03:17 +08:00
typedef std::list< osg::ref_ptr<Intersector> > IntersectorStack;
IntersectorStack _intersectorStack;
2008-07-09 01:35:10 +08:00
bool _useKdTreesWhenAvailable;
2008-07-10 03:40:10 +08:00
bool _dummyTraversal;
2011-03-12 01:20:24 +08:00
2006-10-27 00:03:17 +08:00
osg::ref_ptr<ReadCallback> _readCallback;
2011-03-12 01:20:24 +08:00
2006-10-27 00:03:17 +08:00
typedef std::list< osg::ref_ptr<osg::RefMatrix> > MatrixStack;
MatrixStack _windowStack;
MatrixStack _projectionStack;
MatrixStack _viewStack;
MatrixStack _modelStack;
2008-08-14 22:22:39 +08:00
osg::Vec3 _referenceEyePoint;
Intersector::CoordinateFrame _referenceEyePointCoordinateFrame;
LODSelectionMode _lodSelectionMode;
mutable bool _eyePointDirty;
mutable osg::Vec3 _eyePoint;
2006-10-27 00:03:17 +08:00
};
}
#endif