ModAspectRatioT.cc 7.6 KB
Newer Older
Isaak Lim's avatar
Isaak Lim committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*===========================================================================*\
 *                                                                           *
 *                               OpenMesh                                    *
 *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openmesh.org                                *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenMesh.                                           *
 *                                                                           *
 *  OpenMesh is free software: you can redistribute it and/or modify         *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenMesh is distributed in the hope that it will be useful,              *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenMesh.  If not,                                    *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
33
 \*===========================================================================*/
Isaak Lim's avatar
Isaak Lim committed
34
35
36
37
38
39

/*===========================================================================*\
 *                                                                           *
 *   $Revision: 448 $                                                        *
 *   $Date: 2011-11-04 13:59:37 +0100 (Fri, 04 Nov 2011) $                   *
 *                                                                           *
40
 \*===========================================================================*/
Isaak Lim's avatar
Isaak Lim committed
41
42
43
44
45
46
47
48
49

/** \file ModAspectRatioT.cc
 */

//=============================================================================
//
//  CLASS ModAspectRatioT - IMPLEMENTATION
//
//=============================================================================
50
#define OPENMESH_DECIMATER_MODASPECTRATIOT_C
Isaak Lim's avatar
Isaak Lim committed
51
52
53
54
55
56
57

//== INCLUDES =================================================================

#include "ModAspectRatioT.hh"

//== NAMESPACES ===============================================================

