/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004-2016 \/)\/ * * 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. * * * ****************************************************************************/ /*! \file trimesh_align_pair.cpp \ingroup code_sample \brief the minimal example for aligning two meshes This file contain a minimal example for aligning two meshs. Example call: ./trimesh_align_pair mesh1.ply mesh2.ply output.ply output.ply will contain mesh2.ply rotated in order to be aligned to mesh1.ply */ #include #include #include #include class MyFace; class MyVertex; struct MyUsedTypes : public vcg::UsedTypes< vcg::Use::AsVertexType, vcg::Use::AsFaceType> {}; class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3d, vcg::vertex::Normal3d, vcg::vertex::Color4b, vcg::vertex::BitFlags> {}; class MyFace : public vcg::Face < MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3d, vcg::face::FFAdj, vcg::face::Mark, vcg::face::BitFlags > {}; class MyMesh : public vcg::tri::TriMesh< std::vector, std::vector > {}; using namespace vcg; using namespace std; std::vector* vcg::PointMatchingScale::fix; std::vector* vcg::PointMatchingScale::mov; vcg::Box3d vcg::PointMatchingScale::b; int main(int argc,char ** argv) { if(argc<3) { printf("Usage: trimesh_smooth \n"); return 0; } std::string outputname = "output.ply"; if (argc == 4){ outputname = std::string(argv[3]); } MyMesh m1, m2; //open first mesh int err = tri::io::Importer::Open(m1,argv[1]); if(err) { // all the importers return 0 in case of success printf("Error in reading %s: '%s'\n", argv[1], tri::io::Importer::ErrorMsg(err)); exit(-1); } //open second mesh err = tri::io::Importer::Open(m2,argv[2]); if(err) { // all the importers return 0 in case of success printf("Error in reading %s: '%s'\n", argv[2], tri::io::Importer::ErrorMsg(err)); exit(-1); } //update normals vcg::tri::UpdateNormal::PerVertexNormalizedPerFace(m1); vcg::tri::UpdateNormal::PerVertexNormalizedPerFace(m2); ////PARAMS vcg::AlignPair::Result result; vcg::AlignPair::Param ap; vcg::AlignPair::A2Mesh fix; vcg::AlignPair aa; // 1) Convert fixed mesh and put it into the grid. aa.convertMesh(m1,fix); vcg::AlignPair::A2Grid UG; vcg::AlignPair::A2GridVert VG; if(m1.fn==0 || ap.UseVertexOnly) { fix.initVert(vcg::Matrix44d::Identity()); vcg::AlignPair::InitFixVert(&fix,ap,VG); } else { fix.init(vcg::Matrix44d::Identity()); vcg::AlignPair::initFix(&fix, ap, UG); } // 2) Convert the second mesh and sample a points on it. std::vector tmpmv; aa.convertVertex(m2.vert,tmpmv); aa.sampleMovVert(tmpmv, ap.SampleNum, ap.SampleMode); aa.mov=&tmpmv; aa.fix=&fix; aa.ap = ap; //use identity as first matrix vcg::Matrix44d In; In.SetIdentity(); // Perform the ICP algorithm aa.align(In,UG,VG,result); //rotate m2 using the resulting transformation tri::UpdatePosition::Matrix(m2, result.Tr, true); tri::UpdateBounding::Box(m2); //saves the rotated mesh tri::io::ExporterPLY::Save(m2 ,outputname.c_str()); return 0; }