SmootherT_impl.hh 12 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
2 3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo  
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39 40
 *                                                                           *
 * ========================================================================= */
41

42

Jan Möbius's avatar
Jan Möbius committed
43 44

/** \file SmootherT.cc
45

Jan Möbius's avatar
Jan Möbius committed
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
 */

//=============================================================================
//
//  CLASS SmootherT - IMPLEMENTATION
//
//=============================================================================

#define OPENMESH_SMOOTHERT_C

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

#include <OpenMesh/Core/Utils/vector_cast.hh>
#include <OpenMesh/Tools/Smoother/SmootherT.hh>

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


namespace OpenMesh {
namespace Smoother {


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


template <class Mesh>
SmootherT<Mesh>::
SmootherT(Mesh& _mesh)
74 75
  : mesh_(_mesh),
    skip_features_(false)
Jan Möbius's avatar
Jan Möbius committed
76 77 78 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
  // request properties
  mesh_.request_vertex_status();
  mesh_.request_face_normals();
  mesh_.request_vertex_normals();

  // custom properties
  mesh_.add_property(original_positions_);
  mesh_.add_property(original_normals_);
  mesh_.add_property(new_positions_);
  mesh_.add_property(is_active_);


  // default settings
  component_  = Tangential_and_Normal;
  continuity_ = C0;
  tolerance_  = -1.0;
}


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


template <class Mesh>
SmootherT<Mesh>::
~SmootherT()
{
  // free properties
  mesh_.release_vertex_status();
  mesh_.release_face_normals();
  mesh_.release_vertex_normals();

  // free custom properties
  mesh_.remove_property(original_positions_);
  mesh_.remove_property(original_normals_);
  mesh_.remove_property(new_positions_);
  mesh_.remove_property(is_active_);
}


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


template <class Mesh>
void
SmootherT<Mesh>::
initialize(Component _comp, Continuity _cont)
{
  typename Mesh::VertexIter  v_it, v_end(mesh_.vertices_end());


  // store smoothing settings
  component_  = _comp;
  continuity_ = _cont;


  // update normals
  mesh_.update_face_normals();
  mesh_.update_vertex_normals();


  // store original points & normals
  for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
  {
Jan Möbius's avatar
Jan Möbius committed
140 141
    mesh_.property(original_positions_, *v_it) = mesh_.point(*v_it);
    mesh_.property(original_normals_,   *v_it) = mesh_.normal(*v_it);
Jan Möbius's avatar
Jan Möbius committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  }
}


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


template <class Mesh>
void
SmootherT<Mesh>::
set_active_vertices()
{
  typename Mesh::VertexIter  v_it, v_end(mesh_.vertices_end());


  // is something selected?
  bool nothing_selected(true);
  for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
160
    if (mesh_.status(*v_it).selected())
Jan Möbius's avatar
Jan Möbius committed
161 162 163 164 165 166
    { nothing_selected = false; break; }


  // tagg all active vertices
  for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
  {
Jan Möbius's avatar
Jan Möbius committed
167 168 169
    bool active = ((nothing_selected || mesh_.status(*v_it).selected())
	                 && !mesh_.is_boundary(*v_it)
	                 && !mesh_.status(*v_it).locked());
170 171 172

    if ( skip_features_ ) {

Jan Möbius's avatar
Jan Möbius committed
173
      active = active && !mesh_.status(*v_it).feature();
174

Jan Möbius's avatar
Jan Möbius committed
175
      typename Mesh::VertexOHalfedgeIter  voh_it(mesh_,*v_it);
Jan Möbius's avatar
Jan Möbius committed
176
      for ( ; voh_it.is_valid() ; ++voh_it ) {
177 178

        // If the edge is a feature edge, skip the current vertex while smoothing
179
        if ( mesh_.status(mesh_.edge_handle(*voh_it)).feature() )
180 181
          active = false;

182 183
        typename Mesh::FaceHandle fh1 = mesh_.face_handle(*voh_it );
        typename Mesh::FaceHandle fh2 = mesh_.face_handle(mesh_.opposite_halfedge_handle(*voh_it ) );
184 185 186 187 188 189 190 191 192 193

        // If one of the faces is a feature, lock current vertex
        if ( fh1.is_valid() && mesh_.status( fh1 ).feature() )
          active = false;
        if ( fh2.is_valid() && mesh_.status( fh2 ).feature() )
          active = false;

      }
    }

Jan Möbius's avatar
Jan Möbius committed
194
    mesh_.property(is_active_, *v_it) = active;
Jan Möbius's avatar
Jan Möbius committed
195 196 197 198 199 200 201 202 203
  }


  // C1: remove one ring of boundary vertices
  if (continuity_ == C1)
  {
    typename Mesh::VVIter     vv_it;

    for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
204
      if (mesh_.is_boundary(*v_it))
Jan Möbius's avatar
Jan Möbius committed
205 206
        for (vv_it=mesh_.vv_iter(*v_it); vv_it.is_valid(); ++vv_it)
          mesh_.property(is_active_, *vv_it) = false;
Jan Möbius's avatar
Jan Möbius committed
207 208 209 210 211 212 213 214 215 216
  }


  // C2: remove two rings of boundary vertices
  if (continuity_ == C2)
  {
    typename Mesh::VVIter     vv_it;

    for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
    {
Jan Möbius's avatar
Jan Möbius committed
217 218
      mesh_.status(*v_it).set_tagged(false);
      mesh_.status(*v_it).set_tagged2(false);
Jan Möbius's avatar
Jan Möbius committed
219 220 221
    }

    for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
222 223 224
      if (mesh_.is_boundary(*v_it))
        for (vv_it=mesh_.vv_iter(*v_it); vv_it.is_valid(); ++vv_it)
          mesh_.status(*v_it).set_tagged(true);
Jan Möbius's avatar
Jan Möbius committed
225 226

    for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
227 228 229
      if (mesh_.status(*v_it).tagged())
        for (vv_it=mesh_.vv_iter(*v_it); vv_it.is_valid(); ++vv_it)
          mesh_.status(*v_it).set_tagged2(true);
Jan Möbius's avatar
Jan Möbius committed
230 231 232

    for (v_it=mesh_.vertices_begin(); v_it!=v_end; ++v_it)
    {
Jan Möbius's avatar
Jan Möbius committed
233 234 235 236
      if (mesh_.status(*v_it).tagged2())
        mesh_.property(is_active_, *vv_it) = false;
      mesh_.status(*v_it).set_tagged(false);
      mesh_.status(*v_it).set_tagged2(false);
Jan Möbius's avatar
Jan Möbius committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    }
  }
}


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


template <class Mesh>
void
SmootherT<Mesh>::
set_relative_local_error(Scalar _err)
{
  if (!mesh_.vertices_empty())
  {
252
    typename Mesh::VertexIter  v_it(mesh_.vertices_begin()),
Jan Möbius's avatar
Jan Möbius committed
253 254 255 256 257
                               v_end(mesh_.vertices_end());


    // compute bounding box
    Point  bb_min, bb_max;
258
    bb_min = bb_max = mesh_.point(*v_it);
Jan Möbius's avatar
Jan Möbius committed
259 260
    for (++v_it; v_it!=v_end; ++v_it)
    {
261 262
        minimize(bb_min, mesh_.point(*v_it));
        maximize(bb_max, mesh_.point(*v_it));
Jan Möbius's avatar
Jan Möbius committed
263 264 265 266
    }


    // abs. error = rel. error * bounding-diagonal
267
    set_absolute_local_error(norm(_err * (bb_max-bb_min)));
Jan Möbius's avatar
Jan Möbius committed
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
  }
}


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


template <class Mesh>
void
SmootherT<Mesh>::
set_absolute_local_error(Scalar _err)
{
  tolerance_ = _err;
}


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


template <class Mesh>
void
SmootherT<Mesh>::
disable_local_error_check()
{
  tolerance_ = -1.0;
}


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


template <class Mesh>
void
SmootherT<Mesh>::
smooth(unsigned int _n)
{
  // mark active vertices
  set_active_vertices();

  // smooth _n iterations
  while (_n--)
  {
    compute_new_positions();

    if (component_ == Tangential)
      project_to_tangent_plane();

    else if (tolerance_ >= 0.0)
	local_error_check();

    move_points();
  }
}


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


template <class Mesh>
void
SmootherT<Mesh>::
compute_new_positions()
{
  switch (continuity_)
  {
    case C0:
      compute_new_positions_C0();
      break;

    case C1:
      compute_new_positions_C1();
      break;

    case C2:
      break;
  }
}


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


template <class Mesh>
void
SmootherT<Mesh>::
project_to_tangent_plane()
{
355
  typename Mesh::VertexIter  v_it(mesh_.vertices_begin()),
Jan Möbius's avatar
Jan Möbius committed
356 357 358 359 360 361
                             v_end(mesh_.vertices_end());
  // Normal should be a vector type. In some environment a vector type
  // is different from point type, e.g. OpenSG!
  typename Mesh::Normal      translation, normal;


362
  for (; v_it != v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
363
  {
Jan Möbius's avatar
Jan Möbius committed
364
    if (is_active(*v_it))
Jan Möbius's avatar
Jan Möbius committed
365
    {
Jan Möbius's avatar
Jan Möbius committed
366 367
      translation  = new_position(*v_it)-orig_position(*v_it);
      normal       = orig_normal(*v_it);
Jan Möbius's avatar
Jan Möbius committed
368 369
      normal      *= dot(translation, normal);
      translation -= normal;
Jan Möbius's avatar
Jan Möbius committed
370 371
      translation += vector_cast<typename Mesh::Normal>(orig_position(*v_it));
      set_new_position(*v_it, translation);
Jan Möbius's avatar
Jan Möbius committed
372 373 374 375 376 377 378 379 380 381 382 383 384
    }
  }
}


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


template <class Mesh>
void
SmootherT<Mesh>::
local_error_check()
{
385
  typename Mesh::VertexIter  v_it(mesh_.vertices_begin()),
Jan Möbius's avatar
Jan Möbius committed
386 387 388 389 390 391
                             v_end(mesh_.vertices_end());

  typename Mesh::Normal      translation;
  typename Mesh::Scalar      s;


392
  for (; v_it != v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
393
  {
Jan Möbius's avatar
Jan Möbius committed
394
    if (is_active(*v_it))
Jan Möbius's avatar
Jan Möbius committed
395
    {
Jan Möbius's avatar
Jan Möbius committed
396
      translation  = new_position(*v_it) - orig_position(*v_it);
Jan Möbius's avatar
Jan Möbius committed
397

Jan Möbius's avatar
Jan Möbius committed
398
      s = fabs(dot(translation, orig_normal(*v_it)));
Jan Möbius's avatar
Jan Möbius committed
399 400 401

      if (s > tolerance_)
      {
Jan Möbius's avatar
Jan Möbius committed
402 403 404
        translation *= (tolerance_ / s);
        translation += vector_cast<NormalType>(orig_position(*v_it));
        set_new_position(*v_it, translation);
Jan Möbius's avatar
Jan Möbius committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418
      }
    }
  }
}


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


template <class Mesh>
void
SmootherT<Mesh>::
move_points()
{
419
  typename Mesh::VertexIter  v_it(mesh_.vertices_begin()),
Jan Möbius's avatar
Jan Möbius committed
420 421
                             v_end(mesh_.vertices_end());

422
  for (; v_it != v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
423 424
    if (is_active(*v_it))
      mesh_.set_point(*v_it, mesh_.property(new_positions_, *v_it));
Jan Möbius's avatar
Jan Möbius committed
425 426 427 428 429 430 431
}


//=============================================================================
} // namespace Smoother
} // namespace OpenMesh
//=============================================================================