2008-07-04 23:57:48 +08:00
|
|
|
/* -*-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 OSG_KDTREE
|
|
|
|
#define OSG_KDTREE 1
|
|
|
|
|
|
|
|
#include <osg/Shape>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
#include <map>
|
|
|
|
|
2008-07-04 23:57:48 +08:00
|
|
|
namespace osg
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Implementation of a kdtree for Geometry leaves, to enable fast intersection tests.*/
|
|
|
|
class OSG_EXPORT KdTree : public osg::Shape
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
2008-07-06 20:14:19 +08:00
|
|
|
KdTree();
|
2008-07-04 23:57:48 +08:00
|
|
|
|
2008-07-06 20:14:19 +08:00
|
|
|
KdTree(const KdTree& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
2008-07-04 23:57:48 +08:00
|
|
|
|
|
|
|
META_Shape(osg, KdTree)
|
|
|
|
|
2008-07-13 01:30:17 +08:00
|
|
|
struct OSG_EXPORT BuildOptions
|
2008-07-06 20:14:19 +08:00
|
|
|
{
|
2008-07-08 04:27:56 +08:00
|
|
|
BuildOptions();
|
2008-07-06 20:14:19 +08:00
|
|
|
|
2008-07-16 21:26:06 +08:00
|
|
|
unsigned int _numVerticesProcessed;
|
|
|
|
unsigned int _targetNumTrianglesPerLeaf;
|
|
|
|
unsigned int _maxNumLevels;
|
2008-07-06 20:14:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-04 23:57:48 +08:00
|
|
|
/** Build the kdtree from the specified source geometry object.
|
|
|
|
* retun true on success. */
|
2008-07-06 20:14:19 +08:00
|
|
|
virtual bool build(BuildOptions& buildOptions, osg::Geometry* geometry);
|
2008-07-04 23:57:48 +08:00
|
|
|
|
|
|
|
struct LineSegmentIntersection
|
|
|
|
{
|
|
|
|
LineSegmentIntersection():
|
|
|
|
ratio(-1.0),
|
2008-07-12 00:48:39 +08:00
|
|
|
p0(0),
|
|
|
|
p1(0),
|
|
|
|
p2(0),
|
|
|
|
r0(0.0f),
|
|
|
|
r1(0.0f),
|
|
|
|
r2(0.0f),
|
2008-07-04 23:57:48 +08:00
|
|
|
primitiveIndex(0) {}
|
|
|
|
|
|
|
|
bool operator < (const LineSegmentIntersection& rhs) const { return ratio < rhs.ratio; }
|
|
|
|
|
|
|
|
typedef std::vector<unsigned int> IndexList;
|
|
|
|
typedef std::vector<double> RatioList;
|
|
|
|
|
|
|
|
double ratio;
|
|
|
|
osg::Vec3d intersectionPoint;
|
|
|
|
osg::Vec3 intersectionNormal;
|
2008-07-12 00:48:39 +08:00
|
|
|
|
|
|
|
unsigned int p0;
|
|
|
|
unsigned int p1;
|
|
|
|
unsigned int p2;
|
|
|
|
float r0;
|
|
|
|
float r1;
|
|
|
|
float r2;
|
|
|
|
|
2008-07-04 23:57:48 +08:00
|
|
|
unsigned int primitiveIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
typedef std::vector<LineSegmentIntersection> LineSegmentIntersections;
|
2008-07-04 23:57:48 +08:00
|
|
|
|
|
|
|
/** compute the intersection of a line segment and the kdtree, return true if an intersection has been found.*/
|
2008-07-07 21:21:37 +08:00
|
|
|
virtual bool intersect(const osg::Vec3& start, const osg::Vec3& end, LineSegmentIntersections& intersections) const;
|
2008-07-04 23:57:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
typedef int value_type;
|
|
|
|
|
2008-07-06 20:14:19 +08:00
|
|
|
struct KdNode
|
2008-07-04 23:57:48 +08:00
|
|
|
{
|
2008-07-06 20:14:19 +08:00
|
|
|
KdNode():
|
2008-07-04 23:57:48 +08:00
|
|
|
first(0),
|
|
|
|
second(0) {}
|
|
|
|
|
2008-07-06 20:14:19 +08:00
|
|
|
KdNode(value_type f, value_type s):
|
2008-07-04 23:57:48 +08:00
|
|
|
first(f),
|
|
|
|
second(s) {}
|
|
|
|
|
2008-07-10 03:41:36 +08:00
|
|
|
osg::BoundingBox bb;
|
|
|
|
|
2008-07-04 23:57:48 +08:00
|
|
|
value_type first;
|
|
|
|
value_type second;
|
|
|
|
};
|
2008-07-10 23:50:10 +08:00
|
|
|
|
2008-07-04 23:57:48 +08:00
|
|
|
struct Triangle
|
|
|
|
{
|
2008-07-12 00:48:39 +08:00
|
|
|
Triangle():
|
|
|
|
p0(0),p1(0),p2(0) {}
|
|
|
|
|
|
|
|
Triangle(unsigned int ip0, unsigned int ip1, unsigned int ip2):
|
|
|
|
p0(ip0), p1(ip1), p2(ip2) {}
|
2008-07-04 23:57:48 +08:00
|
|
|
|
|
|
|
bool operator < (const Triangle& rhs) const
|
|
|
|
{
|
2008-07-12 00:48:39 +08:00
|
|
|
if (p0<rhs.p0) return true;
|
|
|
|
if (p0>rhs.p0) return false;
|
|
|
|
if (p1<rhs.p1) return true;
|
|
|
|
if (p1>rhs.p1) return false;
|
|
|
|
return p2<rhs.p2;
|
2008-07-04 23:57:48 +08:00
|
|
|
}
|
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
unsigned int p0;
|
|
|
|
unsigned int p1;
|
|
|
|
unsigned int p2;
|
2008-07-04 23:57:48 +08:00
|
|
|
};
|
|
|
|
|
2008-07-07 21:21:37 +08:00
|
|
|
typedef std::vector< KdNode > KdNodeList;
|
2008-07-12 00:48:39 +08:00
|
|
|
typedef std::vector< Triangle > TriangleList;
|
2008-07-08 04:27:56 +08:00
|
|
|
|
2008-07-06 20:14:19 +08:00
|
|
|
int addNode(const KdNode& node)
|
|
|
|
{
|
|
|
|
int num = _kdNodes.size();
|
|
|
|
_kdNodes.push_back(node);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
KdNode& getNode(int nodeNum) { return _kdNodes[nodeNum]; }
|
|
|
|
const KdNode& getNode(int nodeNum) const { return _kdNodes[nodeNum]; }
|
2008-07-06 20:14:19 +08:00
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
KdNodeList& getNodes() { return _kdNodes; }
|
|
|
|
const KdNodeList& getNodes() const { return _kdNodes; }
|
2008-07-06 20:14:19 +08:00
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
void setVertices(osg::Vec3Array* vertices) { _vertices = vertices; }
|
|
|
|
const osg::Vec3Array* getVertices() const { return _vertices.get(); }
|
2008-07-06 20:14:19 +08:00
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
unsigned int addTriangle(const Triangle& tri)
|
2008-07-10 23:50:10 +08:00
|
|
|
{
|
2008-07-12 00:48:39 +08:00
|
|
|
unsigned int num = _triangles.size();
|
|
|
|
_triangles.push_back(tri);
|
|
|
|
return num;
|
|
|
|
}
|
2008-07-06 20:14:19 +08:00
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
Triangle& getTriangle(unsigned int i) { return _triangles[i]; }
|
|
|
|
const Triangle& getTriangle(unsigned int i) const { return _triangles[i]; }
|
2008-07-06 20:14:19 +08:00
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
TriangleList& getTriangles() { return _triangles; }
|
|
|
|
const TriangleList& getTriangles() const { return _triangles; }
|
2008-07-06 20:14:19 +08:00
|
|
|
|
|
|
|
|
2008-07-12 00:48:39 +08:00
|
|
|
protected:
|
2008-07-06 20:14:19 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Vec3Array> _vertices;
|
2008-07-12 00:48:39 +08:00
|
|
|
KdNodeList _kdNodes;
|
2008-07-06 20:14:19 +08:00
|
|
|
TriangleList _triangles;
|
2008-07-04 23:57:48 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class OSG_EXPORT KdTreeBuilder : public osg::NodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
KdTreeBuilder();
|
|
|
|
|
2008-07-07 02:27:10 +08:00
|
|
|
KdTreeBuilder(const KdTreeBuilder& rhs);
|
|
|
|
|
2008-12-17 20:13:15 +08:00
|
|
|
META_NodeVisitor("osg","KdTreeBuilder")
|
|
|
|
|
2008-07-07 02:27:10 +08:00
|
|
|
virtual KdTreeBuilder* clone() { return new KdTreeBuilder(*this); }
|
|
|
|
|
2008-07-04 23:57:48 +08:00
|
|
|
void apply(osg::Geode& geode);
|
|
|
|
|
2008-07-06 20:14:19 +08:00
|
|
|
KdTree::BuildOptions _buildOptions;
|
|
|
|
|
2008-07-04 23:57:48 +08:00
|
|
|
osg::ref_ptr<osg::KdTree> _kdTreePrototype;
|
2008-07-12 00:48:39 +08:00
|
|
|
|
|
|
|
|
2008-07-06 20:14:19 +08:00
|
|
|
|
2008-07-04 23:57:48 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~KdTreeBuilder() {}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|