OpenMesh: OpenMesh/Tools/Decimater/ModNormalDeviationT.hh Source File

| OpenMesh

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
65 #include <OpenMesh/Tools/Decimater/ModBaseT.hh>
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;
106 typedef NormalConeT<Scalar> NormalCone;
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
131 ~ModNormalDeviationT() {
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 / M_PI * _factor / this->error_tolerance_factor_);
218
219 set_normal_deviation(normal_deviation);
220 this->error_tolerance_factor_ = _factor;
221 }
222 }
223
224
225 void postprocess_collapse(const CollapseInfo& _ci) {
226 // account for changed normals
227 typename Mesh::VertexFaceIter vf_it(mesh_, _ci.v1);
228 for (; vf_it.is_valid(); ++vf_it)
229 mesh_.property(normal_cones_, *vf_it).
230 merge(NormalCone(mesh_.normal(*vf_it)));
231
232
233 // normal cones of deleted triangles
234 typename Mesh::FaceHandle fh;
235
236 if (_ci.vlv1.is_valid()) {
237 fh = mesh_.face_handle(mesh_.opposite_halfedge_handle(_ci.vlv1));
238 if (fh.is_valid())
239 mesh_.property(normal_cones_, fh).
240 merge(mesh_.property(normal_cones_, _ci.fl));
241 }
242
243 if (_ci.v1vr.is_valid()) {
244 fh = mesh_.face_handle(mesh_.opposite_halfedge_handle(_ci.v1vr));
245 if (fh.is_valid())
246 mesh_.property(normal_cones_, fh).
247 merge(mesh_.property(normal_cones_, _ci.fr));
248 }
249 }
250
251
252
253 private:
254
255 Mesh& mesh_;
256 Scalar normal_deviation_;
257 OpenMesh::FPropHandleT<NormalCone> normal_cones_;
258 };
259
260
261 //=============================================================================
262 } // END_NS_DECIMATER
263 } // END_NS_OPENMESH
264 //=============================================================================
265 #endif // OPENMESH_DECIMATER_MODNORMALDEVIATIONT_HH defined
266 //=============================================================================
267
DECIMATING_MODULE
#define DECIMATING_MODULE(Classname, MeshT, Name)
Convenience macro, to be used in derived modules The macro defines the types.
Definition: ModBaseT.hh:154
OpenMesh::Decimater::CollapseInfoT::fr
Mesh::FaceHandle fr
Right face.
Definition: CollapseInfoT.hh:102
OpenMesh::NormalConeT
/class NormalCone NormalCone.hh <ACG/Geometry/Types/NormalCone.hh>
Definition: NormalConeT.hh:86
OpenMesh::Decimater::CollapseInfoT::vrv0
Mesh::HalfedgeHandle vrv0
Outer remaining halfedge of diamond spanned by v0, v1, vl, and vr.
Definition: CollapseInfoT.hh:109
OpenMesh::NormalConeT::angle
Scalar angle() const
returns size of cone (radius in radians)
Definition: NormalConeT.hh:113
OpenMesh::Decimater::ModNormalDeviationT::set_error_tolerance_factor
void set_error_tolerance_factor(double _factor)
set the percentage of normal deviation
Definition: ModNormalDeviationT.hh:212
OpenMesh::Decimater::CollapseInfoT::v0vl
Mesh::HalfedgeHandle v0vl
Outer remaining halfedge of diamond spanned by v0, v1, vl, and vr.
Definition: CollapseInfoT.hh:109
OpenMesh::NormalConeT::merge
void merge(const NormalConeT &)
merge _cone; this instance will then enclose both former cones
Definition: NormalConeT.cc:128
OpenMesh::Decimater::ModNormalDeviationT::normal_deviation
Scalar normal_deviation() const
Get normal deviation ( 0 .. 360 )
Definition: ModNormalDeviationT.hh:138
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
OpenMesh::Decimater::CollapseInfoT::v0
Mesh::VertexHandle v0
Vertex to be removed.
Definition: CollapseInfoT.hh:97
OpenMesh::FPropHandleT
Handle representing a face property.
Definition: Property.hh:529
OpenMesh::Decimater::CollapseInfoT::vlv1
Mesh::HalfedgeHandle vlv1
Outer remaining halfedge of diamond spanned by v0, v1, vl, and vr.
Definition: CollapseInfoT.hh:109
OpenMesh::Decimater::ModNormalDeviationT::collapse_priority
float collapse_priority(const CollapseInfo &_ci)
Control normals when Decimating.
Definition: ModNormalDeviationT.hh:174
OpenMesh::Decimater::ModNormalDeviationT::~ModNormalDeviationT
~ModNormalDeviationT()
Destructor.
Definition: ModNormalDeviationT.hh:131
OpenMesh::Decimater::CollapseInfoT
Stores information about a halfedge collapse.
Definition: CollapseInfoT.hh:80
OpenMesh::Decimater::ModNormalDeviationT::initialize
void initialize()
Allocate and init normal cones.
Definition: ModNormalDeviationT.hh:149
OpenMesh::Attributes::Normal
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:87
OpenMesh::Decimater::CollapseInfoT::fl
Mesh::FaceHandle fl
Left face.
Definition: CollapseInfoT.hh:101
OpenMesh::Decimater::CollapseInfoT::v1vr
Mesh::HalfedgeHandle v1vr
Outer remaining halfedge of diamond spanned by v0, v1, vl, and vr.
Definition: CollapseInfoT.hh:109
OpenMesh::Decimater::ModNormalDeviationT
Use Normal deviation to control decimation.
Definition: ModNormalDeviationT.hh:94
OpenMesh::Decimater::CollapseInfoT::v1
Mesh::VertexHandle v1
Remaining vertex.
Definition: CollapseInfoT.hh:98
OpenMesh::Decimater::ModNormalDeviationT::ModNormalDeviationT
ModNormalDeviationT(MeshT &_mesh, float _max_dev=180.0)
Constructor.
Definition: ModNormalDeviationT.hh:113
OpenMesh::Decimater::CollapseInfoT::p0
Mesh::Point p0
Position of removed vertex.
Definition: CollapseInfoT.hh:99
OpenMesh::Decimater::CollapseInfoT::p1
Mesh::Point p1
Positions of remaining vertex.
Definition: CollapseInfoT.hh:100
OpenMesh::Decimater::ModNormalDeviationT::set_normal_deviation
void set_normal_deviation(Scalar _s)
Set normal deviation ( 0 .. 360 )
Definition: ModNormalDeviationT.hh:143
OpenMesh::Decimater::ModNormalDeviationT::postprocess_collapse
void postprocess_collapse(const CollapseInfo &_ci)
After _from_vh has been collapsed into _to_vh, this method will be called.
Definition: ModNormalDeviationT.hh:225
OpenMesh::Decimater::ModBaseT::mesh
MeshT & mesh()
Access the mesh associated with the decimater.
Definition: ModBaseT.hh:282
OpenMesh::Decimater::ModBaseT
Base class for all decimation modules.
Definition: ModBaseT.hh:197
ModBaseT.hh
Base class for all decimation modules.