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

@ -24,7 +24,7 @@ namespace osg {
* Note, if no slave cameras are attached to the view then the master camera does both the control and implementation of the rendering of the scene,
* but if slave cameras are present then the master controls the view onto the scene, while the slaves implement the rendering of the scene.
*/
class OSG_EXPORT View : public osg::Referenced
class OSG_EXPORT View : public virtual osg::Referenced
{
public :

View File

@ -37,13 +37,22 @@ class OSGUTIL_EXPORT PlaneIntersector : public Intersector
bool operator < (const Intersection& rhs) const
{
if (polyline < rhs.polyline) return true;
if (rhs.polyline < polyline) return false;
if (nodePath < rhs.nodePath) return true;
if (rhs.nodePath < nodePath ) return false;
return (drawable < rhs.drawable);
}
typedef std::vector<osg::Vec3d> Polyline;
osg::NodePath nodePath;
osg::ref_ptr<osg::RefMatrix> matrix;
osg::ref_ptr<osg::Drawable> drawable;
Polyline polyline;
};
typedef std::set<Intersection> Intersections;

View File

@ -43,7 +43,7 @@ osg::Node* DatabaseCacheReadCallback::readNodeFile(const std::string& filename)
FileNameSceneMap::iterator itr = _filenameSceneMap.find(filename);
if (itr != _filenameSceneMap.end())
{
osg::notify(osg::NOTICE)<<"Getting from cache "<<filename<<std::endl;
osg::notify(osg::INFO)<<"Getting from cache "<<filename<<std::endl;
return itr->second.get();
}
@ -59,7 +59,7 @@ osg::Node* DatabaseCacheReadCallback::readNodeFile(const std::string& filename)
if (_filenameSceneMap.size() < _maxNumFilesToCache)
{
osg::notify(osg::NOTICE)<<"Inserting into cache "<<filename<<std::endl;
osg::notify(osg::INFO)<<"Inserting into cache "<<filename<<std::endl;
_filenameSceneMap[filename] = node;
}
@ -79,7 +79,7 @@ osg::Node* DatabaseCacheReadCallback::readNodeFile(const std::string& filename)
break;
}
}
osg::notify(osg::NOTICE)<<"And the replacing with "<<filename<<std::endl;
osg::notify(osg::INFO)<<"And the replacing with "<<filename<<std::endl;
_filenameSceneMap[filename] = node;
}
}

View File

@ -6,24 +6,24 @@ CXXFILES = \
CubeMapGenerator.cpp\
CullVisitor.cpp\
DelaunayTriangulator.cpp\
GLObjectsVisitor.cpp\
DisplayRequirementsVisitor.cpp\
GLObjectsVisitor.cpp\
HalfWayMapGenerator.cpp\
HighlightMapGenerator.cpp\
IntersectionVisitor.cpp\
IntersectVisitor.cpp\
LineSegmentIntersector.cpp\
Optimizer.cpp\
PlaneIntersector.cpp\
PolytopeIntersector.cpp\
IntersectVisitor.cpp\
Optimizer.cpp\
PositionalStateContainer.cpp\
RenderBin.cpp\
StateGraph.cpp\
RenderLeaf.cpp\
RenderStage.cpp\
PositionalStateContainer.cpp\
SceneView.cpp\
Simplifier.cpp\
SmoothingVisitor.cpp\
StateGraph.cpp\
TangentSpaceGenerator.cpp\
Tesselator.cpp\
TransformAttributeFunctor.cpp\

View File

