IntersectVisitor 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #ifndef OSGUTIL_INTERSECTVISITOR
  14. #define OSGUTIL_INTERSECTVISITOR 1
  15. #include <osg/NodeVisitor>
  16. #include <osg/LineSegment>
  17. #include <osg/Geode>
  18. #include <osg/Matrix>
  19. #include <osg/Transform>
  20. #include <osgUtil/Export>
  21. #include <map>
  22. #include <set>
  23. #include <vector>
  24. namespace osgUtil {
  25. /** Deprecated */
  26. class OSGUTIL_EXPORT Hit
  27. {
  28. public:
  29. Hit();
  30. Hit(const Hit& hit);
  31. ~Hit();
  32. Hit& operator = (const Hit& hit);
  33. typedef std::vector<int> VecIndexList;
  34. bool operator < (const Hit& hit) const
  35. {
  36. if (_originalLineSegment<hit._originalLineSegment) return true;
  37. if (hit._originalLineSegment<_originalLineSegment) return false;
  38. return _ratio<hit._ratio;
  39. }
  40. const osg::Vec3& getLocalIntersectPoint() const { return _intersectPoint; }
  41. const osg::Vec3& getLocalIntersectNormal() const { return _intersectNormal; }
  42. const osg::Vec3 getWorldIntersectPoint() const { if (_matrix.valid()) return _intersectPoint*(*_matrix); else return _intersectPoint; }
  43. const osg::Vec3 getWorldIntersectNormal() const ;
  44. float getRatio() const { return _ratio; }
  45. const osg::LineSegment* getOriginalLineSegment() const { return _originalLineSegment.get(); }
  46. const osg::LineSegment* getLocalLineSegment() const { return _localLineSegment.get(); }
  47. osg::NodePath& getNodePath() { return _nodePath; }
  48. const osg::NodePath& getNodePath() const { return _nodePath; }
  49. osg::Geode* getGeode() { return _geode.get(); }
  50. const osg::Geode* getGeode() const { return _geode.get(); }
  51. osg::Drawable* getDrawable() { return _drawable.get(); }
  52. const osg::Drawable* getDrawable() const { return _drawable.get(); }
  53. const osg::RefMatrix* getMatrix() const { return _matrix.get(); }
  54. const osg::RefMatrix* getInverseMatrix() const { return _inverse.get(); }
  55. const VecIndexList& getVecIndexList() const { return _vecIndexList; }
  56. int getPrimitiveIndex() const { return _primitiveIndex; }
  57. float _ratio;
  58. osg::ref_ptr<osg::LineSegment> _originalLineSegment;
  59. osg::ref_ptr<osg::LineSegment> _localLineSegment;
  60. osg::NodePath _nodePath;
  61. osg::ref_ptr<osg::Geode> _geode;
  62. osg::ref_ptr<osg::Drawable> _drawable;
  63. osg::ref_ptr<osg::RefMatrix> _matrix;
  64. osg::ref_ptr<osg::RefMatrix> _inverse;
  65. VecIndexList _vecIndexList;
  66. int _primitiveIndex;
  67. osg::Vec3 _intersectPoint;
  68. osg::Vec3 _intersectNormal;
  69. };
  70. /** Deprecated - use IntersectionVisitor instead.*/
  71. class OSGUTIL_EXPORT IntersectVisitor : public osg::NodeVisitor
  72. {
  73. public:
  74. IntersectVisitor();
  75. virtual ~IntersectVisitor();
  76. META_NodeVisitor(osgUtil, IntersectVisitor)
  77. void reset();
  78. /** Add a line segment to use for intersection testing during scene traversal.
  79. * Note, a maximum of 32 line segments can be added to a IntersectVistor,
  80. * adding more than this will result in warning being emitted to the console
  81. * and the excess segments being ignored.*/
  82. void addLineSegment(osg::LineSegment* seg);
  83. typedef std::vector<Hit> HitList;
  84. typedef std::map<const osg::LineSegment*,HitList > LineSegmentHitListMap;
  85. HitList& getHitList(const osg::LineSegment* seg) { return _segHitList[seg]; }
  86. int getNumHits(const osg::LineSegment* seg) { return _segHitList[seg].size(); }
  87. LineSegmentHitListMap& getSegHitList() { return _segHitList; }
  88. bool hits();
  89. enum LODSelectionMode
  90. {
  91. USE_HIGHEST_LEVEL_OF_DETAIL,
  92. USE_SEGMENT_START_POINT_AS_EYE_POINT_FOR_LOD_LEVEL_SELECTION
  93. };
  94. void setLODSelectionMode(LODSelectionMode mode) { _lodSelectionMode = mode; }
  95. LODSelectionMode getLODSelectionMode() const { return _lodSelectionMode; }
  96. /** Set the eye point in local coordinates.
  97. * This is a pseudo-EyePoint for billboarding and LOD purposes.
  98. * It is copied from the Start point of the most-recently-added segment
  99. * of the intersection ray set (IntersectState::_segList). */
  100. void setEyePoint(const osg::Vec3& eye) { _pseudoEyePoint = eye; }
  101. virtual osg::Vec3 getEyePoint() const;
  102. /** Get the distance from a point to the eye point, distance value in local coordinate system.
  103. * This is calculated using the pseudo-EyePoint (above) when doing LOD calculcations. */
  104. virtual float getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const;
  105. virtual void apply(osg::Node&);
  106. virtual void apply(osg::Drawable&);
  107. virtual void apply(osg::Geode& node);
  108. virtual void apply(osg::Billboard& node);
  109. virtual void apply(osg::Group& node);
  110. virtual void apply(osg::Transform& node);
  111. virtual void apply(osg::Switch& node);
  112. virtual void apply(osg::LOD& node);
  113. protected:
  114. class IntersectState : public osg::Referenced
  115. {
  116. public:
  117. IntersectState();
  118. osg::ref_ptr<osg::RefMatrix> _view_matrix;
  119. osg::ref_ptr<osg::RefMatrix> _view_inverse;
  120. osg::ref_ptr<osg::RefMatrix> _model_matrix;
  121. osg::ref_ptr<osg::RefMatrix> _model_inverse;
  122. typedef std::pair<osg::ref_ptr<osg::LineSegment>,osg::ref_ptr<osg::LineSegment> > LineSegmentPair;
  123. typedef std::vector< LineSegmentPair > LineSegmentList;
  124. LineSegmentList _segList;
  125. typedef unsigned int LineSegmentMask;
  126. typedef std::vector<LineSegmentMask> LineSegmentMaskStack;
  127. LineSegmentMaskStack _segmentMaskStack;
  128. bool isCulled(const osg::BoundingSphere& bs,LineSegmentMask& segMaskOut);
  129. bool isCulled(const osg::BoundingBox& bb,LineSegmentMask& segMaskOut);
  130. void addLineSegment(osg::LineSegment* seg);
  131. protected:
  132. ~IntersectState();
  133. };
  134. bool intersect(osg::Drawable& gset);
  135. void pushMatrix(osg::RefMatrix* matrix, osg::Transform::ReferenceFrame rf);
  136. void popMatrix();
  137. bool enterNode(osg::Node& node);
  138. void leaveNode();
  139. typedef std::vector<osg::ref_ptr<IntersectState> > IntersectStateStack;
  140. IntersectStateStack _intersectStateStack;
  141. LineSegmentHitListMap _segHitList;
  142. LODSelectionMode _lodSelectionMode;
  143. osg::Vec3 _pseudoEyePoint;
  144. };
  145. /** Deprecated Use LineSegmentIntersector/IntersectionVisitor or View::computeIntersections(..).*/
  146. class OSGUTIL_EXPORT PickVisitor : public osgUtil::IntersectVisitor
  147. {
  148. public:
  149. PickVisitor(const osg::Viewport* viewport, const osg::Matrixd& proj, const osg::Matrixd& view, float mx, float my);
  150. void runNestedPickVisitor(osg::Node& node, const osg::Viewport* viewport, const osg::Matrix& proj, const osg::Matrix& view, float mx, float my);
  151. void apply(osg::Projection& projection);
  152. void apply(osg::Camera& camera);
  153. protected:
  154. float _mx;
  155. float _my;
  156. osg::ref_ptr<const osg::Viewport> _lastViewport;
  157. osg::Matrixd _lastProjectionMatrix;
  158. osg::Matrixd _lastViewMatrix;
  159. };
  160. }
  161. #endif