SmootherT.hh 9.16 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.hh
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
//
//=============================================================================

#ifndef OPENMESH_SMOOTHER_SMOOTHERT_HH
#define OPENMESH_SMOOTHER_SMOOTHERT_HH


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

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

//== FORWARDDECLARATIONS ======================================================

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

namespace OpenMesh {
namespace Smoother {

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

/** Base class for smoothing algorithms.
74
 */
Jan Möbius's avatar
Jan Möbius committed
75 76 77 78 79 80 81 82 83 84 85 86
template <class Mesh>
class SmootherT : private Utils::Noncopyable
{
public:

  typedef typename Mesh::Scalar        Scalar;
  typedef typename Mesh::Point         Point;
  typedef typename Mesh::Normal        NormalType;
  typedef typename Mesh::VertexHandle  VertexHandle;
  typedef typename Mesh::EdgeHandle    EdgeHandle;

  // initialize smoother
87
  enum Component {
Jan Möbius's avatar
Jan Möbius committed
88 89 90 91 92
    Tangential,           ///< Smooth tangential direction
    Normal,               ///< Smooth normal direction
    Tangential_and_Normal ///< Smooth tangential and normal direction
  };

93 94 95 96
  enum Continuity {
    C0,
    C1,
    C2
Jan Möbius's avatar
Jan Möbius committed
97 98 99 100
  };

public:

101 102
  /** \brief constructor & destructor
   *
103
   * @param _mesh Reference a triangle or poly mesh
104
   */
Jan Möbius's avatar
Jan Möbius committed
105 106 107 108 109 110
  SmootherT( Mesh& _mesh );
  virtual ~SmootherT();


public:

111 112 113 114 115
  //===========================================================================
  /** @name Initialization and algorithm execution
  * @{ */
  //===========================================================================

116 117 118 119
  /** Initialize smoother
   * \param _comp Determine component to smooth
   * \param _cont Determine Continuity
   */
Jan Möbius's avatar
Jan Möbius committed
120 121
  void initialize(Component _comp, Continuity _cont);

122 123 124 125 126 127 128 129 130
  /// Do _n smoothing iterations
  virtual void smooth(unsigned int _n);

  /** @} */

  //===========================================================================
  /** @name Error control functions
  * @{ */
  //===========================================================================
Jan Möbius's avatar
Jan Möbius committed
131

132 133 134 135 136 137 138 139 140
  /** \brief Set local error relative to bounding box
   *
   * This function sets a maximal error tolerance for the smoother as a fraction
   * of the bounding box of the mesh. First the bounding box diagonal is computed.
   * Then the error is set as the length of the diagonal multiplied with the
   * given factor.
   *
   * @param _err Factor scaling the bounding box diagonal
   */
Jan Möbius's avatar
Jan Möbius committed
141
  void set_relative_local_error(Scalar _err);
142 143 144 145 146 147 148

  /** \brief Set local error as an absolute value
   *
   * Set the maximal error tolerance of the smoother to the given value.
   *
   * @param _err Maximal error
   */
Jan Möbius's avatar
Jan Möbius committed
149
  void set_absolute_local_error(Scalar _err);
150 151 152 153 154

  /** \brief Disable error control of the smoother
   *
   * This function disables the error control of the smoother.
   */
Jan Möbius's avatar
Jan Möbius committed
155 156
  void disable_local_error_check();

157
  /** \brief enable or disable feature handling
158 159 160 161 162
   *
   * This function can be used to control if features on the mesh should be preserved.
   * If enabled, the smoother will keep features and does not modify them. Features
   * can be set via OpenMesh status flags (request status and set primitives as features).
   * Feature flag can be set for vertices edges and faces.
163 164 165 166 167
   *
   * @param _state true  : If features are selected on the mesh, they will be left unmodified\n
   *               false : Features will be ignored
   */
  void skip_features( bool _state ){ skip_features_ = _state; };
Jan Möbius's avatar
Jan Möbius committed
168 169


170
  /** @} */
Jan Möbius's avatar
Jan Möbius committed
171

172
private:
Jan Möbius's avatar
Jan Möbius committed
173

174 175 176 177 178 179 180 181 182 183
  /** \brief  Find active vertices. Resets tagged status !
     *
     * This function recomputes the set of active vertices, which will be touched by the smoother.
     * If nothing on the mesh is selected, all vertices which are not locked, feature or boundary will be
     * marked as active and moved by the smoother.
     * If vertices are selected, than only the selected ones, excluding the locked, feature and boundary vertices will be
     * moved.
     *
     * The function is called first when running the smoother.
     */
Jan Möbius's avatar
Jan Möbius committed
184 185 186 187 188 189 190
  void set_active_vertices();

  // single steps of smoothing
  void compute_new_positions();
  void project_to_tangent_plane();
  void local_error_check();
  void move_points();
191

Jan Möbius's avatar
Jan Möbius committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217


protected:

  // override these
  virtual void compute_new_positions_C0() = 0;
  virtual void compute_new_positions_C1() = 0;



protected:

  // misc helpers

  const Point& orig_position(VertexHandle _vh) const
  { return mesh_.property(original_positions_, _vh); }

  const NormalType& orig_normal(VertexHandle _vh) const
  { return mesh_.property(original_normals_, _vh); }

  const Point& new_position(VertexHandle _vh) const
  { return mesh_.property(new_positions_, _vh); }

  void set_new_position(VertexHandle _vh, const Point& _p)
  { mesh_.property(new_positions_, _vh) = _p; }

218
  bool is_active(VertexHandle _vh) const
Jan Möbius's avatar
Jan Möbius committed
219 220 221 222 223 224 225 226
  { return mesh_.property(is_active_, _vh); }

  Component  component()  const { return component_;  }
  Continuity continuity() const { return continuity_; }

protected:

  Mesh&  mesh_;
227
  bool   skip_features_;
Jan Möbius's avatar
Jan Möbius committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249


private:

  Scalar      tolerance_;
  Scalar      normal_deviation_;
  Component   component_;
  Continuity  continuity_;

  OpenMesh::VPropHandleT<Point>      original_positions_;
  OpenMesh::VPropHandleT<NormalType> original_normals_;
  OpenMesh::VPropHandleT<Point>      new_positions_;
  OpenMesh::VPropHandleT<bool>       is_active_;
};


//=============================================================================
} // namespace Smoother
} // namespace OpenMesh
//=============================================================================
#if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_SMOOTHERT_C)
#define OPENMESH_SMOOTHERT_TEMPLATES
250
#include "SmootherT_impl.hh"
Jan Möbius's avatar
Jan Möbius committed
251 252 253 254 255
#endif
//=============================================================================
#endif // OPENMESH_SMOOTHER_SMOOTHERT_HH defined
//=============================================================================