【OpenMesh】使用标准属性

本文详细介绍了如何在OpenMesh中添加、获取和移除标准属性,包括颜色、位置、法线等,同时解释了属性的引用计数机制以及如何测试属性是否存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个例子展示:
  • 如何添加标准属性
  • 如何获取和设置标准属性的值
正如之前所说,我们可以通过属性绑定数据到网格实体中。OpenMesh提供一连串的标准属性。不像自定义属性,标准属性有一些特定的功能和不同的接口,这就是这篇指南所要讨论的。

下面的表格列举了所有可能的标准属性和能够使用这些属性适合的实体。

 VertexFaceEdgeHalfedge
ColorXXX 
NormalXX X
PositionX   
StatusXXXX
TexCoordX  X

添加一个标准属性到实体只用使用适合的请求方法,比如request_face_normals()。唯一的例外就是position。它不能被添加因为它永久可用,因此它不能够被移除。

在例子中,我们
  • 添加顶点法矢量到网格对象
  • 加载文件
  • 检查是否文件提供顶点normal,如果没有就计算他们
  • 沿着顶点法矢量的方向移动每一个顶点一个单位长度
  • 打印结果位置到std::cout
让我们开始添加顶点法向量到网格中:
mesh.request_vertex_normals();
我们可以用相似的方法请求其他的属性。比如面的法向量:
mesh.request_face_normals();
我们需要这些值来计算顶点向量,使用update_normal(),如果文件没有提供这些。
但是我们可以使用标准属性做更多的事。我们可以验证是否网格已经有了顶点向量
if (!mesh.has_vertex_normals())
{
std::cerr << "ERROR: Standard vertex property 'Normals' not available!\n";
return 1;
}
使用完之后我们移除他们:
mesh.release_vertex_normals();
但是,如果程序中的顶点状态呗请求两次会发生什么?第一次释放没有做实际工作,但是第二次释放回移除它。标准属性有一个引用计数,每次请求都会使计数增加1而每次释放都会使计数减少1.如果计数器减少到0,那么这个属性就会被移除内存。
如上表所述,我们有9个动态请求属性,这些请求函数定义在OpenMesh::Concepts::KernelT:
  • request_edge_status()
  • request_edge_colors()
  • request_face_colors()
  • request_face_normals()
  • request_face_status()
  • request_face_texture_index()
  • request_halfedge_status()
  • request_halfedge_normals()
  • request_halfedge_texcoords1D()
  • request_halfedge_texcoords2D()
  • request_halfedge_texcoords3D()
  • request_vertex_colors()
  • request_vertex_normals()
  • request_vertex_status()
  • request_vertex_texcoords1D()
  • request_vertex_texcoords2D()
  • request_vertex_texcoords3D()
添加的属性可以用以下的函数释放:
  • release_edge_status()
  • release_edge_colors()
  • release_face_colors()
  • release_face_normals()
  • release_face_status()
  • release_face_texture_index()
  • release_halfedge_status()
  • release_halfedge_normals()
  • release_halfedge_texcoords1D()
  • release_halfedge_texcoords2D()
  • release_halfedge_texcoords3D()
  • release_vertex_colors()
  • release_vertex_normals()
  • release_vertex_status()
  • release_vertex_texcoords1D()
  • release_vertex_texcoords2D()
  • release_vertex_texcoords3D()
属性的存在可以用这些函数测试:
  • has_edge_status()
  • has_edge_colors()
  • has_face_colors()
  • has_face_normals()
  • has_face_status()
  • has_face_texture_index()
  • has_halfedge_status()
  • has_halfedge_normals()
  • has_halfedge_texcoords1D()
  • has_halfedge_texcoords2D()
  • has_halfedge_texcoords3D()
  • has_vertex_colors()
  • has_vertex_normals()
  • has_vertex_status()
  • has_vertex_texcoords1D()
  • has_vertex_texcoords2D()
  • has_vertex_texcoords3D()
当属性能够被请求的时候返回true。
这个状态属性用于标记几何元素是被选中了还是删除了。
现在我们知道如何添加和移除标准属性,但是我们如何访问他们呢?我们再次需要网格对象。不像自定义属性,访问属性使用网格成员函数property(),对于每一个标准属性,网格提供get和set方法。我们已经使用了get和set方法,在之前的指南中,当我们为顶点位置计算一个新的地址。这里我们沿着顶点法向量移动一个单位长的距离:
for (MyMesh::VertexIter v_it = mesh.vertices_begin();
v_it != mesh.vertices_end(); ++v_it)
{
mesh.set_point( v_it, mesh.point(v_it)+mesh.normal(v_it) );
}
get方法传入实体句柄并返回想要的属性值,set方法需要一个添加的参数传递新的值到属性中。根据表格,不是每一个get/set方法应用于每一个实体。比如,一个面通常没有纹理坐标(texture coordinates不知道意思)。因此调用mesh.texcoord2D( _face_handle )会导致错误。
因为我们知道如何add/remove/access标准属性,一个更为深刻的问题任然存在。属性的数据类型是什么?这里面有更多的秘密么?下一个指南会给出答案。
完整的代码:
#include <iostream>
// --------------------
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
typedef OpenMesh::TriMesh_ArrayKernelT<> MyMesh;
int main(int argc, char **argv)
{
MyMesh mesh;
if (argc!=2)
{
std::cerr << "Usage: " << argv[0] << " <input>\n";
return 1;
}
// request vertex normals, so the mesh reader can use normal information
// if available
mesh.request_vertex_normals();
// assure we have vertex normals
if (!mesh.has_vertex_normals())
{
std::cerr << "ERROR: Standard vertex property 'Normals' not available!\n";
return 1;
}
OpenMesh::IO::Options opt;
if ( ! OpenMesh::IO::read_mesh(mesh,argv[1], opt))
{
std::cerr << "Error loading mesh from file " << argv[1] << std::endl;
return 1;
}
// If the file did not provide vertex normals, then calculate them
if ( !opt.check( OpenMesh::IO::Options::VertexNormal ) )
{
// we need face normals to update the vertex normals
mesh.request_face_normals();
// let the mesh update the normals
mesh.update_normals();
// dispose the face normals, as we don't need them anymore
mesh.release_face_normals();
}
// move all vertices one unit length along it's normal direction
for (MyMesh::VertexIter v_it = mesh.vertices_begin();
v_it != mesh.vertices_end(); ++v_it)
{
std::cout << "Vertex #" << v_it << ": " << mesh.point( v_it );
mesh.set_point( v_it, mesh.point(v_it)+mesh.normal(v_it) );
std::cout << " moved to " << mesh.point( v_it ) << std::endl;
}
// don't need the normals anymore? Remove them!
mesh.release_vertex_normals();
// just check if it really works
if (mesh.has_vertex_normals())
{
std::cerr << "Ouch! ERROR! Shouldn't have any vertex normals anymore!\n";
return 1;
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值