#ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgViewer/CompositeViewer> #include <osgDB/ReadFile> #include <osg/Geode> #include <osg/Node> #include <osgGA/TrackballManipulator> #include <osg/GraphicsContext> #include <osg/ShapeDrawable> #include <osg/Material> #include <osg/Image> #include <osg/Texture2D> #include <osg/TexEnv> #include <osg/TexGen> #include <osg/MatrixTransform> #include <osg/PositionAttitudeTransform> #include <osg/AnimationPath> #include <osg/Matrixd> #include <osgGA/GUIEventHandler> #include <osgGA/CameraManipulator> #include <osgGA/StandardManipulator> #include <osgGA/OrbitManipulator> #include <osgGA/TrackballManipulator> #include <osgUtil/IntersectionVisitor> #include <osgUtil/LineSegmentIntersector> //创建盒子 osg::ref_ptr<osg::Geode> createBox() { osg::ref_ptr<osg::Geode> geode1 = new osg::Geode; geode1->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0,0.0,0.0),10.0,8.0,6.0))); geode1->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 0.0, 0.0), 0.1, 0.1, 20))); return geode1; } int main() { osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer; osg::ref_ptr<osg::Group> group1 = new osg::Group; //osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("D:\\参考手册\\BIM\\osg\\build1.OSGB"); //group1->addChild(node1.get()); osg::ref_ptr<osgUtil::LineSegmentIntersector> lineSegmentIntesector = new osgUtil::LineSegmentIntersector(osg::Vec3(0,0,15),osg::Vec3(0,0,-15)); osg::ref_ptr<osgUtil::IntersectionVisitor> intersectionVisitor1 = new osgUtil::IntersectionVisitor(lineSegmentIntesector); group1->addChild(createBox()); group1->accept(*intersectionVisitor1.get()); osgUtil::LineSegmentIntersector::Intersections intersections; //输出交点 if (lineSegmentIntesector->containsIntersections()) { intersections = lineSegmentIntesector->getIntersections(); osgUtil::LineSegmentIntersector::Intersections::iterator iter; for (iter = intersections.begin(); iter != intersections.end(); ++iter) { std::cout << "x:"<<iter->getWorldIntersectPoint().x() << " y:" << iter->getWorldIntersectPoint().y() << " z:" << iter->getWorldIntersectPoint().z() << std::endl; } } viewer1->setSceneData(group1.get()); viewer1->setUpViewInWindow(200, 200, 800, 600, 0); return viewer1->run(); }