Developer Documentation
subdivider.cc
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 
43 
44 #include <iostream>
45 #include <sstream>
46 // ---------------------------------------- OpenMesh Stuff
47 #include <OpenMesh/Core/IO/MeshIO.hh>
48 #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
50 #include <OpenMesh/Tools/Utils/getopt.h>
51 // ---------------------------------------- Subdivider
59 
60 // ----------------------------------------------------------------------------
61 
62 using namespace OpenMesh::Subdivider;
63 
66 
75 
77 
78 // ----------------------------------------------------------------------------
79 
80 std::map< std::string, double > timings;
81 
82 // ----------------------------------------------------------------------------
83 
84 template < typename Subdivider >
85 bool subdivide( typename Subdivider::mesh_t& _m, size_t _n,
86  Timer::Format _fmt )
87 {
88  bool rc;
89  Timer t;
90  Subdivider subdivide;
91 
92  std::cout << "Subdivide " << _n
93  << " times with '" << subdivide.name() << "'\n";
94 
95  subdivide.attach(_m);
96  t.start();
97  rc=subdivide( _n );
98  t.stop();
99  subdivide.detach();
100 
101  if (rc)
102  {
103  std::cout << " Done [" << t.as_string(_fmt) << "]\n";
104  timings[subdivide.name()] = t.seconds();
105  }
106  else
107  std::cout << " Failed!\n";
108  return rc;
109 }
110 
111 // ----------------------------------------------------------------------------
112 
113 void usage_and_exit(int _xcode);
114 
115 // ----------------------------------------------------------------------------
116 
117 template < typename Subdivider >
118 int mainT( size_t _n,
119  const std::string& _ifname,
120  const std::string& _ofname,
121  const Timer::Format _fmt )
122 {
123  // -------------------- read mesh
124  std::cout << "Read mesh from file " << _ifname << std::endl;
125 
126  typename Subdivider::mesh_t mesh;
127 
128  if ( OpenMesh::IO::read_mesh( mesh, _ifname ) )
129  std::cout << " Ok\n";
130  else
131  {
132  std::cout << " Failed!\n";
133  return 1;
134  }
135 
136  std::cout << " #V " << mesh.n_vertices()
137  << ", #F " << mesh.n_faces()
138  << ", #E " << mesh.n_edges() << std::endl;
139 
140  // -------------------- subdividing
141  try
142  {
143  if (!subdivide< Subdivider >( mesh, _n, _fmt ))
144  return 1;
145  }
146  catch(std::bad_alloc& x)
147  {
148  std::cerr << "Out of memory: " << x.what() << std::endl;
149  return 1;
150  }
151  catch(std::exception& x)
152  {
153  std::cerr << x.what() << std::endl;
154  return 1;
155  }
156  catch(...)
157  {
158  std::cerr << "Unknown exception!\n";
159  return 1;
160  }
161 
162  // -------------------- write mesh
163 
164  std::cout << " #V " << mesh.n_vertices()
165  << ", #F " << mesh.n_faces()
166  << ", #E " << mesh.n_edges() << std::endl;
167 
168  if ( !_ofname.empty() )
169  {
170  std::cout << "Write resulting mesh to file " << _ofname << "..";
172  {
173  std::cout << "ok\n";
174  }
175  else
176  {
177  std::cerr << "Failed! Could not write file!\n";
178  return 1;
179  }
180  }
181 
182  return 0;
183 }
184 
185 // ----------------------------------------------------------------------------
186 
187 int main(int argc, char **argv)
188 {
189  int c;
190 
191  bool compare_all = false;
192  size_t n;
193  std::string ifname;
194  std::string ofname;
195 
196  enum {
197  TypeSqrt3,
198  TypeLoop,
199  TypeCompSqrt3,
200  TypeCompLoop,
201  TypeLabsikGreiner,
202  TypeModButterfly,
203  TypeCatmullClark
204  } st = TypeSqrt3;
205 
206  Timer::Format fmt = Timer::Automatic;
207 
208  while ( (c=getopt(argc, argv, "csSlLbBhf:"))!=-1 )
209  {
210  switch(c)
211  {
212  case 'c': compare_all=true; break;
213  case 's': st = TypeSqrt3; break;
214  case 'S': st = TypeCompSqrt3; break;
215  case 'l': st = TypeLoop; break;
216  case 'L': st = TypeCompLoop; break;
217  case 'b': st = TypeLabsikGreiner; break;
218  case 'B': st = TypeModButterfly; break;
219  case 'C': st = TypeCatmullClark; std::cerr << "Not yet supported, as it needs a poly mesh!"; break;
220  case 'f':
221  {
222  switch(*optarg)
223  {
224  case 'm': fmt = Timer::MSeconds; break;
225  case 'c': fmt = Timer::HSeconds; break;
226  case 's': fmt = Timer::Seconds; break;
227  case 'a':
228  default: fmt = Timer::Automatic; break;
229  }
230  break;
231  }
232  case 'h': usage_and_exit(0); break;
233  case '?':
234  default: usage_and_exit(1);
235  }
236  }
237 
238  if (argc-optind < 2)
239  usage_and_exit(1);
240 
241  // # iterations
242  {
243  std::stringstream str; str << argv[optind]; str >> n;
244  }
245 
246  // input file
247  ifname = argv[++optind];
248 
249  // output file, if provided
250  if ( ++optind < argc )
251  ofname = argv[optind];
252 
253 
254  // --------------------
255  if ( compare_all )
256  {
257  int rc;
258  rc = mainT<Sqrt3> ( n, ifname, "", fmt );
259  rc += mainT<Loop> ( n, ifname, "", fmt );
260  rc += mainT<CompositeSqrt3> ( n, ifname, "", fmt );
261  rc += mainT<CompositeLoop> ( n, ifname, "", fmt );
262  rc += mainT<InterpolatingSqrt3LG> ( n, ifname, "", fmt );
263  rc += mainT<ModifiedButterfly> ( n, ifname, "", fmt );
264  rc += mainT<CatmullClark> ( n, ifname, "", fmt );
265 
266  if (rc)
267  return rc;
268 
269  std::cout << std::endl;
270 
271  std::map< std::string, double >::iterator it;
272 
273  std::cout << "Timings:\n";
274  for(it = timings.begin();it!=timings.end();++it)
275  std::cout << it->first << ": " << Timer::as_string(it->second)
276  << std::endl;
277  std::cout << std::endl;
278  std::cout << "Ratio composite/native algorithm:\n";
279  std::cout << "sqrt(3): "
280  << timings["Uniform Composite Sqrt3"]/timings["Uniform Sqrt3"]
281  << std::endl
282  << "loop : "
283  << timings["Uniform Composite Loop"]/timings["Uniform Loop"]
284  << std::endl
285  << "Interpolating sqrt(3) : "
286  << timings["Uniform Interpolating Sqrt3"]/timings["Uniform Sqrt3"]
287  << std::endl;
288  return 0;
289  }
290  else switch(st)
291  {
292  case TypeSqrt3:
293  return mainT<Sqrt3>( n, ifname, ofname, fmt );
294  case TypeLoop:
295  return mainT<Loop>( n, ifname, ofname, fmt );
296  case TypeCompSqrt3:
297  return mainT<CompositeSqrt3>( n, ifname, ofname, fmt );
298  case TypeCompLoop:
299  return mainT<CompositeLoop> ( n, ifname, ofname, fmt );
300  case TypeLabsikGreiner:
301  return mainT<InterpolatingSqrt3LG> ( n, ifname, ofname, fmt );
302  case TypeModButterfly:
303  return mainT<ModifiedButterfly> ( n, ifname, ofname, fmt );
304  case TypeCatmullClark:
305  return mainT<CatmullClark> ( n, ifname, ofname, fmt );
306  }
307  return 1;
308 }
309 
310 // ----------------------------------------------------------------------------
311 
312 void usage_and_exit(int _xcode)
313 {
314  std::cout << "Usage: subdivide [Subdivider Type] #Iterations Input [Output].\n";
315  std::cout << std::endl;
316  std::cout << "Subdivider Type\n"
317  << std::endl
318  << " -l\tLoop\n"
319  << " -L\tComposite Loop\n"
320  << " -s\tSqrt3\n"
321  << " -S\tComposite Sqrt3\n"
322  << " -b\tInterpolating Sqrt3 Labsik-Greiner\n"
323  << " -B\tModified Butterfly\n"
324  // << " -C\tCatmullClark\n"
325  << std::endl;
326  exit(_xcode);
327 }
bool write_mesh(const Mesh &_mesh, const std::string &_filename, Options _opt=Options::Default, std::streamsize _precision=6)
Write a mesh to the file _filename.
Definition: MeshIO.hh:207
std::string as_string(Format format=Automatic)
double seconds(void) const
Returns measured time in seconds, if the timer is in state &#39;Stopped&#39;.
void stop(void)
Stop measurement.
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition: MeshIO.hh:112
void start(void)
Start measurement.
Set binary mode for r/w.
Definition: Options.hh:100