58
namespace OpenMesh {
Isaak Lim's avatar
Isaak Lim committed
59
60
61
62
namespace Decimater {

//== IMPLEMENTATION ==========================================================

63
64
template<class MeshT>
typename ModAspectRatioT<MeshT>::Scalar ModAspectRatioT<MeshT>::aspectRatio(
65
    const Point& _v0, const Point& _v1, const Point& _v2) {
Isaak Lim's avatar
Isaak Lim committed
66
67
68
69
70
  Point d0 = _v0 - _v1;
  Point d1 = _v1 - _v2;

  // finds the max squared edge length
  Scalar l2, maxl2 = d0.sqrnorm();
71
  if ((l2 = d1.sqrnorm()) > maxl2)
Isaak Lim's avatar
Isaak Lim committed
72
73
74
    maxl2 = l2;
  // keep searching for the max squared edge length
  d1 = _v2 - _v0;
75
  if ((l2 = d1.sqrnorm()) > maxl2)
Isaak Lim's avatar
Isaak Lim committed
76
77
78
79
80
81
82
83
84
85
86
87
88
    maxl2 = l2;

  // squared area of the parallelogram spanned by d0 and d1
  Scalar a2 = (d0 % d1).sqrnorm();

  // the area of the triangle would be
  // sqrt(a2)/2 or length * height / 2
  // aspect ratio = length / height
  //              = length * length / (2*area)
  //              = length * length / sqrt(a2)

  // returns the length of the longest edge
  //         divided by its corresponding height
89
  return sqrt((maxl2 * maxl2) / a2);
Isaak Lim's avatar
Isaak Lim committed
90
91
92
93
}

//-----------------------------------------------------------------------------

94
95
template<class MeshT>
void ModAspectRatioT<MeshT>::initialize() {
96
97
  typename Mesh::FaceIter f_it, f_end(mesh_.faces_end());
  typename Mesh::FVIter fv_it;
Isaak Lim's avatar
Isaak Lim committed
98

99
100
  for (f_it = mesh_.faces_begin(); f_it != f_end; ++f_it) {
    typename Mesh::Point& p0 = mesh_.point(fv_it = mesh_.fv_iter(f_it));
Isaak Lim's avatar
Isaak Lim committed
101
102
103
    typename Mesh::Point& p1 = mesh_.point(++fv_it);
    typename Mesh::Point& p2 = mesh_.point(++fv_it);

104
    mesh_.property(aspect_, f_it) = 1.0 / aspectRatio(p0, p1, p2);
Isaak Lim's avatar
Isaak Lim committed
105
106
107
108
109
  }
}

//-----------------------------------------------------------------------------

110
111
template<class MeshT>
void ModAspectRatioT<MeshT>::preprocess_collapse(const CollapseInfo& _ci) {
112
113
  typename Mesh::FaceHandle fh;
  typename Mesh::FVIter fv_it;
Isaak Lim's avatar
Isaak Lim committed
114

115
  for (typename Mesh::VFIter vf_it = mesh_.vf_iter(_ci.v0); vf_it; ++vf_it) {
Isaak Lim's avatar
Isaak Lim committed
116
    fh = vf_it.handle();
117
118
    if (fh != _ci.fl && fh != _ci.fr) {
      typename Mesh::Point& p0 = mesh_.point(fv_it = mesh_.fv_iter(fh));
Isaak Lim's avatar
Isaak Lim committed
119
120
121
      typename Mesh::Point& p1 = mesh_.point(++fv_it);
      typename Mesh::Point& p2 = mesh_.point(++fv_it);

122
      mesh_.property(aspect_, fh) = 1.0 / aspectRatio(p0, p1, p2);
Isaak Lim's avatar
Isaak Lim committed
123
124
125
126
127
128
    }
  }
}

//-----------------------------------------------------------------------------

129
130
template<class MeshT>
float ModAspectRatioT<MeshT>::collapse_priority(const CollapseInfo& _ci) {
131
132
133
134
135
  typename Mesh::VertexHandle v2, v3;
  typename Mesh::FaceHandle fh;
  const typename Mesh::Point *p1(&_ci.p1), *p2, *p3;
  typename Mesh::Scalar r0, r1, r0_min(1.0), r1_min(1.0);
  typename Mesh::CVVIter vv_it(mesh_, _ci.v0);
Isaak Lim's avatar
Isaak Lim committed
136
137

  v3 = vv_it.handle();
138
  p3 = &mesh_.point(v3);
Isaak Lim's avatar
Isaak Lim committed
139

140
  while (vv_it) {
Isaak Lim's avatar
Isaak Lim committed
141
142
143
    v2 = v3;
    p2 = p3;

144
    v3 = (++vv_it).handle();
Isaak Lim's avatar
Isaak Lim committed
145
146
147
148
149
    p3 = &mesh_.point(v3);

    fh = mesh_.face_handle(vv_it.current_halfedge_handle());

    // if not boundary
150
151
152
153
    if (fh.is_valid()) {
      // aspect before
      if ((r0 = mesh_.property(aspect_, fh)) < r0_min)
        r0_min = r0;
Isaak Lim's avatar
Isaak Lim committed
154

155
      // aspect after
Isaak Lim's avatar
Isaak Lim committed
156
      if (!(v2 == _ci.v1 || v3 == _ci.v1))
157
158
        if ((r1 = 1.0 / aspectRatio(*p1, *p2, *p3)) < r1_min)
          r1_min = r1;
Isaak Lim's avatar
Isaak Lim committed
159
160
161
    }
  }

162
163
164
165
  if (Base::is_binary()) {
    return
        ((r1_min > r0_min) || (r1_min > min_aspect_)) ? Base::LEGAL_COLLAPSE :
            Base::ILLEGAL_COLLAPSE;
Isaak Lim's avatar
Isaak Lim committed
166

167
  } else {
Isaak Lim's avatar
Isaak Lim committed
168
169
    if (r1_min > r0_min)
      return 1.0 - r1_min;
170
171
172
    else
      return
          (r1_min > min_aspect_) ? 2.0 - r1_min : float(Base::ILLEGAL_COLLAPSE);
Isaak Lim's avatar
Isaak Lim committed
173
174
175
  }
}

176
177
178
179
180
181
182
183
184
185
186
187
188
189
//-----------------------------------------------------------------------------

template<class MeshT>
void ModAspectRatioT<MeshT>::set_error_tolerance_factor(double _factor) {
  if (_factor >= 0.0 && _factor <= 1.0) {
    // the smaller the factor, the larger min_aspect_ gets
    // thus creating a stricter constraint
    // division by (2.0 - error_tolerance_factor_) is for normalization
    double min_aspect = min_aspect_ * (2.0 - _factor) / (2.0 - this->error_tolerance_factor_);
    set_aspect_ratio(1.0/min_aspect);
    this->error_tolerance_factor_ = _factor;
  }
}

Isaak Lim's avatar
Isaak Lim committed
190
191
192
193
//=============================================================================
}
}
//=============================================================================