BaseRemesherT_impl.hh 35.6 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * 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 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
*                                                                            *
\*===========================================================================*/

//=============================================================================
//
//  CLASS BaseRemesherT - IMPLEMENTATION
//
//=============================================================================

#define BASE_REMESHERT_C


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


#include "BaseRemesherT.hh"

// Remshing
#include "DiffGeoT.hh"

// OpenMesh
#include <OpenMesh/Core/Utils/Property.hh>
#include <OpenMesh/Core/System/omstream.hh>

// Selection Stuff
#include <MeshTools/MeshSelectionT.hh>


//== NAMESPACES ===============================================================

namespace Remeshing {


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


template <class Mesh>
BaseRemesherT<Mesh>::
BaseRemesherT(Mesh& _mesh, ProgressEmitter* _progress)
Jan Möbius's avatar
Jan Möbius committed
78
  : mesh_(_mesh), refmesh_(0), bsp_(0), nothing_selected_(true),progress_(_progress) {
Jan Möbius's avatar
Jan Möbius committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

}


//-----------------------------------------------------------------------------


template <class Mesh>
BaseRemesherT<Mesh>::
~BaseRemesherT() {

    delete bsp_;
    delete refmesh_;
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
init_reference()
{
  typename Mesh::VIter     v_it, v_end;
  typename Mesh::FIter     f_it, f_end;
  typename Mesh::CFVIter   fv_it;
  typename Mesh::VHandle   v0, v1, v2;


  // clear old stuff first
  if (bsp_) delete_reference();

  // tag non-locked vertices + one ring
  for (v_it=mesh_.vertices_begin(), v_end=mesh_.vertices_end();
Jan Möbius's avatar
Jan Möbius committed
114 115
      v_it!=v_end; ++v_it)
    mesh_.status(*v_it).set_tagged(!mesh_.status(*v_it).locked());
Jan Möbius's avatar
Jan Möbius committed
116 117 118 119 120 121 122 123 124 125

  MeshSelection::growVertexSelection(&mesh_);

  // create reference mesh
  refmesh_ = new Mesh();

  OpenMesh::VPropHandleT<typename Mesh::VHandle>  vhandles;
  mesh_.add_property(vhandles);

  for (v_it=mesh_.vertices_begin(), v_end=mesh_.vertices_end();
Jan Möbius's avatar
Jan Möbius committed
126 127 128
      v_it!=v_end; ++v_it)
    if (mesh_.status(*v_it).tagged())
      mesh_.property(vhandles, *v_it) = refmesh_->add_vertex(mesh_.point(*v_it));
Jan Möbius's avatar
Jan Möbius committed
129 130

  for (f_it=mesh_.faces_begin(), f_end=mesh_.faces_end();
Jan Möbius's avatar
Jan Möbius committed
131
      f_it!=f_end; ++f_it)
Jan Möbius's avatar
Jan Möbius committed
132
  {
Jan Möbius's avatar
Jan Möbius committed
133 134 135 136
    fv_it = mesh_.cfv_iter(*f_it);
    v0    = *fv_it;
    v1    = *(++fv_it);
    v2    = *(++fv_it);
137

Jan Möbius's avatar
Jan Möbius committed
138
    if (mesh_.status(v0).tagged() &&
Jan Möbius's avatar
Jan Möbius committed
139 140
        mesh_.status(v1).tagged() &&
        mesh_.status(v2).tagged())
Jan Möbius's avatar
Jan Möbius committed
141 142
    {
      refmesh_->add_face( mesh_.property(vhandles, v0),
Jan Möbius's avatar
Jan Möbius committed
143 144
          mesh_.property(vhandles, v1),
          mesh_.property(vhandles, v2) );
Jan Möbius's avatar
Jan Möbius committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    }
  }

  MeshSelection::clearVertexSelection(&mesh_);
  mesh_.remove_property(vhandles);

  // need vertex normals
  refmesh_->request_face_normals();
  refmesh_->request_vertex_normals();
  refmesh_->update_normals(/*Mesh::ANGLE_WEIGHTED*/);


  // build BSP
  bsp_ = new BSP(*refmesh_);
  bsp_->reserve(refmesh_->n_faces());
160
  for (f_it=refmesh_->faces_begin(), f_end=refmesh_->faces_end();
Jan Möbius's avatar
Jan Möbius committed
161
       f_it!=f_end; ++f_it)
Jan Möbius's avatar
Jan Möbius committed
162
    bsp_->push_back(*f_it);
Jan Möbius's avatar
Jan Möbius committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  bsp_->build(10, 100);
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
delete_reference()
{
  delete bsp_;      bsp_     = 0;
  delete refmesh_;  refmesh_ = 0;
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
project_to_reference(typename Mesh::VertexHandle _vh) const
{
  typename Mesh::Point          p = mesh_.point(_vh);
  typename Mesh::FaceHandle     fh = bsp_->nearest(p).handle;
190 191 192 193

  if ( ! fh.is_valid() )
    return;

Jan Möbius's avatar
Jan Möbius committed
194
  typename Mesh::CFVIter        fv_it = refmesh_->cfv_iter(fh);
195

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

Jan Möbius's avatar
Jan Möbius committed
197 198 199 200 201 202
  const typename Mesh::Point&   p0 = refmesh_->point(*fv_it);
  typename Mesh::Normal         n0 = refmesh_->normal(*fv_it);
  const typename Mesh::Point&   p1 = refmesh_->point(*(++fv_it));
  typename Mesh::Normal         n1 = refmesh_->normal(*fv_it);
  const typename Mesh::Point&   p2 = refmesh_->point(*(++fv_it));
  typename Mesh::Normal         n2 = refmesh_->normal(*fv_it);
Jan Möbius's avatar
Jan Möbius committed
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


  // project
  ACG::Geometry::distPointTriangle(p, p0, p1, p2, p);
  mesh_.set_point(_vh, p);


  // get barycentric coordinates
  if (!ACG::Geometry::baryCoord(p, p0, p1, p2, p))
    p[0] = p[1] = p[2] = 1.0/3.0;


  // interpolate normal
  typename Mesh::Normal n;
  n  = (n0 *= p[0]);
  n += (n1 *= p[1]);
  n += (n2 *= p[2]);
  n.normalize();
  mesh_.set_normal(_vh, n);
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
231
remesh(unsigned int           _iters,
Jan Möbius's avatar
Jan Möbius committed
232
       unsigned int           _area_iters,
Matthias Möller's avatar
Matthias Möller committed
233 234
       bool                   _use_projection,
       Selection              _selection) {
Jan Möbius's avatar
Jan Möbius committed
235 236 237

    try
    {
Matthias Möller's avatar
Matthias Möller committed
238 239 240 241
        if (_selection == VERTEX_SELECTION)
          prepare_vertex_selection();
        else if (_selection == FACE_SELECTION)
          prepare_face_selection();
Jan Möbius's avatar
Jan Möbius committed
242 243
        remeshh(_iters, _area_iters, _use_projection);
    }
Matthias Möller's avatar
Matthias Möller committed
244
    catch (std::bad_alloc&)
Jan Möbius's avatar
Jan Möbius committed
245 246 247 248 249 250 251 252 253 254 255 256 257
    {
    mesh_.clear();
    omerr() << "Remeshig: Out of memory\n";
    }

    cleanup();
}

//-----------------------------------------------------------------------------

template <class Mesh>
void
BaseRemesherT<Mesh>::
Matthias Möller's avatar
Matthias Möller committed
258
prepare_vertex_selection()
Jan Möbius's avatar
Jan Möbius committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
{
  typename Mesh::EIter     e_it, e_end;
  typename Mesh::VIter     v_it, v_end;
  typename Mesh::FIter     f_it, f_end;
  typename Mesh::CFVIter   fv_it;
  typename Mesh::CVOHIter  vh_it;
  typename Mesh::VHandle   v0, v1;
  typename Mesh::FHandle   f0, f1;


  // need vertex and edge status
  mesh_.request_vertex_status();
  mesh_.request_edge_status();
  mesh_.request_face_status();



  // if nothing selected -> select all
  nothing_selected_ = true;
  for (v_it=mesh_.vertices_begin(), v_end=mesh_.vertices_end();
       v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
280
    if (mesh_.status(*v_it).selected())
Jan Möbius's avatar
Jan Möbius committed
281 282 283 284
    { nothing_selected_ = false; break; }

  if (nothing_selected_)
    for (v_it=mesh_.vertices_begin(), v_end=mesh_.vertices_end();
Matthias Möller's avatar
Matthias Möller committed
285
   v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
286
      mesh_.status(*v_it).set_selected(true);
Jan Möbius's avatar
Jan Möbius committed
287 288 289 290 291 292



  // lock un-selected vertices & edges
  for (v_it=mesh_.vertices_begin(), v_end=mesh_.vertices_end();
       v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
293
    mesh_.status(*v_it).set_locked(!mesh_.status(*v_it).selected());
Jan Möbius's avatar
Jan Möbius committed
294

295
  for (e_it=mesh_.edges_begin(), e_end=mesh_.edges_end();
Jan Möbius's avatar
Jan Möbius committed
296 297
       e_it!=e_end; ++e_it)
  {
Jan Möbius's avatar
Jan Möbius committed
298 299 300
    v0 = mesh_.to_vertex_handle(mesh_.halfedge_handle(*e_it, 0));
    v1 = mesh_.to_vertex_handle(mesh_.halfedge_handle(*e_it, 1));
    mesh_.status(*e_it).set_locked(mesh_.status(v0).locked() ||
Matthias Möller's avatar
Matthias Möller committed
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 359 360 361 362 363 364 365 366
          mesh_.status(v1).locked());
  }



  // handle feature corners:
  // lock corner vertices (>2 feature edges) and endpoints
  // of feature lines (1 feature edge)
  for (v_it=mesh_.vertices_begin(), v_end=mesh_.vertices_end();
      v_it!=v_end; ++v_it)
  {
    if (mesh_.status(*v_it).feature())
    {
      int c=0;
      for (vh_it=mesh_.cvoh_iter(*v_it); vh_it.is_valid(); ++vh_it)
        if (mesh_.status(mesh_.edge_handle(*vh_it)).feature())
          ++c;
      if (c!=2) mesh_.status(*v_it).set_locked(true);
    }
  }



  // build reference mesh
  init_reference();
//   if (emit_progress_)  Progress().step(5);


  // add properties
  mesh_.add_property(valences_);
  mesh_.add_property(update_);
  mesh_.add_property(area_);
}

//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
prepare_face_selection()
{
  typename Mesh::EIter     e_it, e_end;
  typename Mesh::VIter     v_it, v_end;
  typename Mesh::FIter     f_it, f_end;
  typename Mesh::CFVIter   fv_it;
  typename Mesh::CVOHIter  vh_it;
  typename Mesh::VHandle   v0, v1;
  typename Mesh::FHandle   f0, f1;


  // need vertex and edge status
  mesh_.request_vertex_status();
  mesh_.request_edge_status();
  mesh_.request_face_status();

  // if nothing selected -> select all
  nothing_selected_ = true;
  for (f_it = mesh_.faces_begin(), f_end = mesh_.faces_end(); f_it != f_end;
      ++f_it)
  {
    if (mesh_.status(*f_it).selected())
    {
      nothing_selected_ = false;
      break;
    }
Jan Möbius's avatar
Jan Möbius committed
367 368
  }

Matthias Möller's avatar
Matthias Möller committed
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
  if (nothing_selected_)
    MeshSelection::selectAllFaces(&mesh_);



  // lock un-selected vertices & edges
  for (v_it = mesh_.vertices_begin(), v_end = mesh_.vertices_end();
      v_it != v_end; ++v_it)
  {
    bool all_faces_selected = true;

    for (typename Mesh::ConstVertexFaceIter vf_it = mesh_.cvf_iter(*v_it);
        vf_it.is_valid(); ++vf_it)
    {
      if (!mesh_.status(*vf_it).selected())
      {
        all_faces_selected = false;
        break;
      }
    }
    mesh_.status(*v_it).set_locked(!all_faces_selected);
  }

  for (e_it = mesh_.edges_begin(), e_end = mesh_.edges_end();
      e_it != e_end; ++e_it)
  {
    if (mesh_.is_boundary(*e_it))
    {
      mesh_.status(*e_it).set_locked(true);
    }
    else
    {
      f0 = mesh_.face_handle(mesh_.halfedge_handle(*e_it, 0));
      f1 = mesh_.face_handle(mesh_.halfedge_handle(*e_it, 1));

      mesh_.status(*e_it).set_locked(!(mesh_.status(f0).selected() && mesh_.status(f1).selected()));
    }
  }
Jan Möbius's avatar
Jan Möbius committed
407

Matthias Möller's avatar
Matthias Möller committed
408
  MeshSelection::clearFaceSelection(&mesh_);
Jan Möbius's avatar
Jan Möbius committed
409 410 411 412 413

  // handle feature corners:
  // lock corner vertices (>2 feature edges) and endpoints
  // of feature lines (1 feature edge)
  for (v_it=mesh_.vertices_begin(), v_end=mesh_.vertices_end();
Jan Möbius's avatar
Jan Möbius committed
414
      v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
415
  {
Jan Möbius's avatar
Jan Möbius committed
416
    if (mesh_.status(*v_it).feature())
Jan Möbius's avatar
Jan Möbius committed
417 418
    {
      int c=0;
Jan Möbius's avatar
Jan Möbius committed
419 420 421 422
      for (vh_it=mesh_.cvoh_iter(*v_it); vh_it.is_valid(); ++vh_it)
        if (mesh_.status(mesh_.edge_handle(*vh_it)).feature())
          ++c;
      if (c!=2) mesh_.status(*v_it).set_locked(true);
Jan Möbius's avatar
Jan Möbius committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    }
  }



  // build reference mesh
  init_reference();
//   if (emit_progress_)  Progress().step(5);


  // add properties
  mesh_.add_property(valences_);
  mesh_.add_property(update_);
  mesh_.add_property(area_);
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
446
remeshh(unsigned int           _iters,
Jan Möbius's avatar
Jan Möbius committed
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
	unsigned int _area_iters, bool _use_projection) {

    double progress_step = 100.0 / (((double)_iters/4.0 + (double)_area_iters));
    double total_progress = 0.0;

    for (unsigned int i = 0; i < _iters; ++i) {

        split_long_edges();
        if(progress_ != NULL) {
            total_progress += progress_step;
            progress_->sendProgressSignal(total_progress);
        }

        collapse_short_edges();
        if(progress_ != NULL) {
            total_progress += progress_step;
            progress_->sendProgressSignal(total_progress);
        }

        flip_edges();
        if(progress_ != NULL) {
            total_progress += progress_step;
            progress_->sendProgressSignal(total_progress);
        }

        tangential_smoothing(_use_projection);
        if(progress_ != NULL) {
            total_progress += progress_step;
            progress_->sendProgressSignal(total_progress);
        }
    }

    if (_area_iters) {
        balanace_area(_area_iters, _use_projection);
        if(progress_ != NULL) {
            total_progress += progress_step;
            progress_->sendProgressSignal(total_progress);
        }
    }

    // feature edges block flips -> might lead to caps
    remove_caps();
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
cleanup()
{
  typename Mesh::EIter     e_it, e_end;
  typename Mesh::VIter     v_it, v_end;
  typename Mesh::FIter     f_it, f_end;
  typename Mesh::CFVIter   fv_it;
  typename Mesh::CVOHIter  vh_it;
  typename Mesh::VHandle   v0, v1;
  typename Mesh::FHandle   f0, f1;


  // remove properties
  mesh_.remove_property(valences_);
  mesh_.remove_property(update_);
  mesh_.remove_property(area_);


  // remove reference again
  delete_reference();


  // unlock all vertices & edges
  for (v_it=mesh_.vertices_begin(), v_end=mesh_.vertices_end();
       v_it!=v_end; ++v_it)
  {
Jan Möbius's avatar
Jan Möbius committed
523
    mesh_.status(*v_it).set_locked(false);
Jan Möbius's avatar
Jan Möbius committed
524
  }
525
  for (e_it=mesh_.edges_begin(), e_end=mesh_.edges_end();
Jan Möbius's avatar
Jan Möbius committed
526 527
       e_it!=e_end; ++e_it)
  {
Jan Möbius's avatar
Jan Möbius committed
528
    mesh_.status(*e_it).set_locked(false);
Jan Möbius's avatar
Jan Möbius committed
529 530 531 532 533 534 535
  }


  // de-select if nothing was selected before
  if (nothing_selected_)
    for (v_it=mesh_.vertices_begin(), v_end=mesh_.vertices_end();
	 v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
536
      mesh_.status(*v_it).set_selected(false);
Jan Möbius's avatar
Jan Möbius committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554



  // free vertex and edge status
  mesh_.release_vertex_status();
  mesh_.release_edge_status();
  mesh_.release_face_status();
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
split_long_edges()
{
555 556 557 558 559
  typename Mesh::VIter v_it, v_end;
  typename Mesh::EIter e_it, e_end;
  typename Mesh::VHandle v0, v1, vh;
  typename Mesh::EHandle eh, e0, e1;
  typename Mesh::FHandle f0, f1, f2, f3;
Jan Möbius's avatar
Jan Möbius committed
560

561 562
  bool ok, is_feature, is_boundary;
  int i;
Jan Möbius's avatar
Jan Möbius committed
563 564

  // un-tagg
565
  for (v_it = mesh_.vertices_begin(), v_end = mesh_.vertices_end(); v_it != v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
566
    mesh_.status(*v_it).set_tagged(false);
Jan Möbius's avatar
Jan Möbius committed
567 568 569 570 571 572

  // handle Nastran PIDs during refinement
  OpenMesh::FPropHandleT<unsigned int> pids;
  mesh_.get_property_handle(pids, "Nastran PIDs");

  // split long edges
573
  for (ok = false, i = 0; !ok && i < 100; ++i) {
Jan Möbius's avatar
Jan Möbius committed
574 575
    ok = true;

576
    for (e_it = mesh_.edges_begin(), e_end = mesh_.edges_end(); e_it != e_end; ++e_it) {
Jan Möbius's avatar
Jan Möbius committed
577 578
      v0 = mesh_.to_vertex_handle(mesh_.halfedge_handle(*e_it, 0));
      v1 = mesh_.to_vertex_handle(mesh_.halfedge_handle(*e_it, 1));
Jan Möbius's avatar
Jan Möbius committed
579

Jan Möbius's avatar
Jan Möbius committed
580
      if (!mesh_.status(*e_it).locked() && is_too_long(v0, v1)) {
581 582
        const typename Mesh::Point& p0 = mesh_.point(v0);
        const typename Mesh::Point& p1 = mesh_.point(v1);
Jan Möbius's avatar
Jan Möbius committed
583

Jan Möbius's avatar
Jan Möbius committed
584 585
        is_feature = mesh_.status(*e_it).feature();
        is_boundary = mesh_.is_boundary(*e_it);
Jan Möbius's avatar
Jan Möbius committed
586

587
        vh = mesh_.add_vertex((p0 + p1) * 0.5);
Jan Möbius's avatar
Jan Möbius committed
588
        mesh_.split(*e_it, vh);
Jan Möbius's avatar
Jan Möbius committed
589

590
        mesh_.status(vh).set_selected(true);
Jan Möbius's avatar
Jan Möbius committed
591

592 593
        if (is_feature) {
          eh = (is_boundary ? mesh_.edge_handle(mesh_.n_edges() - 2) : mesh_.edge_handle(mesh_.n_edges() - 3));
Jan Möbius's avatar
Jan Möbius committed
594

595 596 597 598
          mesh_.status(eh).set_feature(true);
        } else {
          project_to_reference(vh);
        }
Jan Möbius's avatar
Jan Möbius committed
599

600
        if (pids.is_valid()) {
Jan Möbius's avatar
Jan Möbius committed
601
          e0 = *e_it;
602
          e1 = (is_boundary ? mesh_.edge_handle(mesh_.n_edges() - 2) : mesh_.edge_handle(mesh_.n_edges() - 3));
Jan Möbius's avatar
Jan Möbius committed
603

604 605 606 607
          f0 = mesh_.face_handle(mesh_.halfedge_handle(e0, 0));
          f1 = mesh_.face_handle(mesh_.halfedge_handle(e0, 1));
          f2 = mesh_.face_handle(mesh_.halfedge_handle(e1, 0));
          f3 = mesh_.face_handle(mesh_.halfedge_handle(e1, 1));
Jan Möbius's avatar
Jan Möbius committed
608

609
          if (f0.is_valid() && f3.is_valid()) mesh_.property(pids, f3) = mesh_.property(pids, f0);
Jan Möbius's avatar
Jan Möbius committed
610

611 612
          if (f1.is_valid() && f2.is_valid()) mesh_.property(pids, f2) = mesh_.property(pids, f1);
        }
Jan Möbius's avatar
Jan Möbius committed
613

614
        ok = false;
Jan Möbius's avatar
Jan Möbius committed
615 616 617 618
      }
    }
  }

619
  if (i == 100) omlog() << "split break\n";
Jan Möbius's avatar
Jan Möbius committed
620 621 622 623 624 625 626 627 628 629 630
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
collapse_short_edges()
{
631 632 633 634 635 636 637 638 639 640
  typename Mesh::EIter e_it, e_end;
  typename Mesh::CVVIter vv_it;
  typename Mesh::VHandle v0, v1;
  typename Mesh::HHandle h0, h1, h01, h10;
  typename Mesh::Point p;
  bool ok, skip, b0, b1, l0, l1, f0, f1;
  int i;
  bool hcol01, hcol10;

  for (ok = false, i = 0; !ok && i < 100; ++i) {
Jan Möbius's avatar
Jan Möbius committed
641 642
    ok = true;

643
    for (e_it = mesh_.edges_begin(), e_end = mesh_.edges_end(); e_it != e_end; ++e_it) {
Jan Möbius's avatar
Jan Möbius committed
644 645 646
      if (!mesh_.status(*e_it).deleted() && !mesh_.status(*e_it).locked()) {
        h10 = mesh_.halfedge_handle(*e_it, 0);
        h01 = mesh_.halfedge_handle(*e_it, 1);
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
        v0 = mesh_.to_vertex_handle(h10);
        v1 = mesh_.to_vertex_handle(h01);

        if (is_too_short(v0, v1)) {
          // get status
          b0 = mesh_.is_boundary(v0);
          b1 = mesh_.is_boundary(v1);
          l0 = mesh_.status(v0).locked();
          l1 = mesh_.status(v1).locked();
          f0 = mesh_.status(v0).feature();
          f1 = mesh_.status(v1).feature();
          hcol01 = hcol10 = true;

          // boundary rules
          if (b0 && b1) {
Jan Möbius's avatar
Jan Möbius committed
662
            if (!mesh_.is_boundary(*e_it))
663 664 665 666 667 668 669 670 671 672 673 674 675 676
              continue;
          } else if (b0)
            hcol01 = false;
          else if (b1)
            hcol10 = false;

          // locked rules
          if (l0 && l1)
            continue;
          else if (l0)
            hcol01 = false;
          else if (l1)
            hcol10 = false;

Max Lyon's avatar
Max Lyon committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
          // feature vertex rules
          if (f0 && f1)
            continue;
          else if (f0)
            hcol01 = false;
          else if (f1)
            hcol10 = false;

          // feature edge rules
          // if vertex is incident to feature edge but collapse edge is not feature dont collapse

          // hcol01
          {
            int feature_valence_0 = 0;
            for (auto eh : mesh_.ve_range(v0))
              if (mesh_.status(eh).feature())
                ++feature_valence_0;
            if (feature_valence_0 == 2)
            {
              if (!mesh_.status(*e_it).feature())
                hcol01 = false;
            }
            else if (feature_valence_0 != 0)
              hcol01 = false;
          }
          // hcol10
          {
            int feature_valence_1 = 0;
            for (auto eh : mesh_.ve_range(v1))
              if (mesh_.status(eh).feature())
                ++feature_valence_1;
            if (feature_valence_1 == 2)
            {
              if (!mesh_.status(*e_it).feature())
                hcol10 = false;
            }
            else if (feature_valence_1 != 0)
              hcol10 = false;
          }
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

          // the other two edges removed by collapse must not be features
          h0 = mesh_.prev_halfedge_handle(h01);
          h1 = mesh_.next_halfedge_handle(h10);
          if (mesh_.status(mesh_.edge_handle(h0)).feature() || mesh_.status(mesh_.edge_handle(h1)).feature())
            hcol01 = false;
          h0 = mesh_.prev_halfedge_handle(h10);
          h1 = mesh_.next_halfedge_handle(h01);
          if (mesh_.status(mesh_.edge_handle(h0)).feature() || mesh_.status(mesh_.edge_handle(h1)).feature())
            hcol10 = false;

          // topological rules
          if (hcol01)
            hcol01 = mesh_.is_collapse_ok(h01);
          if (hcol10)
            hcol10 = mesh_.is_collapse_ok(h10);

          // both collapses possible: collapse into vertex w/ higher valence
          if (hcol01 && hcol10) {
            if (mesh_.valence(v0) < mesh_.valence(v1))
              hcol10 = false;
            else
              hcol01 = false;
          }

741 742 743 744 745 746
          // check if collapse flips triangles
          if (collapse_flips_normal(h01))
            hcol01 = false;
          if (collapse_flips_normal(h10))
            hcol10 = false;

747 748 749 750
          // try v1 -> v0
          if (hcol10) {
            // don't create too long edges
            skip = false;
Jan Möbius's avatar
Jan Möbius committed
751 752
            for (vv_it = mesh_.cvv_iter(v1); vv_it.is_valid() && !skip; ++vv_it)
              if (is_too_long(v0, *vv_it))
753 754 755 756 757 758 759 760 761 762 763 764
                skip = true;

            if (!skip) {
              mesh_.collapse(h10);
              ok = false;
            }
          }

          // try v0 -> v1
          else if (hcol01) {
            // don't create too long edges
            skip = false;
Jan Möbius's avatar
Jan Möbius committed
765 766
            for (vv_it = mesh_.cvv_iter(v0); vv_it.is_valid() && !skip; ++vv_it)
              if (is_too_long(v1, *vv_it))
767 768 769 770 771 772 773 774
                skip = true;

            if (!skip) {
              mesh_.collapse(h01);
              ok = false;
            }
          }
        }
Jan Möbius's avatar
Jan Möbius committed
775 776 777 778 779 780
      }
    }
  }

  mesh_.garbage_collection();

781 782
  if (i == 100)
    omlog() << "collapse break\n";
Jan Möbius's avatar
Jan Möbius committed
783 784 785 786 787 788 789 790 791 792 793
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
flip_edges()
{
Jan Möbius's avatar
Jan Möbius committed
794 795 796 797 798 799
  typename Mesh::EIter e_it, e_end;
  typename Mesh::VIter v_it, v_end;
  typename Mesh::VHandle v0, v1, v2, v3, vh;
  typename Mesh::HHandle hh;
  typename Mesh::Point p;
  typename Mesh::FHandle fh;
Jan Möbius's avatar
Jan Möbius committed
800

Jan Möbius's avatar
Jan Möbius committed
801 802 803 804 805
  int val0, val1, val2, val3;
  int val_opt0, val_opt1, val_opt2, val_opt3;
  int ve0, ve1, ve2, ve3, ve_before, ve_after;
  bool ok;
  int i;
Jan Möbius's avatar
Jan Möbius committed
806 807

  // compute vertex valences
Jan Möbius's avatar
Jan Möbius committed
808
  for (v_it = mesh_.vertices_begin(), v_end = mesh_.vertices_end(); v_it != v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
809
    mesh_.property(valences_, *v_it) = mesh_.valence(*v_it);
Jan Möbius's avatar
Jan Möbius committed
810 811

  // flip all edges
Jan Möbius's avatar
Jan Möbius committed
812
  for (ok = false, i = 0; !ok && i < 100; ++i) {
Jan Möbius's avatar
Jan Möbius committed
813 814
    ok = true;

Jan Möbius's avatar
Jan Möbius committed
815
    for (e_it = mesh_.edges_begin(), e_end = mesh_.edges_end(); e_it != e_end; ++e_it) {
Jan Möbius's avatar
Jan Möbius committed
816 817
      if (!mesh_.status(*e_it).locked() && !mesh_.status(*e_it).feature()) {
        hh = mesh_.halfedge_handle(*e_it, 0);
Jan Möbius's avatar
Jan Möbius committed
818 819 820 821 822 823
        v0 = mesh_.to_vertex_handle(hh);
        v2 = mesh_.to_vertex_handle(mesh_.next_halfedge_handle(hh));
        if ( !mesh_.next_halfedge_handle(hh).is_valid() ) {
          std::cerr << "Error v2" << std::endl;
          continue;
        }
Jan Möbius's avatar
Jan Möbius committed
824
        hh = mesh_.halfedge_handle(*e_it, 1);
Jan Möbius's avatar
Jan Möbius committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
        v1 = mesh_.to_vertex_handle(hh);
        v3 = mesh_.to_vertex_handle(mesh_.next_halfedge_handle(hh));
        if ( !mesh_.next_halfedge_handle(hh).is_valid() ) {
          std::cerr << "Error v3" << std::endl;
          continue;
        }

        if ( !v2.is_valid())
          continue;

        if (!mesh_.status(v0).locked() && !mesh_.status(v1).locked() && !mesh_.status(v2).locked()
            && !mesh_.status(v3).locked()) {
          val0 = mesh_.property(valences_, v0);
          val1 = mesh_.property(valences_, v1);
          val2 = mesh_.property(valences_, v2);
          val3 = mesh_.property(valences_, v3);

          val_opt0 = (mesh_.is_boundary(v0) ? 4 : 6);
          val_opt1 = (mesh_.is_boundary(v1) ? 4 : 6);
          val_opt2 = (mesh_.is_boundary(v2) ? 4 : 6);
          val_opt3 = (mesh_.is_boundary(v3) ? 4 : 6);

          ve0 = (val0 - val_opt0);
          ve0 *= ve0;
          ve1 = (val1 - val_opt1);
          ve1 *= ve1;
          ve2 = (val2 - val_opt2);
          ve2 *= ve2;
          ve3 = (val3 - val_opt3);
          ve3 *= ve3;

          ve_before = ve0 + ve1 + ve2 + ve3;

          --val0;
          --val1;
          ++val2;
          ++val3;

          ve0 = (val0 - val_opt0);
          ve0 *= ve0;
          ve1 = (val1 - val_opt1);
          ve1 *= ve1;
          ve2 = (val2 - val_opt2);
          ve2 *= ve2;
          ve3 = (val3 - val_opt3);
          ve3 *= ve3;

          ve_after = ve0 + ve1 + ve2 + ve3;

874
          if (ve_before > ve_after && mesh_.is_flip_ok(*e_it) && !edge_flip_flips_normal(*e_it)) {
Jan Möbius's avatar
Jan Möbius committed
875
            mesh_.flip(*e_it);
Jan Möbius's avatar
Jan Möbius committed
876 877 878 879 880 881 882
            --mesh_.property(valences_, v0);
            --mesh_.property(valences_, v1);
            ++mesh_.property(valences_, v2);
            ++mesh_.property(valences_, v3);
            ok = false;
          }
        }
Jan Möbius's avatar
Jan Möbius committed
883 884 885 886
      }
    }
  }

Jan Möbius's avatar
Jan Möbius committed
887 888
  if (i == 100)
    omlog() << "flip break\n";
Jan Möbius's avatar
Jan Möbius committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
tangential_smoothing(bool _use_projection)
{
  typename Mesh::VIter     v_it, v_end(mesh_.vertices_end());
  typename Mesh::CVVIter   vv_it;
  typename Mesh::Point     u, n;


  // tag active vertices
  for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
907 908 909
    mesh_.status(*v_it).set_tagged( !mesh_.status(*v_it).locked() &&
				   !mesh_.status(*v_it).feature() &&
				   !mesh_.is_boundary(*v_it) );
Jan Möbius's avatar
Jan Möbius committed
910

911 912 913
  mesh_.update_face_normals();

  double damping = 1.0;
Jan Möbius's avatar
Jan Möbius committed
914 915 916 917 918 919

  // smooth
  for (int iters=0; iters<10; ++iters)
  {
    for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
    {
Jan Möbius's avatar
Jan Möbius committed
920
      if (mesh_.status(*v_it).tagged())
Jan Möbius's avatar
Jan Möbius committed
921
      {
922
        u.vectorize(0.0);
Max Lyon's avatar
Max Lyon committed
923 924
        int valence = 0;
        int feature_valence = 0;
925

Max Lyon's avatar
Max Lyon committed
926
        for (auto heh : mesh_.voh_range(*v_it))
927
        {
Max Lyon's avatar
Max Lyon committed
928 929 930
          u += mesh_.point(mesh_.to_vertex_handle(heh));
          if (mesh_.status(mesh_.edge_handle(heh)).feature())
            ++feature_valence;
931 932 933 934 935 936 937 938 939 940 941 942
          ++valence;
        }

        if (valence)
        {
          u *= (1.0/valence);
          u -= mesh_.point(*v_it);
          n  = mesh_.normal(*v_it);
          n *= (u | n);
          u -= n;
        }

Max Lyon's avatar
Max Lyon committed
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
        if (feature_valence == 2)
        {
          // project update onto feature directions
          typename Mesh::Point feature_dir(0.0,0.0,0.0);
          for (auto heh : mesh_.voh_range(*v_it))
          {
            if (mesh_.status(mesh_.edge_handle(heh)).feature())
            {
              auto dir = mesh_.point(mesh_.to_vertex_handle(heh)) - mesh_.point(mesh_.from_vertex_handle(heh));
              if ((dir | feature_dir) > 0)
                feature_dir += dir;
              else
                feature_dir -= dir;
            }
          }
          if (feature_dir.sqrnorm() > 0)
            feature_dir.normalize();
          u = (feature_dir | u) * feature_dir;
        }
        else if (feature_valence != 0)
        {
          // more than two or exactly one incident feature edge means vertex should be preserved
          u.vectorize(0.0);
        }

968
        mesh_.property(update_, *v_it) = u;
Jan Möbius's avatar
Jan Möbius committed
969 970 971 972
      }
    }

    for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
973
      if (mesh_.status(*v_it).tagged())
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
        mesh_.point(*v_it) += damping*mesh_.property(update_, *v_it);

    // check if normals changed
    bool normals_changed = false;
    for (auto fh : mesh_.faces())
    {
      if ((mesh_.calc_face_normal(fh) | mesh_.normal(fh)) < 0)
      {
        normals_changed = true;
        break;
      }
    }

    if (normals_changed)
    {
      // revert update and try again with smaller step
      for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
        if (mesh_.status(*v_it).tagged())
          mesh_.point(*v_it) -= damping*mesh_.property(update_, *v_it);
      damping *= 0.5;
    }
Jan Möbius's avatar
Jan Möbius committed
995 996 997 998 999
  }


  // reset tagged status
  for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
1000
    mesh_.status(*v_it).set_tagged(false);
Jan Möbius's avatar
Jan Möbius committed
1001 1002 1003 1004 1005


  // project
  if (_use_projection)
    for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
1006 1007
      if (!mesh_.status(*v_it).locked() && !mesh_.status(*v_it).feature())
	project_to_reference(*v_it);
Jan Möbius's avatar
Jan Möbius committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
balanace_area(unsigned int _iters, bool _use_projection)
{
  typename Mesh::VIter     v_it, v_end(mesh_.vertices_end());
  typename Mesh::CVVIter   vv_it;
  typename Mesh::Scalar    w, ww;
  typename Mesh::Point     u, n;


  DiffGeoT<Mesh>  diffgeo(mesh_);


  // tag active vertices
  for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
  {
Jan Möbius's avatar
Jan Möbius committed
1031 1032 1033
    bool active = ( !mesh_.status(*v_it).locked() &&
        !mesh_.status(*v_it).feature() &&
        !mesh_.is_boundary(*v_it) );
Jan Möbius's avatar
Jan Möbius committed
1034 1035

    // don't move neighbors of boundary vertices
Jan Möbius's avatar
Jan Möbius committed
1036 1037 1038
    for (vv_it=mesh_.cvv_iter(*v_it); active && vv_it.is_valid(); ++vv_it)
      if (mesh_.is_boundary(*vv_it))
        active = false;
Jan Möbius's avatar
Jan Möbius committed
1039

Jan Möbius's avatar
Jan Möbius committed
1040
    mesh_.status(*v_it).set_tagged( active );
Jan Möbius's avatar
Jan Möbius committed
1041 1042 1043 1044 1045 1046
  }


  // tag2 vertices for which area has to be computed
  for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
  {
Jan Möbius's avatar
Jan Möbius committed
1047 1048
    mesh_.status(*v_it).set_tagged2(false);
    for (vv_it=mesh_.cvv_iter(*v_it); vv_it.is_valid(); ++vv_it)
Jan Möbius's avatar
Jan Möbius committed
1049
    {
Jan Möbius's avatar
Jan Möbius committed
1050
      if (mesh_.status(*vv_it).tagged())
Jan Möbius's avatar
Jan Möbius committed
1051
      {
Jan Möbius's avatar
Jan Möbius committed
1052 1053
        mesh_.status(*v_it).set_tagged2(true);
        break;
Jan Möbius's avatar
Jan Möbius committed
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
      }
    }
  }



  for (unsigned int bla=0; bla<_iters; ++bla)
  {
    // compute vertex areas
    for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
1064 1065
      if (mesh_.status(*v_it).tagged2())
        mesh_.property(area_, *v_it) = pow(diffgeo.compute_area(*v_it), 2);
Jan Möbius's avatar
Jan Möbius committed
1066 1067 1068 1069 1070 1071 1072 1073



    // smooth
    for (int iters=0; iters<10; ++iters)
    {
      for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
      {
Jan Möbius's avatar
Jan Möbius committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
        if (mesh_.status(*v_it).tagged())
        {
          u.vectorize(0.0);
          ww   = 0;

          for (vv_it=mesh_.cvv_iter(*v_it); vv_it.is_valid(); ++vv_it)
          {
            w   = mesh_.property(area_, *vv_it);
            u  += mesh_.point(*vv_it) * w;
            ww += w;
          }

          if (ww)
          {
            u *= (1.0/ww);
            u -= mesh_.point(*v_it);
            n  = mesh_.normal(*v_it);
            n *= (u | n);
            u -= n;

            u *= 0.1;
          }

          mesh_.property(update_, *v_it) = u;
        }
Jan Möbius's avatar
Jan Möbius committed
1099 1100 1101
      }

      for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
1102 1103 1104 1105
        if (!mesh_.status(*v_it).locked() &&
            !mesh_.status(*v_it).feature() &&
            !mesh_.is_boundary(*v_it))
          mesh_.point(*v_it) += mesh_.property(update_, *v_it);
Jan Möbius's avatar
Jan Möbius committed
1106 1107 1108 1109 1110 1111
    }


    // project
    if (_use_projection)
      for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
1112 1113
        if (!mesh_.status(*v_it).locked() && !mesh_.status(*v_it).feature())
          project_to_reference(*v_it);
Jan Möbius's avatar
Jan Möbius committed
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145


//     if (emit_progress_)  
//       if (!Progress().step(3))
// 	break;
  }
}


//-----------------------------------------------------------------------------


template <class Mesh>
void
BaseRemesherT<Mesh>::
remove_caps()
{
  typename Mesh::EdgeIter        e_it, e_end(mesh_.edges_end());
  typename Mesh::HalfedgeHandle  h;
  typename Mesh::Scalar          a0, a1, amin, aa(cos(170.0 * M_PI / 180.0));
  typename Mesh::VHandle         v, vb, vd;
  typename Mesh::FHandle         fb, fd;
  typename Mesh::Point           a, b, c, d;


  // handle Nastran PIDs for edge flips
  OpenMesh::FPropHandleT<unsigned int> pids;
  mesh_.get_property_handle(pids, "Nastran PIDs");


  for (e_it=mesh_.edges_begin(); e_it!=e_end; ++e_it)
  {
Jan Möbius's avatar
Jan Möbius committed
1146 1147
    if (!mesh_.status(*e_it).locked() &&
        mesh_.is_flip_ok(*e_it))
Jan Möbius's avatar
Jan Möbius committed
1148
    {
Jan Möbius's avatar
Jan Möbius committed
1149
      h  = mesh_.halfedge_handle(*e_it, 0);
Jan Möbius's avatar
Jan Möbius committed
1150 1151 1152 1153 1154
      a  = mesh_.point(mesh_.to_vertex_handle(h));

      h  = mesh_.next_halfedge_handle(h);
      b  = mesh_.point(vb=mesh_.to_vertex_handle(h));

Jan Möbius's avatar
Jan Möbius committed
1155
      h  = mesh_.halfedge_handle(*e_it, 1);
Jan Möbius's avatar
Jan Möbius committed
1156 1157 1158 1159 1160 1161 1162
      c  = mesh_.point(mesh_.to_vertex_handle(h));

      h  = mesh_.next_halfedge_handle(h);
      d  = mesh_.point(vd=mesh_.to_vertex_handle(h));

      a0 = ( (a-b).normalize() | (c-b).normalize() );
      a1 = ( (a-d).normalize() | (c-d).normalize() );
1163

Jan Möbius's avatar
Jan Möbius committed
1164 1165 1166 1167 1168 1169
      if (a0 < a1)  { amin = a0; v = vb; }
      else          { amin = a1; v = vd; }

      // is it a cap?
      if (amin < aa)
      {
Jan Möbius's avatar
Jan Möbius committed
1170 1171 1172
        // feature edge and feature vertex -> seems to be intended
        if (mesh_.status(*e_it).feature() && mesh_.status(v).feature())
          continue;
Jan Möbius's avatar
Jan Möbius committed
1173

Jan Möbius's avatar
Jan Möbius committed
1174 1175 1176 1177 1178
        // handle PIDs: flip = split + collapse
        if (pids.is_valid())
        {
          fb = mesh_.face_handle(mesh_.halfedge_handle(*e_it, 0));
          fd = mesh_.face_handle(mesh_.halfedge_handle(*e_it, 1));
Jan Möbius's avatar
Jan Möbius committed
1179

Jan Möbius's avatar
Jan Möbius committed
1180 1181 1182 1183 1184
          if (v == vb)
            mesh_.property(pids, fb) = mesh_.property(pids, fd);
          else
            mesh_.property(pids, fd) = mesh_.property(pids, fb);
        }
Jan Möbius's avatar
Jan Möbius committed
1185

Jan Möbius's avatar
Jan Möbius committed
1186 1187 1188
        // project v onto feature edge
        if (mesh_.status(*e_it).feature())
          mesh_.set_point(v, (a+c)*0.5);
Jan Möbius's avatar
Jan Möbius committed
1189

Jan Möbius's avatar
Jan Möbius committed
1190 1191
        // flip
        mesh_.flip(*e_it);
Jan Möbius's avatar
Jan Möbius committed
1192 1193 1194 1195 1196 1197
      }
    }
  }
}


1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
template<class Mesh>
bool
BaseRemesherT<Mesh>::
edge_flip_flips_normal(EdgeHandle _eh)
{
  if (mesh_.is_boundary(_eh))
    return true;

  auto heh = mesh_.halfedge_handle(_eh, 0);

  // get the four points of the two incident faces in ccw order
  auto p0 = mesh_.point(mesh_.to_vertex_handle(heh));
  auto p1 = mesh_.point(mesh_.to_vertex_handle(mesh_.next_halfedge_handle(heh)));
  auto p2 = mesh_.point(mesh_.from_vertex_handle(heh));
  auto p3 = mesh_.point(mesh_.to_vertex_handle(mesh_.next_halfedge_handle(mesh_.opposite_halfedge_handle(heh))));

  // compute normals before flip
  auto n0_before = (p1-p0) % (p2-p0);
  auto n1_before = (p2-p0) % (p3-p0);

  // compute normals after flip
  auto n0_after = (p1-p0) % (p3-p0);
  auto n1_after = (p3-p2) % (p1-p2);

  // compare all pairs of before and after normals
  if ((n0_before | n0_after) < 0 || (n1_before | n1_after) < 0 ||
      (n0_before | n1_after) < 0 || (n1_before | n0_after) < 0)
    return true;

  return false;
}


template<class Mesh>
bool
BaseRemesherT<Mesh>::
collapse_flips_normal(HalfedgeHandle _heh)
{
  if (!mesh_.is_collapse_ok(_heh))
    return true;

  // get faces that are removed by the collapse
  auto fh0 = mesh_.face_handle(_heh);
  auto fh1 = mesh_.face_handle(mesh_.opposite_halfedge_handle(_heh));

  auto collapsing_vertex = mesh_.from_vertex_handle(_heh);
  auto point_before = mesh_.point(collapsing_vertex);

  // compute normals before collapse
  std::vector<decltype (mesh_.calc_face_normal(OpenMesh::FaceHandle(0)))> normals_before;
  for (auto fh : mesh_.vf_range(collapsing_vertex))
    normals_before.push_back(mesh_.calc_face_normal(fh));

  // move point
  mesh_.point(collapsing_vertex) = mesh_.point(mesh_.to_vertex_handle(_heh));

  bool collapse_ok = true;

  int i = 0;
  for (auto fh : mesh_.vf_range(collapsing_vertex))
  {
    if (fh != fh0 && fh != fh1) // we don't care about faces that are removec by the collapse
    {
      auto normal_after = mesh_.calc_face_normal(fh);
      if ((normal_after | normals_before[i]) <= 0)
        collapse_ok = false;
    }
    ++i;
  }

  // move point back
  mesh_.point(collapsing_vertex) = point_before;

  return !collapse_ok;
}


Jan Möbius's avatar
Jan Möbius committed
1275 1276 1277
//=============================================================================
} // namespace Remeshing
//=============================================================================