|
发表于 2013-3-14 10:51:50
|
显示全部楼层
array 发表于 2013-3-14 10:20
。。。您这就叫做延长了么,还有您可以提交一个足够短的,可以运行且说明问题的程序让别人帮助调试。否则单 ...
那个不就是按照对应的方向向外延长单位向量么?难道不对?希望指点,拜谢……
下面是测试代码- #include <osgViewer/Viewer>
- #include <osgViewer/ViewerEventHandlers>
- #include <osg/Geode>
- #include <osg/Geometry>
- #include <osg/Node>
- #include <osg/Group>
- #include <osg/LineSegment>
- #include <osg/ShapeDrawable>
- #include <osg/BlendColor>
- #include <osg/Material>
- #include <osg/BoundingBox>
- #include <osg/ComputeBoundsVisitor>
- #include <osg/Shape>
- #include <osg/PositionAttitudeTransform>
- #include <osgDB/ReadFile>
- #include <osgDB/WriteFile>
- #include <osgGA/GUIEventHandler>
- #include <osgGA/StateSetManipulator>
- #include <osgUtil/Optimizer>
- #include <osgUtil/IntersectionVisitor>
- #include <osgUtil/IntersectVisitor>
- #include <math.h>
- using namespace std;
- typedef std::vector<osg::Vec3dArray *> ArrayVector;
- osg::ref_ptr<osg::Group>modelGroup=new osg::Group;
- osg::Vec3Array *pointList=new osg::Vec3Array;
- void getIntersections( osg::Vec3dArray *buttomPointList,osg::Group* sphereGroup, osg::Node* model, osg::Vec3d p_start,osg::Vec3d p_end)
- {
- osg::Vec3d n1=p_start-p_end;n1.normalize();
- osg::Vec3d n2=p_end-p_start;n2.normalize();
- osg::Vec3d pStart=p_start+n1;
- osg::Vec3d pEnd=p_end+n2;
- osg::ref_ptr< osgUtil::LineSegmentIntersector > _lineSegmentIntersector = new osgUtil::LineSegmentIntersector(pStart,pEnd);
- 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();
- osg::ref_ptr<osg::Geode>viewLine=new osg::Geode;
- osg::ref_ptr<osg::Geometry>geom=new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array>v=new osg::Vec3Array;
- osg::Vec4 color=osg::Vec4(1.0f,0.0f,0.0f,1.0f);
- v->push_back(p_start);
- v->push_back(p_end);
- geom->setVertexArray(v.get());
- osg::ref_ptr<osg::Vec4Array>vc=new osg::Vec4Array;
- vc->push_back(color);
- geom->setColorArray(vc.get());
- geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,2));
- viewLine->addDrawable(geom);
- sphereGroup->addChild(viewLine);
- for (; hitr != _intersections.end();hitr++)
- {
- osg::Vec3d point=hitr->getWorldIntersectPoint();
- cout<<" x: "<<point.x()<<" y: "<<point.y()<<" z: "<<point.z()<<endl;
- buttomPointList->push_back(point);
- osg::Geode* geode=new osg::Geode;
- osg::ShapeDrawable *viewPoint=new osg::ShapeDrawable(new osg::Sphere(point,0.1f));
- viewPoint->setColor(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
- geode->addDrawable(viewPoint);
- sphereGroup->addChild(geode);
- }
- }
- class PolygonHandler:public osgGA::GUIEventHandler
- {
- public:
- PolygonHandler()
- {
- }
- ~PolygonHandler()
- {}
- bool PolygonHandler::handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)
- {
- osgViewer::View *viewer=dynamic_cast<osgViewer::View*>(&aa);
- switch(ea.getEventType())
- {
- case (osgGA::GUIEventAdapter::PUSH):
- {
- x=ea.getX();
- y=ea.getY();
- if (ea.getButton()==1)
- {
- osgUtil::LineSegmentIntersector::Intersections intersections;
- if (viewer->computeIntersections(x,y,intersections))
- {
- osgUtil::LineSegmentIntersector::Intersection intersection=*intersections.begin();
- _point=osg::Vec3d(intersection.getWorldIntersectPoint());
- pointList->push_back(_point);
- if (pointList->size()>=2&&pointList->size()%2==0)
- {
- osg::Vec3d Point1=pointList->at(pointList->size()-2);
- osg::Vec3d Point2=pointList->at(pointList->size()-1);
- osg::ref_ptr< osg::Vec3dArray> buttomList = new osg::Vec3dArray;
- osg::ref_ptr<osg::Group>group=new osg::Group;
- getIntersections(buttomList,group,modelGroup,Point1,Point2);
- viewer->getSceneData()->asGroup()->addChild(group);
- }
- }
- }
- return false;
- }break;
- default:
- return false;
- }
- }
- private:
- osg::Vec3d _point;
- double x;
- double y;
- };
- int main()
- {
- osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;
- osg::ref_ptr<osg::Group>root=new osg::Group;
- osg::ref_ptr<osg::Node>model=osgDB::readNodeFile("moxing.osg");
- modelGroup->addChild(model.get());
- root->addChild(modelGroup.get());
- viewer->addEventHandler(new osgViewer::WindowSizeHandler);
- viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
- viewer->addEventHandler(new PolygonHandler);
- osgUtil::Optimizer optimizer;
- optimizer.optimize(root.get());
- viewer->setSceneData(root.get());
- viewer->realize();
- viewer->run();
- return 0;
- }
复制代码
moxing.osg.gz
(110.35 KB, 下载次数: 1)
|
|