diff --git a/src/osgSim/SphereSegment.cpp b/src/osgSim/SphereSegment.cpp index f3defa746..5d717deef 100644 --- a/src/osgSim/SphereSegment.cpp +++ b/src/osgSim/SphereSegment.cpp @@ -1217,6 +1217,8 @@ struct TriangleIntersectOperator TriangleList _toTraverse; }; + typedef std::list< osg::ref_ptr > EdgeList; + struct Triangle : public osg::Referenced { @@ -1276,6 +1278,39 @@ struct TriangleIntersectOperator Edge* _e2; Edge* _e3; }; + + struct Polygon + { + Polygon(Triangle* tri) + { + if (tri->_e1) _edges.push_back(tri->_e1); + if (tri->_e2) _edges.push_back(tri->_e2); + if (tri->_e3) _edges.push_back(tri->_e3); + } + + template + unsigned int computeIntersections(I intersector) + { + // collect all the intersecting edges + EdgeList hitEdges; + for(EdgeList::iterator itr = _edges.begin(); + itr != _edges.end(); + ++itr) + { + Edge* edge = const_cast(itr->get()); + if (intersector(edge)) + { + hitEdges.push_back(edge); + } + } + osg::notify(osg::NOTICE)<<"Got intersection with edges "< IndexArray; typedef std::vector< osg::ref_ptr > TriangleArray; typedef std::set< osg::ref_ptr, dereference_less > EdgeSet; - typedef std::list< osg::ref_ptr > EdgeList; VertexArray _originalVertices; RegionArray _regions; @@ -1609,6 +1643,13 @@ struct TriangleIntersectOperator osg::notify(osg::NOTICE)<<"Removed duplicate triangles : num triangles out "<<_triangles.size()<_e1 = addEdge(tri->_p1, tri->_p2, tri); + tri->_e2 = addEdge(tri->_p2, tri->_p3, tri); + tri->_e3 = addEdge(tri->_p1, tri->_p3, tri); + } + void buildEdges() { _edges.clear(); @@ -1625,14 +1666,10 @@ struct TriangleIntersectOperator int numIntersections = rc.numberOfIntersectingSurfaces(); if (numIntersections==1) { - tri->_e1 = addEdge(tri->_p1, tri->_p2, tri); - tri->_e2 = addEdge(tri->_p2, tri->_p3, tri); - tri->_e3 = addEdge(tri->_p1, tri->_p3, tri); + buildEdges(tri); } #else - tri->_e1 = addEdge(tri->_p1, tri->_p2, tri); - tri->_e2 = addEdge(tri->_p2, tri->_p3, tri); - tri->_e3 = addEdge(tri->_p1, tri->_p3, tri); + buildEdges(tri); #endif } osg::notify(osg::NOTICE)<<"Number of edges "<<_edges.size()<get(); - RegionCounter rc; - rc.add(_regions[tri->_p1]); - rc.add(_regions[tri->_p2]); - rc.add(_regions[tri->_p3]); - int numIntersections = rc.numberOfIntersectingSurfaces(); - if (numIntersections==0) ++numZero; - else if (numIntersections==1) ++numOne; - else if (numIntersections==2) ++numTwo; - else if (numIntersections>=3) ++numMore; - - if (numIntersections>=2) - { - osg::Vec3Array* newLine = new osg::Vec3Array; - newLine->push_back(_originalVertices[tri->_p1]+_centre); - newLine->push_back(_originalVertices[tri->_p2]+_centre); - newLine->push_back(_originalVertices[tri->_p3]+_centre); - newLine->push_back(_originalVertices[tri->_p1]+_centre); - _generatedLines.push_back(newLine); - } - - } - osg::notify(osg::NOTICE)<<" numZero "<_intersectionType = TriangleIntersectOperator::Edge::MID_POINT; + edge->_intersectionVertex = v1*one_minus_r + v2*r; + } + + return true; + } +}; + +struct AzimPlaneIntersector +{ + AzimPlaneIntersector(TriangleIntersectOperator& tif, double azim): + _tif(tif) + { + _plane.set(cos(azim),-sin(azim),0.0,0.0); + } + + TriangleIntersectOperator& _tif; + osg::Plane _plane; + + inline bool operator() (TriangleIntersectOperator::Edge* edge) + { + edge->_intersectionType = TriangleIntersectOperator::Edge::NO_INTERSECTION; + + osg::Vec3& v1 = _tif._originalVertices[edge->_p1]; + osg::Vec3& v2 = _tif._originalVertices[edge->_p2]; + + double d1 = _plane.distance(v1); + double d2 = _plane.distance(v2); + + // if both points inside then disgard + if (d1<0.0 && d2<0.0) return false; + + // if both points outside then disgard + if (d1>0.0 && d2>0.0) return false; + + if (d1==0.0) + { + if (d2==0.0) + { + edge->_intersectionType = TriangleIntersectOperator::Edge::BOTH_ENDS; + } + else + { + edge->_intersectionType = TriangleIntersectOperator::Edge::POINT_1; + } + } + else if (d2==0.0) + { + edge->_intersectionType = TriangleIntersectOperator::Edge::POINT_2; + } + else + { + + double div = d2-d1; + if (div==0.0) + { + edge->_intersectionType = TriangleIntersectOperator::Edge::NO_INTERSECTION; + return false; + } + + double r = -d1 / div; + if (r<0.0 || r>1.0) + { + edge->_intersectionType = TriangleIntersectOperator::Edge::NO_INTERSECTION; + return false; + } + + osg::notify(osg::NOTICE)<<"r = "<_intersectionType = TriangleIntersectOperator::Edge::MID_POINT; edge->_intersectionVertex = v1*one_minus_r + v2*r; @@ -2131,6 +2201,73 @@ struct RadiusIntersector }; +void TriangleIntersectOperator::countMultipleIntersections() +{ + osg::notify(osg::NOTICE)<<"countMultipleIntersections("<get(); + RegionCounter rc; + rc.add(_regions[tri->_p1]); + rc.add(_regions[tri->_p2]); + rc.add(_regions[tri->_p3]); + int numIntersections = rc.numberOfIntersectingSurfaces(); + if (numIntersections==0) ++numZero; + else if (numIntersections==1) ++numOne; + else if (numIntersections==2) ++numTwo; + else if (numIntersections>=3) ++numMore; + + if (numIntersections>=2) + { + buildEdges(tri); + + osg::notify(osg::NOTICE)<<"Testing Polygon with numIntersections = "<0 && !singleIntersectFound) + { + osg::Vec3Array* newLine = new osg::Vec3Array; + newLine->push_back(_originalVertices[tri->_p1]+_centre); + newLine->push_back(_originalVertices[tri->_p2]+_centre); + newLine->push_back(_originalVertices[tri->_p3]+_centre); + newLine->push_back(_originalVertices[tri->_p1]+_centre); + _generatedLines.push_back(newLine); + } + } + + } + osg::notify(osg::NOTICE)<<" numZero "<