2012-10-09 09:12:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2004-2012 \/)\/ *
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2012-10-11 12:40:07 +02:00
|
|
|
/*! \file trimesh_attribute.cpp
|
|
|
|
\ingroup code_sample
|
|
|
|
|
|
|
|
\brief the minimal example of using the attributes
|
|
|
|
|
|
|
|
Attributes are a simple mechanism to associate user-defined 'attributes' to the simplicies and to the mesh.
|
|
|
|
\ref attributes for more Details
|
|
|
|
*/
|
2008-10-28 10:18:52 +01:00
|
|
|
|
2011-04-01 19:07:57 +02:00
|
|
|
#include<vcg/complex/complex.h>
|
[ Changes in definition of TriMesh: PART I ]
Note for the developers: the change to make to existing projects is very little
but strictly necessary to compile. This change IS NOT backward compliant.
==== OLD ==== way to define a TriMesh:
// forward declarations
class MyVertex;
class MyEdge;
class MyFace;
class MyVertex: public VertexSimp2 < MyVertex, MyEdge, MyFace, vertex::Coord3f,...other components>{};
class MyFace: public FaceSimp2 < MyVertex, MyEdge, MyFace, face::VertexRef,...other components>{};
class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{};
==== NEW ==== way to define a TriMesh:
// forward declarations
class MyVertex;
class MyEdge;
class MyFace;
// declaration of which types is used as VertexType, which type is used as FaceType and so on...
class MyUsedTypes: public vcg::UsedType < vcg::Use<MyVertex>::AsVertexType,
vcg::Use<MyFace>::AsFaceType>{};
class MyVertex: public Vertex < MyUsedTypes, vertex::Coord3f,...other components>{};
class MyFace: public Face < MyUsedTypes, face::VertexRef,...other components>{};
class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{};
===== classes introduced
[vcg::UsedType] : it is a class containing all the types that must be passed to the definition of Vertex, Face, Edge... This
class replaces the list of typenames to pass as first templates and the need to specify the maximal simplicial. So
<MyVertex, MyEdge, MyFace becomes <MyUsedTypes<
and
VertexSimp2 becomes Vertex
[vcg::Use] : an auxiliary class to give a simple way to specify the role of a type
Note 2: the order of templates parameters to vcg::UsedTypes is unimportant, e.g:
class MyUsedTypes: public vcg::UsedType <vcg::Use<MyVertex>::AsVertexType,
vcg::Use<MyEdge>::AsEdgeType,
vcg::Use<MyFace>::AsFaceType>{};
is the same as:
class MyUsedTypes: public vcg::UsedType <vcg::Use<MyFace>::AsFaceType,
vcg::Use<MyEdge>::AsEdgeType,
vcg::Use<MyVertex>::AsVertexType>{};
Note 3: you only need to specify the type you use. If you do not have edges you do not need
to include vcg::Use<MyEdge>::AsEdgeType in the template list of UsedTypes.
==== the Part II will be a tiny change to the class TriMesh it self.
2010-03-15 11:44:40 +01:00
|
|
|
class MyEdge;
|
2008-10-28 10:18:52 +01:00
|
|
|
class MyFace;
|
|
|
|
class MyVertex;
|
2012-10-16 11:08:19 +02:00
|
|
|
struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<MyVertex> ::AsVertexType,
|
|
|
|
vcg::Use<MyFace> ::AsFaceType>{};
|
[ Changes in definition of TriMesh: PART I ]
Note for the developers: the change to make to existing projects is very little
but strictly necessary to compile. This change IS NOT backward compliant.
==== OLD ==== way to define a TriMesh:
// forward declarations
class MyVertex;
class MyEdge;
class MyFace;
class MyVertex: public VertexSimp2 < MyVertex, MyEdge, MyFace, vertex::Coord3f,...other components>{};
class MyFace: public FaceSimp2 < MyVertex, MyEdge, MyFace, face::VertexRef,...other components>{};
class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{};
==== NEW ==== way to define a TriMesh:
// forward declarations
class MyVertex;
class MyEdge;
class MyFace;
// declaration of which types is used as VertexType, which type is used as FaceType and so on...
class MyUsedTypes: public vcg::UsedType < vcg::Use<MyVertex>::AsVertexType,
vcg::Use<MyFace>::AsFaceType>{};
class MyVertex: public Vertex < MyUsedTypes, vertex::Coord3f,...other components>{};
class MyFace: public Face < MyUsedTypes, face::VertexRef,...other components>{};
class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{};
===== classes introduced
[vcg::UsedType] : it is a class containing all the types that must be passed to the definition of Vertex, Face, Edge... This
class replaces the list of typenames to pass as first templates and the need to specify the maximal simplicial. So
<MyVertex, MyEdge, MyFace becomes <MyUsedTypes<
and
VertexSimp2 becomes Vertex
[vcg::Use] : an auxiliary class to give a simple way to specify the role of a type
Note 2: the order of templates parameters to vcg::UsedTypes is unimportant, e.g:
class MyUsedTypes: public vcg::UsedType <vcg::Use<MyVertex>::AsVertexType,
vcg::Use<MyEdge>::AsEdgeType,
vcg::Use<MyFace>::AsFaceType>{};
is the same as:
class MyUsedTypes: public vcg::UsedType <vcg::Use<MyFace>::AsFaceType,
vcg::Use<MyEdge>::AsEdgeType,
vcg::Use<MyVertex>::AsVertexType>{};
Note 3: you only need to specify the type you use. If you do not have edges you do not need
to include vcg::Use<MyEdge>::AsEdgeType in the template list of UsedTypes.
==== the Part II will be a tiny change to the class TriMesh it self.
2010-03-15 11:44:40 +01:00
|
|
|
|
2012-10-16 11:08:19 +02:00
|
|
|
class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f,vcg::vertex::Normal3f>{};
|
|
|
|
class MyFace : public vcg::Face< MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f> {};
|
2008-10-28 10:18:52 +01:00
|
|
|
|
2012-10-15 16:09:24 +02:00
|
|
|
class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> > {};
|
2009-03-20 11:24:13 +01:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
MyMesh m;
|
|
|
|
// add a per-vertex attribute with type float named "Irradiance"
|
2013-01-30 18:18:55 +01:00
|
|
|
MyMesh::PerVertexAttributeHandle<float> named_hv = vcg::tri::Allocator<MyMesh>:: GetPerVertexAttribute<float> (m,std::string("Irradiance"));
|
2009-03-20 11:24:13 +01:00
|
|
|
|
|
|
|
// add a per-vertex attribute with type float named "Radiosity"
|
2013-01-30 18:18:55 +01:00
|
|
|
vcg::tri::Allocator<MyMesh>:: GetPerVertexAttribute<float> (m,std::string("Radiosity"));
|
2009-03-20 11:24:13 +01:00
|
|
|
|
|
|
|
// add a per-vertex attribute with type bool and no name specified
|
2013-01-30 18:18:55 +01:00
|
|
|
MyMesh::PerVertexAttributeHandle<bool> anon_hv = vcg::tri::Allocator<MyMesh>:: GetPerVertexAttribute<bool> (m);
|
2009-03-20 11:24:13 +01:00
|
|
|
|
2012-10-09 10:43:35 +02:00
|
|
|
// add a per-face attribute with type bool and no name specified
|
2013-01-30 18:18:55 +01:00
|
|
|
MyMesh::PerFaceAttributeHandle<bool> anon_hf = vcg::tri::Allocator<MyMesh>:: GetPerFaceAttribute<bool> (m);
|
2009-03-20 11:24:13 +01:00
|
|
|
|
|
|
|
MyMesh::VertexIterator vi; int i = 0;
|
|
|
|
for(vi = m.vert.begin(); vi != m.vert.end(); ++vi,++i){
|
2012-10-13 23:35:42 +02:00
|
|
|
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
|
2009-03-20 11:24:13 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 18:18:55 +01:00
|
|
|
vcg::tri::Allocator<MyMesh>::ClearPerVertexAttribute<float>(m,named_hv);
|
|
|
|
|
2012-10-15 09:52:40 +02:00
|
|
|
// query if an attribute is present or not
|
2012-10-15 16:09:24 +02:00
|
|
|
bool hasRadiosity = vcg::tri::HasPerVertexAttribute(m,"Radiosity");
|
2012-10-13 23:35:42 +02:00
|
|
|
|
2012-10-15 09:52:40 +02:00
|
|
|
// obtain another handle of a previously attribute
|
2013-01-30 18:18:55 +01:00
|
|
|
MyMesh::PerVertexAttributeHandle<float> ret_hv = vcg::tri::Allocator<MyMesh>:: FindPerVertexAttribute<float>(m,"Radiosity");
|
2012-10-13 23:35:42 +02:00
|
|
|
|
|
|
|
// you can also have PerMesh attributes
|
2013-01-30 18:18:55 +01:00
|
|
|
MyMesh::PerMeshAttributeHandle<int> hm = vcg::tri::Allocator<MyMesh>:: GetPerMeshAttribute<int> (m,std::string("ADummyIntegerAttribute"));
|
2009-03-20 11:24:13 +01:00
|
|
|
|
2012-10-13 23:35:42 +02:00
|
|
|
// PerMesh attributes are accessed directly using the handle itself
|
|
|
|
hm() = 10;
|
2012-10-11 12:40:07 +02:00
|
|
|
|
2012-10-15 09:52:40 +02:00
|
|
|
// delete an attribute by name
|
2012-10-15 16:09:24 +02:00
|
|
|
vcg::tri::Allocator<MyMesh>::DeletePerVertexAttribute(m,"Radiosity");
|
2009-03-20 11:24:13 +01:00
|
|
|
|
2012-10-15 09:52:40 +02:00
|
|
|
// delete an attribute by handle
|
2012-10-15 16:09:24 +02:00
|
|
|
vcg::tri::Allocator<MyMesh>::DeletePerVertexAttribute(m,anon_hv);
|
2009-03-20 11:24:13 +01:00
|
|
|
|
2012-10-13 23:35:42 +02:00
|
|
|
bool res;
|
2012-10-15 16:09:24 +02:00
|
|
|
res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,named_hv); printf("Is Valid: %s\n",res?"Yes":"No");
|
|
|
|
res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,anon_hf); printf("Is Valid: %s\n",res?"Yes":"No");
|
|
|
|
res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,hm); printf("Is Valid: %s\n",res?"Yes":"No");
|
|
|
|
vcg::tri::Allocator<MyMesh>::DeletePerVertexAttribute(m,ret_hv);
|
|
|
|
vcg::tri::Allocator<MyMesh>::DeletePerFaceAttribute(m,anon_hf);
|
|
|
|
res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,named_hv); printf("Is Valid: %s\n",res?"Yes":"No");
|
|
|
|
res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,anon_hf); printf("Is Valid: %s\n",res?"Yes":"No");
|
|
|
|
res = vcg::tri::Allocator<MyMesh>::IsValidHandle(m,hm); printf("Is Valid: %s\n",res?"Yes":"No");
|
2009-03-20 11:24:13 +01:00
|
|
|
}
|