Developer Documentation
AdaptiveRemesherT.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 //=============================================================================
51 //
52 // CLASS AdaptiveRemesherT - IMPLEMENTATION
53 //
54 //=============================================================================
55 
56 #define ADAPTIVE_REMESHERT_C
57 
58 
59 //== INCLUDES =================================================================
60 
61 #include "AdaptiveRemesherT.hh"
62 #include <ACG/Geometry/Algorithms.hh>
63 #include "DiffGeoT.hh"
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 namespace Remeshing {
69 
70 
71 //== IMPLEMENTATION ==========================================================
72 
73 
74 template <class Mesh>
75 void
78 {
79  Base::init_reference();
80 
81  // compute max curvature on both meshes
82  Base::refmesh_->add_property(refcurv_);
83  compute_curvature(*Base::refmesh_, refcurv_);
84  Base::mesh_.add_property(curvature_);
85  compute_curvature(Base::mesh_, curvature_);
86 }
87 
88 
89 //-----------------------------------------------------------------------------
90 
91 
92 template <class Mesh>
93 void
95 project_to_reference(typename Mesh::VertexHandle _vh) const
96 {
97  typename Mesh::Point p = Base::mesh_.point(_vh);
98  typename Mesh::FaceHandle fh = Base::bsp_->nearest(p).handle;
99 
100  if ( ! fh.is_valid() ) {
101  std::cerr << "AdaptiveRemesherT: Projection, invalid face handle" << std::endl;
102  return;
103  }
104 
105  typename Mesh::CFVIter fv_it = Base::refmesh_->cfv_iter(fh);
106 
107 
108  const typename Mesh::Point& p0 = Base::refmesh_->point(*fv_it);
109  typename Mesh::Normal n0 = Base::refmesh_->normal(*fv_it);
110  typename Mesh::Scalar c0 = Base::refmesh_->property(refcurv_, *fv_it);
111  const typename Mesh::Point& p1 = Base::refmesh_->point(*(++fv_it));
112  typename Mesh::Normal n1 = Base::refmesh_->normal(*fv_it);
113  typename Mesh::Scalar c1 = Base::refmesh_->property(refcurv_, *fv_it);
114  const typename Mesh::Point& p2 = Base::refmesh_->point(*(++fv_it));
115  typename Mesh::Normal n2 = Base::refmesh_->normal(*fv_it);
116  typename Mesh::Scalar c2 = Base::refmesh_->property(refcurv_, *fv_it);
117 
118 
119  // project
120  //Geometry::dist_point_triangle(p, p0, p1, p2, p);
121  ACG::Geometry::distPointTriangle<typename Mesh::Point>(p, p0, p1, p2, p);
122  Base::mesh_.set_point(_vh, p);
123 
124 
125  // get barycentric coordinates
126  if (!ACG::Geometry::baryCoord(p, p0, p1, p2, p))
127  p[0] = p[1] = p[2] = 1.0/3.0;
128 
129 
130  // interpolate normal
131  typename Mesh::Normal n;
132  n = (n0 *= p[0]);
133  n += (n1 *= p[1]);
134  n += (n2 *= p[2]);
135  n.normalize();
136  Base::mesh_.set_normal(_vh, n);
137 
138 
139  // interpolate curvature
140  typename Mesh::Scalar c;
141  c = (c0 *= p[0]);
142  c += (c1 *= p[1]);
143  c += (c2 *= p[2]);
144  Base::mesh_.property(curvature_, _vh) = c;
145 }
146 
147 
148 //-----------------------------------------------------------------------------
149 
150 
151 template <class Mesh>
152 void
155 {
156  DiffGeoT<Mesh> diffgeo(_mesh);
157  diffgeo.compute(2);
158 
159  typename Mesh::VIter v_it, v_end(_mesh.vertices_end());
160 
161  for (v_it=_mesh.vertices_begin(); v_it!=v_end; ++v_it)
162  _mesh.property(_ph, *v_it) = diffgeo.max_curvature(*v_it);
163 }
164 
165 
166 //-----------------------------------------------------------------------------
167 
168 
169 template <class Mesh>
170 void
172 remesh(Scalar _error,
173  Scalar _emin,
174  Scalar _emax,
175  unsigned int _iters,
176  bool _use_projection,
177  Selection _selection)
178 {
179  // set thesholds
180  error_ = _error;
181  emin_ = _emin;
182  emax_ = _emax;
183 
184  // do it
185  Base::remesh(_iters, 0, _use_projection, _selection);
186 
187  // free curvature property (refmesh has been deleted already)
188  Base::mesh_.remove_property(curvature_);
189 }
190 
191 
192 //-----------------------------------------------------------------------------
193 
194 
195 template <class Mesh>
196 bool
198 is_too_long(VertexHandle v0, VertexHandle v1) const
199 {
200  const Point& p0 = Base::mesh_.point(v0);
201  const Point& p1 = Base::mesh_.point(v1);
202 
203  Scalar c = 0.5 * ( Base::mesh_.property(curvature_, v0) +
204  Base::mesh_.property(curvature_, v1) );
205 
206  Scalar e = 2.0/c*error_ - error_*error_;
207  e = (e <= 0.0 ? error_ : 2.0*sqrt(e));
208 
209  if (e<emin_) e=emin_; else if (e>emax_) e=emax_;
210  Scalar emax = 4.0/3.0 * e;
211 
212  return ((p0 - p1).sqrnorm() > emax*emax);
213 }
214 
215 
216 //-----------------------------------------------------------------------------
217 
218 
219 template <class Mesh>
220 bool
222 is_too_short(VertexHandle v0, VertexHandle v1) const
223 {
224  const Point& p0 = Base::mesh_.point(v0);
225  const Point& p1 = Base::mesh_.point(v1);
226 
227  Scalar c = 0.5 * ( Base::mesh_.property(curvature_, v0) +
228  Base::mesh_.property(curvature_, v1) );
229 
230  Scalar e = 2.0/c*error_ - error_*error_;
231  e = (e <= 0.0 ? error_ : 2.0*sqrt(e));
232 
233  if (e<emin_) e=emin_; else if (e>emax_) e=emax_;
234  Scalar emin = 4.0/5.0 * e;
235 
236  return ((p0 - p1).sqrnorm() < emin*emin);
237 }
238 
239 
240 //=============================================================================
241 } // namespace Remeshing
242 //=============================================================================
bool baryCoord(const VectorT< Scalar, 3 > &_p, const VectorT< Scalar, 3 > &_u, const VectorT< Scalar, 3 > &_v, const VectorT< Scalar, 3 > &_w, VectorT< Scalar, 3 > &_result)
Definition: Algorithms.cc:93
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:117
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:139
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:115
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:113