ModifiedButterFlyT.hh 19 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

Jan Möbius's avatar
Jan Möbius committed
42
/** \file ModifiedButterFlyT.hh
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

The modified butterfly scheme of Denis Zorin, Peter Schröder and Wim Sweldens, 
``Interpolating subdivision for meshes with arbitrary topology,'' in Proceedings 
of SIGGRAPH 1996, ACM SIGGRAPH, 1996, pp. 189-192.

Clement Courbet - clement.courbet@ecp.fr
*/

//=============================================================================
//
//  CLASS ModifiedButterflyT
//
//=============================================================================


#ifndef SP_MODIFIED_BUTTERFLY_H
#define SP_MODIFIED_BUTTERFLY_H

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


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

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


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

Jan Möbius's avatar
Jan Möbius committed
82 83 84 85 86 87 88 89 90

/** Modified Butterfly subdivision algorithm
 *
 * Implementation of the modified butterfly scheme of Denis Zorin, Peter Schröder and Wim Sweldens, 
 * ``Interpolating subdivision for meshes with arbitrary topology,'' in Proceedings 
 * of SIGGRAPH 1996, ACM SIGGRAPH, 1996, pp. 189-192.
 *
 * Clement Courbet - clement.courbet@ecp.fr
 */
91
template <typename MeshType, typename RealType = double>
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
class ModifiedButterflyT : 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;
  typedef std::vector<real_t>                     weight_t;

public:


  ModifiedButterflyT() : parent_t()
  { init_weights(); }


  ModifiedButterflyT( mesh_t& _m) : parent_t(_m)
  { init_weights(); }


  ~ModifiedButterflyT() {}


public:


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


  /// Pre-compute weights
124
  void init_weights(size_t _max_valence=30)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  {
    weights.resize(_max_valence);

    //special case: K==3, K==4
    weights[3].resize(4);
    weights[3][0] = real_t(5.0)/12;
    weights[3][1] = real_t(-1.0)/12;
    weights[3][2] = real_t(-1.0)/12;
    weights[3][3] = real_t(3.0)/4;

    weights[4].resize(5);
    weights[4][0] = real_t(3.0)/8;
    weights[4][1] = 0;
    weights[4][2] = real_t(-1.0)/8;
    weights[4][3] = 0;
    weights[4][4] = real_t(3.0)/4;

    for(unsigned int K = 5; K<_max_valence; ++K)
    {
        weights[K].resize(K+1);
        // s(j) = ( 1/4 + cos(2*pi*j/K) + 1/2 * cos(4*pi*j/K) )/K
Jan Möbius's avatar
Jan Möbius committed
146
        double invK  = 1.0/static_cast<double>(K);
147 148 149
        real_t sum = 0;
        for(unsigned int j=0; j<K; ++j)
        {
Jan Möbius's avatar
Jan Möbius committed
150
            weights[K][j] = static_cast<real_t>((0.25 + cos(2.0*M_PI*static_cast<double>(j)*invK) + 0.5*cos(4.0*M_PI*static_cast<double>(j)*invK))*invK);
151 152
            sum += weights[K][j];
        }
Jan Möbius's avatar
Jan Möbius committed
153
        weights[K][K] = static_cast<real_t>(1.0) - sum;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    }
  }


protected:


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


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


Jan Möbius's avatar
Jan Möbius committed
177
  bool subdivide( MeshType& _m, size_t _n , const bool _update_points = true)
178
  {
179 180 181

    ///TODO:Implement fixed positions

182 183 184 185 186 187 188 189 190 191 192
    typename mesh_t::FaceIter   fit, f_end;
    typename mesh_t::EdgeIter   eit, e_end;
    typename mesh_t::VertexIter vit;

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

      // This is an interpolating scheme, old vertices remain the same.
      typename mesh_t::VertexIter initialVerticesEnd = _m.vertices_end();
      for ( vit  = _m.vertices_begin(); vit != initialVerticesEnd; ++vit)
193
        _m.property( vp_pos_, *vit ) = _m.point(*vit);
194 195 196

      // Compute position for new vertices and store them in the edge property
      for (eit=_m.edges_begin(); eit != _m.edges_end(); ++eit)
197
        compute_midpoint( _m, *eit );
198 199 200 201 202 203 204 205


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

      // Attention! Creating new edges, hence make sure the loop ends correctly.
      e_end = _m.edges_end();
      for (eit=_m.edges_begin(); eit != e_end; ++eit)
206
        split_edge(_m, *eit );
207 208 209 210 211 212 213


      // Commit changes in topology and reconsitute consistency

      // Attention! Creating new faces, hence make sure the loop ends correctly.
      f_end   = _m.faces_end();
      for (fit = _m.faces_begin(); fit != f_end; ++fit)
214
        split_face(_m, *fit );
215 216 217 218 219


      // Commit changes in geometry
      for ( vit  = /*initialVerticesEnd;*/_m.vertices_begin();
            vit != _m.vertices_end(); ++vit)
Jan Möbius's avatar
Jan Möbius committed
220
        _m.set_point(*vit, _m.property( vp_pos_, *vit ) );
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

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

    return true;
  }

