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_LINESEGMENTINTERSECTOR
|
|
|
|
#define OSGUTIL_LINESEGMENTINTERSECTOR 1
|
|
|
|
|
|
|
|
#include <osgUtil/IntersectionVisitor>
|
|
|
|
|
|
|
|
namespace osgUtil
|
|
|
|
{
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
/** Concrete class for implementing line intersections with the scene graph.
|
2006-11-29 00:30:38 +08:00
|
|
|
* To be used in conjunction with IntersectionVisitor. */
|
|
|
|
class OSGUTIL_EXPORT LineSegmentIntersector : public Intersector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2013-10-03 16:45:57 +08:00
|
|
|
/** Construct a LineSegmentIntersector that runs between the specified start and end points in MODEL coordinates. */
|
2006-11-29 00:30:38 +08:00
|
|
|
LineSegmentIntersector(const osg::Vec3d& start, const osg::Vec3d& end);
|
2011-03-12 01:20:24 +08:00
|
|
|
|
2013-10-03 16:45:57 +08:00
|
|
|
/** Construct a LineSegmentIntersector that runs between the specified start and end points in the specified coordinate frame. */
|
|
|
|
LineSegmentIntersector(CoordinateFrame cf, const osg::Vec3d& start, const osg::Vec3d& end, LineSegmentIntersector* parent = NULL,
|
|
|
|
osgUtil::Intersector::IntersectionLimit intersectionLimit = osgUtil::Intersector::NO_LIMIT);
|
2011-03-12 01:20:24 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
/** Convenience constructor for supporting picking in WINDOW, or PROJECTION coordinates
|
2006-11-29 00:30:38 +08:00
|
|
|
* In WINDOW coordinates creates a start value of (x,y,0) and end value of (x,y,1).
|
2007-01-10 18:09:05 +08:00
|
|
|
* In PROJECTION coordinates (clip space cube) creates a start value of (x,y,-1) and end value of (x,y,1).
|
2006-11-29 00:30:38 +08:00
|
|
|
* In VIEW and MODEL coordinates creates a start value of (x,y,0) and end value of (x,y,1).*/
|
|
|
|
LineSegmentIntersector(CoordinateFrame cf, double x, double y);
|
2011-03-12 01:20:24 +08:00
|
|
|
|
2013-05-13 21:12:13 +08:00
|
|
|
struct OSGUTIL_EXPORT Intersection
|
2006-11-29 00:30:38 +08:00
|
|
|
{
|
|
|
|
Intersection():
|
|
|
|
ratio(-1.0),
|
|
|
|
primitiveIndex(0) {}
|
2011-03-12 01:20:24 +08:00
|
|
|
|
2006-11-29 00:30:38 +08:00
|
|
|
bool operator < (const Intersection& rhs) const { return ratio < rhs.ratio; }
|
|
|
|
|
2007-01-10 18:40:12 +08:00
|
|
|
typedef std::vector<unsigned int> IndexList;
|
|
|
|
typedef std::vector<double> RatioList;
|
2006-11-29 00:30:38 +08:00
|
|
|
|
|
|
|
double ratio;
|
|
|
|
osg::NodePath nodePath;
|
|
|
|
osg::ref_ptr<osg::Drawable> drawable;
|
|
|
|
osg::ref_ptr<osg::RefMatrix> matrix;
|
|
|
|
osg::Vec3d localIntersectionPoint;
|
|
|
|
osg::Vec3 localIntersectionNormal;
|
|
|
|
IndexList indexList;
|
2007-01-10 18:40:12 +08:00
|
|
|
RatioList ratioList;
|
2006-11-29 00:30:38 +08:00
|
|
|
unsigned int primitiveIndex;
|
2011-03-12 01:20:24 +08:00
|
|
|
|
2007-01-10 21:52:22 +08:00
|
|
|
const osg::Vec3d& getLocalIntersectPoint() const { return localIntersectionPoint; }
|
|
|
|
osg::Vec3d getWorldIntersectPoint() const { return matrix.valid() ? localIntersectionPoint * (*matrix) : localIntersectionPoint; }
|
2011-03-12 01:20:24 +08:00
|
|
|
|
2007-01-10 21:52:22 +08:00
|
|
|
const osg::Vec3& getLocalIntersectNormal() const { return localIntersectionNormal; }
|
|
|
|
osg::Vec3 getWorldIntersectNormal() const { return matrix.valid() ? osg::Matrix::transform3x3(osg::Matrix::inverse(*matrix),localIntersectionNormal) : localIntersectionNormal; }
|
2013-05-04 03:26:27 +08:00
|
|
|
|
|
|
|
/** convinience function for mapping the intersection point to any textures assigned to the objects intersected.
|
|
|
|
* Returns the Texture pointer and texture coords of object hit when a texture is available on the object, returns NULL otherwise.*/
|
|
|
|
osg::Texture* getTextureLookUp(osg::Vec3& tc) const;
|
2013-09-24 16:56:47 +08:00
|
|
|
|
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::multiset<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()); }
|
2011-03-12 01:20:24 +08:00
|
|
|
|
2007-08-12 21:17:37 +08:00
|
|
|
inline void setStart(const osg::Vec3d& start) { _start = start; }
|
2007-08-12 21:18:50 +08:00
|
|
|
inline const osg::Vec3d& getStart() const { return _start; }
|
|
|
|
|
2007-08-12 21:17:37 +08:00
|
|
|
inline void setEnd(const osg::Vec3d& end) { _end = end; }
|
2008-05-07 21:49:32 +08:00
|
|
|
inline const osg::Vec3d& getEnd() const { return _end; }
|
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
|
|
|
|
2013-10-03 16:45:57 +08:00
|
|
|
virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable,
|
|
|
|
const osg::Vec3d& s, const osg::Vec3d& e);
|
|
|
|
|
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
|
|
|
|
2013-10-03 16:45:57 +08:00
|
|
|
/** Compute the matrix that transforms the local coordinate system of parent Intersector (usually
|
|
|
|
the current intersector) into the child coordinate system of the child Intersector.
|
|
|
|
cf parameter indicates the coordinate frame of parent Intersector. */
|
|
|
|
static osg::Matrix getTransformation(osgUtil::IntersectionVisitor& iv, CoordinateFrame cf);
|
|
|
|
|
|
|
|
protected:
|
2011-03-12 01:20:24 +08:00
|
|
|
|
2006-11-29 00:30:38 +08:00
|
|
|
bool intersects(const osg::BoundingSphere& bs);
|
|
|
|
bool intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const osg::BoundingBox& bb);
|
|
|
|
|
|
|
|
LineSegmentIntersector* _parent;
|
|
|
|
|
|
|
|
osg::Vec3d _start;
|
|
|
|
osg::Vec3d _end;
|
2011-03-12 01:20:24 +08:00
|
|
|
|
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
|
|
|
|
|