@ -21,176 +21,177 @@
using namespace osgUtil;
struct TriangleIntersection
namespace LineSegmentIntersectorUtils
{
TriangleIntersection(unsigned int index, const osg::Vec3& normal, float r1, const osg::Vec3* v1, float r2, const osg::Vec3* v2, float r3, const osg::Vec3* v3):
_index(index),
_normal(normal),
_r1(r1),
_v1(v1),
_r2(r2),
_v2(v2),
_r3(r3),
_v3(v3) {}
unsigned int _index;
const osg::Vec3 _normal;
float _r1;
const osg::Vec3* _v1;
float _r2;
const osg::Vec3* _v2;
float _r3;
const osg::Vec3* _v3;
};
struct TriangleIntersection
{
TriangleIntersection(unsigned int index, const osg::Vec3& normal, float r1, const osg::Vec3* v1, float r2, const osg::Vec3* v2, float r3, const osg::Vec3* v3):
_index(index),
_normal(normal),
_r1(r1),
_v1(v1),
_r2(r2),
_v2(v2),
_r3(r3),
_v3(v3) {}
struct TriangleIntersector
{
osg::Vec3 _s;
osg::Vec3 _d;
float _length;
int _index;
float _ratio;
bool _hit;
unsigned int _index;
const osg::Vec3 _normal;
float _r1;
const osg::Vec3* _v1;
float _r2;
const osg::Vec3* _v2;
float _r3;
const osg::Vec3* _v3;
};
typedef std::multimap<float,TriangleIntersection> TriangleIntersections;
TriangleIntersections _intersections;
TriangleIntersector()
struct TriangleIntersector
{
}
osg::Vec3 _s;
osg::Vec3 _d;
float _length;
TriangleIntersector(const osg::Vec3d& start, osg::Vec3d& end, float ratio=FLT_MAX)
{
set(start,end,ratio);
}
int _index;
float _ratio;
bool _hit;
void set(const osg::Vec3d& start, osg::Vec3d& end, float ratio=FLT_MAX)
{
_hit=false;
_index = 0;
_ratio = ratio;
TriangleIntersections _intersections;
_s = start;
_d = end - start;
_length = _d.length();
_d /= _length;
}
// bool intersect(const Vec3& v1,const Vec3& v2,const Vec3& v3,float& r)
inline void operator () (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool treatVertexDataAsTemporary)
{
++_index;
if (v1==v2 || v2==v3 || v1==v3) return;
osg::Vec3 v12 = v2-v1;
osg::Vec3 n12 = v12^_d;
float ds12 = (_s-v1)*n12;
float d312 = (v3-v1)*n12;
if (d312>=0.0f)
TriangleIntersector()
{
if (ds12<0.0f) return;
if (ds12>d312) return;
}
else // d312 < 0
{
if (ds12>0.0f) return;
if (ds12<d312) return;
_length = 0.0f;
_index = 0;
_ratio = 0.0f;
_hit = false;
}
osg::Vec3 v23 = v3-v2;
osg::Vec3 n23 = v23^_d;
float ds23 = (_s-v2)*n23;
float d123 = (v1-v2)*n23;
if (d123>=0.0f)
void set(const osg::Vec3d& start, osg::Vec3d& end, float ratio=FLT_MAX)
{
if (ds23<0.0f) return;
if (ds23>d123) return;
}
else // d123 < 0
{
if (ds23>0.0f) return;
if (ds23<d123) return;
_hit=false;
_index = 0;
_ratio = ratio;
_s = start;
_d = end - start;
_length = _d.length();
_d /= _length;
}
osg::Vec3 v31 = v1-v3;
osg::Vec3 n31 = v31^_d;
float ds31 = (_s-v3)*n31;
float d231 = (v2-v3)*n31;
if (d231>=0.0f)
inline void operator () (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool treatVertexDataAsTemporary)
{
if (ds31<0.0f) return;
if (ds31>d231) return;
}
else // d231 < 0
{
if (ds31>0.0f) return;
if (ds31<d231) return;
++_index;
if (v1==v2 || v2==v3 || v1==v3) return;
osg::Vec3 v12 = v2-v1;
osg::Vec3 n12 = v12^_d;
float ds12 = (_s-v1)*n12;
float d312 = (v3-v1)*n12;
if (d312>=0.0f)
{
if (ds12<0.0f) return;
if (ds12>d312) return;
}
else // d312 < 0
{
if (ds12>0.0f) return;
if (ds12<d312) return;
}
osg::Vec3 v23 = v3-v2;
osg::Vec3 n23 = v23^_d;
float ds23 = (_s-v2)*n23;
float d123 = (v1-v2)*n23;
if (d123>=0.0f)
{
if (ds23<0.0f) return;
if (ds23>d123) return;
}
else // d123 < 0
{
if (ds23>0.0f) return;
if (ds23<d123) return;
}
osg::Vec3 v31 = v1-v3;
osg::Vec3 n31 = v31^_d;
float ds31 = (_s-v3)*n31;
float d231 = (v2-v3)*n31;
if (d231>=0.0f)
{
if (ds31<0.0f) return;
if (ds31>d231) return;
}
else // d231 < 0
{
if (ds31>0.0f) return;
if (ds31<d231) return;
}
float r3;
if (ds12==0.0f) r3=0.0f;
else if (d312!=0.0f) r3 = ds12/d312;
else return; // the triangle and the line must be parallel intersection.
float r1;
if (ds23==0.0f) r1=0.0f;
else if (d123!=0.0f) r1 = ds23/d123;
else return; // the triangle and the line must be parallel intersection.
float r2;
if (ds31==0.0f) r2=0.0f;
else if (d231!=0.0f) r2 = ds31/d231;
else return; // the triangle and the line must be parallel intersection.
float total_r = (r1+r2+r3);
if (total_r!=1.0f)
{
if (total_r==0.0f) return; // the triangle and the line must be parallel intersection.
float inv_total_r = 1.0f/total_r;
r1 *= inv_total_r;
r2 *= inv_total_r;
r3 *= inv_total_r;
}
osg::Vec3 in = v1*r1+v2*r2+v3*r3;
if (!in.valid())
{
osg::notify(osg::WARN)<<"Warning:: Picked up error in TriangleIntersect"<<std::endl;
osg::notify(osg::WARN)<<" ("<<v1<<",\t"<<v2<<",\t"<<v3<<")"<<std::endl;
osg::notify(osg::WARN)<<" ("<<r1<<",\t"<<r2<<",\t"<<r3<<")"<<std::endl;
return;
}
float d = (in-_s)*_d;
if (d<0.0f) return;
if (d>_length) return;
osg::Vec3 normal = v12^v23;
normal.normalize();
float r = d/_length;
if (treatVertexDataAsTemporary)
{
_intersections.insert(std::pair<const float,TriangleIntersection>(r,TriangleIntersection(_index-1,normal,r1,0,r2,0,r3,0)));
}
else
{
_intersections.insert(std::pair<const float,TriangleIntersection>(r,TriangleIntersection(_index-1,normal,r1,&v1,r2,&v2,r3,&v3)));
}
_hit = true;
}
};
float r3;
if (ds12==0.0f) r3=0.0f;
else if (d312!=0.0f) r3 = ds12/d312;
else return; // the triangle and the line must be parallel intersection.
float r1;
if (ds23==0.0f) r1=0.0f;
else if (d123!=0.0f) r1 = ds23/d123;
else return; // the triangle and the line must be parallel intersection.
float r2;
if (ds31==0.0f) r2=0.0f;
else if (d231!=0.0f) r2 = ds31/d231;
else return; // the triangle and the line must be parallel intersection.
float total_r = (r1+r2+r3);
if (total_r!=1.0f)
{
if (total_r==0.0f) return; // the triangle and the line must be parallel intersection.
float inv_total_r = 1.0f/total_r;
r1 *= inv_total_r;
r2 *= inv_total_r;
r3 *= inv_total_r;
}
osg::Vec3 in = v1*r1+v2*r2+v3*r3;
if (!in.valid())
{
osg::notify(osg::WARN)<<"Warning:: Picked up error in TriangleIntersect"<<std::endl;
osg::notify(osg::WARN)<<" ("<<v1<<",\t"<<v2<<",\t"<<v3<<")"<<std::endl;
osg::notify(osg::WARN)<<" ("<<r1<<",\t"<<r2<<",\t"<<r3<<")"<<std::endl;
return;
}
float d = (in-_s)*_d;
if (d<0.0f) return;
if (d>_length) return;
osg::Vec3 normal = v12^v23;
normal.normalize();
float r = d/_length;
if (treatVertexDataAsTemporary)
{
_intersections.insert(std::pair<const float,TriangleIntersection>(r,TriangleIntersection(_index-1,normal,r1,0,r2,0,r3,0)));
}
else
{
_intersections.insert(std::pair<const float,TriangleIntersection>(r,TriangleIntersection(_index-1,normal,r1,&v1,r2,&v2,r3,&v3)));
}
_hit = true;
}
};
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
@ -286,14 +287,15 @@ void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Dr
s = _start;
e = _end;
osg::TriangleFunctor<TriangleIntersector> ti;
osg::TriangleFunctor<LineSegmentIntersectorUtils::TriangleIntersector> ti;
ti.set(s,e);
drawable->accept(ti);
if (ti._hit)
{
osg::Geometry* geometry = drawable->asGeometry();
for(TriangleIntersector::TriangleIntersections::iterator thitr = ti._intersections.begin();
for(LineSegmentIntersectorUtils::TriangleIntersections::iterator thitr = ti._intersections.begin();
thitr != ti._intersections.end();
++thitr)
{
@ -304,7 +306,7 @@ void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Dr
// remap ratio into _start, _end range
ratio = ((s-_start).length() + ratio * (e-s).length() )/(_end-_start).length();
TriangleIntersection& triHit = thitr->second;
LineSegmentIntersectorUtils::TriangleIntersection& triHit = thitr->second;
Intersection hit;
hit.ratio = ratio;

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;
Intersection hit;
hit.nodePath = iv.getNodePath();
hit.drawable = drawable;
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;
}
insertIntersection(hit);
}