private: // topological modifiers

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

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


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

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

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

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

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

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


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

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

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

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

    _m.set_halfedge_handle(fh_new, heh1);
  }


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

    typename mesh_t::HalfedgeHandle new_heh, opp_new_heh, t_heh;
    typename mesh_t::VertexHandle   vh;
    typename mesh_t::VertexHandle   vh1(_m.to_vertex_handle(heh));
    typename mesh_t::Point          zero(0,0,0);

    // new vertex
    vh                = _m.new_vertex( zero );

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


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

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

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

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

    _m.set_face_handle( new_heh, _m.face_handle(heh) );
    _m.set_halfedge_handle( vh, new_heh);
359 360 361 362 363

    // We cant reconnect a non existing face, so we skip this here if necessary
    if ( !_m.is_boundary(heh) )
      _m.set_halfedge_handle( _m.face_handle(heh), heh );

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
    _m.set_halfedge_handle( vh1, opp_new_heh );

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

private: // geometry helper

  void compute_midpoint(mesh_t& _m, const typename mesh_t::EdgeHandle& _eh)
  {
    typename mesh_t::HalfedgeHandle heh, opp_heh;

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

    typename mesh_t::Point pos(0,0,0);

    typename mesh_t::VertexHandle a_0(_m.to_vertex_handle(heh));
    typename mesh_t::VertexHandle a_1(_m.to_vertex_handle(opp_heh));

    // boundary edge: 4-point scheme
    if (_m.is_boundary(_eh) )
    {
        pos = _m.point(a_0);
        pos += _m.point(a_1);
390
        pos *= static_cast<RealType>(9.0/16.0);
391 392 393 394 395 396 397 398 399 400 401 402
        typename mesh_t::Point tpos;
        if(_m.is_boundary(heh))
        {
            tpos = _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
            tpos += _m.point(_m.to_vertex_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh))));
        }
        else
        {
            assert(_m.is_boundary(opp_heh));
            tpos = _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(opp_heh)));
            tpos += _m.point(_m.to_vertex_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(opp_heh))));
        }
