LoopT.hh 14.6 KB
Newer Older
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2013 by Computer Graphics Group, RWTH Aachen      *
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
33
34
35
36
37
38
39
40
 *                           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/>.                                      *
 *                                                                           *
\*===========================================================================*/ 

/*===========================================================================*\
 *                                                                           *             
 *   $Revision$                                                         *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
Jan Möbius committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

/** \file LoopT.hh

 */

//=============================================================================
//
//  CLASS LoopT
//
//=============================================================================

#ifndef OPENMESH_SUBDIVIDER_UNIFORM_LOOPT_HH
#define OPENMESH_SUBDIVIDER_UNIFORM_LOOPT_HH


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

#include <OpenMesh/Core/System/config.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/SubdividerT.hh>
#include <OpenMesh/Core/Utils/vector_cast.hh>
// -------------------- STL
#include <vector>
#if defined(OM_CC_MIPS)
#  include <math.h>
#else
#  include <cmath>
#endif


//== NAMESPACE ================================================================

namespace OpenMesh   { // BEGIN_NS_OPENMESH
namespace Subdivider { // BEGIN_NS_DECIMATER
namespace Uniform    { // BEGIN_NS_DECIMATER


//== CLASS DEFINITION =========================================================

/** %Uniform Loop subdivision algorithm.
 *
 *  Implementation as described in
 *
 *  C. T. Loop, "Smooth Subdivision Surfaces Based on Triangles",
 *  M.S. Thesis, Department of Mathematics, University of Utah, August 1987.
 *
 */
template <typename MeshType, typename RealType = float>
class LoopT : public SubdividerT<MeshType, RealType>
{
public:

  typedef RealType                                real_t;
  typedef MeshType                                mesh_t;
  typedef SubdividerT< mesh_t, real_t >           parent_t;

  typedef std::pair< real_t, real_t >             weight_t;
  typedef std::vector< std::pair<real_t,real_t> > weights_t;

public:


  LoopT(void) : parent_t(), _1over8( 1.0/8.0 ), _3over8( 3.0/8.0 )
  { init_weights(); }


  LoopT( mesh_t& _m ) : parent_t(_m), _1over8( 1.0/8.0 ), _3over8( 3.0/8.0 )
  { init_weights(); }


  ~LoopT() {}


public:


  const char *name() const { return "Uniform Loop"; }


  /// Pre-compute weights
  void init_weights(size_t _max_valence=50)
  {
    weights_.resize(_max_valence);
    std::generate(weights_.begin(), weights_.end(), compute_weight());
  }


protected:


  bool prepare( mesh_t& _m )
  {
    _m.add_property( vp_pos_ );
    _m.add_property( ep_pos_ );
    return true;
  }


