Developer Documentation

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
48 //=============================================================================
49 //
50 // CLASS ModNormalFlipping
51 //
52 //=============================================================================
53
54
55 #ifndef OPENMESH_DECIMATER_MODNORMALFLIPPING_HH
56 #define OPENMESH_DECIMATER_MODNORMALFLIPPING_HH
57
58
59 //== INCLUDES =================================================================
60
61 #include <OpenMesh/Tools/Decimater/ModBaseT.hh>
62
63 //== NAMESPACES ===============================================================
64
65 namespace OpenMesh { // BEGIN_NS_OPENMESH
66 namespace Decimater { // BEGIN_NS_DECIMATER
67
68
69 //== CLASS DEFINITION =========================================================
70
79 template <typename MeshT>
80 class ModNormalFlippingT : public ModBaseT< MeshT >
81 {
82 public:
83
84 DECIMATING_MODULE( ModNormalFlippingT, MeshT, NormalFlipping );
85
86 public:
87
89 ModNormalFlippingT( MeshT &_mesh) : Base(_mesh, true)
90 {
91 set_max_normal_deviation( 90.0f );
92 const bool mesh_has_normals = _mesh.has_face_normals();
93 _mesh.request_face_normals();
94
95 if (!mesh_has_normals)
96 {
97 omerr() << "Mesh has no face normals. Compute them automatically." << std::endl;
98 _mesh.update_face_normals();
99 }
100 }
101
102
103 ~ModNormalFlippingT()
104 {
105 Base::mesh().release_face_normals();
106 }
107
108
109 public:
110
125 float collapse_priority(const CollapseInfo& _ci)
126 {
127 // simulate collapse
128 Base::mesh().set_point(_ci.v0, _ci.p1);
129
130 // check for flipping normals
131 typename Mesh::ConstVertexFaceIter vf_it(Base::mesh(), _ci.v0);
132 typename Mesh::FaceHandle fh;
133 typename Mesh::Scalar c(1.0);
134
135 for (; vf_it.is_valid(); ++vf_it)
136 {
137 fh = *vf_it;
138 if (fh != _ci.fl && fh != _ci.fr)
139 {
140 typename Mesh::Normal n1 = Base::mesh().normal(fh);
141 typename Mesh::Normal n2 = Base::mesh().calc_face_normal(fh);
142
143 c = dot(n1, n2);
144
145 if (c < min_cos_)
146 break;
147 }
148 }
149
150 // undo simulation changes
151 Base::mesh().set_point(_ci.v0, _ci.p0);
152
153 return float( (c < min_cos_) ? Base::ILLEGAL_COLLAPSE : Base::LEGAL_COLLAPSE );
154 }
155
157 void set_error_tolerance_factor(double _factor) {
158 if (_factor >= 0.0 && _factor <= 1.0) {
159 // the smaller the factor, the smaller max_deviation_ gets
160 // thus creating a stricter constraint
161 // division by error_tolerance_factor_ is for normalization
162 double max_normal_deviation = (max_deviation_ * 180.0/M_PI) * _factor / this->error_tolerance_factor_;
163 set_max_normal_deviation(max_normal_deviation);
164 this->error_tolerance_factor_ = _factor;
165 }
166 }
167
168
169 public:
170
172 double max_normal_deviation() const { return max_deviation_ / M_PI * 180.0; }
173
179 void set_max_normal_deviation(double _d) {
180 max_deviation_ = _d / 180.0 * M_PI;
181 min_cos_ = cos(max_deviation_);
182 }
183
184 private:
185
186 // hide this method
187 void set_binary(bool _b) {}
188
189 private:
190
191 // maximum normal deviation
192 double max_deviation_, min_cos_;
193 };
194
195
196 //=============================================================================
197 } // END_NS_DECIMATER
198 } // END_NS_OPENMESH
199 //=============================================================================
200 #endif // OPENACG_MODNORMALFLIPPING_HH defined
201 //=============================================================================
202
OpenMesh::Decimater::ModNormalFlippingT::ModNormalFlippingT
ModNormalFlippingT(MeshT &_mesh)
Constructor.
Definition: ModNormalFlippingT.hh:89
OpenMesh::Decimater::ModNormalFlippingT::set_error_tolerance_factor
void set_error_tolerance_factor(double _factor)
set the percentage of maximum normal deviation
Definition: ModNormalFlippingT.hh:157
OpenMesh::Decimater::ModNormalFlippingT::set_max_normal_deviation
void set_max_normal_deviation(double _d)
Definition: ModNormalFlippingT.hh:179
ModBaseT.hh
OpenMesh::Decimater::CollapseInfoT::fl
Mesh::FaceHandle fl
Left face.
Definition: CollapseInfoT.hh:95
OpenMesh::PolyMeshT::Scalar
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:110
OpenMesh::Decimater::CollapseInfoT::fr
Mesh::FaceHandle fr
Right face.
Definition: CollapseInfoT.hh:96
OpenMesh::PolyMeshT::ConstVertexFaceIter
Kernel::ConstVertexFaceIter ConstVertexFaceIter
Circulator.
Definition: PolyMeshT.hh:176
OpenMesh::dot
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Definition: VectorAdapter.hh:176
OpenMesh::PolyMeshT::Normal
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
OpenMesh::Decimater::CollapseInfoT
Definition: CollapseInfoT.hh:74
OpenMesh::Decimater::ModNormalFlippingT::collapse_priority
float collapse_priority(const CollapseInfo &_ci)
Definition: ModNormalFlippingT.hh:125
OpenMesh::Decimater::CollapseInfoT::v0
Mesh::VertexHandle v0
Vertex to be removed.
Definition: CollapseInfoT.hh:91
OpenMesh::Decimater::CollapseInfoT::p1
Mesh::Point p1
Positions of remaining vertex.
Definition: CollapseInfoT.hh:94
OpenMesh::Decimater::CollapseInfoT::p0
Mesh::Point p0
Position of removed vertex.
Definition: CollapseInfoT.hh:93
OpenMesh::Decimater::ModBaseT
Definition: ModBaseT.hh:192
OpenMesh
Definition: MeshItems.hh:59
OpenMesh::Decimater::ModNormalFlippingT::max_normal_deviation
double max_normal_deviation() const
get normal deviation
Definition: ModNormalFlippingT.hh:172
OpenMesh::Decimater::ModNormalFlippingT
Definition: ModNormalFlippingT.hh:80