VCG Library
trimesh_attribute.cpp
Go to the documentation of this file.
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 class MyEdge;
34 class MyFace;
35 class MyVertex;
36 struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<MyVertex> ::AsVertexType,
37  vcg::Use<MyFace> ::AsFaceType>{};
38 
39 class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f,vcg::vertex::Normal3f>{};
40 class MyFace : public vcg::Face< MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f> {};
41 
42 class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> > {};
43 
44 int main()
45 {
46  MyMesh m;
48  // add a per-vertex attribute with type float named "Irradiance"
49  MyMesh::PerVertexAttributeHandle<float> named_hv = vcg::tri::Allocator<MyMesh>:: GetPerVertexAttribute<float> (m,std::string("Irradiance"));
51 
52  // add a per-vertex attribute with type float named "Radiosity"
53  vcg::tri::Allocator<MyMesh>:: GetPerVertexAttribute<float> (m,std::string("Radiosity"));
54 
55  // add a per-vertex attribute with type bool and no name specified
56  MyMesh::PerVertexAttributeHandle<bool> anon_hv = vcg::tri::Allocator<MyMesh>:: GetPerVertexAttribute<bool> (m);
57 
58  // add a per-face attribute with type bool and no name specified
59  MyMesh::PerFaceAttributeHandle<bool> anon_hf = vcg::tri::Allocator<MyMesh>:: GetPerFaceAttribute<bool> (m);
60 
62  MyMesh::VertexIterator vi; int i;
63  for(i=0, vi = m.vert.begin(); vi != m.vert.end(); ++vi,++i){
64  named_hv[vi] = 1.0f; // [] operator takes a iterator
65  named_hv[*vi] = 1.0f; // or a MyMesh::VertexType object
66  named_hv[&*vi]= 1.0f; // or a pointer to it
67  named_hv[i] = 1.0f; // or an integer index
68  }
70 
72 
73  // query if an attribute is present or not
74  bool hasRadiosity = vcg::tri::HasPerVertexAttribute(m,"Radiosity");
75 
76  // obtain another handle of a previously attribute
77  MyMesh::PerVertexAttributeHandle<float> ret_hv = vcg::tri::Allocator<MyMesh>:: FindPerVertexAttribute<float>(m,"Radiosity");
78 
80  // you can also have PerMesh attributes
81  MyMesh::PerMeshAttributeHandle<int> hm = vcg::tri::Allocator<MyMesh>:: GetPerMeshAttribute<int> (m,std::string("ADummyIntegerAttribute"));
82  // PerMesh attributes are accessed directly using the handle itself
83  hm() = 10;
85 
87  // delete an attribute by name
89 
90  // delete an attribute by handle
93 
94  bool res;
95  res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,named_hv); printf("Is Valid: %s\n",res?"Yes":"No");
96  res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,anon_hf); printf("Is Valid: %s\n",res?"Yes":"No");
97  res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,hm); printf("Is Valid: %s\n",res?"Yes":"No");
100  res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,named_hv); printf("Is Valid: %s\n",res?"Yes":"No");
101  res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,anon_hf); printf("Is Valid: %s\n",res?"Yes":"No");
102  res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,hm); printf("Is Valid: %s\n",res?"Yes":"No");
103 }