  bool cleanup( mesh_t& _m )
  {
    _m.remove_property( vp_pos_ );
    _m.remove_property( ep_pos_ );
    return true;
  }


Jan Möbius's avatar
Jan Möbius committed
146
  bool subdivide( mesh_t& _m, size_t _n, const bool _update_points = true)
Jan Möbius's avatar
Jan Möbius committed
147
  {
148
149
150

    ///TODO:Implement fixed positions

Jan Möbius's avatar
Jan Möbius committed
151
152
153
154
155
156
157
158
    typename mesh_t::FaceIter   fit, f_end;
    typename mesh_t::EdgeIter   eit, e_end;
    typename mesh_t::VertexIter vit;

    // Do _n subdivisions
    for (size_t i=0; i < _n; ++i)
    {

159
160
161
      if(_update_points) {
        // compute new positions for old vertices
        for (vit = _m.vertices_begin(); vit != _m.vertices_end(); ++vit) {
Jan Möbius's avatar
Jan Möbius committed
162
          smooth(_m, *vit);
163
164
        }
      }
Jan Möbius's avatar
Jan Möbius committed
165
166
167

      // Compute position for new vertices and store them in the edge property
      for (eit=_m.edges_begin(); eit != _m.edges_end(); ++eit)
Jan Möbius's avatar
Jan Möbius committed
168
        compute_midpoint( _m, *eit );
Jan Möbius's avatar
Jan Möbius committed
169
170
171
172
173
174
175

      // Split each edge at midpoint and store precomputed positions (stored in
      // edge property ep_pos_) in the vertex property vp_pos_;

      // Attention! Creating new edges, hence make sure the loop ends correctly.
      e_end = _m.edges_end();
      for (eit=_m.edges_begin(); eit != e_end; ++eit)
Jan Möbius's avatar
Jan Möbius committed
176
        split_edge(_m, *eit );
Jan Möbius's avatar
Jan Möbius committed
177
178
179
180
181
182
183


      // Commit changes in topology and reconsitute consistency

      // Attention! Creating new faces, hence make sure the loop ends correctly.
      f_end   = _m.faces_end();
      for (fit = _m.faces_begin(); fit != f_end; ++fit)
Jan Möbius's avatar
Jan Möbius committed
184
        split_face(_m, *fit );
Jan Möbius's avatar
Jan Möbius committed
185

186
187
188
189
      if(_update_points) {
        // Commit changes in geometry
        for ( vit  = _m.vertices_begin();
            vit != _m.vertices_end(); ++vit) {
Jan Möbius's avatar
Jan Möbius committed
190
            _m.set_point(*vit, _m.property( vp_pos_, *vit ) );
191
192
193
        }
      }

Jan Möbius's avatar
Jan Möbius committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

#if defined(_DEBUG) || defined(DEBUG)
      // Now we have an consistent mesh!
      assert( OpenMesh::Utils::MeshCheckerT<mesh_t>(_m).check() );
#endif
    }

    return true;
  }

private:

  /// Helper functor to compute weights for Loop-subdivision
  /// \internal
  struct compute_weight
  {
    compute_weight() : valence(-1) { }
    weight_t operator() (void)
    {
#if !defined(OM_CC_MIPS)
      using std::cos;
#endif
      //              1
217
      // alpha(n) = ---- * (40 - ( 3 + 2 cos( 2 Pi / n ) )� )
Jan Möbius's avatar
Jan Möbius committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
      //             64

      if (++valence)
      {
        double   inv_v  = 1.0/double(valence);
        double   t      = (3.0 + 2.0 * cos( 2.0 * M_PI * inv_v) );
        double   alpha  = (40.0 - t * t)/64.0;

        return weight_t( 1.0-alpha, inv_v*alpha);
      }
      return weight_t(0.0, 0.0);
    }
    int valence;
  };

private: // topological modifiers

  void split_face(mesh_t& _m, const typename mesh_t::FaceHandle& _fh)
  {
    typename mesh_t::HalfedgeHandle
      heh1(_m.halfedge_handle(_fh)),
      heh2(_m.next_halfedge_handle(_m.next_halfedge_handle(heh1))),
      heh3(_m.next_halfedge_handle(_m.next_halfedge_handle(heh2)));

    // Cutting off every corner of the 6_gon
    corner_cutting( _m, heh1 );
    corner_cutting( _m, heh2 );
    corner_cutting( _m, heh3 );
  }


