VCG Library
trimesh_join.cpp
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 ****************************************************************************/
23 #include<vcg/complex/complex.h>
24 #include<vcg/complex/algorithms/clean.h>
25 #include<vcg/complex/algorithms/clip.h>
26 
27 // input output
28 #include <wrap/io_trimesh/import_ply.h>
29 #include <wrap/io_trimesh/export_ply.h>
30 
31 using namespace vcg;
32 using namespace std;
33 
34 class MyFace;
35 class MyVertex;
36 
37 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
38  Use<MyFace> ::AsFaceType>{};
39 
40 class MyVertex : public Vertex <MyUsedTypes, vertex::Coord3f, vertex::BitFlags >{};
41 class MyFace : public Face < MyUsedTypes, face::VertexRef, face::BitFlags > {};
42 class MyMesh : public vcg::tri::TriMesh< vector<MyVertex>, vector<MyFace> > {};
43 
44 
45 
46 int main(int argc,char **argv )
47 {
48  if(argc<2)
49  {
50  printf( "Usage: trimesh_join [opt] filename.ply [filename.ply | *] \n"
51  "where opt can be:\n"
52  " -b xmin ymin zmin xmax ymax zmax : \n"
53  " Returns only mesh composed by faces inside specified bbox\n"
54  " -t Just scan all the input files computing the total bbox\n"
55  );
56  exit(0);
57  }
58 
59  MyMesh ml,mr;
60  Box3f ClipBB,TotBB;
61  bool ClipFlag=false,MergeFlag=true;
62  int i=1;
63  // Parsing option loop
64  while(argv[i][0]=='-')
65  {
66  switch(argv[i][1])
67  {
68  case 'b': {
69  if(argc<i+7) {
70  printf("Error in parsing bbox option");
71  exit(0);
72  }
73  ClipBB.min=Point3f::Construct(atof(argv[i+1]),atof(argv[i+2]),atof(argv[i+3]));
74  ClipBB.max=Point3f::Construct(atof(argv[i+4]),atof(argv[i+5]),atof(argv[i+6]));
75  i+=6;
76  printf("Clipping incoming meshes with box:\n (%7.4f %7.4f %7.4f) - (%7.4f %7.4f %7.4f)\n",
77  ClipBB.min[0],ClipBB.min[1],ClipBB.min[2],
78  ClipBB.max[0],ClipBB.max[1],ClipBB.max[2]);
79  ClipFlag=true;
80  } break;
81  case 't': MergeFlag=false; break;
82  default : printf("Unknown option '%s'\n",argv[i]);
83  }
84  ++i;
85  }
86  while(i<argc)
87  {
88  if(vcg::tri::io::ImporterPLY<MyMesh>::Open(mr,argv[i])!=0)
89  {
90  printf("Error reading file %s\n",argv[1]);
91  exit(0);
92  }
93  printf("Input mesh %3i vn:%9i fn:%9i\n",i, mr.VN(), mr.FN());
94  if(ClipFlag)
95  {
96  tri::GenericVertexInterpolator<MyMesh> interp(mr);
97  tri::TriMeshClipper<MyMesh>::Box(ClipBB,interp,mr);
98  printf(" clipped to vn:%9i fn:%9i\n", mr.VN(), mr.FN());
99  }
101  TotBB.Add(mr.bbox);
102 
103  if(MergeFlag) tri::Append<MyMesh,MyMesh>::Mesh(ml,mr); // append mesh mr to ml
104  ++i;
105  }
106 
107  printf("Output mesh vn:%i fn:%i\n",ml.VN(),ml.FN());
108 
109  tri::io::ExporterPLY<MyMesh>::Save(ml,"joined.ply");
111  printf("Removed %i duplicated vertices\n",dv);
112  tri::io::ExporterPLY<MyMesh>::Save(ml,"joined_unif.ply");
113  printf("Final BBox of mesh :\n (%7.4f %7.4f %7.4f) - (%7.4f %7.4f %7.4f)\n",
114  TotBB.min[0],TotBB.min[1],TotBB.min[2],
115  TotBB.max[0],TotBB.max[1],TotBB.max[2]);
116 
117 }
118