/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004-2009 \/)\/ * * 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. * * * ****************************************************************************/ #include #include #include #include #include #include #include #include #include using namespace vcg; using namespace std; class MyEdge; class MyFace; class MyVertex; struct MyUsedTypes : public UsedTypes< Use ::AsVertexType, Use ::AsEdgeType, Use ::AsFaceType>{}; class MyVertex : public Vertex{}; class MyFace : public Face< MyUsedTypes, face::VertexRef, face::Normal3f, face::Mark, face::BitFlags, face::VFAdj, face::FFAdj > {}; class MyEdge : public Edge< MyUsedTypes, edge::VertexRef, edge::BitFlags>{}; class MyMesh : public tri::TriMesh< vector, vector, vector > {}; int main( int argc, char **argv ) { MyMesh baseMesh,voronoiMesh, voronoiPoly, delaunayMesh; if(argc < 6 ) { printf("Usage: trimesh_voronoi mesh [sampleNum] voronoiRelaxIter delaunayRefinementStep delaunayRelaxStep \n"); return -1; } int sampleNum = atoi(argv[2]); int iterNum = atoi(argv[3]); int refineStep = atoi(argv[4]); int relaxStep = atoi(argv[5]); int t0=clock(); int ret= tri::io::ImporterPLY::Open(baseMesh,argv[1]); if(ret!=0) { printf("Unable to open %s for '%s'\n",argv[1],tri::io::ImporterPLY::ErrorMsg(ret)); return -1; } tri::VoronoiProcessingParameter vpp; tri::io::ImporterPLY::Open(baseMesh,argv[1]); int t1=clock(); printf("Read %30s (%7i vn %7i fn) in %6.3f \n",argv[1],baseMesh.vn,baseMesh.fn,float(t1-t0)/CLOCKS_PER_SEC); vector pointVec; float radius; tri::PoissonSampling(baseMesh,pointVec,sampleNum,radius); vector seedVec; tri::VoronoiProcessing::PreprocessForVoronoi(baseMesh,radius,vpp); tri::VoronoiProcessing::SeedToVertexConversion(baseMesh,pointVec,seedVec); int t2=clock(); printf("Preprocessed %30s (%7i vn %7i fn) Computed %i seed (asked %i) (radius %f) in %6.3f\n", argv[1],baseMesh.vn,baseMesh.fn, seedVec.size(), sampleNum, radius, float(t2-t1)/CLOCKS_PER_SEC); tri::EuclideanDistance df; vpp.geodesicRelaxFlag=false; int actualIter = tri::VoronoiProcessing::VoronoiRelaxing(baseMesh, seedVec, iterNum, df, vpp); int t3=clock(); printf("relaxed %lu seeds for %i(up to %i) iterations in %f secs\n", seedVec.size(), actualIter, iterNum,float(t3-t2)/CLOCKS_PER_SEC); tri::io::ExporterPLY::Save(baseMesh,"baseMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY ); if(tri::VoronoiProcessing::CheckVoronoiTopology(baseMesh,seedVec)) { tri::VoronoiProcessing::ConvertVoronoiDiagramToMesh(baseMesh,voronoiMesh,voronoiPoly,seedVec, vpp); } else { printf("WARNING some voronoi region are not disk like; the resulting delaunay triangulation is not manifold.\n"); refineStep=1; } tri::VoronoiProcessing::ConvertDelaunayTriangulationToMesh(baseMesh,delaunayMesh,seedVec,true); tri::io::ExporterPLY::Save(delaunayMesh,"delaunayBaseMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTFLAGS,false ); tri::VoronoiProcessing::RelaxRefineTriangulationSpring(baseMesh,delaunayMesh,refineStep,relaxStep); int t4=clock(); printf("Refined %i times and relaxed %i to a %i v %i f mesh in %f secs\n", refineStep, relaxStep, delaunayMesh.vn,delaunayMesh.fn,float(t4-t3)/CLOCKS_PER_SEC); tri::io::ExporterPLY::Save(baseMesh,"baseMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY ); tri::io::ExporterPLY::Save(voronoiMesh,"voronoiMesh.ply",tri::io::Mask::IOM_VERTCOLOR ); tri::io::ExporterPLY::Save(delaunayMesh,"delaunayMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTFLAGS,false ); tri::io::ExporterPLY::Save(voronoiPoly,"voronoiPoly.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_EDGEINDEX,false); return 0; }