VCG Library
trimesh_attribute_saving.cpp
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
32 #include<vcg/complex/complex.h>
33 #include<vcg/complex/algorithms/create/platonic.h>
34 #include<wrap/io_trimesh/export_ply.h>
35 class MyEdge;
36 class MyFace;
37 class MyVertex;
38 struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<MyVertex>::AsVertexType,
39  vcg::Use<MyFace> ::AsFaceType>{};
40 
41 class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f,vcg::vertex::Normal3f, vcg::vertex::BitFlags>{};
42 class MyFace : public vcg::Face< MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f, vcg::face::BitFlags> {};
43 
44 class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> > {};
45 int main()
46 {
47  MyMesh m;
48  Torus<MyMesh>(m, 3.0f, 1.0f);
50  // add a per-vertex attribute with type float named "GaussianCurvature"
51  MyMesh::PerVertexAttributeHandle<float>
52  hvf = vcg::tri::Allocator<MyMesh>:: GetPerVertexAttribute<float> (m,std::string("GaussianCurvature"));
53 
54  MyMesh::PerVertexAttributeHandle<vcg::Point3f>
55  hv3f = vcg::tri::Allocator<MyMesh>:: GetPerVertexAttribute<vcg::Point3f> (m,std::string("InvertedNormal"));
56 
57  // add a per-face attribute with type float named "FaceArea"
58  MyMesh::PerFaceAttributeHandle<float>
59  hff = vcg::tri::Allocator<MyMesh>:: GetPerFaceAttribute<float> (m,std::string("FaceArea"));
61  vcg::tri::Allocator<MyMesh>::ClearPerVertexAttribute<float>(m,hvf, float(M_PI*2));
62  vcg::tri::Allocator<MyMesh>::ClearPerVertexAttribute<vcg::Point3f>(m,hv3f, vcg::Point3f(0,0,0));
63 
64  ForEachFace(m, [&](MyFace &f){
65  hff[&f]=vcg::DoubleArea(f)*0.5f;
66  for(int i=0;i<3;++i){
67  hvf[f.V(i)] -= vcg::Angle(f.P1(i)-f.P0(i),f.P2(i)-f.P0(i));
68  hv3f[f.V(i)] -= vcg::NormalizedTriangleNormal(f);
69  }
70  });
71 
73  vcg::tri::io::PlyInfo pi;
74  pi.AddPerVertexFloatAttribute("GaussianCurvature","quality");
75  pi.AddPerFaceFloatAttribute("FaceArea");
76  pi.AddPerVertexPoint3fAttribute(m,"InvertedNormal");
77  vcg::tri::io::ExporterPLY<MyMesh>::Save(m,"MeshWithCurvature.ply",false,pi);
78 }