Moved the building and intersecting of the KdTree into the .cpp, and cleaned up
the header to ready it for wider usage
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
#include <osg/Shape>
|
||||
#include <osg/Geometry>
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
|
||||
@@ -50,6 +52,12 @@ class OSG_EXPORT KdTree : public osg::Shape
|
||||
{
|
||||
LineSegmentIntersection():
|
||||
ratio(-1.0),
|
||||
p0(0),
|
||||
p1(0),
|
||||
p2(0),
|
||||
r0(0.0f),
|
||||
r1(0.0f),
|
||||
r2(0.0f),
|
||||
primitiveIndex(0) {}
|
||||
|
||||
bool operator < (const LineSegmentIntersection& rhs) const { return ratio < rhs.ratio; }
|
||||
@@ -60,21 +68,25 @@ class OSG_EXPORT KdTree : public osg::Shape
|
||||
double ratio;
|
||||
osg::Vec3d intersectionPoint;
|
||||
osg::Vec3 intersectionNormal;
|
||||
IndexList indexList;
|
||||
RatioList ratioList;
|
||||
|
||||
unsigned int p0;
|
||||
unsigned int p1;
|
||||
unsigned int p2;
|
||||
float r0;
|
||||
float r1;
|
||||
float r2;
|
||||
|
||||
unsigned int primitiveIndex;
|
||||
};
|
||||
|
||||
|
||||
typedef std::multiset<LineSegmentIntersection> LineSegmentIntersections;
|
||||
typedef std::vector<LineSegmentIntersection> LineSegmentIntersections;
|
||||
|
||||
/** compute the intersection of a line segment and the kdtree, return true if an intersection has been found.*/
|
||||
virtual bool intersect(const osg::Vec3& start, const osg::Vec3& end, LineSegmentIntersections& intersections) const;
|
||||
|
||||
|
||||
|
||||
typedef int value_type;
|
||||
typedef std::vector< value_type > Indices;
|
||||
|
||||
struct KdNode
|
||||
{
|
||||
@@ -94,25 +106,28 @@ class OSG_EXPORT KdTree : public osg::Shape
|
||||
|
||||
struct Triangle
|
||||
{
|
||||
Triangle(unsigned int p1, unsigned int p2, unsigned int p3):
|
||||
_p1(p1), _p2(p2), _p3(p3) {}
|
||||
Triangle():
|
||||
p0(0),p1(0),p2(0) {}
|
||||
|
||||
Triangle(unsigned int ip0, unsigned int ip1, unsigned int ip2):
|
||||
p0(ip0), p1(ip1), p2(ip2) {}
|
||||
|
||||
bool operator < (const Triangle& rhs) const
|
||||
{
|
||||
if (_p1<rhs._p1) return true;
|
||||
if (_p1>rhs._p1) return false;
|
||||
if (_p2<rhs._p2) return true;
|
||||
if (_p2>rhs._p2) return false;
|
||||
return _p3<rhs._p3;
|
||||
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;
|
||||
}
|
||||
|
||||
unsigned int _p1;
|
||||
unsigned int _p2;
|
||||
unsigned int _p3;
|
||||
unsigned int p0;
|
||||
unsigned int p1;
|
||||
unsigned int p2;
|
||||
};
|
||||
|
||||
typedef std::vector< unsigned int > AxisStack;
|
||||
typedef std::vector< KdNode > KdNodeList;
|
||||
typedef std::vector< Triangle > TriangleList;
|
||||
|
||||
int addNode(const KdNode& node)
|
||||
{
|
||||
@@ -121,77 +136,34 @@ class OSG_EXPORT KdTree : public osg::Shape
|
||||
return num;
|
||||
}
|
||||
|
||||
/// note, nodeNum is positive to distinguish from leftNum
|
||||
KdNode& getNode(int nodeNum)
|
||||
{
|
||||
if (nodeNum<0 || nodeNum>static_cast<int>(_kdNodes.size())-1)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"Warning: getNode("<<nodeNum<<") _kdNodes.size()="<<_kdNodes.size()<<std::endl;
|
||||
}
|
||||
return _kdNodes[nodeNum];
|
||||
}
|
||||
|
||||
/// note, nodeNum is positive to distinguish from leftNum
|
||||
const KdNode& getNode(int nodeNum) const
|
||||
{
|
||||
if (nodeNum<0 || nodeNum>static_cast<int>(_kdNodes.size())-1)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"Warning: getNode("<<nodeNum<<") _kdNodes.size()="<<_kdNodes.size()<<std::endl;
|
||||
}
|
||||
return _kdNodes[nodeNum];
|
||||
}
|
||||
KdNode& getNode(int nodeNum) { return _kdNodes[nodeNum]; }
|
||||
const KdNode& getNode(int nodeNum) const { return _kdNodes[nodeNum]; }
|
||||
|
||||
osg::BoundingBox& getBoundingBox(int nodeNum)
|
||||
KdNodeList& getNodes() { return _kdNodes; }
|
||||
const KdNodeList& getNodes() const { return _kdNodes; }
|
||||
|
||||
void setVertices(osg::Vec3Array* vertices) { _vertices = vertices; }
|
||||
const osg::Vec3Array* getVertices() const { return _vertices.get(); }
|
||||
|
||||
unsigned int addTriangle(const Triangle& tri)
|
||||
{
|
||||
return _kdNodes[nodeNum].bb;
|
||||
unsigned int num = _triangles.size();
|
||||
_triangles.push_back(tri);
|
||||
return num;
|
||||
}
|
||||
|
||||
Triangle& getTriangle(unsigned int i) { return _triangles[i]; }
|
||||
const Triangle& getTriangle(unsigned int i) const { return _triangles[i]; }
|
||||
|
||||
void computeDivisions(BuildOptions& options);
|
||||
TriangleList& getTriangles() { return _triangles; }
|
||||
const TriangleList& getTriangles() const { return _triangles; }
|
||||
|
||||
int divide(BuildOptions& options, osg::BoundingBox& bb, int nodeIndex, unsigned int level);
|
||||
|
||||
struct RayData
|
||||
{
|
||||
RayData(const osg::Vec3& s, const osg::Vec3& e):
|
||||
_s(s),
|
||||
_e(e)
|
||||
{
|
||||
_d = e - s;
|
||||
_length = _d.length();
|
||||
_inverse_length = 1.0f/_length;
|
||||
_d *= _inverse_length;
|
||||
}
|
||||
|
||||
|
||||
osg::Vec3 _s;
|
||||
osg::Vec3 _e;
|
||||
|
||||
osg::Vec3 _d;
|
||||
float _length;
|
||||
float _inverse_length;
|
||||
};
|
||||
|
||||
bool intersect(const KdNode& node, const RayData& rayData, osg::Vec3 s, osg::Vec3 e, LineSegmentIntersections& intersections) const;
|
||||
bool intersectAndClip(osg::Vec3& s, osg::Vec3& e, const osg::BoundingBox& bb) const;
|
||||
|
||||
typedef std::vector< osg::BoundingBox > BoundingBoxList;
|
||||
typedef std::vector< Triangle > TriangleList;
|
||||
typedef std::vector< osg::Vec3 > CenterList;
|
||||
|
||||
osg::observer_ptr<osg::Geometry> _geometry;
|
||||
|
||||
osg::BoundingBox _bb;
|
||||
|
||||
AxisStack _axisStack;
|
||||
KdNodeList _kdNodes;
|
||||
protected:
|
||||
|
||||
osg::ref_ptr<osg::Vec3Array> _vertices;
|
||||
|
||||
Indices _primitiveIndices;
|
||||
|
||||
KdNodeList _kdNodes;
|
||||
TriangleList _triangles;
|
||||
CenterList _centers;
|
||||
|
||||
};
|
||||
|
||||
@@ -210,6 +182,8 @@ class OSG_EXPORT KdTreeBuilder : public osg::NodeVisitor
|
||||
KdTree::BuildOptions _buildOptions;
|
||||
|
||||
osg::ref_ptr<osg::KdTree> _kdTreePrototype;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user