From d3fd43cc6f572d3adc14687b7700b1c825043aea Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 10 Jul 2008 15:50:10 +0000 Subject: [PATCH] Streamlined KdTree implementation --- include/osg/KdTree | 95 +++------ src/osg/KdTree.cpp | 478 +++++++++++++++++++++++++-------------------- 2 files changed, 286 insertions(+), 287 deletions(-) diff --git a/include/osg/KdTree b/include/osg/KdTree index 49ff58f3a..baf4c5b43 100644 --- a/include/osg/KdTree +++ b/include/osg/KdTree @@ -91,24 +91,7 @@ class OSG_EXPORT KdTree : public osg::Shape value_type first; value_type second; }; - - - struct KdLeaf - { - KdLeaf(): - first(0), - second(0) {} - - KdLeaf(value_type f, value_type s): - first(f), - second(s) {} - - osg::BoundingBox bb; - - value_type first; - value_type second; - }; - + struct Triangle { Triangle(unsigned int p1, unsigned int p2, unsigned int p3): @@ -130,46 +113,6 @@ class OSG_EXPORT KdTree : public osg::Shape typedef std::vector< unsigned int > AxisStack; typedef std::vector< KdNode > KdNodeList; - typedef std::vector< KdLeaf > KdLeafList; - - /// note, leafNum is negative to distinguish from nodeNum - int addLeaf(const KdLeaf& leaf) { int num = _kdLeaves.size(); _kdLeaves.push_back(leaf); return -(num+1); } - - int replaceLeaf(int leafNum, const KdLeaf& leaf) - { - int num = -leafNum-1; - - if (num>static_cast(_kdLeaves.size())-1) - { - osg::notify(osg::NOTICE)<<"Warning: replaceChild("<_kdNodes.reserve()="<size()*2; _primitiveIndices.reserve(estimatedNumTriangles); - _boundingBoxes.reserve(estimatedNumTriangles); _triangles.reserve(estimatedNumTriangles); _centers.reserve(estimatedNumTriangles); @@ -120,14 +116,13 @@ bool KdTree::build(BuildOptions& options, osg::Geometry* geometry) _primitiveIndices.reserve(vertices->size()); + KdNode node(-1, _primitiveIndices.size()); + node.bb = _bb; - - KdLeaf leaf(0, _primitiveIndices.size()); - - int leafNum = addLeaf(leaf); + int nodeNum = addNode(node); osg::BoundingBox bb = _bb; - int nodeNum = divide(options, bb, leafNum, 0); + nodeNum = divide(options, bb, nodeNum, 0); #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<"Root nodeNum="<=0.0f) - { - if (ds12<0.0f) continue; - if (ds12>d312) continue; - } - else // d312 < 0 - { - if (ds12>0.0f) continue; - if (ds12=0.0f) - { - if (ds23<0.0f) continue; - if (ds23>d123) continue; - } - else // d123 < 0 - { - if (ds23>0.0f) continue; - if (ds23=0.0f) - { - if (ds31<0.0f) continue; - if (ds31>d231) continue; - } - else // d231 < 0 - { - if (ds31>0.0f) continue; - if (ds31_length) continue; - - osg::Vec3 normal = v12^v23; - normal.normalize(); - - float r = d/_length; - - LineSegmentIntersection intersection; - intersection.ratio = r; - intersection.primitiveIndex = _primitiveIndices[i]; - intersection.intersectionPoint = in; - intersection.intersectionNormal = normal; - - intersection.indexList.push_back(tri._p1); - intersection.indexList.push_back(tri._p2); - intersection.indexList.push_back(tri._p3); - - intersection.ratioList.push_back(r1); - intersection.ratioList.push_back(r2); - intersection.ratioList.push_back(r3); - - intersections.insert(intersection); - - // osg::notify(osg::NOTICE)<<" got intersection ("<0) intersect(getNode(node.first), start, end, ls, le, intersections); - else if (node.first<0) intersect(getLeaf(node.first), start, end, intersections); + if (!intersectAndClip(ls, le, node.bb)) return false; - if (node.second>0) intersect(getNode(node.second), start, end, ls, le, intersections); - else if (node.second<0) intersect(getLeaf(node.second), start, end, intersections); +#if 0 + { + osg::notify(osg::NOTICE)<<"Failed intersectAndClip("<=0.0f) + { + if (ds12<0.0f) continue; + if (ds12>d312) continue; + } + else // d312 < 0 + { + if (ds12>0.0f) continue; + if (ds12=0.0f) + { + if (ds23<0.0f) continue; + if (ds23>d123) continue; + } + else // d123 < 0 + { + if (ds23>0.0f) continue; + if (ds23=0.0f) + { + if (ds31<0.0f) continue; + if (ds31>d231) continue; + } + else // d231 < 0 + { + if (ds31>0.0f) continue; + if (ds31rayData._length) continue; + + osg::Vec3 normal = v12^v23; + normal.normalize(); + + float r = d* rayData._inverse_length; + + LineSegmentIntersection intersection; + intersection.ratio = r; + intersection.primitiveIndex = _primitiveIndices[i]; + intersection.intersectionPoint = in; + intersection.intersectionNormal = normal; + + intersection.indexList.push_back(tri._p1); + intersection.indexList.push_back(tri._p2); + intersection.indexList.push_back(tri._p3); + + intersection.ratioList.push_back(r1); + intersection.ratioList.push_back(r2); + intersection.ratioList.push_back(r3); + + intersections.insert(intersection); + + // osg::notify(osg::NOTICE)<<" got intersection ("<