Fixes/clean up of KdTree/LineSegmentIntersector/PolytopeIntersector

This commit is contained in:
Robert Osfield 2017-05-10 09:19:29 +01:00
parent 6e1866ac18
commit a74b4b94a9
5 changed files with 22 additions and 61 deletions

View File

@ -468,7 +468,7 @@ int main( int argc, char **argv )
if (useKdTree) if (useKdTree)
{ {
OSG_NOTICE<<"Buildering KdTrees"<<std::endl; OSG_NOTICE<<"Building KdTrees"<<std::endl;
osg::ref_ptr<osg::KdTreeBuilder> builder = new osg::KdTreeBuilder; osg::ref_ptr<osg::KdTreeBuilder> builder = new osg::KdTreeBuilder;
loadedModel->accept(*builder); loadedModel->accept(*builder);
} }

View File

@ -158,9 +158,9 @@ class OSG_EXPORT KdTree : public osg::Shape
switch(numVertices) switch(numVertices)
{ {
case(1): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex]); break; case(1): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex]); break;
case(2): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex]+1); break; case(2): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex+1]); break;
case(3): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex]+1, _vertexIndices[primitiveIndex]+2); break; case(3): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex+1], _vertexIndices[primitiveIndex+2]); break;
case(4): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex]+1, _vertexIndices[primitiveIndex]+2, _vertexIndices[primitiveIndex]+3); break; case(4): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex+1], _vertexIndices[primitiveIndex+2], _vertexIndices[primitiveIndex+3]); break;
default : OSG_NOTICE<<"Warning: KdTree::intersect() encounted unsupported primitive size of "<<numVertices<<std::endl; break; default : OSG_NOTICE<<"Warning: KdTree::intersect() encounted unsupported primitive size of "<<numVertices<<std::endl; break;
} }
} }

View File

