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::Qualityf, face::Color4b, 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 
53 int main(int argc,char ** argv )
54 {
55  MyMesh base, basecopy, poly;
56  int ret0=0, ret1=0;
57  if(argc>1) ret0 = tri::io::Importer<MyMesh>::Open(base,argv[1]);
58 
59  if(base.FN() == 0) Torus(base,10,4,48,24);
60 
61  if(argc>2) ret1 = tri::io::Importer<MyMesh>::Open(poly,argv[2]);
63  printf( "Mesh %s has %i vert and %i faces\n", argv[1], base.VN(), base.FN() );
64  printf( "Poly %s has %i vert and %i edges\n", argv[2], poly.VN(), poly.EN() );
65  if(poly.EN() == 0) {
66  srand(time(nullptr));
67  tri::CutTree<MyMesh> ct(base);
68  ct.Build(poly,rand()%base.fn);
69  }
70  tri::io::ExporterPLY<MyMesh>::Save(poly,"0_cut_tree.ply",tri::io::Mask::IOM_EDGEINDEX);
71 
72  tri::CoM<MyMesh> cc(base);
73  cc.Init();
74  bool ret = cc.TagFaceEdgeSelWithPolyLine(poly);
75  if(ret)
76  {
79  tri::CutMeshAlongSelectedFaceEdges<MyMesh>(basecopy);
80  tri::io::ExporterPLY<MyMesh>::Save(basecopy,"base_cut_with_tree.ply");
81  }
82  // Selected vertices are 'locked' during the smoothing.
83  cc.SelectBoundaryVertex(poly);
84 // cc.SelectUniformlyDistributed(poly,10); // lock some vertices uniformly just for fun
85 
86  // Two smoothing runs,
87  // the first that allows fast movement over the surface (long edges that can skim surface details)
88  cc.par.surfDistThr = base.bbox.Diag()/100.0f;
89  cc.par.maxSimpEdgeLen = base.bbox.Diag()/50.0f;
90  cc.par.minRefEdgeLen = base.bbox.Diag()/100.0f;
91  cc.SmoothProject(poly,30,0.7f,.3f);
92  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);
93 
94  // The second smooting run more accurate to adapt to the surface
95  cc.par.surfDistThr = base.bbox.Diag()/1000.0f;
96  cc.par.maxSimpEdgeLen = base.bbox.Diag()/1000.0f;
97  cc.par.minRefEdgeLen = base.bbox.Diag()/2000.0f;
98  cc.SmoothProject(poly,10,0.01f,.99f);
99  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);
100 
101  Distribution<float> dist;
102  cc.EvaluateHausdorffDistance(poly, dist );
103 
104  // Adapt the polyline to the mesh (in the end it will have vertices only on edges and vertices of the base mesh)
105  cc.RefineCurveByBaseMesh(poly);
106  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);
107  // Safely split the mesh with this refined polyline
108  cc.SplitMeshWithPolyline(poly);
109  tri::io::ExporterPLY<MyMesh>::Save(base,"3_mesh_refined.ply",tri::io::Mask::IOM_VERTCOLOR+tri::io::Mask::IOM_VERTQUALITY);
110  // Now the two meshes should have coincident edges
111  cc.TagFaceEdgeSelWithPolyLine(poly);
112  CutMeshAlongSelectedFaceEdges(base);
113  tri::io::ExporterPLY<MyMesh>::Save(base,"4_mesh_cut.ply",tri::io::Mask::IOM_VERTCOLOR+tri::io::Mask::IOM_VERTQUALITY);
114 
115  return 0;
116 }
117