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 | |
---|
19 | namespace osgUtil |
---|
20 | { |
---|
21 | |
---|
22 | /** Concrete class for implementing line intersections with the scene graph. |
---|
23 | * To be used in conjunction with IntersectionVisitor. */ |
---|
24 | class OSGUTIL_EXPORT LineSegmentIntersector : public Intersector |
---|
25 | { |
---|
26 | public: |
---|
27 | |
---|
28 | /** Construct a LineSegmentIntersector the 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 the 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); |
---|
33 | |
---|
34 | /** Convenience constructor for supporting picking in WINDOW, or PROJECTION coordinates |
---|
35 | * In WINDOW coordinates creates a start value of (x,y,0) and end value of (x,y,1). |
---|
36 | * In PROJECTION coordinates (clip space cube) creates a start value of (x,y,-1) and end value of (x,y,1). |
---|
37 | * In VIEW and MODEL coordinates creates a start value of (x,y,0) and end value of (x,y,1).*/ |
---|
38 | LineSegmentIntersector(CoordinateFrame cf, double x, double y); |
---|
39 | |
---|
40 | struct Intersection |
---|
41 | { |
---|
42 | Intersection(): |
---|
43 | ratio(-1.0), |
---|
44 | primitiveIndex(0) {} |
---|
45 | |
---|
46 | bool operator < (const Intersection& rhs) const { return ratio < rhs.ratio; } |
---|
47 | |
---|
48 | typedef std::vector<unsigned int> IndexList; |
---|
49 | typedef std::vector<double> RatioList; |
---|
50 | |
---|
51 | double ratio; |
---|
52 | osg::NodePath nodePath; |
---|
53 | osg::ref_ptr<osg::Drawable> drawable; |
---|
54 | osg::ref_ptr<osg::RefMatrix> matrix; |
---|
55 | osg::Vec3d localIntersectionPoint; |
---|
56 | osg::Vec3 localIntersectionNormal; |
---|
57 | IndexList indexList; |
---|
58 | RatioList ratioList; |
---|
59 | unsigned int primitiveIndex; |
---|
60 | |
---|
61 | const osg::Vec3d& getLocalIntersectPoint() const { return localIntersectionPoint; } |
---|
62 | osg::Vec3d getWorldIntersectPoint() const { return matrix.valid() ? localIntersectionPoint * (*matrix) : localIntersectionPoint; } |
---|
63 | |
---|
64 | const osg::Vec3& getLocalIntersectNormal() const { return localIntersectionNormal; } |
---|
65 | osg::Vec3 getWorldIntersectNormal() const { return matrix.valid() ? osg::Matrix::transform3x3(osg::Matrix::inverse(*matrix),localIntersectionNormal) : localIntersectionNormal; } |
---|
66 | |
---|
67 | /** convinience function for mapping the intersection point to any textures assigned to the objects intersected. |
---|
68 | * Returns the Texture pointer and texture coords of object hit when a texture is available on the object, returns NULL otherwise.*/ |
---|
69 | osg::Texture* getTextureLookUp(osg::Vec3& tc) const; |
---|
70 | |
---|
71 | }; |
---|
72 | |
---|
73 | typedef std::multiset<Intersection> Intersections; |
---|
74 | |
---|
75 | inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); } |
---|
76 | |
---|
77 | inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; } |
---|
78 | |
---|
79 | inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); } |
---|
80 | |
---|
81 | inline void setStart(const osg::Vec3d& start) { _start = start; } |
---|
82 | inline const osg::Vec3d& getStart() const { return _start; } |
---|
83 | |
---|
84 | inline void setEnd(const osg::Vec3d& end) { _end = end; } |
---|
85 | inline const osg::Vec3d& getEnd() const { return _end; } |
---|
86 | |
---|
87 | public: |
---|
88 | |
---|
89 | virtual Intersector* clone(osgUtil::IntersectionVisitor& iv); |
---|
90 | |
---|
91 | virtual bool enter(const osg::Node& node); |
---|
92 | |
---|
93 | virtual void leave(); |
---|
94 | |
---|
95 | virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable); |
---|
96 | |
---|
97 | virtual void reset(); |
---|
98 | |
---|
99 | virtual bool containsIntersections() { return !getIntersections().empty(); } |
---|
100 | |
---|
101 | protected: |
---|
102 | |
---|
103 | bool intersects(const osg::BoundingSphere& bs); |
---|
104 | bool intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const osg::BoundingBox& bb); |
---|
105 | |
---|
106 | LineSegmentIntersector* _parent; |
---|
107 | |
---|
108 | osg::Vec3d _start; |
---|
109 | osg::Vec3d _end; |
---|
110 | |
---|
111 | Intersections _intersections; |
---|
112 | |
---|
113 | }; |
---|
114 | |
---|
115 | } |
---|
116 | |
---|
117 | #endif |
---|
118 | |
---|