VCG Library
trimesh_topological_cut.cpp
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2012 \/)\/ *
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 
24 #include<vcg/complex/complex.h>
25 #include<wrap/io_trimesh/import.h>
26 #include<wrap/io_trimesh/export.h>
27 
28 
29 #include<vcg/complex/algorithms/cut_tree.h>
30 #include<vcg/complex/algorithms/curve_on_manifold.h>
31 #include<vcg/complex/algorithms/crease_cut.h>
32 
33 using namespace vcg;
34 
35 class MyEdge;
36 class MyFace;
37 class MyVertex;
38 struct MyUsedTypes : public UsedTypes<Use<MyVertex>::AsVertexType, Use<MyEdge>::AsEdgeType, Use<MyFace>::AsFaceType>{};
39 
40 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::Qualityf, vertex::Color4b, vertex::VEAdj, vertex::VFAdj,vertex::BitFlags >{};
41 class MyEdge : public Edge< MyUsedTypes, edge::VertexRef, edge::VEAdj, edge::EEAdj, edge::BitFlags> {};
42 class MyFace : public Face < MyUsedTypes, face::VertexRef, face::Normal3f, face::VFAdj, face::FFAdj, face::Mark, face::Color4b, face::BitFlags > {};
43 class MyMesh : public tri::TriMesh< std::vector<MyVertex>, std::vector<MyEdge>, std::vector<MyFace> >{};
44 
45 
46 int main(int argc,char ** argv )
47 {
48  MyMesh base, basecopy, poly;
49  int ret0 = tri::io::Importer<MyMesh>::Open(base,argv[1]);
50  int ret1 = 0;
51  if(argc>2) ret1 = tri::io::Importer<MyMesh>::Open(poly,argv[2]);
52  if(ret0 != 0 || ret1 != 0)
53  {
54  printf("Failed Loading\n");
55  exit(-1);
56  }
57 
59  printf( "Mesh %s has %i vert and %i faces\n", argv[1], base.VN(), base.FN() );
60  printf( "Poly %s has %i vert and %i edges\n", argv[2], poly.VN(), poly.EN() );
61  if(poly.EN()==0) {
62  srand(time(0));
63  tri::CutTree<MyMesh> ct(base);
64  ct.BuildVisitTree(poly,rand()%base.fn);
65  }
66  tri::io::ExporterPLY<MyMesh>::Save(poly,"0_cut_tree.ply",tri::io::Mask::IOM_EDGEINDEX);
67 
68  tri::CoM<MyMesh> cc(base);
69  cc.Init();
70  cc.MarkFauxEdgeWithPolyLine(poly);
73  tri::CutMeshAlongNonFauxEdges<MyMesh>(basecopy);
74  tri::io::ExporterPLY<MyMesh>::Save(basecopy,"base_cut_with_tree.ply");
75 
76  // Selected vertices are 'locked' during the smoothing.
77  cc.SelectBoundaryVertex(poly);
78  cc.SelectUniformlyDistributed(poly,20); // lock some vertices uniformly just for fun
79 
80  // Two smoothing runs,
81  // the first that allows fast movement over the surface (long edges that can skim surface details)
82  cc.par.surfDistThr = base.bbox.Diag()/100.0;
83  cc.par.maxSimpEdgeLen = base.bbox.Diag()/50.0;
84  cc.par.minRefEdgeLen = base.bbox.Diag()/100.0;
85  cc.SmoothProject(poly,10,0.7,.3);
86  tri::io::ExporterPLY<MyMesh>::Save(poly,"1_poly_smooth.ply",tri::io::Mask::IOM_EDGEINDEX+tri::io::Mask::IOM_VERTCOLOR+tri::io::Mask::IOM_VERTQUALITY);
87 
88  // The second smooting run more accurate to adapt to the surface
89  cc.par.surfDistThr = base.bbox.Diag()/1000.0;
90  cc.par.maxSimpEdgeLen = base.bbox.Diag()/1000.0;
91  cc.par.minRefEdgeLen = base.bbox.Diag()/2000.0;
92  cc.SmoothProject(poly,10,0.01,.99);
93  tri::io::ExporterPLY<MyMesh>::Save(poly,"2_poly_smooth.ply",tri::io::Mask::IOM_EDGEINDEX+tri::io::Mask::IOM_VERTCOLOR+tri::io::Mask::IOM_VERTQUALITY);
94 
95  Distribution<float> dist;
96  cc.EvaluateHausdorffDistance(poly, dist );
97 
98  // Adapt the polyline to the mesh (in the end it will have vertices only on edges and vertices of the base mesh)
99  cc.RefineCurveByBaseMesh(poly);
100  tri::io::ExporterPLY<MyMesh>::Save(poly,"3_poly_refined.ply",tri::io::Mask::IOM_EDGEINDEX+tri::io::Mask::IOM_VERTCOLOR+tri::io::Mask::IOM_VERTQUALITY);
101  // Safely split the mesh with this refined polyline
102  cc.SplitMeshWithPolyline(poly);
103  tri::io::ExporterPLY<MyMesh>::Save(base,"3_mesh_refined.ply",tri::io::Mask::IOM_VERTCOLOR+tri::io::Mask::IOM_VERTQUALITY);
104  // Now the two meshes should have coincident edges
105  cc.MarkFauxEdgeWithPolyLine(poly);
106  CutMeshAlongNonFauxEdges(base);
107  tri::io::ExporterPLY<MyMesh>::Save(base,"4_mesh_cut.ply",tri::io::Mask::IOM_VERTCOLOR+tri::io::Mask::IOM_VERTQUALITY);
108 
109  return 0;
110 }
111