VCG Library
trimesh_align_pair.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
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 ****************************************************************************/
37 #include <vcg/complex/complex.h>
38 
39 #include <vcg/complex/algorithms/align_pair.h>
40 
41 #include <wrap/io_trimesh/import.h>
42 #include <wrap/io_trimesh/export_ply.h>
43 
44 class MyFace;
45 class MyVertex;
46 
47 struct MyUsedTypes :
48  public vcg::UsedTypes< vcg::Use<MyVertex>::AsVertexType,
49  vcg::Use<MyFace>::AsFaceType>
50 {};
51 
52 class MyVertex :
53  public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3d, vcg::vertex::Normal3d, vcg::vertex::Color4b, vcg::vertex::BitFlags>
54 {};
55 
56 class MyFace :
57  public vcg::Face < MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3d, vcg::face::FFAdj, vcg::face::Mark, vcg::face::BitFlags >
58 {};
59 
60 class MyMesh :
61  public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> >
62 {};
63 
64 using namespace vcg;
65 using namespace std;
66 
67 std::vector<vcg::Point3d>* vcg::PointMatchingScale::fix;
68 std::vector<vcg::Point3d>* vcg::PointMatchingScale::mov;
69 vcg::Box3d vcg::PointMatchingScale::b;
70 
71 int main(int argc,char ** argv)
72 {
73  if(argc<3) {
74  printf("Usage: trimesh_smooth <filename> <filename>\n");
75  return 0;
76  }
77  std::string outputname = "output.ply";
78  if (argc == 4){
79  outputname = std::string(argv[3]);
80  }
81 
82  MyMesh m1, m2;
83 
84  //open first mesh
85  int err = tri::io::Importer<MyMesh>::Open(m1,argv[1]);
86  if(err) { // all the importers return 0 in case of success
87  printf("Error in reading %s: '%s'\n", argv[1], tri::io::Importer<MyMesh>::ErrorMsg(err));
88  exit(-1);
89  }
90 
91  //open second mesh
92  err = tri::io::Importer<MyMesh>::Open(m2,argv[2]);
93  if(err) { // all the importers return 0 in case of success
94  printf("Error in reading %s: '%s'\n", argv[2], tri::io::Importer<MyMesh>::ErrorMsg(err));
95  exit(-1);
96  }
97 
98  //update normals
101 
103  vcg::AlignPair::Result result;
104  vcg::AlignPair::Param ap;
105  vcg::AlignPair::A2Mesh fix;
106  vcg::AlignPair aa;
107 
108  // 1) Convert fixed mesh and put it into the grid.
109  aa.convertMesh<MyMesh>(m1,fix);
110 
111  vcg::AlignPair::A2Grid UG;
112  vcg::AlignPair::A2GridVert VG;
113 
114  if(m1.fn==0 || ap.UseVertexOnly) {
115  fix.initVert(vcg::Matrix44d::Identity());
116  vcg::AlignPair::InitFixVert(&fix,ap,VG);
117  }
118  else {
119  fix.init(vcg::Matrix44d::Identity());
120  vcg::AlignPair::initFix(&fix, ap, UG);
121  }
122 
123 
124  // 2) Convert the second mesh and sample a <ap.SampleNum> points on it.
125  std::vector<vcg::AlignPair::A2Vertex> tmpmv;
126  aa.convertVertex(m2.vert,tmpmv);
127  aa.sampleMovVert(tmpmv, ap.SampleNum, ap.SampleMode);
128 
129  aa.mov=&tmpmv;
130  aa.fix=&fix;
131  aa.ap = ap;
132 
133  //use identity as first matrix
134  vcg::Matrix44d In;
135  In.SetIdentity();
136 
137  // Perform the ICP algorithm
138  aa.align(In,UG,VG,result);
139 
140  //rotate m2 using the resulting transformation
141  tri::UpdatePosition<MyMesh>::Matrix(m2, result.Tr, true);
143 
144  //saves the rotated mesh
145  tri::io::ExporterPLY<MyMesh>::Save(m2 ,outputname.c_str());
146 
147  return 0;
148 }
149