| OpenMesh

ModAspectRatioT_impl.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 ModAspectRatioT - IMPLEMENTATION
49 //
50 //=============================================================================
51 #define OPENMESH_DECIMATER_MODASPECTRATIOT_C
52
53 //== INCLUDES =================================================================
54
55 #include "ModAspectRatioT.hh"
56
57 //== NAMESPACES ===============================================================
58
59 namespace OpenMesh {
60 namespace Decimater {
61
62 //== IMPLEMENTATION ==========================================================
63
64 template<class MeshT>
65 typename ModAspectRatioT<MeshT>::Scalar ModAspectRatioT<MeshT>::aspectRatio(
66 const Point& _v0, const Point& _v1, const Point& _v2) {
67 Point d0 = _v0 - _v1;
68 Point d1 = _v1 - _v2;
69
70 // finds the max squared edge length
71 Scalar l2, maxl2 = d0.sqrnorm();
72 if ((l2 = d1.sqrnorm()) > maxl2)
73 maxl2 = l2;
74 // keep searching for the max squared edge length
75 d1 = _v2 - _v0;
76 if ((l2 = d1.sqrnorm()) > maxl2)
77 maxl2 = l2;
78
79 // squared area of the parallelogram spanned by d0 and d1
80 Scalar a2 = (d0 % d1).sqrnorm();
81
82 // the area of the triangle would be
83 // sqrt(a2)/2 or length * height / 2
84 // aspect ratio = length / height
85 // = length * length / (2*area)
86 // = length * length / sqrt(a2)
87
88 // returns the length of the longest edge
89 // divided by its corresponding height
90 return sqrt((maxl2 * maxl2) / a2);
91 }
92
93 //-----------------------------------------------------------------------------
94
95 template<class MeshT>
96 void ModAspectRatioT<MeshT>::initialize() {
97 typename Mesh::FaceIter f_it, f_end(mesh_.faces_end());
98 typename Mesh::FVIter fv_it;
99
100 for (f_it = mesh_.faces_begin(); f_it != f_end; ++f_it) {
101 fv_it = mesh_.fv_iter(*f_it);
102 typename Mesh::Point& p0 = mesh_.point(*fv_it);
103 typename Mesh::Point& p1 = mesh_.point(*(++fv_it));
104 typename Mesh::Point& p2 = mesh_.point(*(++fv_it));
105
106 mesh_.property(aspect_, *f_it) = static_cast<typename Mesh::Scalar>(1.0) / aspectRatio(p0, p1, p2);
107 }
108 }
109
110 //-----------------------------------------------------------------------------
111
112 template<class MeshT>
113 void ModAspectRatioT<MeshT>::preprocess_collapse(const CollapseInfo& _ci) {
114 typename Mesh::FaceHandle fh;
115 typename Mesh::FVIter fv_it;
116
117 for (typename Mesh::VFIter vf_it = mesh_.vf_iter(_ci.v0); vf_it.is_valid(); ++vf_it) {
118 fh = *vf_it;
119 if (fh != _ci.fl && fh != _ci.fr) {
120 fv_it = mesh_.fv_iter(fh);
121 typename Mesh::Point& p0 = mesh_.point(*fv_it);
122 typename Mesh::Point& p1 = mesh_.point(*(++fv_it));
123 typename Mesh::Point& p2 = mesh_.point(*(++fv_it));
124
125 mesh_.property(aspect_, fh) = static_cast<typename Mesh::Scalar>(1.0) / aspectRatio(p0, p1, p2);
126 }
127 }
128 }
129
130 //-----------------------------------------------------------------------------
131
132 template<class MeshT>
133 float ModAspectRatioT<MeshT>::collapse_priority(const CollapseInfo& _ci) {
134 typename Mesh::VertexHandle v2, v3;
135 typename Mesh::FaceHandle fh;
136 const typename Mesh::Point* p1(&_ci.p1);
137 typename Mesh::Scalar r0, r1, r0_min(1.0), r1_min(1.0);
138 typename Mesh::ConstVertexOHalfedgeIter voh_it(mesh_, _ci.v0);
139
140 v3 = mesh_.to_vertex_handle(*voh_it);
141 auto p3 = &mesh_.point(v3);
142
143 while (voh_it.is_valid()) {
144 v2 = v3;
145 auto p2 = p3;
146
147 ++voh_it;
148 v3 = mesh_.to_vertex_handle(*voh_it);
149 p3 = &mesh_.point(v3);
150
151 fh = mesh_.face_handle(*voh_it);
152
153 // if not boundary
154 if (fh.is_valid()) {
155 // aspect before
156 if ((r0 = mesh_.property(aspect_, fh)) < r0_min)
157 r0_min = r0;
158
159 // aspect after
160 if (!(v2 == _ci.v1 || v3 == _ci.v1))
161 if ((r1 = static_cast<typename Mesh::Scalar>(1.0) / aspectRatio(*p1, *p2, *p3)) < r1_min)
162 r1_min = r1;
163 }
164 }
165
166 if (Base::is_binary()) {
167 return
168 ((r1_min > r0_min) || (r1_min > min_aspect_)) ? float(Base::LEGAL_COLLAPSE) :
169 float(Base::ILLEGAL_COLLAPSE);
170
171 } else {
172 if (r1_min > r0_min)
173 return 1.f - float(r1_min);
174 else
175 return
176 (r1_min > min_aspect_) ? 2.f - float(r1_min) : float(Base::ILLEGAL_COLLAPSE);
177 }
178 }
179
180 //-----------------------------------------------------------------------------
181
182 template<class MeshT>
183 void ModAspectRatioT<MeshT>::set_error_tolerance_factor(double _factor) {
184 if (_factor >= 0.0 && _factor <= 1.0) {
185 // the smaller the factor, the larger min_aspect_ gets
186 // thus creating a stricter constraint
187 // division by (2.0 - error_tolerance_factor_) is for normalization
188 float min_aspect = min_aspect_ * (2.f - float(_factor)) / (2.f - float(this->error_tolerance_factor_));
189 set_aspect_ratio(1.f/min_aspect);
190 this->error_tolerance_factor_ = _factor;
191 }
192 }
193
194 //=============================================================================
195 }
196 }
197 //=============================================================================
OpenMesh::Decimater::ModAspectRatioT::collapse_priority
float collapse_priority(const CollapseInfo &_ci) override
Returns the collapse priority.
Definition: ModAspectRatioT_impl.hh:133
OpenMesh::PolyMeshT::Scalar
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:110
OpenMesh::Decimater::ModAspectRatioT::set_error_tolerance_factor
void set_error_tolerance_factor(double _factor) override
set percentage of aspect ratio
Definition: ModAspectRatioT_impl.hh:183
OpenMesh::PolyMeshT::ConstVertexOHalfedgeIter
Kernel::ConstVertexOHalfedgeIter ConstVertexOHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:173
ModAspectRatioT.hh
OpenMesh::Decimater::ModAspectRatioT::initialize
void initialize() override
precompute face aspect ratio
Definition: ModAspectRatioT_impl.hh:96
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
OpenMesh::PolyMeshT::VertexHandle
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
OpenMesh::Decimater::ModAspectRatioT::preprocess_collapse
void preprocess_collapse(const CollapseInfo &_ci) override
update aspect ratio of one-ring
Definition: ModAspectRatioT_impl.hh:113