IntersectVisitor
Go to the documentation of this file.
/* -*-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_INTERSECTVISITOR
#define OSGUTIL_INTERSECTVISITOR 1
#include <osg/NodeVisitor>
#include <osg/LineSegment>
#include <osg/Geode>
#include <osg/Matrix>
#include <osg/Transform>
#include <osgUtil/Export>
#include <map>
#include <set>
#include <vector>
namespace osgUtil {
class OSGUTIL_EXPORT Hit
{
public:
Hit();
Hit(const Hit& hit);
~Hit();
Hit& operator = (const Hit& hit);
typedef std::vector<int> VecIndexList;
bool operator < (const Hit& hit) const
{
if (_originalLineSegment<hit._originalLineSegment) return true;
if (hit._originalLineSegment<_originalLineSegment) return false;
return _ratio<hit._ratio;
}
const osg::Vec3& getLocalIntersectPoint() const { return _intersectPoint; }
const osg::Vec3& getLocalIntersectNormal() const { return _intersectNormal; }
const osg::Vec3 getWorldIntersectPoint() const { if (_matrix.valid()) return _intersectPoint*(*_matrix); else return _intersectPoint; }
const osg::Vec3 getWorldIntersectNormal() const ;
float getRatio() const { return _ratio; }
const osg::LineSegment* getOriginalLineSegment() const { return _originalLineSegment.get(); }
const osg::LineSegment* getLocalLineSegment() const { return _localLineSegment.get(); }
osg::NodePath& getNodePath() { return _nodePath; }
const osg::NodePath& getNodePath() const { return _nodePath; }
osg::Geode* getGeode() { return _geode.get(); }
const osg::Geode* getGeode() const { return _geode.get(); }
osg::Drawable* getDrawable() { return _drawable.get(); }
const osg::Drawable* getDrawable() const { return _drawable.get(); }
const osg::RefMatrix* getMatrix() const { return _matrix.get(); }
const osg::RefMatrix* getInverseMatrix() const { return _inverse.get(); }
const VecIndexList& getVecIndexList() const { return _vecIndexList; }
int getPrimitiveIndex() const { return _primitiveIndex; }
float _ratio;
osg::ref_ptr<osg::LineSegment> _originalLineSegment;
osg::ref_ptr<osg::LineSegment> _localLineSegment;
osg::NodePath _nodePath;
osg::ref_ptr<osg::Geode> _geode;
osg::ref_ptr<osg::Drawable> _drawable;
osg::ref_ptr<osg::RefMatrix> _matrix;
osg::ref_ptr<osg::RefMatrix> _inverse;
VecIndexList _vecIndexList;
int _primitiveIndex;
osg::Vec3 _intersectPoint;
osg::Vec3 _intersectNormal;
};
class OSGUTIL_EXPORT IntersectVisitor : public osg::NodeVisitor
{
public:
IntersectVisitor();
virtual ~IntersectVisitor();
META_NodeVisitor(osgUtil, IntersectVisitor)
void reset();
void addLineSegment(osg::LineSegment* seg);
typedef std::vector<Hit> HitList;
typedef std::map<const osg::LineSegment*,HitList > LineSegmentHitListMap;
HitList& getHitList(const osg::LineSegment* seg) { return _segHitList[seg]; }
int getNumHits(const osg::LineSegment* seg) { return _segHitList[seg].size(); }
LineSegmentHitListMap& getSegHitList() { return _segHitList; }
bool hits();
enum LODSelectionMode
{
USE_HIGHEST_LEVEL_OF_DETAIL,
USE_SEGMENT_START_POINT_AS_EYE_POINT_FOR_LOD_LEVEL_SELECTION
};
void setLODSelectionMode(LODSelectionMode mode) { _lodSelectionMode = mode; }
LODSelectionMode getLODSelectionMode() const { return _lodSelectionMode; }
void setEyePoint(const osg::Vec3& eye) { _pseudoEyePoint = eye; }
virtual osg::Vec3 getEyePoint() const;
virtual float getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const;
virtual void apply(osg::Node&);
virtual void apply(osg::Drawable&);
virtual void apply(osg::Geode& node);
virtual void apply(osg::Billboard& node);
virtual void apply(osg::Group& node);
virtual void apply(osg::Transform& node);
virtual void apply(osg::Switch& node);
virtual void apply(osg::LOD& node);
protected:
class IntersectState : public osg::Referenced
{
public:
IntersectState();
osg::ref_ptr<osg::RefMatrix> _view_matrix;
osg::ref_ptr<osg::RefMatrix> _view_inverse;
osg::ref_ptr<osg::RefMatrix> _model_matrix;
osg::ref_ptr<osg::RefMatrix> _model_inverse;
typedef std::pair<osg::ref_ptr<osg::LineSegment>,osg::ref_ptr<osg::LineSegment> > LineSegmentPair;
typedef std::vector< LineSegmentPair > LineSegmentList;
LineSegmentList _segList;
typedef unsigned int LineSegmentMask;
typedef std::vector<LineSegmentMask> LineSegmentMaskStack;
LineSegmentMaskStack _segmentMaskStack;
bool isCulled(const osg::BoundingSphere& bs,LineSegmentMask& segMaskOut);
bool isCulled(const osg::BoundingBox& bb,LineSegmentMask& segMaskOut);
void addLineSegment(osg::LineSegment* seg);
protected:
~IntersectState();
};
bool intersect(osg::Drawable& gset);
void pushMatrix(osg::RefMatrix* matrix, osg::Transform::ReferenceFrame rf);
void popMatrix();
bool enterNode(osg::Node& node);
void leaveNode();
typedef std::vector<osg::ref_ptr<IntersectState> > IntersectStateStack;
IntersectStateStack _intersectStateStack;
LineSegmentHitListMap _segHitList;
LODSelectionMode _lodSelectionMode;
osg::Vec3 _pseudoEyePoint;
};
class OSGUTIL_EXPORT PickVisitor : public osgUtil::IntersectVisitor
{
public:
PickVisitor(const osg::Viewport* viewport, const osg::Matrixd& proj, const osg::Matrixd& view, float mx, float my);
void runNestedPickVisitor(osg::Node& node, const osg::Viewport* viewport, const osg::Matrix& proj, const osg::Matrix& view, float mx, float my);
void apply(osg::Projection& projection);
void apply(osg::Camera& camera);
protected:
float _mx;
float _my;
osg::ref_ptr<const osg::Viewport> _lastViewport;
osg::Matrixd _lastProjectionMatrix;
osg::Matrixd _lastViewMatrix;
};
}
#endif
Geode
LineSegment
Matrix
NodeVisitor
Transform
osgUtil::Hit::Hit
Hit()
osgUtil::Hit::Hit
Hit(const Hit &hit)
osgUtil::Hit::_geode
osg::ref_ptr< osg::Geode > _geode
Definition IntersectVisitor:78
osgUtil::Hit::getVecIndexList
const VecIndexList & getVecIndexList() const
Definition IntersectVisitor:70
osgUtil::Hit::_originalLineSegment
osg::ref_ptr< osg::LineSegment > _originalLineSegment
Definition IntersectVisitor:75
osgUtil::Hit::getDrawable
const osg::Drawable * getDrawable() const
Definition IntersectVisitor:67
osgUtil::Hit::_primitiveIndex
int _primitiveIndex
Definition IntersectVisitor:84
osgUtil::Hit::getNodePath
osg::NodePath & getNodePath()
Definition IntersectVisitor:62
osgUtil::Hit::_drawable
osg::ref_ptr< osg::Drawable > _drawable
Definition IntersectVisitor:79
osgUtil::Hit::getWorldIntersectNormal
const osg::Vec3 getWorldIntersectNormal() const
osgUtil::Hit::getNodePath
const osg::NodePath & getNodePath() const
Definition IntersectVisitor:63
osgUtil::Hit::getPrimitiveIndex
int getPrimitiveIndex() const
Definition IntersectVisitor:71
osgUtil::Hit::_inverse
osg::ref_ptr< osg::RefMatrix > _inverse
Definition IntersectVisitor:81
osgUtil::Hit::getDrawable
osg::Drawable * getDrawable()
Definition IntersectVisitor:66
osgUtil::Hit::getRatio
float getRatio() const
Definition IntersectVisitor:59
osgUtil::Hit::getMatrix
const osg::RefMatrix * getMatrix() const
Definition IntersectVisitor:68
osgUtil::Hit::~Hit
~Hit()
osgUtil::Hit::getLocalIntersectPoint
const osg::Vec3 & getLocalIntersectPoint() const
Definition IntersectVisitor:53
osgUtil::Hit::_ratio
float _ratio
Definition IntersectVisitor:74
osgUtil::Hit::getLocalLineSegment
const osg::LineSegment * getLocalLineSegment() const
Definition IntersectVisitor:61
osgUtil::Hit::VecIndexList
std::vector< int > VecIndexList
Definition IntersectVisitor:43
osgUtil::Hit::_intersectNormal
osg::Vec3 _intersectNormal
Definition IntersectVisitor:86
osgUtil::Hit::_matrix
osg::ref_ptr< osg::RefMatrix > _matrix
Definition IntersectVisitor:80
osgUtil::Hit::getOriginalLineSegment
const osg::LineSegment * getOriginalLineSegment() const
Definition IntersectVisitor:60
osgUtil::Hit::getInverseMatrix
const osg::RefMatrix * getInverseMatrix() const
Definition IntersectVisitor:69
osgUtil::Hit::_localLineSegment
osg::ref_ptr< osg::LineSegment > _localLineSegment
Definition IntersectVisitor:76
osgUtil::Hit::_vecIndexList
VecIndexList _vecIndexList
Definition IntersectVisitor:83
osgUtil::Hit::getGeode
osg::Geode * getGeode()
Definition IntersectVisitor:64
osgUtil::Hit::getWorldIntersectPoint
const osg::Vec3 getWorldIntersectPoint() const
Definition IntersectVisitor:56
osgUtil::Hit::_nodePath
osg::NodePath _nodePath
Definition IntersectVisitor:77
osgUtil::Hit::getGeode
const osg::Geode * getGeode() const
Definition IntersectVisitor:65
osgUtil::Hit::_intersectPoint
osg::Vec3 _intersectPoint
Definition IntersectVisitor:85
osgUtil::Hit::getLocalIntersectNormal
const osg::Vec3 & getLocalIntersectNormal() const
Definition IntersectVisitor:54
osgUtil::IntersectVisitor::IntersectState::addLineSegment
void addLineSegment(osg::LineSegment *seg)
osgUtil::IntersectVisitor::IntersectState::_view_inverse
osg::ref_ptr< osg::RefMatrix > _view_inverse
Definition IntersectVisitor:162
osgUtil::IntersectVisitor::IntersectState::~IntersectState
~IntersectState()
osgUtil::IntersectVisitor::IntersectState::LineSegmentList
std::vector< LineSegmentPair > LineSegmentList
Definition IntersectVisitor:167
osgUtil::IntersectVisitor::IntersectState::_model_inverse
osg::ref_ptr< osg::RefMatrix > _model_inverse
Definition IntersectVisitor:164
osgUtil::IntersectVisitor::IntersectState::LineSegmentMask
unsigned int LineSegmentMask
Definition IntersectVisitor:170
osgUtil::IntersectVisitor::IntersectState::_segList
LineSegmentList _segList
Definition IntersectVisitor:168
osgUtil::IntersectVisitor::IntersectState::_view_matrix
osg::ref_ptr< osg::RefMatrix > _view_matrix
Definition IntersectVisitor:161
osgUtil::IntersectVisitor::IntersectState::_model_matrix
osg::ref_ptr< osg::RefMatrix > _model_matrix
Definition IntersectVisitor:163
osgUtil::IntersectVisitor::IntersectState::_segmentMaskStack
LineSegmentMaskStack _segmentMaskStack
Definition IntersectVisitor:172
osgUtil::IntersectVisitor::IntersectState::LineSegmentPair
std::pair< osg::ref_ptr< osg::LineSegment >, osg::ref_ptr< osg::LineSegment > > LineSegmentPair
Definition IntersectVisitor:166
osgUtil::IntersectVisitor::IntersectState::isCulled
bool isCulled(const osg::BoundingBox &bb, LineSegmentMask &segMaskOut)
osgUtil::IntersectVisitor::IntersectState::IntersectState
IntersectState()
osgUtil::IntersectVisitor::IntersectState::LineSegmentMaskStack
std::vector< LineSegmentMask > LineSegmentMaskStack
Definition IntersectVisitor:171
osgUtil::IntersectVisitor::IntersectState::isCulled
bool isCulled(const osg::BoundingSphere &bs, LineSegmentMask &segMaskOut)
osgUtil::IntersectVisitor
Definition IntersectVisitor:94
osgUtil::IntersectVisitor::apply
virtual void apply(osg::Billboard &node)
osgUtil::IntersectVisitor::setEyePoint
void setEyePoint(const osg::Vec3 &eye)
Definition IntersectVisitor:134
osgUtil::IntersectVisitor::META_NodeVisitor
META_NodeVisitor(osgUtil, IntersectVisitor) void reset()
osgUtil::IntersectVisitor::apply
virtual void apply(osg::LOD &node)
osgUtil::IntersectVisitor::_pseudoEyePoint
osg::Vec3 _pseudoEyePoint
Definition IntersectVisitor:200
osgUtil::IntersectVisitor::HitList
std::vector< Hit > HitList
Definition IntersectVisitor:110
osgUtil::IntersectVisitor::apply
virtual void apply(osg::Node &)
osgUtil::IntersectVisitor::apply
virtual void apply(osg::Drawable &)
osgUtil::IntersectVisitor::pushMatrix
void pushMatrix(osg::RefMatrix *matrix, osg::Transform::ReferenceFrame rf)
osgUtil::IntersectVisitor::getLODSelectionMode
LODSelectionMode getLODSelectionMode() const
Definition IntersectVisitor:128
osgUtil::IntersectVisitor::IntersectStateStack
std::vector< osg::ref_ptr< IntersectState > > IntersectStateStack
Definition IntersectVisitor:193
osgUtil::IntersectVisitor::getNumHits
int getNumHits(const osg::LineSegment *seg)
Definition IntersectVisitor:115
osgUtil::IntersectVisitor::popMatrix
void popMatrix()
osgUtil::IntersectVisitor::intersect
bool intersect(osg::Drawable &gset)
osgUtil::IntersectVisitor::hits
bool hits()
osgUtil::IntersectVisitor::leaveNode
void leaveNode()
osgUtil::IntersectVisitor::apply
virtual void apply(osg::Switch &node)
osgUtil::IntersectVisitor::getEyePoint
virtual osg::Vec3 getEyePoint() const
osgUtil::IntersectVisitor::~IntersectVisitor
virtual ~IntersectVisitor()
osgUtil::IntersectVisitor::IntersectVisitor
IntersectVisitor()
osgUtil::IntersectVisitor::apply
virtual void apply(osg::Geode &node)
osgUtil::IntersectVisitor::_segHitList
LineSegmentHitListMap _segHitList
Definition IntersectVisitor:197
osgUtil::IntersectVisitor::addLineSegment
void addLineSegment(osg::LineSegment *seg)
osgUtil::IntersectVisitor::getSegHitList
LineSegmentHitListMap & getSegHitList()
Definition IntersectVisitor:117
osgUtil::IntersectVisitor::_lodSelectionMode
LODSelectionMode _lodSelectionMode
Definition IntersectVisitor:199
osgUtil::IntersectVisitor::getDistanceToEyePoint
virtual float getDistanceToEyePoint(const osg::Vec3 &pos, bool withLODScale) const
osgUtil::IntersectVisitor::_intersectStateStack
IntersectStateStack _intersectStateStack
Definition IntersectVisitor:195
osgUtil::IntersectVisitor::setLODSelectionMode
void setLODSelectionMode(LODSelectionMode mode)
Definition IntersectVisitor:127
osgUtil::IntersectVisitor::enterNode
bool enterNode(osg::Node &node)
osgUtil::IntersectVisitor::apply
virtual void apply(osg::Transform &node)
osgUtil::IntersectVisitor::LODSelectionMode
LODSelectionMode
Definition IntersectVisitor:122
osgUtil::IntersectVisitor::USE_HIGHEST_LEVEL_OF_DETAIL
@ USE_HIGHEST_LEVEL_OF_DETAIL
Definition IntersectVisitor:123
osgUtil::IntersectVisitor::USE_SEGMENT_START_POINT_AS_EYE_POINT_FOR_LOD_LEVEL_SELECTION
@ USE_SEGMENT_START_POINT_AS_EYE_POINT_FOR_LOD_LEVEL_SELECTION
Definition IntersectVisitor:124
osgUtil::IntersectVisitor::apply
virtual void apply(osg::Group &node)
osgUtil::IntersectVisitor::LineSegmentHitListMap
std::map< const osg::LineSegment *, HitList > LineSegmentHitListMap
Definition IntersectVisitor:111
osgUtil::IntersectVisitor::getHitList
HitList & getHitList(const osg::LineSegment *seg)
Definition IntersectVisitor:113
osgUtil::PickVisitor::PickVisitor
PickVisitor(const osg::Viewport *viewport, const osg::Matrixd &proj, const osg::Matrixd &view, float mx, float my)
osgUtil::PickVisitor::_lastViewport
osg::ref_ptr< const osg::Viewport > _lastViewport
Definition IntersectVisitor:221
osgUtil::PickVisitor::_lastViewMatrix
osg::Matrixd _lastViewMatrix
Definition IntersectVisitor:223
osgUtil::PickVisitor::_my
float _my
Definition IntersectVisitor:219
osgUtil::PickVisitor::runNestedPickVisitor
void runNestedPickVisitor(osg::Node &node, const osg::Viewport *viewport, const osg::Matrix &proj, const osg::Matrix &view, float mx, float my)
osgUtil::PickVisitor::_mx
float _mx
Definition IntersectVisitor:218
osgUtil::PickVisitor::apply
void apply(osg::Camera &camera)
osgUtil::PickVisitor::_lastProjectionMatrix
osg::Matrixd _lastProjectionMatrix
Definition IntersectVisitor:222
osgUtil::PickVisitor::apply
void apply(osg::Projection &projection)
osg::Billboard
Definition Billboard:27
osg::Camera
Definition Camera:45
osg::Drawable
Definition Drawable:89
osg::Geode
Definition Geode:29
osg::Group
Definition Group:29
osg::LOD
Definition LOD:36
osg::LineSegment
Definition LineSegment:25
osg::Matrixd
Definition Matrixd:27
osg::NodeVisitor
Definition NodeVisitor:82
osg::NodeVisitor::reset
virtual void reset()
Definition NodeVisitor:167
osg::Node
Definition Node:72
osg::Projection
Definition Projection:25
osg::Referenced
Definition Referenced:44
osg::Switch
Definition Switch:27
osg::Transform
Definition Transform:75
osg::Transform::ReferenceFrame
ReferenceFrame
Definition Transform:98
osg::Viewport
Definition Viewport:24
osg::ref_ptr
Definition ref_ptr:32
osgUtil
Definition NodeVisitor:25
osg::Vec3
Vec3f Vec3
Definition Vec3:21
osg::BoundingBox
BoundingBoxd BoundingBox
Definition BoundingBox:257
osg::BoundingSphere
BoundingSphered BoundingSphere
Definition BoundingSphere:314
osg::Matrix
Matrixd Matrix
Definition Matrix:27
osg::NodePath
std::vector< Node * > NodePath
Definition Node:47
osg::RefMatrix
RefMatrixd RefMatrix
Definition Matrix:28
Export
OSGUTIL_EXPORT
#define OSGUTIL_EXPORT
Definition osgUtil/Export:40