@ -64,7 +64,7 @@ struct PrimitiveIndicesCollector
inline void operator () (unsigned int p0) inline void operator () (unsigned int p0)
{ {
OSG_NOTICE<<" point ("<<p0<<")"<<std::endl; //OSG_NOTICE<<" point ("<<p0<<")"<<std::endl;
const osg::Vec3& v0 = (*(_buildKdTree->_kdTree.getVertices()))[p0]; const osg::Vec3& v0 = (*(_buildKdTree->_kdTree.getVertices()))[p0];
_buildKdTree->_kdTree.addPoint(p0); _buildKdTree->_kdTree.addPoint(p0);
@ -78,7 +78,7 @@ struct PrimitiveIndicesCollector
inline void operator () (unsigned int p0, unsigned int p1) inline void operator () (unsigned int p0, unsigned int p1)
{ {
OSG_NOTICE<<" line ("<<p0<<", "<<p1<<")"<<std::endl; //OSG_NOTICE<<" line ("<<p0<<", "<<p1<<")"<<std::endl;
const osg::Vec3& v0 = (*(_buildKdTree->_kdTree.getVertices()))[p0]; const osg::Vec3& v0 = (*(_buildKdTree->_kdTree.getVertices()))[p0];
const osg::Vec3& v1 = (*(_buildKdTree->_kdTree.getVertices()))[p1]; const osg::Vec3& v1 = (*(_buildKdTree->_kdTree.getVertices()))[p1];
@ -127,7 +127,7 @@ struct PrimitiveIndicesCollector
inline void operator () (unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3) inline void operator () (unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3)
{ {
OSG_NOTICE<<" quad ("<<p0<<", "<<p1<<", "<<p2<<", "<<p3<<")"<<std::endl; //OSG_NOTICE<<" quad ("<<p0<<", "<<p1<<", "<<p2<<", "<<p3<<")"<<std::endl;
const osg::Vec3& v0 = (*(_buildKdTree->_kdTree.getVertices()))[p0]; const osg::Vec3& v0 = (*(_buildKdTree->_kdTree.getVertices()))[p0];
const osg::Vec3& v1 = (*(_buildKdTree->_kdTree.getVertices()))[p1]; const osg::Vec3& v1 = (*(_buildKdTree->_kdTree.getVertices()))[p1];
@ -207,7 +207,6 @@ bool BuildKdTree::build(KdTree::BuildOptions& options, osg::Geometry* geometry)
osg::BoundingBox bb = _bb; osg::BoundingBox bb = _bb;
nodeNum = divide(options, bb, nodeNum, 0); nodeNum = divide(options, bb, nodeNum, 0);
OSG_NOTICE<<"After KdTree setup"<<std::endl;
osg::KdTree::Indices& primitiveIndices = _kdTree.getPrimitiveIndices(); osg::KdTree::Indices& primitiveIndices = _kdTree.getPrimitiveIndices();
KdTree::Indices new_indices; KdTree::Indices new_indices;
@ -283,21 +282,18 @@ int BuildKdTree::divide(KdTree::BuildOptions& options, osg::BoundingBox& bb, int
int istart = -node.first-1; int istart = -node.first-1;
int iend = istart+node.second-1; int iend = istart+node.second-1;
OSG_NOTICE<<"Computing bb on KdNode, numPrimitives="<<node.second<<std::endl;
// leaf is done, now compute bound on it. // leaf is done, now compute bound on it.
node.bb.init(); node.bb.init();
for(int i=istart; i<=iend; ++i) for(int i=istart; i<=iend; ++i)
{ {
unsigned int primitiveIndex = _kdTree.getPrimitiveIndices()[_primitiveIndices[i]]; unsigned int primitiveIndex = _kdTree.getPrimitiveIndices()[_primitiveIndices[i]];
unsigned int numPoints = _kdTree.getVertexIndices()[primitiveIndex++]; unsigned int numPoints = _kdTree.getVertexIndices()[primitiveIndex++];
OSG_NOTICE<<" Primitive "<<primitiveIndex<<", numPoints="<<numPoints<<std::endl;
for(; numPoints>0; --numPoints) for(; numPoints>0; --numPoints)
{ {
unsigned int vi = _kdTree.getVertexIndices()[primitiveIndex++]; unsigned int vi = _kdTree.getVertexIndices()[primitiveIndex++];
const osg::Vec3& v = (*_kdTree.getVertices())[vi]; const osg::Vec3& v = (*_kdTree.getVertices())[vi];
node.bb.expandBy(v); node.bb.expandBy(v);
OSG_NOTICE<<" vi="<<vi<<", v="<<v<<std::endl;
} }
} }

View File

@ -233,8 +233,6 @@ struct IntersectFunctor
{ {
if (_settings->_limitOneIntersection && _hit) return; if (_settings->_limitOneIntersection && _hit) return;
// OSG_NOTICE<<" intersect(v0=("<<v0<<"), v1=("<<v1<<"), v2=("<<v2<<") )"<<std::endl;
// const StartEnd startend = _startEndStack.back(); // const StartEnd startend = _startEndStack.back();
// const osg::Vec3& ls = startend.first; // const osg::Vec3& ls = startend.first;
// const osg::Vec3& le = startend.second; // const osg::Vec3& le = startend.second;
@ -314,11 +312,6 @@ struct IntersectFunctor
hit.primitiveIndex = _primitiveIndex; hit.primitiveIndex = _primitiveIndex;
hit.localIntersectionPoint = in; hit.localIntersectionPoint = in;
//OSG_NOTICE<<" intersection ("<<in<<") ratio="<<hit.ratio<<", _start=("<<_start<<"), _end=("<<_end<<")"<<std::endl;
// osg::Vec3 computed_inttersection = _start*(1.0-r)+_end*r;
//OSG_NOTICE<<" computed_intersection("<<computed_inttersection<<")"<<std::endl;
hit.localIntersectionNormal = normal; hit.localIntersectionNormal = normal;
if (_settings->_vertices.valid()) if (_settings->_vertices.valid())
@ -375,35 +368,31 @@ struct IntersectFunctor
intersect(v1,v2,v3); intersect(v1,v2,v3);
} }
bool intersect(const osg::Vec3Array*, int , unsigned int) void intersect(const osg::Vec3Array*, int , unsigned int)
{ {
return false;
} }
bool intersect(const osg::Vec3Array*, int, unsigned int, unsigned int) void intersect(const osg::Vec3Array*, int, unsigned int, unsigned int)
{ {
return false;
} }
bool intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1, unsigned int p2) void intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1, unsigned int p2)
{ {
if (_settings->_limitOneIntersection && _hit) return false; if (_settings->_limitOneIntersection && _hit) return;
_primitiveIndex = primitiveIndex; _primitiveIndex = primitiveIndex;
intersect((*vertices)[p0], (*vertices)[p1], (*vertices)[p2]); intersect((*vertices)[p0], (*vertices)[p1], (*vertices)[p2]);
return false;
} }
bool intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3) void intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3)
{ {
if (_settings->_limitOneIntersection && _hit) return false; if (_settings->_limitOneIntersection && _hit) return;
_primitiveIndex = primitiveIndex; _primitiveIndex = primitiveIndex;
intersect((*vertices)[p0], (*vertices)[p1], (*vertices)[p3]); intersect((*vertices)[p0], (*vertices)[p1], (*vertices)[p3]);
intersect((*vertices)[p1], (*vertices)[p2], (*vertices)[p3]); intersect((*vertices)[p1], (*vertices)[p2], (*vertices)[p3]);
return false;
} }
}; };

View File

@ -339,75 +339,51 @@ struct IntersectFunctor
} }
} }
bool intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0) void intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0)
{ {
if (_settings->_limitOneIntersection && _hit) return false; if (_settings->_limitOneIntersection && _hit) return;
if (contains((*vertices)[p0])) if (contains((*vertices)[p0]))
{ {
_primitiveIndex = primitiveIndex; _primitiveIndex = primitiveIndex;
addIntersection(); addIntersection();
return true;
}
else
{
return false;
} }
} }
bool intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1) void intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1)
{ {
if (_settings->_limitOneIntersection && _hit) return false; if (_settings->_limitOneIntersection && _hit) return;
if (contains((*vertices)[p0], (*vertices)[p1])) if (contains((*vertices)[p0], (*vertices)[p1]))
{ {
_primitiveIndex = primitiveIndex; _primitiveIndex = primitiveIndex;
addIntersection(); addIntersection();
return true;
}
else
{
return false;
} }
} }
bool intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1, unsigned int p2) void intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1, unsigned int p2)
{ {
if (_settings->_limitOneIntersection && _hit) return false; if (_settings->_limitOneIntersection && _hit) return;
if (contains((*vertices)[p0], (*vertices)[p1], (*vertices)[p2])) if (contains((*vertices)[p0], (*vertices)[p1], (*vertices)[p2]))
{ {
_primitiveIndex = primitiveIndex; _primitiveIndex = primitiveIndex;
addIntersection(); addIntersection();
return true;
}
else
{
return false;
} }
} }
bool intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3) void intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3)
{ {
if (_settings->_limitOneIntersection && _hit) return false; if (_settings->_limitOneIntersection && _hit) return;
if (contains((*vertices)[p0], (*vertices)[p1], (*vertices)[p2], (*vertices)[p3])) if (contains((*vertices)[p0], (*vertices)[p1], (*vertices)[p2], (*vertices)[p3]))
{ {
_primitiveIndex = primitiveIndex; _primitiveIndex = primitiveIndex;
addIntersection(); addIntersection();
return true;
}
else
{
return false;
} }
} }