  void corner_cutting(mesh_t& _m, const typename mesh_t::HalfedgeHandle& _he)
  {
    // Define Halfedge Handles
    typename mesh_t::HalfedgeHandle
      heh1(_he),
      heh5(heh1),
      heh6(_m.next_halfedge_handle(heh1));

    // Cycle around the polygon to find correct Halfedge
    for (; _m.next_halfedge_handle(_m.next_halfedge_handle(heh5)) != heh1;
         heh5 = _m.next_halfedge_handle(heh5))
    {}

    typename mesh_t::VertexHandle
      vh1 = _m.to_vertex_handle(heh1),
      vh2 = _m.to_vertex_handle(heh5);

    typename mesh_t::HalfedgeHandle
      heh2(_m.next_halfedge_handle(heh5)),
      heh3(_m.new_edge( vh1, vh2)),
      heh4(_m.opposite_halfedge_handle(heh3));

    /* Intermediate result
     *
     *            *
     *         5 /|\
     *          /_  \
     *    vh2> *     *
     *        /|\3   |\
     *       /_  \|4   \
     *      *----\*----\*
     *          1 ^   6
     *            vh1 (adjust_outgoing halfedge!)
     */

    // Old and new Face
    typename mesh_t::FaceHandle     fh_old(_m.face_handle(heh6));
    typename mesh_t::FaceHandle     fh_new(_m.new_face());


    // Re-Set Handles around old Face
    _m.set_next_halfedge_handle(heh4, heh6);
    _m.set_next_halfedge_handle(heh5, heh4);

    _m.set_face_handle(heh4, fh_old);
    _m.set_face_handle(heh5, fh_old);
    _m.set_face_handle(heh6, fh_old);
    _m.set_halfedge_handle(fh_old, heh4);

    // Re-Set Handles around new Face
    _m.set_next_halfedge_handle(heh1, heh3);
    _m.set_next_halfedge_handle(heh3, heh2);

    _m.set_face_handle(heh1, fh_new);
    _m.set_face_handle(heh2, fh_new);
    _m.set_face_handle(heh3, fh_new);

    _m.set_halfedge_handle(fh_new, heh1);
  }


  void split_edge(mesh_t& _m, const typename mesh_t::EdgeHandle& _eh)
  {
    typename mesh_t::HalfedgeHandle
      heh     = _m.halfedge_handle(_eh, 0),
      opp_heh = _m.halfedge_handle(_eh, 1);

    typename mesh_t::HalfedgeHandle new_heh, opp_new_heh, t_heh;
    typename mesh_t::VertexHandle   vh;
    typename mesh_t::VertexHandle   vh1(_m.to_vertex_handle(heh));
319
320
321
    typename mesh_t::Point          midP(_m.point(_m.to_vertex_handle(heh)));
    midP += _m.point(_m.to_vertex_handle(opp_heh));
    midP *= 0.5;
Jan Möbius's avatar
Jan Möbius committed
322
323

    // new vertex
324
    vh                = _m.new_vertex( midP );
Jan Möbius's avatar
Jan Möbius committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405

    // memorize position, will be set later
    _m.property( vp_pos_, vh ) = _m.property( ep_pos_, _eh );


    // Re-link mesh entities
    if (_m.is_boundary(_eh))
    {
      for (t_heh = heh;
           _m.next_halfedge_handle(t_heh) != opp_heh;
           t_heh = _m.opposite_halfedge_handle(_m.next_halfedge_handle(t_heh)))
      {}
    }
    else
    {
      for (t_heh = _m.next_halfedge_handle(opp_heh);
           _m.next_halfedge_handle(t_heh) != opp_heh;
           t_heh = _m.next_halfedge_handle(t_heh) )
      {}
    }

    new_heh     = _m.new_edge(vh, vh1);
    opp_new_heh = _m.opposite_halfedge_handle(new_heh);
    _m.set_vertex_handle( heh, vh );

    _m.set_next_halfedge_handle(t_heh, opp_new_heh);
    _m.set_next_halfedge_handle(new_heh, _m.next_halfedge_handle(heh));
    _m.set_next_halfedge_handle(heh, new_heh);
    _m.set_next_halfedge_handle(opp_new_heh, opp_heh);

    if (_m.face_handle(opp_heh).is_valid())
    {
      _m.set_face_handle(opp_new_heh, _m.face_handle(opp_heh));
      _m.set_halfedge_handle(_m.face_handle(opp_new_heh), opp_new_heh);
    }

    _m.set_face_handle( new_heh, _m.face_handle(heh) );
    _m.set_halfedge_handle( vh, new_heh);
    _m.set_halfedge_handle( _m.face_handle(heh), heh );
    _m.set_halfedge_handle( vh1, opp_new_heh );

    // Never forget this, when playing with the topology
    _m.adjust_outgoing_halfedge( vh );
    _m.adjust_outgoing_halfedge( vh1 );
  }

private: // geometry helper

