OpenMesh
PolyMeshT_impl.hh
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
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 //=============================================================================
46 //
47 // CLASS PolyMeshT - IMPLEMENTATION
48 //
49 //=============================================================================
50 
51 
52 #define OPENMESH_POLYMESH_C
53 
54 
55 //== INCLUDES =================================================================
56 
57 #include <OpenMesh/Core/Mesh/PolyMeshT.hh>
58 #include <OpenMesh/Core/Geometry/LoopSchemeMaskT.hh>
59 #include <OpenMesh/Core/Utils/GenProg.hh>
60 #include <OpenMesh/Core/Utils/vector_cast.hh>
61 #include <OpenMesh/Core/Utils/vector_traits.hh>
63 #include <vector>
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 
69 namespace OpenMesh {
70 
71 //== IMPLEMENTATION ==========================================================
72 
73 template <class Kernel>
75 {
76  assert(Kernel::has_edge_status());//this function needs edge status property
77  uint n_feature_edges = 0;
78  for (EdgeIter e_it = Kernel::edges_begin(); e_it != Kernel::edges_end(); ++e_it)
79  {
80  if (fabs(calc_dihedral_angle(*e_it)) > _angle_tresh)
81  {//note: could be optimized by comparing cos(dih_angle) vs. cos(_angle_tresh)
82  this->status(*e_it).set_feature(true);
83  n_feature_edges++;
84  }
85  else
86  {
87  this->status(*e_it).set_feature(false);
88  }
89  }
90  return n_feature_edges;
91 }
92 
93 //-----------------------------------------------------------------------------
94 
95 template <class Kernel>
98 {
99  return calc_face_normal_impl(_fh, typename GenProg::IF<
101  PointIs3DTag,
102  PointIsNot3DTag
103  >::Result());
104 }
105 
106 template <class Kernel>
108 PolyMeshT<Kernel>::calc_face_normal_impl(FaceHandle _fh, PointIs3DTag) const
109 {
110  assert(this->halfedge_handle(_fh).is_valid());
111  ConstFaceVertexIter fv_it(this->cfv_iter(_fh));
112 
113  // Safeguard for 1-gons
114  if (!(++fv_it).is_valid()) return Normal(0, 0, 0);
115 
116  // Safeguard for 2-gons
117  if (!(++fv_it).is_valid()) return Normal(0, 0, 0);
118 
119  // use Newell's Method to compute the surface normal
120  Normal n(0,0,0);
121  for(fv_it = this->cfv_iter(_fh); fv_it.is_valid(); ++fv_it)
122  {
123  // next vertex
124  ConstFaceVertexIter fv_itn = fv_it;
125  ++fv_itn;
126 
127  if (!fv_itn.is_valid())
128  fv_itn = this->cfv_iter(_fh);
129 
130  // http://www.opengl.org/wiki/Calculating_a_Surface_Normal
131  const Point a = this->point(*fv_it) - this->point(*fv_itn);
132  const Point b = this->point(*fv_it) + this->point(*fv_itn);
133 
134 
135  // Due to traits, the value types of normals and points can be different.
136  // Therefore we cast them here.
137  n[0] += static_cast<typename vector_traits<Normal>::value_type>(a[1] * b[2]);
138  n[1] += static_cast<typename vector_traits<Normal>::value_type>(a[2] * b[0]);
139  n[2] += static_cast<typename vector_traits<Normal>::value_type>(a[0] * b[1]);
140  }
141 
142  const typename vector_traits<Normal>::value_type length = norm(n);
143 
144  // The expression ((n *= (1.0/norm)),n) is used because the OpenSG
145  // vector class does not return self after component-wise
146  // self-multiplication with a scalar!!!
147  return (length != typename vector_traits<Normal>::value_type(0))
148  ? ((n *= (typename vector_traits<Normal>::value_type(1)/length)), n)
149  : Normal(0, 0, 0);
150 }
151 
152 template <class Kernel>
154 PolyMeshT<Kernel>::calc_face_normal_impl(FaceHandle, PointIsNot3DTag) const
155 {
156  // Dummy fallback implementation
157  // Returns just an initialized all 0 normal
158  // This function is only used if we don't hate a matching implementation
159  // for normal computation with the current vector type defined in the mesh traits
160 
161  assert(false);
162 
163  Normal normal;
164  vectorize(normal,0);
165  return normal;
166 }
167 
168 //-----------------------------------------------------------------------------
169 
170 template <class Kernel>
174  const Point& _p1,
175  const Point& _p2) const
176 {
177  return calc_face_normal_impl(_p0, _p1, _p2, typename GenProg::IF<
179  PointIs3DTag,
180  PointIsNot3DTag
181  >::Result());
182 }
183 
184 template <class Kernel>
187 calc_face_normal_impl(const Point& _p0,
188  const Point& _p1,
189  const Point& _p2,
190  PointIs3DTag) const
191 {
192 #if 1
193  // The OpenSG <Vector>::operator -= () does not support the type Point
194  // as rhs. Therefore use vector_cast at this point!!!
195  // Note! OpenSG distinguishes between Normal and Point!!!
196  Normal p1p0(vector_cast<Normal>(_p0)); p1p0 -= vector_cast<Normal>(_p1);
197  Normal p1p2(vector_cast<Normal>(_p2)); p1p2 -= vector_cast<Normal>(_p1);
198 
199  Normal n = cross(p1p2, p1p0);
200  typename vector_traits<Normal>::value_type length = norm(n);
201 
202  // The expression ((n *= (1.0/norm)),n) is used because the OpenSG
203  // vector class does not return self after component-wise
204  // self-multiplication with a scalar!!!
205  return (length != typename vector_traits<Normal>::value_type(0))
206  ? ((n *= (typename vector_traits<Normal>::value_type(1)/length)),n)
207  : Normal(0,0,0);
208 #else
209  Point p1p0 = _p0; p1p0 -= _p1;
210  Point p1p2 = _p2; p1p2 -= _p1;
211 
212  Normal n = vector_cast<Normal>(cross(p1p2, p1p0));
213  typename vector_traits<Normal>::value_type length = norm(n);
214 
215  return (length != 0.0) ? n *= (1.0/length) : Normal(0,0,0);
216 #endif
217 }
218 
219 template <class Kernel>
221 PolyMeshT<Kernel>::calc_face_normal_impl(const Point&, const Point&, const Point&, PointIsNot3DTag) const
222 {
223 
224  // Dummy fallback implementation
225  // Returns just an initialized all 0 normal
226  // This function is only used if we don't hate a matching implementation
227  // for normal computation with the current vector type defined in the mesh traits
228 
229  assert(false);
230 
231  Normal normal;
232  vectorize(normal,0);
233  return normal;
234 }
235 
236 //-----------------------------------------------------------------------------
237 
238 template <class Kernel>
241 calc_face_centroid(FaceHandle _fh) const
242 {
243  Point _pt;
244  vectorize(_pt, 0);
245  Scalar valence = 0.0;
246  for (ConstFaceVertexIter cfv_it = this->cfv_iter(_fh); cfv_it.is_valid(); ++cfv_it, valence += 1.0)
247  {
248  _pt += this->point(*cfv_it);
249  }
250  _pt /= valence;
251  return _pt;
252 }
253 //-----------------------------------------------------------------------------
254 
255 
256 template <class Kernel>
257 void
260 {
261  // Face normals are required to compute the vertex and the halfedge normals
262  if (Kernel::has_face_normals() ) {
263  update_face_normals();
264 
265  if (Kernel::has_vertex_normals() ) update_vertex_normals();
266  if (Kernel::has_halfedge_normals()) update_halfedge_normals();
267  }
268 }
269 
270 
271 //-----------------------------------------------------------------------------
272 
273 
274 template <class Kernel>
275 void
278 {
279  FaceIter f_it(Kernel::faces_sbegin()), f_end(Kernel::faces_end());
280 
281  for (; f_it != f_end; ++f_it)
282  this->set_normal(*f_it, calc_face_normal(*f_it));
283 }
284 
285 
286 //-----------------------------------------------------------------------------
287 
288 
289 template <class Kernel>
290 void
292 update_halfedge_normals(const double _feature_angle)
293 {
294  HalfedgeIter h_it(Kernel::halfedges_begin()), h_end(Kernel::halfedges_end());
295 
296  for (; h_it != h_end; ++h_it)
297  this->set_normal(*h_it, calc_halfedge_normal(*h_it, _feature_angle));
298 }
299 
300 
301 //-----------------------------------------------------------------------------
302 
303 
304 template <class Kernel>
307 calc_halfedge_normal(HalfedgeHandle _heh, const double _feature_angle) const
308 {
309  if(Kernel::is_boundary(_heh))
310  return Normal(0,0,0);
311  else
312  {
313  std::vector<FaceHandle> fhs; fhs.reserve(10);
314 
315  HalfedgeHandle heh = _heh;
316 
317  // collect CW face-handles
318  do
319  {
320  fhs.push_back(Kernel::face_handle(heh));
321 
322  heh = Kernel::next_halfedge_handle(heh);
323  heh = Kernel::opposite_halfedge_handle(heh);
324  }
325  while(heh != _heh && !Kernel::is_boundary(heh) && !is_estimated_feature_edge(heh, _feature_angle));
326 
327  // collect CCW face-handles
328  if(heh != _heh && !is_estimated_feature_edge(_heh, _feature_angle))
329  {
330  heh = Kernel::opposite_halfedge_handle(_heh);
331 
332  if ( !Kernel::is_boundary(heh) ) {
333  do
334  {
335 
336  fhs.push_back(Kernel::face_handle(heh));
337 
338  heh = Kernel::prev_halfedge_handle(heh);
339  heh = Kernel::opposite_halfedge_handle(heh);
340  }
341  while(!Kernel::is_boundary(heh) && !is_estimated_feature_edge(heh, _feature_angle));
342  }
343  }
344 
345  Normal n(0,0,0);
346  for(unsigned int i=0; i<fhs.size(); ++i)
347  n += Kernel::normal(fhs[i]);
348 
349  return normalize(n);
350  }
351 }
352 
353 
354 //-----------------------------------------------------------------------------
355 
356 
357 template <class Kernel>
358 bool
360 is_estimated_feature_edge(HalfedgeHandle _heh, const double _feature_angle) const
361 {
362  EdgeHandle eh = Kernel::edge_handle(_heh);
363 
364  if(Kernel::has_edge_status())
365  {
366  if(Kernel::status(eh).feature())
367  return true;
368  }
369 
370  if(Kernel::is_boundary(eh))
371  return false;
372 
373  // compute angle between faces
374  FaceHandle fh0 = Kernel::face_handle(_heh);
375  FaceHandle fh1 = Kernel::face_handle(Kernel::opposite_halfedge_handle(_heh));
376 
377  Normal fn0 = Kernel::normal(fh0);
378  Normal fn1 = Kernel::normal(fh1);
379 
380  // dihedral angle above angle threshold
381  return ( dot(fn0,fn1) < cos(_feature_angle) );
382 }
383 
384 
385 //-----------------------------------------------------------------------------
386 
387 
388 template <class Kernel>
392 {
393  Normal n;
394  calc_vertex_normal_fast(_vh,n);
395 
396  Scalar length = norm(n);
397  if (length != 0.0) n *= (Scalar(1.0)/length);
398 
399  return n;
400 }
401 
402 //-----------------------------------------------------------------------------
403 template <class Kernel>
406 {
407  vectorize(_n, 0.0);
408  for (ConstVertexFaceIter vf_it = this->cvf_iter(_vh); vf_it.is_valid(); ++vf_it)
409  _n += this->normal(*vf_it);
410 }
411 
412 //-----------------------------------------------------------------------------
413 template <class Kernel>
416 {
417  vectorize(_n, 0.0);
418  ConstVertexIHalfedgeIter cvih_it = this->cvih_iter(_vh);
419  if (! cvih_it.is_valid() )
420  {//don't crash on isolated vertices
421  return;
422  }
423  Normal in_he_vec;
424  calc_edge_vector(*cvih_it, in_he_vec);
425  for ( ; cvih_it.is_valid(); ++cvih_it)
426  {//calculates the sector normal defined by cvih_it and adds it to _n
427  if (this->is_boundary(*cvih_it))
428  {
429  continue;
430  }
431  HalfedgeHandle out_heh(this->next_halfedge_handle(*cvih_it));
432  Normal out_he_vec;
433  calc_edge_vector(out_heh, out_he_vec);
434  _n += cross(in_he_vec, out_he_vec);//sector area is taken into account
435  in_he_vec = out_he_vec;
436  in_he_vec *= -1;//change the orientation
437  }
438 }
439 
440 //-----------------------------------------------------------------------------
441 template <class Kernel>
444 {
445  static const LoopSchemeMaskDouble& loop_scheme_mask__ =
447 
448  Normal t_v(0.0,0.0,0.0), t_w(0.0,0.0,0.0);
449  unsigned int vh_val = this->valence(_vh);
450  unsigned int i = 0;
451  for (ConstVertexOHalfedgeIter cvoh_it = this->cvoh_iter(_vh); cvoh_it.is_valid(); ++cvoh_it, ++i)
452  {
453  VertexHandle r1_v( this->to_vertex_handle(*cvoh_it) );
454  t_v += (typename vector_traits<Point>::value_type)(loop_scheme_mask__.tang0_weight(vh_val, i))*this->point(r1_v);
455  t_w += (typename vector_traits<Point>::value_type)(loop_scheme_mask__.tang1_weight(vh_val, i))*this->point(r1_v);
456  }
457  _n = cross(t_w, t_v);//hack: should be cross(t_v, t_w), but then the normals are reversed?
458 }
459 
460 //-----------------------------------------------------------------------------
461 
462 
463 template <class Kernel>
464 void
467 {
468  VertexIter v_it(Kernel::vertices_begin()), v_end(Kernel::vertices_end());
469 
470  for (; v_it!=v_end; ++v_it)
471  this->set_normal(*v_it, calc_vertex_normal(*v_it));
472 }
473 
474 //=============================================================================
475 } // namespace OpenMesh
476 //=============================================================================
void update_halfedge_normals(const double _feature_angle=0.8)
Update normal vectors for all halfedges.
Definition: PolyMeshT_impl.hh:292
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:110
Base type for a polygonal mesh.
Definition: PolyMeshT.hh:90
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
void calc_vertex_normal_loop(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:443
virtual Normal calc_halfedge_normal(HalfedgeHandle _heh, const double _feature_angle=0.8) const
Calculate halfedge normal for one specific halfedge.
Definition: PolyMeshT_impl.hh:307
void calc_face_centroid(FaceHandle _fh, Point &_pt) const
calculates the average of the vertices defining _fh
Definition: PolyMeshT.hh:275
virtual Normal calc_face_normal(FaceHandle _fh) const
Calculate normal vector for face _fh.
Definition: PolyMeshT_impl.hh:97
void update_vertex_normals()
Update normal vectors for all vertices.
Definition: PolyMeshT_impl.hh:466
void update_normals()
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:259
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:82
Kernel::ConstVertexFaceIter ConstVertexFaceIter
Circulator.
Definition: PolyMeshT.hh:176
Kernel::ConstVertexIHalfedgeIter ConstVertexIHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:174
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
void update_face_normals()
Update normal vectors for all faces.
Definition: PolyMeshT_impl.hh:277
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Definition: VectorAdapter.hh:176
void calc_vertex_normal_correct(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:415
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
Normal calc_vertex_normal(VertexHandle _vh) const
Calculate vertex normal for one specific vertex.
Definition: PolyMeshT_impl.hh:391
implements cache for the weights of the original Loop scheme supported:
Definition: LoopSchemeMaskT.hh:66
Helper class providing information about a vector type.
Definition: vector_traits.hh:88
osg::Vec3f cross(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Definition: VectorAdapter.hh:191
void calc_vertex_normal_fast(VertexHandle _vh, Normal &_n) const
Different methods for calculation of the normal at _vh:
Definition: PolyMeshT_impl.hh:405
static T & Instance()
Singleton access function.
Definition: SingletonT.hh:89
Kernel::ConstVertexOHalfedgeIter ConstVertexOHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:173
unsigned int find_feature_edges(Scalar _angle_tresh=OpenMesh::deg_to_rad(44.0))
tags an edge as a feature if its dihedral angle is larger than _angle_tresh returns the number of the...
Definition: PolyMeshT_impl.hh:74
void vector_cast(const src_t &_src, dst_t &_dst, GenProg::Int2Type< n >)
Cast vector type to another vector type by copying the vector elements.
Definition: vector_cast.hh:81
T::value_type value_type
Type of the scalar value.
Definition: vector_traits.hh:94
This file provides the streams omlog, omout, and omerr.
bool is_estimated_feature_edge(HalfedgeHandle _heh, const double _feature_angle) const
identifies feature edges w.r.t.
Definition: PolyMeshT_impl.hh:360
Kernel::ConstFaceVertexIter ConstFaceVertexIter
Circulator.
Definition: PolyMeshT.hh:177

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .