RulesT.cc 58.6 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

/** \file RulesT.cc
    
 */

//=============================================================================
//
//  Rules - IMPLEMENTATION
//
//=============================================================================


#define OPENMESH_SUBDIVIDER_ADAPTIVE_RULEST_CC


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

#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/IO/MeshIO.hh>
#include "RulesT.hh"
// --------------------
#if defined(OM_CC_MIPS)
#  include <math.h>
#else
#  include <cmath>
#endif

#if defined(OM_CC_MSVC)
#  pragma warning(disable:4244)
#endif

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

namespace OpenMesh   { // BEGIN_NS_OPENMESH
namespace Subdivider { // BEGIN_NS_DECIMATER
Jan Möbius's avatar
Jan Möbius committed
83
namespace Adaptive   { // BEGIN_NS_ADAPTIVE
Jan Möbius's avatar
Jan Möbius committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107


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

#define MOBJ Base::mesh_.data
#define FH face_handle
#define VH vertex_handle
#define EH edge_handle
#define HEH halfedge_handle
#define NHEH next_halfedge_handle
#define PHEH prev_halfedge_handle
#define OHEH opposite_halfedge_handle
#define TVH  to_vertex_handle
#define FVH  from_vertex_handle

// ------------------------------------------------------------------ Tvv3 ----


template<class M>
void
Tvv3<M>::raise(typename M::FaceHandle& _fh, state_t _target_state) 
{
  if (MOBJ(_fh).state() < _target_state) 
  {
108
    this->update(_fh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
109
110
111
112
113
114
115
116

    typename M::VertexVertexIter          vv_it;
    typename M::FaceVertexIter            fv_it;
    typename M::VertexHandle              vh;
    typename M::Point                     position(0.0, 0.0, 0.0);
    typename M::Point                     face_position;
    const typename M::Point               zero_point(0.0, 0.0, 0.0);
    std::vector<typename M::VertexHandle> vertex_vector;
Jan Möbius's avatar
Jan Möbius committed
117

Jan Möbius's avatar
Jan Möbius committed
118
119

    // raise all adjacent vertices to level x-1
120
    for (fv_it = Base::mesh_.fv_iter(_fh); fv_it.is_valid(); ++fv_it) {
Jan Möbius's avatar
Jan Möbius committed
121

Jan Möbius's avatar
Jan Möbius committed
122
      vertex_vector.push_back(*fv_it);
Jan Möbius's avatar
Jan Möbius committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    }

    while(!vertex_vector.empty()) {

      vh = vertex_vector.back();
      vertex_vector.pop_back();

      if (_target_state > 1)
        Base::prev_rule()->raise(vh, _target_state - 1);
    }

    face_position = MOBJ(_fh).position(_target_state - 1);
    
    typename M::EdgeHandle               eh;
    std::vector<typename M::EdgeHandle>  edge_vector;

    // interior face
    if (!Base::mesh_.is_boundary(_fh) || MOBJ(_fh).final()) { 

      // insert new vertex
      vh = Base::mesh_.new_vertex();

      Base::mesh_.split(_fh, vh);

Jan Möbius's avatar
Jan Möbius committed
147
      typename M::Scalar valence(0.0);
Jan Möbius's avatar
Jan Möbius committed
148

Jan Möbius's avatar
Jan Möbius committed
149
      // calculate display position for new vertex
Jan Möbius's avatar
Jan Möbius committed
150
      for (vv_it = Base::mesh_.vv_iter(vh); vv_it.is_valid(); ++vv_it)
Jan Möbius's avatar
Jan Möbius committed
151
      {
152
        position += Base::mesh_.point(*vv_it);
Jan Möbius's avatar
Jan Möbius committed
153
        valence += 1.0;
Jan Möbius's avatar
Jan Möbius committed
154
155
156
157
158
159
160
161
162
163
164
165
      }

      position /= valence;

      // set attributes for new vertex
      Base::mesh_.set_point(vh, position);
      MOBJ(vh).set_position(_target_state, zero_point);
      MOBJ(vh).set_state(_target_state);
      MOBJ(vh).set_not_final();

      typename M::VertexOHalfedgeIter      voh_it;
      // check for edge flipping
Jan Möbius's avatar
Jan Möbius committed
166
      for (voh_it = Base::mesh_.voh_iter(vh); voh_it.is_valid(); ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
167

Jan Möbius's avatar
Jan Möbius committed
168
        if (Base::mesh_.FH(*voh_it).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
169

Jan Möbius's avatar
Jan Möbius committed
170
171
172
          MOBJ(Base::mesh_.FH(*voh_it)).set_state(_target_state);
          MOBJ(Base::mesh_.FH(*voh_it)).set_not_final();
          MOBJ(Base::mesh_.FH(*voh_it)).set_position(_target_state - 1, face_position);
Jan Möbius's avatar
Jan Möbius committed
173
174


Jan Möbius's avatar
Jan Möbius committed
175
176
177
          for (state_t j = 0; j < _target_state; ++j) {
            MOBJ(Base::mesh_.FH(*voh_it)).set_position(j, MOBJ(_fh).position(j));
          }
Jan Möbius's avatar
Jan Möbius committed
178

Jan Möbius's avatar
Jan Möbius committed
179
180
181
182
183
184
185
186
187
188
189
          if (Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it))).is_valid()) {

            if (MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it)))).state() == _target_state) {

              if (Base::mesh_.is_flip_ok(Base::mesh_.EH(Base::mesh_.NHEH(*voh_it)))) {

                edge_vector.push_back(Base::mesh_.EH(Base::mesh_.NHEH(*voh_it)));
              }
            }
          }
        }
Jan Möbius's avatar
Jan Möbius committed
190
191
192
193
194
195
196
197
198
199
200
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
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
319
320
321
322
323
324
325
      }
    }
    
    // boundary face
    else { 

      typename M::VertexHandle vh1 = Base::mesh_.new_vertex(),
	                          vh2 = Base::mesh_.new_vertex();
      
      typename M::HalfedgeHandle hh2 = Base::mesh_.HEH(_fh),
                                    hh1, hh3;
      
      while (!Base::mesh_.is_boundary(Base::mesh_.OHEH(hh2)))
	hh2 = Base::mesh_.NHEH(hh2);
      
      eh = Base::mesh_.EH(hh2);
      
      hh2 = Base::mesh_.NHEH(hh2);
      hh1 = Base::mesh_.NHEH(hh2);
      
      assert(Base::mesh_.is_boundary(eh));

      Base::mesh_.split(eh, vh1);

      eh = Base::mesh_.EH(Base::mesh_.PHEH(hh2));

      assert(Base::mesh_.is_boundary(eh));

      Base::mesh_.split(eh, vh2);

      hh3 = Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.PHEH(hh1)));

      typename M::VertexHandle   vh0(Base::mesh_.TVH(hh1)),
	                            vh3(Base::mesh_.FVH(hh2));

      // set display position and attributes for new vertices
      Base::mesh_.set_point(vh1, (Base::mesh_.point(vh0) * 2.0 + Base::mesh_.point(vh3)) / 3.0);

      MOBJ(vh1).set_position(_target_state, zero_point);
      MOBJ(vh1).set_state(_target_state);
      MOBJ(vh1).set_not_final();

      MOBJ(vh0).set_position(_target_state, MOBJ(vh0).position(_target_state - 1) * 3.0);
      MOBJ(vh0).set_state(_target_state);
      MOBJ(vh0).set_not_final();

      // set display position and attributes for old vertices
      Base::mesh_.set_point(vh2, (Base::mesh_.point(vh3) * 2.0 + Base::mesh_.point(vh0)) / 3.0);
      MOBJ(vh2).set_position(_target_state, zero_point);
      MOBJ(vh2).set_state(_target_state);
      MOBJ(vh2).set_not_final();

      MOBJ(vh3).set_position(_target_state, MOBJ(vh3).position(_target_state - 1) * 3.0);
      MOBJ(vh3).set_state(_target_state);
      MOBJ(vh3).set_not_final();

      // init 3 faces
      MOBJ(Base::mesh_.FH(hh1)).set_state(_target_state);
      MOBJ(Base::mesh_.FH(hh1)).set_not_final();
      MOBJ(Base::mesh_.FH(hh1)).set_position(_target_state - 1, face_position);
      
      MOBJ(Base::mesh_.FH(hh2)).set_state(_target_state);
      MOBJ(Base::mesh_.FH(hh2)).set_not_final();
      MOBJ(Base::mesh_.FH(hh2)).set_position(_target_state - 1, face_position);

      MOBJ(Base::mesh_.FH(hh3)).set_state(_target_state);
      MOBJ(Base::mesh_.FH(hh3)).set_final();
      MOBJ(Base::mesh_.FH(hh3)).set_position(_target_state - 1, face_position);
      

      for (state_t j = 0; j < _target_state; ++j) {
	MOBJ(Base::mesh_.FH(hh1)).set_position(j, MOBJ(_fh).position(j));
      }
      
      for (state_t j = 0; j < _target_state; ++j) {

	MOBJ(Base::mesh_.FH(hh2)).set_position(j, MOBJ(_fh).position(j));
      }
      
      for (state_t j = 0; j < _target_state; ++j) {

	MOBJ(Base::mesh_.FH(hh3)).set_position(j, MOBJ(_fh).position(j));
      }
      
      // check for edge flipping
      if (Base::mesh_.FH(Base::mesh_.OHEH(hh1)).is_valid()) {

	if (MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(hh1))).state() == _target_state) {

	  if (Base::mesh_.is_flip_ok(Base::mesh_.EH(hh1))) {

	    edge_vector.push_back(Base::mesh_.EH(hh1));
	  }
	}
      }

      if (Base::mesh_.FH(Base::mesh_.OHEH(hh2)).is_valid()) {

	if (MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(hh2))).state() == _target_state) {

	  if (Base::mesh_.is_flip_ok(Base::mesh_.EH(hh2))) {

	    edge_vector.push_back(Base::mesh_.EH(hh2));
	  }
	}
      }
    }
  
    // flip edges
    while (!edge_vector.empty()) {

      eh = edge_vector.back();
      edge_vector.pop_back();
      
      assert(Base::mesh_.is_flip_ok(eh));

      Base::mesh_.flip(eh);

      MOBJ(Base::mesh_.FH(Base::mesh_.HEH(eh, 0))).set_final();
      MOBJ(Base::mesh_.FH(Base::mesh_.HEH(eh, 1))).set_final();

      MOBJ(Base::mesh_.FH(Base::mesh_.HEH(eh, 0))).set_state(_target_state);
      MOBJ(Base::mesh_.FH(Base::mesh_.HEH(eh, 1))).set_state(_target_state);

      MOBJ(Base::mesh_.FH(Base::mesh_.HEH(eh, 0))).set_position(_target_state, face_position);
      MOBJ(Base::mesh_.FH(Base::mesh_.HEH(eh, 1))).set_position(_target_state, face_position);
    }
  }
}


