OpenMesh 教程:使用网格属性和特性

使用网格属性和特性

翻译,原文链接

:原文标题为 Using mesh attributes and traits. Attribute 中文意思是属性,前面几篇文章中提到的 Property 中文翻译也是属性,我也不知道怎么翻译才能体现两个词的区别。从英文来看,Attribute 有附加和归属的意思,而 Property 有财产的意思,所以简单地可以认为 Attribute 指的是固有属性,一旦定义便不可删除,Property 则指的是可删除和添加的动态属性。

本文的主要内容是说明如何改变坐标、法向量、颜色和纹理 (positions, normals, colors, textures) 等变量的数据类型。

在前面的教程中 (使用标准属性) ,我们介绍了如何利用 request 函数来使用标准属性,而这些属性的数据类型通过 handle 的模板参数 (MyMesh::FPropHanleT<int>) 来确定,我们将标准属性的数据类型称为网格特性 (mesh traits)。有了这些特性,我们就可以拓展网格的数据结构,可以改变下面两个重要的特征:

  1. 改变坐标、法向量、颜色和纹理坐标
  2. 拓展网格要素 Vertex, Face, Edge, and Halfedge (参见 Extending the mesh using traits

所有自定义特性都必须派生于默认特性

struct MyTraits : OpenMesh::DefaultTraits

如上文所说,我们可以改变 MyMesh::Point, MyMesh::Normal, MyMesh::Color, MyMesh::TexCoord 的数据类型。 具体的方法就是在定义 MyTraints 时重新定义这些网格基础类型的,新的类型既可以是 OpenMesh 已有的类型,也可以是其他库中的类型 (如 Eigen)。比如下面的代码就将 MyMesh::Point 标量数据类型由默认的 float 改为 double ,也就是用 OpenMesh::Vec3d 替换 OpenMesh::Vec3d

typedef OpenMesh::Vec3d Point;
typedef OpenMesh::Vec3d Normal;

(一般情况下,Point 和 Normal 的标量数据类型最好保持一致,避免后续计算中有太多的类型转换)

值得注意的是,上面的代码其实是覆盖的父类 OpenMesh::DefaultTraits 中的默认定义。实际上 OpenMesh::DefaultTraits 一般都是空的,但它隐式地定义了 Point, Normal, TexCoord, Color 和另外一个属性 (attribute)

 // HalfedgeAttributes( OpenMesh::Attributes::PrevHalfedge );

PrevHalfedge 和之前的动态属性不同,它并不控制任何属性 (property),但是它对网格类型却有很大的影响,因为它表示的是半边 (halfedge) 的结构。它有两方面的影响:

  1. 快速读写 previous halfedge
  2. 增加内存消耗

是否使用这个特性取决于我们的需求。举个例子,在用函数 add_face() 创建面时,如果有 previous halfedge 的信息,计算量就会大幅下降。通常情况下,我们都会使用这个性质,如果不用的话,可以用下面的代码删除它

// HalfedgeAttributes( OpenMesh::Attributes::None );

这样每条边我们就可以少 8 字节的内存,可以节省相当多的内存,因为根据 Euler 公式 V − E + F = 2 ( 1 − g ) V - E + F = 2(1 - g) VE+F=2(1g) ,对于三角形网格 g = 0 g=0 g=0,边的数量 E 近似等于 E ≈ 3 V E \approx 3 V E3V

本文的完整代码如下

#include <iostream>
#include <typeinfo>
// --------------------
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/Geometry/VectorT.hh>
#ifndef DOXY_IGNORE_THIS
// Define my personal traits
struct MyTraits : OpenMesh::DefaultTraits
{
  // Let Point and Normal be a vector of doubles
  typedef OpenMesh::Vec3d Point;
  typedef OpenMesh::Vec3d Normal;
  // Already defined in OpenMesh::DefaultTraits
  // HalfedgeAttributes( OpenMesh::Attributes::PrevHalfedge );
  
  // Uncomment next line to disable attribute PrevHalfedge
  // HalfedgeAttributes( OpenMesh::Attributes::None );
  //
  // or
  //
  // HalfedgeAttributes( 0 );
};
#endif
// Define my mesh with the new traits!
typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits>  MyMesh;
// ------------------------------------------------------------------ main ----
int main(int argc, char **argv)
{
  MyMesh mesh;
  if (argc!=2)
  {
    std::cerr << "Usage: " << argv[0] << " <input>\n";
    return 1;
  }
  // Just make sure that point element type is double
  if ( typeid( OpenMesh::vector_traits<MyMesh::Point>::value_type ) 
       != typeid(double) )
  {
    std::cerr << "Ouch! ERROR! Data type is wrong!\n";
    return 1;
  }
  // Make sure that normal element type is double
  if ( typeid( OpenMesh::vector_traits<MyMesh::Normal>::value_type ) 
       != typeid(double) )
  {
    std::cerr << "Ouch! ERROR! Data type is wrong!\n";
    return 1;
  }
  // Add vertex normals as default property (ref. previous tutorial)
  mesh.request_vertex_normals();
  // Add face normals as default property
  mesh.request_face_normals();
  // load a mesh
  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 ) &&
       mesh.has_face_normals() && mesh.has_vertex_normals() )
  {
    // let the mesh update the normals
    mesh.update_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;
  }
  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值