VCG Library
trimesh_refine.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 
25 #include <vcg/complex/algorithms/refine.h>
26 #include <vcg/complex/algorithms/refine_loop.h>
27 
28 #include <vcg/complex/algorithms/bitquad_creation.h>
29 
30 // input output
31 #include <wrap/io_trimesh/import_ply.h>
32 #include <wrap/io_trimesh/export.h>
33 
34 using namespace vcg;
35 using namespace std;
36 
37 
38 class MyEdge; // dummy prototype never used
39 class MyFace;
40 class MyVertex;
41 
42 struct MyUsedTypes : public UsedTypes< Use<MyVertex>::AsVertexType,
43  Use<MyFace>::AsFaceType>{};
44 
45 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags >{};
46 class MyFace : public Face < MyUsedTypes, face::InfoOcf, face::FFAdjOcf, face::VertexRef, face::BitFlags > {};
47 class MyMesh : public vcg::tri::TriMesh< vector<MyVertex>, face::vector_ocf<MyFace> > {};
48 
49 
50 
51 #define FLAT 0
52 #define LOOP 1
53 #define CATMULL 2
54 #define BUTTERFLY 3
55 #define ONE_QUAD_X_EDGE 4
56 
57 int main(int argc, char **argv)
58 {
59  if(argc<4)
60  {
61  printf(
62  "\n PlyRefine (" __DATE__ ")\n"
63  " Visual Computing Group I.S.T.I. C.N.R.\n"
64  "Usage: PlyRefine filein.ply fileout.[ply|off|obj|...] ref_step [opt] \n"
65  "Commands: \n"
66  " Refinement rules:\n"
67  " -m use simple midpoint subdivision (default) \n"
68  " -b use butterfly subdivision scheme \n"
69  " -l use loop subdivision scheme \n"
70  " -o use one-quad-per-edge schema (*) \n"
71  " -c use Catmull-Clark (*) \n"
72  " -e# refine only if the the edge is longer than #(default 0.0)\n"
73  "Info:\n"
74  " (*) produces quad-only meshes, but updates topology only, \n"
75  " and leaves geometry unaffected \n"
76  );
77  exit(2);
78  }
79 
80  int RefMode = FLAT ;
81  int i=4; int n_steps; float length=0;
82  while(i<argc)
83  {
84  if(argv[i][0]!='-')
85  {printf("Error unable to parse option '%s'\n",argv[i]); exit(5);}
86  switch(argv[i][1])
87  {
88  case 'm' : RefMode=FLAT; break;
89  case 'b' : RefMode=BUTTERFLY; break;
90  case 'l' : RefMode=LOOP; break;
91  case 'c' : RefMode=CATMULL; break;
92  case 'o' : RefMode=ONE_QUAD_X_EDGE; break;
93  case 'e' : length=(float)atof(argv[i]+2); break;
94  default : {printf("Error unable to parse option '%s'\n",argv[i]); exit(0);}
95  }
96  ++i;
97  }
98 
99  MyMesh m;
100 
101  if(tri::io::ImporterPLY<MyMesh>::Open(m,argv[1])!=0) {
102  printf("Error reading file %s\n",argv[1]);
103  exit(1);
104  }
105 
106  m.face.EnableFFAdjacency();
110  printf("Input mesh vn:%i fn:%i\n",m.VN(),m.FN());
111 
112  n_steps=atoi(argv[3]);
113 
114  for(i=0;i < n_steps;++i)
115  {
116  switch(RefMode)
117  {
118  case FLAT:
119  tri::Refine<MyMesh, tri::MidPoint<MyMesh> >(m,tri::MidPoint<MyMesh>(&m),length);
120  break;
121  case LOOP:
122  tri::RefineOddEven<MyMesh, tri::OddPointLoop<MyMesh>, tri::EvenPointLoop<MyMesh> >(m, tri::OddPointLoop<MyMesh>(m), tri::EvenPointLoop<MyMesh>(), length);
123  break;
124  case CATMULL:
125  tri::BitQuadCreation<MyMesh>::MakePureByCatmullClark(m);
127  break;
128  case ONE_QUAD_X_EDGE:
129  tri::BitQuadCreation<MyMesh>::MakePureByRefine(m);
131  break;
132  case BUTTERFLY:
133  tri::Refine<MyMesh, tri::MidPointButterfly<MyMesh> >(m,tri::MidPointButterfly<MyMesh>(m),length);
134  break;
135  }
136  }
137 
138  printf("Output mesh vn:%i fn:%i\n",m.VN(),m.FN());
139 
140  vcg::tri::io::PlyInfo pi;
141  pi.mask|=vcg::tri::io::Mask::IOM_BITPOLYGONAL;
142  vcg::tri::io::Exporter<MyMesh>::Save(m,argv[2],pi.mask);
143  return 0;
144 }