template<class M>
void Tvv3<M>::raise(typename M::VertexHandle& _vh, state_t _target_state) {

  if (MOBJ(_vh).state() < _target_state) {

326
    this->update(_vh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

    // multiply old position by 3
    MOBJ(_vh).set_position(_target_state, MOBJ(_vh).position(_target_state - 1) * 3.0);

    MOBJ(_vh).inc_state();

    assert(MOBJ(_vh).state() == _target_state);
  }
}


// ------------------------------------------------------------------ Tvv4 ----


template<class M>
void
Tvv4<M>::raise(typename M::FaceHandle& _fh, state_t _target_state) 
{

  if (MOBJ(_fh).state() < _target_state) {

348
    this->update(_fh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
349
350
351
352
353
354
355
356
357

    typename M::FaceVertexIter              fv_it;
    typename M::VertexHandle                temp_vh;
    typename M::Point                       face_position;
    const typename M::Point                 zero_point(0.0, 0.0, 0.0);
    std::vector<typename M::VertexHandle>   vertex_vector;
    std::vector<typename M::HalfedgeHandle> halfedge_vector;

    // raise all adjacent vertices to level x-1
358
    for (fv_it = Base::mesh_.fv_iter(_fh); fv_it.is_valid(); ++fv_it) {
Jan Möbius's avatar
Jan Möbius committed
359

Jan Möbius's avatar
Jan Möbius committed
360
      vertex_vector.push_back(*fv_it);
Jan Möbius's avatar
Jan Möbius committed
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
406
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
439
440
441
442
443
444
445
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
471
472
473
474
475
476
477
478
479
480
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
508
509
510
511
512
513
514
515
516
517
518
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
    }

    while(!vertex_vector.empty()) {

      temp_vh = vertex_vector.back();
      vertex_vector.pop_back();

      if (_target_state > 1) {
        Base::prev_rule()->raise(temp_vh, _target_state - 1);
      }
    }

    face_position = MOBJ(_fh).position(_target_state - 1);

    typename M::HalfedgeHandle hh[3];
    typename M::VertexHandle   vh[3];
    typename M::VertexHandle   new_vh[3];
    typename M::FaceHandle     fh[4];
    typename M::EdgeHandle     eh;
    typename M::HalfedgeHandle temp_hh;

    // normal (final) face
    if (MOBJ(_fh).final()) {

      // define three halfedge handles around the face
      hh[0] = Base::mesh_.HEH(_fh);
      hh[1] = Base::mesh_.NHEH(hh[0]);
      hh[2] = Base::mesh_.NHEH(hh[1]);

      assert(hh[0] == Base::mesh_.NHEH(hh[2]));

      vh[0] = Base::mesh_.TVH(hh[0]);
      vh[1] = Base::mesh_.TVH(hh[1]);
      vh[2] = Base::mesh_.TVH(hh[2]);
      
      new_vh[0] = Base::mesh_.add_vertex(zero_point);
      new_vh[1] = Base::mesh_.add_vertex(zero_point);
      new_vh[2] = Base::mesh_.add_vertex(zero_point);

      // split three edges
      split_edge(hh[0], new_vh[0], _target_state);
      eh = Base::mesh_.EH(Base::mesh_.PHEH(hh[2]));
      split_edge(hh[1], new_vh[1], _target_state);
      split_edge(hh[2], new_vh[2], _target_state);

      assert(Base::mesh_.FVH(hh[2]) == vh[1]);
      assert(Base::mesh_.FVH(hh[1]) == vh[0]);
      assert(Base::mesh_.FVH(hh[0]) == vh[2]);

      if (Base::mesh_.FH(Base::mesh_.OHEH(hh[0])).is_valid()) 
      {
        temp_hh = Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(hh[0])));
        if (MOBJ(Base::mesh_.FH(temp_hh)).red_halfedge() != temp_hh)
          halfedge_vector.push_back(temp_hh);
      }

      if (Base::mesh_.FH(Base::mesh_.OHEH(hh[1])).is_valid()) {

	temp_hh = Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(hh[1])));
	if (MOBJ(Base::mesh_.FH(temp_hh)).red_halfedge() != temp_hh)
	  halfedge_vector.push_back(temp_hh);
      }

      if (Base::mesh_.FH(Base::mesh_.OHEH(hh[2])).is_valid()) {

	temp_hh = Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(hh[2])));
	if (MOBJ(Base::mesh_.FH(temp_hh)).red_halfedge() != temp_hh)
	  halfedge_vector.push_back(temp_hh);
      }
    }
    
    // splitted face, check for type
    else {

      // define red halfedge handle
      typename M::HalfedgeHandle red_hh(MOBJ(_fh).red_halfedge());

      if (Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.PHEH(red_hh))).is_valid() 
          && Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh)))).is_valid() 
          && MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.PHEH(red_hh)))).red_halfedge() == red_hh 
          && MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh))))).red_halfedge() == red_hh) 
      {

        // three times divided face
        vh[0] = Base::mesh_.TVH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh)))));
        vh[1] = Base::mesh_.TVH(red_hh);
        vh[2] = Base::mesh_.TVH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.PHEH(red_hh))));
          
        new_vh[0] = Base::mesh_.FVH(red_hh);
        new_vh[1] = Base::mesh_.TVH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh)));
        new_vh[2] = Base::mesh_.TVH(Base::mesh_.NHEH(red_hh));
    
        hh[0] = Base::mesh_.PHEH(Base::mesh_.OHEH(Base::mesh_.PHEH(red_hh)));
        hh[1] = Base::mesh_.PHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh))));
        hh[2] = Base::mesh_.NHEH(red_hh);
    
        eh = Base::mesh_.EH(red_hh);
      }
      
      else 
      {

	if ((Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.PHEH(red_hh))).is_valid() && 
             MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.PHEH(red_hh)))).red_halfedge() 
             == red_hh )
	    || (Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh)))).is_valid() 
	    && MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh))))).red_halfedge() == red_hh))
        {

	  // double divided face
	  if (MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.PHEH(red_hh)))).red_halfedge() == red_hh) 
	  {
        // first case
        vh[0] = Base::mesh_.TVH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh)));
        vh[1] = Base::mesh_.TVH(red_hh);
        vh[2] = Base::mesh_.TVH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.PHEH(red_hh))));
      
        new_vh[0] = Base::mesh_.FVH(red_hh);
        new_vh[1] = Base::mesh_.add_vertex(zero_point);
        new_vh[2] = Base::mesh_.TVH(Base::mesh_.NHEH(red_hh));
  
        hh[0] = Base::mesh_.PHEH(Base::mesh_.OHEH(Base::mesh_.PHEH(red_hh)));
        hh[1] = Base::mesh_.PHEH(Base::mesh_.OHEH(red_hh));
        hh[2] = Base::mesh_.NHEH(red_hh);
  
        // split one edge
        eh = Base::mesh_.EH(red_hh);
  
        split_edge(hh[1], new_vh[1], _target_state);
  
        assert(Base::mesh_.FVH(hh[2]) == vh[1]);
        assert(Base::mesh_.FVH(hh[1]) == vh[0]);
        assert(Base::mesh_.FVH(hh[0]) == vh[2]);
        
        if (Base::mesh_.FH(Base::mesh_.OHEH(hh[1])).is_valid()) 
        {
          temp_hh = Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(hh[1])));
          if (MOBJ(Base::mesh_.FH(temp_hh)).red_halfedge() != temp_hh)
            halfedge_vector.push_back(temp_hh);
        }
	  }
	  else 
	  {

	    // second case
	    vh[0] = Base::mesh_.TVH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh)))));
	    vh[1] = Base::mesh_.TVH(red_hh);
	    vh[2] = Base::mesh_.TVH(Base::mesh_.NHEH(red_hh));
      
	    new_vh[0] = Base::mesh_.FVH(red_hh);
	    new_vh[1] = Base::mesh_.TVH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh)));
	    new_vh[2] = Base::mesh_.add_vertex(zero_point);

	    hh[0] = Base::mesh_.PHEH(red_hh);
	    hh[1] = Base::mesh_.PHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh))));
	    hh[2] = Base::mesh_.NHEH(red_hh);

	    // split one edge
	    eh = Base::mesh_.EH(red_hh);

	    split_edge(hh[2], new_vh[2], _target_state);

	    assert(Base::mesh_.FVH(hh[2]) == vh[1]);
	    assert(Base::mesh_.FVH(hh[1]) == vh[0]);
	    assert(Base::mesh_.FVH(hh[0]) == vh[2]);

	    if (Base::mesh_.FH(Base::mesh_.OHEH(hh[2])).is_valid()) {
	    
	      temp_hh = Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(hh[2])));
	      if (MOBJ(Base::mesh_.FH(temp_hh)).red_halfedge() != temp_hh)
		halfedge_vector.push_back(temp_hh);
	    }
	  }
	}

	else {
	  
	  // one time divided face
	  vh[0] = Base::mesh_.TVH(Base::mesh_.NHEH(Base::mesh_.OHEH(red_hh)));
	  vh[1] = Base::mesh_.TVH(red_hh);
	  vh[2] = Base::mesh_.TVH(Base::mesh_.NHEH(red_hh));
      
	  new_vh[0] = Base::mesh_.FVH(red_hh);
	  new_vh[1] = Base::mesh_.add_vertex(zero_point);
	  new_vh[2] = Base::mesh_.add_vertex(zero_point);

	  hh[0] = Base::mesh_.PHEH(red_hh);
	  hh[1] = Base::mesh_.PHEH(Base::mesh_.OHEH(red_hh));
	  hh[2] = Base::mesh_.NHEH(red_hh);

	  // split two edges
	  eh = Base::mesh_.EH(red_hh);

	  split_edge(hh[1], new_vh[1], _target_state);
	  split_edge(hh[2], new_vh[2], _target_state);

	  assert(Base::mesh_.FVH(hh[2]) == vh[1]);
	  assert(Base::mesh_.FVH(hh[1]) == vh[0]);
	  assert(Base::mesh_.FVH(hh[0]) == vh[2]);

	  if (Base::mesh_.FH(Base::mesh_.OHEH(hh[1])).is_valid()) {

	    temp_hh = Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(hh[1])));
	    if (MOBJ(Base::mesh_.FH(temp_hh)).red_halfedge() != temp_hh)
	      halfedge_vector.push_back(temp_hh);
	  }

	  if (Base::mesh_.FH(Base::mesh_.OHEH(hh[2])).is_valid()) {

	    temp_hh = Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(hh[2])));
	    if (MOBJ(Base::mesh_.FH(temp_hh)).red_halfedge() != temp_hh)
	      halfedge_vector.push_back(temp_hh);
	  }
      	}
      }
    }

    // continue here for all cases
	
    // flip edge
    if (Base::mesh_.is_flip_ok(eh)) {
      
      Base::mesh_.flip(eh);
    }
    
    // search new faces
    fh[0] = Base::mesh_.FH(hh[0]);
    fh[1] = Base::mesh_.FH(hh[1]);
    fh[2] = Base::mesh_.FH(hh[2]);
    fh[3] = Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(hh[0])));

    assert(_fh == fh[0] || _fh == fh[1] || _fh == fh[2] || _fh == fh[3]); 

    // init new faces
    for (int i = 0; i <= 3; ++i) {

      MOBJ(fh[i]).set_state(_target_state);
      MOBJ(fh[i]).set_final();
      MOBJ(fh[i]).set_position(_target_state, face_position);
      MOBJ(fh[i]).set_red_halfedge(Base::mesh_.InvalidHalfedgeHandle);
    }

    // init new vertices and edges
    for (int i = 0; i <= 2; ++i) {
	
      MOBJ(new_vh[i]).set_position(_target_state, zero_point);
      MOBJ(new_vh[i]).set_state(_target_state);
      MOBJ(new_vh[i]).set_not_final();

      Base::mesh_.set_point(new_vh[i], (Base::mesh_.point(vh[i]) + Base::mesh_.point(vh[(i + 2) % 3])) * 0.5);

      MOBJ(Base::mesh_.EH(hh[i])).set_state(_target_state);
      MOBJ(Base::mesh_.EH(hh[i])).set_position(_target_state, zero_point);
      MOBJ(Base::mesh_.EH(hh[i])).set_final();

      MOBJ(Base::mesh_.EH(Base::mesh_.NHEH(hh[i]))).set_state(_target_state);
      MOBJ(Base::mesh_.EH(Base::mesh_.NHEH(hh[i]))).set_position(_target_state, zero_point);
      MOBJ(Base::mesh_.EH(Base::mesh_.NHEH(hh[i]))).set_final();

      MOBJ(Base::mesh_.EH(Base::mesh_.PHEH(hh[i]))).set_state(_target_state);
      MOBJ(Base::mesh_.EH(Base::mesh_.PHEH(hh[i]))).set_position(_target_state, zero_point);
      MOBJ(Base::mesh_.EH(Base::mesh_.PHEH(hh[i]))).set_final();
    }

    // check, if opposite triangle needs splitting
    while (!halfedge_vector.empty()) {

      temp_hh = halfedge_vector.back();
      halfedge_vector.pop_back();

      check_edge(temp_hh, _target_state);
    }

    assert(MOBJ(fh[0]).state() == _target_state);
    assert(MOBJ(fh[1]).state() == _target_state);
    assert(MOBJ(fh[2]).state() == _target_state);
    assert(MOBJ(fh[3]).state() == _target_state);
  }
}


template<class M>
void
Tvv4<M>::raise(typename M::VertexHandle& _vh, state_t _target_state)
{
 
  if (MOBJ(_vh).state() < _target_state)
  {

650
    this->update(_vh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665

    // multiply old position by 4
    MOBJ(_vh).set_position(_target_state, MOBJ(_vh).position(_target_state - 1) * 4.0);

    MOBJ(_vh).inc_state();
  }
}


template<class M>
void
Tvv4<M>::raise(typename M::EdgeHandle& _eh, state_t _target_state) 
{ 
  if (MOBJ(_eh).state() < _target_state) 
  {
666
    this->update(_eh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685

    typename M::FaceHandle fh(Base::mesh_.FH(Base::mesh_.HEH(_eh, 0)));

    if (!fh.is_valid())
      fh=Base::mesh_.FH(Base::mesh_.HEH(_eh, 1));

    raise(fh, _target_state);

    assert(MOBJ(_eh).state() == _target_state);
  }
}

#ifndef DOXY_IGNORE_THIS

template<class M>
void
Tvv4<M>::split_edge(typename M::HalfedgeHandle &_hh, 
                       typename M::VertexHandle   &_vh, 
                       state_t _target_state) 
Jan Möbius's avatar
Jan Möbius committed
686
                       {
Jan Möbius's avatar
Jan Möbius committed
687
688
689
690
691
692
693
694
  typename M::HalfedgeHandle temp_hh;

  if (Base::mesh_.FH(Base::mesh_.OHEH(_hh)).is_valid()) 
  {
    if (!MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(_hh))).final()) 
    {    
      if (MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(_hh))).red_halfedge().is_valid()) 
      {        
Jan Möbius's avatar
Jan Möbius committed
695
        temp_hh = MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(_hh))).red_halfedge();
Jan Möbius's avatar
Jan Möbius committed
696
697
698
      }
      else 
      {
Jan Möbius's avatar
Jan Möbius committed
699
700
        // two cases for divided, but not visited face
        if (MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(_hh))))).state()
Jan Möbius's avatar
Jan Möbius committed
701
702
            == MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(_hh))).state()) 
        {
Jan Möbius's avatar
Jan Möbius committed
703
704
          temp_hh = Base::mesh_.PHEH(Base::mesh_.OHEH(_hh));
        }
Jan Möbius's avatar
Jan Möbius committed
705

Jan Möbius's avatar
Jan Möbius committed
706
707
        else if (MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(_hh))))).state()
            == MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(_hh))).state())
Jan Möbius's avatar
Jan Möbius committed
708
        {
Jan Möbius's avatar
Jan Möbius committed
709
710
          temp_hh = Base::mesh_.NHEH(Base::mesh_.OHEH(_hh));
        }
Jan Möbius's avatar
Jan Möbius committed
711
712
713
714
715
716
717
      }
    }
    else
      temp_hh = Base::mesh_.InvalidHalfedgeHandle;
  }
  else
    temp_hh = Base::mesh_.InvalidHalfedgeHandle;
Jan Möbius's avatar
Jan Möbius committed
718

Jan Möbius's avatar
Jan Möbius committed
719
720
  // split edge
  Base::mesh_.split(Base::mesh_.EH(_hh), _vh);
Jan Möbius's avatar
Jan Möbius committed
721

Jan Möbius's avatar
Jan Möbius committed
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  if (Base::mesh_.FVH(_hh) == _vh) 
  {	    
    MOBJ(Base::mesh_.EH(Base::mesh_.PHEH(Base::mesh_.OHEH(Base::mesh_.PHEH(_hh))))).set_state(MOBJ(Base::mesh_.EH(_hh)).state());
    _hh = Base::mesh_.PHEH(Base::mesh_.OHEH(Base::mesh_.PHEH(_hh)));
  }

  if (Base::mesh_.FH(Base::mesh_.OHEH(_hh)).is_valid()) {
	  
    MOBJ(Base::mesh_.EH(Base::mesh_.PHEH(Base::mesh_.OHEH(_hh)))).set_not_final();
    MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(_hh))).set_state(_target_state-1);
    MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(_hh)))))).set_state(_target_state-1);

    MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(_hh))).set_not_final();
    MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(_hh)))))).set_not_final();

    MOBJ(Base::mesh_.EH(Base::mesh_.PHEH(Base::mesh_.OHEH(_hh)))).set_state(_target_state);

    if (temp_hh.is_valid()) {
	
      MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(_hh))).set_red_halfedge(temp_hh);
      MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(_hh)))))).set_red_halfedge(temp_hh);
    } 
    else {

      typename M::FaceHandle 
        fh1(Base::mesh_.FH(Base::mesh_.OHEH(_hh))),
        fh2(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(_hh))))));

      MOBJ(fh1).set_red_halfedge(Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(_hh))));
      MOBJ(fh2).set_red_halfedge(Base::mesh_.OHEH(Base::mesh_.PHEH(Base::mesh_.OHEH(_hh))));

      const typename M::Point zero_point(0.0, 0.0, 0.0);

      MOBJ(fh1).set_position(_target_state - 1, zero_point);
      MOBJ(fh2).set_position(_target_state - 1, zero_point);
    }
  }

  // init edges
  MOBJ(Base::mesh_.EH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(_hh))))).set_state(_target_state - 1);
  MOBJ(Base::mesh_.EH(Base::mesh_.NHEH(Base::mesh_.OHEH(Base::mesh_.NHEH(_hh))))).set_final();

  MOBJ(Base::mesh_.EH(_hh)).set_state(_target_state - 1);
  MOBJ(Base::mesh_.EH(_hh)).set_final();
}  


template<class M>
void Tvv4<M>::check_edge(const typename M::HalfedgeHandle& _hh, 
                         state_t _target_state) 
{
  typename M::FaceHandle fh1(Base::mesh_.FH(_hh)),
    fh2(Base::mesh_.FH(Base::mesh_.OHEH(_hh)));

  assert(fh1.is_valid());
  assert(fh2.is_valid());

  typename M::HalfedgeHandle red_hh(MOBJ(fh1).red_halfedge());
  
  if (!MOBJ(fh1).final()) {

    assert (MOBJ(fh1).final() == MOBJ(fh2).final());
    assert (!MOBJ(fh1).final());
    assert (MOBJ(fh1).red_halfedge() == MOBJ(fh2).red_halfedge());

    const typename M::Point zero_point(0.0, 0.0, 0.0);

    MOBJ(fh1).set_position(_target_state - 1, zero_point);
    MOBJ(fh2).set_position(_target_state - 1, zero_point);

    assert(red_hh.is_valid());

    if (!red_hh.is_valid()) {

      MOBJ(fh1).set_state(_target_state - 1);
      MOBJ(fh2).set_state(_target_state - 1);

      MOBJ(fh1).set_red_halfedge(_hh);
      MOBJ(fh2).set_red_halfedge(_hh);

      MOBJ(Base::mesh_.EH(_hh)).set_not_final();
      MOBJ(Base::mesh_.EH(_hh)).set_state(_target_state - 1);
    }

    else {

      MOBJ(Base::mesh_.EH(_hh)).set_not_final();
      MOBJ(Base::mesh_.EH(_hh)).set_state(_target_state - 1);

      raise(fh1, _target_state);

      assert(MOBJ(fh1).state() == _target_state);
    }
  }
}


// -------------------------------------------------------------------- VF ----


template<class M>
void VF<M>::raise(typename M::FaceHandle& _fh, state_t _target_state) 
{
  if (MOBJ(_fh).state() < _target_state) {

827
    this->update(_fh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
828

Jan Möbius's avatar
Jan Möbius committed
829
    // raise all neighbor vertices to level x-1
Jan Möbius's avatar
Jan Möbius committed
830
831
832
833
834
835
    typename M::FaceVertexIter            fv_it;
    typename M::VertexHandle              vh;
    std::vector<typename M::VertexHandle> vertex_vector;

    if (_target_state > 1) {

836
      for (fv_it = Base::mesh_.fv_iter(_fh); fv_it.is_valid(); ++fv_it) {
Jan Möbius's avatar
Jan Möbius committed
837

Jan Möbius's avatar
Jan Möbius committed
838
        vertex_vector.push_back(*fv_it);
Jan Möbius's avatar
Jan Möbius committed
839
840
841
842
      }

      while (!vertex_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
843
844
        vh = vertex_vector.back();
        vertex_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
845

Jan Möbius's avatar
Jan Möbius committed
846
        Base::prev_rule()->raise(vh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
847
848
849
850
      }
    }

    // calculate new position
Jan Möbius's avatar
Jan Möbius committed
851
852
    typename M::Point  position(0.0, 0.0, 0.0);
    typename M::Scalar valence(0.0);
Jan Möbius's avatar
Jan Möbius committed
853

854
    for (fv_it = Base::mesh_.fv_iter(_fh); fv_it.is_valid(); ++fv_it) {
Jan Möbius's avatar
Jan Möbius committed
855

Jan Möbius's avatar
Jan Möbius committed
856
      valence  += 1.0;
Jan Möbius's avatar
Jan Möbius committed
857
      position += Base::mesh_.data(*fv_it).position(_target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
858
859
860
861
862
863
864
865
    }

    position /= valence;

    // boundary rule
    if (Base::number() == Base::subdiv_rule()->number() + 1 && 
        Base::mesh_.is_boundary(_fh)                  && 
        !MOBJ(_fh).final())
Jan Möbius's avatar
Jan Möbius committed
866
      position *= static_cast<typename M::Scalar>(0.5);
Jan Möbius's avatar
Jan Möbius committed
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883

    MOBJ(_fh).set_position(_target_state, position);
    MOBJ(_fh).inc_state();

    assert(_target_state == MOBJ(_fh).state());
  }
}


// -------------------------------------------------------------------- FF ----


template<class M>
void FF<M>::raise(typename M::FaceHandle& _fh, state_t _target_state) {

  if (MOBJ(_fh).state() < _target_state) {

884
    this->update(_fh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
885

Jan Möbius's avatar
Jan Möbius committed
886
    // raise all neighbor faces to level x-1
Jan Möbius's avatar
Jan Möbius committed
887
888
889
890
891
892
    typename M::FaceFaceIter              ff_it;
    typename M::FaceHandle                fh;
    std::vector<typename M::FaceHandle>   face_vector;

    if (_target_state > 1) {

Jan Möbius's avatar
Jan Möbius committed
893
      for (ff_it = Base::mesh_.ff_iter(_fh); ff_it.is_valid(); ++ff_it) {
Jan Möbius's avatar
Jan Möbius committed
894

Jan Möbius's avatar
Jan Möbius committed
895
        face_vector.push_back(*ff_it);
Jan Möbius's avatar
Jan Möbius committed
896
897
898
899
      }

      while (!face_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
900
901
        fh = face_vector.back();
        face_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
902

Jan Möbius's avatar
Jan Möbius committed
903
        Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
904
905
      }

Jan Möbius's avatar
Jan Möbius committed
906
      for (ff_it = Base::mesh_.ff_iter(_fh); ff_it.is_valid(); ++ff_it) {
Jan Möbius's avatar
Jan Möbius committed
907

Jan Möbius's avatar
Jan Möbius committed
908
        face_vector.push_back(*ff_it);
Jan Möbius's avatar
Jan Möbius committed
909
910
911
912
      }

      while (!face_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
913
914
        fh = face_vector.back();
        face_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
915

Jan Möbius's avatar
Jan Möbius committed
916
917
        while (MOBJ(fh).state() < _target_state - 1)
          Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
918
919
920
921
      }
    }

    // calculate new position
Jan Möbius's avatar
Jan Möbius committed
922
923
    typename M::Point  position(0.0, 0.0, 0.0);
    typename M::Scalar valence(0.0);
Jan Möbius's avatar
Jan Möbius committed
924

Jan Möbius's avatar
Jan Möbius committed
925
    for (ff_it = Base::mesh_.ff_iter(_fh); ff_it.is_valid(); ++ff_it) {
Jan Möbius's avatar
Jan Möbius committed
926

Jan Möbius's avatar
Jan Möbius committed
927
      valence  += 1.0;
Jan Möbius's avatar
Jan Möbius committed
928

Jan Möbius's avatar
Jan Möbius committed
929
      position += Base::mesh_.data(*ff_it).position(_target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
    }

    position /= valence;

    MOBJ(_fh).set_position(_target_state, position);
    MOBJ(_fh).inc_state();
  }
}


// ------------------------------------------------------------------- FFc ----


template<class M>
void FFc<M>::raise(typename M::FaceHandle& _fh, state_t _target_state) 
{
  if (MOBJ(_fh).state() < _target_state) {

948
    this->update(_fh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
949

Jan Möbius's avatar
Jan Möbius committed
950
    // raise all neighbor faces to level x-1
Jan Möbius's avatar
Jan Möbius committed
951
952
953
954
955
956
    typename M::FaceFaceIter              ff_it(Base::mesh_.ff_iter(_fh));
    typename M::FaceHandle                fh;
    std::vector<typename M::FaceHandle>   face_vector;

    if (_target_state > 1) 
    {
Jan Möbius's avatar
Jan Möbius committed
957
      for (; ff_it.is_valid(); ++ff_it)
Jan Möbius's avatar
Jan Möbius committed
958
        face_vector.push_back(*ff_it);
Jan Möbius's avatar
Jan Möbius committed
959
960
961

      while (!face_vector.empty()) 
      {
Jan Möbius's avatar
Jan Möbius committed
962
963
964
        fh = face_vector.back();
        face_vector.pop_back();
        Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
965
966
      }

Jan Möbius's avatar
Jan Möbius committed
967
      for (ff_it = Base::mesh_.ff_iter(_fh); ff_it.is_valid(); ++ff_it)
Jan Möbius's avatar
Jan Möbius committed
968
        face_vector.push_back(*ff_it);
Jan Möbius's avatar
Jan Möbius committed
969
970
971

      while (!face_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
972
973
        fh = face_vector.back();
        face_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
974

Jan Möbius's avatar
Jan Möbius committed
975
976
        while (MOBJ(fh).state() < _target_state - 1)
          Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
977
978
979
980
      }
    }

    // calculate new position
Jan Möbius's avatar
Jan Möbius committed
981
982
    typename M::Point  position(0.0, 0.0, 0.0);
    typename M::Scalar valence(0.0);
Jan Möbius's avatar
Jan Möbius committed
983

Jan Möbius's avatar
Jan Möbius committed
984
    for (ff_it = Base::mesh_.ff_iter(_fh); ff_it.is_valid(); ++ff_it)
Jan Möbius's avatar
Jan Möbius committed
985
    {
Jan Möbius's avatar
Jan Möbius committed
986
      valence += 1.0;
Jan Möbius's avatar
Jan Möbius committed
987
      position += Base::mesh_.data(*ff_it).position(_target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
988
989
990
991
992
993
994
    }

    position /= valence;

    // choose coefficient c
    typename M::Scalar c = Base::coeff();

Jan Möbius's avatar
Jan Möbius committed
995
    position *= (static_cast<typename M::Scalar>(1.0) - c);
Jan Möbius's avatar
Jan Möbius committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
    position += MOBJ(_fh).position(_target_state - 1) * c;

    MOBJ(_fh).set_position(_target_state, position);
    MOBJ(_fh).inc_state();
  }
}


// -------------------------------------------------------------------- FV ----


template<class M>
void FV<M>::raise(typename M::VertexHandle& _vh, state_t _target_state) 
{

  if (MOBJ(_vh).state() < _target_state) {

1013
    this->update(_vh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
1014

Jan Möbius's avatar
Jan Möbius committed
1015
    // raise all neighbor vertices to level x-1
Jan Möbius's avatar
Jan Möbius committed
1016
1017
1018
1019
1020
1021
    typename M::VertexFaceIter            vf_it(Base::mesh_.vf_iter(_vh));
    typename M::FaceHandle                fh;
    std::vector<typename M::FaceHandle>   face_vector;

    if (_target_state > 1) {

Jan Möbius's avatar
Jan Möbius committed
1022
      for (; vf_it.is_valid(); ++vf_it) {
Jan Möbius's avatar
Jan Möbius committed
1023

Jan Möbius's avatar
Jan Möbius committed
1024
        face_vector.push_back(*vf_it);
Jan Möbius's avatar
Jan Möbius committed
1025
1026
1027
1028
      }

      while (!face_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
1029
1030
        fh = face_vector.back();
        face_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
1031

Jan Möbius's avatar
Jan Möbius committed
1032
        Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1033
1034
      }

Jan Möbius's avatar
Jan Möbius committed
1035
      for (vf_it = Base::mesh_.vf_iter(_vh); vf_it.is_valid(); ++vf_it) {
Jan Möbius's avatar
Jan Möbius committed
1036

Jan Möbius's avatar
Jan Möbius committed
1037
        face_vector.push_back(*vf_it);
Jan Möbius's avatar
Jan Möbius committed
1038
1039
1040
1041
      }

      while (!face_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
1042
1043
        fh = face_vector.back();
        face_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
1044

Jan Möbius's avatar
Jan Möbius committed
1045
1046
        while (MOBJ(fh).state() < _target_state - 1)
          Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1047
1048
1049
1050
      }
    }

    // calculate new position
Jan Möbius's avatar
Jan Möbius committed
1051
1052
    typename M::Point  position(0.0, 0.0, 0.0);
    typename M::Scalar valence(0.0);
Jan Möbius's avatar
Jan Möbius committed
1053

Jan Möbius's avatar
Jan Möbius committed
1054
    for (vf_it = Base::mesh_.vf_iter(_vh); vf_it.is_valid(); ++vf_it) {
Jan Möbius's avatar
Jan Möbius committed
1055

Jan Möbius's avatar
Jan Möbius committed
1056
      valence  += 1.0;
Jan Möbius's avatar
Jan Möbius committed
1057
      position += Base::mesh_.data(*vf_it).position(_target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
    }

    position /= valence;

    MOBJ(_vh).set_position(_target_state, position);
    MOBJ(_vh).inc_state();

    if (Base::number() == Base::n_rules() - 1) {

      Base::mesh_.set_point(_vh, position);
      MOBJ(_vh).set_final();
    }
  }
}


// ------------------------------------------------------------------- FVc ----


template<class M>
void FVc<M>::raise(typename M::VertexHandle& _vh, state_t _target_state) 
{
  if (MOBJ(_vh).state() < _target_state) {

1082
    this->update(_vh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093

    typename M::VertexOHalfedgeIter       voh_it;
    typename M::FaceHandle                fh;
    std::vector<typename M::FaceHandle>   face_vector;
    int                                      valence(0);

    face_vector.clear();

    // raise all neighbour faces to level x-1
    if (_target_state > 1) {

Jan Möbius's avatar
Jan Möbius committed
1094
      for (voh_it = Base::mesh_.voh_iter(_vh); voh_it.is_valid(); ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
1095

Jan Möbius's avatar
Jan Möbius committed
1096
        if (Base::mesh_.FH(*voh_it).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
1097

Jan Möbius's avatar
Jan Möbius committed
1098
          face_vector.push_back(Base::mesh_.FH(*voh_it));
Jan Möbius's avatar
Jan Möbius committed
1099

Jan Möbius's avatar
Jan Möbius committed
1100
          if (Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it))).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
1101

Jan Möbius's avatar
Jan Möbius committed
1102
1103
1104
            face_vector.push_back(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it))));
          }
        }
Jan Möbius's avatar
Jan Möbius committed
1105
1106
1107
1108
      }

      while (!face_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
1109
1110
        fh = face_vector.back();
        face_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
1111

Jan Möbius's avatar
Jan Möbius committed
1112
        Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1113
1114
      }

Jan Möbius's avatar
Jan Möbius committed
1115
      for (voh_it = Base::mesh_.voh_iter(_vh); voh_it.is_valid(); ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
1116

Jan Möbius's avatar
Jan Möbius committed
1117
        if (Base::mesh_.FH(*voh_it).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
1118

Jan Möbius's avatar
Jan Möbius committed
1119
          face_vector.push_back(Base::mesh_.FH(*voh_it));
Jan Möbius's avatar
Jan Möbius committed
1120

Jan Möbius's avatar
Jan Möbius committed
1121
          if (Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it))).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
1122

Jan Möbius's avatar
Jan Möbius committed
1123
1124
1125
            face_vector.push_back(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it))));
          }
        }
Jan Möbius's avatar
Jan Möbius committed
1126
1127
1128
1129
      }

      while (!face_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
1130
1131
        fh = face_vector.back();
        face_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
1132

Jan Möbius's avatar
Jan Möbius committed
1133
1134
        while (MOBJ(fh).state() < _target_state - 1)
          Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1135
1136
      }

Jan Möbius's avatar
Jan Möbius committed
1137
      for (voh_it = Base::mesh_.voh_iter(_vh); voh_it.is_valid(); ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
1138

Jan Möbius's avatar
Jan Möbius committed
1139
        if (Base::mesh_.FH(*voh_it).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
1140

Jan Möbius's avatar
Jan Möbius committed
1141
          face_vector.push_back(Base::mesh_.FH(*voh_it));
Jan Möbius's avatar
Jan Möbius committed
1142

Jan Möbius's avatar
Jan Möbius committed
1143
          if (Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it))).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
1144

Jan Möbius's avatar
Jan Möbius committed
1145
1146
1147
            face_vector.push_back(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it))));
          }
        }
Jan Möbius's avatar
Jan Möbius committed
1148
1149
1150
1151
      }

      while (!face_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
1152
1153
        fh = face_vector.back();
        face_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
1154

Jan Möbius's avatar
Jan Möbius committed
1155
1156
        while (MOBJ(fh).state() < _target_state - 1)
          Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
      }
    }

    // calculate new position
    typename M::Point               position(0.0, 0.0, 0.0);
    typename M::Scalar              c;
#if 0
    const typename M::Scalar        _2pi(2.0*M_PI);
    const typename M::Scalar        _2over3(2.0/3.0);

    for (voh_it = Base::mesh_.voh_iter(_vh); voh_it; ++voh_it) 
    {     
      ++valence;
    }

    // choose coefficient c
    c = _2over3 * ( cos( _2pi / valence) + 1.0);
#else
    valence = Base::mesh_.valence(_vh);
Jan Möbius's avatar
Jan Möbius committed
1176
    c       = typename M::Scalar(coeff(valence));
Jan Möbius's avatar
Jan Möbius committed
1177
1178
1179
#endif


Jan Möbius's avatar
Jan Möbius committed
1180
    for (voh_it = Base::mesh_.voh_iter(_vh); voh_it.is_valid(); ++voh_it) {
Jan Möbius's avatar
Jan Möbius committed
1181

1182
      fh = Base::mesh_.FH(*voh_it);
Jan Möbius's avatar
Jan Möbius committed
1183
      if (fh.is_valid())
Jan Möbius's avatar
Jan Möbius committed
1184
        Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1185

1186
      fh = Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it)));
Jan Möbius's avatar
Jan Möbius committed
1187
      if (fh.is_valid())
Jan Möbius's avatar
Jan Möbius committed
1188
        Base::prev_rule()->raise(fh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1189

1190
      if (Base::mesh_.FH(*voh_it).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
1191

Jan Möbius's avatar
Jan Möbius committed
1192
        if (Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it))).is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
1193

Jan Möbius's avatar
Jan Möbius committed
1194
          position += MOBJ(Base::mesh_.FH(*voh_it)).position(_target_state - 1) * c;
Jan Möbius's avatar
Jan Möbius committed
1195

Jan Möbius's avatar
Jan Möbius committed
1196
          position += MOBJ(Base::mesh_.FH(Base::mesh_.OHEH(Base::mesh_.NHEH(*voh_it)))).position(_target_state - 1) * ( typename M::Scalar(1.0) - c);
Jan Möbius's avatar
Jan Möbius committed
1197
1198
        }
        else {
Jan Möbius's avatar
Jan Möbius committed
1199

Jan Möbius's avatar
Jan Möbius committed
1200
1201
          position += MOBJ(Base::mesh_.FH(*voh_it)).position(_target_state - 1);
        }
Jan Möbius's avatar
Jan Möbius committed
1202
1203
1204
1205
      }

      else {

Jan Möbius's avatar
Jan Möbius committed
1206
        --valence;
Jan Möbius's avatar
Jan Möbius committed
1207
1208
      }
    } 
Jan Möbius's avatar
Jan Möbius committed
1209

Jan Möbius's avatar
Jan Möbius committed
1210
    position /= typename M::Scalar(valence);
Jan Möbius's avatar
Jan Möbius committed
1211
1212
1213
1214
1215

    MOBJ(_vh).set_position(_target_state, position);
    MOBJ(_vh).inc_state();

    assert(MOBJ(_vh).state() == _target_state);
Jan Möbius's avatar
Jan Möbius committed
1216

Jan Möbius's avatar
Jan Möbius committed
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
    // check if last rule
    if (Base::number() == Base::n_rules() - 1) {

      Base::mesh_.set_point(_vh, position);
      MOBJ(_vh).set_final();
    }
  }
}

template<class M>
std::vector<double> FVc<M>::coeffs_;

template <class M>
void FVc<M>::init_coeffs(size_t _max_valence)
{
  if ( coeffs_.size() == _max_valence+1 )
    return;

  if ( coeffs_.size() < _max_valence+1 )
  {
    const double _2pi(2.0*M_PI);
    const double _2over3(2.0/3.0);

    if (coeffs_.empty())
      coeffs_.push_back(0.0); // dummy for valence 0
      
    for(size_t v=coeffs_.size(); v <= _max_valence; ++v)
      coeffs_.push_back(_2over3 * ( cos( _2pi / v) + 1.0));    
  }
}


// -------------------------------------------------------------------- VV ----


