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,6 +21,9 @@
using namespace osgUtil;
namespace LineSegmentIntersectorUtils
{
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):
@ -43,6 +46,7 @@ struct TriangleIntersection
const osg::Vec3* _v3;
};
typedef std::multimap<float,TriangleIntersection> TriangleIntersections;
struct TriangleIntersector
{
@ -54,17 +58,14 @@ struct TriangleIntersector
float _ratio;
bool _hit;
typedef std::multimap<float,TriangleIntersection> TriangleIntersections;
TriangleIntersections _intersections;
TriangleIntersector()
{
}
TriangleIntersector(const osg::Vec3d& start, osg::Vec3d& end, float ratio=FLT_MAX)
{
set(start,end,ratio);
_length = 0.0f;
_index = 0;
_ratio = 0.0f;
_hit = false;
}
void set(const osg::Vec3d& start, osg::Vec3d& end, float ratio=FLT_MAX)
@ -79,7 +80,6 @@ struct TriangleIntersector
_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;
@ -191,6 +191,7 @@ struct TriangleIntersector
};
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
@ -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;
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;
}
}