OSG得到OSGB中几何体顶点、法向量、三角形数据并可视化

参考这位大哥的博客https://blog.csdn.net/qq_31709249/article/details/94357183

代码放到我的github上面了

https://github.com/DwyaneLegend/osgb_data_analysis

以下是我的理解以及改动之后的代码

我先展示一下最终main函数中实现的核心代码

osgViewer::Viewer viewer;
			osg::ref_ptr<osg::Group> root = new osg::Group;
			osg::ref_ptr<osgDB::Options> options = new osgDB::Options("noRotation");
			for (size_t i = 0; i<fileNames.size(); i++)
			{
				string name = fileNames[i];
				osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(name);
				string modelName = Utility::getFileNameFromPath(name);
				PositionVisitor visitor = PositionVisitor(modelName,textSize);
				node->accept(visitor);
				root->addChild(visitor.createRandomColorOsgNode(i));				
			}
			//root->addChild(Utility::createCoorAxis(textSize));
			cout<<"num children"<<root->getNumChildren()<<endl;
			viewer.setSceneData(root);
			viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
			viewer.addEventHandler(new osgViewer::StatsHandler);
			viewer.setUpViewOnSingleScreen(1);//这里是单屏幕显示
			viewer.run();

首先将osgb文件读取到node中   name为osgb文件绝对路径

osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(name);

然后自己编写Positionvisitor类,继承于NodeVisitor,相当于访问器,用于访问一个node中drawable的所有Geom

PositionVisitor visitor = PositionVisitor(modelName,textSize);

 以下为Positionvisitor.h

class PositionVisitor
	:public osg::NodeVisitor
{
protected:
	vector<Geom*> allGeom;
	osg::Vec4 geomColor;
	string modelName;
	int textSize;//提示文字的大小
	osg::BoundingBox boundingBox;
public:
	virtual  void apply(osg::Geode& node) override;
	void dealTriangleInfo(ModelAttributeFunctor attributeFunctor,osg::TriangleIndexFunctor<TriangleIndex> indexFunctor);//处理访问器得到的信息,构建三角形关系
	osg::ref_ptr<osg::Node> createOsgNode(osg::Vec4 color,int order);//根据指定的颜色,将geom中的数据创建成osg节点
	osg::ref_ptr<osg::Node> createRandomColorOsgNode(int order);//将geom中的数据创建成osg节点,颜色随机
	osg::ref_ptr<osgText::Text> createTipText(short direction);//创建提示文字
	osg::ref_ptr<osgText::Text> createCenterText(osg::BoundingBox);//创建geom的中心点
	PositionVisitor(string ModelName);
	PositionVisitor(string ModelName, int TextSize);
	~PositionVisitor();
};

上面提到的Geom 如下,存储了顶点,三角形等信息,之后我再解释geom是什么

class Geom
{
public:
	vector<Vertex*> vertices;
	vector<Triangle*> triangles;
	osg::BoundingBox  boundingBox;
	bool isTwoTriangleNeighbor(int triangle1Index,int triangle2Index);
	osg::ref_ptr<osg::Geode> createOsgNode(osg::Vec4 color);
	osg::ref_ptr<osg::Geode> Geom::createOsgNode_Point(osg::Vec4 color);
	osg::ref_ptr<osg::Geode> Geom::createOsgNode_Triangle(osg::Vec4 color);
	Geom();
	~Geom();
};

然后node的accept函数可以调用visitor(基类NodeVisitor)的apply函数

node->accept(visitor);

我们自己重写apply函数 通过node得到drawable,然后drawable的accep

  • 10
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在OpenSceneGraph (OSG),要修改或删除节点的部分顶点数据并进行可视化更新,可以按照以下步骤进行操作: 1. 获取节点:首先,需要获取包含要修改的顶点数据的节点。可以使用场景图遍历或根据节点名称等方式获取到要操作的节点。 2. 获取顶点数据:使用节点的getOrCreateStateSet()方获取节点的状态集。然后,通过getStateAttribute()方和类型标识(如osg::Geometry)获取到几何体属性节点(osg::Geometry)。再通过osg::Geometry的getVertexArray()方获取到顶点数据数组osg::Vec3Array。 3. 修改顶点数据:根据需要,可以直接通过osg::Vec3Array的方修改顶点数据。例如,可以使用set()方修改指定索引处的顶点坐标,或使用push_back()方添加新的顶点。 4. 更新可视化:修改完顶点数据后,需要更新场景图以进行可视化更新。可以调用osg::NodeVisitor类的traverse()方遍历整个场景图,并调用osg::Geometry的dirtyDisplayList()和dirtyBound()方标记几何体属性需要更新。之后,调用osgViewer::Viewer类的frame()方更新视图。 下面是一个简单的示例代码片段,展示了如何修改节点顶点数据并进行可视化更新: ```cpp // 获取节点 osg::ref_ptr<osg::Node> node = ...; // 获取顶点数据 osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(node->getOrCreateStateSet()->getAttribute(osg::StateAttribute::Type::GEOMETRY)); osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()); // 修改顶点数据 (*vertices)[0] = osg::Vec3(1.0, 2.0, 3.0); // 修改第一个顶点坐标 // 更新可视化 geometry->dirtyDisplayList(); geometry->dirtyBound(); osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer; viewer->setSceneData(node); viewer->frame(); ``` 请注意,这只是一个简单的示例,实际操作可能会涉及更多的步骤和细节,具体根据你的场景和需求进行相应的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值