From 2744e1330fec24f2773fdce2211b11791acf5032 Mon Sep 17 00:00:00 2001 From: cignoni Date: Tue, 4 Mar 2014 00:41:54 +0000 Subject: [PATCH] Added new sample on voronoi processing --- .../trimesh_voronoi/trimesh_voronoi.cpp | 123 ++++++++++++++++++ .../trimesh_voronoi/trimesh_voronoi.pro | 8 ++ 2 files changed, 131 insertions(+) create mode 100644 apps/sample/trimesh_voronoi/trimesh_voronoi.cpp create mode 100644 apps/sample/trimesh_voronoi/trimesh_voronoi.pro diff --git a/apps/sample/trimesh_voronoi/trimesh_voronoi.cpp b/apps/sample/trimesh_voronoi/trimesh_voronoi.cpp new file mode 100644 index 00000000..eeb9037c --- /dev/null +++ b/apps/sample/trimesh_voronoi/trimesh_voronoi.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** +* 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); + 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); + + MyMesh::PerVertexAttributeHandle sources; + sources= tri::Allocator::GetPerVertexAttribute (baseMesh,"sources"); + tri::Geodesic::Compute(baseMesh, seedVec, df,std::numeric_limits::max(),0,&sources); + + 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; +} diff --git a/apps/sample/trimesh_voronoi/trimesh_voronoi.pro b/apps/sample/trimesh_voronoi/trimesh_voronoi.pro new file mode 100644 index 00000000..e314bdda --- /dev/null +++ b/apps/sample/trimesh_voronoi/trimesh_voronoi.pro @@ -0,0 +1,8 @@ +include(../common.pri) +TARGET = trimesh_voronoi +SOURCES += trimesh_voronoi.cpp ../../../wrap/ply/plylib.cpp + +CONFIG(release, debug | release){ +DEFINES += NDEBUG +} +