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