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_LINESEGMENTINTERSECTOR
15#define OSGUTIL_LINESEGMENTINTERSECTOR 1
17#include <osgUtil/IntersectionVisitor>
22/** Concrete class for implementing line intersections with the scene graph.
23 * To be used in conjunction with IntersectionVisitor. */
24class OSGUTIL_EXPORT LineSegmentIntersector : public Intersector
28 /** Construct a LineSegmentIntersector that runs between the specified start and end points in MODEL coordinates. */
29 LineSegmentIntersector(const osg::Vec3d& start, const osg::Vec3d& end);
31 /** Construct a LineSegmentIntersector that runs between the specified start and end points in the specified coordinate frame. */
32 LineSegmentIntersector(CoordinateFrame cf, const osg::Vec3d& start, const osg::Vec3d& end, LineSegmentIntersector* parent = NULL,
33 osgUtil::Intersector::IntersectionLimit intersectionLimit = osgUtil::Intersector::NO_LIMIT);
35 /** Convenience constructor for supporting picking in WINDOW, or PROJECTION coordinates
36 * In WINDOW coordinates creates a start value of (x,y,0) and end value of (x,y,1).
37 * In PROJECTION coordinates (clip space cube) creates a start value of (x,y,-1) and end value of (x,y,1).
38 * In VIEW and MODEL coordinates creates a start value of (x,y,0) and end value of (x,y,1).*/
39 LineSegmentIntersector(CoordinateFrame cf, double x, double y);
41 struct OSGUTIL_EXPORT Intersection
47 bool operator < (const Intersection& rhs) const { return ratio < rhs.ratio; }
49 typedef std::vector<unsigned int> IndexList;
50 typedef std::vector<double> RatioList;
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;
60 unsigned int primitiveIndex;
62 const osg::Vec3d& getLocalIntersectPoint() const { return localIntersectionPoint; }
63 osg::Vec3d getWorldIntersectPoint() const { return matrix.valid() ? localIntersectionPoint * (*matrix) : localIntersectionPoint; }
65 const osg::Vec3& getLocalIntersectNormal() const { return localIntersectionNormal; }
66 osg::Vec3 getWorldIntersectNormal() const { return matrix.valid() ? osg::Matrix::transform3x3(osg::Matrix::inverse(*matrix),localIntersectionNormal) : localIntersectionNormal; }
68 /** Convenience function for mapping the intersection point to any textures assigned to the objects intersected.
69 * Returns the Texture pointer and texture coords of object hit when a texture is available on the object, returns NULL otherwise.*/
70 osg::Texture* getTextureLookUp(osg::Vec3& tc) const;
74 typedef std::multiset<Intersection> Intersections;
76 inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); }
78 inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }
80 inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); }
82 inline void setStart(const osg::Vec3d& start) { _start = start; }
83 inline const osg::Vec3d& getStart() const { return _start; }
85 inline void setEnd(const osg::Vec3d& end) { _end = end; }
86 inline const osg::Vec3d& getEnd() const { return _end; }
90 virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
92 virtual bool enter(const osg::Node& node);
96 virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
98 virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable,
99 const osg::Vec3d& s, const osg::Vec3d& e);
101 virtual void reset();
103 virtual bool containsIntersections() { return !getIntersections().empty(); }
105 /** Compute the matrix that transforms the local coordinate system of parent Intersector (usually
106 the current intersector) into the child coordinate system of the child Intersector.
107 cf parameter indicates the coordinate frame of parent Intersector. */
108 static osg::Matrix getTransformation(osgUtil::IntersectionVisitor& iv, CoordinateFrame cf);
112 bool intersects(const osg::BoundingSphere& bs);
113 bool intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const osg::BoundingBox& bb);
115 LineSegmentIntersector* _parent;
120 Intersections _intersections;