trimesh_voronoi.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-2009                                           \/)\/    *
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 ****************************************************************************/
00023 #include <stdio.h>
00024 #include<vcg/complex/complex.h>
00025 #include<vcg/complex/algorithms/create/platonic.h>
00026 #include<wrap/io_trimesh/import_ply.h>
00027 #include<wrap/io_trimesh/export_off.h>
00028 #include<wrap/io_trimesh/export_ply.h>
00029 #include<wrap/io_trimesh/export_dxf.h>
00030 #include<vcg/complex/algorithms/point_sampling.h>
00031 #include<vcg/complex/algorithms/voronoi_processing.h>
00032 
00033 
00034 using namespace vcg;
00035 using namespace std;
00036 
00037 class MyEdge;
00038 class MyFace;
00039 class MyVertex;
00040 struct MyUsedTypes : public UsedTypes<  Use<MyVertex>   ::AsVertexType,
00041                                         Use<MyEdge>     ::AsEdgeType,
00042                                         Use<MyFace>     ::AsFaceType>{};
00043 
00044 class MyVertex  : public Vertex<MyUsedTypes,  vertex::Coord3f, vertex::Normal3f, vertex::VFAdj, vertex::Qualityf, vertex::Color4b, vertex::BitFlags  >{};
00045 class MyFace    : public Face< MyUsedTypes,   face::VertexRef, face::Normal3f, face::Mark, face::BitFlags, face::VFAdj, face::FFAdj > {};
00046 class MyEdge    : public Edge< MyUsedTypes, edge::VertexRef, edge::BitFlags>{};
00047 class MyMesh    : public tri::TriMesh< vector<MyVertex>, vector<MyEdge>, vector<MyFace>   > {};
00048 
00049 int main( int argc, char **argv )
00050 {
00051   MyMesh baseMesh,voronoiMesh, voronoiPoly, delaunayMesh;
00052   if(argc < 6 )
00053   {
00054     printf("Usage: trimesh_voronoi mesh [sampleNum] voronoiRelaxIter delaunayRefinementStep delaunayRelaxStep \n");
00055      return -1;
00056   }
00057   int sampleNum = atoi(argv[2]);
00058   int iterNum   = atoi(argv[3]);
00059 
00060   int refineStep   = atoi(argv[4]);
00061   int relaxStep   = atoi(argv[5]);
00062 
00063   int t0=clock();
00064   int ret= tri::io::ImporterPLY<MyMesh>::Open(baseMesh,argv[1]);
00065   if(ret!=0)
00066   {
00067     printf("Unable to open %s for '%s'\n",argv[1],tri::io::ImporterPLY<MyMesh>::ErrorMsg(ret));
00068     return -1;
00069   }
00070   tri::VoronoiProcessingParameter vpp;
00071 
00072   tri::io::ImporterPLY<MyMesh>::Open(baseMesh,argv[1]);
00073   int t1=clock();
00074   printf("Read         %30s (%7i vn %7i fn) in %6.3f \n",argv[1],baseMesh.vn,baseMesh.fn,float(t1-t0)/CLOCKS_PER_SEC);
00075 
00076 
00077   vector<Point3f> pointVec;
00078   float radius;
00079   tri::PoissonSampling<MyMesh>(baseMesh,pointVec,sampleNum,radius);
00080   vector<MyVertex *> seedVec;
00081   tri::VoronoiProcessing<MyMesh>::PreprocessForVoronoi(baseMesh,radius,vpp);
00082   tri::VoronoiProcessing<MyMesh>::SeedToVertexConversion(baseMesh,pointVec,seedVec);
00083 
00084   int t2=clock();
00085   printf("Preprocessed %30s (%7i vn %7i fn) Computed   %i seed (asked %i) (radius %f) in %6.3f\n",
00086          argv[1],baseMesh.vn,baseMesh.fn, seedVec.size(), sampleNum, radius, float(t2-t1)/CLOCKS_PER_SEC);
00087 
00088   tri::EuclideanDistance<MyMesh> df;
00089   vpp.geodesicRelaxFlag=false;
00090   int actualIter = tri::VoronoiProcessing<MyMesh>::VoronoiRelaxing(baseMesh, seedVec, iterNum, df, vpp);
00091 
00092   int t3=clock();
00093   printf("relaxed %lu seeds for %i(up to %i) iterations in %f secs\n",
00094          seedVec.size(), actualIter, iterNum,float(t3-t2)/CLOCKS_PER_SEC);
00095 
00096   tri::io::ExporterPLY<MyMesh>::Save(baseMesh,"baseMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY );
00097   if(tri::VoronoiProcessing<MyMesh>::CheckVoronoiTopology(baseMesh,seedVec))
00098   {
00099      tri::VoronoiProcessing<MyMesh>::ConvertVoronoiDiagramToMesh(baseMesh,voronoiMesh,voronoiPoly,seedVec, vpp);
00100   }
00101   else
00102   {
00103     printf("WARNING some voronoi region are not disk like; the resulting delaunay triangulation is not manifold.\n");
00104     refineStep=1;
00105   }
00106 
00107   tri::VoronoiProcessing<MyMesh>::ConvertDelaunayTriangulationToMesh(baseMesh,delaunayMesh,seedVec,true);
00108   tri::io::ExporterPLY<MyMesh>::Save(delaunayMesh,"delaunayBaseMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTFLAGS,false );
00109   tri::VoronoiProcessing<MyMesh>::RelaxRefineTriangulationSpring(baseMesh,delaunayMesh,refineStep,relaxStep);
00110 
00111   int t4=clock();
00112   printf("Refined %i times and relaxed %i to a %i v %i f mesh in %f secs\n",
00113          refineStep, relaxStep, delaunayMesh.vn,delaunayMesh.fn,float(t4-t3)/CLOCKS_PER_SEC);
00114 
00115   tri::io::ExporterPLY<MyMesh>::Save(baseMesh,"baseMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY );
00116   tri::io::ExporterPLY<MyMesh>::Save(voronoiMesh,"voronoiMesh.ply",tri::io::Mask::IOM_VERTCOLOR );
00117   tri::io::ExporterPLY<MyMesh>::Save(delaunayMesh,"delaunayMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTFLAGS,false );
00118   tri::io::ExporterPLY<MyMesh>::Save(voronoiPoly,"voronoiPoly.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_EDGEINDEX,false);
00119   return 0;
00120 }


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