403
        tpos *= static_cast<RealType>(-1.0/16.0);
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
        pos += tpos;
    }
    else
    {
        int valence_a_0 = _m.valence(a_0);
        int valence_a_1 = _m.valence(a_1);
        assert(valence_a_0>2);
        assert(valence_a_1>2);

        if( (valence_a_0==6 && valence_a_1==6) || (_m.is_boundary(a_0) && valence_a_1==6) || (_m.is_boundary(a_1) && valence_a_0==6) || (_m.is_boundary(a_0) && _m.is_boundary(a_1)) )// use 8-point scheme
        {
            real_t alpha    = real_t(1.0/2);
            real_t beta     = real_t(1.0/8);
            real_t gamma    = real_t(-1.0/16);

            //get points
            typename mesh_t::VertexHandle b_0, b_1, c_0, c_1, c_2, c_3;
            typename mesh_t::HalfedgeHandle t_he;

            t_he = _m.next_halfedge_handle(_m.opposite_halfedge_handle(heh));
            b_0 = _m.to_vertex_handle(t_he);
            if(!_m.is_boundary(_m.opposite_halfedge_handle(t_he)))
            {
                t_he = _m.next_halfedge_handle(_m.opposite_halfedge_handle(t_he));
                c_0 = _m.to_vertex_handle(t_he);
            }

            t_he = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh));
            b_1 = _m.to_vertex_handle(t_he);
            if(!_m.is_boundary(t_he))
            {
                t_he = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(t_he));
                c_1 = _m.to_vertex_handle(t_he);
            }

            t_he = _m.next_halfedge_handle(_m.opposite_halfedge_handle(opp_heh));
            assert(b_1.idx()==_m.to_vertex_handle(t_he).idx());
            if(!_m.is_boundary(_m.opposite_halfedge_handle(t_he)))
            {
                t_he = _m.next_halfedge_handle(_m.opposite_halfedge_handle(t_he));
                c_2 = _m.to_vertex_handle(t_he);
            }

            t_he = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(opp_heh));
            assert(b_0==_m.to_vertex_handle(t_he));
            if(!_m.is_boundary(t_he))
            {
                t_he = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(t_he));
                c_3 = _m.to_vertex_handle(t_he);
            }

            //compute position.
            //a0,a1,b0,b1 must exist.
            assert(a_0.is_valid());
            assert(a_1.is_valid());
            assert(b_0.is_valid());
            assert(b_1.is_valid());
            //The other vertices may be created from symmetry is they are on the other side of the boundary.

            pos = _m.point(a_0);
            pos += _m.point(a_1);
            pos *= alpha;

            typename mesh_t::Point tpos ( _m.point(b_0) );
            tpos += _m.point(b_1);
            tpos *= beta;
            pos += tpos;

            typename mesh_t::Point pc_0, pc_1, pc_2, pc_3;
            if(c_0.is_valid())
                pc_0 = _m.point(c_0);
            else //create the point by symmetry
            {
                    pc_0 = _m.point(a_1) + _m.point(b_0) - _m.point(a_0);
            }
            if(c_1.is_valid())
                pc_1 = _m.point(c_1);
            else //create the point by symmetry
            {
                    pc_1 = _m.point(a_1) + _m.point(b_1) - _m.point(a_0);
            }
            if(c_2.is_valid())
                pc_2 = _m.point(c_2);
            else //create the point by symmetry
            {
                    pc_2 = _m.point(a_0) + _m.point(b_1) - _m.point(a_1);
            }
            if(c_3.is_valid())
                pc_3 = _m.point(c_3);
            else //create the point by symmetry
            {
                    pc_3 = _m.point(a_0) + _m.point(b_0) - _m.point(a_1);
            }
            tpos = pc_0;
            tpos += pc_1;
            tpos += pc_2;
            tpos += pc_3;
            tpos *= gamma;
            pos += tpos;
        }
        else //at least one endpoint is [irregular and not in boundary]
        {
506
          RealType normFactor = static_cast<RealType>(0.0);
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

            if(valence_a_0!=6 && !_m.is_boundary(a_0))
            {
                assert((int)weights[valence_a_0].size()==valence_a_0+1);
                typename mesh_t::HalfedgeHandle t_he = opp_heh;
                for(int i = 0; i < valence_a_0 ; t_he=_m.next_halfedge_handle(_m.opposite_halfedge_handle(t_he)), ++i)
                {
                    pos += weights[valence_a_0][i] * _m.point(_m.to_vertex_handle(t_he));
                }
                assert(t_he==opp_heh);

                //add irregular vertex:
                pos += weights[valence_a_0][valence_a_0] * _m.point(a_0);
                ++normFactor;
            }

            if(valence_a_1!=6  && !_m.is_boundary(a_1))
            {
                assert((int)weights[valence_a_1].size()==valence_a_1+1);
                typename mesh_t::HalfedgeHandle t_he = heh;
                for(int i = 0; i < valence_a_1 ; t_he=_m.next_halfedge_handle(_m.opposite_halfedge_handle(t_he)), ++i)
                {
                    pos += weights[valence_a_1][i] * _m.point(_m.to_vertex_handle(t_he));
                }
                assert(t_he==heh);
                //add irregular vertex:
                pos += weights[valence_a_1][valence_a_1] * _m.point(a_1);
                ++normFactor;
            }

            assert(normFactor>0.1); //normFactor should be 1 or 2

            //if both vertices are irregular, average positions:
            pos /= normFactor;
        }
    }
    _m.property( ep_pos_, _eh ) = pos;
  }

private: // data

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

  weights_t     weights;

};

} // END_NS_UNIFORM
} // END_NS_SUBDIVIDER
} // END_NS_OPENMESH
#endif