Merge pull request #260 from blobfish/primitiveIndex

Intersections and primitive index
This commit is contained in:
OpenSceneGraph git repository 2017-06-19 08:12:47 +01:00 committed by GitHub
commit 9ba59eceaa
5 changed files with 55 additions and 26 deletions

View File

@ -349,7 +349,9 @@ public:
if (picker->containsIntersections()) if (picker->containsIntersections())
{ {
osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection(); osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection();
osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl; osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl
<<" primitive index "<<intersection.primitiveIndex
<<std::endl;
osg::NodePath& nodePath = intersection.nodePath; osg::NodePath& nodePath = intersection.nodePath;
node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;

View File

@ -69,41 +69,45 @@ class OSG_EXPORT KdTree : public osg::Shape
inline unsigned int addPoint(unsigned int p0) inline unsigned int addPoint(unsigned int p0)
{ {
unsigned int i = _vertexIndices.size(); unsigned int i = _vertexIndices.size();
_primitiveIndices.push_back(i); _vertexIndices.push_back(_primitiveIndices.size() + _degenerateCount);
_vertexIndices.push_back(1); _vertexIndices.push_back(1);
_vertexIndices.push_back(p0); _vertexIndices.push_back(p0);
_primitiveIndices.push_back(i);
return i; return i;
} }
inline unsigned int addLine(unsigned int p0, unsigned int p1) inline unsigned int addLine(unsigned int p0, unsigned int p1)
{ {
unsigned int i = _vertexIndices.size(); unsigned int i = _vertexIndices.size();
_primitiveIndices.push_back(i); _vertexIndices.push_back(_primitiveIndices.size() + _degenerateCount);
_vertexIndices.push_back(2); _vertexIndices.push_back(2);
_vertexIndices.push_back(p0); _vertexIndices.push_back(p0);
_vertexIndices.push_back(p1); _vertexIndices.push_back(p1);
_primitiveIndices.push_back(i);
return i; return i;
} }
inline unsigned int addTriangle(unsigned int p0, unsigned int p1, unsigned int p2) inline unsigned int addTriangle(unsigned int p0, unsigned int p1, unsigned int p2)
{ {
unsigned int i = _vertexIndices.size(); unsigned int i = _vertexIndices.size();
_primitiveIndices.push_back(i); _vertexIndices.push_back(_primitiveIndices.size() + _degenerateCount);
_vertexIndices.push_back(3); _vertexIndices.push_back(3);
_vertexIndices.push_back(p0); _vertexIndices.push_back(p0);
_vertexIndices.push_back(p1); _vertexIndices.push_back(p1);
_vertexIndices.push_back(p2); _vertexIndices.push_back(p2);
_primitiveIndices.push_back(i);
return i; return i;
} }
inline unsigned int addQuad(unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3) inline unsigned int addQuad(unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3)
{ {
unsigned int i = _vertexIndices.size(); unsigned int i = _vertexIndices.size();
_primitiveIndices.push_back(i); _vertexIndices.push_back(_primitiveIndices.size() + _degenerateCount);
_vertexIndices.push_back(4); _vertexIndices.push_back(4);
_vertexIndices.push_back(p0); _vertexIndices.push_back(p0);
_vertexIndices.push_back(p1); _vertexIndices.push_back(p1);
_vertexIndices.push_back(p2); _vertexIndices.push_back(p2);
_vertexIndices.push_back(p3); _vertexIndices.push_back(p3);
_primitiveIndices.push_back(i);
return i; return i;
} }
@ -154,13 +158,14 @@ class OSG_EXPORT KdTree : public osg::Shape
for(int i=istart; i<iend; ++i) for(int i=istart; i<iend; ++i)
{ {
unsigned int primitiveIndex = _primitiveIndices[i]; unsigned int primitiveIndex = _primitiveIndices[i];
unsigned int originalPIndex = _vertexIndices[primitiveIndex++];
unsigned int numVertices = _vertexIndices[primitiveIndex++]; unsigned int numVertices = _vertexIndices[primitiveIndex++];
switch(numVertices) switch(numVertices)
{ {
case(1): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex]); break; case(1): functor.intersect(_vertices.get(), originalPIndex, _vertexIndices[primitiveIndex]); break;
case(2): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex+1]); break; case(2): functor.intersect(_vertices.get(), originalPIndex, _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(), originalPIndex, _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(), originalPIndex, _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;
} }
} }
@ -174,7 +179,7 @@ class OSG_EXPORT KdTree : public osg::Shape
} }
} }
unsigned int _degenerateCount;
protected: protected:

View File

