Developer Documentation
AdaptiveRemesherT_impl.hh
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 //
45 // CLASS AdaptiveRemesherT - IMPLEMENTATION
46 //
47 //=============================================================================
48 
49 #define ADAPTIVE_REMESHERT_C
50 
51 
52 //== INCLUDES =================================================================
53 
54 #include "AdaptiveRemesherT.hh"
55 #include <ACG/Geometry/Algorithms.hh>
56 #include "DiffGeoT.hh"
57 
58 
59 //== NAMESPACES ===============================================================
60 
61 namespace Remeshing {
62 
63 
64 //== IMPLEMENTATION ==========================================================
65 
66 
67 template <class Mesh>
68 void
69 AdaptiveRemesherT<Mesh>::
70 init_reference()
71 {
72  Base::init_reference();
73 
74  // compute max curvature on both meshes
75  Base::refmesh_->add_property(refcurv_);
76  compute_curvature(*Base::refmesh_, refcurv_);
77  Base::mesh_.add_property(curvature_);
78  compute_curvature(Base::mesh_, curvature_);
79 }
80 
81 
82 //-----------------------------------------------------------------------------
83 
84 
85 template <class Mesh>
86 void
87 AdaptiveRemesherT<Mesh>::
88 project_to_reference(typename Mesh::VertexHandle _vh) const
89 {
90  typename Mesh::Point p = Base::mesh_.point(_vh);
91  typename Mesh::FaceHandle fh = Base::bsp_->nearest(p).handle;
92 
93  if ( ! fh.is_valid() ) {
94  std::cerr << "AdaptiveRemesherT: Projection, invalid face handle" << std::endl;
95  return;
96  }
97 
98  typename Mesh::CFVIter fv_it = Base::refmesh_->cfv_iter(fh);
99 
100 
101  const typename Mesh::Point& p0 = Base::refmesh_->point(*fv_it);
102  typename Mesh::Normal n0 = Base::refmesh_->normal(*fv_it);
103  typename Mesh::Scalar c0 = Base::refmesh_->property(refcurv_, *fv_it);
104  const typename Mesh::Point& p1 = Base::refmesh_->point(*(++fv_it));
105  typename Mesh::Normal n1 = Base::refmesh_->normal(*fv_it);
106  typename Mesh::Scalar c1 = Base::refmesh_->property(refcurv_, *fv_it);
107  const typename Mesh::Point& p2 = Base::refmesh_->point(*(++fv_it));
108  typename Mesh::Normal n2 = Base::refmesh_->normal(*fv_it);
109  typename Mesh::Scalar c2 = Base::refmesh_->property(refcurv_, *fv_it);
110 
111 
112  // project
113  //Geometry::dist_point_triangle(p, p0, p1, p2, p);
114  ACG::Geometry::distPointTriangle<typename Mesh::Point>(p, p0, p1, p2, p);
115  Base::mesh_.set_point(_vh, p);
116 
117 
118  // get barycentric coordinates
119  if (!ACG::Geometry::baryCoord(p, p0, p1, p2, p))
120  p[0] = p[1] = p[2] = 1.0/3.0;
121 
122 
123  // interpolate normal
124  typename Mesh::Normal n;
125  n = (n0 *= p[0]);
126  n += (n1 *= p[1]);
127  n += (n2 *= p[2]);
128  n.normalize();
129  Base::mesh_.set_normal(_vh, n);
130 
131 
132  // interpolate curvature
133  typename Mesh::Scalar c;
134  c = (c0 *= p[0]);
135  c += (c1 *= p[1]);
136  c += (c2 *= p[2]);
137  Base::mesh_.property(curvature_, _vh) = c;
138 }
139 
140 
141 //-----------------------------------------------------------------------------
142 
143 
144 template <class Mesh>
145 void
146 AdaptiveRemesherT<Mesh>::
147 compute_curvature(Mesh& _mesh, OpenMesh::VPropHandleT<Scalar> _ph)
148 {
149  DiffGeoT<Mesh> diffgeo(_mesh);
150  diffgeo.compute(2);
151 
152  typename Mesh::VIter v_it, v_end(_mesh.vertices_end());
153 
154  for (v_it=_mesh.vertices_begin(); v_it!=v_end; ++v_it)
155  _mesh.property(_ph, *v_it) = diffgeo.max_curvature(*v_it);
156 }
157 
158 
159 //-----------------------------------------------------------------------------
160 
161 
162 template <class Mesh>
163 void
164 AdaptiveRemesherT<Mesh>::
165 remesh(Scalar _error,
166  Scalar _emin,
167  Scalar _emax,
168  unsigned int _iters,
169  bool _use_projection,
170  Selection _selection)
171 {
172  // set thesholds
173  error_ = _error;
174  emin_ = _emin;
175  emax_ = _emax;
176 
177  // do it
178  Base::remesh(_iters, 0, _use_projection, _selection);
179 
180  // free curvature property (refmesh has been deleted already)
181  Base::mesh_.remove_property(curvature_);
182 }
183 
184 
185 //-----------------------------------------------------------------------------
186 
187 
188 template <class Mesh>
189 bool
190 AdaptiveRemesherT<Mesh>::
191 is_too_long(VertexHandle v0, VertexHandle v1) const
192 {
193  const Point& p0 = Base::mesh_.point(v0);
194  const Point& p1 = Base::mesh_.point(v1);
195 
196  Scalar c = 0.5 * ( Base::mesh_.property(curvature_, v0) +
197  Base::mesh_.property(curvature_, v1) );
198 
199  Scalar e = 2.0/c*error_ - error_*error_;
200  e = (e <= 0.0 ? error_ : 2.0*sqrt(e));
201 
202  if (e<emin_) e=emin_; else if (e>emax_) e=emax_;
203  Scalar emax = 4.0/3.0 * e;
204 
205  return ((p0 - p1).sqrnorm() > emax*emax);
206 }
207 
208 
209 //-----------------------------------------------------------------------------
210 
211 
212 template <class Mesh>
213 bool
214 AdaptiveRemesherT<Mesh>::
215 is_too_short(VertexHandle v0, VertexHandle v1) const
216 {
217  const Point& p0 = Base::mesh_.point(v0);
218  const Point& p1 = Base::mesh_.point(v1);
219 
220  Scalar c = 0.5 * ( Base::mesh_.property(curvature_, v0) +
221  Base::mesh_.property(curvature_, v1) );
222 
223  Scalar e = 2.0/c*error_ - error_*error_;
224  e = (e <= 0.0 ? error_ : 2.0*sqrt(e));
225 
226  if (e<emin_) e=emin_; else if (e>emax_) e=emax_;
227  Scalar emin = 4.0/5.0 * e;
228 
229  return ((p0 - p1).sqrnorm() < emin*emin);
230 }
231 
232 
233 //=============================================================================
234 } // namespace Remeshing
235 //=============================================================================
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
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:83
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:110
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136