RayIntersector 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_RAYINTERSECTOR
  14. #define OSGUTIL_RAYINTERSECTOR 1
  15. #include <osgUtil/IntersectionVisitor>
  16. namespace osgUtil
  17. {
  18. /** RayIntersector implements possibly-infinite line intersections with the scene graph.
  19. *
  20. * Compared with LineSegmentIntersector, RayIntersector supports infinite intersection
  21. * lines, start and end point can be given in homogeneous coordinates and projection
  22. * matrix is allowed to have z-far plane at infinity (often used in shadow volume
  23. * technique).
  24. *
  25. * Currently, picking of objects at infinity is not supported. Please, contribute.
  26. *
  27. * The class is be used in conjunction with IntersectionVisitor. */
  28. class OSGUTIL_EXPORT RayIntersector : public Intersector
  29. {
  30. public:
  31. /** Construct a RayIntersector. You will need to provide start and end point,
  32. * or start point and direction. See setStart() and setDirecton(). */
  33. RayIntersector(CoordinateFrame cf = MODEL, RayIntersector* parent = NULL,
  34. osgUtil::Intersector::IntersectionLimit intersectionLimit = osgUtil::Intersector::NO_LIMIT);
  35. /** Construct a RayIntersector that runs from start point in specified direction to the infinity.
  36. * Start and direction are provided in MODEL coordinates. */
  37. RayIntersector(const osg::Vec3d& start, const osg::Vec3d& direction);
  38. /** Construct a RayIntersector the runs from start point in specified direction to the infinity in the specified coordinate frame. */
  39. RayIntersector(CoordinateFrame cf, const osg::Vec3d& start, const osg::Vec3d& direction, RayIntersector* parent = NULL,
  40. osgUtil::Intersector::IntersectionLimit intersectionLimit = osgUtil::Intersector::NO_LIMIT);
  41. /** Convenience constructor for supporting picking in WINDOW and PROJECTION coordinates.
  42. * In WINDOW coordinates, it creates a start value of (x,y,0) and end value of (x,y,1).
  43. * In PROJECTION coordinates (clip space cube), it creates a start value of (x,y,-1) and end value of (x,y,1).
  44. * In VIEW and MODEL coordinates, it creates a start value of (x,y,0) and end value of (x,y,1).*/
  45. RayIntersector(CoordinateFrame cf, double x, double y);
  46. struct OSGUTIL_EXPORT Intersection
  47. {
  48. Intersection() : distance(-1.0), primitiveIndex(0) {}
  49. bool operator < (const Intersection& rhs) const { return distance < rhs.distance; }
  50. typedef std::vector<unsigned int> IndexList;
  51. typedef std::vector<double> RatioList;
  52. double distance;
  53. osg::NodePath nodePath;
  54. osg::ref_ptr<osg::Drawable> drawable;
  55. osg::ref_ptr<osg::RefMatrix> matrix;
  56. osg::Vec3d localIntersectionPoint;
  57. osg::Vec3 localIntersectionNormal;
  58. IndexList indexList;
  59. RatioList ratioList;
  60. unsigned int primitiveIndex;
  61. const osg::Vec3d& getLocalIntersectPoint() const { return localIntersectionPoint; }
  62. osg::Vec3d getWorldIntersectPoint() const { return matrix.valid() ? localIntersectionPoint * (*matrix) : localIntersectionPoint; }
  63. const osg::Vec3& getLocalIntersectNormal() const { return localIntersectionNormal; }
  64. osg::Vec3 getWorldIntersectNormal() const { return matrix.valid() ? osg::Matrix::transform3x3(osg::Matrix::inverse(*matrix),localIntersectionNormal) : localIntersectionNormal; }
  65. /** Convenience function for mapping the intersection point to any textures assigned to the objects intersected.
  66. * Returns the Texture pointer and texture coords of object hit when a texture is available on the object, returns NULL otherwise.*/
  67. osg::Texture* getTextureLookUp(osg::Vec3& tc) const;
  68. };
  69. typedef std::multiset<Intersection> Intersections;
  70. inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); }
  71. inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }
  72. inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); }
  73. virtual void setStart(const osg::Vec3d& start) { _start = start; }
  74. inline const osg::Vec3d& getStart() const { return _start; }
  75. virtual void setDirection(const osg::Vec3d& dir) { _direction = dir; }
  76. inline const osg::Vec3d& getDirection() const { return _direction; }
  77. public:
  78. virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
  79. virtual bool enter(const osg::Node& node);
  80. virtual void leave();
  81. virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
  82. virtual void reset();
  83. virtual bool containsIntersections() { return !getIntersections().empty(); }
  84. protected:
  85. virtual bool intersects(const osg::BoundingSphere& bs);
  86. bool intersectAndClip(osg::Vec3d& s, const osg::Vec3d& d, osg::Vec3d& e, const osg::BoundingBox& bb);
  87. RayIntersector* _parent;
  88. osg::Vec3d _start;
  89. osg::Vec3d _direction;
  90. Intersections _intersections;
  91. };
  92. }
  93. #endif