@ -86,6 +86,7 @@ struct PrimitiveIndicesCollector
if (v0==v1) if (v0==v1)
{ {
//OSG_NOTICE<<"Disgarding degenerate triangle"<<std::endl; //OSG_NOTICE<<"Disgarding degenerate triangle"<<std::endl;
_buildKdTree->_kdTree._degenerateCount++;
return; return;
} }
@ -111,6 +112,7 @@ struct PrimitiveIndicesCollector
if (v0==v1 || v1==v2 || v2==v0) if (v0==v1 || v1==v2 || v2==v0)
{ {
//OSG_NOTICE<<"Disgarding degenerate triangle"<<std::endl; //OSG_NOTICE<<"Disgarding degenerate triangle"<<std::endl;
_buildKdTree->_kdTree._degenerateCount++;
return; return;
} }
@ -138,6 +140,7 @@ struct PrimitiveIndicesCollector
if (v0==v1 || v1==v2 || v2==v0 || v3==v0 || v3==v1 || v3==v2) if (v0==v1 || v1==v2 || v2==v0 || v3==v0 || v3==v1 || v3==v2)
{ {
//OSG_NOTICE<<"Disgarding degenerate quad"<<std::endl; //OSG_NOTICE<<"Disgarding degenerate quad"<<std::endl;
_buildKdTree->_kdTree._degenerateCount++;
return; return;
} }
@ -287,6 +290,7 @@ int BuildKdTree::divide(KdTree::BuildOptions& options, osg::BoundingBox& bb, int
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]];
primitiveIndex++; //skip original Primitive index
unsigned int numPoints = _kdTree.getVertexIndices()[primitiveIndex++]; unsigned int numPoints = _kdTree.getVertexIndices()[primitiveIndex++];
for(; numPoints>0; --numPoints) for(; numPoints>0; --numPoints)
@ -480,12 +484,13 @@ KdTree::BuildOptions::BuildOptions():
// //
// KdTree // KdTree
KdTree::KdTree() KdTree::KdTree() : _degenerateCount(0)
{ {
} }
KdTree::KdTree(const KdTree& rhs, const osg::CopyOp& copyop): KdTree::KdTree(const KdTree& rhs, const osg::CopyOp& copyop):
Shape(rhs, copyop), Shape(rhs, copyop),
_degenerateCount(rhs._degenerateCount),
_vertices(rhs._vertices), _vertices(rhs._vertices),
_kdNodes(rhs._kdNodes) _kdNodes(rhs._kdNodes)
{ {

View File

@ -357,15 +357,15 @@ struct IntersectFunctor
// handle triangles // handle triangles
void operator()(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2, bool /*treatVertexDataAsTemporary*/) void operator()(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2, bool /*treatVertexDataAsTemporary*/)
{ {
++_primitiveIndex;
intersect(v0,v1,v2); intersect(v0,v1,v2);
++_primitiveIndex;
} }
void operator()(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3, bool /*treatVertexDataAsTemporary*/) void operator()(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3, bool /*treatVertexDataAsTemporary*/)
{ {
++_primitiveIndex;
intersect(v0,v1,v3); intersect(v0,v1,v3);
intersect(v1,v2,v3); intersect(v1,v2,v3);
++_primitiveIndex;
} }
void intersect(const osg::Vec3Array*, int , unsigned int) void intersect(const osg::Vec3Array*, int , unsigned int)

View File

@ -256,9 +256,11 @@ struct IntersectFunctor
{ {
if (_settings->_limitOneIntersection && _hit) return; if (_settings->_limitOneIntersection && _hit) return;
if ((_settings->_primitiveMask&PolytopeIntersector::POINT_PRIMITIVES)==0)
{
++_primitiveIndex; ++_primitiveIndex;
return;
if ((_settings->_primitiveMask&PolytopeIntersector::POINT_PRIMITIVES)==0) return; }
// initialize the set of vertices to test. // initialize the set of vertices to test.
src.clear(); src.clear();
@ -278,7 +280,11 @@ struct IntersectFunctor
{ {
const osg::Plane& plane=*pitr; const osg::Plane& plane=*pitr;
double d1=plane.distance(v0); double d1=plane.distance(v0);
if (d1<0.0) return; // point outside if (d1<0.0) // point outside
{
++_primitiveIndex;
return;
}
} }
} }
} }
@ -286,6 +292,8 @@ struct IntersectFunctor
src.push_back(v0); src.push_back(v0);
addIntersection(); addIntersection();
++_primitiveIndex;
} }
// handle lines // handle lines
@ -293,9 +301,11 @@ struct IntersectFunctor
{ {
if (_settings->_limitOneIntersection && _hit) return; if (_settings->_limitOneIntersection && _hit) return;
if ((_settings->_primitiveMask&PolytopeIntersector::LINE_PRIMITIVES)==0)
{
++_primitiveIndex; ++_primitiveIndex;
return;
if ((_settings->_primitiveMask&PolytopeIntersector::LINE_PRIMITIVES)==0) return; }
src.clear(); src.clear();
src.push_back(v0); src.push_back(v0);
@ -305,6 +315,7 @@ struct IntersectFunctor
{ {
addIntersection(); addIntersection();
} }
++_primitiveIndex;
} }
// handle triangles // handle triangles
@ -312,9 +323,11 @@ struct IntersectFunctor
{ {
if (_settings->_limitOneIntersection && _hit) return; if (_settings->_limitOneIntersection && _hit) return;
if ((_settings->_primitiveMask&PolytopeIntersector::TRIANGLE_PRIMITIVES)==0)
{
++_primitiveIndex; ++_primitiveIndex;
return;
if ((_settings->_primitiveMask&PolytopeIntersector::TRIANGLE_PRIMITIVES)==0) return; }
src.clear(); src.clear();
src.push_back(v0); src.push_back(v0);
@ -326,15 +339,18 @@ struct IntersectFunctor
{ {
addIntersection(); addIntersection();
} }
++_primitiveIndex;
} }
void operator()(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3, bool /*treatVertexDataAsTemporary*/) void operator()(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3, bool /*treatVertexDataAsTemporary*/)
{ {
if (_settings->_limitOneIntersection && _hit) return; if (_settings->_limitOneIntersection && _hit) return;
if ((_settings->_primitiveMask&PolytopeIntersector::TRIANGLE_PRIMITIVES)==0)
{
++_primitiveIndex; ++_primitiveIndex;
return;
if ((_settings->_primitiveMask&PolytopeIntersector::TRIANGLE_PRIMITIVES)==0) return; }
src.clear(); src.clear();
@ -348,6 +364,7 @@ struct IntersectFunctor
{ {
addIntersection(); addIntersection();
} }
++_primitiveIndex;
} }
void intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0) void intersect(const osg::Vec3Array* vertices, int primitiveIndex, unsigned int p0)