PolytopeIntersector 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_POLYTOPEINTERSECTOR
  14. #define OSGUTIL_POLYTOPEINTERSECTOR 1
  15. #include <osgUtil/IntersectionVisitor>
  16. namespace osgUtil
  17. {
  18. /** Concrete class for implementing polytope intersections with the scene graph.
  19. * To be used in conjunction with IntersectionVisitor. */
  20. class OSGUTIL_EXPORT PolytopeIntersector : public Intersector
  21. {
  22. public:
  23. /** Construct a PolytopeIntersector using specified polytope in MODEL coordinates.*/
  24. PolytopeIntersector(const osg::Polytope& polytope);
  25. /** Construct a PolytopeIntersector using specified polytope in specified coordinate frame.*/
  26. PolytopeIntersector(CoordinateFrame cf, const osg::Polytope& polytope);
  27. /** Convenience constructor for supporting picking in WINDOW, or PROJECTION coordinates
  28. * In WINDOW coordinates (clip space cube) creates a five sided polytope box that has a front face at 0.0 and sides around box xMin, yMin, xMax, yMax.
  29. * In PROJECTION coordinates (clip space cube) creates a five sided polytope box that has a front face at -1 and sides around box xMin, yMin, xMax, yMax.
  30. * In VIEW and MODEL coordinates (clip space cube) creates a five sided polytope box that has a front face at 0.0 and sides around box xMin, yMin, xMax, yMax.*/
  31. PolytopeIntersector(CoordinateFrame cf, double xMin, double yMin, double xMax, double yMax);
  32. /** Get the Polytope used by the intersector.*/
  33. osg::Polytope& getPolytope() { return _polytope;}
  34. /** Get the const Polytope used by the intersector.*/
  35. const osg::Polytope& getPolytope() const { return _polytope;}
  36. typedef osg::Plane::Vec3_type Vec3_type;
  37. struct Intersection
  38. {
  39. Intersection():
  40. distance(0.0),
  41. maxDistance(0.0),
  42. numIntersectionPoints(0),
  43. primitiveIndex(0) {}
  44. bool operator < (const Intersection& rhs) const
  45. {
  46. if (distance < rhs.distance) return true;
  47. if (rhs.distance < distance) return false;
  48. if (primitiveIndex < rhs.primitiveIndex) return true;
  49. if (rhs.primitiveIndex < primitiveIndex) return false;
  50. if (nodePath < rhs.nodePath) return true;
  51. if (rhs.nodePath < nodePath ) return false;
  52. return (drawable < rhs.drawable);
  53. }
  54. enum { MaxNumIntesectionPoints=6 };
  55. double distance; ///< distance from reference plane
  56. double maxDistance; ///< maximum distance of intersection points from reference plane
  57. osg::NodePath nodePath;
  58. osg::ref_ptr<osg::Drawable> drawable;
  59. osg::ref_ptr<osg::RefMatrix> matrix;
  60. Vec3_type localIntersectionPoint; ///< center of all intersection points
  61. unsigned int numIntersectionPoints;
  62. Vec3_type intersectionPoints[MaxNumIntesectionPoints];
  63. unsigned int primitiveIndex; ///< primitive index
  64. };
  65. typedef std::set<Intersection> Intersections;
  66. inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); }
  67. inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }
  68. inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); }
  69. /// dimension enum to specify primitive types to check.
  70. enum {
  71. POINT_PRIMITIVES = (1<<0), /// check for points
  72. LINE_PRIMITIVES = (1<<1), /// check for lines
  73. TRIANGLE_PRIMITIVES = (1<<2), /// check for triangles and other primitives like quad, polygons that can be decomposed into triangles
  74. ALL_PRIMITIVES = ( POINT_PRIMITIVES | LINE_PRIMITIVES | TRIANGLE_PRIMITIVES )
  75. };
  76. /** Set which Primitives should be tested for intersections.*/
  77. void setPrimitiveMask(unsigned int mask) { _primitiveMask = mask; }
  78. /** Get which Primitives should be tested for intersections.*/
  79. unsigned int getPrimitiveMask() const { return _primitiveMask; }
  80. /** set the plane used to sort the intersections.
  81. * The intersections are sorted by the distance of the localIntersectionPoint
  82. * and the reference plane. The default for the reference plane is the
  83. * last plane of the polytope.
  84. */
  85. inline void setReferencePlane(const osg::Plane& plane) { _referencePlane = plane; }
  86. inline const osg::Plane& getReferencePlane() const { return _referencePlane; }
  87. #ifdef OSG_USE_DEPRECATED_API
  88. enum {
  89. DimZero = POINT_PRIMITIVES, /// deprecated, use POINT_PRIMITIVES
  90. DimOne = LINE_PRIMITIVES, /// deprecated, use POINT_PRIMITIVES
  91. DimTwo = TRIANGLE_PRIMITIVES, /// deprecated, use POINT_PRIMITIVES
  92. AllDims = ALL_PRIMITIVES /// deprecated, use ALL_PRIMITIVES
  93. };
  94. /** deprecated, use setPrimtiveMask() */
  95. inline void setDimensionMask(unsigned int mask) { setPrimitiveMask(mask); }
  96. /** deprecated, use getPrimtiveMask() */
  97. inline unsigned int getDimensionMask() const { return getPrimitiveMask(); }
  98. #endif
  99. public:
  100. virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
  101. virtual bool enter(const osg::Node& node);
  102. virtual void leave();
  103. virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
  104. virtual void reset();
  105. virtual bool containsIntersections() { return !getIntersections().empty(); }
  106. protected:
  107. PolytopeIntersector* _parent;
  108. osg::Polytope _polytope;
  109. unsigned int _primitiveMask; ///< mask which dimensions should be checked
  110. osg::Plane _referencePlane; ///< plane to use for sorting intersections
  111. Intersections _intersections;
  112. };
  113. }
  114. #endif