Developer Documentation
ModNormalDeviationT.hh
Go to the documentation of this file.
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  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
52 //=============================================================================
53 //
54 // CLASS ModNormalDeviationT
55 //
56 //=============================================================================
57 
58 
59 #ifndef OPENMESH_DECIMATER_MODNORMALDEVIATIONT_HH
60 #define OPENMESH_DECIMATER_MODNORMALDEVIATIONT_HH
61 
62 
63 //== INCLUDES =================================================================
64 
66 #include <OpenMesh/Core/Utils/Property.hh>
67 #include <OpenMesh/Core/Geometry/NormalConeT.hh>
68 
69 
70 //== NAMESPACES ===============================================================
71 
72 namespace OpenMesh {
73 namespace Decimater {
74 
75 
76 //== CLASS DEFINITION =========================================================
77 
78 
93 template <class MeshT>
94 class ModNormalDeviationT : public ModBaseT< MeshT >
95 {
96 public:
97 
98  DECIMATING_MODULE( ModNormalDeviationT, MeshT, NormalDeviation );
99 
100  typedef typename Mesh::Scalar Scalar;
101  typedef typename Mesh::Point Point;
102  typedef typename Mesh::Normal Normal;
103  typedef typename Mesh::VertexHandle VertexHandle;
104  typedef typename Mesh::FaceHandle FaceHandle;
105  typedef typename Mesh::EdgeHandle EdgeHandle;
107 
108 
109 
110 public:
111 
113  ModNormalDeviationT(MeshT& _mesh, float _max_dev = 180.0)
114  : Base(_mesh, true), mesh_(Base::mesh())
115  {
116  set_normal_deviation(_max_dev);
117  mesh_.add_property(normal_cones_);
118 
119  const bool mesh_has_normals = _mesh.has_face_normals();
120  _mesh.request_face_normals();
121 
122  if (!mesh_has_normals)
123  {
124  omerr() << "Mesh has no face normals. Compute them automatically." << std::endl;
125  _mesh.update_face_normals();
126  }
127  }
128 
129 
132  mesh_.remove_property(normal_cones_);
133  mesh_.release_face_normals();
134  }
135 
136 
138  Scalar normal_deviation() const {
139  return normal_deviation_ / M_PI * 180.0;
140  }
141 
143  void set_normal_deviation(Scalar _s) {
144  normal_deviation_ = _s / static_cast<Scalar>(180.0) * static_cast<Scalar>(M_PI);
145  }
146 
147 
149  void initialize() {
150  if (!normal_cones_.is_valid())
151  mesh_.add_property(normal_cones_);
152 
153  typename Mesh::FaceIter f_it = mesh_.faces_begin(),
154  f_end = mesh_.faces_end();
155 
156  for (; f_it != f_end; ++f_it)
157  mesh_.property(normal_cones_, *f_it) = NormalCone(mesh_.normal(*f_it));
158  }
159 
174  float collapse_priority(const CollapseInfo& _ci) {
175  // simulate collapse
176  mesh_.set_point(_ci.v0, _ci.p1);
177 
178 
179  typename Mesh::Scalar max_angle(0.0);
180  typename Mesh::ConstVertexFaceIter vf_it(mesh_, _ci.v0);
181  typename Mesh::FaceHandle fh, fhl, fhr;
182 
183  if (_ci.v0vl.is_valid()) fhl = mesh_.face_handle(_ci.v0vl);
184  if (_ci.vrv0.is_valid()) fhr = mesh_.face_handle(_ci.vrv0);
185 
186  for (; vf_it.is_valid(); ++vf_it) {
187  fh = *vf_it;
188  if (fh != _ci.fl && fh != _ci.fr) {
189  NormalCone nc = mesh_.property(normal_cones_, fh);
190 
191  nc.merge(NormalCone(mesh_.calc_face_normal(fh)));
192  if (fh == fhl) nc.merge(mesh_.property(normal_cones_, _ci.fl));
193  if (fh == fhr) nc.merge(mesh_.property(normal_cones_, _ci.fr));
194 
195  if (nc.angle() > max_angle) {
196  max_angle = nc.angle();
197  if (max_angle > 0.5 * normal_deviation_)
198  break;
199  }
200  }
201  }
202 
203 
204  // undo simulation changes
205  mesh_.set_point(_ci.v0, _ci.p0);
206 
207 
208  return (max_angle < 0.5 * normal_deviation_ ? max_angle : float( Base::ILLEGAL_COLLAPSE ));
209  }
210 
212  void set_error_tolerance_factor(double _factor) {
213  if (_factor >= 0.0 && _factor <= 1.0) {
214  // the smaller the factor, the smaller normal_deviation_ gets
215  // thus creating a stricter constraint
216  // division by error_tolerance_factor_ is for normalization
217  Scalar normal_deviation = (normal_deviation_ * static_cast<Scalar>(180.0)/static_cast<Scalar>(M_PI) ) * _factor / this->error_tolerance_factor_;
218  set_normal_deviation(normal_deviation);
219  this->error_tolerance_factor_ = _factor;
220  }
221  }
222 
223 
225  // account for changed normals
226  typename Mesh::VertexFaceIter vf_it(mesh_, _ci.v1);
227  for (; vf_it.is_valid(); ++vf_it)
228  mesh_.property(normal_cones_, *vf_it).
229  merge(NormalCone(mesh_.normal(*vf_it)));
230 
231 
232  // normal cones of deleted triangles
233  typename Mesh::FaceHandle fh;
234 
235  if (_ci.vlv1.is_valid()) {
236  fh = mesh_.face_handle(mesh_.opposite_halfedge_handle(_ci.vlv1));
237  if (fh.is_valid())
238  mesh_.property(normal_cones_, fh).
239  merge(mesh_.property(normal_cones_, _ci.fl));
240  }
241 
242  if (_ci.v1vr.is_valid()) {
243  fh = mesh_.face_handle(mesh_.opposite_halfedge_handle(_ci.v1vr));
244  if (fh.is_valid())
245  mesh_.property(normal_cones_, fh).
246  merge(mesh_.property(normal_cones_, _ci.fr));
247  }
248  }
249 
250 
251 
252 private:
253 
254  Mesh& mesh_;
255  Scalar normal_deviation_;
257 };
258 
259 
260 //=============================================================================
261 } // END_NS_DECIMATER
262 } // END_NS_OPENMESH
263 //=============================================================================
264 #endif // OPENMESH_DECIMATER_MODNORMALDEVIATIONT_HH defined
265 //=============================================================================
266 
Mesh::VertexHandle v1
Remaining vertex.
void postprocess_collapse(const CollapseInfo &_ci)
Mesh::FaceHandle fl
Left face.
Mesh::FaceHandle fr
Right face.
Scalar normal_deviation() const
Get normal deviation ( 0 .. 360 )
Mesh::Point p1
Positions of remaining vertex.
MeshT & mesh()
Access the mesh associated with the decimater.
Definition: ModBaseT.hh:282
void set_error_tolerance_factor(double _factor)
set the percentage of normal deviation
Mesh::Point p0
Position of removed vertex.
void initialize()
Allocate and init normal cones.
Mesh::VertexHandle v0
Vertex to be removed.
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:117
void merge(const NormalConeT &)
merge _cone; this instance will then enclose both former cones
Definition: NormalConeT.cc:128
Scalar angle() const
returns size of cone (radius in radians)
Definition: NormalConeT.hh:113
void set_normal_deviation(Scalar _s)
Set normal deviation ( 0 .. 360 )
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:139
Kernel::VertexFaceIter VertexFaceIter
Circulator.
Definition: PolyMeshT.hh:169
float collapse_priority(const CollapseInfo &_ci)
Control normals when Decimating.
Kernel::ConstVertexFaceIter ConstVertexFaceIter
Circulator.
Definition: PolyMeshT.hh:179
Use Normal deviation to control decimation.
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:115
ModNormalDeviationT(MeshT &_mesh, float _max_dev=180.0)
Constructor.
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:113