osg解析系列-如何对一个模型(如圆柱)进行裁剪且绘制出相交的平面

int main()
{
	osg::Cylinder* cylinder = new osg::Cylinder(osg::Vec3(0.f, 0.f, 0.f), 2.0f, 10.0f);
	osg::ShapeDrawable* sd = new osg::ShapeDrawable(cylinder);
	sd->setColor(osg::Vec4(0.8f, 0.5f, 0.2f, 1.f));
	osg::Geode* geode = new osg::Geode;
	geode->addDrawable(sd);

	osg::ref_ptr<osg::ClipPlane> clipPlane = new osg::ClipPlane(0,0.6,0,0.8,  1);//截断平面

	osg::ref_ptr<osg::ClipNode> clipNode = new osg::ClipNode();
	clipNode->addClipPlane(clipPlane.get());
	clipNode->addChild(geode);
	clipNode->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);

	osg::ref_ptr<osg::Group> root = new osg::Group();
	root->addChild(clipNode.get());

	osgViewer::Viewer viewer;
	viewer.setSceneData(root.get());


	//如果 面求交
	osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup();
	osg::ref_ptr<osgUtil::PlaneIntersector> plane = new osgUtil::PlaneIntersector(osg::Plane(0.6, 0, 0.8, 1));
	intersectorGroup->addIntersector(plane.get());
	//遍历整个节点树(也可以是某个子树或几何体节点)
	osgUtil::IntersectionVisitor intersectVisitor(intersectorGroup.get());
	geode->accept(intersectVisitor);

	osg::Vec3Array* coords = new osg::Vec3Array;
	if (intersectorGroup->containsIntersections())
	{//有交点
		std::cout << "Found intersections " << std::endl;

		osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors();
		for (osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin();
			intersector_itr != intersectors.end();
			++intersector_itr)
		{
			osgUtil::PlaneIntersector* lsi = dynamic_cast<osgUtil::PlaneIntersector*>(intersector_itr->get());
			if (lsi)
			{
				osgUtil::PlaneIntersector::Intersections& intersections = lsi->getIntersections();
				std::cout << intersections.size() << std::endl;

				osgUtil::PlaneIntersector::Intersections::iterator itr;
				for (itr = intersections.begin();
					itr != intersections.end();
					++itr)
				{
					osgUtil::PlaneIntersector::Intersection& intersection = *itr;

					// OSG_NOTICE<<"  transforming "<<std::endl;
					// transform points on polyline
					for (osgUtil::PlaneIntersector::Intersection::Polyline::iterator pitr = intersection.polyline.begin();
						pitr != intersection.polyline.end();
						++pitr)
					{
						if (intersection.matrix.valid())
						{
							*pitr = (*pitr) * (*intersection.matrix);
						}
						coords->push_back(*pitr+osg::Vec3d(5,0,0));
						std::cout << "pos=" << (*pitr).x() << "," << (*pitr).y() << "," << (*pitr).z() << std::endl;
					}
				}
			}
		}

	}

	//相交的面
	if (coords->size() > 3)
	{
		osg::ref_ptr<osgUtil::DelaunayTriangulator> dt = new osgUtil::DelaunayTriangulator();
		// 添加点坐标
		dt->setInputPointArray(coords);
		dt->triangulate();
		osg::DrawElementsUInt* pElem = dt->getTriangles();

		osg::ref_ptr<osg::Geometry> ClipPlane = new osg::Geometry();
		ClipPlane->setVertexArray(coords);
		ClipPlane->addPrimitiveSet(pElem);
		geode->addDrawable(ClipPlane.get());
	}

	viewer.run();
}

运行结果:
在这里插入图片描述
在这里插入图片描述

相关推荐

  1. leetcode 1035.不线

    2024-03-10 08:00:04       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 08:00:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 08:00:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 08:00:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 08:00:04       20 阅读

热门阅读

  1. 基于MapReduce的汽车数据清洗与统计案例

    2024-03-10 08:00:04       20 阅读
  2. wpf中的Border和Background

    2024-03-10 08:00:04       19 阅读
  3. Neo4J图数据库入门示例

    2024-03-10 08:00:04       24 阅读
  4. SQL 注入攻击 - insert注入

    2024-03-10 08:00:04       30 阅读
  5. SQL之常用字符串函数

    2024-03-10 08:00:04       23 阅读
  6. Apache HBase

    2024-03-10 08:00:04       24 阅读
  7. 微信小程序提交成功设置提示

    2024-03-10 08:00:04       21 阅读
  8. 机器学习是什么

    2024-03-10 08:00:04       22 阅读
  9. Chapter 8 - 23. Congestion Management in TCP Storage Networks

    2024-03-10 08:00:04       24 阅读
  10. 机器学习(2_1)经验误差,拟合度,评估方法

    2024-03-10 08:00:04       25 阅读
  11. BJFU|计算机网络缩写对照表

    2024-03-10 08:00:04       20 阅读