trimesh_allocate.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004-2012                                           \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00032 #include<vcg/complex/complex.h>
00033 #include<vcg/complex/algorithms/create/platonic.h>
00034 
00035 class MyEdge;
00036 class MyFace;
00037 class MyVertex;
00038 struct MyUsedTypes : public vcg::UsedTypes<     vcg::Use<MyVertex>              ::AsVertexType,
00039                                             vcg::Use<MyFace>                    ::AsFaceType>{};
00040 
00041 class MyVertex  : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f,vcg::vertex::Normal3f>{};
00042 class MyFace    : public vcg::Face< MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f> {};
00043 
00044 class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> > {};
00045 
00046 int main()
00047 {
00048   MyMesh m;
00049   MyMesh::VertexIterator vi = vcg::tri::Allocator<MyMesh>::AddVertices(m,3);
00050   MyMesh::FaceIterator fi = vcg::tri::Allocator<MyMesh>::AddFaces(m,1);
00051 
00052   MyMesh::VertexPointer ivp[4];
00053   ivp[0]=&*vi; vi->P()=MyMesh::CoordType ( 0.0, 0.0, 0.0); ++vi;
00054   ivp[1]=&*vi; vi->P()=MyMesh::CoordType ( 1.0, 0.0, 0.0); ++vi;
00055   ivp[2]=&*vi; vi->P()=MyMesh::CoordType ( 0.0, 1.0, 0.0); ++vi;
00056 
00057   fi->V(0)=ivp[0];
00058   fi->V(1)=ivp[1];
00059   fi->V(2)=ivp[2];
00060 
00061   // Alternative, more compact, method for adding a single vertex
00062   ivp[3]= &*vcg::tri::Allocator<MyMesh>::AddVertex(m,MyMesh::CoordType ( 1.0, 1.0, 0.0));
00063 
00064   // Alternative, more compact, method for adding a single face (once you have the vertex pointers)
00065   vcg::tri::Allocator<MyMesh>::AddFace(m, ivp[1],ivp[0],ivp[3]);
00066 
00067   // a potentially dangerous pointer to a mesh element
00068   MyMesh::FacePointer fp = &m.face[0];
00069   vcg::tri::Allocator<MyMesh>::PointerUpdater<MyMesh::FacePointer> pu;
00070 
00071   // now the fp pointer could be no more valid due to eventual re-allocation of the m.face vector.
00072   vcg::tri::Allocator<MyMesh>::AddVertices(m,3);
00073   vcg::tri::Allocator<MyMesh>::AddFaces(m,1,pu);
00074 
00075   // check if an update of the pointer is needed and do it.
00076   if(pu.NeedUpdate()) pu.Update(fp);
00077 
00078   // Now fill the mesh with an Icosahedron and then delete some faces
00079   vcg::tri::Icosahedron(m);
00080   vcg::tri::Allocator<MyMesh>::DeleteFace(m,m.face[1]);
00081   vcg::tri::Allocator<MyMesh>::DeleteFace(m,m.face[3]);
00082 
00083   // If you loop in a mesh with deleted elements you have to skip them!
00084   for(fi = m.face.begin(); fi!=m.face.end(); ++fi )
00085   {
00086      if(!fi->IsD()) //    <---- Check added
00087        {
00088         MyMesh::CoordType b = vcg::Barycenter(*fi);
00089        }
00090   }
00091 
00092   // WRONG WAY of iterating: FN() != m.face.size() if there are deleted elements
00093   for(int i=0;i<m.FN();++i)
00094   {
00095      if(!fi->IsD())
00096        {
00097         MyMesh::CoordType b = vcg::Barycenter(*fi);
00098        }
00099   }
00100 
00101   // To remove the elements marked as deleted use
00102   vcg::tri::Allocator<MyMesh>::CompactFaceVector(m);
00103   vcg::tri::Allocator<MyMesh>::CompactVertexVector(m);
00104 
00105   // To clean all the containers from deleted elements
00106   vcg::tri::Allocator<MyMesh>::CompactEveryVector(m);
00107 
00108   // finally lets copy this mesh onto another one.
00109   MyMesh m2;
00110   vcg::tri::Append<MyMesh,MyMesh>::MeshCopy(m2,m);
00111 
00112 }


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:38:13