  void compute_midpoint(mesh_t& _m, const typename mesh_t::EdgeHandle& _eh)
  {
#define V( X ) vector_cast< typename mesh_t::Normal >( X )
    typename mesh_t::HalfedgeHandle heh, opp_heh;

    heh      = _m.halfedge_handle( _eh, 0);
    opp_heh  = _m.halfedge_handle( _eh, 1);

    typename mesh_t::Point
      pos(_m.point(_m.to_vertex_handle(heh)));

    pos += V( _m.point(_m.to_vertex_handle(opp_heh)) );

    // boundary edge: just average vertex positions
    if (_m.is_boundary(_eh) )
    {
      pos *= 0.5;
    }
    else // inner edge: add neighbouring Vertices to sum
    {
      pos *= real_t(3.0);
      pos += V(_m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh))));
      pos += V(_m.point(_m.to_vertex_handle(_m.next_halfedge_handle(opp_heh))));
      pos *= _1over8;
    }
    _m.property( ep_pos_, _eh ) = pos;
#undef V
  }

  void smooth(mesh_t& _m, const typename mesh_t::VertexHandle& _vh)
  {
    typename mesh_t::Point            pos(0.0,0.0,0.0);

406
    if (_m.is_boundary(_vh) ) // if boundary: Point 1-6-1
Jan Möbius's avatar
Jan Möbius committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
    {
      typename mesh_t::HalfedgeHandle heh, prev_heh;
      heh      = _m.halfedge_handle( _vh );

      if ( heh.is_valid() )
      {
        assert( _m.is_boundary( _m.edge_handle( heh ) ) );

        prev_heh = _m.prev_halfedge_handle( heh );

        typename mesh_t::VertexHandle
          to_vh   = _m.to_vertex_handle( heh ),
          from_vh = _m.from_vertex_handle( prev_heh );

        // ( v_l + 6 v + v_r ) / 8
        pos  = _m.point( _vh );
        pos *= real_t(6.0);
        pos += vector_cast< typename mesh_t::Normal >( _m.point( to_vh ) );
        pos += vector_cast< typename mesh_t::Normal >( _m.point( from_vh ) );
        pos *= _1over8;

      }
      else
        return;
    }
    else // inner vertex: (1-a) * p + a/n * Sum q, q in one-ring of p
    {
      typedef typename mesh_t::Normal   Vec;
      typename mesh_t::VertexVertexIter vvit;
      size_t                            valence(0);

      // Calculate Valence and sum up neighbour points
Jan Möbius's avatar
Jan Möbius committed
439
      for (vvit=_m.vv_iter(_vh); vvit.is_valid(); ++vvit) {
Jan Möbius's avatar
Jan Möbius committed
440
        ++valence;
Jan Möbius's avatar
Jan Möbius committed
441
        pos += vector_cast< Vec >( _m.point(*vvit) );
Jan Möbius's avatar
Jan Möbius committed
442
443
444
      }
      pos *= weights_[valence].second; // alpha(n)/n * Sum q, q in one-ring of p
      pos += weights_[valence].first
Jan Möbius's avatar
Jan Möbius committed
445
          * vector_cast<Vec>(_m.point(_vh)); // + (1-a)*p
Jan Möbius's avatar
Jan Möbius committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
    }

    _m.property( vp_pos_, _vh ) = pos;
  }

private: // data

  OpenMesh::VPropHandleT< typename mesh_t::Point > vp_pos_;
  OpenMesh::EPropHandleT< typename mesh_t::Point > ep_pos_;

  weights_t     weights_;

  const real_t _1over8;
  const real_t _3over8;

};


//=============================================================================
} // END_NS_UNIFORM
} // END_NS_SUBDIVIDER
} // END_NS_OPENMESH
//=============================================================================
#endif // OPENMESH_SUBDIVIDER_UNIFORM_COMPOSITELOOPT_HH defined
//=============================================================================