|
楼主 |
发表于 2012-11-21 14:34:10
|
显示全部楼层
gis_wudi 发表于 2012-11-20 13:41
这个问题最好的解决方式就是跟代码
- void getIntersections( osg::Vec3dArray *buttomPointList,osg::Group* sphereGroup, osg::Node* model, osg::Vec3d p_start,osg::Vec3d p_end)
- {
- osg::ref_ptr< osgUtil::LineSegmentIntersector > _lineSegmentIntersector = new osgUtil::LineSegmentIntersector(p_start,p_end);
- osgUtil::IntersectionVisitor _iv(_lineSegmentIntersector.get());
- model->accept(_iv);
- osgUtil::LineSegmentIntersector::Intersections _intersections = _lineSegmentIntersector->getIntersections();
- int _intersectionNumber=_intersections.size();
- osgUtil::LineSegmentIntersector::Intersections::iterator hitr = _intersections.begin();
- for (; hitr != _intersections.end();hitr++)
- {
- buttomPointList->push_back(hitr->getWorldIntersectPoint());
- }
- for (int i=0;i<buttomPointList->size();i++)
- {
- osg::Geode* geode=new osg::Geode;
- osg::ShapeDrawable *viewPoint=new osg::ShapeDrawable(new osg::Sphere(buttomPointList->at(i),0.01f));
- viewPoint->setColor(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
- geode->addDrawable(viewPoint);
- sphereGroup->addChild(geode);
- }
- }
- osg::ref_ptr<osg::Group>getVisiableRange(osg::Vec3d eyePoint,osg::Vec3dArray* buttomList,osg::Node *model,float gre,float yel,float red)
- {
- osg::ref_ptr<osg::Group>visiableRange=new osg::Group;
- osg::Vec4 colorGre=osg::Vec4(0.0f,1.0f,0.0f,gre);
- osg::Vec4 colorYel=osg::Vec4(1.0f,1.0f,0.0f,yel);
- osg::Vec4 colorRed=osg::Vec4(1.0f,0.0f,0.0f,red);
- for (int i=0;i<buttomList->size()-1;i++)
- {
- osg::ref_ptr<osg::Group>group=new osg::Group;
- osg::ref_ptr<osg::Group>group2=new osg::Group;
- osg::ref_ptr<osg::Group>geodeGre=new osg::Group;
- osg::ref_ptr<osg::Group>geodeYel=new osg::Group;
- osg::ref_ptr<osg::Group>geodeRed=new osg::Group;
- osg::ref_ptr<osg::Vec3dArray>pointArrYel=new osg::Vec3dArray;
- osg::ref_ptr<osg::Vec3dArray>pointArrRed=new osg::Vec3dArray;
- osg::ref_ptr<osg::Vec3dArray> pointArrGre=new osg::Vec3dArray;
- osg::Vec3d end=buttomList->at(i);
- osg::Vec3dArray* hitList=new osg::Vec3dArray;
- getIntersections(hitList,group,model,eyePoint,end);
- osg::Vec3d end2=buttomList->at(i+1);
- osg::Vec3dArray* hitList2=new osg::Vec3dArray;
- getIntersections(hitList2,group2,model,eyePoint,end2);
- cout<<"hitList:--"<<hitList->size()<<"hitList2:--"<<hitList2->size()<<endl<<endl;
- //||((end-hitList->at(0)).length()<0.1&&(end2-hitList2->at(0)).length()<0.1)||((eyePoint-hitList->at(0)).length()<0.1&&(eyePoint-hitList2->at(0)).length()<0.1)
- if (hitList->size()==0 && hitList2->size()==0)
- {
- pointArrGre->push_back(eyePoint);
- pointArrGre->push_back(end);
- pointArrGre->push_back(end2);
- }
- if (hitList->size()==0&&hitList2->size()!=0)
- {
- osg::Vec3d visiablePoint2=hitList2->at(0);
- pointArrYel->push_back(eyePoint);
- pointArrYel->push_back(end);
- pointArrYel->push_back(visiablePoint2);
- pointArrRed->push_back(visiablePoint2);
- pointArrRed->push_back(end);
- pointArrRed->push_back(end2);
- }
- else if (hitList->size()!=0&&hitList2->size()==0)
- {
- osg::Vec3d visiablePoint=hitList->at(0);
- pointArrYel->push_back(eyePoint);
- pointArrYel->push_back(visiablePoint);
- pointArrYel->push_back(end2);
- pointArrRed->push_back(visiablePoint);
- pointArrRed->push_back(end);
- pointArrRed->push_back(end2);
- }
- else
- {
- osg::Vec3d visiablePoint=hitList->at(0);//(hitList->at(1)-hitList->at(0)).length()<0.2?hitList->at(1):
- osg::Vec3d visiablePoint2=hitList2->at(0);//(hitList2->at(1)-hitList->at(0)).length()<0.2?hitList2->at(1):
- pointArrYel->push_back(visiablePoint);
- pointArrYel->push_back(visiablePoint2);
- pointArrRed->push_back(visiablePoint);
- pointArrRed->push_back(end);
- pointArrRed->push_back(end2);
- pointArrRed->push_back(visiablePoint2);
- }
- geodeYel->addChild(getPoly(pointArrYel,colorYel));
- geodeRed->addChild(getPoly(pointArrRed,colorRed));
- geodeGre->addChild(getPoly(pointArrGre,colorGre));
- visiableRange->addChild(geodeYel);
- visiableRange->addChild(geodeRed);
- visiableRange->addChild(geodeGre);
- visiableRange->addChild(group);
- visiableRange->addChild(group2);
- }
- // break;
- return visiableRange;
- }
复制代码 osg::ref_ptr< osg::Vec3dArray> buttomList = new osg::Vec3dArray;
buttomList->push_back(start);
osg::ref_ptr<osg::Group>group=new osg::Group;
getIntersections(buttomList,group,modelGroup,start,end);
buttomList->push_back(end);
cout<<"buttomPointList Size"<<buttomList->size()<<endl<<endl;
viewer->getSceneData()->asGroup()->addChild(getVisiableRange( eyePoint,buttomList,modelGroup,0.2,0.2,0.2));
这个是handle()调用的上面函数的部分。
上面第一个函数:getIntersections()是求的交点的函数,使用lineSegmentIntersector;
第二个函数:getVisiableRange()是获得可是范围多边形的函数,分为四种情况:1)两条边都没有交点,直接可视,用绿色三角形表示;2)左边没有交点,右边有交点,取第一个交点作为可视与不可视的划分,该交点的前部分用黄色三角形表示,后部分用红色三角形;3)同2)条件相反;4)两条边都有交点,分别取第一个交点作为划分,黄色三角形为可见部分,红色的(应该是梯形)为不可见部分。
我还没有找到哪里错了,但是结果不对,各位人才,帮忙看看吧,拜谢……
|
|