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_POLYTOPEINTERSECTOR
15#define OSGUTIL_POLYTOPEINTERSECTOR 1
17#include <osgUtil/IntersectionVisitor>
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
28 /** Construct a PolytopeIntersector using specified polytope in MODEL coordinates.*/
29 PolytopeIntersector(const osg::Polytope& polytope);
31 /** Construct a PolytopeIntersector using specified polytope in specified coordinate frame.*/
32 PolytopeIntersector(CoordinateFrame cf, const osg::Polytope& polytope);
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);
40 /** Get the Polytope used by the intersector.*/
41 osg::Polytope& getPolytope() { return _polytope;}
43 /** Get the const Polytope used by the intersector.*/
44 const osg::Polytope& getPolytope() const { return _polytope;}
47 typedef osg::Plane::Vec3_type Vec3_type;
54 numIntersectionPoints(0),
57 bool operator < (const Intersection& rhs) const
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);
68 enum { MaxNumIntesectionPoints=6 };
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
81 typedef std::set<Intersection> Intersections;
83 inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); }
85 inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }
87 inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); }
90 /// dimension enum to specify primitive types to check.
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 )
98 /** Set which Primitives should be tested for intersections.*/
99 void setPrimitiveMask(unsigned int mask) { _primitiveMask = mask; }
101 /** Get which Primitives should be tested for intersections.*/
102 unsigned int getPrimitiveMask() const { return _primitiveMask; }
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.
109 inline void setReferencePlane(const osg::Plane& plane) { _referencePlane = plane; }
111 inline const osg::Plane& getReferencePlane() const { return _referencePlane; }
113#ifdef OSG_USE_DEPRECATED_API
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
121 /** deprecated, use setPrimtiveMask() */
122 inline void setDimensionMask(unsigned int mask) { setPrimitiveMask(mask); }
124 /** deprecated, use getPrimtiveMask() */
125 inline unsigned int getDimensionMask() const { return getPrimitiveMask(); }
130 virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
132 virtual bool enter(const osg::Node& node);
134 virtual void leave();
136 virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
138 virtual void reset();
140 virtual bool containsIntersections() { return !getIntersections().empty(); }
144 PolytopeIntersector* _parent;
146 osg::Polytope _polytope;
148 unsigned int _primitiveMask; ///< mask which dimensions should be checked
149 osg::Plane _referencePlane; ///< plane to use for sorting intersections
151 Intersections _intersections;