Further work on PlaneIntersector

This commit is contained in:
Robert Osfield
2006-11-29 14:21:59 +00:00
parent 35cb04437d
commit ab7d1ecc42
6 changed files with 314 additions and 168 deletions

View File

@@ -21,6 +21,127 @@
using namespace osgUtil;
namespace PlaneIntersectorUtils
{
struct TriangleIntersection
{
osg::Vec3d v[2];
};
typedef std::list<TriangleIntersection> TriangleIntersections;
struct TriangleIntersector
{
osg::Plane _plane;
osg::Polytope _polytope;
bool _hit;
TriangleIntersections _intersections;
TriangleIntersector()
{
_hit = false;
}
void set(const osg::Plane& plane, const osg::Polytope& polytope)
{
_plane = plane;
_polytope = polytope;
_hit = false;
}
inline void operator () (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool)
{
double d1 = _plane.distance(v1);
double d2 = _plane.distance(v2);
double d3 = _plane.distance(v3);
unsigned int numBelow = 0;
unsigned int numAbove = 0;
unsigned int numOnPlane = 0;
if (d1<0) ++numBelow;
else if (d1>0) ++numAbove;
else ++numOnPlane;
if (d2<0) ++numBelow;
else if (d2>0) ++numAbove;
else ++numOnPlane;
if (d3<0) ++numBelow;
else if (d3>0) ++numAbove;
else ++numOnPlane;
// trivially discard triangles that are completely one side of the plane
if (numAbove==3 || numBelow==3) return;
_hit = true;
if (numOnPlane==3)
{
// triangle lives wholy in the plane
osg::notify(osg::NOTICE)<<"3"<<std::endl;
return;
}
if (numOnPlane==2)
{
// one edge lives wholy in the plane
osg::notify(osg::NOTICE)<<"2"<<std::endl;
return;
}
if (numOnPlane==1)
{
// one point lives wholy in the plane
osg::notify(osg::NOTICE)<<"1"<<std::endl;
return;
}
TriangleIntersection ti;
unsigned int numIntersects = 0;
if (d1*d2 < 0.0)
{
// edge 12 itersects
double div = 1.0 / (d2-d1);
ti.v[numIntersects++] = v1* (d2*div) - v2 * (d1*div);
}
if (d2*d3 < 0.0)
{
// edge 23 itersects
double div = 1.0 / (d3-d2);
ti.v[numIntersects++] = v2* (d3*div) - v3 * (d2*div);
}
if (d1*d3 < 0.0)
{
if (numIntersects<2)
{
// edge 13 itersects
double div = 1.0 / (d3-d1);
ti.v[numIntersects++] = v1* (d3*div) - v3 * (d1*div);
}
else
{
osg::notify(osg::NOTICE)<<"!!! too many intersecting edges found !!!"<<std::endl;
}
}
osg::notify(osg::NOTICE)<<"ti.v1="<<ti.v[0]<<" ti.v2="<<ti.v[1]<<std::endl;
_intersections.push_back(ti);
}
};
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
@@ -101,18 +222,32 @@ void PlaneIntersector::leave()
void PlaneIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable)
{
osg::notify(osg::NOTICE)<<"PlaneIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable)"<<std::endl;
// osg::notify(osg::NOTICE)<<"PlaneIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable)"<<std::endl;
if ( !_plane.intersect( drawable->getBound() ) ) return;
if ( _plane.intersect( drawable->getBound() )!=0 ) return;
if ( !_polytope.contains( drawable->getBound() ) ) return;
osg::notify(osg::NOTICE)<<"Succed PlaneIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable)"<<std::endl;
// osg::notify(osg::NOTICE)<<"Succed PlaneIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable)"<<std::endl;
osg::TriangleFunctor<PlaneIntersectorUtils::TriangleIntersector> ti;
ti.set(_plane,_polytope);
drawable->accept(ti);
if (ti._hit)
{
Intersection hit;
hit.nodePath = iv.getNodePath();
hit.drawable = drawable;
insertIntersection(hit);
osg::notify(osg::NOTICE)<<std::endl<<"++++++++++++ Found intersections +++++++++++++++++++++++++"<<std::endl<<std::endl;
}
else
{
osg::notify(osg::NOTICE)<<std::endl<<"------------ No intersections -------------------------"<<std::endl<<std::endl;
}
Intersection hit;
hit.nodePath = iv.getNodePath();
hit.drawable = drawable;
insertIntersection(hit);
}