struct Subset
{
std::vector<float> vertexs;//位置
std::vector<float> normals;//法向
std::vector<float> texCoords;//纹理
std::vector<unsigned int> indices;//索引下标
std::vector<unsigned int> faceMtrls;//面材质索引
}; class GetSimplifySTLDataVisitor : public osg::NodeVisitor
{
public:
GetSimplifySTLDataVisitor(Subset &s):dstSubset(s),
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{
} virtual void apply(osg::Geode& geode)
{
unsigned int count = geode.getNumDrawables();
for ( unsigned int i = ; i < count; i++ )
{
osg::Geometry *geometry = geode.getDrawable( i )->asGeometry();
if ( !geometry )
continue;
// 顶点数据
osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
int vertexlNum = vertices->size();
for ( int i=; i<vertexlNum; i++) {
dstSubset.vertexs.push_back( vertices->at(i).x() );
dstSubset.vertexs.push_back( vertices->at(i).y() );
dstSubset.vertexs.push_back( vertices->at(i).z() );
}
// 法向量
osg::Vec3Array* normals = dynamic_cast<osg::Vec3Array*>(geometry->getNormalArray());
int normalNum = normals->size();
for (int i=; i<normalNum; i++) {
dstSubset.normals.push_back( normals->at(i).x() );
dstSubset.normals.push_back( normals->at(i).y() );
dstSubset.normals.push_back( normals->at(i).z() );
}
normalBindKinds.insert(geometry->getNormalBinding());
if(osg::Geometry::BIND_PER_VERTEX != geometry->getNormalBinding() )
{
std::string msg="未处理的法向量绑定方式";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
// 索引数组
for ( unsigned int i = ; i < geometry->getNumPrimitiveSets(); ++i )
{
osg::PrimitiveSet* ps = geometry->getPrimitiveSet( i );
if ( !ps ) continue;
pts.insert(ps->getType());
modes.insert(ps->getMode());
switch( ps->getType() )
{
case osg::PrimitiveSet::DrawElementsUIntPrimitiveType :
{
osg::DrawElementsUInt* deui = dynamic_cast<osg::DrawElementsUInt*>(ps);
const unsigned indexNum = deui->getNumIndices();
switch( deui->getMode() )
{
case osg::PrimitiveSet::TRIANGLES :
{
for (unsigned int i=; i<indexNum; i++)
{
dstSubset.indices.push_back( deui->at(i) );
}
break;
}
default:
{
std::string msg="未处理的绘制模式";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
}
break;
}
case osg::PrimitiveSet::DrawElementsUShortPrimitiveType :
{
osg::DrawElementsUShort* de = dynamic_cast<osg::DrawElementsUShort*>(ps);
const unsigned indexNum = de->getNumIndices();
switch( de->getMode() )
{
case osg::PrimitiveSet::TRIANGLES :
{
for (unsigned int i=; i<indexNum; i++)
{
dstSubset.indices.push_back( de->at(i) );
}
break;
}
case osg::PrimitiveSet::TRIANGLE_STRIP :
{
for (unsigned int i=; i<indexNum-; i++)
{
//此处索引为何与基数偶数有关,可百度GL_TRIANGLE_STRIP
if (i%==)
{
dstSubset.indices.push_back( de->at(i) );
dstSubset.indices.push_back( de->at(i+) );
dstSubset.indices.push_back( de->at(i+) );
}
else
{
dstSubset.indices.push_back( de->at(i) );
dstSubset.indices.push_back( de->at(i+) );
dstSubset.indices.push_back( de->at(i+) );
}
}
break;
}
default:
{
std::string msg="未处理的绘制模式";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
}
break;
}
case osg::PrimitiveSet::DrawArraysPrimitiveType :
{
osg::DrawArrays* da = dynamic_cast<osg::DrawArrays*>(ps);
int first=da->getFirst();
int count=da->getCount();
switch( da->getMode() )
{
case osg::PrimitiveSet::TRIANGLES :
{
for ( int i=first; i<first+count; i++) {
dstSubset.indices.push_back( i );
}
break;
}
case osg::PrimitiveSet::TRIANGLE_STRIP :
{
for ( int i=first; i<first+count-; i++) {
if(i%==)
{
dstSubset.indices.push_back( i );
dstSubset.indices.push_back( i+ );
dstSubset.indices.push_back( i+ );
}
else
{
dstSubset.indices.push_back( i );
dstSubset.indices.push_back( i+ );
dstSubset.indices.push_back( i+ );
}
}
break;
}
default:
{
std::string msg="未处理的绘制模式";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
}
break;
}
default:
{
std::string msg="未处理的图元类型";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
}
}
}
}
private:
Subset &dstSubset;
//以下三个数组用来测试
std::set<osg::Geometry::AttributeBinding> normalBindKinds;
std::set<osg::PrimitiveSet::Type> pts;
std::set<GLenum> modes;
};

NodeVisitor的使用-遍历Geode节点下的Geometry并获取顶点、法向量等数据的更多相关文章

  1. NodeVisitor的使用-遍历Geode节点并在它与父节点之间添加一个LOD节点

    #include <osg\NodeVisitor>#include <osg\MatrixTransform>#include <osg\PagedLOD>#in ...

  2. PHP遍历文件夹下的文件和获取到input name的值

    <?php$dir = dirname(__FILE__); //要遍历的目录名字 ->当前文件所在的文件夹//$dir='D:\PHP\wamp\www\admin\hosts\admi ...

  3. Extjs4.x Ext.tree.Panel 遍历当前节点下的所有子节点

    Ext.define('WMS.controller.Org', { extend: 'Ext.app.Controller', stores: ['OrgUser', 'OrgTree'], mod ...

  4. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  5. VBS获取Ini配置文件一个节点下的所有字段的值

    ''* 功能:使用VBS读取ini文件中指定节点下的所有值'* 输入参数:inipath :ini文件的地址'*           initypes :ini文件中包含在"["和 ...

  6. 遍历树节点(多层)的方法(java)

    前序遍历,后序遍历,广度遍历,深度遍历,遍历一级节点.以及按钮如何响应点击事件. import java.awt.*; import java.awt.event.*; import java.uti ...

  7. asp.net 遍历文件夹下全部子文件夹并绑定到gridview上

    遍历文件夹下所有子文件夹,并且遍历配置文件某一节点中所有key,value并且绑定到GridView上 Helper app_Helper = new Helper(); DataSet ds = n ...

  8. 记录JS如何使用广度遍历找到节点的所有父节点

    我们在实际的工作业务场景中经常遇到这样的场景,求取树数据中某个节点的父亲节点以及所有的父亲节点,这样的场景下不建议使用深度遍历,使用广度遍历可以更快找到. 1.案例解说 比如树的长相是这样的: 树的数 ...

  9. C#遍历文件夹下所有文件

    FolderForm.cs的代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using ...

随机推荐

  1. PHP 数组的遍历的几种方式(以及foreach与for/while+each效率的比较)

    * 使用foreach遍历数组时要注意的问题: * 1.foreach在遍历之前会自动重置指针使用其指向第一个元素,所以foreach可以多次遍历 * 2.foreach遍历完成之后,指针是没有指向数 ...

  2. CentOS启用sudo,禁用root远程登录

    CentOS默认不启用sudo,且可以直接用超级管理员身份登录服务器.ubuntu这方面做得比较好,为了安全,减小误操作带来的损失,还是推荐启用sudo. 1.添加sudo用户 执行 visudo 命 ...

  3. nginx反向代理proxy模块相关参数

    http_proxy_module Proxy_pass proxy_pass指令属于ngx_http_proxy_module模块,此模块可以将请求转发到另一台服务器:官方说明:http://ngi ...

  4. Effective Java 学习笔记之创建和销毁对象

    一.考虑用静态工厂方法代替构造器 1.此处的静态工厂方法是指返回指为类的对象的静态方法,而不是设计模式中的静态工厂方法. 2.静态工厂方法的优势有: a.使用不同的方法名称可显著地表明两个静态工厂方法 ...

  5. dispaly:table-cell,inline-block,阐述以及案例

    display:table 此元素会作为块级表格来显示(类似 <table>),表格前后带有换行符.dispaly:table-row 此元素会作为一个表格行显示(类似 <tr> ...

  6. 关于tableView的错误提示

    WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWit ...

  7. 国外开源的PACS服务器

    国外开源的PACS服务器 罗朝辉(http://www.cnblogs.com/kesalin/) 本文遵循"署名-非商业用途-保持一致"创作公用协议 名称:Dcm4che评级:★ ...

  8. 2 GPS utility methods

    Methond 1 is to check whether the GPS is on: 1 2 3 4 5 public boolean isGPSIsOn() { LocationManager ...

  9. 16 BasicHashTable基本哈希表类(三)——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...

  10. Python sorted函数对列表排序

    http://jingyan.baidu.com/article/f3ad7d0ffe8e1409c2345b48.html http://www.cnblogs.com/100thMountain/ ...