OpenMesh
ModNormalFlippingT.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 
53 //=============================================================================
54 //
55 // CLASS ModNormalFlipping
56 //
57 //=============================================================================
58 
59 
60 #ifndef OPENMESH_DECIMATER_MODNORMALFLIPPING_HH
61 #define OPENMESH_DECIMATER_MODNORMALFLIPPING_HH
62 
63 
64 //== INCLUDES =================================================================
65 
67 
68 //== NAMESPACES ===============================================================
69 
70 namespace OpenMesh { // BEGIN_NS_OPENMESH
71 namespace Decimater { // BEGIN_NS_DECIMATER
72 
73 
74 //== CLASS DEFINITION =========================================================
75 
84 template <typename MeshT>
85 class ModNormalFlippingT : public ModBaseT< MeshT >
86 {
87 public:
88 
89  DECIMATING_MODULE( ModNormalFlippingT, MeshT, NormalFlipping );
90 
91 public:
92 
94  ModNormalFlippingT( MeshT &_mesh) : Base(_mesh, true)
95  {
96  set_max_normal_deviation( 90.0f );
97  const bool mesh_has_normals = _mesh.has_face_normals();
98  _mesh.request_face_normals();
99 
100  if (!mesh_has_normals)
101  {
102  omerr() << "Mesh has no face normals. Compute them automatically." << std::endl;
103  _mesh.update_face_normals();
104  }
105  }
106 
107 
109  {
110  Base::mesh().release_face_normals();
111  }
112 
113 
114 public:
115 
130  float collapse_priority(const CollapseInfo& _ci)
131  {
132  // simulate collapse
133  Base::mesh().set_point(_ci.v0, _ci.p1);
134 
135  // check for flipping normals
136  typename Mesh::ConstVertexFaceIter vf_it(Base::mesh(), _ci.v0);
137  typename Mesh::FaceHandle fh;
138  typename Mesh::Scalar c(1.0);
139 
140  for (; vf_it.is_valid(); ++vf_it)
141  {
142  fh = *vf_it;
143  if (fh != _ci.fl && fh != _ci.fr)
144  {
145  typename Mesh::Normal n1 = Base::mesh().normal(fh);
146  typename Mesh::Normal n2 = Base::mesh().calc_face_normal(fh);
147 
148  c = dot(n1, n2);
149 
150  if (c < min_cos_)
151  break;
152  }
153  }
154 
155  // undo simulation changes
156  Base::mesh().set_point(_ci.v0, _ci.p0);
157 
158  return float( (c < min_cos_) ? Base::ILLEGAL_COLLAPSE : Base::LEGAL_COLLAPSE );
159  }
160 
162  void set_error_tolerance_factor(double _factor) {
163  if (_factor >= 0.0 && _factor <= 1.0) {
164  // the smaller the factor, the smaller max_deviation_ gets
165  // thus creating a stricter constraint
166  // division by error_tolerance_factor_ is for normalization
167  double max_normal_deviation = (max_deviation_ * 180.0/M_PI) * _factor / this->error_tolerance_factor_;
168  set_max_normal_deviation(max_normal_deviation);
169  this->error_tolerance_factor_ = _factor;
170  }
171  }
172 
173 
174 public:
175 
177  double max_normal_deviation() const { return max_deviation_ / M_PI * 180.0; }
178 
184  void set_max_normal_deviation(double _d) {
185  max_deviation_ = _d / 180.0 * M_PI;
186  min_cos_ = cos(max_deviation_);
187  }
188 
189 private:
190 
191  // hide this method
192  void set_binary(bool _b) {}
193 
194 private:
195 
196  // maximum normal deviation
197  double max_deviation_, min_cos_;
198 };
199 
200 
201 //=============================================================================
202 } // END_NS_DECIMATER
203 } // END_NS_OPENMESH
204 //=============================================================================
205 #endif // OPENACG_MODNORMALFLIPPING_HH defined
206 //=============================================================================
207 
#define DECIMATING_MODULE(Classname, MeshT, Name)
Convenience macro, to be used in derived modules The macro defines the types.
Definition: ModBaseT.hh:154
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Definition: VectorAdapter.hh:181
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
void set_max_normal_deviation(double _d)
Set normal deviation.
Definition: ModNormalFlippingT.hh:184
double max_normal_deviation() const
get normal deviation
Definition: ModNormalFlippingT.hh:177
Mesh::FaceHandle fl
Left face.
Definition: CollapseInfoT.hh:101
Mesh::Point p0
Position of removed vertex.
Definition: CollapseInfoT.hh:99
Mesh::VertexHandle v0
Vertex to be removed.
Definition: CollapseInfoT.hh:97
Mesh::Point p1
Positions of remaining vertex.
Definition: CollapseInfoT.hh:100
Decimating module to avoid flipping of faces.
Definition: ModNormalFlippingT.hh:85
ModNormalFlippingT(MeshT &_mesh)
Constructor.
Definition: ModNormalFlippingT.hh:94
float collapse_priority(const CollapseInfo &_ci)
Compute collapse priority due to angular deviation of face normals before and after a collapse...
Definition: ModNormalFlippingT.hh:130
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:87
Stores information about a halfedge collapse.
Definition: CollapseInfoT.hh:80
Mesh::FaceHandle fr
Right face.
Definition: CollapseInfoT.hh:102
void set_error_tolerance_factor(double _factor)
set the percentage of maximum normal deviation
Definition: ModNormalFlippingT.hh:162
Base class for all decimation modules.
Definition: ModBaseT.hh:197
Base class for all decimation modules.

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .