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 | |
---|
68 | typedef std::multiset<Intersection> Intersections; |
---|
69 | |
---|
70 | inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); } |
---|
71 | |
---|
72 | inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; } |
---|
73 | |
---|
74 | inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); } |
---|
75 | |
---|
76 | inline void setStart(const osg::Vec3d& start) { _start = start; } |
---|
77 | inline const osg::Vec3d& getStart() const { return _start; } |
---|
78 | |
---|
79 | inline void setEnd(const osg::Vec3d& end) { _end = end; } |
---|
80 | inline const osg::Vec3d& getEnd() const { return _end; } |
---|
81 | |
---|
82 | public: |
---|
83 | |
---|
84 | virtual Intersector* clone(osgUtil::IntersectionVisitor& iv); |
---|
85 | |
---|
86 | virtual bool enter(const osg::Node& node); |
---|
87 | |
---|
88 | virtual void leave(); |
---|
89 | |
---|
90 | virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable); |
---|
91 | |
---|
92 | virtual void reset(); |
---|
93 | |
---|
94 | virtual bool containsIntersections() { return !getIntersections().empty(); } |
---|
95 | |
---|
96 | protected: |
---|
97 | |
---|
98 | bool intersects(const osg::BoundingSphere& bs); |
---|
99 | bool intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const osg::BoundingBox& bb); |
---|
100 | |
---|
101 | LineSegmentIntersector* _parent; |
---|
102 | |
---|
103 | osg::Vec3d _start; |
---|
104 | osg::Vec3d _end; |
---|
105 | |
---|
106 | Intersections _intersections; |
---|
107 | |
---|
108 | }; |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | #endif |
---|
113 | |
---|