openscenegraph
LineSegmentIntersector
Go to the documentation of this file.
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
14#ifndef OSGUTIL_LINESEGMENTINTERSECTOR
15#define OSGUTIL_LINESEGMENTINTERSECTOR 1
16
17#include <osgUtil/IntersectionVisitor>
18
19namespace osgUtil
20{
21
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
25{
26 public:
27
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);
30
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);
34
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);
40
41 struct OSGUTIL_EXPORT Intersection
42 {
43 Intersection():
44 ratio(-1.0),
45 primitiveIndex(0) {}
46
47 bool operator < (const Intersection& rhs) const { return ratio < rhs.ratio; }
48
49 typedef std::vector<unsigned int> IndexList;
50 typedef std::vector<double> RatioList;
51
52 double ratio;
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
62 const osg::Vec3d& getLocalIntersectPoint() const { return localIntersectionPoint; }
63 osg::Vec3d getWorldIntersectPoint() const { return matrix.valid() ? localIntersectionPoint * (*matrix) : localIntersectionPoint; }
64
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; }
67
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;
71
72 };
73
74 typedef std::multiset<Intersection> Intersections;
75
76 inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); }
77
78 inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }
79
80 inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); }
81
82 inline void setStart(const osg::Vec3d& start) { _start = start; }
83 inline const osg::Vec3d& getStart() const { return _start; }
84
85 inline void setEnd(const osg::Vec3d& end) { _end = end; }
86 inline const osg::Vec3d& getEnd() const { return _end; }
87
88 public:
89
90 virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
91
92 virtual bool enter(const osg::Node& node);
93
94 virtual void leave();
95
96 virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
97
98 virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable,
99 const osg::Vec3d& s, const osg::Vec3d& e);
100
101 virtual void reset();
102
103 virtual bool containsIntersections() { return !getIntersections().empty(); }
104
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);
109
110protected:
111
112 bool intersects(const osg::BoundingSphere& bs);
113 bool intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const osg::BoundingBox& bb);
114
115 LineSegmentIntersector* _parent;
116
117 osg::Vec3d _start;
118 osg::Vec3d _end;
119
120 Intersections _intersections;
121
122};
123
124}
125
126#endif
127