OpenSceneGraph/include/osgUtil/PolytopeIntersector
Robert Osfield f537ece404 From Leandro Motto Barros,
"I've made a few changes to osgUtil::PolytopeIntersector so that it
actually uses double precision floating point numbers everywhere (as
long as OSG_USE_FLOAT_PLANE is not defined).

I needed double precision intersections in a project I am working on.
These changes fixed the problems I was having -- this is all testing I
have done.

Notice that I have changed
osgUtil::PolytopeIntersector::Intersection's members to use doubles
(osg::Vec3d, instead of osg::Vec3). I could have added #ifdef's there
too, but I think it is better to not change the types of stuff in the
public interface depending on some preprocessor definition.

The modified files are attached. A diff also follows, for those who like it."

With the following changes from Robert Osfield:

"I've just reviewed your changes and have just tweaked them a little to
streamline them.  What I have done in the PolytopeIntersector header
is add:

       typedef osg::Plane::Vec3_type Vec3_type;

And then use this typedef in the definition of the vertices rather
then Vec3d as you did.  Next changes were to PolytopeInteresector.cpp
where to the PolytopeIntersectorUtils defintions of the Vec3_type, and
value_type which now simply read:

   typedef osg::Plane::Vec3_type Vec3_type;
   typedef Vec3_type::value_type value_type;

This way I was able to complete avoid any if def's and have essential
the same implementation as you achieved.  Changes now checked into
svn/trunk."
2012-02-08 10:06:58 +00:00

137 lines
5.8 KiB
C++

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* 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
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGUTIL_POLYTOPEINTERSECTOR
#define OSGUTIL_POLYTOPEINTERSECTOR 1
#include <osgUtil/IntersectionVisitor>
namespace osgUtil
{
/** Concrete class for implementing polytope intersections with the scene graph.
* To be used in conjunction with IntersectionVisitor. */
class OSGUTIL_EXPORT PolytopeIntersector : public Intersector
{
public:
/// 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.*/
PolytopeIntersector(const osg::Polytope& polytope);
/** Construct a PolytopeIntersector using specified polytope in specified coordinate frame.*/
PolytopeIntersector(CoordinateFrame cf, const osg::Polytope& polytope);
/** Convenience constructor for supporting picking in WINDOW, or PROJECTION coordinates
* 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);
typedef osg::Plane::Vec3_type Vec3_type;
struct Intersection
{
Intersection():
distance(0.0),
maxDistance(0.0),
numIntersectionPoints(0),
primitiveIndex(0) {}
bool operator < (const Intersection& rhs) const
{
if (distance < rhs.distance) return true;
if (rhs.distance < distance) return false;
if (primitiveIndex < rhs.primitiveIndex) return true;
if (rhs.primitiveIndex < primitiveIndex) return false;
if (nodePath < rhs.nodePath) return true;
if (rhs.nodePath < nodePath ) return false;
return (drawable < rhs.drawable);
}
enum { MaxNumIntesectionPoints=6 };
double distance; ///< distance from reference plane
double maxDistance; ///< maximum distance of intersection points from reference plane
osg::NodePath nodePath;
osg::ref_ptr<osg::Drawable> drawable;
osg::ref_ptr<osg::RefMatrix> matrix;
Vec3_type localIntersectionPoint; ///< center of all intersection points
unsigned int numIntersectionPoints;
Vec3_type intersectionPoints[MaxNumIntesectionPoints];
unsigned int primitiveIndex; ///< primitive index
};
typedef std::set<Intersection> Intersections;
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()); }
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; }
public:
virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
virtual bool enter(const osg::Node& node);
virtual void leave();
virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
virtual void reset();
virtual bool containsIntersections() { return !getIntersections().empty(); }
protected:
PolytopeIntersector* _parent;
osg::Polytope _polytope;
unsigned int _dimensionMask; ///< mask which dimensions should be checked
osg::Plane _referencePlane; ///< plane to use for sorting intersections
Intersections _intersections;
};
}
#endif