与自定义属性不同,用户通过将类型传递给句柄来指定数据类型(例如,MyMesh :: FPropHandleT <int>),标准属性的数据类型由所谓的网格特征定义。 通过traits,我们可以自定义和扩展网格数据结构。 我们可以通过改变两个重要特征来做到这一点:
- 更改位置,法线,颜色和纹理坐标的数据类型
- 扩展网格实体Vertex,Face,Edge和Halfedge
每个自定义特征都应该来自默认特征。
如上所述,我们可以更改基本类型MyMesh :: Point,MyMesh :: Normal,MyMesh :: Color和MyMesh :: TexCoord的基本数据类型。 我们可以使用提供的vector类,或者我们使用另一个库中的另一个。 这里我们简单地用OpenMesh :: Vec3d替换默认类型OpenMesh :: Vec3f(在OpenMesh :: DefaultTraits中定义)的位置和法线
实际上,结构OpenMesh :: DefaultTraits只是空的。 它只定义了Point,Normal,TexCoord和Color的类型以及我们一直隐式使用的一个属性:PrevHalfedge属性不同,因为它不控制属性。 然而,它对最终的网格类型产生了很大的影响,因为它为halfedge结构增加了额外的信息。 影响是双重的:
- 快速访问以前的halfedge
- 增加内存消耗
使用此功能非常依赖于我们的需求。 前面的halfedges非常方便的一种情况是网状成员函数add_face()。 当有关前一个halfedge的信息可用时,成员函数的执行时间会急剧下降。 通常我们想要这些信息。 但如果没有,因为我们必须节省内存,我们可以轻松地删除它。
然后我们需要每个边缘少8个字节,这可以是非常多的,因为可以从欧拉公式($ V-E + F = 2(1-g)$)得到,对于常规三角形网格与$ g = 0 $边数$ E $约为顶点数$ 3 $的三倍:$ E \约3 \ cdot V $。
#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;
}