CompositeT.cc 37 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
2
3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo    
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
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
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
 *                                                                           *
 * ========================================================================= */
41
42
43
44
45
46
47

/*===========================================================================*\
 *                                                                           *             
 *   $Revision$                                                         *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
Jan Möbius committed
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

/** \file Uniform/Composite/CompositeT.cc

 */

//=============================================================================
//
//  CLASS CompositeT - IMPLEMENTATION
//
//=============================================================================

#ifndef OPENMESH_SUBDIVIDER_UNIFORM_COMPOSITE_CC
#define OPENMESH_SUBDIVIDER_UNIFORM_COMPOSITE_CC


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


#include <vector>
#include <OpenMesh/Tools/Subdivider/Uniform/Composite/CompositeT.hh>


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

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


//== IMPLEMENTATION ==========================================================


template <typename MeshType, typename RealType>
bool CompositeT<MeshType,RealType>::prepare( MeshType& _m )
{
  // store mesh for later usage in subdivide(), cleanup() and all rules.
  p_mesh_ = &_m;

  typename MeshType::VertexIter v_it(_m.vertices_begin());

  for (; v_it != _m.vertices_end(); ++v_it)
Jan Möbius's avatar
Jan Möbius committed
89
    _m.data(*v_it).set_position(_m.point(*v_it));
Jan Möbius's avatar
Jan Möbius committed
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

  return true;
}



