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