OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IntersectVisitor.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSGUTIL_INTERSECTVISITOR
15 #define OSGUTIL_INTERSECTVISITOR 1
16 
17 #include <osg/NodeVisitor>
18 #include <osg/LineSegment>
19 #include <osg/Geode>
20 #include <osg/Matrix>
21 #include <osg/Transform>
22 
23 #include <osgUtil/Export>
24 
25 #include <map>
26 #include <set>
27 #include <vector>
28 
29 namespace osgUtil {
30 
31 
33 {
35  public:
36 
37  Hit();
38  Hit(const Hit& hit);
39  ~Hit();
40 
41  Hit& operator = (const Hit& hit);
42 
43  typedef std::vector<int> VecIndexList;
44 
45  bool operator < (const Hit& hit) const
46  {
47  if (_originalLineSegment<hit._originalLineSegment) return true;
48  if (hit._originalLineSegment<_originalLineSegment) return false;
49  return _ratio<hit._ratio;
50  }
51 
52 
53  const osg::Vec3& getLocalIntersectPoint() const { return _intersectPoint; }
54  const osg::Vec3& getLocalIntersectNormal() const { return _intersectNormal; }
55 
56  const osg::Vec3 getWorldIntersectPoint() const { if (_matrix.valid()) return _intersectPoint*(*_matrix); else return _intersectPoint; }
57  const osg::Vec3 getWorldIntersectNormal() const ;
58 
59  float getRatio() const { return _ratio; }
60  const osg::LineSegment* getOriginalLineSegment() const { return _originalLineSegment.get(); }
61  const osg::LineSegment* getLocalLineSegment() const { return _localLineSegment.get(); }
62  osg::NodePath& getNodePath() { return _nodePath; }
63  const osg::NodePath& getNodePath() const { return _nodePath; }
64  osg::Geode* getGeode() { return _geode.get(); }
65  const osg::Geode* getGeode() const { return _geode.get(); }
66  osg::Drawable* getDrawable() { return _drawable.get(); }
67  const osg::Drawable* getDrawable() const { return _drawable.get(); }
68  const osg::RefMatrix* getMatrix() const { return _matrix.get(); }
69  const osg::RefMatrix* getInverseMatrix() const { return _inverse.get(); }
70  const VecIndexList& getVecIndexList() const { return _vecIndexList; }
71  int getPrimitiveIndex() const { return _primitiveIndex; }
72 
73 
74  float _ratio;
82 
83  VecIndexList _vecIndexList;
87 
88 
89 };
90 
91 
94 {
95  public:
96 
98  virtual ~IntersectVisitor();
99 
101 
102  void reset();
103 
108  void addLineSegment(osg::LineSegment* seg);
109 
110  typedef std::vector<Hit> HitList;
111  typedef std::map<const osg::LineSegment*,HitList > LineSegmentHitListMap;
112 
113  HitList& getHitList(const osg::LineSegment* seg) { return _segHitList[seg]; }
114 
115  int getNumHits(const osg::LineSegment* seg) { return _segHitList[seg].size(); }
116 
117  LineSegmentHitListMap& getSegHitList() { return _segHitList; }
118 
119  bool hits();
120 
122  {
124  USE_SEGMENT_START_POINT_AS_EYE_POINT_FOR_LOD_LEVEL_SELECTION
125  };
126 
127  void setLODSelectionMode(LODSelectionMode mode) { _lodSelectionMode = mode; }
128  LODSelectionMode getLODSelectionMode() const { return _lodSelectionMode; }
129 
134  void setEyePoint(const osg::Vec3& eye) { _pseudoEyePoint = eye; }
135 
136  virtual osg::Vec3 getEyePoint() const;
137 
138 
141  virtual float getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const;
142 
143  virtual void apply(osg::Node&);
144  virtual void apply(osg::Drawable&);
145  virtual void apply(osg::Geode& node);
146  virtual void apply(osg::Billboard& node);
147 
148  virtual void apply(osg::Group& node);
149  virtual void apply(osg::Transform& node);
150  virtual void apply(osg::Switch& node);
151  virtual void apply(osg::LOD& node);
152 
153  protected:
154 
156  {
157  public:
158 
159  IntersectState();
160 
165 
166  typedef std::pair<osg::ref_ptr<osg::LineSegment>,osg::ref_ptr<osg::LineSegment> > LineSegmentPair;
167  typedef std::vector< LineSegmentPair > LineSegmentList;
168  LineSegmentList _segList;
169 
170  typedef unsigned int LineSegmentMask;
171  typedef std::vector<LineSegmentMask> LineSegmentMaskStack;
172  LineSegmentMaskStack _segmentMaskStack;
173 
174  bool isCulled(const osg::BoundingSphere& bs,LineSegmentMask& segMaskOut);
175  bool isCulled(const osg::BoundingBox& bb,LineSegmentMask& segMaskOut);
176 
177  void addLineSegment(osg::LineSegment* seg);
178 
179  protected:
180 
181  ~IntersectState();
182 
183  };
184 
185  bool intersect(osg::Drawable& gset);
186 
187  void pushMatrix(osg::RefMatrix* matrix, osg::Transform::ReferenceFrame rf);
188  void popMatrix();
189 
190  bool enterNode(osg::Node& node);
191  void leaveNode();
192 
193  typedef std::vector<osg::ref_ptr<IntersectState> > IntersectStateStack;
194 
195  IntersectStateStack _intersectStateStack;
196 
197  LineSegmentHitListMap _segHitList;
198 
201 };
202 
205 {
206  public:
207 
208  PickVisitor(const osg::Viewport* viewport, const osg::Matrixd& proj, const osg::Matrixd& view, float mx, float my);
209 
210  void runNestedPickVisitor(osg::Node& node, const osg::Viewport* viewport, const osg::Matrix& proj, const osg::Matrix& view, float mx, float my);
211 
212  void apply(osg::Projection& projection);
213 
214  void apply(osg::Camera& camera);
215 
216  protected:
217 
218  float _mx;
219  float _my;
220 
224 };
225 
226 }
227 
228 #endif
229 
std::vector< LineSegmentMask > LineSegmentMaskStack
osg::NodePath _nodePath
int getNumHits(const osg::LineSegment *seg)
std::pair< osg::ref_ptr< osg::LineSegment >, osg::ref_ptr< osg::LineSegment > > LineSegmentPair
const osg::RefMatrix * getMatrix() const
std::vector< Hit > HitList
IntersectStateStack _intersectStateStack
const osg::LineSegment * getLocalLineSegment() const
LineSegmentHitListMap _segHitList
std::vector< LineSegmentPair > LineSegmentList
osg::Drawable * getDrawable()
osg::Vec3 _intersectNormal
osg::ref_ptr< osg::Drawable > _drawable
LODSelectionMode getLODSelectionMode() const
const osg::Vec3 & getLocalIntersectNormal() const
const VecIndexList & getVecIndexList() const
osg::Vec3 _intersectPoint
Definition: LOD.h:35
const osg::Vec3 & getLocalIntersectPoint() const
osg::ref_ptr< osg::Geode > _geode
HitList & getHitList(const osg::LineSegment *seg)
osg::ref_ptr< osg::RefMatrix > _model_matrix
const osg::RefMatrix * getInverseMatrix() const
std::map< const osg::LineSegment *, HitList > LineSegmentHitListMap
osg::ref_ptr< osg::LineSegment > _originalLineSegment
void setEyePoint(const osg::Vec3 &eye)
osg::ref_ptr< osg::RefMatrix > _view_inverse
LineSegmentHitListMap & getSegHitList()
#define META_NodeVisitor(library, name)
Definition: NodeVisitor.h:49
osg::ref_ptr< osg::RefMatrix > _inverse
osg::ref_ptr< osg::LineSegment > _localLineSegment
const osg::Vec3 getWorldIntersectPoint() const
osg::ref_ptr< osg::RefMatrix > _matrix
osg::Geode * getGeode()
int getPrimitiveIndex() const
VecIndexList _vecIndexList
std::vector< int > VecIndexList
const osg::Geode * getGeode() const
const osg::NodePath & getNodePath() const
Definition: Node.h:71
const osg::LineSegment * getOriginalLineSegment() const
const osg::Drawable * getDrawable() const
#define OSGUTIL_EXPORT
Definition: Export.h:40
osg::ref_ptr< osg::RefMatrix > _model_inverse
osg::Matrixd _lastProjectionMatrix
osg::NodePath & getNodePath()
std::vector< osg::ref_ptr< IntersectState > > IntersectStateStack
osg::Matrixd _lastViewMatrix
float getRatio() const
osg::ref_ptr< const osg::Viewport > _lastViewport
void setLODSelectionMode(LODSelectionMode mode)
osg::ref_ptr< osg::RefMatrix > _view_matrix
LODSelectionMode _lodSelectionMode
std::vector< Node * > NodePath
Definition: Node.h:44
Shader generator framework.
Definition: RenderInfo.h:20