Developer Documentation
ModQuadricT.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 
53 //=============================================================================
54 //
55 // CLASS ModQuadric - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 #define OPENMESH_DECIMATER_MODQUADRIC_CC
60 
61 //== INCLUDES =================================================================
62 
64 
65 
66 //== NAMESPACE ===============================================================
67 
68 namespace OpenMesh { // BEGIN_NS_OPENMESH
69 namespace Decimater { // BEGIN_NS_DECIMATER
70 
71 
72 //== IMPLEMENTATION ==========================================================
73 
74 
75 template<class DecimaterType>
76 void
79 {
80  using Geometry::Quadricd;
81  // alloc quadrics
82  if (!quadrics_.is_valid())
83  Base::mesh().add_property( quadrics_ );
84 
85  // clear quadrics
86  typename Mesh::VertexIter v_it = Base::mesh().vertices_begin(),
87  v_end = Base::mesh().vertices_end();
88 
89  for (; v_it != v_end; ++v_it)
90  Base::mesh().property(quadrics_, *v_it).clear();
91 
92  // calc (normal weighted) quadric
93  typename Mesh::FaceIter f_it = Base::mesh().faces_begin(),
94  f_end = Base::mesh().faces_end();
95 
96  typename Mesh::FaceVertexIter fv_it;
97  typename Mesh::VertexHandle vh0, vh1, vh2;
98  typedef Vec3d Vec3;
99 
100  for (; f_it != f_end; ++f_it)
101  {
102  fv_it = Base::mesh().fv_iter(*f_it);
103  vh0 = *fv_it; ++fv_it;
104  vh1 = *fv_it; ++fv_it;
105  vh2 = *fv_it;
106 
107  Vec3 v0, v1, v2;
108  {
109  using namespace OpenMesh;
110 
111  v0 = vector_cast<Vec3>(Base::mesh().point(vh0));
112  v1 = vector_cast<Vec3>(Base::mesh().point(vh1));
113  v2 = vector_cast<Vec3>(Base::mesh().point(vh2));
114  }
115 
116  Vec3 n = (v1-v0) % (v2-v0);
117  double area = n.norm();
118  if (area > FLT_MIN)
119  {
120  n /= area;
121  area *= 0.5;
122  }
123 
124  const double a = n[0];
125  const double b = n[1];
126  const double c = n[2];
127  const double d = -(vector_cast<Vec3>(Base::mesh().point(vh0))|n);
128 
129  Quadricd q(a, b, c, d);
130  q *= area;
131 
132  Base::mesh().property(quadrics_, vh0) += q;
133  Base::mesh().property(quadrics_, vh1) += q;
134  Base::mesh().property(quadrics_, vh2) += q;
135  }
136 }
137 
138 //-----------------------------------------------------------------------------
139 
140 template<class MeshT>
142  if (this->is_binary()) {
143  if (_factor >= 0.0 && _factor <= 1.0) {
144  // the smaller the factor, the smaller max_err_ gets
145  // thus creating a stricter constraint
146  // division by error_tolerance_factor_ is for normalization
147  double max_err = max_err_ * _factor / this->error_tolerance_factor_;
148  set_max_err(max_err);
149  this->error_tolerance_factor_ = _factor;
150 
151  initialize();
152  }
153  }
154 }
155 
156 //=============================================================================
157 } // END_NS_DECIMATER
158 } // END_NS_OPENMESH
159 //=============================================================================
virtual void initialize(void)
Initalize the module and prepare the mesh for decimation.
Definition: ModQuadricT.cc:78
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
Kernel::FaceVertexIter FaceVertexIter
Circulator.
Definition: PolyMeshT.hh:170
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:139
void clear()
set all entries to zero
Definition: QuadricT.hh:171
void set_error_tolerance_factor(double _factor)
set the percentage of maximum quadric error
Definition: ModQuadricT.cc:141