OpenMesh
LoopT.hh
Go to the documentation of this file.
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2022, 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 
48 //=============================================================================
49 //
50 // CLASS LoopT
51 //
52 //=============================================================================
53 
54 #ifndef OPENMESH_SUBDIVIDER_UNIFORM_LOOPT_HH
55 #define OPENMESH_SUBDIVIDER_UNIFORM_LOOPT_HH
56 
57 
58 //== INCLUDES =================================================================
59 
60 #include <OpenMesh/Core/System/config.hh>
62 #include <OpenMesh/Core/Utils/vector_cast.hh>
63 #include <OpenMesh/Core/Utils/Property.hh>
64 // -------------------- STL
65 #include <vector>
66 #if defined(OM_CC_MIPS)
67 # include <math.h>
68 #else
69 # include <cmath>
70 #endif
71 
72 
73 //== NAMESPACE ================================================================
74 
75 namespace OpenMesh { // BEGIN_NS_OPENMESH
76 namespace Subdivider { // BEGIN_NS_DECIMATER
77 namespace Uniform { // BEGIN_NS_DECIMATER
78 
79 
80 //== CLASS DEFINITION =========================================================
81 
90 template <typename MeshType, typename RealType = double>
91 class LoopT : public SubdividerT<MeshType, RealType>
92 {
93 public:
94 
95  typedef RealType real_t;
96  typedef MeshType mesh_t;
98 
99  typedef std::pair< real_t, real_t > weight_t;
100  typedef std::vector< std::pair<real_t,real_t> > weights_t;
101 
102 public:
103 
104 
105  LoopT(void) : parent_t(), _1over8( 1.0/8.0 ), _3over8( 3.0/8.0 )
106  { init_weights(); }
107 
108 
109  explicit LoopT( mesh_t& _m ) : parent_t(_m), _1over8( 1.0/8.0 ), _3over8( 3.0/8.0 )
110  { init_weights(); }
111 
112 
113  ~LoopT() {}
114 
115 
116 public:
117 
118 
119  const char *name() const override { return "Uniform Loop"; }
120 
121 
123  void init_weights(size_t _max_valence=50)
124  {
125  weights_.resize(_max_valence);
126  std::generate(weights_.begin(), weights_.end(), compute_weight());
127  }
128 
129 
130 protected:
131 
132 
133  bool prepare( mesh_t& _m ) override
134  {
135  _m.add_property( vp_pos_ );
136  _m.add_property( ep_pos_ );
137  return true;
138  }
139 
140 
141  bool cleanup( mesh_t& _m ) override
142  {
143  _m.remove_property( vp_pos_ );
144  _m.remove_property( ep_pos_ );
145  return true;
146  }
147 
148 
149  bool subdivide( mesh_t& _m, size_t _n, const bool _update_points = true) override
150  {
151 
153 
154  typename mesh_t::FaceIter fit, f_end;
155  typename mesh_t::EdgeIter eit, e_end;
156  typename mesh_t::VertexIter vit;
157 
158  // Do _n subdivisions
159  for (size_t i=0; i < _n; ++i)
160  {
161 
162  if(_update_points) {
163  // compute new positions for old vertices
164  for (vit = _m.vertices_begin(); vit != _m.vertices_end(); ++vit) {
165  smooth(_m, *vit);
166  }
167  }
168 
169  // Compute position for new vertices and store them in the edge property
170  for (eit=_m.edges_begin(); eit != _m.edges_end(); ++eit)
171  compute_midpoint( _m, *eit );
172 
173  // Split each edge at midpoint and store precomputed positions (stored in
174  // edge property ep_pos_) in the vertex property vp_pos_;
175 
176  // Attention! Creating new edges, hence make sure the loop ends correctly.
177  for (auto eh : _m.edges())
178  split_edge(_m, eh );
179 
180 
181  // Commit changes in topology and reconsitute consistency
182 
183  // Attention! Creating new faces, hence make sure the loop ends correctly.
184  for (auto fh : _m.faces())
185  split_face(_m, fh );
186 
187  if(_update_points) {
188  // Commit changes in geometry
189  for ( vit = _m.vertices_begin();
190  vit != _m.vertices_end(); ++vit) {
191  _m.set_point(*vit, _m.property( vp_pos_, *vit ) );
192  }
193  }
194 
195 
196 #if defined(_DEBUG) || defined(DEBUG)
197  // Now we have an consistent mesh!
198  assert( OpenMesh::Utils::MeshCheckerT<mesh_t>(_m).check() );
199 #endif
200  }
201 
202  return true;
203  }
204 
205 private:
206 
209  struct compute_weight
210  {
211  compute_weight() : valence(-1) { }
212  weight_t operator() (void)
213  {
214 #if !defined(OM_CC_MIPS)
215  using std::cos;
216 #endif
217  // 1
218  // alpha(n) = ---- * (40 - ( 3 + 2 cos( 2 Pi / n ) )� )
219  // 64
220 
221  if (++valence)
222  {
223  double inv_v = 1.0/double(valence);
224  double t = (3.0 + 2.0 * cos( 2.0 * M_PI * inv_v) );
225  double alpha = (40.0 - t * t)/64.0;
226 
227  return weight_t( static_cast<real_t>(1.0-alpha), static_cast<real_t>(inv_v*alpha) );
228  }
229  return weight_t(static_cast<real_t>(0.0), static_cast<real_t>(0.0));
230  }
231  int valence;
232  };
233 
234 private: // topological modifiers
235 
236  void split_face(mesh_t& _m, const typename mesh_t::FaceHandle& _fh)
237  {
238  typename mesh_t::HalfedgeHandle
239  heh1(_m.halfedge_handle(_fh)),
240  heh2(_m.next_halfedge_handle(_m.next_halfedge_handle(heh1))),
241  heh3(_m.next_halfedge_handle(_m.next_halfedge_handle(heh2)));
242 
243  // Cutting off every corner of the 6_gon
244  corner_cutting( _m, heh1 );
245  corner_cutting( _m, heh2 );
246  corner_cutting( _m, heh3 );
247  }
248 
249 
250  void corner_cutting(mesh_t& _m, const typename mesh_t::HalfedgeHandle& _he)
251  {
252  // Define Halfedge Handles
253  typename mesh_t::HalfedgeHandle
254  heh1(_he),
255  heh5(heh1),
256  heh6(_m.next_halfedge_handle(heh1));
257 
258  // Cycle around the polygon to find correct Halfedge
259  for (; _m.next_halfedge_handle(_m.next_halfedge_handle(heh5)) != heh1;
260  heh5 = _m.next_halfedge_handle(heh5))
261  {}
262 
263  typename mesh_t::VertexHandle
264  vh1 = _m.to_vertex_handle(heh1),
265  vh2 = _m.to_vertex_handle(heh5);
266 
267  typename mesh_t::HalfedgeHandle
268  heh2(_m.next_halfedge_handle(heh5)),
269  heh3(_m.new_edge( vh1, vh2)),
270  heh4(_m.opposite_halfedge_handle(heh3));
271 
272  /* Intermediate result
273  *
274  * *
275  * 5 /|\
276  * /_ \
277  * vh2> * *
278  * /|\3 |\
279  * /_ \|4 \
280  * *----\*----\*
281  * 1 ^ 6
282  * vh1 (adjust_outgoing halfedge!)
283  */
284 
285  // Old and new Face
286  typename mesh_t::FaceHandle fh_old(_m.face_handle(heh6));
287  typename mesh_t::FaceHandle fh_new(_m.new_face());
288 
289 
290  // Re-Set Handles around old Face
291  _m.set_next_halfedge_handle(heh4, heh6);
292  _m.set_next_halfedge_handle(heh5, heh4);
293 
294  _m.set_face_handle(heh4, fh_old);
295  _m.set_face_handle(heh5, fh_old);
296  _m.set_face_handle(heh6, fh_old);
297  _m.set_halfedge_handle(fh_old, heh4);
298 
299  // Re-Set Handles around new Face
300  _m.set_next_halfedge_handle(heh1, heh3);
301  _m.set_next_halfedge_handle(heh3, heh2);
302 
303  _m.set_face_handle(heh1, fh_new);
304  _m.set_face_handle(heh2, fh_new);
305  _m.set_face_handle(heh3, fh_new);
306 
307  _m.set_halfedge_handle(fh_new, heh1);
308  }
309 
310 
311  void split_edge(mesh_t& _m, const typename mesh_t::EdgeHandle& _eh)
312  {
313  typename mesh_t::HalfedgeHandle
314  heh = _m.halfedge_handle(_eh, 0),
315  opp_heh = _m.halfedge_handle(_eh, 1);
316 
317  typename mesh_t::HalfedgeHandle new_heh, opp_new_heh, t_heh;
318  typename mesh_t::VertexHandle vh;
319  typename mesh_t::VertexHandle vh1(_m.to_vertex_handle(heh));
320  typename mesh_t::Point midP(_m.point(_m.to_vertex_handle(heh)));
321  midP += _m.point(_m.to_vertex_handle(opp_heh));
322  midP *= static_cast<RealType>(0.5);
323 
324  // new vertex
325  vh = _m.new_vertex( midP );
326 
327  // memorize position, will be set later
328  _m.property( vp_pos_, vh ) = _m.property( ep_pos_, _eh );
329 
330 
331  // Re-link mesh entities
332  if (_m.is_boundary(_eh))
333  {
334  for (t_heh = heh;
335  _m.next_halfedge_handle(t_heh) != opp_heh;
336  t_heh = _m.opposite_halfedge_handle(_m.next_halfedge_handle(t_heh)))
337  {}
338  }
339  else
340  {
341  for (t_heh = _m.next_halfedge_handle(opp_heh);
342  _m.next_halfedge_handle(t_heh) != opp_heh;
343  t_heh = _m.next_halfedge_handle(t_heh) )
344  {}
345  }
346 
347  new_heh = _m.new_edge(vh, vh1);
348  opp_new_heh = _m.opposite_halfedge_handle(new_heh);
349  _m.set_vertex_handle( heh, vh );
350 
351  _m.set_next_halfedge_handle(t_heh, opp_new_heh);
352  _m.set_next_halfedge_handle(new_heh, _m.next_halfedge_handle(heh));
353  _m.set_next_halfedge_handle(heh, new_heh);
354  _m.set_next_halfedge_handle(opp_new_heh, opp_heh);
355 
356  if (_m.face_handle(opp_heh).is_valid())
357  {
358  _m.set_face_handle(opp_new_heh, _m.face_handle(opp_heh));
359  _m.set_halfedge_handle(_m.face_handle(opp_new_heh), opp_new_heh);
360  }
361 
362  _m.set_face_handle( new_heh, _m.face_handle(heh) );
363  _m.set_halfedge_handle( vh, new_heh);
364 
365  // We cant reconnect a non existing face, so we skip this here if necessary
366  if ( !_m.is_boundary(heh) )
367  _m.set_halfedge_handle( _m.face_handle(heh), heh );
368 
369  _m.set_halfedge_handle( vh1, opp_new_heh );
370 
371  // Never forget this, when playing with the topology
372  _m.adjust_outgoing_halfedge( vh );
373  _m.adjust_outgoing_halfedge( vh1 );
374  }
375 
376 private: // geometry helper
377 
378  void compute_midpoint(mesh_t& _m, const typename mesh_t::EdgeHandle& _eh)
379  {
380 #define V( X ) vector_cast< typename mesh_t::Normal >( X )
381  typename mesh_t::HalfedgeHandle heh, opp_heh;
382 
383  heh = _m.halfedge_handle( _eh, 0);
384  opp_heh = _m.halfedge_handle( _eh, 1);
385 
386  typename mesh_t::Point
387  pos(_m.point(_m.to_vertex_handle(heh)));
388 
389  pos += V( _m.point(_m.to_vertex_handle(opp_heh)) );
390 
391  // boundary edge: just average vertex positions
392  if (_m.is_boundary(_eh) )
393  {
394  pos *= static_cast<RealType>(0.5);
395  }
396  else // inner edge: add neighbouring Vertices to sum
397  {
398  pos *= real_t(3.0);
399  pos += V(_m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh))));
400  pos += V(_m.point(_m.to_vertex_handle(_m.next_halfedge_handle(opp_heh))));
401  pos *= _1over8;
402  }
403  _m.property( ep_pos_, _eh ) = pos;
404 #undef V
405  }
406 
407  void smooth(mesh_t& _m, const typename mesh_t::VertexHandle& _vh)
408  {
409  typename mesh_t::Point pos(0.0,0.0,0.0);
410 
411  if (_m.is_boundary(_vh) ) // if boundary: Point 1-6-1
412  {
413  typename mesh_t::HalfedgeHandle heh, prev_heh;
414  heh = _m.halfedge_handle( _vh );
415 
416  if ( heh.is_valid() )
417  {
418  assert( _m.is_boundary( _m.edge_handle( heh ) ) );
419 
420  prev_heh = _m.prev_halfedge_handle( heh );
421 
422  typename mesh_t::VertexHandle
423  to_vh = _m.to_vertex_handle( heh ),
424  from_vh = _m.from_vertex_handle( prev_heh );
425 
426  // ( v_l + 6 v + v_r ) / 8
427  pos = _m.point( _vh );
428  pos *= real_t(6.0);
429  pos += vector_cast< typename mesh_t::Normal >( _m.point( to_vh ) );
430  pos += vector_cast< typename mesh_t::Normal >( _m.point( from_vh ) );
431  pos *= _1over8;
432 
433  }
434  else
435  return;
436  }
437  else // inner vertex: (1-a) * p + a/n * Sum q, q in one-ring of p
438  {
439  typedef typename mesh_t::Normal Vec;
440  typename mesh_t::VertexVertexIter vvit;
441  size_t valence(0);
442 
443  // Calculate Valence and sum up neighbour points
444  for (vvit=_m.vv_iter(_vh); vvit.is_valid(); ++vvit) {
445  ++valence;
446  pos += vector_cast< Vec >( _m.point(*vvit) );
447  }
448  pos *= weights_[valence].second; // alpha(n)/n * Sum q, q in one-ring of p
449  pos += weights_[valence].first
450  * vector_cast<Vec>(_m.point(_vh)); // + (1-a)*p
451  }
452 
453  _m.property( vp_pos_, _vh ) = pos;
454  }
455 
456 private: // data
457 
460 
461  weights_t weights_;
462 
463  const real_t _1over8;
464  const real_t _3over8;
465 
466 };
467 
468 
469 //=============================================================================
470 } // END_NS_UNIFORM
471 } // END_NS_SUBDIVIDER
472 } // END_NS_OPENMESH
473 //=============================================================================
474 #endif // OPENMESH_SUBDIVIDER_UNIFORM_COMPOSITELOOPT_HH defined
475 //=============================================================================
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
Triangle mesh based on the ArrayKernel.
Definition: TriMesh_ArrayKernelT.hh:96
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
Kernel::EdgeHandle EdgeHandle
Scalar type.
Definition: PolyMeshT.hh:138
Kernel::FaceIter FaceIter
Scalar type.
Definition: PolyMeshT.hh:146
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
SmartVertexHandle new_vertex()
Uses default copy and assignment operator.
Definition: PolyMeshT.hh:201
Kernel::FaceHandle FaceHandle
Scalar type.
Definition: PolyMeshT.hh:139
Kernel::HalfedgeHandle HalfedgeHandle
Scalar type.
Definition: PolyMeshT.hh:137
Kernel::EdgeIter EdgeIter
Scalar type.
Definition: PolyMeshT.hh:145
Kernel::VertexVertexIter VertexVertexIter
Circulator.
Definition: PolyMeshT.hh:162
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
Kernel::VertexIter VertexIter
Scalar type.
Definition: PolyMeshT.hh:143
Uniform Loop subdivision algorithm.
Definition: LoopT.hh:92
bool cleanup(mesh_t &_m) override
Cleanup mesh after usage, e.g. remove added properties.
Definition: LoopT.hh:141
const char * name() const override
Return name of subdivision algorithm.
Definition: LoopT.hh:119
void init_weights(size_t _max_valence=50)
Pre-compute weights.
Definition: LoopT.hh:123
bool prepare(mesh_t &_m) override
Prepare mesh, e.g. add properties.
Definition: LoopT.hh:133
bool subdivide(mesh_t &_m, size_t _n, const bool _update_points=true) override
Subdivide mesh _m _n times.
Definition: LoopT.hh:149
Abstract base class for uniform subdivision algorithms.
Definition: SubdividerT.hh:89
bool operator()(MeshType &_m, size_t _n, const bool _update_points=true)
Subdivide the mesh _m _n times.
Definition: SubdividerT.hh:122
Check integrity of mesh.
Definition: MeshCheckerT.hh:74

Project OpenMesh, ©  Visual Computing Institute, RWTH Aachen. Documentation generated using doxygen .