Developer Documentation

smooth.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
45 #include <iostream>
46 #include <OpenMesh/Core/IO/MeshIO.hh> // include before kernel type!
47 #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
48 #include <OpenMesh/Tools/Utils/Timer.hh>
49 #include <OpenMesh/Tools/Smoother/JacobiLaplaceSmootherT.hh>
50 #include <OpenMesh/Tools/Utils/getopt.h>
51
52
53 using namespace OpenMesh;
54 using namespace Smoother;
55
56
57 struct MyTraits : public OpenMesh::DefaultTraits
58 {
59 #if 1
60 typedef OpenMesh::Vec3f Point;
61 typedef OpenMesh::Vec3f Normal;
62 #else
63 typedef OpenMesh::Vec3d Point;
64 typedef OpenMesh::Vec3d Normal;
65 #endif
66 };
67
68 typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits> MyMesh;
69
70
71 //-----------------------------------------------------------------------------
72
73 void usage_and_exit(int _xcode)
74 {
75 std::cout << std::endl;
76 std::cout << "Usage: smooth [Options] <iterations> <input mesh> <output mesh>\n";
77 std::cout << std::endl;
78 std::cout << "Options \n"
79 << std::endl
80 << " -c <0|1> \t continuity (C0,C1). Default: C1\n"
81 << " -t \t\t smooth tangential direction. Default: Enabled\n"
82 << " -n \t\t smooth normal direction. Default: Enabled\n"
83 << std::endl;
84 exit(_xcode);
85 }
86
87
88 //-----------------------------------------------------------------------------
89
90
91 int main(int argc, char **argv)
92 {
93 int c;
94
95 MyMesh mesh;
96 OpenMesh::Utils::Timer t;
97 std::string ifname;
98 std::string ofname;
99
100 SmootherT<MyMesh>::Continuity
101 continuity = SmootherT<MyMesh>::C1;
102
103 SmootherT<MyMesh>::Component
104 component = SmootherT<MyMesh>::Tangential_and_Normal;
105
106 int iterations;
107
108 // ---------------------------------------- evaluate command line
109
110 while ( (c=getopt(argc, argv, "tnc:h"))!=-1 )
111 {
112 switch(c)
113 {
114 case 'c': {
115 switch(*optarg)
116 {
117 case '0' : continuity = SmootherT<MyMesh>::C0; break;
118 case '1' : continuity = SmootherT<MyMesh>::C1; break;
119 }
120 break;
121 }
122 case 't':
123 component = component==SmootherT<MyMesh>::Normal
124 ? SmootherT<MyMesh>::Tangential_and_Normal
125 : SmootherT<MyMesh>::Tangential;
126 break;
127
128 case 'n':
129 component = component==SmootherT<MyMesh>::Tangential
130 ? SmootherT<MyMesh>::Tangential_and_Normal
131 : SmootherT<MyMesh>::Normal;
132 break;
133
134 case 'h': usage_and_exit(0); break;
135 case '?':
136 default: usage_and_exit(1);
137 }
138 }
139
140 if (argc-optind < 3)
141 usage_and_exit(1);
142
143
144 // # iterations
145 {
146 std::stringstream str; str << argv[optind]; str >> iterations;
147 }
148
149
150 // input file
151 ifname = argv[++optind];
152
153
154 // output file
155 ofname = argv[++optind];
156
157
158 OpenMesh::IO::Options opt;
159
160 // ---------------------------------------- read mesh
161
162 omout() << "read mesh..." << std::flush;
163 t.start();
164 OpenMesh::IO::read_mesh(mesh, ifname, opt);
165 t.stop();
166 omout() << "done (" << t.as_string() << ")\n";
167
168 omout() << " #V " << mesh.n_vertices() << std::endl;
169
170 // ---------------------------------------- smooth
171
172 JacobiLaplaceSmootherT<MyMesh> smoother(mesh);
173 smoother.initialize(component,continuity);
174
175 omout() << "smoothing..." << std::flush;
176
177 t.start();
178 smoother.smooth(iterations);
179 t.stop();
180
181 omout() << "done (";
182 omout() << t.seconds() << "s ~ ";
183 omout() << t.as_string() << ", "
184 << (iterations*mesh.n_vertices())/t.seconds() << " Vertices/s)\n";
185
186 // ---------------------------------------- write mesh
187
188 omout() << "write mesh..." << std::flush;
189 t.start();
190 OpenMesh::IO::write_mesh(mesh, ofname, opt);
191 t.stop();
192 omout() << "done (" << t.as_string() << ")\n";
193
194 return 0;
195 }
OpenMesh::TriMesh_ArrayKernelT
Definition: PolyMesh_ArrayKernelT.hh:73
OpenMesh::Utils::Timer::seconds
double seconds(void) const
Returns measured time in seconds, if the timer is in state &#39;Stopped&#39;.
OpenMesh::Utils::Timer
Definition: Timer.hh:82
OpenMesh::IO::write_mesh
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
OpenMesh::Utils::Timer::as_string
std::string as_string(Format format=Automatic)
OpenMesh::DefaultTraits
Definition: Traits.hh:121
JacobiLaplaceSmootherT.hh
Timer.hh
OpenMesh::IO::Options
Set options for reader/writer modules.
Definition: Options.hh:90
OpenMesh::Smoother::JacobiLaplaceSmootherT
Definition: JacobiLaplaceSmootherT.hh:75
OpenMesh::VectorT< float, 3 >
OpenMesh::Utils::Timer::start
void start(void)
Start measurement.
OpenMesh::IO::read_mesh
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition: MeshIO.hh:112
OpenMesh
Definition: MeshItems.hh:59
OpenMesh::Utils::Timer::stop
void stop(void)
Stop measurement.
OpenMesh::Smoother::SmootherT
Definition: SmootherT.hh:76