Added --points and --lines command line options that do a very simplistic conversion of geometry primitives to points or lines respectively, used to aid testing of intersectors

This commit is contained in:
Robert Osfield 2017-05-09 11:33:22 +01:00
parent 03f73d3aad
commit 6e1866ac18
5 changed files with 937 additions and 1441 deletions

View File

@ -408,6 +408,36 @@ protected:
}; };
class ConvertPrimitives : public osg::NodeVisitor
{
public:
osg::PrimitiveSet::Mode _mode;
ConvertPrimitives(osg::PrimitiveSet::Mode mode):
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_mode(mode) {}
void apply(osg::Geometry& geometry)
{
if (!geometry.getVertexArray()) return;
unsigned int numVertices = geometry.getVertexArray()->getNumElements();
if (_mode==osg::PrimitiveSet::POINTS)
{
// remove previous primitive sets.
geometry.removePrimitiveSet(0, geometry.getNumPrimitiveSets());
geometry.addPrimitiveSet(new osg::DrawArrays(_mode, 0,numVertices));
}
else if (_mode==osg::PrimitiveSet::LINES)
{
geometry.removePrimitiveSet(0, geometry.getNumPrimitiveSets());
geometry.addPrimitiveSet(new osg::DrawArrays(_mode, 0,numVertices));
}
}
};
int main( int argc, char **argv ) int main( int argc, char **argv )
{ {
osg::ArgumentParser arguments(&argc, argv); osg::ArgumentParser arguments(&argc, argv);
@ -433,6 +463,9 @@ int main( int argc, char **argv )
return 1; return 1;
} }
while(arguments.read("--points")) { ConvertPrimitives cp(osg::PrimitiveSet::POINTS); loadedModel->accept(cp); }
while(arguments.read("--lines")) { ConvertPrimitives cp(osg::PrimitiveSet::LINES); loadedModel->accept(cp); }
if (useKdTree) if (useKdTree)
{ {
OSG_NOTICE<<"Buildering KdTrees"<<std::endl; OSG_NOTICE<<"Buildering KdTrees"<<std::endl;
@ -440,6 +473,7 @@ int main( int argc, char **argv )
loadedModel->accept(*builder); loadedModel->accept(*builder);
} }
// assign the scene graph to viewer // assign the scene graph to viewer
viewer.setSceneData(loadedModel); viewer.setSceneData(loadedModel);

View File

@ -48,42 +48,65 @@ class OSG_EXPORT KdTree : public osg::Shape
* retun true on success. */ * retun true on success. */
virtual bool build(BuildOptions& buildOptions, osg::Geometry* geometry); virtual bool build(BuildOptions& buildOptions, osg::Geometry* geometry);
struct LineSegmentIntersection
void setVertices(osg::Vec3Array* vertices) { _vertices = vertices; }
const osg::Vec3Array* getVertices() const { return _vertices.get(); }
typedef std::vector< unsigned int > Indices;
// index in the VertexIndices vector
void setPrimitiveIndices(const Indices& indices) { _primitiveIndices = indices; }
Indices& getPrimitiveIndices() { return _primitiveIndices; }
const Indices& getPrimitiveIndices() const { return _primitiveIndices; }
// vector containing the primitive vertex index data packed as no_vertice_indices then vertex indices ie. for points it's (1, p0), for lines (2, p0, p1) etc.
void setVertexIndices(const Indices& indices) { _vertexIndices = indices; }
Indices& getVertexIndices() { return _vertexIndices; }
const Indices& getVertexIndices() const { return _vertexIndices; }
inline unsigned int addPoint(unsigned int p0)
{ {
LineSegmentIntersection(): unsigned int i = _vertexIndices.size();
ratio(-1.0), _primitiveIndices.push_back(i);
p0(0), _vertexIndices.push_back(1);
p1(0), _vertexIndices.push_back(p0);
p2(0), return i;
r0(0.0f), }
r1(0.0f), inline unsigned int addLine(unsigned int p0, unsigned int p1)
r2(0.0f), {
primitiveIndex(0) {} unsigned int i = _vertexIndices.size();
_primitiveIndices.push_back(i);
_vertexIndices.push_back(2);
_vertexIndices.push_back(p0);
_vertexIndices.push_back(p1);
return i;
}
bool operator < (const LineSegmentIntersection& rhs) const { return ratio < rhs.ratio; } inline unsigned int addTriangle(unsigned int p0, unsigned int p1, unsigned int p2)
{
unsigned int i = _vertexIndices.size();
_primitiveIndices.push_back(i);
_vertexIndices.push_back(3);
_vertexIndices.push_back(p0);
_vertexIndices.push_back(p1);
_vertexIndices.push_back(p2);
return i;
}
typedef std::vector<unsigned int> IndexList; inline unsigned int addQuad(unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3)
typedef std::vector<double> RatioList; {
unsigned int i = _vertexIndices.size();
_primitiveIndices.push_back(i);
_vertexIndices.push_back(4);
_vertexIndices.push_back(p0);
_vertexIndices.push_back(p1);
_vertexIndices.push_back(p2);
_vertexIndices.push_back(p3);
return i;
}
double ratio;
osg::Vec3d intersectionPoint;
osg::Vec3 intersectionNormal;
unsigned int p0;
unsigned int p1;
unsigned int p2;
float r0;
float r1;
float r2;
unsigned int primitiveIndex;
};
typedef std::vector<LineSegmentIntersection> LineSegmentIntersections;
/** compute the intersection of a line segment and the kdtree, return true if an intersection has been found.*/
virtual bool intersect(const osg::Vec3d& start, const osg::Vec3d& end, LineSegmentIntersections& intersections) const;
typedef int value_type; typedef int value_type;
@ -103,31 +126,7 @@ class OSG_EXPORT KdTree : public osg::Shape
value_type first; value_type first;
value_type second; value_type second;
}; };
struct Triangle
{
Triangle():
p0(0),p1(0),p2(0) {}
Triangle(unsigned int ip0, unsigned int ip1, unsigned int ip2):
p0(ip0), p1(ip1), p2(ip2) {}
bool operator < (const Triangle& rhs) const
{
if (p0<rhs.p0) return true;
if (p0>rhs.p0) return false;
if (p1<rhs.p1) return true;
if (p1>rhs.p1) return false;
return p2<rhs.p2;
}
unsigned int p0;
unsigned int p1;
unsigned int p2;
};
typedef std::vector< KdNode > KdNodeList; typedef std::vector< KdNode > KdNodeList;
typedef std::vector< Triangle > TriangleList;
int addNode(const KdNode& node) int addNode(const KdNode& node)
{ {
@ -142,21 +141,6 @@ class OSG_EXPORT KdTree : public osg::Shape
KdNodeList& getNodes() { return _kdNodes; } KdNodeList& getNodes() { return _kdNodes; }
const KdNodeList& getNodes() const { return _kdNodes; } const KdNodeList& getNodes() const { return _kdNodes; }
void setVertices(osg::Vec3Array* vertices) { _vertices = vertices; }
const osg::Vec3Array* getVertices() const { return _vertices.get(); }
unsigned int addTriangle(const Triangle& tri)
{
unsigned int num = static_cast<unsigned int>(_triangles.size());
_triangles.push_back(tri);
return num;
}
Triangle& getTriangle(unsigned int i) { return _triangles[i]; }
const Triangle& getTriangle(unsigned int i) const { return _triangles[i]; }
TriangleList& getTriangles() { return _triangles; }
const TriangleList& getTriangles() const { return _triangles; }
template<class IntersectFunctor> template<class IntersectFunctor>
void intersect(IntersectFunctor& functor, const KdNode& node) const void intersect(IntersectFunctor& functor, const KdNode& node) const
@ -169,11 +153,16 @@ class OSG_EXPORT KdTree : public osg::Shape
for(int i=istart; i<iend; ++i) for(int i=istart; i<iend; ++i)
{ {
const KdTree::Triangle& tri = _triangles[i]; unsigned int primitiveIndex = _primitiveIndices[i];
unsigned int numVertices = _vertexIndices[primitiveIndex++];
// OSG_NOTICE<<" tri("<<tri.p1<<","<<tri.p2<<","<<tri.p3<<")"<<std::endl; switch(numVertices)
{
functor.intersect(_vertices.get(), i, tri.p0, tri.p1, tri.p2); case(1): functor.intersect(_vertices.get(), i, _vertexIndices[primitiveIndex]); 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(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;
}
} }
} }
else if (functor.enter(node.bb)) else if (functor.enter(node.bb))
@ -189,10 +178,10 @@ class OSG_EXPORT KdTree : public osg::Shape
protected: protected:
osg::ref_ptr<osg::Vec3Array> _vertices; osg::ref_ptr<osg::Vec3Array> _vertices;
KdNodeList _kdNodes; Indices _primitiveIndices;
TriangleList _triangles; Indices _vertexIndices;
KdNodeList _kdNodes;
}; };
class OSG_EXPORT KdTreeBuilder : public osg::NodeVisitor class OSG_EXPORT KdTreeBuilder : public osg::NodeVisitor

