|
我用了几种方法分别求交,下面是代码,请各位大侠帮助指导一下问题所在
osg::ref_ptr<osg::Vec3Array> p_start=new osg::Vec3Array();
osg::ref_ptr<osg::Vec3Array> p_end=new osg::Vec3Array();
osg::ref_ptr<osg::Vec3Array> linePt=new osg::Vec3Array();
方法一 用LineSegmentIntersector求交
osg::Vec3 InterPt;
for (int i=0;i<linepoints.GetSize();i++)//linepoints为折线顶点数组
{
InterPt.set(linepoints[i].x-485339.216600,linepoints[i].y-4056762.280100,linepoints[i].H-500);
p_start->push_back(InterPt);
InterPt.set(linepoints[i].x-485339.216600,linepoints[i].y-4056762.280100,linepoints[i].H+500);
p_end->push_back(InterPt);
}
osg::Vec3 vec3_p1;
osgUtil::LineSegmentIntersector::Intersections _intersections;
for (int i=0;i<p_start->size();i++)
{
osg::ref_ptr< osgUtil::LineSegmentIntersector > _lineSegmentIntersector = new osgUtil::LineSegmentIntersector((*p_start)[i],(*p_end)[i]);
osgUtil::IntersectionVisitor _iv(_lineSegmentIntersector.get());
pView->mOSG->getViewer()->getSceneData()->accept(_iv);
_intersections=_lineSegmentIntersector->getIntersections();
int _intersectionNumber=_intersections.size();
if (_intersectionNumber!=0)
{
osgUtil::LineSegmentIntersector::Intersections::iterator hitr = _intersections.begin();
vec3_p1 =hitr->getWorldIntersectPoint();
testv->push_back(vec3_p1);
hitr++;
}
}
然后对testv数组画线,为甚么达不到预期的效果,如果直接将地点数组相连,那么线会穿过地形,所以想到了这种办法
我想问的是为甚与地形的交点为什么求出来的不对呢
方法二:PlaneIntersector面求交方法
osg::Vec3 position ;
position.set(QD.x-485339.216600,QD.y-4056762.280100,QD.z);
testv->push_back(position);
for (int i = 0; i < Row; i++)
{
position.set(BestGrid[i].x-485339.216600,BestGrid[i].y-4056762.280100,BestGrid[i].z);
testv->push_back(position);
}
position.set(ZD.x-485339.216600,ZD.y-4056762.280100,ZD.z);
testv->push_back(position);
for (int i=0;i<testv->size()-1;i++)
{
osgUtil::PlaneIntersector::Intersections intersections;
osg::Plane plane;
osg::Vec3d upVector (0.0,1.0, 0.0);
osg::Vec3 startPt = (*testv)[i];
osg::Vec3 endPt = (*testv)[i+1];
osg::Vec3f planeNormal = (endPt-startPt)^upVector;
planeNormal.normalize();
plane.set( planeNormal, startPt);
osg::Vec3f norm(endPt-startPt);
norm.normalize();
osg::Polytope polytope;
polytope.add( osg::Plane(norm,(*testv)[i]));
polytope.add( osg::Plane(-norm,(*testv)[i+1]));
osg::ref_ptr<osgUtil::PlaneIntersector>intersector = new osgUtil::PlaneIntersector(plane,polytope);
osgUtil::IntersectionVisitor iv(intersector.get());
pView->mOSG->getViewer()->getSceneData()->accept(iv);
if (intersector->containsIntersections())
{
osgUtil::PlaneIntersector::Intersections& intersections = intersector->getIntersections();
osgUtil::PlaneIntersector::Intersections::iterator itr;
double x1,y1,z1;
for (itr = intersections.begin() ; itr != intersections.end();++itr)
{
for( unsigned int j=0; j<(*itr).polyline.size(); ++j)
{
x1 = (*itr).polyline[j].x();
y1 = (*itr).polyline[j].y();
z1 = (*itr).polyline[j].z();
linepoint->push_back(osg::Vec3(x1,y1,z1));
}
}
}
}
然后对linepoint画线
1.约束面的加的是否有错,因为我这里是画折线,而不单单是一条直线,所以试了好多种方法都不能 解决约束面的问题
如果按照我上面的方式家约束面择画出来一小段直线 而且尚不能确定他的正确性
2.如果不家约束面 择画一大坨线,乱七八糟的线的集合,而且地形上下都有连线,所以请版主仔细讲一下我的约束面的问题,我实在找不到办法了 |
|