VCG Library
trimesh_allocate.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 #include<vcg/complex/algorithms/create/platonic.h>
34 
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>{};
42 class MyFace : public vcg::Face< MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f> {};
43 
44 class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> > {};
45 
46 int main()
47 {
48  MyMesh m;
49  MyMesh::VertexIterator vi = vcg::tri::Allocator<MyMesh>::AddVertices(m,3);
50  MyMesh::FaceIterator fi = vcg::tri::Allocator<MyMesh>::AddFaces(m,1);
51 
52  MyMesh::VertexPointer ivp[4];
53  ivp[0]=&*vi; vi->P()=MyMesh::CoordType ( 0.0, 0.0, 0.0); ++vi;
54  ivp[1]=&*vi; vi->P()=MyMesh::CoordType ( 1.0, 0.0, 0.0); ++vi;
55  ivp[2]=&*vi; vi->P()=MyMesh::CoordType ( 0.0, 1.0, 0.0); ++vi;
56 
57  fi->V(0)=ivp[0];
58  fi->V(1)=ivp[1];
59  fi->V(2)=ivp[2];
60 
61  // Alternative, more compact, method for adding a single vertex
62  ivp[3]= &*vcg::tri::Allocator<MyMesh>::AddVertex(m,MyMesh::CoordType ( 1.0, 1.0, 0.0));
63 
64  // Alternative, more compact, method for adding a single face (once you have the vertex pointers)
65  vcg::tri::Allocator<MyMesh>::AddFace(m, ivp[1],ivp[0],ivp[3]);
66 
67  // a potentially dangerous pointer to a mesh element
68  MyMesh::FacePointer fp = &m.face[0];
69  vcg::tri::Allocator<MyMesh>::PointerUpdater<MyMesh::FacePointer> pu;
70 
71  // now the fp pointer could be no more valid due to eventual re-allocation of the m.face vector.
74 
75  // check if an update of the pointer is needed and do it.
76  if(pu.NeedUpdate()) pu.Update(fp);
77 
78  // Now fill the mesh with an Icosahedron and then delete some faces
79  vcg::tri::Icosahedron(m);
82 
83  // If you loop in a mesh with deleted elements you have to skip them!
84  MyMesh::CoordType b(0,0,0);
85  for(fi = m.face.begin(); fi!=m.face.end(); ++fi )
86  {
87  if(!fi->IsD()) // <---- Check added
88  {
89  b += vcg::Barycenter(*fi);
90  }
91  }
92 
93  // WRONG WAY of iterating: FN() != m.face.size() if there are deleted elements
94  for(int i=0;i<m.FN();++i)
95  {
96  if(!fi->IsD())
97  {
98  b += vcg::Barycenter(*fi);
99  }
100  }
101 
102  // To remove the elements marked as deleted use
105 
106  // To clean all the containers from deleted elements
108 
109  // finally lets copy this mesh onto another one.
110  MyMesh m2;
112 
113 }