View File

@ -14,6 +14,7 @@
#include <osg/KdTree> #include <osg/KdTree>
#include <osg/Geode> #include <osg/Geode>
#include <osg/TriangleIndexFunctor> #include <osg/TriangleIndexFunctor>
#include <osg/TemplatePrimitiveIndexFunctor>
#include <osg/Timer> #include <osg/Timer>
#include <osg/io_utils> #include <osg/io_utils>
@ -26,6 +27,7 @@ using namespace osg;
// //
// BuildKdTree Declarartion - class used for building an single KdTree // BuildKdTree Declarartion - class used for building an single KdTree
struct BuildKdTree struct BuildKdTree
{ {
BuildKdTree(KdTree& kdTree): BuildKdTree(KdTree& kdTree):
@ -53,19 +55,54 @@ protected:
BuildKdTree& operator = (const BuildKdTree&) { return *this; } BuildKdTree& operator = (const BuildKdTree&) { return *this; }
}; };
//////////////////////////////////////////////////////////////////////////////// struct PrimitiveIndicesCollector
//
// Functor for collecting triangle indices from Geometry
struct TriangleIndicesCollector
{ {
TriangleIndicesCollector(): PrimitiveIndicesCollector():
_buildKdTree(0) _buildKdTree(0)
{ {
} }
inline void operator () (unsigned int p0)
{
OSG_NOTICE<<" point ("<<p0<<")"<<std::endl;
const osg::Vec3& v0 = (*(_buildKdTree->_kdTree.getVertices()))[p0];
_buildKdTree->_kdTree.addPoint(p0);
osg::BoundingBox bb;
bb.expandBy(v0);
_buildKdTree->_primitiveIndices.push_back(_buildKdTree->_centers.size());
_buildKdTree->_centers.push_back(bb.center());
}
inline void operator () (unsigned int p0, unsigned int p1)
{
OSG_NOTICE<<" line ("<<p0<<", "<<p1<<")"<<std::endl;
const osg::Vec3& v0 = (*(_buildKdTree->_kdTree.getVertices()))[p0];
const osg::Vec3& v1 = (*(_buildKdTree->_kdTree.getVertices()))[p1];
// discard degenerate points
if (v0==v1)
{
//OSG_NOTICE<<"Disgarding degenerate triangle"<<std::endl;
return;
}
_buildKdTree->_kdTree.addLine(p0,p1);
osg::BoundingBox bb;
bb.expandBy(v0);
bb.expandBy(v1);
_buildKdTree->_primitiveIndices.push_back(_buildKdTree->_centers.size());
_buildKdTree->_centers.push_back(bb.center());
}
inline void operator () (unsigned int p0, unsigned int p1, unsigned int p2) inline void operator () (unsigned int p0, unsigned int p1, unsigned int p2)
{ {
// OSG_NOTICE<<" triangle ("<<p0<<", "<<p1<<", "<<p2<<")"<<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];
const osg::Vec3& v2 = (*(_buildKdTree->_kdTree.getVertices()))[p2]; const osg::Vec3& v2 = (*(_buildKdTree->_kdTree.getVertices()))[p2];
@ -77,16 +114,43 @@ struct TriangleIndicesCollector
return; return;
} }
unsigned int i = _buildKdTree->_kdTree.addTriangle(KdTree::Triangle(p0,p1,p2)); _buildKdTree->_kdTree.addTriangle(p0,p1,p2);
osg::BoundingBox bb; osg::BoundingBox bb;
bb.expandBy(v0); bb.expandBy(v0);
bb.expandBy(v1); bb.expandBy(v1);
bb.expandBy(v2); bb.expandBy(v2);
_buildKdTree->_primitiveIndices.push_back(_buildKdTree->_centers.size());
_buildKdTree->_centers.push_back(bb.center()); _buildKdTree->_centers.push_back(bb.center());
_buildKdTree->_primitiveIndices.push_back(i); }
inline void operator () (unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3)
{
OSG_NOTICE<<" quad ("<<p0<<", "<<p1<<", "<<p2<<", "<<p3<<")"<<std::endl;
const osg::Vec3& v0 = (*(_buildKdTree->_kdTree.getVertices()))[p0];
const osg::Vec3& v1 = (*(_buildKdTree->_kdTree.getVertices()))[p1];
const osg::Vec3& v2 = (*(_buildKdTree->_kdTree.getVertices()))[p2];
const osg::Vec3& v3 = (*(_buildKdTree->_kdTree.getVertices()))[p3];
// discard degenerate points
if (v0==v1 || v1==v2 || v2==v0 || v3==v0 || v3==v1 || v3==v2)
{
//OSG_NOTICE<<"Disgarding degenerate quad"<<std::endl;
return;
}
_buildKdTree->_kdTree.addQuad(p0,p1,p2,p3);
osg::BoundingBox bb;
bb.expandBy(v0);
bb.expandBy(v1);
bb.expandBy(v2);
bb.expandBy(v3);
_buildKdTree->_primitiveIndices.push_back(_buildKdTree->_centers.size());
_buildKdTree->_centers.push_back(bb.center());
} }
BuildKdTree* _buildKdTree; BuildKdTree* _buildKdTree;
@ -129,11 +193,9 @@ bool BuildKdTree::build(KdTree::BuildOptions& options, osg::Geometry* geometry)
_primitiveIndices.reserve(estimatedNumTriangles); _primitiveIndices.reserve(estimatedNumTriangles);
_centers.reserve(estimatedNumTriangles); _centers.reserve(estimatedNumTriangles);
_kdTree.getTriangles().reserve(estimatedNumTriangles); osg::TemplatePrimitiveIndexFunctor<PrimitiveIndicesCollector> collectIndices;
collectIndices._buildKdTree = this;
osg::TriangleIndexFunctor<TriangleIndicesCollector> collectTriangleIndices; geometry->accept(collectIndices);
collectTriangleIndices._buildKdTree = this;
geometry->accept(collectTriangleIndices);
_primitiveIndices.reserve(vertices->size()); _primitiveIndices.reserve(vertices->size());
@ -145,14 +207,18 @@ 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);
// now reorder the triangle list so that it's in order as per the primitiveIndex list. OSG_NOTICE<<"After KdTree setup"<<std::endl;
KdTree::TriangleList triangleList(_kdTree.getTriangles().size()); osg::KdTree::Indices& primitiveIndices = _kdTree.getPrimitiveIndices();
for(unsigned int i=0; i<_primitiveIndices.size(); ++i)
{
triangleList[i] = _kdTree.getTriangle(_primitiveIndices[i]);
}
_kdTree.getTriangles().swap(triangleList); KdTree::Indices new_indices;
new_indices.reserve(_primitiveIndices.size());
for(Indices::iterator itr = _primitiveIndices.begin();
itr != _primitiveIndices.end();
++itr)
{
new_indices.push_back(primitiveIndices[*itr]);
}
primitiveIndices.swap(new_indices);
#ifdef VERBOSE_OUTPUT #ifdef VERBOSE_OUTPUT
@ -217,18 +283,22 @@ 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)
{ {
const KdTree::Triangle& tri = _kdTree.getTriangle(_primitiveIndices[i]); unsigned int primitiveIndex = _kdTree.getPrimitiveIndices()[_primitiveIndices[i]];
const osg::Vec3& v0 = (*_kdTree.getVertices())[tri.p0]; unsigned int numPoints = _kdTree.getVertexIndices()[primitiveIndex++];
const osg::Vec3& v1 = (*_kdTree.getVertices())[tri.p1]; OSG_NOTICE<<" Primitive "<<primitiveIndex<<", numPoints="<<numPoints<<std::endl;
const osg::Vec3& v2 = (*_kdTree.getVertices())[tri.p2]; for(; numPoints>0; --numPoints)
node.bb.expandBy(v0); {
node.bb.expandBy(v1); unsigned int vi = _kdTree.getVertexIndices()[primitiveIndex++];
node.bb.expandBy(v2); const osg::Vec3& v = (*_kdTree.getVertices())[vi];
node.bb.expandBy(v);
OSG_NOTICE<<" vi="<<vi<<", v="<<v<<std::endl;
}
} }
if (node.bb.valid()) if (node.bb.valid())
@ -314,26 +384,6 @@ int BuildKdTree::divide(KdTree::BuildOptions& options, osg::BoundingBox& bb, int
KdTree::KdNode leftLeaf(-istart-1, (right-istart)+1); KdTree::KdNode leftLeaf(-istart-1, (right-istart)+1);
KdTree::KdNode rightLeaf(-left-1, (iend-left)+1); KdTree::KdNode rightLeaf(-left-1, (iend-left)+1);
#if 0
OSG_NOTICE<<"In node.first ="<<node.first <<" node.second ="<<node.second<<std::endl;
OSG_NOTICE<<" leftLeaf.first ="<<leftLeaf.first <<" leftLeaf.second ="<<leftLeaf.second<<std::endl;
OSG_NOTICE<<" rightLeaf.first="<<rightLeaf.first<<" rightLeaf.second="<<rightLeaf.second<<std::endl;
OSG_NOTICE<<" left="<<left<<" right="<<right<<std::endl;
if (node.second != (leftLeaf.second +rightLeaf.second))
{
OSG_NOTICE<<"*** Error in size, leaf.second="<<node.second
<<", leftLeaf.second="<<leftLeaf.second
<<", rightLeaf.second="<<rightLeaf.second<<std::endl;
}
else
{
OSG_NOTICE<<"Size OK, leaf.second="<<node.second
<<", leftLeaf.second="<<leftLeaf.second
<<", rightLeaf.second="<<rightLeaf.second<<std::endl;
}
#endif
if (leftLeaf.second<=0) if (leftLeaf.second<=0)
{ {
//OSG_NOTICE<<"LeftLeaf empty"<<std::endl; //OSG_NOTICE<<"LeftLeaf empty"<<std::endl;
@ -421,316 +471,6 @@ int BuildKdTree::divide(KdTree::BuildOptions& options, osg::BoundingBox& bb, int
} }
////////////////////////////////////////////////////////////////////////////////
//
// IntersectKdTree
//
struct IntersectKdTree
{
IntersectKdTree(const osg::Vec3Array& vertices,
const KdTree::KdNodeList& nodes,
const KdTree::TriangleList& triangles,
KdTree::LineSegmentIntersections& intersections,
const osg::Vec3d& s, const osg::Vec3d& e):
_vertices(vertices),
_kdNodes(nodes),
_triangles(triangles),
_intersections(intersections),
_s(s),
_e(e)
{
_d = e - s;
_length = _d.length();
_inverse_length = _length!=0.0f ? 1.0f/_length : 0.0;
_d *= _inverse_length;
_d_invX = _d.x()!=0.0f ? _d/_d.x() : osg::Vec3(0.0f,0.0f,0.0f);
_d_invY = _d.y()!=0.0f ? _d/_d.y() : osg::Vec3(0.0f,0.0f,0.0f);
_d_invZ = _d.z()!=0.0f ? _d/_d.z() : osg::Vec3(0.0f,0.0f,0.0f);
}
void intersect(const KdTree::KdNode& node, const osg::Vec3& s, const osg::Vec3& e) const;
bool intersectAndClip(osg::Vec3& s, osg::Vec3& e, const osg::BoundingBox& bb) const;
const osg::Vec3Array& _vertices;
const KdTree::KdNodeList& _kdNodes;
const KdTree::TriangleList& _triangles;
KdTree::LineSegmentIntersections& _intersections;
osg::Vec3 _s;
osg::Vec3 _e;
osg::Vec3 _d;
float _length;
float _inverse_length;
osg::Vec3 _d_invX;
osg::Vec3 _d_invY;
osg::Vec3 _d_invZ;
protected:
IntersectKdTree& operator = (const IntersectKdTree&) { return *this; }
};
void IntersectKdTree::intersect(const KdTree::KdNode& node, const osg::Vec3& ls, const osg::Vec3& le) const
{
if (node.first<0)
{
// treat as a leaf
//OSG_NOTICE<<"KdTree::intersect("<<&leaf<<")"<<std::endl;
int istart = -node.first-1;
int iend = istart + node.second;
for(int i=istart; i<iend; ++i)
{
//const Triangle& tri = _triangles[_primitiveIndices[i]];
const KdTree::Triangle& tri = _triangles[i];
// OSG_NOTICE<<" tri("<<tri.p1<<","<<tri.p2<<","<<tri.p3<<")"<<std::endl;
const osg::Vec3& v0 = _vertices[tri.p0];
const osg::Vec3& v1 = _vertices[tri.p1];
const osg::Vec3& v2 = _vertices[tri.p2];
osg::Vec3 T = _s - v0;
osg::Vec3 E2 = v2 - v0;
osg::Vec3 E1 = v1 - v0;
osg::Vec3 P = _d ^ E2;
float det = P * E1;
float r,r0,r1,r2;
const float esplison = 1e-10f;
if (det>esplison)
{
float u = (P*T);
if (u<0.0 || u>det) continue;
osg::Vec3 Q = T ^ E1;
float v = (Q*_d);
if (v<0.0 || v>det) continue;
if ((u+v)> det) continue;
float inv_det = 1.0f/det;
float t = (Q*E2)*inv_det;
if (t<0.0 || t>_length) continue;
u *= inv_det;
v *= inv_det;
r0 = 1.0f-u-v;
r1 = u;
r2 = v;
r = t * _inverse_length;
}
else if (det<-esplison)
{
float u = (P*T);
if (u>0.0 || u<det) continue;
osg::Vec3 Q = T ^ E1;
float v = (Q*_d);
if (v>0.0 || v<det) continue;
if ((u+v) < det) continue;
float inv_det = 1.0f/det;
float t = (Q*E2)*inv_det;
if (t<0.0 || t>_length) continue;
u *= inv_det;
v *= inv_det;
r0 = 1.0f-u-v;
r1 = u;
r2 = v;
r = t * _inverse_length;
}
else
{
continue;
}
osg::Vec3 in = v0*r0 + v1*r1 + v2*r2;
osg::Vec3 normal = E1^E2;
normal.normalize();
#if 1
_intersections.push_back(KdTree::LineSegmentIntersection());
KdTree::LineSegmentIntersection& intersection = _intersections.back();
intersection.ratio = r;
intersection.primitiveIndex = i;
intersection.intersectionPoint = in;
intersection.intersectionNormal = normal;
intersection.p0 = tri.p0;
intersection.p1 = tri.p1;
intersection.p2 = tri.p2;
intersection.r0 = r0;
intersection.r1 = r1;
intersection.r2 = r2;
#endif
// OSG_NOTICE<<" got intersection ("<<in<<") ratio="<<r<<std::endl;
}
}
else
{
if (node.first>0)
{
osg::Vec3 l(ls), e(le);
if (intersectAndClip(l,e, _kdNodes[node.first].bb))
{
intersect(_kdNodes[node.first], l, e);
}
}
if (node.second>0)
{
osg::Vec3 l(ls), e(le);
if (intersectAndClip(l,e, _kdNodes[node.second].bb))
{
intersect(_kdNodes[node.second], l, e);
}
}
}
}
bool IntersectKdTree::intersectAndClip(osg::Vec3& s, osg::Vec3& e, const osg::BoundingBox& bb) const
{
//return true;
//if (!bb.valid()) return true;
// compate s and e against the xMin to xMax range of bb.
if (s.x()<=e.x())
{
// trivial reject of segment wholely outside.
if (e.x()<bb.xMin()) return false;
if (s.x()>bb.xMax()) return false;
if (s.x()<bb.xMin())
{
// clip s to xMin.
s = s+_d_invX*(bb.xMin()-s.x());
}
if (e.x()>bb.xMax())
{
// clip e to xMax.
e = s+_d_invX*(bb.xMax()-s.x());
}
}
else
{
if (s.x()<bb.xMin()) return false;
if (e.x()>bb.xMax()) return false;
if (e.x()<bb.xMin())
{
// clip s to xMin.
e = s+_d_invX*(bb.xMin()-s.x());
}
if (s.x()>bb.xMax())
{
// clip e to xMax.
s = s+_d_invX*(bb.xMax()-s.x());
}
}
// compate s and e against the yMin to yMax range of bb.
if (s.y()<=e.y())
{
// trivial reject of segment wholely outside.
if (e.y()<bb.yMin()) return false;
if (s.y()>bb.yMax()) return false;
if (s.y()<bb.yMin())
{
// clip s to yMin.
s = s+_d_invY*(bb.yMin()-s.y());
}
if (e.y()>bb.yMax())
{
// clip e to yMax.
e = s+_d_invY*(bb.yMax()-s.y());
}
}
else
{
if (s.y()<bb.yMin()) return false;
if (e.y()>bb.yMax()) return false;
if (e.y()<bb.yMin())
{
// clip s to yMin.
e = s+_d_invY*(bb.yMin()-s.y());
}
if (s.y()>bb.yMax())
{
// clip e to yMax.
s = s+_d_invY*(bb.yMax()-s.y());
}
}
// compate s and e against the zMin to zMax range of bb.
if (s.z()<=e.z())
{
// trivial reject of segment wholely outside.
if (e.z()<bb.zMin()) return false;
if (s.z()>bb.zMax()) return false;
if (s.z()<bb.zMin())
{
// clip s to zMin.
s = s+_d_invZ*(bb.zMin()-s.z());
}
if (e.z()>bb.zMax())
{
// clip e to zMax.
e = s+_d_invZ*(bb.zMax()-s.z());
}
}
else
{
if (s.z()<bb.zMin()) return false;
if (e.z()>bb.zMax()) return false;
if (e.z()<bb.zMin())
{
// clip s to zMin.
e = s+_d_invZ*(bb.zMin()-s.z());
}
if (s.z()>bb.zMax())
{
// clip e to zMax.
s = s+_d_invZ*(bb.zMax()-s.z());
}
}
// OSG_NOTICE<<"clampped segment "<<s<<" "<<e<<std::endl;
// if (s==e) return false;
return true;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// //
// KdTree::BuildOptions // KdTree::BuildOptions
@ -753,8 +493,7 @@ KdTree::KdTree()
KdTree::KdTree(const KdTree& rhs, const osg::CopyOp& copyop): KdTree::KdTree(const KdTree& rhs, const osg::CopyOp& copyop):
Shape(rhs, copyop), Shape(rhs, copyop),
_vertices(rhs._vertices), _vertices(rhs._vertices),
_kdNodes(rhs._kdNodes), _kdNodes(rhs._kdNodes)
_triangles(rhs._triangles)
{ {
} }
@ -764,27 +503,6 @@ bool KdTree::build(BuildOptions& options, osg::Geometry* geometry)
return build.build(options, geometry); return build.build(options, geometry);
} }
bool KdTree::intersect(const osg::Vec3d& start, const osg::Vec3d& end, LineSegmentIntersections& intersections) const
{
if (_kdNodes.empty())
{
OSG_NOTICE<<"Warning: _kdTree is empty"<<std::endl;
return false;
}
unsigned int numIntersectionsBefore = intersections.size();
IntersectKdTree intersector(*_vertices,
_kdNodes,
_triangles,
intersections,
start, end);
intersector.intersect(getNode(0), start, end);
return numIntersectionsBefore != intersections.size();
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// //
// KdTreeBuilder // KdTreeBuilder

View File

@ -21,187 +21,393 @@
#include <osg/KdTree> #include <osg/KdTree>
#include <osg/Timer> #include <osg/Timer>
#include <osg/TexMat> #include <osg/TexMat>
#include <osg/TemplatePrimitiveFunctor>
using namespace osgUtil; using namespace osgUtil;
namespace LineSegmentIntersectorUtils namespace LineSegmentIntersectorUtils
{ {
struct TriangleIntersection
struct Settings : public osg::Referenced
{
Settings() :
_lineSegIntersector(0),
_iv(0),
_drawable(0),
_limitOneIntersection(false) {}
osgUtil::LineSegmentIntersector* _lineSegIntersector;
osgUtil::IntersectionVisitor* _iv;
osg::Drawable* _drawable;
osg::ref_ptr<osg::Vec3Array> _vertices;
bool _limitOneIntersection;
};
template<typename Vec3, typename value_type>
struct IntersectFunctor
{
osg::ref_ptr<Settings> _settings;
unsigned int _primitiveIndex;
Vec3 _start;
Vec3 _end;
typedef std::pair< Vec3, Vec3> StartEnd;
typedef std::vector< StartEnd > StartEndStack;
StartEndStack _startEndStack;
Vec3 _d;
value_type _length;
value_type _inverse_length;
Vec3 _d_invX;
Vec3 _d_invY;
Vec3 _d_invZ;
bool _hit;
IntersectFunctor():
_primitiveIndex(0),
_hit(false)
{ {
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;
protected:
TriangleIntersection& operator = (const TriangleIntersection&) { return *this; }
};
typedef std::multimap<float,TriangleIntersection> TriangleIntersections;
template<typename Vec3, typename value_type> void set(const osg::Vec3d& s, const osg::Vec3d& e, Settings* settings)
struct TriangleIntersector
{ {
Vec3 _s; _settings = settings;
Vec3 _d;
value_type _length;
int _index; _start = s;
value_type _ratio; _end = e;
bool _hit;
bool _limitOneIntersection;
TriangleIntersections* _intersections;
TriangleIntersector() _startEndStack.push_back(StartEnd(_start,_end));
_d = e - s;
_length = _d.length();
_inverse_length = (_length!=0.0) ? 1.0/_length : 0.0;
_d *= _inverse_length;
_d_invX = _d.x()!=0.0 ? _d/_d.x() : Vec3(0.0,0.0,0.0);
_d_invY = _d.y()!=0.0 ? _d/_d.y() : Vec3(0.0,0.0,0.0);
_d_invZ = _d.z()!=0.0 ? _d/_d.z() : Vec3(0.0,0.0,0.0);
}
bool enter(const osg::BoundingBox& bb)
{
StartEnd startend = _startEndStack.back();
Vec3& s = startend.first;
Vec3& e = startend.second;
//return true;
//if (!bb.valid()) return true;
// compare s and e against the xMin to xMax range of bb.
if (s.x()<=e.x())
{ {
_intersections = 0;
_length = 0.0f; // trivial reject of segment wholely outside.
_index = 0; if (e.x()<bb.xMin()) return false;
_ratio = 0.0f; if (s.x()>bb.xMax()) return false;
_hit = false;
_limitOneIntersection = false; if (s.x()<bb.xMin())
{
// clip s to xMin.
s = s+_d_invX*(bb.xMin()-s.x());
}
if (e.x()>bb.xMax())
{
// clip e to xMax.
e = s+_d_invX*(bb.xMax()-s.x());
}
}
else
{
if (s.x()<bb.xMin()) return false;
if (e.x()>bb.xMax()) return false;
if (e.x()<bb.xMin())
{
// clip s to xMin.
e = s+_d_invX*(bb.xMin()-s.x());
}
if (s.x()>bb.xMax())
{
// clip e to xMax.
s = s+_d_invX*(bb.xMax()-s.x());
}
} }
void set(TriangleIntersections* intersections) // compate s and e against the yMin to yMax range of bb.
if (s.y()<=e.y())
{ {
_intersections = intersections;
// trivial reject of segment wholely outside.
if (e.y()<bb.yMin()) return false;
if (s.y()>bb.yMax()) return false;
if (s.y()<bb.yMin())
{
// clip s to yMin.
s = s+_d_invY*(bb.yMin()-s.y());
}
if (e.y()>bb.yMax())
{
// clip e to yMax.
e = s+_d_invY*(bb.yMax()-s.y());
}
}
else
{
if (s.y()<bb.yMin()) return false;
if (e.y()>bb.yMax()) return false;
if (e.y()<bb.yMin())
{
// clip s to yMin.
e = s+_d_invY*(bb.yMin()-s.y());
}
if (s.y()>bb.yMax())
{
// clip e to yMax.
s = s+_d_invY*(bb.yMax()-s.y());
}
} }
void set(const osg::Vec3d& start, const osg::Vec3d& end, value_type ratio=FLT_MAX) // compate s and e against the zMin to zMax range of bb.
if (s.z()<=e.z())
{ {
_hit=false;
_index = 0;
_ratio = ratio;
_s = start; // trivial reject of segment wholely outside.
_d = end - start; if (e.z()<bb.zMin()) return false;
_length = _d.length(); if (s.z()>bb.zMax()) return false;
_d /= _length;
if (s.z()<bb.zMin())
{
// clip s to zMin.
s = s+_d_invZ*(bb.zMin()-s.z());
}
if (e.z()>bb.zMax())
{
// clip e to zMax.
e = s+_d_invZ*(bb.zMax()-s.z());
}
}
else
{
if (s.z()<bb.zMin()) return false;
if (e.z()>bb.zMax()) return false;
if (e.z()<bb.zMin())
{
// clip s to zMin.
e = s+_d_invZ*(bb.zMin()-s.z());
}
if (s.z()>bb.zMax())
{
// clip e to zMax.
s = s+_d_invZ*(bb.zMax()-s.z());
}
} }
inline void operator () (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3) // OSG_NOTICE<<"clampped segment "<<s<<" "<<e<<std::endl;
_startEndStack.push_back(startend);
return true;
}
void leave()
{
// OSG_NOTICE<<"leave() "<<_startEndStack.size()<<std::endl;
_startEndStack.pop_back();
}
void intersect(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2)
{
if (_settings->_limitOneIntersection && _hit) return;
// OSG_NOTICE<<" intersect(v0=("<<v0<<"), v1=("<<v1<<"), v2=("<<v2<<") )"<<std::endl;
// const StartEnd startend = _startEndStack.back();
// const osg::Vec3& ls = startend.first;
// const osg::Vec3& le = startend.second;
Vec3 T = _start - v0;
Vec3 E2 = v2 - v0;
Vec3 E1 = v1 - v0;
Vec3 P = _d ^ E2;
value_type det = P * E1;
value_type r,r0,r1,r2;
const value_type esplison = 1e-10;
if (det>esplison)
{ {
++_index; value_type u = (P*T);
if (u<0.0 || u>det) return;
if (_limitOneIntersection && _hit) return; osg::Vec3 Q = T ^ E1;
value_type v = (Q*_d);
if (v<0.0 || v>det) return;
if (v1==v2 || v2==v3 || v1==v3) return; if ((u+v)> det) return;
Vec3 v12 = v2-v1; value_type inv_det = 1.0/det;
Vec3 n12 = v12^_d; value_type t = (Q*E2)*inv_det;
value_type ds12 = (_s-v1)*n12; if (t<0.0 || t>_length) return;
value_type 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;
}
Vec3 v23 = v3-v2; u *= inv_det;
Vec3 n23 = v23^_d; v *= inv_det;
value_type ds23 = (_s-v2)*n23;
value_type 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;
}
Vec3 v31 = v1-v3; r0 = 1.0-u-v;
Vec3 n31 = v31^_d; r1 = u;
value_type ds31 = (_s-v3)*n31; r2 = v;
value_type d231 = (v2-v3)*n31; r = t * _inverse_length;
if (d231>=0.0f) }
{ else if (det<-esplison)
if (ds31<0.0f) return; {
if (ds31>d231) return; value_type u = (P*T);
} if (u>0.0 || u<det) return;
else // d231 < 0
{
if (ds31>0.0f) return;
if (ds31<d231) return;
}
Vec3 Q = T ^ E1;
value_type v = (Q*_d);
if (v>0.0 || v<det) return;
value_type r3; if ((u+v) < det) return;
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.
value_type r1; value_type inv_det = 1.0/det;
if (ds23==0.0f) r1=0.0f; value_type t = (Q*E2)*inv_det;
else if (d123!=0.0f) r1 = ds23/d123; if (t<0.0 || t>_length) return;
else return; // the triangle and the line must be parallel intersection.
value_type r2; u *= inv_det;
if (ds31==0.0f) r2=0.0f; v *= inv_det;
else if (d231!=0.0f) r2 = ds31/d231;
else return; // the triangle and the line must be parallel intersection.
value_type 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.
value_type inv_total_r = 1.0f/total_r;
r1 *= inv_total_r;
r2 *= inv_total_r;
r3 *= inv_total_r;
}
Vec3 in = v1*r1+v2*r2+v3*r3;
if (!in.valid())
{
OSG_WARN<<"Warning:: Picked up error in TriangleIntersect"<<std::endl;
OSG_WARN<<" ("<<v1<<",\t"<<v2<<",\t"<<v3<<")"<<std::endl;
OSG_WARN<<" ("<<r1<<",\t"<<r2<<",\t"<<r3<<")"<<std::endl;
return;
}
value_type d = (in-_s)*_d;
if (d<0.0f) return;
if (d>_length) return;
Vec3 normal = v12^v23;
normal.normalize();
value_type r = d/_length;
_intersections->insert(std::pair<const float,TriangleIntersection>(r,TriangleIntersection(_index-1,normal,r1,&v1,r2,&v2,r3,&v3)));
_hit = true;
r0 = 1.0-u-v;
r1 = u;
r2 = v;
r = t * _inverse_length;
}
else
{
return;
} }
}; Vec3 in = v0*r0 + v1*r1 + v2*r2;
Vec3 normal = E1^E2;
normal.normalize();
}
LineSegmentIntersector::Intersection hit;
hit.ratio = r;
hit.matrix = _settings->_iv->getModelMatrix();
hit.nodePath = _settings->_iv->getNodePath();
hit.drawable = _settings->_drawable;
hit.primitiveIndex = _primitiveIndex;
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;
if (_settings->_vertices.valid())
{
const osg::Vec3* first = &(_settings->_vertices->front());
hit.indexList.reserve(3);
hit.ratioList.reserve(3);
if (r0!=0.0f)
{
hit.indexList.push_back(&v0-first);
hit.ratioList.push_back(r0);
}
if (r1!=0.0f)
{
hit.indexList.push_back(&v1-first);
hit.ratioList.push_back(r1);
}
if (r2!=0.0f)
{
hit.indexList.push_back(&v2-first);
hit.ratioList.push_back(r2);
}
}
_settings->_lineSegIntersector->insertIntersection(hit);
_hit = true;
}
// handle lines
void operator()(const osg::Vec3&, bool /*treatVertexDataAsTemporary*/)
{
++_primitiveIndex;
}
void operator()(const osg::Vec3&, const osg::Vec3&, bool /*treatVertexDataAsTemporary*/)
{
++_primitiveIndex;
}
// handle triangles
void operator()(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2, bool /*treatVertexDataAsTemporary*/)
{
++_primitiveIndex;
intersect(v0,v1,v2);
}
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(v1,v2,v3);
}
bool intersect(const osg::Vec3Array*, int , unsigned int)
{
return false;
}
bool 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)
{
if (_settings->_limitOneIntersection && _hit) return false;
_primitiveIndex = primitiveIndex;
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)
{
if (_settings->_limitOneIntersection && _hit) return false;
_primitiveIndex = primitiveIndex;
intersect((*vertices)[p0], (*vertices)[p1], (*vertices)[p3]);
intersect((*vertices)[p1], (*vertices)[p2], (*vertices)[p3]);
return false;
}
};
} // namespace LineSegmentIntersectorUtils
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
@ -315,157 +521,37 @@ void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Dr
void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable, void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable,
const osg::Vec3d& s, const osg::Vec3d& e) const osg::Vec3d& s, const osg::Vec3d& e)
{ {
osg::KdTree* kdTree = iv.getUseKdTreeWhenAvailable() ? dynamic_cast<osg::KdTree*>(drawable->getShape()) : 0; if (reachedLimit()) return;
if (kdTree)
osg::ref_ptr<LineSegmentIntersectorUtils::Settings> settings = new LineSegmentIntersectorUtils::Settings;
settings->_lineSegIntersector = this;
settings->_iv = &iv;
settings->_drawable = drawable;
settings->_limitOneIntersection = (_intersectionLimit == LIMIT_ONE_PER_DRAWABLE || _intersectionLimit == LIMIT_ONE);
osg::Geometry* geometry = drawable->asGeometry();
if (geometry)
{ {
osg::KdTree::LineSegmentIntersections intersections; settings->_vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
intersections.reserve(4);
if (kdTree->intersect(s,e,intersections))
{
// OSG_NOTICE<<"Got KdTree intersections"<<std::endl;
for(osg::KdTree::LineSegmentIntersections::iterator itr = intersections.begin();
itr != intersections.end();
++itr)
{
osg::KdTree::LineSegmentIntersection& lsi = *(itr);
// get ratio in s,e range
double ratio = lsi.ratio;
// remap ratio into _start, _end range
double remap_ratio = ((s-_start).length() + ratio * (e-s).length() )/(_end-_start).length();
Intersection hit;
hit.ratio = remap_ratio;
hit.matrix = iv.getModelMatrix();
hit.nodePath = iv.getNodePath();
hit.drawable = drawable;
hit.primitiveIndex = lsi.primitiveIndex;
hit.localIntersectionPoint = _start*(1.0-remap_ratio) + _end*remap_ratio;
// OSG_NOTICE<<"KdTree: ratio="<<hit.ratio<<" ("<<hit.localIntersectionPoint<<")"<<std::endl;
hit.localIntersectionNormal = lsi.intersectionNormal;
hit.indexList.reserve(3);
hit.ratioList.reserve(3);
if (lsi.r0!=0.0f)
{
hit.indexList.push_back(lsi.p0);
hit.ratioList.push_back(lsi.r0);
}
if (lsi.r1!=0.0f)
{
hit.indexList.push_back(lsi.p1);
hit.ratioList.push_back(lsi.r1);
}
if (lsi.r2!=0.0f)
{
hit.indexList.push_back(lsi.p2);
hit.ratioList.push_back(lsi.r2);
}
insertIntersection(hit);
}
}
return;
} }
LineSegmentIntersectorUtils::TriangleIntersections intersections; osg::KdTree* kdTree = iv.getUseKdTreeWhenAvailable() ? dynamic_cast<osg::KdTree*>(drawable->getShape()) : 0;
if (getPrecisionHint()==USE_DOUBLE_CALCULATIONS) if (getPrecisionHint()==USE_DOUBLE_CALCULATIONS)
{ {
OSG_INFO<<"Using double intersections"<<std::endl; osg::TemplatePrimitiveFunctor<LineSegmentIntersectorUtils::IntersectFunctor<osg::Vec3d, double> > intersector;
typedef LineSegmentIntersectorUtils::TriangleIntersector<osg::Vec3d, osg::Vec3d::value_type> TriangleIntersector; intersector.set(s,e, settings.get());
osg::TriangleFunctor< TriangleIntersector > ti;
ti.set(&intersections); if (kdTree) kdTree->intersect(intersector, kdTree->getNode(0));
ti.set(s,e); else drawable->accept(intersector);
ti._limitOneIntersection = (_intersectionLimit == LIMIT_ONE_PER_DRAWABLE || _intersectionLimit == LIMIT_ONE);
drawable->accept(ti);
} }
else else
{ {
OSG_INFO<<"Using float intersections"<<std::endl; osg::TemplatePrimitiveFunctor<LineSegmentIntersectorUtils::IntersectFunctor<osg::Vec3f, float> > intersector;
typedef LineSegmentIntersectorUtils::TriangleIntersector<osg::Vec3f, osg::Vec3f::value_type> TriangleIntersector; intersector.set(s,e, settings.get());
osg::TriangleFunctor< TriangleIntersector > ti;
ti.set(&intersections); if (kdTree) kdTree->intersect(intersector, kdTree->getNode(0));
ti.set(s,e); else drawable->accept(intersector);
ti._limitOneIntersection = (_intersectionLimit == LIMIT_ONE_PER_DRAWABLE || _intersectionLimit == LIMIT_ONE);
drawable->accept(ti);
}
if (!intersections.empty())
{
osg::Geometry* geometry = drawable->asGeometry();
for(LineSegmentIntersectorUtils::TriangleIntersections::iterator thitr = intersections.begin();
thitr != intersections.end();
++thitr)
{
// get ratio in s,e range
double ratio = thitr->first;
// remap ratio into _start, _end range
double remap_ratio = ((s-_start).length() + ratio * (e-s).length() )/(_end-_start).length();
if ( _intersectionLimit == LIMIT_NEAREST && !getIntersections().empty() )
{
if (remap_ratio >= getIntersections().begin()->ratio )
break;
else
getIntersections().clear();
}
LineSegmentIntersectorUtils::TriangleIntersection& triHit = thitr->second;
Intersection hit;
hit.ratio = remap_ratio;
hit.matrix = iv.getModelMatrix();
hit.nodePath = iv.getNodePath();
hit.drawable = drawable;
hit.primitiveIndex = triHit._index;
hit.localIntersectionPoint = _start*(1.0-remap_ratio) + _end*remap_ratio;
// OSG_NOTICE<<"Conventional: ratio="<<hit.ratio<<" ("<<hit.localIntersectionPoint<<")"<<std::endl;
hit.localIntersectionNormal = triHit._normal;
if (geometry)
{
osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
if (vertices)
{
osg::Vec3* first = &(vertices->front());
if (triHit._v1)
{
hit.indexList.push_back(triHit._v1-first);
hit.ratioList.push_back(triHit._r1);
}
if (triHit._v2)
{
hit.indexList.push_back(triHit._v2-first);
hit.ratioList.push_back(triHit._r2);
}
if (triHit._v3)
{
hit.indexList.push_back(triHit._v3-first);
hit.ratioList.push_back(triHit._r3);
}
}
}
insertIntersection(hit);
}
} }
} }

File diff suppressed because it is too large Load Diff