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 have 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,Scalar(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_normal(FaceHandle _fh) const
188 {
189  return calc_face_normal(_fh);
190 }
191 
192 template <class Kernel>
195 calc_face_normal_impl(const Point& _p0,
196  const Point& _p1,
197  const Point& _p2,
198  PointIs3DTag) const
199 {
200 #if 1
201  // The OpenSG <Vector>::operator -= () does not support the type Point
202  // as rhs. Therefore use vector_cast at this point!!!
203  // Note! OpenSG distinguishes between Normal and Point!!!
204  Normal p1p0(vector_cast<Normal>(_p0)); p1p0 -= vector_cast<Normal>(_p1);
205  Normal p1p2(vector_cast<Normal>(_p2)); p1p2 -= vector_cast<Normal>(_p1);
206 
207  Normal n = cross(p1p2, p1p0);
208  typename vector_traits<Normal>::value_type length = norm(n);
209 
210  // The expression ((n *= (1.0/norm)),n) is used because the OpenSG
211  // vector class does not return self after component-wise
212  // self-multiplication with a scalar!!!
213  return (length != typename vector_traits<Normal>::value_type(0))
214  ? ((n *= (typename vector_traits<Normal>::value_type(1)/length)),n)
215  : Normal(0,0,0);
216 #else
217  Point p1p0 = _p0; p1p0 -= _p1;
218  Point p1p2 = _p2; p1p2 -= _p1;
219 
220  Normal n = vector_cast<Normal>(cross(p1p2, p1p0));
221  typename vector_traits<Normal>::value_type length = norm(n);
222 
223  return (length != 0.0) ? n *= (1.0/length) : Normal(0,0,0);
224 #endif
225 }
226 
227 template <class Kernel>
229 PolyMeshT<Kernel>::calc_face_normal_impl(const Point&, const Point&, const Point&, PointIsNot3DTag) const
230 {
231 
232  // Dummy fallback implementation
233  // Returns just an initialized all 0 normal
234  // This function is only used if we don't have a matching implementation
235  // for normal computation with the current vector type defined in the mesh traits
236 
237  assert(false);
238 
239  Normal normal;
240  vectorize(normal,Scalar(0));
241  return normal;
242 }
243 
244 //-----------------------------------------------------------------------------
245 
246 template <class Kernel>
249 calc_face_centroid(FaceHandle _fh) const
250 {
251  Point _pt;
252  vectorize(_pt, Scalar(0));
253  Scalar valence = 0.0;
254  for (ConstFaceVertexIter cfv_it = this->cfv_iter(_fh); cfv_it.is_valid(); ++cfv_it, valence += 1.0)
255  {
256  _pt += this->point(*cfv_it);
257  }
258  _pt /= valence;
259  return _pt;
260 }
261 
262 //-----------------------------------------------------------------------------
263 
264 template<class Kernel>
267 calc_centroid(FaceHandle _fh) const
268 {
269  return calc_face_centroid(_fh);
270 }
271 
272 //-----------------------------------------------------------------------------
273 
274 template<class Kernel>
277 calc_centroid(EdgeHandle _eh) const
278 {
279  return this->calc_edge_midpoint(_eh);
280 }
281 
282 //-----------------------------------------------------------------------------
283 
284 template<class Kernel>
287 calc_centroid(HalfedgeHandle _heh) const
288 {
289  return this->calc_edge_midpoint(this->edge_handle(_heh));
290 }
291 
292 //-----------------------------------------------------------------------------
293 
294 template<class Kernel>
298 {
299  return this->point(_vh);
300 }
301 
302 //-----------------------------------------------------------------------------
303 
304 template<class Kernel>
307 calc_centroid(MeshHandle /*_mh*/) const
308 {
309  return this->vertices().avg(getPointsProperty(*this));
310 }
311 
312 //-----------------------------------------------------------------------------
313 
314 template <class Kernel>
315 void
318 {
319  // Face normals are required to compute the vertex and the halfedge normals
320  if (Kernel::has_face_normals() ) {
321  update_face_normals();
322 
323  if (Kernel::has_vertex_normals() ) update_vertex_normals();
324  if (Kernel::has_halfedge_normals()) update_halfedge_normals();
325  }
326 }
327 
328 
329 //-----------------------------------------------------------------------------
330 
331 
332 template <class Kernel>
333 void
336 {
337  FaceIter f_it(Kernel::faces_sbegin()), f_end(Kernel::faces_end());
338 
339  for (; f_it != f_end; ++f_it)
340  this->set_normal(*f_it, calc_face_normal(*f_it));
341 }
342 
343 
344 //-----------------------------------------------------------------------------
345 
346 
347 template <class Kernel>
348 void
350 update_halfedge_normals(const double _feature_angle)
351 {
352  HalfedgeIter h_it(Kernel::halfedges_begin()), h_end(Kernel::halfedges_end());
353 
354  for (; h_it != h_end; ++h_it)
355  this->set_normal(*h_it, calc_halfedge_normal(*h_it, _feature_angle));
356 }
357 
358 
359 //-----------------------------------------------------------------------------
360 
361 
362 template <class Kernel>
365 calc_halfedge_normal(HalfedgeHandle _heh, const double _feature_angle) const
366 {
367  if(Kernel::is_boundary(_heh))
368  return Normal(0,0,0);
369  else
370  {
371  std::vector<FaceHandle> fhs; fhs.reserve(10);
372 
373  HalfedgeHandle heh = _heh;
374 
375  // collect CW face-handles
376  do
377  {
378  fhs.push_back(Kernel::face_handle(heh));
379 
380  heh = Kernel::next_halfedge_handle(heh);
381  heh = Kernel::opposite_halfedge_handle(heh);
382  }
383  while(heh != _heh && !Kernel::is_boundary(heh) && !is_estimated_feature_edge(heh, _feature_angle));
384 
385  // collect CCW face-handles
386  if(heh != _heh && !is_estimated_feature_edge(_heh, _feature_angle))
387  {
388  heh = Kernel::opposite_halfedge_handle(_heh);
389 
390  if ( !Kernel::is_boundary(heh) ) {
391  do
392  {
393 
394  fhs.push_back(Kernel::face_handle(heh));
395 
396  heh = Kernel::prev_halfedge_handle(heh);
397  heh = Kernel::opposite_halfedge_handle(heh);
398  }
399  while(!Kernel::is_boundary(heh) && !is_estimated_feature_edge(heh, _feature_angle));
400  }
401  }
402 
403  Normal n(0,0,0);
404  for(unsigned int i=0; i<fhs.size(); ++i)
405  n += Kernel::normal(fhs[i]);
406 
407  return normalize(n);
408  }
409 }
410 
411 
412 //-----------------------------------------------------------------------------
413 
414 
415 template <class Kernel>
418 calc_normal(HalfedgeHandle _heh, const double _feature_angle) const
419 {
420  return calc_halfedge_normal(_heh, _feature_angle);
421 }
422 
423 
424 //-----------------------------------------------------------------------------
425 
426 
427 template <class Kernel>
428 bool
430 is_estimated_feature_edge(HalfedgeHandle _heh, const double _feature_angle) const
431 {
432  EdgeHandle eh = Kernel::edge_handle(_heh);
433 
434  if(Kernel::has_edge_status())
435  {
436  if(Kernel::status(eh).feature())
437  return true;
438  }
439 
440  if(Kernel::is_boundary(eh))
441  return false;
442 
443  // compute angle between faces
444  FaceHandle fh0 = Kernel::face_handle(_heh);
445  FaceHandle fh1 = Kernel::face_handle(Kernel::opposite_halfedge_handle(_heh));
446 
447  Normal fn0 = Kernel::normal(fh0);
448  Normal fn1 = Kernel::normal(fh1);
449 
450  // dihedral angle above angle threshold
451  return ( dot(fn0,fn1) < cos(_feature_angle) );
452 }
453 
454 
455 //-----------------------------------------------------------------------------
456 
457 
458 template <class Kernel>
462 {
463  Normal n;
464  calc_vertex_normal_fast(_vh,n);
465 
466  Scalar length = norm(n);
467  if (length != 0.0) n *= (Scalar(1.0)/length);
468 
469  return n;
470 }
471 
472 //-----------------------------------------------------------------------------
473 template <class Kernel>
476 {
477  vectorize(_n, Scalar(0));
478  for (ConstVertexFaceIter vf_it = this->cvf_iter(_vh); vf_it.is_valid(); ++vf_it)
479  _n += this->normal(*vf_it);
480 }
481 
482 //-----------------------------------------------------------------------------
483 template <class Kernel>
486 {
487  vectorize(_n, Scalar(0));
488  ConstVertexIHalfedgeIter cvih_it = this->cvih_iter(_vh);
489  if (! cvih_it.is_valid() )
490  {//don't crash on isolated vertices
491  return;
492  }
493  Normal in_he_vec;
494  calc_edge_vector(*cvih_it, in_he_vec);
495  for ( ; cvih_it.is_valid(); ++cvih_it)
496  {//calculates the sector normal defined by cvih_it and adds it to _n
497  if (this->is_boundary(*cvih_it))
498  {
499  continue;
500  }
501  HalfedgeHandle out_heh(this->next_halfedge_handle(*cvih_it));
502  Normal out_he_vec;
503  calc_edge_vector(out_heh, out_he_vec);
504  _n += cross(in_he_vec, out_he_vec);//sector area is taken into account
505  in_he_vec = out_he_vec;
506  in_he_vec *= -1;//change the orientation
507  }
508  Scalar length = norm(_n);
509  if (length != 0.0)
510  _n *= (Scalar(1.0)/length);
511 }
512 
513 //-----------------------------------------------------------------------------
514 template <class Kernel>
517 {
518  static const LoopSchemeMaskDouble& loop_scheme_mask__ =
520 
521  Normal t_v(0.0,0.0,0.0), t_w(0.0,0.0,0.0);
522  unsigned int vh_val = this->valence(_vh);
523  unsigned int i = 0;
524  for (ConstVertexOHalfedgeIter cvoh_it = this->cvoh_iter(_vh); cvoh_it.is_valid(); ++cvoh_it, ++i)
525  {
526  VertexHandle r1_v( this->to_vertex_handle(*cvoh_it) );
527  t_v += (typename vector_traits<Point>::value_type)(loop_scheme_mask__.tang0_weight(vh_val, i))*this->point(r1_v);
528  t_w += (typename vector_traits<Point>::value_type)(loop_scheme_mask__.tang1_weight(vh_val, i))*this->point(r1_v);
529  }
530  _n = cross(t_w, t_v);//hack: should be cross(t_v, t_w), but then the normals are reversed?
531 }
532 
533 //-----------------------------------------------------------------------------
534 
535 template<class Kernel>
539 {
540  Normal n;
541  calc_vertex_normal_correct(_vh, n);
542  return n;
543 }
544 
545 //-----------------------------------------------------------------------------
546 
547 template <class Kernel>
548 void
551 {
552  VertexIter v_it(Kernel::vertices_begin()), v_end(Kernel::vertices_end());
553 
554  for (; v_it!=v_end; ++v_it)
555  this->set_normal(*v_it, calc_vertex_normal(*v_it));
556 }
557 
558 //=============================================================================
559 } // namespace OpenMesh
560 //=============================================================================
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
virtual Normal calc_face_normal(FaceHandle _fh) const
Calculate normal vector for face _fh.
Definition: PolyMeshT_impl.hh:97
bool is_estimated_feature_edge(HalfedgeHandle _heh, const double _feature_angle) const
identifies feature edges w.r.t.
Definition: PolyMeshT_impl.hh:430
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:110
void update_vertex_normals()
Update normal vectors for all vertices.
Definition: PolyMeshT_impl.hh:550
void update_halfedge_normals(const double _feature_angle=0.8)
Update normal vectors for all halfedges.
Definition: PolyMeshT_impl.hh:350
implements cache for the weights of the original Loop scheme supported:
Definition: LoopSchemeMaskT.hh:66
Point calc_centroid(FaceHandle _fh) const
Computes and returns the average of the vertices defining _fh (same as calc_face_centroid) ...
Definition: PolyMeshT_impl.hh:267
void calc_vertex_normal_loop(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:516
Kernel::ConstFaceVertexIter ConstFaceVertexIter
Circulator.
Definition: PolyMeshT.hh:177
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:365
Helper class providing information about a vector type.
Definition: vector_traits.hh:88
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
Kernel::ConstVertexOHalfedgeIter ConstVertexOHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:173
Normal calc_normal(FaceHandle _fh) const
same as calc_face_normal
Definition: PolyMeshT_impl.hh:187
void calc_vertex_normal_correct(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:485
void update_normals()
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:317
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
Kernel::ConstVertexFaceIter ConstVertexFaceIter
Circulator.
Definition: PolyMeshT.hh:176
Normal calc_vertex_normal(VertexHandle _vh) const
Calculate vertex normal for one specific vertex.
Definition: PolyMeshT_impl.hh:461
void calc_face_centroid(FaceHandle _fh, Point &_pt) const
calculates the average of the vertices defining _fh
Definition: PolyMeshT.hh:279
T::value_type value_type
Type of the scalar value.
Definition: vector_traits.hh:94
This file provides the streams omlog, omout, and omerr.
void update_face_normals()
Update normal vectors for all faces.
Definition: PolyMeshT_impl.hh:335
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
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:475
Handle type for meshes to simplify some template programming.
Definition: Handles.hh:148
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
Kernel::ConstVertexIHalfedgeIter ConstVertexIHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:174
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:82
Base type for a polygonal mesh.
Definition: PolyMeshT.hh:90
static T & Instance()
Singleton access function.
Definition: SingletonT.hh:86

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