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
/// dimension enum to specify primitive types to check.
enum {
DimZero = (1<<0), ///< check for points
DimOne = (1<<1), ///< check for lines
DimTwo = (1<<2), ///< check for triangles, quad
AllDims = (DimZero | DimOne | DimTwo)
};
/** 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);
struct Intersection
{
Intersection() {}
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;
osg::Vec3 localIntersectionPoint; ///< center of all intersection points
unsigned int numIntersectionPoints;
osg::Vec3 intersectionPoints[MaxNumIntesectionPoints];
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
inline unsigned int getDimensionMask() const { return _dimensionMask; }
/** set the dimension mask.
* As polytope-triangle and polytope-quad intersections are expensive to compute
* it is possible to turn them off by calling setDimensionMask( DimZero | DimOne )
*/
inline void setDimensionMask(unsigned int dimensionMask) { _dimensionMask = dimensionMask; }
inline const osg::Plane& getReferencePlane() const { return _referencePlane; }
/** 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; }
2006-11-29 00:30:38 +08:00
public:
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();
virtual bool containsIntersections() { return !_intersections.empty(); }
protected:
PolytopeIntersector* _parent;
osg::Polytope _polytope;
2007-12-09 00:37:05 +08:00
unsigned int _dimensionMask; ///< mask which dimensions should be checked
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