vcglib/apps/sample/trimesh_optional/trimesh_optional.cpp

148 lines
6.2 KiB
C++
Raw Normal View History

/****************************************************************************
* 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. *
* *
****************************************************************************/
2013-11-25 14:12:09 +01:00
/*! \file trimesh_optional.cpp
\ingroup code_sample
\brief the minimal example of using the \ref optional_component "Optional Components".
This file shows how to dynamically allocate component that you do not need most of the time.
*/
#include<vcg/complex/complex.h>
#include<vcg/complex/algorithms/create/platonic.h>
#include<vcg/complex/algorithms/update/topology.h>
#include<vcg/complex/algorithms/update/flag.h>
#include<vcg/complex/algorithms/update/normal.h>
#include<vcg/complex/algorithms/update/bounding.h>
#include<vcg/complex/algorithms/update/curvature.h>
#include <vcg/complex/algorithms/refine_loop.h>
#include <wrap/io_trimesh/export_off.h>
2013-11-25 14:12:09 +01:00
class MyFace;
class MyVertex;
struct MyUsedTypes: public vcg::UsedTypes<
vcg::Use<MyVertex>::AsVertexType,
vcg::Use<MyFace >::AsFaceType>{};
class MyVertex : public vcg::Vertex< MyUsedTypes,
vcg::vertex::Coord3f, vcg::vertex::Qualityf,
vcg::vertex::Color4b, vcg::vertex::BitFlags,
vcg::vertex::Normal3f, vcg::vertex::VFAdj >{};
class MyFace : public vcg::Face< MyUsedTypes,
vcg::face::FFAdj, vcg::face::VFAdj,
vcg::face::Color4b, vcg::face::VertexRef,
vcg::face::BitFlags, vcg::face::Normal3f > {};
class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex >, std::vector<MyFace > > {};
2013-11-25 14:12:09 +01:00
class MyVertexOcf;
class MyFaceOcf;
2013-11-25 14:12:09 +01:00
struct MyUsedTypesOcf: public vcg::UsedTypes<
vcg::Use<MyVertexOcf>::AsVertexType,
vcg::Use<MyFaceOcf>::AsFaceType>{};
2013-11-25 14:12:09 +01:00
class MyVertexOcf : public vcg::Vertex< MyUsedTypesOcf,
vcg::vertex::InfoOcf, // <--- Note the use of the 'special' InfoOcf component
vcg::vertex::Coord3f, vcg::vertex::QualityfOcf,
vcg::vertex::Color4b, vcg::vertex::BitFlags,
vcg::vertex::Normal3f, vcg::vertex::VFAdjOcf >{};
class MyFaceOcf : public vcg::Face< MyUsedTypesOcf,
vcg::face::InfoOcf, // <--- Note the use of the 'special' InfoOcf component
vcg::face::FFAdjOcf, vcg::face::VFAdjOcf,
vcg::face::Color4bOcf, vcg::face::VertexRef,
vcg::face::BitFlags, vcg::face::Normal3fOcf > {};
// the mesh class must make use of the 'vector_ocf' containers instead of the classical std::vector
class MyMeshOcf : public vcg::tri::TriMesh< vcg::vertex::vector_ocf<MyVertexOcf>, vcg::face::vector_ocf<MyFaceOcf> > {};
using namespace vcg;
using namespace std;
[ 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
int main(int , char **)
{
2013-11-25 14:12:09 +01:00
MyMesh cm;
MyMeshOcf cmof;
tri::Tetrahedron(cm);
2005-12-02 01:49:50 +01:00
tri::Tetrahedron(cmof);
2013-11-25 14:12:09 +01:00
printf("Generated mesh has %i vertices and %i triangular faces\n",cm.VN(),cm.FN());
2013-11-25 14:12:09 +01:00
assert(tri::HasFFAdjacency(cmof) == false);
cmof.face.EnableFFAdjacency();
assert(tri::HasFFAdjacency(cmof) == true);
2013-11-25 14:12:09 +01:00
assert(tri::HasVFAdjacency(cmof) == false);
cmof.vert.EnableVFAdjacency();
cmof.face.EnableVFAdjacency();
assert(tri::HasVFAdjacency(cmof) == true);
2013-11-25 14:12:09 +01:00
tri::UpdateTopology<MyMesh >::FaceFace(cm);
tri::UpdateTopology<MyMeshOcf>::FaceFace(cmof);
2013-11-25 14:12:09 +01:00
tri::UpdateFlags<MyMesh >::FaceBorderFromFF(cm);
tri::UpdateFlags<MyMeshOcf>::FaceBorderFromFF(cmof);
tri::UpdateNormal<MyMesh >::PerVertexPerFace(cm);
cmof.face.EnableNormal(); // remove this line and you will throw an exception for a missing 'normal' component
tri::UpdateNormal<MyMeshOcf>::PerVertexPerFace(cmof);
cmof.vert.EnableQuality();
2013-11-25 14:12:09 +01:00
tri::UpdateColor<MyMeshOcf>::PerVertexConstant(cmof,Color4b::LightGray);
printf("cmof IsColorEnabled() %s\n",cmof.face.back().IsColorEnabled()?"Yes":"No");
cmof.face.EnableColor();
tri::UpdateColor<MyMeshOcf>::PerFaceConstant(cmof,Color4b::LightGray);
cmof.vert[0].C()=Color4b::Red;
2013-11-25 14:12:09 +01:00
printf("cmof IsColorEnabled() %s\n",cmof.face.back().IsColorEnabled()?"Yes":"No");
printf("cm IsColorEnabled() %s\n",cm.face.back().IsColorEnabled()?"Yes":"No");
printf("cm IsColorEnabled() %s\n",cm.face.back().HasColor()?"Yes":"No");
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]);
int t0=0,t1=0,t2=0;
2013-11-25 14:12:09 +01:00
while(float(t1-t0)/CLOCKS_PER_SEC < 0.1f)
{
t0=clock();
2013-11-25 14:12:09 +01:00
tri::RefineOddEven<MyMesh> (cm, tri::OddPointLoop<MyMesh>(cm), tri::EvenPointLoop<MyMesh>(), 0);
t1=clock();
2013-11-25 14:12:09 +01:00
tri::RefineOddEven<MyMeshOcf> (cmof, tri::OddPointLoop<MyMeshOcf>(cmof), tri::EvenPointLoop<MyMeshOcf>(), 0);
t2=clock();
}
2013-11-25 14:12:09 +01:00
printf("Last Iteration: Refined a tetra up to a mesh of %i faces in:\n"
"Standard Component %5.2f sec\n"
"OCF Component %5.2f sec\n",cm.FN(),float(t1-t0)/CLOCKS_PER_SEC,float(t2-t1)/CLOCKS_PER_SEC);
tri::io::ExporterOFF<MyMeshOcf>::Save(cmof,"test.off",tri::io::Mask::IOM_VERTCOLOR);
return 0;
}