Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
LaplaceSmootherT.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 LaplaceSmootherT - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 #define OPENMESH_LAPLACE_SMOOTHERT_C
60 
61 //== INCLUDES =================================================================
62 
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 
69 namespace OpenMesh {
70 namespace Smoother {
71 
72 
73 //== IMPLEMENTATION ==========================================================
74 
75 
76 template <class Mesh>
77 LaplaceSmootherT<Mesh>::
78 LaplaceSmootherT(Mesh& _mesh)
79  : SmootherT<Mesh>(_mesh)
80 {
81  // custom properties
82  Base::mesh_.add_property(vertex_weights_);
83  Base::mesh_.add_property(edge_weights_);
84 }
85 
86 
87 //-----------------------------------------------------------------------------
88 
89 
90 template <class Mesh>
91 LaplaceSmootherT<Mesh>::
92 ~LaplaceSmootherT()
93 {
94  // free custom properties
95  Base::mesh_.remove_property(vertex_weights_);
96  Base::mesh_.remove_property(edge_weights_);
97 }
98 
99 
100 //-----------------------------------------------------------------------------
101 
102 
103 template <class Mesh>
104 void
105 LaplaceSmootherT<Mesh>::
106 initialize(Component _comp, Continuity _cont)
107 {
108  SmootherT<Mesh>::initialize(_comp, _cont);
109 
110  // calculate weights
111  switch (_comp)
112  {
113  case Base::Tangential:
114  compute_weights(UniformWeighting);
115  break;
116 
117 
118  case Base::Normal:
119  compute_weights(CotWeighting);
120  break;
121 
122 
123  case Base::Tangential_and_Normal:
124  compute_weights(UniformWeighting);
125  break;
126  }
127 }
128 
129 
130 //-----------------------------------------------------------------------------
131 
132 
133 template <class Mesh>
134 void
135 LaplaceSmootherT<Mesh>::
136 compute_weights(LaplaceWeighting _weighting)
137 {
138  typename Mesh::VertexIter v_it, v_end(Base::mesh_.vertices_end());
139  typename Mesh::EdgeIter e_it, e_end(Base::mesh_.edges_end());
140  typename Mesh::HalfedgeHandle heh0, heh1, heh2;
141  typename Mesh::VertexHandle v0, v1;
142  const typename Mesh::Point *p0, *p1, *p2;
143  typename Mesh::Normal d0, d1;
144  typename Mesh::Scalar weight, lb(-1.0), ub(1.0);
145 
146 
147 
148  // init vertex weights
149  for (v_it=Base::mesh_.vertices_begin(); v_it!=v_end; ++v_it)
150  Base::mesh_.property(vertex_weights_, *v_it) = 0.0;
151 
152 
153 
154  switch (_weighting)
155  {
156  // Uniform weighting
157  case UniformWeighting:
158  {
159  for (e_it=Base::mesh_.edges_begin(); e_it!=e_end; ++e_it)
160  {
161  heh0 = Base::mesh_.halfedge_handle(*e_it, 0);
162  heh1 = Base::mesh_.halfedge_handle(*e_it, 1);
163  v0 = Base::mesh_.to_vertex_handle(heh0);
164  v1 = Base::mesh_.to_vertex_handle(heh1);
165 
166  Base::mesh_.property(edge_weights_, *e_it) = 1.0;
167  Base::mesh_.property(vertex_weights_, v0) += 1.0;
168  Base::mesh_.property(vertex_weights_, v1) += 1.0;
169  }
170 
171  break;
172  }
173 
174 
175  // Cotangent weighting
176  case CotWeighting:
177  {
178  for (e_it=Base::mesh_.edges_begin(); e_it!=e_end; ++e_it)
179  {
180  weight = 0.0;
181 
182  heh0 = Base::mesh_.halfedge_handle(*e_it, 0);
183  v0 = Base::mesh_.to_vertex_handle(heh0);
184  p0 = &Base::mesh_.point(v0);
185 
186  heh1 = Base::mesh_.halfedge_handle(*e_it, 1);
187  v1 = Base::mesh_.to_vertex_handle(heh1);
188  p1 = &Base::mesh_.point(v1);
189 
190  heh2 = Base::mesh_.next_halfedge_handle(heh0);
191  p2 = &Base::mesh_.point(Base::mesh_.to_vertex_handle(heh2));
192  d0 = (*p0 - *p2); d0.normalize();
193  d1 = (*p1 - *p2); d1.normalize();
194  weight += static_cast<typename Mesh::Scalar>(1.0) / tan(acos(std::max(lb, std::min(ub, dot(d0,d1) ))));
195 
196  heh2 = Base::mesh_.next_halfedge_handle(heh1);
197  p2 = &Base::mesh_.point(Base::mesh_.to_vertex_handle(heh2));
198  d0 = (*p0 - *p2); d0.normalize();
199  d1 = (*p1 - *p2); d1.normalize();
200  weight += static_cast<typename Mesh::Scalar>(1.0) / tan(acos(std::max(lb, std::min(ub, dot(d0,d1) ))));
201 
202  Base::mesh_.property(edge_weights_, *e_it) = weight;
203  Base::mesh_.property(vertex_weights_, v0) += weight;
204  Base::mesh_.property(vertex_weights_, v1) += weight;
205  }
206  break;
207  }
208  }
209 
210 
211  // invert vertex weights:
212  // before: sum of edge weights
213  // after: one over sum of edge weights
214  for (v_it=Base::mesh_.vertices_begin(); v_it!=v_end; ++v_it)
215  {
216  weight = Base::mesh_.property(vertex_weights_, *v_it);
217  if (weight)
218  Base::mesh_.property(vertex_weights_, *v_it) = static_cast<typename Mesh::Scalar>(1.0) / weight;
219  }
220 }
221 
222 
223 
224 //=============================================================================
225 } // namespace Smoother
226 } // namespace OpenMesh
227 //=============================================================================
void initialize(Component _comp, Continuity _cont)
Definition: SmootherT.cc:127
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:115
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:113
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:139
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:87
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:117