OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ModRoundnessT.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 
53 //=============================================================================
54 //
55 // CLASS ModRoundnessT
56 //
57 //=============================================================================
58 
59 #ifndef OPENMESH_DECIMATER_MODROUNDNESST_HH
60 #define OPENMESH_DECIMATER_MODROUNDNESST_HH
61 
62 
63 //== INCLUDES =================================================================
64 
66 #include <math.h>
67 
68 #if defined(OM_CC_MSVC)
69 # define OM_ENABLE_WARNINGS 4244
70 # pragma warning(disable : OM_ENABLE_WARNINGS )
71 #endif
72 
73 //== NAMESPACE ================================================================
74 
75 namespace OpenMesh { // BEGIN_NS_OPENMESH
76 namespace Decimater { // BEGIN_NS_DECIMATER
77 
78 
79 //== CLASS DEFINITION =========================================================
80 
81 
90 template <class MeshT>
91 class ModRoundnessT : public ModBaseT<MeshT>
92 {
93  public:
94  DECIMATING_MODULE( ModRoundnessT, MeshT, Roundness );
95 
96  public:
97 
98  // typedefs
99  typedef typename MeshT::Point Point;
100  typedef typename vector_traits<Point>::value_type value_type;
101 
102  public:
103 
105  ModRoundnessT( MeshT &_dec ) :
106  Base(_dec, false),
107  min_r_(-1.0)
108  { }
109 
112 
113  public: // inherited
114 
125  float collapse_priority(const CollapseInfo& _ci)
126  {
127  // using namespace OpenMesh;
128 
129  typename Mesh::ConstVertexOHalfedgeIter voh_it(Base::mesh(), _ci.v0);
130  double r;
131  double priority = 0.0; //==LEGAL_COLLAPSE
132  typename Mesh::FaceHandle fhC, fhB;
133  Vec3f B,C;
134 
135  if ( min_r_ < 0.0f ) // continues mode
136  {
137  C = vector_cast<Vec3f>(Base::mesh().point( Base::mesh().to_vertex_handle(*voh_it)));
138  fhC = Base::mesh().face_handle( *voh_it );
139 
140  for (++voh_it; voh_it.is_valid(); ++voh_it)
141  {
142  B = C;
143  fhB = fhC;
144  C = vector_cast<Vec3f>(Base::mesh().point(Base::mesh().to_vertex_handle(*voh_it)));
145  fhC = Base::mesh().face_handle( *voh_it );
146 
147  if ( fhB == _ci.fl || fhB == _ci.fr )
148  continue;
149 
150  // simulate collapse using position of v1
151  r = roundness( vector_cast<Vec3f>(_ci.p1), B, C );
152 
153  // return the maximum non-roundness
154  priority = std::max( priority, (1.0-r) );
155 
156  }
157  }
158  else // binary mode
159  {
160  C = vector_cast<Vec3f>(Base::mesh().point( Base::mesh().to_vertex_handle(*voh_it)));
161  fhC = Base::mesh().face_handle( *voh_it );
162 
163  for (++voh_it; voh_it.is_valid() && (priority==Base::LEGAL_COLLAPSE); ++voh_it)
164  {
165  B = C;
166  fhB = fhC;
167  C = vector_cast<Vec3f>(Base::mesh().point(Base::mesh().to_vertex_handle(*voh_it)));
168  fhC = Base::mesh().face_handle( *voh_it );
169 
170  if ( fhB == _ci.fl || fhB == _ci.fr )
171  continue;
172 
173  priority = ( (r=roundness( vector_cast<Vec3f>(_ci.p1), B, C )) < min_r_)
174  ? Base::ILLEGAL_COLLAPSE : Base::LEGAL_COLLAPSE;
175  }
176  }
177 
178  return (float) priority;
179  }
180 
182  void set_error_tolerance_factor(double _factor) {
183  if (this->is_binary()) {
184  if (_factor >= 0.0 && _factor <= 1.0) {
185  // the smaller the factor, the smaller min_r_ gets
186  // thus creating a stricter constraint
187  // division by error_tolerance_factor_ is for normalization
188  value_type min_roundness = min_r_ * _factor / this->error_tolerance_factor_;
189  set_min_roundness(min_roundness);
190  this->error_tolerance_factor_ = _factor;
191  }
192 }
193  }
194 
195 
196 public: // specific methods
197 
198  void set_min_angle( float _angle, bool /* _binary=true */ )
199  {
200  assert( _angle > 0 && _angle < 60 );
201 
202  _angle = float(M_PI * _angle /180.0);
203 
204  Vec3f A,B,C;
205 
206  A = Vec3f( 0.0f, 0.0f, 0.0f);
207  B = Vec3f( 2.0f * cos(_angle), 0.0f, 0.0f);
208  C = Vec3f( cos(_angle), sin(_angle), 0.0f);
209 
210  double r1 = roundness(A,B,C);
211 
212  _angle = float(0.5 * ( M_PI - _angle ));
213 
214  A = Vec3f( 0.0f, 0.0f, 0.0f);
215  B = Vec3f( 2.0f*cos(_angle), 0.0f, 0.0f);
216  C = Vec3f( cos(_angle), sin(_angle), 0.0f);
217 
218  double r2 = roundness(A,B,C);
219 
220  set_min_roundness( value_type(std::min(r1,r2)), true );
221  }
222 
230  void set_min_roundness( value_type _min_roundness, bool _binary=true )
231  {
232  assert( 0.0 <= _min_roundness && _min_roundness <= 1.0 );
233  min_r_ = _min_roundness;
234  Base::set_binary(_binary);
235  }
236 
239  {
240  min_r_ = -1.0;
241  Base::set_binary(false);
242  }
243 
244  // Compute a normalized roundness of a triangle ABC
245  //
246  // Having
247  // A,B,C corner points of triangle
248  // a,b,c the vectors BC,CA,AB
249  // Area area of triangle
250  //
251  // then define
252  //
253  // radius of circumference
254  // R := -----------------------
255  // length of shortest edge
256  //
257  // ||a|| * ||b|| * ||c||
258  // ---------------------
259  // 4 * Area ||a|| * ||b|| * ||c||
260  // = ----------------------- = -----------------------------------
261  // min( ||a||,||b||,||c||) 4 * Area * min( ||a||,||b||,||c|| )
262  //
263  // ||a|| * ||b|| * ||c||
264  // = -------------------------------------------------------
265  // 4 * 1/2 * ||cross(B-A,C-A)|| * min( ||a||,||b||,||c|| )
266  //
267  // a'a * b'b * c'c
268  // R� = ----------------------------------------------------------
269  // 4 * cross(B-A,C-A)'cross(B-A,C-A) * min( a'a, b'b, c'c )
270  //
271  // a'a * b'b * c'c
272  // R = 1/2 * sqrt(---------------------------)
273  // AA * min( a'a, b'b, c'c )
274  //
275  // At angle 60� R has it's minimum for all edge lengths = sqrt(1/3)
276  //
277  // Define normalized roundness
278  //
279  // nR := sqrt(1/3) / R
280  //
281  // AA * min( a'a, b'b, c'c )
282  // = sqrt(4/3) * sqrt(---------------------------)
283  // a'a * b'b * c'c
284  //
285  double roundness( const Vec3f& A, const Vec3f& B, const Vec3f &C )
286  {
287  const value_type epsilon = value_type(1e-15);
288 
289  static const value_type sqrt43 = value_type(sqrt(4.0/3.0)); // 60�,a=b=c, **)
290 
291  Vec3f vecAC = C-A;
292  Vec3f vecAB = B-A;
293 
294  // compute squared values to avoid sqrt-computations
295  value_type aa = (B-C).sqrnorm();
296  value_type bb = vecAC.sqrnorm();
297  value_type cc = vecAB.sqrnorm();
298  value_type AA = cross(vecAC,vecAB).sqrnorm(); // without factor 1/4 **)
299 
300  if ( AA < epsilon )
301  return 0.0;
302 
303  double nom = AA * std::min( std::min(aa,bb),cc );
304  double denom = aa * bb * cc;
305  double nR = sqrt43 * sqrt(nom/denom);
306 
307  return nR;
308  }
309 
310  private:
311 
312  value_type min_r_;
313 };
314 
315 
316 //=============================================================================
317 } // END_NS_DECIMATER
318 } // END_NS_OPENMESH
319 //=============================================================================
320 #if defined(OM_CC_MSVC) && defined(OM_ENABLE_WARNINGS)
321 # pragma warning(default : OM_ENABLE_WARNINGS)
322 # undef OM_ENABLE_WARNINGS
323 #endif
324 //=============================================================================
325 #endif // OPENMESH_DECIMATER_MODROUNDNESST_HH defined
326 //=============================================================================
327 
Mesh::FaceHandle fr
Right face.
Definition: CollapseInfoT.hh:102
Stores information about a halfedge collapse.
Definition: CollapseInfoT.hh:80
float collapse_priority(const CollapseInfo &_ci)
Compute collapse priority due to roundness of triangle.
Definition: ModRoundnessT.hh:125
ModRoundnessT(MeshT &_dec)
Constructor.
Definition: ModRoundnessT.hh:105
Mesh::FaceHandle fl
Left face.
Definition: CollapseInfoT.hh:101
Kernel::ConstVertexOHalfedgeIter ConstVertexOHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:176
void vector_cast(const src_t &_src, dst_t &_dst, GenProg::Int2Type< n >)
Cast vector type to another vector type by copying the vector elements.
Definition: vector_cast.hh:86
void unset_min_roundness()
Unset minimum value constraint and enable non-binary mode.
Definition: ModRoundnessT.hh:238
void set_error_tolerance_factor(double _factor)
set the percentage of minimum roundness
Definition: ModRoundnessT.hh:182
~ModRoundnessT()
Destructor.
Definition: ModRoundnessT.hh:111
osg::Vec3f cross(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Definition: VectorAdapter.hh:196
#define DECIMATING_MODULE(Classname, MeshT, Name)
Convenience macro, to be used in derived modules The macro defines the types.
Definition: ModBaseT.hh:154
Mesh::VertexHandle v0
Vertex to be removed.
Definition: CollapseInfoT.hh:97
Helper class providing information about a vector type.
Definition: vector_traits.hh:93
Mesh::Point p1
Positions of remaining vertex.
Definition: CollapseInfoT.hh:100
Use Roundness of triangles to control decimation.
Definition: ModRoundnessT.hh:91
bool is_binary(void) const
Returns true if criteria returns a binary value.
Definition: ModBaseT.hh:225
Base class for all decimation modules.
void set_min_roundness(value_type _min_roundness, bool _binary=true)
Set a minimum roundness value.
Definition: ModRoundnessT.hh:230
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
Base class for all decimation modules.
Definition: ModBaseT.hh:197

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .