Sqrt3InterpolatingSubdividerLabsikGreinerT.hh 24.4 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
Jan Möbius's avatar
Jan Möbius committed
2 3 4
 *                                                                           *
 *                               OpenMesh                                    *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
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

/** \file Sqrt3InterpolatingSubdividerLabsikGreinerT.hh
Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47 48
 *
 * Interpolating Labsik Greiner Subdivider as described in 
 * "Interpolating sqrt(3) subdivision" Labsik & Greiner, 2000
 *
 * Clement Courbet - clement.courbet@ecp.fr
 *
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
*/

//=============================================================================
//
//  CLASS InterpolatingSqrt3LGT
//
//=============================================================================

#ifndef OPENMESH_SUBDIVIDER_UNIFORM_INTERP_SQRT3T_LABSIK_GREINER_HH
#define OPENMESH_SUBDIVIDER_UNIFORM_INTERP_SQRT3T_LABSIK_GREINER_HH


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

#include <OpenMesh/Core/Mesh/Handles.hh>
#include <OpenMesh/Core/System/config.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/SubdividerT.hh>

#if defined(_DEBUG) || defined(DEBUG)
// Makes life lot easier, when playing/messing around with low-level topology
// changing methods of OpenMesh
#  include <OpenMesh/Tools/Utils/MeshCheckerT.hh>
#  define ASSERT_CONSISTENCY( T, m ) \
     assert(OpenMesh::Utils::MeshCheckerT<T>(m).check())
#else
#  define ASSERT_CONSISTENCY( T, m )
#endif
// -------------------- STL
#include <vector>
#if defined(OM_CC_MIPS)
#  include <math.h>
#else
#  include <cmath>
#endif

//#define MIRROR_TRIANGLES
//#define MIN_NORM

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

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


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


/** %Uniform Interpolating Sqrt3 subdivision algorithm
 *
Jan Möbius's avatar
Jan Möbius committed
99 100 101 102 103 104
 * Implementation of the interpolating Labsik Greiner Subdivider as described in 
 * "interpolating sqrt(3) subdivision" Labsik & Greiner, 2000
 *
 * Clement Courbet - clement.courbet@ecp.fr
*/

105
template <typename MeshType, typename RealType = double>
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
class InterpolatingSqrt3LGT : public SubdividerT< MeshType, RealType >
{
public:

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

  typedef std::vector< std::vector<real_t> >      weights_t;

public:


  InterpolatingSqrt3LGT(void) : parent_t()
  { init_weights(); }

  InterpolatingSqrt3LGT(MeshType &_m) : parent_t(_m)
  { init_weights(); }

  virtual ~InterpolatingSqrt3LGT() {}


public:


Jan Möbius's avatar
Jan Möbius committed
131
  const char *name() const override { return "Uniform Interpolating Sqrt3"; }
132 133 134 135 136 137 138

  /// Pre-compute weights
  void init_weights(size_t _max_valence=50)
  {
    weights_.resize(_max_valence);

    weights_[3].resize(4);
Jan Möbius's avatar
Jan Möbius committed
139 140 141 142
    weights_[3][0] = real_t(+4.0/27);
    weights_[3][1] = real_t(-5.0/27);
    weights_[3][2] = real_t(+4.0/27);
    weights_[3][3] = real_t(+8.0/9);
143 144

    weights_[4].resize(5);
Jan Möbius's avatar
Jan Möbius committed
145 146 147 148 149
    weights_[4][0] = real_t(+2.0/9);
    weights_[4][1] = real_t(-1.0/9);
    weights_[4][2] = real_t(-1.0/9);
    weights_[4][3] = real_t(+2.0/9);
    weights_[4][4] = real_t(+7.0/9);
150 151 152 153

    for(unsigned int K=5; K<_max_valence; ++K)
    {
        weights_[K].resize(K+1);
Jan Möbius's avatar
Jan Möbius committed
154 155
        double aH = 2.0*cos(M_PI/static_cast<double>(K))/3.0;
        weights_[K][K] = static_cast<real_t>(1.0 - aH*aH);
156 157
        for(unsigned int i=0; i<K; ++i)
        {
Jan Möbius's avatar
Jan Möbius committed
158 159 160

            weights_[K][i] = static_cast<real_t>((aH*aH + 2.0*aH*cos(2.0*static_cast<double>(i)*M_PI/static_cast<double>(K) + M_PI/static_cast<double>(K)) +
                                      2.0*aH*aH*cos(4.0*static_cast<double>(i)*M_PI/static_cast<double>(K) + 2.0*M_PI/static_cast<double>(K)))/static_cast<double>(K));
161 162 163 164 165 166 167 168 169 170 171 172
        }
    }

    //just to be sure:
    weights_[6].resize(0);

  }


protected:


Jan Möbius's avatar
Jan Möbius committed
173
  bool prepare( MeshType& _m ) override
174 175 176 177 178 179 180 181 182 183 184 185
  {
    _m.request_edge_status();
    _m.add_property( fp_pos_ );
    _m.add_property( ep_nv_ );
    _m.add_property( mp_gen_ );
    _m.property( mp_gen_ ) = 0;

    return _m.has_edge_status() 
      &&   ep_nv_.is_valid() && mp_gen_.is_valid();
  }


Jan Möbius's avatar
Jan Möbius committed
186
  bool cleanup( MeshType& _m ) override
187 188 189 190 191 192 193 194 195
  {
    _m.release_edge_status();
    _m.remove_property( fp_pos_ );
    _m.remove_property( ep_nv_ );
    _m.remove_property( mp_gen_ );
    return true;
  }


Jan Möbius's avatar
Jan Möbius committed
196
  bool subdivide( MeshType& _m, size_t _n , const bool _update_points = true) override
197
  {
198 199 200

    ///TODO:Implement fixed positions

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    typename MeshType::VertexIter       vit;
    typename MeshType::VertexVertexIter vvit;
    typename MeshType::EdgeIter         eit;
    typename MeshType::FaceIter         fit;
    typename MeshType::FaceVertexIter   fvit;
    typename MeshType::FaceHalfedgeIter fheit;
    typename MeshType::VertexHandle     vh;
    typename MeshType::HalfedgeHandle   heh;
    typename MeshType::Point            pos(0,0,0), zero(0,0,0);
    size_t                              &gen = _m.property( mp_gen_ );

    for (size_t l=0; l<_n; ++l)
    {
      // tag existing edges
      for (eit=_m.edges_begin(); eit != _m.edges_end();++eit)
      {
Jan Möbius's avatar
Jan Möbius committed
217 218 219
        _m.status( *eit ).set_tagged( true );
        if ( (gen%2) && _m.is_boundary(*eit) )
          compute_new_boundary_points( _m, *eit ); // *) creates new vertices
220 221 222 223 224 225
      }

      // insert new vertices, and store pos in vp_pos_
      typename MeshType::FaceIter fend = _m.faces_end();
      for (fit = _m.faces_begin();fit != fend; ++fit)
      {
Jan Möbius's avatar
Jan Möbius committed
226
        if (_m.is_boundary(*fit))
227 228
        {
            if(gen%2)
229
                _m.property(fp_pos_, *fit).invalidate();
230 231 232
            else
            {
                //find the interior boundary halfedge
233
                for( heh = _m.halfedge_handle(*fit); !_m.is_boundary( _m.opposite_halfedge_handle(heh) ); heh = _m.next_halfedge_handle(heh) )
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
                  ;
                assert(_m.is_boundary( _m.opposite_halfedge_handle(heh) ));
                pos = zero;
                //check for two boundaries case:
                if( _m.is_boundary(_m.next_halfedge_handle(heh)) || _m.is_boundary(_m.prev_halfedge_handle(heh)) )
                {
                    if(_m.is_boundary(_m.prev_halfedge_handle(heh)))
                        heh = _m.prev_halfedge_handle(heh); //ensure that the boundary halfedges are heh and heh->next
                    //check for three boundaries case:
                    if(_m.is_boundary(_m.next_halfedge_handle(_m.next_halfedge_handle(heh))))
                    {
                        //three boundaries, use COG of triangle
                        pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(heh));
                        pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
                        pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(_m.prev_halfedge_handle(heh)));
                    }
                    else
                    {
#ifdef MIRROR_TRIANGLES
                        //two boundaries, mirror two triangles
                        pos += real_t(2.0/9) * _m.point(_m.to_vertex_handle(heh));
                        pos += real_t(4.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
                        pos += real_t(4.0/9) * _m.point(_m.to_vertex_handle(_m.prev_halfedge_handle(heh)));
                        pos += real_t(-1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
#else
                        pos += real_t(7.0/24) * _m.point(_m.to_vertex_handle(heh));
                        pos += real_t(3.0/8) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
                        pos += real_t(3.0/8) * _m.point(_m.to_vertex_handle(_m.prev_halfedge_handle(heh)));
                        pos += real_t(-1.0/24) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
#endif
                    }
                }
                else
                {
                    vh = _m.to_vertex_handle(_m.next_halfedge_handle(heh));
                    //check last vertex regularity
                    if((_m.valence(vh) == 6) || _m.is_boundary(vh))
                    {
#ifdef MIRROR_TRIANGLES
                        //use regular rule and mirror one triangle
                        pos += real_t(5.0/9) * _m.point(vh);
                        pos += real_t(3.0/9) * _m.point(_m.to_vertex_handle(heh));
                        pos += real_t(3.0/9) * _m.point(_m.to_vertex_handle(_m.opposite_halfedge_handle(heh)));
                        pos += real_t(-1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.next_halfedge_handle(heh)))));
                        pos += real_t(-1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
#else
#ifdef MIN_NORM
                        pos += real_t(1.0/9) * _m.point(vh);
                        pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(heh));
                        pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(_m.opposite_halfedge_handle(heh)));
                        pos += real_t(1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.next_halfedge_handle(heh)))));
                        pos += real_t(1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
#else
                        pos += real_t(1.0/2) * _m.point(vh);
                        pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(heh));
                        pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(_m.opposite_halfedge_handle(heh)));
                        pos += real_t(-1.0/12) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.next_halfedge_handle(heh)))));
                        pos += real_t(-1.0/12) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
#endif
#endif
                    }
                    else
                    {
                        //irregular setting, use usual irregular rule
                        unsigned int K = _m.valence(vh);
                        pos += weights_[K][K]*_m.point(vh);
                        heh = _m.opposite_halfedge_handle( _m.next_halfedge_handle(heh) );
                        for(unsigned int i = 0; i<K; ++i, heh = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)) )
                        {
                            pos += weights_[K][i]*_m.point(_m.to_vertex_handle(heh));
                        }
                    }
                }
                vh   = _m.add_vertex( pos );
308
                _m.property(fp_pos_, *fit) = vh;
309 310 311 312 313 314 315 316
            }
        }
        else
        {
            pos = zero;
            int nOrdinary = 0;
            
            //check number of extraordinary vertices
Jan Möbius's avatar
Jan Möbius committed
317
            for(fvit = _m.fv_iter( *fit ); fvit.is_valid(); ++fvit)
318
                if( (_m.valence(*fvit)) == 6 || _m.is_boundary(*fvit) )
319 320 321 322
                    ++nOrdinary;

            if(nOrdinary==3)
            {
Jan Möbius's avatar
Jan Möbius committed
323
                for(fheit = _m.fh_iter( *fit ); fheit.is_valid(); ++fheit)
324 325
                {
                    //one ring vertex has weight 32/81
326
                    heh = *fheit;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
                    assert(_m.to_vertex_handle(heh).is_valid());
                    pos += real_t(32.0/81) * _m.point(_m.to_vertex_handle(heh));
                    //tip vertex has weight -1/81
                    heh = _m.opposite_halfedge_handle(heh);
                    assert(heh.is_valid());
                    assert(_m.next_halfedge_handle(heh).is_valid());
                    assert(_m.to_vertex_handle(_m.next_halfedge_handle(heh)).is_valid());
                    pos -= real_t(1.0/81) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
                    //outer vertices have weight -2/81
                    heh = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh));
                    assert(heh.is_valid());
                    assert(_m.next_halfedge_handle(heh).is_valid());
                    assert(_m.to_vertex_handle(_m.next_halfedge_handle(heh)).is_valid());
                    pos -= real_t(2.0/81) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
                    heh = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh));
                    assert(heh.is_valid());
                    assert(_m.next_halfedge_handle(heh).is_valid());
                    assert(_m.to_vertex_handle(_m.next_halfedge_handle(heh)).is_valid());
                    pos -= real_t(2.0/81) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
                }
            }
            else
            {
                //only use irregular vertices:
Jan Möbius's avatar
Jan Möbius committed
351
                for(fheit = _m.fh_iter( *fit ); fheit.is_valid(); ++fheit)
352
                {
Jan Möbius's avatar
Jan Möbius committed
353
                    vh = _m.to_vertex_handle(*fheit);
354 355 356 357
                    if( (_m.valence(vh) != 6) && (!_m.is_boundary(vh)) )
                    {
                        unsigned int K = _m.valence(vh);
                        pos += weights_[K][K]*_m.point(vh);
358
                        heh = _m.opposite_halfedge_handle( *fheit );
359 360 361 362 363 364 365 366 367 368
                        for(unsigned int i = 0; i<K; ++i, heh = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)) )
                        {
                            pos += weights_[K][i]*_m.point(_m.to_vertex_handle(heh));
                        }
                    }
                }
                pos *= real_t(1.0/(3-nOrdinary));
            }

            vh   = _m.add_vertex( pos );
