VCG Library
trimesh_optional.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 ****************************************************************************/
23 
34 #include<vcg/complex/complex.h>
35 
36 #include<vcg/complex/algorithms/create/platonic.h>
37 #include<vcg/complex/algorithms/update/curvature.h>
38 #include <vcg/complex/algorithms/refine_loop.h>
39 #include <wrap/io_trimesh/export_off.h>
40 
41 class MyFace;
42 class MyVertex;
43 
44 struct MyUsedTypes: public vcg::UsedTypes<
45  vcg::Use<MyVertex>::AsVertexType,
46  vcg::Use<MyFace >::AsFaceType>{};
47 
48 class MyVertex : public vcg::Vertex< MyUsedTypes,
49  vcg::vertex::Coord3f, vcg::vertex::Qualityf,
50  vcg::vertex::Color4b, vcg::vertex::BitFlags,
51  vcg::vertex::Normal3f, vcg::vertex::VFAdj >{};
52 
53 class MyFace : public vcg::Face< MyUsedTypes,
54  vcg::face::FFAdj, vcg::face::VFAdj,
55  vcg::face::Color4b, vcg::face::VertexRef,
56  vcg::face::BitFlags, vcg::face::Normal3f > {};
57 class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex >, std::vector<MyFace > > {};
58 
59 
60 class MyVertexOcf;
61 class MyFaceOcf;
62 
63 struct MyUsedTypesOcf: public vcg::UsedTypes<
64  vcg::Use<MyVertexOcf>::AsVertexType,
65  vcg::Use<MyFaceOcf>::AsFaceType>{};
66 
67 class MyVertexOcf : public vcg::Vertex< MyUsedTypesOcf,
68  vcg::vertex::InfoOcf, // <--- Note the use of the 'special' InfoOcf component
69  vcg::vertex::Coord3f, vcg::vertex::QualityfOcf,
70  vcg::vertex::Color4b, vcg::vertex::BitFlags,
71  vcg::vertex::Normal3f, vcg::vertex::VFAdjOcf >{};
72 
73 class MyFaceOcf : public vcg::Face< MyUsedTypesOcf,
74  vcg::face::InfoOcf, // <--- Note the use of the 'special' InfoOcf component
75  vcg::face::FFAdjOcf, vcg::face::VFAdjOcf,
76  vcg::face::Color4bOcf, vcg::face::VertexRef,
77  vcg::face::BitFlags, vcg::face::Normal3fOcf > {};
78 
79 // the mesh class must make use of the 'vector_ocf' containers instead of the classical std::vector
80 class MyMeshOcf : public vcg::tri::TriMesh< vcg::vertex::vector_ocf<MyVertexOcf>, vcg::face::vector_ocf<MyFaceOcf> > {};
81 
82 
83 using namespace vcg;
84 using namespace std;
85 
86 int main(int , char **)
87 {
88  MyMesh cm;
89  MyMeshOcf cmof;
90 
91  tri::Tetrahedron(cm);
92  tri::Tetrahedron(cmof);
93 
94  printf("Generated mesh has %i vertices and %i triangular faces\n",cm.VN(),cm.FN());
95 
96  assert(tri::HasFFAdjacency(cmof) == false);
97  cmof.face.EnableFFAdjacency();
98  assert(tri::HasFFAdjacency(cmof) == true);
99 
100  assert(tri::HasVFAdjacency(cmof) == false);
101  cmof.vert.EnableVFAdjacency();
102  cmof.face.EnableVFAdjacency();
103  assert(tri::HasVFAdjacency(cmof) == true);
104 
107 
110 
112  cmof.face.EnableNormal(); // remove this line and you will throw an exception for a missing 'normal' component
114 
115  cmof.vert.EnableQuality();
116 
117  tri::UpdateColor<MyMeshOcf>::PerVertexConstant(cmof,Color4b::LightGray);
118  printf("cmof IsColorEnabled() %s\n",cmof.face.back().IsColorEnabled()?"Yes":"No");
119  cmof.face.EnableColor();
120  tri::UpdateColor<MyMeshOcf>::PerFaceConstant(cmof,Color4b::LightGray);
121  cmof.vert[0].C()=Color4b::Red;
122 
123  printf("cmof IsColorEnabled() %s\n",cmof.face.back().IsColorEnabled()?"Yes":"No");
124  printf("cm IsColorEnabled() %s\n",cm.face.back().IsColorEnabled()?"Yes":"No");
125  printf("cm IsColorEnabled() %s\n",cm.face.back().HasColor()?"Yes":"No");
126 
127  printf("Normal of face 0 is %f %f %f\n\n",cm.face[0].N()[0],cm.face[0].N()[1],cm.face[0].N()[2]);
128  int t0=0,t1=0,t2=0;
129  while(float(t1-t0)/CLOCKS_PER_SEC < 0.1f)
130  {
131  t0=clock();
132  tri::RefineOddEven<MyMesh> (cm, tri::OddPointLoop<MyMesh>(cm), tri::EvenPointLoop<MyMesh>(), 0);
133  t1=clock();
134  tri::RefineOddEven<MyMeshOcf> (cmof, tri::OddPointLoop<MyMeshOcf>(cmof), tri::EvenPointLoop<MyMeshOcf>(), 0);
135  t2=clock();
136  }
137  printf("Last Iteration: Refined a tetra up to a mesh of %i faces in:\n"
138  "Standard Component %5.2f sec\n"
139  "OCF Component %5.2f sec\n",cm.FN(),float(t1-t0)/CLOCKS_PER_SEC,float(t2-t1)/CLOCKS_PER_SEC);
140  tri::io::ExporterOFF<MyMeshOcf>::Save(cmof,"test.off",tri::io::Mask::IOM_VERTCOLOR);
141 
142  return 0;
143 }