template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::Tvv3()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::VertexHandle vh;
  typename MeshType::FaceIter     f_it;
  typename MeshType::EdgeIter     e_it;
  typename MeshType::VertexIter   v_it;
  typename MeshType::Point        zero_point(0.0, 0.0, 0.0);
  size_t                          n_edges, n_faces, n_vertices, j;

  // Store number of original edges
  n_faces    = mesh_.n_faces();
  n_edges    = mesh_.n_edges();
  n_vertices = mesh_.n_vertices();

  // reserve enough memory for iterator
  mesh_.reserve(n_vertices + n_faces, n_edges + 3 * n_faces, 3 * n_faces);

  // set new positions for vertices
  v_it = mesh_.vertices_begin();
  for (j = 0; j < n_vertices; ++j) {
Jan Möbius's avatar
Jan Möbius committed
119
    mesh_.data(*v_it).set_position(mesh_.data(*v_it).position() * 3.0);
Jan Möbius's avatar
Jan Möbius committed
120
121
122
123
124
125
126
127
128
129
130
    ++v_it;
  }

  // Split each face
  f_it = mesh_.faces_begin();
  for (j = 0; j < n_faces; ++j) {

    vh = mesh_.add_vertex(zero_point);

    mesh_.data(vh).set_position(zero_point);

Jan Möbius's avatar
Jan Möbius committed
131
    mesh_.split(*f_it, vh);
Jan Möbius's avatar
Jan Möbius committed
132
133
134
135
136
137
138
139
140
141

    ++f_it;
  }

  // Flip each old edge
  std::vector<typename MeshType::EdgeHandle> edge_vector;
  edge_vector.clear();

  e_it = mesh_.edges_begin();
  for (j = 0; j < n_edges; ++j) {
Jan Möbius's avatar
Jan Möbius committed
142
143
    if (mesh_.is_flip_ok(*e_it)) {
      mesh_.flip(*e_it);
Jan Möbius's avatar
Jan Möbius committed
144
    } else {
Jan Möbius's avatar
Jan Möbius committed
145
      edge_vector.push_back(*e_it);
Jan Möbius's avatar
Jan Möbius committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    }
    ++e_it;
  }

  // split all boundary edges
  while (!edge_vector.empty()) {
    vh = mesh_.add_vertex(zero_point);
    mesh_.data(vh).set_position(zero_point);
    mesh_.split(edge_vector.back(), vh);
    edge_vector.pop_back();
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::Tvv4()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::VertexHandle     vh;
  typename MeshType::FaceIter         f_it;
  typename MeshType::EdgeIter         e_it;
  typename MeshType::VertexIter       v_it;
  typename MeshType::Point            zero_point(0.0, 0.0, 0.0);
170
  size_t                              n_edges, n_faces, n_vertices, j;
Jan Möbius's avatar
Jan Möbius committed
171
172
173
174
175
176
177
178
179
180
181
182

  // Store number of original edges
  n_faces    = mesh_.n_faces();
  n_edges    = mesh_.n_edges();
  n_vertices = mesh_.n_vertices();

  // reserve memory ahead for the succeeding operations
  mesh_.reserve(n_vertices + n_edges, 2 * n_edges + 3 * n_faces, 4 * n_faces);

  // set new positions for vertices
  v_it = mesh_.vertices_begin();
  for (j = 0; j < n_vertices; ++j) {
Jan Möbius's avatar
Jan Möbius committed
183
    mesh_.data(*v_it).set_position(mesh_.data(*v_it).position() * 4.0);
Jan Möbius's avatar
Jan Möbius committed
184
185
186
187
188
189
190
    ++v_it;
  }

  // Split each edge
  e_it = mesh_.edges_begin();
  for (j = 0; j < n_edges; ++j) {

Jan Möbius's avatar
Jan Möbius committed
191
    vh = split_edge(mesh_.halfedge_handle(*e_it, 0));
Jan Möbius's avatar
Jan Möbius committed
192
193
194
195
196
197
198
199
    mesh_.data(vh).set_position(zero_point);

    ++e_it;
  }

  // Corner Cutting of Each Face
  f_it = mesh_.faces_begin();
  for (j = 0; j < n_faces; ++j) {
Jan Möbius's avatar
Jan Möbius committed
200
    typename MeshType::HalfedgeHandle heh1(mesh_.halfedge_handle(*f_it));
Jan Möbius's avatar
Jan Möbius committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    typename MeshType::HalfedgeHandle heh2(mesh_.next_halfedge_handle(mesh_.next_halfedge_handle(heh1)));
    typename MeshType::HalfedgeHandle heh3(mesh_.next_halfedge_handle(mesh_.next_halfedge_handle(heh2)));

    // Cutting off every corner of the 6_gon

    corner_cutting(heh1);
    corner_cutting(heh2);
    corner_cutting(heh3);

    ++f_it;
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::Tfv()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::VertexHandle     vh;
  typename MeshType::FaceIter         f_it;
  typename MeshType::EdgeIter         e_it;
  typename MeshType::VertexIter       v_it;
  typename MeshType::VertexFaceIter   vf_it;
  typename MeshType::FaceFaceIter     ff_it;
  typename MeshType::Point            cog;
  const typename MeshType::Point      zero_point(0.0, 0.0, 0.0);
228
  size_t                              n_edges, n_faces, n_vertices, j, valence;
Jan Möbius's avatar
Jan Möbius committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242

  // Store number of original edges
  n_faces = mesh_.n_faces();
  n_edges = mesh_.n_edges();
  n_vertices = mesh_.n_vertices();

  // reserve enough space for iterator
  mesh_.reserve(n_vertices + n_faces, n_edges + 3 * n_faces, 3 * n_faces);

  // set new positions for vertices
  v_it = mesh_.vertices_begin();
  for (j = 0; j < n_vertices; ++j) {
    valence = 0;
    cog = zero_point;
Jan Möbius's avatar
Jan Möbius committed
243
    for (vf_it = mesh_.vf_iter(*v_it); vf_it; ++vf_it) {
Jan Möbius's avatar
Jan Möbius committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
      ++valence;
      cog += vf_it->position();
    }
    cog /= valence;

    v_it->set_position(cog);
    ++v_it;
  }

  // Split each face, insert new vertex and calculate position
  f_it = mesh_.faces_begin();
  for (j = 0; j < n_faces; ++j) {

    vh = mesh_.add_vertex();

    valence = 0;
    cog = zero_point;
Jan Möbius's avatar
Jan Möbius committed
261
    for (ff_it = mesh_.ff_iter(*f_it); ff_it; ++ff_it) {
Jan Möbius's avatar
Jan Möbius committed
262
263
264
265
266
      ++valence;
      cog += ff_it->position();
    }
    cog /= valence;

Jan Möbius's avatar
Jan Möbius committed
267
    mesh_.split(*f_it, vh);
Jan Möbius's avatar
Jan Möbius committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

    for (vf_it = mesh_.vf_iter(vh); vf_it; ++vf_it) {
      vf_it->set_position(f_it->position());
    }

    mesh_.deref(vh).set_position(cog);

    mesh_.set_point(vh, cog);

    ++f_it;
  }

  // Flip each old edge
  e_it = mesh_.edges_begin();
  for (j = 0; j < n_edges; ++j) {
Jan Möbius's avatar
Jan Möbius committed
283
284
    if (mesh_.is_flip_ok(*e_it))
      mesh_.flip(*e_it);
Jan Möbius's avatar
Jan Möbius committed
285
286
287
288
289
290
291
292
293
294
295
    ++e_it;
  }
}



template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VF()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

Jan Möbius's avatar
Jan Möbius committed
296
  typename MeshType::Point          cog,zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
297
298
299
300
301
  typename MeshType::FaceVertexIter fv_it;
  typename MeshType::FaceIter       f_it;

  for (f_it = mesh_.faces_begin(); f_it != mesh_.faces_end(); ++f_it) {

Jan Möbius's avatar
Jan Möbius committed
302
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
303
304
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
305
    for (fv_it = mesh_.fv_iter(*f_it); fv_it.is_valid(); ++fv_it) {
Jan Möbius's avatar
Jan Möbius committed
306
      cog += mesh_.data(*fv_it).position();
Jan Möbius's avatar
Jan Möbius committed
307
308
309
      ++valence;
    }
    cog /= valence;
Jan Möbius's avatar
Jan Möbius committed
310
    mesh_.data(*f_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
311
312
313
314
315
316
317
318
319
320
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VFa(Coeff& _coeff)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  unsigned int                        valence[3], i;
Jan Möbius's avatar
Jan Möbius committed
321
  typename MeshType::Point                cog,zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
322
323
324
325
326
327
328
329
330
  typename MeshType::Scalar               alpha;
  typename MeshType::FaceIter             f_it;
  typename MeshType::HalfedgeHandle       heh;
  typename MeshType::VertexHandle         vh[3];
  typename MeshType::VertexOHalfedgeIter  voh_it;
  typename MeshType::FaceVertexIter       fv_it;

  for (f_it = mesh_.faces_begin(); f_it != mesh_.faces_end(); ++f_it) {

Jan Möbius's avatar
Jan Möbius committed
331
    heh = mesh_.halfedge_handle(*f_it);
Jan Möbius's avatar
Jan Möbius committed
332
333
334
335
336
337
    for (i = 0; i <= 2; ++i) {

      valence[i] = 0;
      vh[i] = mesh_.to_vertex_handle(heh);

      for (voh_it = mesh_.voh_iter(vh[i]); voh_it; ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
338
        ++valence[i];
Jan Möbius's avatar
Jan Möbius committed
339
340
341
342
343
344
345
      }

      heh = mesh_.next_halfedge_handle(heh);
    }

    if (valence[0] <= valence[1])
      if (valence[0] <= valence[2])
Jan Möbius's avatar
Jan Möbius committed
346
        i = 0;
Jan Möbius's avatar
Jan Möbius committed
347
      else
Jan Möbius's avatar
Jan Möbius committed
348
        i = 2;
Jan Möbius's avatar
Jan Möbius committed
349
350
    else
      if (valence[1] <= valence[2])
Jan Möbius's avatar
Jan Möbius committed
351
        i = 1;
Jan Möbius's avatar
Jan Möbius committed
352
      else
Jan Möbius's avatar
Jan Möbius committed
353
        i = 2;
Jan Möbius's avatar
Jan Möbius committed
354
355
356
357
358

    alpha = _coeff(valence[i]);

    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
359
    for (fv_it = mesh_.fv_iter(*f_it); fv_it.is_valid(); ++fv_it) {
Jan Möbius's avatar
Jan Möbius committed
360
361
      if (*fv_it == vh[i]) {
        cog += fv_it->position() * alpha;
Jan Möbius's avatar
Jan Möbius committed
362
      } else {
Jan Möbius's avatar
Jan Möbius committed
363
        cog += fv_it->position() * (1.0 - alpha) / 2.0;
Jan Möbius's avatar
Jan Möbius committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
      }
    }

    f_it->set_position(cog);
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VFa(scalar_t _alpha)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  unsigned int                        valence[3], i;
  typename MeshType::Point                cog,
Jan Möbius's avatar
Jan Möbius committed
379
  zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
380
381
382
383
384
385
386
387
  typename MeshType::FaceIter             f_it;
  typename MeshType::HalfedgeHandle       heh;
  typename MeshType::VertexHandle         vh[3];
  typename MeshType::VertexOHalfedgeIter  voh_it;
  typename MeshType::FaceVertexIter       fv_it;

  for (f_it = mesh_.faces_begin(); f_it != mesh_.faces_end(); ++f_it) {

Jan Möbius's avatar
Jan Möbius committed
388
    heh = mesh_.halfedge_handle(*f_it);
Jan Möbius's avatar
Jan Möbius committed
389
390
391
392
393
394
    for (i = 0; i <= 2; ++i) {

      valence[i] = 0;
      vh[i] = mesh_.to_vertex_handle(heh);

      for (voh_it = mesh_.voh_iter(vh[i]); voh_it; ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
395
        ++valence[i];
Jan Möbius's avatar
Jan Möbius committed
396
397
398
399
400
401
402
      }

      heh = mesh_.next_halfedge_handle(heh);
    }

    if (valence[0] <= valence[1])
      if (valence[0] <= valence[2])
Jan Möbius's avatar
Jan Möbius committed
403
        i = 0;
Jan Möbius's avatar
Jan Möbius committed
404
      else
Jan Möbius's avatar
Jan Möbius committed
405
        i = 2;
Jan Möbius's avatar
Jan Möbius committed
406
407
    else
      if (valence[1] <= valence[2])
Jan Möbius's avatar
Jan Möbius committed
408
        i = 1;
Jan Möbius's avatar
Jan Möbius committed
409
      else
Jan Möbius's avatar
Jan Möbius committed
410
        i = 2;
Jan Möbius's avatar
Jan Möbius committed
411
412
413

    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
414
    for (fv_it = mesh_.fv_iter(*f_it); fv_it.is_valid(); ++fv_it) {
Jan Möbius's avatar
Jan Möbius committed
415
416
      if (*fv_it == vh[i]) {
        cog += fv_it->position() * _alpha;
Jan Möbius's avatar
Jan Möbius committed
417
      } else {
Jan Möbius's avatar
Jan Möbius committed
418
        cog += fv_it->position() * (1.0 - _alpha) / 2.0;
Jan Möbius's avatar
Jan Möbius committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
      }
    }

    f_it->set_position(cog);
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::FF()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::Point              cog,
                                    zero_point(0.0, 0.0, 0.0);
  typename MeshType::FaceFaceIter       ff_it;
  typename MeshType::FaceIter           f_it;
  std::vector<typename MeshType::Point> point_vector;

  point_vector.clear();

  for (f_it = mesh_.faces_begin(); f_it != mesh_.faces_end(); ++f_it)
  {
Jan Möbius's avatar
Jan Möbius committed
442
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
443
444
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
445
    for (ff_it = mesh_.ff_iter(*f_it); ff_it.is_valid(); ++ff_it)
Jan Möbius's avatar
Jan Möbius committed
446
    {
Jan Möbius's avatar
Jan Möbius committed
447
      cog += mesh_.data(*ff_it).position();
Jan Möbius's avatar
Jan Möbius committed
448
449
450
451
452
453
454
455
456
      ++valence;
    }
    cog /= valence;
    point_vector.push_back(cog);
  }

  for (f_it = mesh_.faces_end(); f_it != mesh_.faces_begin(); )
  {
    --f_it;
Jan Möbius's avatar
Jan Möbius committed
457
    mesh_.data(*f_it).set_position(point_vector.back());
Jan Möbius's avatar
Jan Möbius committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
    point_vector.pop_back();
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::FFc(Coeff& _coeff)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::Point           cog,
                                 zero_point(0.0, 0.0, 0.0);
  typename MeshType::FaceFaceIter    ff_it;
  typename MeshType::FaceIter        f_it;
  typename MeshType::Scalar          c;
  std::vector<typename MeshType::Point> point_vector;

  for (f_it = mesh_.faces_begin(); f_it != mesh_.faces_end(); ++f_it) {

Jan Möbius's avatar
Jan Möbius committed
477
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
478
479
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
480
    for (ff_it = mesh_.ff_iter(*f_it); ff_it; ++ff_it) {
Jan Möbius's avatar
Jan Möbius committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
      cog += ff_it->position();
      ++valence;
    }
    cog /= valence;

    c = _coeff(valence);

    cog = cog * (1.0 - c) + f_it->position() * c;

    point_vector.push_back(cog);

  }
  for (f_it = mesh_.faces_end(); f_it != mesh_.faces_begin(); ) {

    --f_it;
    f_it->set_position(point_vector.back());
    point_vector.pop_back();

  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::FFc(scalar_t _c)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

Jan Möbius's avatar
Jan Möbius committed
508
  typename MeshType::Point           cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
509
510
511
512
513
514
  typename MeshType::FaceFaceIter    ff_it;
  typename MeshType::FaceIter        f_it;
  std::vector<typename MeshType::Point> point_vector;

  for (f_it = mesh_.faces_begin(); f_it != mesh_.faces_end(); ++f_it) {

Jan Möbius's avatar
Jan Möbius committed
515
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
516
517
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
518
    for (ff_it = mesh_.ff_iter(*f_it); ff_it; ++ff_it) {
Jan Möbius's avatar
Jan Möbius committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
      cog += ff_it->position();
      ++valence;
    }
    cog /= valence;

    cog = cog * (1.0 - _c) + f_it->position() * _c;

    point_vector.push_back(cog);

  }
  for (f_it = mesh_.faces_end(); f_it != mesh_.faces_begin(); ) {

    --f_it;
    f_it->set_position(point_vector.back());
    point_vector.pop_back();
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::FV()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::Point          cog,
                                zero_point(0.0, 0.0, 0.0);
  typename MeshType::VertexFaceIter vf_it;
  typename MeshType::VertexIter     v_it;

  for (v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it) {

Jan Möbius's avatar
Jan Möbius committed
550
    unsigned int  valence = 0;
Jan Möbius's avatar
Jan Möbius committed
551
552
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
553
    for (vf_it = mesh_.vf_iter(*v_it); vf_it; ++vf_it) {
Jan Möbius's avatar
Jan Möbius committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
      cog += vf_it->position();
      ++valence;
    }
    cog /= valence;
    v_it->set_position(cog);
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::FVc(Coeff& _coeff)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

Jan Möbius's avatar
Jan Möbius committed
568

Jan Möbius's avatar
Jan Möbius committed
569
570
571
572
573
574
575
576
  typename MeshType::Point                cog,
                                      zero_point(0.0, 0.0, 0.0);
  scalar_t               c;
  typename MeshType::VertexOHalfedgeIter  voh_it;
  typename MeshType::VertexIter           v_it;

  for (v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it) {

Jan Möbius's avatar
Jan Möbius committed
577
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
578
579
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
580
    for (voh_it = mesh_.voh_iter(*v_it); voh_it.is_valid(); ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
581
582
583
584
585
      ++valence;
    }

    c = _coeff(valence);

Jan Möbius's avatar
Jan Möbius committed
586
    for (voh_it = mesh_.voh_iter(*v_it); voh_it.is_valid(); ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
587

588
      if (mesh_.face_handle(*voh_it).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
589

Jan Möbius's avatar
Jan Möbius committed
590
591
592
593
594
595
        if (mesh_.face_handle(mesh_.opposite_halfedge_handle(mesh_.next_halfedge_handle(*voh_it))).is_valid()) {
          cog += mesh_.data(mesh_.face_handle(*voh_it)).position() * c;
          cog += mesh_.data(mesh_.face_handle(mesh_.opposite_halfedge_handle(mesh_.next_halfedge_handle(*voh_it)))).position() * (1.0 - c);
        } else {
          cog += mesh_.data(mesh_.face_handle(*voh_it)).position();
        }
Jan Möbius's avatar
Jan Möbius committed
596
      } else {
Jan Möbius's avatar
Jan Möbius committed
597
        --valence;
Jan Möbius's avatar
Jan Möbius committed
598
599
600
601
602
603
      }
    }

    if (valence > 0)
      cog /= valence;

Jan Möbius's avatar
Jan Möbius committed
604
    mesh_.data(*v_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
605
606
607
608
609
610
611
612
613
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::FVc(scalar_t _c)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

Jan Möbius's avatar
Jan Möbius committed
614
  typename MeshType::Point               cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
615
616
617
618
619
  typename MeshType::VertexOHalfedgeIter voh_it;
  typename MeshType::VertexIter          v_it;

  for (v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it) {

Jan Möbius's avatar
Jan Möbius committed
620
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
621
622
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
623
    for (voh_it = mesh_.voh_iter(*v_it); voh_it; ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
624
625
626
      ++valence;
    }

Jan Möbius's avatar
Jan Möbius committed
627
    for (voh_it = mesh_.voh_iter(*v_it); voh_it; ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
628

629
      if (mesh_.face_handle(*voh_it).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
630

Jan Möbius's avatar
Jan Möbius committed
631
632
633
634
635
636
        if (mesh_.face_handle(mesh_.opposite_halfedge_handle(mesh_.next_halfedge_handle(*voh_it))).is_valid()) {
          cog += mesh_.deref(mesh_.face_handle(*voh_it)).position() * _c;
          cog += mesh_.deref(mesh_.face_handle(mesh_.opposite_halfedge_handle(mesh_.next_halfedge_handle(*voh_it)))).position() * (1.0 - _c);
        } else {
          cog += mesh_.deref(mesh_.face_handle(*voh_it)).position();
        }
Jan Möbius's avatar
Jan Möbius committed
637
      } else {
Jan Möbius's avatar
Jan Möbius committed
638
        --valence;
Jan Möbius's avatar
Jan Möbius committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
      }
    }

    if (valence > 0)
      cog /= valence;

    v_it->set_position(cog);
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VdE()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::EdgeIter             e_it;
Jan Möbius's avatar
Jan Möbius committed
656
  typename MeshType::Point                cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
657
658
659
660
661
  typename MeshType::HalfedgeHandle       heh1, heh2;

  for (e_it = mesh_.edges_begin(); e_it != mesh_.edges_end(); ++e_it) {

    cog = zero_point;
Jan Möbius's avatar
Jan Möbius committed
662
    unsigned int valence = 2;
Jan Möbius's avatar
Jan Möbius committed
663

Jan Möbius's avatar
Jan Möbius committed
664
    heh1 = mesh_.halfedge_handle(*e_it, 0);
Jan Möbius's avatar
Jan Möbius committed
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
    heh2 = mesh_.opposite_halfedge_handle(heh1);
    cog += mesh_.data(mesh_.to_vertex_handle(heh1)).position();
    cog += mesh_.data(mesh_.to_vertex_handle(heh2)).position();

    if (!mesh_.is_boundary(heh1)) {
      cog += mesh_.data(mesh_.to_vertex_handle(mesh_.next_halfedge_handle(heh1))).position();
      ++valence;
    }

    if (!mesh_.is_boundary(heh2)) {
      cog += mesh_.data(mesh_.to_vertex_handle(mesh_.next_halfedge_handle(heh2))).position();
      ++valence;
    }

    cog /= valence;

Jan Möbius's avatar
Jan Möbius committed
681
    mesh_.data(*e_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
682
683
684
685
686
687
688
689
690
691
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VdEc(scalar_t _c)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::EdgeIter           e_it;
Jan Möbius's avatar
Jan Möbius committed
692
  typename MeshType::Point              cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
693
694
695
696
697
698
699
700
  typename MeshType::HalfedgeHandle     heh;

  for (e_it = mesh_.edges_begin(); e_it != mesh_.edges_end(); ++e_it) {

    cog = zero_point;

    for (int i = 0; i <= 1; ++i) {

Jan Möbius's avatar
Jan Möbius committed
701
      heh = mesh_.halfedge_handle(*e_it, i);
Jan Möbius's avatar
Jan Möbius committed
702
703
704
705
706
707
708
709
710
711
712
      if (!mesh_.is_boundary(heh))
      {
        cog += mesh_.point(mesh_.to_vertex_handle(mesh_.next_halfedge_handle(heh))) * (0.5 - _c);
        cog += mesh_.data(mesh_.to_vertex_handle(heh)).position() * _c;
      }
      else
      {
        cog += mesh_.data(mesh_.to_vertex_handle(heh)).position();
      }
    }

Jan Möbius's avatar
Jan Möbius committed
713
    mesh_.data(*e_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
714
715
716
717
718
719
720
721
722
723
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VdEg(scalar_t _gamma)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::EdgeIter             e_it;
Jan Möbius's avatar
Jan Möbius committed
724
  typename MeshType::Point                cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
725
726
727
728
729
730
731
732
733
734
  typename MeshType::HalfedgeHandle       heh;
  typename MeshType::VertexOHalfedgeIter  voh_it;
  unsigned int                        valence[2], i;

  for (e_it = mesh_.edges_begin(); e_it != mesh_.edges_end(); ++e_it) {

    cog = zero_point;

    for (i = 0; i <= 1; ++i)
    {
Jan Möbius's avatar
Jan Möbius committed
735
      heh = mesh_.halfedge_handle(*e_it, i);
Jan Möbius's avatar
Jan Möbius committed
736
737
738
739
740
741
742
743
744
745
746
747
748
749
      valence[i] = 0;

      // look for lowest valence vertex
      for (voh_it = mesh_.voh_iter(mesh_.to_vertex_handle(heh)); voh_it; ++voh_it)
      {
        ++valence[i];
      }
    }

    if (valence[0] < valence[1])
      i = 0;
    else
      i = 1;

Jan Möbius's avatar
Jan Möbius committed
750
    heh = mesh_.halfedge_handle(*e_it, i);
Jan Möbius's avatar
Jan Möbius committed
751
752
753
754
755
756
757
758
759

    if (!mesh_.is_boundary(heh)) {
      cog += mesh_.point(mesh_.to_vertex_handle(mesh_.next_halfedge_handle(heh))) * (_gamma);
      cog += mesh_.data(mesh_.to_vertex_handle(heh)).position() * (1.0 - 3.0 * _gamma);
    } else {
      cog += mesh_.data(mesh_.to_vertex_handle(heh)).position() * (1.0 - 2.0 * _gamma);
    }


Jan Möbius's avatar
Jan Möbius committed
760
    heh = mesh_.halfedge_handle(*e_it, 1-i);
Jan Möbius's avatar
Jan Möbius committed
761
762
763
764
765
766
767
768
769
770
771

    if (!mesh_.is_boundary(heh))
    {
      cog += mesh_.point(mesh_.to_vertex_handle(mesh_.next_halfedge_handle(heh))) * (_gamma);
      cog += mesh_.data(mesh_.to_vertex_handle(heh)).position() * _gamma;
    }
    else
    {
      cog += mesh_.data(mesh_.to_vertex_handle(heh)).position() * 2.0 * _gamma;
    }

Jan Möbius's avatar
Jan Möbius committed
772
    mesh_.data(*e_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
773
774
775
776
777
778
779
780
781
782
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VdEg(Coeff& _coeff)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::EdgeIter             e_it;
Jan Möbius's avatar
Jan Möbius committed
783
  typename MeshType::Point                cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
784
785
786
787
788
789
790
791
792
793
794
  typename MeshType::HalfedgeHandle       heh;
  typename MeshType::VertexOHalfedgeIter  voh_it;
  unsigned int                        valence[2], i;
  scalar_t               gamma;

  for (e_it = mesh_.edges_begin(); e_it != mesh_.edges_end(); ++e_it) {

    cog = zero_point;

    for (i = 0; i <= 1; ++i) {

Jan Möbius's avatar
Jan Möbius committed
795
      heh = mesh_.halfedge_handle(*e_it, i);
Jan Möbius's avatar
Jan Möbius committed
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
      valence[i] = 0;

      // look for lowest valence vertex
      for (voh_it = mesh_.voh_iter(mesh_.to_vertex_handle(heh)); voh_it; ++voh_it)
      {
        ++valence[i];
      }
    }

    if (valence[0] < valence[1])
      i = 0;
    else
      i = 1;

    gamma = _coeff(valence[i]);

Jan Möbius's avatar
Jan Möbius committed
812
    heh = mesh_.halfedge_handle(*e_it, i);
Jan Möbius's avatar
Jan Möbius committed
813
814
815
816
817
818
819
820
821
822
823
824

    if (!mesh_.is_boundary(heh))
    {
      cog += mesh_.point(mesh_.to_vertex_handle(mesh_.next_halfedge_handle(heh))) * (gamma);
      cog += mesh_.data(mesh_.to_vertex_handle(heh)).position() * (1.0 - 3.0 * gamma);
    }
    else
    {
      cog += mesh_.data(mesh_.to_vertex_handle(heh)).position() * (1.0 - 2.0 * gamma);
    }


Jan Möbius's avatar
Jan Möbius committed
825
    heh = mesh_.halfedge_handle(*e_it, 1-i);
Jan Möbius's avatar
Jan Möbius committed
826
827
828
829
830
831
832
833

    if (!mesh_.is_boundary(heh)) {
      cog += mesh_.point(mesh_.to_vertex_handle(mesh_.next_halfedge_handle(heh))) * (gamma);
      cog += mesh_.data(mesh_.to_vertex_handle(heh)).position() * gamma;
    } else {
      cog += mesh_.data(mesh_.to_vertex_handle(heh)).position() * 2.0 * gamma;
    }

Jan Möbius's avatar
Jan Möbius committed
834
    mesh_.data(*e_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
835
836
837
838
839
840
841
842
843
844
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::EV()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::VertexIter       v_it;
Jan Möbius's avatar
Jan Möbius committed
845
  typename MeshType::Point            cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
846
847
848
849
  typename MeshType::VertexEdgeIter   ve_it;

  for (v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it)
  {
Jan Möbius's avatar
Jan Möbius committed
850
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
851
852
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
853
    for (ve_it = mesh_.ve_iter(*v_it); ve_it; ++ve_it) {
Jan Möbius's avatar
Jan Möbius committed
854
855
856
857
858
859
      cog += mesh_.data(ve_it).position();
      ++valence;
    }

    cog /= valence;

Jan Möbius's avatar
Jan Möbius committed
860
    mesh_.data(*v_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
861
862
863
864
865
866
867
868
869
870
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::EVc(Coeff& _coeff)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::VertexIter            v_it;
Jan Möbius's avatar
Jan Möbius committed
871
  typename MeshType::Point                 cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
872
873
874
875
876
  typename MeshType::VertexOHalfedgeIter   voh_it;
  scalar_t                c;

  for (v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it)
  {
Jan Möbius's avatar
Jan Möbius committed
877
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
878
879
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
880
    for (voh_it = mesh_.voh_iter(*v_it); voh_it.is_valid(); ++voh_it)
Jan Möbius's avatar
Jan Möbius committed
881
882
883
884
    {
      ++valence;
    }

Jan Möbius's avatar
Jan Möbius committed
885
886
    // Coefficients always work on double so we cast them to the correct scalar here
    c = static_cast<scalar_t>(_coeff(valence));
Jan Möbius's avatar
Jan Möbius committed
887

Jan Möbius's avatar
Jan Möbius committed
888
    for (voh_it = mesh_.voh_iter(*v_it); voh_it.is_valid(); ++voh_it) {
889
890
      cog += mesh_.data(mesh_.edge_handle(*voh_it)).position() * c;
      cog += mesh_.data(mesh_.edge_handle(mesh_.next_halfedge_handle(*voh_it))).position() * (1.0 - c);
Jan Möbius's avatar
Jan Möbius committed
891
892
893
894
    }

    cog /= valence;

Jan Möbius's avatar
Jan Möbius committed
895
    mesh_.data(*v_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
896
897
898
899
900
901
902
903
904
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::EVc(scalar_t _c)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;
  typename MeshType::VertexIter           v_it;
Jan Möbius's avatar
Jan Möbius committed
905
  typename MeshType::Point                cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
906
907
908
  typename MeshType::VertexOHalfedgeIter  voh_it;

  for (v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it) {
Jan Möbius's avatar
Jan Möbius committed
909
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
910
911
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
912
    for (voh_it = mesh_.voh_iter(*v_it); voh_it; ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
913
914
915
      ++valence;
    }

Jan Möbius's avatar
Jan Möbius committed
916
    for (voh_it = mesh_.voh_iter(*v_it); voh_it; ++voh_it) {
917
918
      cog += mesh_.data(mesh_.edge_handle(*voh_it)).position() * _c;
      cog += mesh_.data(mesh_.edge_handle(mesh_.next_halfedge_handle(*voh_it))).position() * (1.0 - _c);
Jan Möbius's avatar
Jan Möbius committed
919
920
921
922
    }

    cog /= valence;

Jan Möbius's avatar
Jan Möbius committed
923
    mesh_.data(*v_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
924
925
926
927
928
929
930
931
932
933
934
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::EF()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::FaceIter         f_it;
  typename MeshType::FaceEdgeIter     fe_it;
Jan Möbius's avatar
Jan Möbius committed
935
  typename MeshType::Point            cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
936
937

  for (f_it = mesh_.faces_begin(); f_it != mesh_.faces_end(); ++f_it) {
Jan Möbius's avatar
Jan Möbius committed
938
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
939
940
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
941
    for (fe_it = mesh_.fe_iter(*f_it); fe_it; ++fe_it) {
Jan Möbius's avatar
Jan Möbius committed
942
943
944
945
946
      ++valence;
      cog += mesh_.data(fe_it).position();
    }

    cog /= valence;
Jan Möbius's avatar
Jan Möbius committed
947
    mesh_.data(*f_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
948
949
950
951
952
953
954
955
956
957
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::FE()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::EdgeIter       e_it;
Jan Möbius's avatar
Jan Möbius committed
958
  typename MeshType::Point          cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
959
960

  for (e_it = mesh_.edges_begin(); e_it != mesh_.edges_end(); ++e_it) {
Jan Möbius's avatar
Jan Möbius committed
961
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
962
963
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
964
965
    if (mesh_.face_handle(mesh_.halfedge_handle(*e_it, 0)).is_valid()) {
      cog += mesh_.data(mesh_.face_handle(mesh_.halfedge_handle(*e_it, 0))).position();
Jan Möbius's avatar
Jan Möbius committed
966
967
968
      ++valence;
    }

Jan Möbius's avatar
Jan Möbius committed
969
970
    if (mesh_.face_handle(mesh_.halfedge_handle(*e_it, 1)).is_valid()) {
      cog += mesh_.data(mesh_.face_handle(mesh_.halfedge_handle(*e_it, 1))).position();
Jan Möbius's avatar
Jan Möbius committed
971
972
973
974
      ++valence;
    }

    cog /= valence;
Jan Möbius's avatar
Jan Möbius committed
975
    mesh_.data(*e_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
976
977
978
979
980
981
982
983
984
985
986
987
988
989
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VE()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::EdgeIter      e_it;
  typename MeshType::Point         cog;

  for (e_it = mesh_.edges_begin(); e_it != mesh_.edges_end(); ++e_it)
  {
Jan Möbius's avatar
Jan Möbius committed
990
991
    cog = mesh_.data(mesh_.to_vertex_handle(mesh_.halfedge_handle(*e_it, 0))).position();
    cog += mesh_.data(mesh_.to_vertex_handle(mesh_.halfedge_handle(*e_it, 1))).position();
Jan Möbius's avatar
Jan Möbius committed
992
    cog /= 2.0;
Jan Möbius's avatar
Jan Möbius committed
993
    mesh_.data(*e_it).set_position(cog);
Jan Möbius's avatar
Jan Möbius committed
994
995
996
997
998
999
1000
1001
1002
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VV()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

Jan Möbius's avatar
Jan Möbius committed
1003
  typename MeshType::Point                cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
1004
1005
1006
1007
1008
1009
1010
1011
  typename MeshType::VertexVertexIter     vv_it;
  typename MeshType::VertexIter           v_it;
  std::vector<typename MeshType::Point>   point_vector;

  point_vector.clear();

  for (v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it) {

Jan Möbius's avatar
Jan Möbius committed
1012
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
1013
1014
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
1015
    for (vv_it = mesh_.vv_iter(*v_it); vv_it; ++vv_it) {
Jan Möbius's avatar
Jan Möbius committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
      cog += vv_it->position();
      ++valence;
    }
    cog /= valence;
    point_vector.push_back(cog);
  }

  for (v_it = mesh_.vertices_end(); v_it != mesh_.vertices_begin(); )
  {
    --v_it;
Jan Möbius's avatar
Jan Möbius committed
1026
    mesh_.data(*v_it).set_position(point_vector.back());
Jan Möbius's avatar
Jan Möbius committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
    point_vector.pop_back();
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VVc(Coeff& _coeff)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  typename MeshType::Point              cog,
                                    zero_point(0.0, 0.0, 0.0);
  typename MeshType::VertexVertexIter   vv_it;
  typename MeshType::VertexIter         v_it;
  scalar_t             c;
  std::vector<typename MeshType::Point> point_vector;

  for (v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it)
  {
Jan Möbius's avatar
Jan Möbius committed
1046
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
1047
1048
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
1049
    for (vv_it = mesh_.vv_iter(*v_it); vv_it; ++vv_it)
Jan Möbius's avatar
Jan Möbius committed
1050
1051
1052
1053
1054
1055
    {
      cog += vv_it->position();
      ++valence;
    }
    cog /= valence;
    c = _coeff(valence);
Jan Möbius's avatar
Jan Möbius committed
1056
    cog = cog * (1 - c) + mesh_.data(*v_it).position() * c;
Jan Möbius's avatar
Jan Möbius committed
1057
1058
1059
1060
1061
    point_vector.push_back(cog);
  }
  for (v_it = mesh_.vertices_end(); v_it != mesh_.vertices_begin(); )
  {
    --v_it;
Jan Möbius's avatar
Jan Möbius committed
1062
    mesh_.data(*v_it).set_position(point_vector.back());
Jan Möbius's avatar
Jan Möbius committed
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
    point_vector.pop_back();
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::VVc(scalar_t _c)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

Jan Möbius's avatar
Jan Möbius committed
1073
  typename MeshType::Point              cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
1074
1075
1076
1077
1078
1079
  typename MeshType::VertexVertexIter   vv_it;
  typename MeshType::VertexIter         v_it;
  std::vector<typename MeshType::Point> point_vector;

  for (v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it) {

Jan Möbius's avatar
Jan Möbius committed
1080
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
1081
1082
    cog = zero_point;

Jan Möbius's avatar
Jan Möbius committed
1083
    for (vv_it = mesh_.vv_iter(*v_it); vv_it; ++vv_it) {
Jan Möbius's avatar
Jan Möbius committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
      cog += mesh_.data(vv_it).position();
      ++valence;
    }
    cog /= valence;

    cog = cog * (1.0 - _c) + v_it->position() * _c;

    point_vector.push_back(cog);

  }
  for (v_it = mesh_.vertices_end(); v_it != mesh_.vertices_begin(); ) {

    --v_it;
Jan Möbius's avatar
Jan Möbius committed
1097
    mesh_.data(*v_it).set_position(point_vector.back());
Jan Möbius's avatar
Jan Möbius committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
    point_vector.pop_back();

  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::EdE()
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

Jan Möbius's avatar
Jan Möbius committed
1109
  typename MeshType::Point              cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
1110
1111
1112
1113
1114
1115
1116
1117
  typename MeshType::EdgeIter           e_it;
  typename MeshType::HalfedgeHandle     heh;
  std::vector<typename MeshType::Point> point_vector;

  point_vector.clear();

  for (e_it = mesh_.edges_begin(); e_it != mesh_.edges_end(); ++e_it) {

Jan Möbius's avatar
Jan Möbius committed
1118
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
1119
1120
1121
    cog = zero_point;

    for (int i = 0; i <= 1; ++i) {
Jan Möbius's avatar
Jan Möbius committed
1122
      heh = mesh_.halfedge_handle(*e_it, i);
Jan Möbius's avatar
Jan Möbius committed
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
      if (mesh_.face_handle(heh).is_valid())
      {
        cog += mesh_.data(mesh_.edge_handle(mesh_.next_halfedge_handle(heh))).position();
        cog += mesh_.data(mesh_.edge_handle(mesh_.next_halfedge_handle(mesh_.next_halfedge_handle(heh)))).position();
        ++valence;
        ++valence;
      }
    }

    cog /= valence;
    point_vector.push_back(cog);
  }

  for (e_it = mesh_.edges_end(); e_it != mesh_.edges_begin(); )
  {
    --e_it;
Jan Möbius's avatar
Jan Möbius committed
1139
    mesh_.data(*e_it).set_position(point_vector.back());
Jan Möbius's avatar
Jan Möbius committed
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
    point_vector.pop_back();
  }
}


template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::EdEc(scalar_t _c)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

Jan Möbius's avatar
Jan Möbius committed
1150
  typename MeshType::Point                 cog, zero_point(0.0, 0.0, 0.0);
Jan Möbius's avatar
Jan Möbius committed
1151
1152
1153
1154
1155
1156
1157
1158
  typename MeshType::EdgeIter              e_it;
  typename MeshType::HalfedgeHandle        heh;
  std::vector<typename MeshType::Point>    point_vector;

  point_vector.clear();

  for (e_it = mesh_.edges_begin(); e_it != mesh_.edges_end(); ++e_it)
  {
Jan Möbius's avatar
Jan Möbius committed
1159
    unsigned int valence = 0;
Jan Möbius's avatar
Jan Möbius committed
1160
1161
1162
    cog = zero_point;

    for (int i = 0; i <= 1; ++i) {
Jan Möbius's avatar
Jan Möbius committed
1163
      heh = mesh_.halfedge_handle(*e_it, i);
Jan Möbius's avatar
Jan Möbius committed
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
      if (mesh_.face_handle(heh).is_valid())
      {
        cog += mesh_.data(mesh_.edge_handle(mesh_.next_halfedge_handle(heh))).position() * (1.0 - _c);
        cog += mesh_.data(mesh_.edge_handle(mesh_.next_halfedge_handle(mesh_.next_halfedge_handle(heh)))).position() * (1.0 - _c);
        ++valence;
        ++valence;
      }
    }

    cog /= valence;
    cog += mesh_.data(e_it).position() * _c;
    point_vector.push_back(cog);
  }

  for (e_it = mesh_.edges_end(); e_it != mesh_.edges_begin(); ) {

    --e_it;
Jan Möbius's avatar
Jan Möbius committed
1181
    mesh_.data(*e_it).set_position(point_vector.back());
Jan Möbius's avatar
Jan Möbius committed
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
    point_vector.pop_back();
  }
}


/// Corner Cutting
template<typename MeshType, typename RealType>
void CompositeT<MeshType,RealType>::corner_cutting(HalfedgeHandle _heh)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  // Define Halfedge Handles
  typename MeshType::HalfedgeHandle heh5(_heh);
  typename MeshType::HalfedgeHandle heh6(mesh_.next_halfedge_handle(_heh));

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

  typename MeshType::HalfedgeHandle heh2(mesh_.next_halfedge_handle(heh5));
  typename MeshType::HalfedgeHandle
    heh3(mesh_.new_edge(mesh_.to_vertex_handle(_heh),
      mesh_.to_vertex_handle(heh5)));
  typename MeshType::HalfedgeHandle heh4(mesh_.opposite_halfedge_handle(heh3));

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

  // Init new face
  mesh_.data(fh_new).set_position(mesh_.data(fh_old).position());

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

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

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

  mesh_.set_face_handle(_heh, fh_new);
  mesh_.set_face_handle(heh2, fh_new);
  mesh_.set_face_handle(heh3, fh_new);

  mesh_.set_halfedge_handle(fh_new, _heh);
}


/// Split Edge
template<typename MeshType, typename RealType>
typename MeshType::VertexHandle
CompositeT<MeshType,RealType>::split_edge(HalfedgeHandle _heh)
{
  assert(p_mesh_); MeshType& mesh_ = *p_mesh_;

  HalfedgeHandle heh1;
  HalfedgeHandle heh2;
  HalfedgeHandle heh3;
  HalfedgeHandle temp_heh;

  VertexHandle
    vh,
    vh1(mesh_.to_vertex_handle(_heh)),
    vh2(mesh_.from_vertex_handle(_heh));

  // Calculate and Insert Midpoint of Edge
  vh = mesh_.add_vertex((mesh_.point(vh2) + mesh_.point(vh1)) / 2.0);
  // Re-Set Handles
  heh2 = mesh_.opposite_halfedge_handle(_heh);

  if (!mesh_.is_boundary(mesh_.edge_handle(_heh))) {

    for (temp_heh = mesh_.next_halfedge_handle(heh2);
   mesh_.next_halfedge_handle(temp_heh) != heh2;
   temp_heh = mesh_.next_halfedge_handle(temp_heh) ) {}
  } else {
    for (temp_heh = _heh;
   mesh_.next_halfedge_handle(temp_heh) != heh2;
   temp_heh = mesh_.opposite_halfedge_handle(mesh_.next_halfedge_handle(temp_heh))) {}
  }

  heh1 = mesh_.new_edge(vh, vh1);
  heh3 = mesh_.opposite_halfedge_handle(heh1);
  mesh_.set_vertex_handle(_heh, vh);
  mesh_.set_next_halfedge_handle(temp_heh, heh3);
  mesh_.set_next_halfedge_handle(heh1, mesh_.next_halfedge_handle(_heh));
  mesh_.set_next_halfedge_handle(_heh, heh1);
  mesh_.set_next_halfedge_handle(heh3, heh2);
  if (mesh_.face_handle(heh2).is_valid()) {
    mesh_.set_face_handle(heh3, mesh_.face_handle(heh2));
    mesh_.set_halfedge_handle(mesh_.face_handle(heh3), heh3);
  }
  mesh_.set_face_handle(heh1, mesh_.face_handle(_heh));
  mesh_.set_halfedge_handle(vh, heh1);
  mesh_.set_halfedge_handle(mesh_.face_handle(_heh), _heh);
  mesh_.set_halfedge_handle(vh1, heh3);

  return vh;
}


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