369
            _m.property(fp_pos_, *fit) = vh;
370 371 372 373 374 375
        }
      }

      //split faces
      for (fit = _m.faces_begin();fit != fend; ++fit)
      {
Jan Möbius's avatar
Jan Möbius committed
376
        if ( _m.is_boundary(*fit) && (gen%2))
377
        {
Jan Möbius's avatar
Jan Möbius committed
378
                boundary_split( _m, *fit );
379 380 381
        }
        else
        {
382
            assert(_m.property(fp_pos_, *fit).is_valid());
Jan Möbius's avatar
Jan Möbius committed
383
            _m.split( *fit, _m.property(fp_pos_, *fit) );
384 385 386 387 388
        }
      }

      // flip old edges
      for (eit=_m.edges_begin(); eit != _m.edges_end(); ++eit)
Jan Möbius's avatar
Jan Möbius committed
389 390
        if ( _m.status( *eit ).tagged() && !_m.is_boundary( *eit ) )
          _m.flip(*eit);
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

      // Now we have an consistent mesh!
      ASSERT_CONSISTENCY( MeshType, _m );

      // increase generation by one
      ++gen;
    }
    return true;
  }

private:

  // Pre-compute location of new boundary points for odd generations
  // and store them in the edge property ep_nv_;
  void compute_new_boundary_points( MeshType& _m, 
                                    const typename MeshType::EdgeHandle& _eh)
  {
    assert( _m.is_boundary(_eh) );

    typename MeshType::HalfedgeHandle heh;
    typename MeshType::VertexHandle   vh1, vh2, vh3, vh4, vhl, vhr;
    typename MeshType::Point          zero(0,0,0), P1, P2, P3, P4;

    /*
    //       *---------*---------*
    //      / \       / \       / \
    //     /   \     /   \     /   \
    //    /     \   /     \   /     \
    //   /       \ /       \ /       \
    //  *---------*--#---#--*---------*
    //                
    //  ^         ^  ^   ^  ^         ^
    //  P1        P2 pl  pr P3        P4
    */
    // get halfedge pointing from P3 to P2 (outer boundary halfedge)

    heh = _m.halfedge_handle(_eh, 
                             _m.is_boundary(_m.halfedge_handle(_eh,1)));
    
    assert( _m.is_boundary( _m.next_halfedge_handle( heh ) ) );
    assert( _m.is_boundary( _m.prev_halfedge_handle( heh ) ) );

    vh1 = _m.to_vertex_handle( _m.next_halfedge_handle( heh ) );
    vh2 = _m.to_vertex_handle( heh );
    vh3 = _m.from_vertex_handle( heh );
    vh4 = _m.from_vertex_handle( _m.prev_halfedge_handle( heh ));
    
    P1  = _m.point(vh1);
    P2  = _m.point(vh2);
    P3  = _m.point(vh3);
    P4  = _m.point(vh4);

    vhl = _m.add_vertex(real_t(-5.0/81)*P1 + real_t(20.0/27)*P2 + real_t(10.0/27)*P3 + real_t(-4.0/81)*P4);
    vhr = _m.add_vertex(real_t(-5.0/81)*P4 + real_t(20.0/27)*P3 + real_t(10.0/27)*P2 + real_t(-4.0/81)*P1);

    _m.property(ep_nv_, _eh).first  = vhl;
    _m.property(ep_nv_, _eh).second = vhr; 
  }


  void boundary_split( MeshType& _m, const typename MeshType::FaceHandle& _fh )
  {
    assert( _m.is_boundary(_fh) );

    typename MeshType::VertexHandle     vhl, vhr;
    typename MeshType::FaceEdgeIter     fe_it;
    typename MeshType::HalfedgeHandle   heh;

    // find boundary edge
Jan Möbius's avatar
Jan Möbius committed
460
    for( fe_it=_m.fe_iter( _fh ); fe_it.is_valid() && !_m.is_boundary( *fe_it ); ++fe_it ) {};
461 462

    // use precomputed, already inserted but not linked vertices
Jan Möbius's avatar
Jan Möbius committed
463 464
    vhl = _m.property(ep_nv_, *fe_it).first;
    vhr = _m.property(ep_nv_, *fe_it).second;
465 466 467 468 469 470 471 472 473 474 475 476 477 478

    /*
    //       *---------*---------*
    //      / \       / \       / \
    //     /   \     /   \     /   \
    //    /     \   /     \   /     \
    //   /       \ /       \ /       \
    //  *---------*--#---#--*---------*
    //                
    //  ^         ^  ^   ^  ^         ^
    //  P1        P2 pl  pr P3        P4
    */
    // get halfedge pointing from P2 to P3 (inner boundary halfedge)

Jan Möbius's avatar
Jan Möbius committed
479
    heh = _m.halfedge_handle(*fe_it, _m.is_boundary(_m.halfedge_handle(*fe_it,0)));
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

    typename MeshType::HalfedgeHandle pl_P3;

    // split P2->P3 (heh) in P2->pl (heh) and pl->P3
    boundary_split( _m, heh, vhl );         // split edge
    pl_P3 = _m.next_halfedge_handle( heh ); // store next halfedge handle
    boundary_split( _m, heh );              // split face

    // split pl->P3 in pl->pr and pr->P3
    boundary_split( _m, pl_P3, vhr );
    boundary_split( _m, pl_P3 );

    assert( _m.is_boundary( vhl ) && _m.halfedge_handle(vhl).is_valid() );
    assert( _m.is_boundary( vhr ) && _m.halfedge_handle(vhr).is_valid() );
  }

  void boundary_split(MeshType& _m, 
                      const typename MeshType::HalfedgeHandle& _heh,
                      const typename MeshType::VertexHandle& _vh)
  {
    assert( _m.is_boundary( _m.edge_handle(_heh) ) );

    typename MeshType::HalfedgeHandle 
      heh(_heh),
      opp_heh( _m.opposite_halfedge_handle(_heh) ),
      new_heh, opp_new_heh;
    typename MeshType::VertexHandle   to_vh(_m.to_vertex_handle(heh));
    typename MeshType::HalfedgeHandle t_heh;
    
    /*
     *            P5
     *             *
     *            /|\
     *           /   \
     *          /     \
     *         /       \
     *        /         \
     *       /_ heh  new \
     *      *-----\*-----\*\-----*
     *             ^      ^ t_heh
     *            _vh     to_vh
     *
     *     P1     P2     P3     P4
     */
    // Re-Setting Handles
    
    // find halfedge point from P4 to P3
    for(t_heh = heh; 
        _m.next_halfedge_handle(t_heh) != opp_heh; 
        t_heh = _m.opposite_halfedge_handle(_m.next_halfedge_handle(t_heh)))
    {}
    
    assert( _m.is_boundary( t_heh ) );

    new_heh     = _m.new_edge( _vh, to_vh );
    opp_new_heh = _m.opposite_halfedge_handle(new_heh);

    // update halfedge connectivity
    _m.set_next_halfedge_handle(t_heh,   opp_new_heh);                  // P4-P3 -> P3-P2
    _m.set_next_halfedge_handle(new_heh, _m.next_halfedge_handle(heh)); // P2-P3 -> P3-P5   
    _m.set_next_halfedge_handle(heh,         new_heh);                  // P1-P2 -> P2-P3
    _m.set_next_halfedge_handle(opp_new_heh, opp_heh);                  // P3-P2 -> P2-P1

    // both opposite halfedges point to same face
    _m.set_face_handle(opp_new_heh, _m.face_handle(opp_heh));

    // let heh finally point to new inserted vertex
    _m.set_vertex_handle(heh, _vh); 

    // let heh and new_heh point to same face
    _m.set_face_handle(new_heh, _m.face_handle(heh));

    // let opp_new_heh be the new outgoing halfedge for to_vh 
    // (replaces for opp_heh)
    _m.set_halfedge_handle( to_vh, opp_new_heh );

    // let opp_heh be the outgoing halfedge for _vh
    _m.set_halfedge_handle( _vh, opp_heh );
  }

  void boundary_split( MeshType& _m, 
                       const typename MeshType::HalfedgeHandle& _heh)
  {
    assert( _m.is_boundary( _m.opposite_halfedge_handle( _heh ) ) );

    typename MeshType::HalfedgeHandle 
      heh(_heh),
      n_heh(_m.next_halfedge_handle(heh));

    typename MeshType::VertexHandle   
      to_vh(_m.to_vertex_handle(heh));

    typename MeshType::HalfedgeHandle 
      heh2(_m.new_edge(to_vh,
                       _m.to_vertex_handle(_m.next_halfedge_handle(n_heh)))),
      heh3(_m.opposite_halfedge_handle(heh2));

    typename MeshType::FaceHandle
      new_fh(_m.new_face()),
      fh(_m.face_handle(heh));
    
    // Relink (half)edges    
    _m.set_face_handle(heh,  new_fh);
    _m.set_face_handle(heh2, new_fh);
    _m.set_next_halfedge_handle(heh2, _m.next_halfedge_handle(_m.next_halfedge_handle(n_heh)));
    _m.set_next_halfedge_handle(heh,  heh2);
    _m.set_face_handle( _m.next_halfedge_handle(heh2), new_fh);

    _m.set_next_halfedge_handle(heh3,                           n_heh);
    _m.set_next_halfedge_handle(_m.next_halfedge_handle(n_heh), heh3);
    _m.set_face_handle(heh3,  fh);

    _m.set_halfedge_handle(    fh, n_heh);
    _m.set_halfedge_handle(new_fh, heh);


  }

private:

  weights_t     weights_;
  OpenMesh::FPropHandleT< typename MeshType::VertexHandle >             fp_pos_;
  OpenMesh::EPropHandleT< std::pair< typename MeshType::VertexHandle,
                                     typename MeshType::VertexHandle> > ep_nv_;
  OpenMesh::MPropHandleT< size_t >                                      mp_gen_;
};


//=============================================================================
} // END_NS_UNIFORM
} // END_NS_SUBDIVIDER
} // END_NS_OPENMESH
//=============================================================================
#endif // OPENMESH_SUBDIVIDER_UNIFORM_SQRT3T_HH
//=============================================================================