openscenegraph
PolytopeIntersector
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_POLYTOPEINTERSECTOR
15#define OSGUTIL_POLYTOPEINTERSECTOR 1
16
17#include <osgUtil/IntersectionVisitor>
18
19namespace osgUtil
20{
21
22/** Concrete class for implementing polytope intersections with the scene graph.
23 * To be used in conjunction with IntersectionVisitor. */
24class OSGUTIL_EXPORT PolytopeIntersector : public Intersector
25{
26 public:
27
28 /** Construct a PolytopeIntersector using specified polytope in MODEL coordinates.*/
29 PolytopeIntersector(const osg::Polytope& polytope);
30
31 /** Construct a PolytopeIntersector using specified polytope in specified coordinate frame.*/
32 PolytopeIntersector(CoordinateFrame cf, const osg::Polytope& polytope);
33
34 /** Convenience constructor for supporting picking in WINDOW, or PROJECTION coordinates
35 * In WINDOW coordinates (clip space cube) creates a five sided polytope box that has a front face at 0.0 and sides around box xMin, yMin, xMax, yMax.
36 * In PROJECTION coordinates (clip space cube) creates a five sided polytope box that has a front face at -1 and sides around box xMin, yMin, xMax, yMax.
37 * In VIEW and MODEL coordinates (clip space cube) creates a five sided polytope box that has a front face at 0.0 and sides around box xMin, yMin, xMax, yMax.*/
38 PolytopeIntersector(CoordinateFrame cf, double xMin, double yMin, double xMax, double yMax);
39
40 /** Get the Polytope used by the intersector.*/
41 osg::Polytope& getPolytope() { return _polytope;}
42
43 /** Get the const Polytope used by the intersector.*/
44 const osg::Polytope& getPolytope() const { return _polytope;}
45
46
47 typedef osg::Plane::Vec3_type Vec3_type;
48
49 struct Intersection
50 {
51 Intersection():
52 distance(0.0),
53 maxDistance(0.0),
54 numIntersectionPoints(0),
55 primitiveIndex(0) {}
56
57 bool operator < (const Intersection& rhs) const
58 {
59 if (distance < rhs.distance) return true;
60 if (rhs.distance < distance) return false;
61 if (primitiveIndex < rhs.primitiveIndex) return true;
62 if (rhs.primitiveIndex < primitiveIndex) return false;
63 if (nodePath < rhs.nodePath) return true;
64 if (rhs.nodePath < nodePath ) return false;
65 return (drawable < rhs.drawable);
66 }
67
68 enum { MaxNumIntesectionPoints=6 };
69
70 double distance; ///< distance from reference plane
71 double maxDistance; ///< maximum distance of intersection points from reference plane
72 osg::NodePath nodePath;
73 osg::ref_ptr<osg::Drawable> drawable;
74 osg::ref_ptr<osg::RefMatrix> matrix;
75 Vec3_type localIntersectionPoint; ///< center of all intersection points
76 unsigned int numIntersectionPoints;
77 Vec3_type intersectionPoints[MaxNumIntesectionPoints];
78 unsigned int primitiveIndex; ///< primitive index
79 };
80
81 typedef std::set<Intersection> Intersections;
82
83 inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); }
84
85 inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }
86
87 inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); }
88
89
90 /// dimension enum to specify primitive types to check.
91 enum {
92 POINT_PRIMITIVES = (1<<0), /// check for points
93 LINE_PRIMITIVES = (1<<1), /// check for lines
94 TRIANGLE_PRIMITIVES = (1<<2), /// check for triangles and other primitives like quad, polygons that can be decomposed into triangles
95 ALL_PRIMITIVES = ( POINT_PRIMITIVES | LINE_PRIMITIVES | TRIANGLE_PRIMITIVES )
96 };
97
98 /** Set which Primitives should be tested for intersections.*/
99 void setPrimitiveMask(unsigned int mask) { _primitiveMask = mask; }
100
101 /** Get which Primitives should be tested for intersections.*/
102 unsigned int getPrimitiveMask() const { return _primitiveMask; }
103
104 /** set the plane used to sort the intersections.
105 * The intersections are sorted by the distance of the localIntersectionPoint
106 * and the reference plane. The default for the reference plane is the
107 * last plane of the polytope.
108 */
109 inline void setReferencePlane(const osg::Plane& plane) { _referencePlane = plane; }
110
111 inline const osg::Plane& getReferencePlane() const { return _referencePlane; }
112
113#ifdef OSG_USE_DEPRECATED_API
114 enum {
115 DimZero = POINT_PRIMITIVES, /// deprecated, use POINT_PRIMITIVES
116 DimOne = LINE_PRIMITIVES, /// deprecated, use POINT_PRIMITIVES
117 DimTwo = TRIANGLE_PRIMITIVES, /// deprecated, use POINT_PRIMITIVES
118 AllDims = ALL_PRIMITIVES /// deprecated, use ALL_PRIMITIVES
119 };
120
121 /** deprecated, use setPrimtiveMask() */
122 inline void setDimensionMask(unsigned int mask) { setPrimitiveMask(mask); }
123
124 /** deprecated, use getPrimtiveMask() */
125 inline unsigned int getDimensionMask() const { return getPrimitiveMask(); }
126#endif
127
128public:
129
130 virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
131
132 virtual bool enter(const osg::Node& node);
133
134 virtual void leave();
135
136 virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
137
138 virtual void reset();
139
140 virtual bool containsIntersections() { return !getIntersections().empty(); }
141
142 protected:
143
144 PolytopeIntersector* _parent;
145
146 osg::Polytope _polytope;
147
148 unsigned int _primitiveMask; ///< mask which dimensions should be checked
149 osg::Plane _referencePlane; ///< plane to use for sorting intersections
150
151 Intersections _intersections;
152
153};
154
155}
156
157#endif
158