From 6760de8bdd856327b0b42291a5f09cb2020bff05 Mon Sep 17 00:00:00 2001 From: cignoni Date: Sat, 13 Oct 2012 21:35:42 +0000 Subject: [PATCH] added per mesh attribute example to the trimesh_attribute sample --- .../trimesh_attribute/trimesh_attribute.cpp | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/apps/sample/trimesh_attribute/trimesh_attribute.cpp b/apps/sample/trimesh_attribute/trimesh_attribute.cpp index a846fe7e..b83426b2 100644 --- a/apps/sample/trimesh_attribute/trimesh_attribute.cpp +++ b/apps/sample/trimesh_attribute/trimesh_attribute.cpp @@ -31,61 +31,66 @@ Attributes are a simple mechanism to associate user-defined 'attributes' to the */ #include - +using namespace vcg; class MyEdge; class MyFace; class MyVertex; -struct MyUsedTypes : public vcg::UsedTypes< vcg::Use ::AsVertexType, - vcg::Use ::AsFaceType>{}; +struct MyUsedTypes : public UsedTypes< Use ::AsVertexType, + Use ::AsFaceType>{}; -class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f,vcg::vertex::Normal3f>{}; -class MyFace : public vcg::Face< MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f> {}; +class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f,vertex::Normal3f>{}; +class MyFace : public Face< MyUsedTypes, face::VertexRef, face::Normal3f> {}; -class MyMesh : public vcg::tri::TriMesh< std::vector, std::vector > {}; +class MyMesh : public tri::TriMesh< std::vector, std::vector > {}; int main() { MyMesh m; - //...here m is filled - - // add a per-vertex attribute with type float named "Irradiance" - MyMesh::PerVertexAttributeHandle ih = vcg::tri::Allocator::AddPerVertexAttribute (m,std::string("Irradiance")); + MyMesh::PerVertexAttributeHandle named_hv = tri::Allocator::AddPerVertexAttribute (m,std::string("Irradiance")); // add a per-vertex attribute with type float named "Radiosity" - vcg::tri::Allocator::AddPerVertexAttribute (m,std::string("Radiosity")); + tri::Allocator::AddPerVertexAttribute (m,std::string("Radiosity")); // add a per-vertex attribute with type bool and no name specified - MyMesh::PerVertexAttributeHandle blocked_h = vcg::tri::Allocator::AddPerVertexAttribute (m); + MyMesh::PerVertexAttributeHandle anon_hv = tri::Allocator::AddPerVertexAttribute (m); // add a per-face attribute with type bool and no name specified - MyMesh::PerFaceAttributeHandle blocked_hf = vcg::tri::Allocator::AddPerFaceAttribute (m); + MyMesh::PerFaceAttributeHandle anon_hf = tri::Allocator::AddPerFaceAttribute (m); MyMesh::VertexIterator vi; int i = 0; for(vi = m.vert.begin(); vi != m.vert.end(); ++vi,++i){ - ih[vi] = 1.0f; // [] operator takes a iterator - ih[*vi] = 1.0f; // or a MyMesh::VertexType object - ih[&*vi]= 1.0f; // or a pointer to it - ih[i] = 1.0f; // or an integer index + named_hv[vi] = 1.0f; // [] operator takes a iterator + named_hv[*vi] = 1.0f; // or a MyMesh::VertexType object + named_hv[&*vi]= 1.0f; // or a pointer to it + named_hv[i] = 1.0f; // or an integer index } // you can query if an attribute is present or not - bool hasRadiosity = vcg::tri::HasPerVertexAttribute(m,"Radiosity"); + bool hasRadiosity = tri::HasPerVertexAttribute(m,"Radiosity"); - // Once created with AddPerVertexAttribute, an handle to the attribute can be obtained as follows - MyMesh::PerVertexAttributeHandle rh = vcg::tri::Allocator::GetPerVertexAttribute(m,"Radiosity"); + // Once created with AddPerVertexAttribute, an handle to the attribute can be obtained as follows + MyMesh::PerVertexAttributeHandle ret_hv = tri::Allocator::GetPerVertexAttribute(m,"Radiosity"); - // you can delete an attibute by name - vcg::tri::Allocator::DeletePerVertexAttribute(m,"Radiosity"); + // you can also have PerMesh attributes + MyMesh::PerMeshAttributeHandle hm = tri::Allocator::AddPerMeshAttribute (m,std::string("ADummyIntegerAttribute")); - // you can delete an attibute by handle - vcg::tri::Allocator::DeletePerVertexAttribute(m,blocked_h); + // PerMesh attributes are accessed directly using the handle itself + hm() = 10; - bool res ; - res = vcg::tri::Allocator::IsValidHandle(m,ih);printf("%d\n",res); - res = vcg::tri::Allocator::IsValidHandle(m,blocked_hf);printf("%d\n",res); - vcg::tri::Allocator::DeletePerVertexAttribute(m,ih); - vcg::tri::Allocator::DeletePerFaceAttribute(m,blocked_hf); - res = vcg::tri::Allocator::IsValidHandle(m,ih);printf("%d\n",res); - res = vcg::tri::Allocator::IsValidHandle(m,blocked_hf);printf("%d\n",res); + // you can delete an attribute by name + tri::Allocator::DeletePerVertexAttribute(m,"Radiosity"); + + // you can delete an attribute by handle + tri::Allocator::DeletePerVertexAttribute(m,anon_hv); + + bool res; + res = tri::Allocator::IsValidHandle(m,named_hv); printf("Is Valid: %s\n",res?"Yes":"No"); + res = tri::Allocator::IsValidHandle(m,anon_hf); printf("Is Valid: %s\n",res?"Yes":"No"); + res = tri::Allocator::IsValidHandle(m,hm); printf("Is Valid: %s\n",res?"Yes":"No"); + tri::Allocator::DeletePerVertexAttribute(m,ret_hv); + tri::Allocator::DeletePerFaceAttribute(m,anon_hf); + res = tri::Allocator::IsValidHandle(m,named_hv); printf("Is Valid: %s\n",res?"Yes":"No"); + res = tri::Allocator::IsValidHandle(m,anon_hf); printf("Is Valid: %s\n",res?"Yes":"No"); + res = tri::Allocator::IsValidHandle(m,hm); printf("Is Valid: %s\n",res?"Yes":"No"); }