2011-03-12 01:20:24 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2006-11-29 00:30:38 +08:00
*
2011-03-12 01:20:24 +08:00
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
2006-11-29 00:30:38 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2011-03-12 01:20:24 +08:00
*
2006-11-29 00:30:38 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2011-03-12 01:20:24 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2006-11-29 00:30:38 +08:00
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGUTIL_POLYTOPEINTERSECTOR
#define OSGUTIL_POLYTOPEINTERSECTOR 1
#include <osgUtil/IntersectionVisitor>
namespace osgUtil
{
2007-12-11 01:30:18 +08:00
/** Concrete class for implementing polytope intersections with the scene graph.
2006-11-29 00:30:38 +08:00
* To be used in conjunction with IntersectionVisitor. */
class OSGUTIL_EXPORT PolytopeIntersector : public Intersector
{
public:
2007-12-09 00:37:05 +08:00
/** Construct a PolytopeIntersector using specified polytope in MODEL coordinates.*/
2006-11-29 00:30:38 +08:00
PolytopeIntersector(const osg::Polytope& polytope);
2007-12-09 00:37:05 +08:00
/** Construct a PolytopeIntersector using specified polytope in specified coordinate frame.*/
2006-11-29 00:30:38 +08:00
PolytopeIntersector(CoordinateFrame cf, const osg::Polytope& polytope);
2007-12-09 00:37:05 +08:00
/** Convenience constructor for supporting picking in WINDOW, or PROJECTION coordinates
2006-11-29 00:30:38 +08:00
* 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.
* 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.
* 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.*/
PolytopeIntersector(CoordinateFrame cf, double xMin, double yMin, double xMax, double yMax);
2017-04-20 01:18:46 +08:00
/** Get the Polytope used by the intersector.*/
osg::Polytope& getPolytope() { return _polytope;}
/** Get the const Polytope used by the intersector.*/
const osg::Polytope& getPolytope() const { return _polytope;}
2012-02-08 18:06:58 +08:00
typedef osg::Plane::Vec3_type Vec3_type;
2006-11-29 00:30:38 +08:00
struct Intersection
{
2011-05-06 20:30:58 +08:00
Intersection():
distance(0.0),
maxDistance(0.0),
numIntersectionPoints(0),
primitiveIndex(0) {}
2007-12-09 00:37:05 +08:00
2006-11-29 00:30:38 +08:00
bool operator < (const Intersection& rhs) const
{
2007-12-09 00:37:05 +08:00
if (distance < rhs.distance) return true;
if (rhs.distance < distance) return false;
if (primitiveIndex < rhs.primitiveIndex) return true;
if (rhs.primitiveIndex < primitiveIndex) return false;
2006-11-29 00:30:38 +08:00
if (nodePath < rhs.nodePath) return true;
if (rhs.nodePath < nodePath ) return false;
return (drawable < rhs.drawable);
}
2007-12-09 00:37:05 +08:00
enum { MaxNumIntesectionPoints=6 };
double distance; ///< distance from reference plane
double maxDistance; ///< maximum distance of intersection points from reference plane
2006-11-29 00:30:38 +08:00
osg::NodePath nodePath;
osg::ref_ptr<osg::Drawable> drawable;
2007-12-09 00:37:05 +08:00
osg::ref_ptr<osg::RefMatrix> matrix;
2012-02-08 18:06:58 +08:00
Vec3_type localIntersectionPoint; ///< center of all intersection points
2007-12-09 00:37:05 +08:00
unsigned int numIntersectionPoints;
2012-02-08 18:06:58 +08:00
Vec3_type intersectionPoints[MaxNumIntesectionPoints];
2007-12-09 00:37:05 +08:00
unsigned int primitiveIndex; ///< primitive index
2006-11-29 00:30:38 +08:00
};
2011-03-12 01:20:24 +08:00
2006-11-29 00:30:38 +08:00
typedef std::set<Intersection> Intersections;
2011-03-12 01:20:24 +08:00
2006-11-29 00:30:38 +08:00
inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); }
inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }
inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); }
2007-12-09 00:37:05 +08:00
2017-05-25 01:34:22 +08:00
/// dimension enum to specify primitive types to check.
enum {
POINT_PRIMITIVES = (1<<0), /// check for points
LINE_PRIMITIVES = (1<<1), /// check for lines
TRIANGLE_PRIMITIVES = (1<<2), /// check for triangles and other primitives like quad, polygons that can be decomposed into triangles
ALL_PRIMITIVES = ( POINT_PRIMITIVES | LINE_PRIMITIVES | TRIANGLE_PRIMITIVES )
};
2007-12-09 00:37:05 +08:00
2017-05-25 01:34:22 +08:00
/** Set which Primitives should be tested for intersections.*/
void setPrimitiveMask(unsigned int mask) { _primitiveMask = mask; }
/** Get which Primitives should be tested for intersections.*/
unsigned int getPrimitiveMask() const { return _primitiveMask; }
2007-12-09 00:37:05 +08:00
/** set the plane used to sort the intersections.
* The intersections are sorted by the distance of the localIntersectionPoint
* and the reference plane. The default for the reference plane is the
* last plane of the polytope.
*/
inline void setReferencePlane(const osg::Plane& plane) { _referencePlane = plane; }
2017-05-25 01:34:22 +08:00
inline const osg::Plane& getReferencePlane() const { return _referencePlane; }
2017-05-25 15:48:45 +08:00
#ifdef OSG_USE_DEPRECATED_API
2017-05-25 01:34:22 +08:00
enum {
DimZero = POINT_PRIMITIVES, /// deprecated, use POINT_PRIMITIVES
DimOne = LINE_PRIMITIVES, /// deprecated, use POINT_PRIMITIVES
DimTwo = TRIANGLE_PRIMITIVES, /// deprecated, use POINT_PRIMITIVES
AllDims = ALL_PRIMITIVES /// deprecated, use ALL_PRIMITIVES
};
/** deprecated, use setPrimtiveMask() */
inline void setDimensionMask(unsigned int mask) { setPrimitiveMask(mask); }
/** deprecated, use getPrimtiveMask() */
inline unsigned int getDimensionMask() const { return getPrimitiveMask(); }
#endif
public:
2006-11-29 00:30:38 +08:00
virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
2011-03-12 01:20:24 +08:00
2006-11-29 00:30:38 +08:00
virtual bool enter(const osg::Node& node);
2011-03-12 01:20:24 +08:00
2006-11-29 00:30:38 +08:00
virtual void leave();
2011-03-12 01:20:24 +08:00
2006-11-29 00:30:38 +08:00
virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
2011-03-12 01:20:24 +08:00
2006-11-29 00:30:38 +08:00
virtual void reset();
2011-03-14 18:07:15 +08:00
virtual bool containsIntersections() { return !getIntersections().empty(); }
2006-11-29 00:30:38 +08:00
protected:
PolytopeIntersector* _parent;
osg::Polytope _polytope;
2007-12-09 00:37:05 +08:00
2017-05-25 01:34:22 +08:00
unsigned int _primitiveMask; ///< mask which dimensions should be checked
2007-12-09 00:37:05 +08:00
osg::Plane _referencePlane; ///< plane to use for sorting intersections
2006-11-29 00:30:38 +08:00
Intersections _intersections;
2011-03-12 01:20:24 +08:00
2006-11-29 00:30:38 +08:00
};
}
#endif