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