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