template<class M>
void VV<M>::raise(typename M::VertexHandle& _vh, state_t _target_state) 
{
  if (MOBJ(_vh).state() < _target_state) 
  {
1257
    this->update(_vh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
1258

Jan Möbius's avatar
Jan Möbius committed
1259
    // raise all neighbor vertices to level x-1
Jan Möbius's avatar
Jan Möbius committed
1260
1261
1262
1263
1264
1265
    typename M::VertexVertexIter              vv_it(Base::mesh_.vv_iter(_vh));
    typename M::VertexHandle                  vh;
    std::vector<typename M::VertexHandle>     vertex_vector;

    if (_target_state > 1) {

Jan Möbius's avatar
Jan Möbius committed
1266
      for (; vv_it.is_valid(); ++vv_it) {
Jan Möbius's avatar
Jan Möbius committed
1267

Jan Möbius's avatar
Jan Möbius committed
1268
        vertex_vector.push_back(*vv_it);
Jan Möbius's avatar
Jan Möbius committed
1269
1270
1271
1272
      }

      while (!vertex_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
1273
1274
        vh = vertex_vector.back();
        vertex_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
1275

Jan Möbius's avatar
Jan Möbius committed
1276
        Base::prev_rule()->raise(vh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1277
1278
      }

Jan Möbius's avatar
Jan Möbius committed
1279
      for (; vv_it.is_valid(); ++vv_it) {
Jan Möbius's avatar
Jan Möbius committed
1280

Jan Möbius's avatar
Jan Möbius committed
1281
        vertex_vector.push_back(*vv_it);
Jan Möbius's avatar
Jan Möbius committed
1282
1283
1284
1285
      }

      while (!vertex_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
1286
1287
        vh = vertex_vector.back();
        vertex_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
1288

Jan Möbius's avatar
Jan Möbius committed
1289
        Base::prev_rule()->raise(vh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1290
1291
1292
1293
      }
    }

    // calculate new position
Jan Möbius's avatar
Jan Möbius committed
1294
1295
    typename M::Point  position(0.0, 0.0, 0.0);
    typename M::Scalar valence(0.0);
Jan Möbius's avatar
Jan Möbius committed
1296

Jan Möbius's avatar
Jan Möbius committed
1297
    for (vv_it = Base::mesh_.vv_iter(_vh); vv_it.is_valid(); ++vv_it) {
Jan Möbius's avatar
Jan Möbius committed
1298

Jan Möbius's avatar
Jan Möbius committed
1299
      valence  += 1.0;
Jan Möbius's avatar
Jan Möbius committed
1300
      position += Base::mesh_.data(*vv_it).position(_target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
    }

    position /= valence;

    MOBJ(_vh).set_position(_target_state, position);
    MOBJ(_vh).inc_state();

    // check if last rule
    if (Base::number() == Base::n_rules() - 1) {

      Base::mesh_.set_point(_vh, position);
      MOBJ(_vh).set_final();
    }
  }
}


// ------------------------------------------------------------------- VVc ----


template<class M>
void VVc<M>::raise(typename M::VertexHandle& _vh, state_t _target_state)
{
  if (MOBJ(_vh).state() < _target_state) {

1326
    this->update(_vh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
1327

Jan Möbius's avatar
Jan Möbius committed
1328
    // raise all neighbor vertices to level x-1
Jan Möbius's avatar
Jan Möbius committed
1329
1330
1331
1332
1333
1334
    typename M::VertexVertexIter              vv_it(Base::mesh_.vv_iter(_vh));
    typename M::VertexHandle                  vh;
    std::vector<typename M::VertexHandle>     vertex_vector;

    if (_target_state > 1) {

Jan Möbius's avatar
Jan Möbius committed
1335
      for (; vv_it.is_valid(); ++vv_it) {
Jan Möbius's avatar
Jan Möbius committed
1336

Jan Möbius's avatar
Jan Möbius committed
1337
        vertex_vector.push_back(*vv_it);
Jan Möbius's avatar
Jan Möbius committed
1338
1339
1340
1341
      }

      while (!vertex_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
1342
1343
        vh = vertex_vector.back();
        vertex_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
1344

Jan Möbius's avatar
Jan Möbius committed
1345
        Base::prev_rule()->raise(vh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1346
1347
      }

Jan Möbius's avatar
Jan Möbius committed
1348
      for (; vv_it.is_valid(); ++vv_it) {
Jan Möbius's avatar
Jan Möbius committed
1349

Jan Möbius's avatar
Jan Möbius committed
1350
        vertex_vector.push_back(*vv_it);
Jan Möbius's avatar
Jan Möbius committed
1351
1352
1353
1354
      }

      while (!vertex_vector.empty()) {

Jan Möbius's avatar
Jan Möbius committed
1355
1356
        vh = vertex_vector.back();
        vertex_vector.pop_back();
Jan Möbius's avatar
Jan Möbius committed
1357

Jan Möbius's avatar
Jan Möbius committed
1358
        Base::prev_rule()->raise(vh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1359
1360
1361
1362
      }
    }

    // calculate new position
Jan Möbius's avatar
Jan Möbius committed
1363
1364
    typename M::Point  position(0.0, 0.0, 0.0);
    typename M::Scalar valence(0.0);
Jan Möbius's avatar
Jan Möbius committed
1365
1366
    typename M::Scalar c;

Jan Möbius's avatar
Jan Möbius committed
1367
    for (vv_it = Base::mesh_.vv_iter(_vh); vv_it.is_valid(); ++vv_it)
Jan Möbius's avatar
Jan Möbius committed
1368
    {
Jan Möbius's avatar
Jan Möbius committed
1369
      valence += 1.0;
Jan Möbius's avatar
Jan Möbius committed
1370
      position += Base::mesh_.data(*vv_it).position(_target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1371
1372
1373
1374
    }

    position /= valence;

Jan Möbius's avatar
Jan Möbius committed
1375
    // choose coefficient c
Jan Möbius's avatar
Jan Möbius committed
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
    c = Base::coeff();

    position *= (1.0 - c);
    position += MOBJ(_vh).position(_target_state - 1) * c;

    MOBJ(_vh).set_position(_target_state, position);
    MOBJ(_vh).inc_state();

    if (Base::number() == Base::n_rules() - 1) 
    {
      Base::mesh_.set_point(_vh, position);
      MOBJ(_vh).set_final();
    }
  }
}


// -------------------------------------------------------------------- VE ----


template<class M>
void VE<M>::raise(typename M::EdgeHandle& _eh, state_t _target_state) 
{
  if (MOBJ(_eh).state() < _target_state) {

1401
    this->update(_eh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419

    // raise all neighbour vertices to level x-1
    typename M::VertexHandle          vh;
    typename M::HalfedgeHandle        hh1(Base::mesh_.HEH(_eh, 0)),
                                         hh2(Base::mesh_.HEH(_eh, 1));

    if (_target_state > 1) {

      vh = Base::mesh_.TVH(hh1);
      
      Base::prev_rule()->raise(vh, _target_state - 1);

      vh = Base::mesh_.TVH(hh2);
      
      Base::prev_rule()->raise(vh, _target_state - 1);
    }

    // calculate new position
Jan Möbius's avatar
Jan Möbius committed
1420
1421
    typename M::Point  position(0.0, 0.0, 0.0);
    const typename M::Scalar valence(2.0);
Jan Möbius's avatar
Jan Möbius committed
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441

    position += MOBJ(Base::mesh_.TVH(hh1)).position(_target_state - 1);
    position += MOBJ(Base::mesh_.TVH(hh2)).position(_target_state - 1);

    position /= valence;

    MOBJ(_eh).set_position(_target_state, position);
    MOBJ(_eh).inc_state();
  }
}


// ------------------------------------------------------------------- VdE ----


template<class M>
void VdE<M>::raise(typename M::EdgeHandle& _eh, state_t _target_state) 
{
  if (MOBJ(_eh).state() < _target_state) 
  {
1442
    this->update(_eh, _target_state);
Jan Möbius's avatar
Jan Möbius committed
1443

Jan Möbius's avatar
Jan Möbius committed
1444
    // raise all neighbor vertices to level x-1
Jan Möbius's avatar
Jan Möbius committed
1445
1446
    typename M::VertexHandle             vh;
    typename M::HalfedgeHandle           hh1(Base::mesh_.HEH(_eh, 0)),
Jan Möbius's avatar
Jan Möbius committed
1447
        hh2(Base::mesh_.HEH(_eh, 1));
Jan Möbius's avatar
Jan Möbius committed
1448
1449
1450
1451
1452
1453
1454
1455
1456
    typename M::FaceHandle                fh1, fh2;

    if (_target_state > 1) {

      fh1 = Base::mesh_.FH(hh1);
      fh2 = Base::mesh_.FH(hh2);

      if (fh1.is_valid()) {

Jan Möbius's avatar
Jan Möbius committed
1457
        Base::prev_rule()->raise(fh1, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1458

Jan Möbius's avatar
Jan Möbius committed
1459
1460
        vh = Base::mesh_.TVH(Base::mesh_.NHEH(hh1));
        Base::prev_rule()->raise(vh, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1461
      }
Jan Möbius's avatar
Jan Möbius committed
1462

Jan Möbius's avatar
Jan Möbius committed
1463
1464
      if (fh2.is_valid()) {

Jan Möbius's avatar
Jan Möbius committed
1465
        Base::prev_rule()->raise(fh2, _target_state - 1);
Jan Möbius's avatar
Jan Möbius committed
1466