Developer Documentation

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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>
67 #include <OpenMesh/Core/System/omstream.hh>
68 #include <vector>
69
70
71 //== NAMESPACES ===============================================================
72
73
74 namespace OpenMesh {
75
76 //== IMPLEMENTATION ==========================================================
77
78 template <class Kernel>
79 uint PolyMeshT<Kernel>::find_feature_edges(Scalar _angle_tresh)
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>
101 typename PolyMeshT<Kernel>::Normal
102 PolyMeshT<Kernel>::calc_face_normal(FaceHandle _fh) const
103 {
104 return calc_face_normal_impl(_fh, typename GenProg::IF<
105 vector_traits<PolyMeshT<Kernel>::Point>::size_ == 3,
106 PointIs3DTag,
107 PointIsNot3DTag
108 >::Result());
109 }
110
111 template <class Kernel>
112 typename PolyMeshT<Kernel>::Normal
113 PolyMeshT<Kernel>::calc_face_normal_impl(FaceHandle _fh, PointIs3DTag) const
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 n[0] += a[1] * b[2];
140 n[1] += a[2] * b[0];
141 n[2] += a[0] * b[1];
142 }
143
144 const typename vector_traits<Normal>::value_type norm = n.length();
145
146 // The expression ((n *= (1.0/norm)),n) is used because the OpenSG
147 // vector class does not return self after component-wise
148 // self-multiplication with a scalar!!!
149 return (norm != typename vector_traits<Normal>::value_type(0))
150 ? ((n *= (typename vector_traits<Normal>::value_type(1)/norm)), n)
151 : Normal(0, 0, 0);
152 }
153
154 template <class Kernel>
155 typename PolyMeshT<Kernel>::Normal
156 PolyMeshT<Kernel>::calc_face_normal_impl(FaceHandle, PointIsNot3DTag) const
157 {
158 // Dummy fallback implementation
159 return Normal(typename Normal::value_type(0));
160 }
161
162 //-----------------------------------------------------------------------------
163
164 template <class Kernel>
165 typename PolyMeshT<Kernel>::Normal
166 PolyMeshT<Kernel>::
167 calc_face_normal(const Point& _p0,
168 const Point& _p1,
169 const Point& _p2) const
170 {
171 return calc_face_normal_impl(_p0, _p1, _p2, typename GenProg::IF<
172 vector_traits<PolyMeshT<Kernel>::Point>::size_ == 3,
173 PointIs3DTag,
174 PointIsNot3DTag
175 >::Result());
176 }
177
178 template <class Kernel>
179 typename PolyMeshT<Kernel>::Normal
180 PolyMeshT<Kernel>::
181 calc_face_normal_impl(const Point& _p0,
182 const Point& _p1,
183 const Point& _p2,
184 PointIs3DTag) const
185 {
186 #if 1
187 // The OpenSG <Vector>::operator -= () does not support the type Point
188 // as rhs. Therefore use vector_cast at this point!!!
189 // Note! OpenSG distinguishes between Normal and Point!!!
190 Normal p1p0(vector_cast<Normal>(_p0)); p1p0 -= vector_cast<Normal>(_p1);
191 Normal p1p2(vector_cast<Normal>(_p2)); p1p2 -= vector_cast<Normal>(_p1);
192
193 Normal n = cross(p1p2, p1p0);
194 typename vector_traits<Normal>::value_type norm = n.length();
195
196 // The expression ((n *= (1.0/norm)),n) is used because the OpenSG
197 // vector class does not return self after component-wise
198 // self-multiplication with a scalar!!!
199 return (norm != typename vector_traits<Normal>::value_type(0)) ? ((n *= (typename vector_traits<Normal>::value_type(1)/norm)),n) : Normal(0,0,0);
200 #else
201 Point p1p0 = _p0; p1p0 -= _p1;
202 Point p1p2 = _p2; p1p2 -= _p1;
203
204 Normal n = vector_cast<Normal>(cross(p1p2, p1p0));
205 typename vector_traits<Normal>::value_type norm = n.length();
206
207 return (norm != 0.0) ? n *= (1.0/norm) : Normal(0,0,0);
208 #endif
209 }
210
211 template <class Kernel>
212 typename PolyMeshT<Kernel>::Normal
213 PolyMeshT<Kernel>::calc_face_normal_impl(const Point&, const Point&, const Point&, PointIsNot3DTag) const
214 {
215 return Normal(typename Normal::value_type(0));
216 }
217
218 //-----------------------------------------------------------------------------
219
220 template <class Kernel>
221 typename PolyMeshT<Kernel>::Point
222 PolyMeshT<Kernel>::
223 calc_face_centroid(FaceHandle _fh) const
224 {
225 Point _pt;
226 _pt.vectorize(0);
227 Scalar valence = 0.0;
228 for (ConstFaceVertexIter cfv_it = this->cfv_iter(_fh); cfv_it.is_valid(); ++cfv_it, valence += 1.0)
229 {
230 _pt += this->point(*cfv_it);
231 }
232 _pt /= valence;
233 return _pt;
234 }
235 //-----------------------------------------------------------------------------
236
237
238 template <class Kernel>
239 void
240 PolyMeshT<Kernel>::
241 update_normals()
242 {
243 // Face normals are required to compute the vertex and the halfedge normals
244 if (Kernel::has_face_normals() ) {
245 update_face_normals();
246
247 if (Kernel::has_vertex_normals() ) update_vertex_normals();
248 if (Kernel::has_halfedge_normals()) update_halfedge_normals();
249 }
250 }
251
252
253 //-----------------------------------------------------------------------------
254
255
256 template <class Kernel>
257 void
258 PolyMeshT<Kernel>::
259 update_face_normals()
260 {
261 FaceIter f_it(Kernel::faces_begin()), f_end(Kernel::faces_end());
262
263 for (; f_it != f_end; ++f_it)
264 this->set_normal(*f_it, calc_face_normal(*f_it));
265 }
266
267
268 //-----------------------------------------------------------------------------
269
270
271 template <class Kernel>
272 void
273 PolyMeshT<Kernel>::
274 update_halfedge_normals(const double _feature_angle)
275 {
276 HalfedgeIter h_it(Kernel::halfedges_begin()), h_end(Kernel::halfedges_end());
277
278 for (; h_it != h_end; ++h_it)
279 this->set_normal(*h_it, calc_halfedge_normal(*h_it, _feature_angle));
280 }
281
282
283 //-----------------------------------------------------------------------------
284
285
286 template <class Kernel>
287 typename PolyMeshT<Kernel>::Normal
288 PolyMeshT<Kernel>::
289 calc_halfedge_normal(HalfedgeHandle _heh, const double _feature_angle) const
290 {
291 if(Kernel::is_boundary(_heh))
292 return Normal(0,0,0);
293 else
294 {
295 std::vector<FaceHandle> fhs; fhs.reserve(10);
296
297 HalfedgeHandle heh = _heh;
298
299 // collect CW face-handles
300 do
301 {
302 fhs.push_back(Kernel::face_handle(heh));
303
304 heh = Kernel::next_halfedge_handle(heh);
305 heh = Kernel::opposite_halfedge_handle(heh);
306 }
307 while(heh != _heh && !Kernel::is_boundary(heh) && !is_estimated_feature_edge(heh, _feature_angle));
308
309 // collect CCW face-handles
310 if(heh != _heh && !is_estimated_feature_edge(_heh, _feature_angle))
311 {
312 heh = Kernel::opposite_halfedge_handle(_heh);
313
314 if ( !Kernel::is_boundary(heh) ) {
315 do
316 {
317
318 fhs.push_back(Kernel::face_handle(heh));
319
320 heh = Kernel::prev_halfedge_handle(heh);
321 heh = Kernel::opposite_halfedge_handle(heh);
322 }
323 while(!Kernel::is_boundary(heh) && !is_estimated_feature_edge(heh, _feature_angle));
324 }
325 }
326
327 Normal n(0,0,0);
328 for(unsigned int i=0; i<fhs.size(); ++i)
329 n += Kernel::normal(fhs[i]);
330
331 return n.normalize();
332 }
333 }
334
335
336 //-----------------------------------------------------------------------------
337
338
339 template <class Kernel>
340 bool
341 PolyMeshT<Kernel>::
342 is_estimated_feature_edge(HalfedgeHandle _heh, const double _feature_angle) const
343 {
344 EdgeHandle eh = Kernel::edge_handle(_heh);
345
346 if(Kernel::has_edge_status())
347 {
348 if(Kernel::status(eh).feature())
349 return true;
350 }
351
352 if(Kernel::is_boundary(eh))
353 return false;
354
355 // compute angle between faces
356 FaceHandle fh0 = Kernel::face_handle(_heh);
357 FaceHandle fh1 = Kernel::face_handle(Kernel::opposite_halfedge_handle(_heh));
358
359 Normal fn0 = Kernel::normal(fh0);
360 Normal fn1 = Kernel::normal(fh1);
361
362 // dihedral angle above angle threshold
363 return ( dot(fn0,fn1) < cos(_feature_angle) );
364 }
365
366
367 //-----------------------------------------------------------------------------
368
369
370 template <class Kernel>
371 typename PolyMeshT<Kernel>::Normal
372 PolyMeshT<Kernel>::
373 calc_vertex_normal(VertexHandle _vh) const
374 {
375 Normal n;
376 calc_vertex_normal_fast(_vh,n);
377
378 Scalar norm = n.length();
379 if (norm != 0.0) n *= (Scalar(1.0)/norm);
380
381 return n;
382 }
383
384 //-----------------------------------------------------------------------------
385 template <class Kernel>
386 void PolyMeshT<Kernel>::
387 calc_vertex_normal_fast(VertexHandle _vh, Normal& _n) const
388 {
389 _n.vectorize(0.0);
390 for (ConstVertexFaceIter vf_it = this->cvf_iter(_vh); vf_it.is_valid(); ++vf_it)
391 _n += this->normal(*vf_it);
392 }
393
394 //-----------------------------------------------------------------------------
395 template <class Kernel>
396 void PolyMeshT<Kernel>::
397 calc_vertex_normal_correct(VertexHandle _vh, Normal& _n) const
398 {
399 _n.vectorize(0.0);
400 ConstVertexIHalfedgeIter cvih_it = this->cvih_iter(_vh);
401 if (! cvih_it.is_valid() )
402 {//don't crash on isolated vertices
403 return;
404 }
405 Normal in_he_vec;
406 calc_edge_vector(*cvih_it, in_he_vec);
407 for ( ; cvih_it.is_valid(); ++cvih_it)
408 {//calculates the sector normal defined by cvih_it and adds it to _n
409 if (this->is_boundary(*cvih_it))
410 {
411 continue;
412 }
413 HalfedgeHandle out_heh(this->next_halfedge_handle(*cvih_it));
414 Normal out_he_vec;
415 calc_edge_vector(out_heh, out_he_vec);
416 _n += cross(in_he_vec, out_he_vec);//sector area is taken into account
417 in_he_vec = out_he_vec;
418 in_he_vec *= -1;//change the orientation
419 }
420 }
421
422 //-----------------------------------------------------------------------------
423 template <class Kernel>
424 void PolyMeshT<Kernel>::
425 calc_vertex_normal_loop(VertexHandle _vh, Normal& _n) const
426 {
427 static const LoopSchemeMaskDouble& loop_scheme_mask__ =
428 LoopSchemeMaskDoubleSingleton::Instance();
429
430 Normal t_v(0.0,0.0,0.0), t_w(0.0,0.0,0.0);
431 unsigned int vh_val = this->valence(_vh);
432 unsigned int i = 0;
433 for (ConstVertexOHalfedgeIter cvoh_it = this->cvoh_iter(_vh); cvoh_it.is_valid(); ++cvoh_it, ++i)
434 {
435 VertexHandle r1_v( this->to_vertex_handle(*cvoh_it) );
436 t_v += (typename vector_traits<Point>::value_type)(loop_scheme_mask__.tang0_weight(vh_val, i))*this->point(r1_v);
437 t_w += (typename vector_traits<Point>::value_type)(loop_scheme_mask__.tang1_weight(vh_val, i))*this->point(r1_v);
438 }
439 _n = cross(t_w, t_v);//hack: should be cross(t_v, t_w), but then the normals are reversed?
440 }
441
442 //-----------------------------------------------------------------------------
443
444
445 template <class Kernel>
446 void
447 PolyMeshT<Kernel>::
448 update_vertex_normals()
449 {
450 VertexIter v_it(Kernel::vertices_begin()), v_end(Kernel::vertices_end());
451
452 for (; v_it!=v_end; ++v_it)
453 this->set_normal(*v_it, calc_vertex_normal(*v_it));
454 }
455
456 //=============================================================================
457 } // namespace OpenMesh
458 //=============================================================================
OpenMesh::PolyMeshT::ConstFaceVertexIter
Kernel::ConstFaceVertexIter ConstFaceVertexIter
Circulator.
Definition: PolyMeshT.hh:180
OpenMesh::vector_traits
Definition: vector_traits.hh:93
OpenMesh::PolyMeshT::ConstVertexOHalfedgeIter
Kernel::ConstVertexOHalfedgeIter ConstVertexOHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:176
OpenMesh
Definition: MeshItems.hh:64
OpenMesh::PolyMeshT::calc_face_centroid
void calc_face_centroid(FaceHandle _fh, Point &_pt) const
calculates the average of the vertices defining _fh
Definition: PolyMeshT.hh:276
OpenMesh::PolyMeshT::PointIsNot3DTag
Definition: PolyMeshT.hh:559
OpenMesh::PolyMeshT::calc_halfedge_normal
virtual Normal calc_halfedge_normal(HalfedgeHandle _heh, const double _feature_angle=0.8) const
Calculate halfedge normal for one specific halfedge.
Definition: PolyMeshT.cc:289
OpenMesh::vector_traits::value_type
T::value_type value_type
Type of the scalar value.
Definition: vector_traits.hh:99
OpenMesh::PolyMeshT::update_halfedge_normals
void update_halfedge_normals(const double _feature_angle=0.8)
Update normal vectors for all halfedges.
Definition: PolyMeshT.cc:274
OpenMesh::vector_cast
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
OpenMesh::PolyMeshT::Point
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:115
omstream.hh
OpenMesh::cross
osg::Vec3f cross(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Definition: VectorAdapter.hh:196
OpenMesh::PolyMeshT::calc_vertex_normal
Normal calc_vertex_normal(VertexHandle _vh) const
Calculate vertex normal for one specific vertex.
Definition: PolyMeshT.cc:373
OpenMesh::PolyMeshT::find_feature_edges
unsigned int find_feature_edges(Scalar _angle_tresh=OpenMesh::deg_to_rad(44.0))
Definition: PolyMeshT.cc:79
OpenMesh::PolyMeshT::PointIs3DTag
Definition: PolyMeshT.hh:558
OpenMesh::PolyMeshT::ConstVertexIHalfedgeIter
Kernel::ConstVertexIHalfedgeIter ConstVertexIHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:177
OpenMesh::PolyMeshT::update_vertex_normals
void update_vertex_normals()
Update normal vectors for all vertices.
Definition: PolyMeshT.cc:448
OpenMesh::PolyMeshT::calc_vertex_normal_fast
void calc_vertex_normal_fast(VertexHandle _vh, Normal &_n) const
Definition: PolyMeshT.cc:387
OpenMesh::PolyMeshT::update_face_normals
void update_face_normals()
Update normal vectors for all faces.
Definition: PolyMeshT.cc:259
OpenMesh::PolyMeshT::calc_vertex_normal_loop
void calc_vertex_normal_loop(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT.cc:425
OpenMesh::FaceHandle
Handle for a face entity.
Definition: Handles.hh:146
OpenMesh::dot
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Definition: VectorAdapter.hh:181
OpenMesh::PolyMeshT::Scalar
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:113
OpenMesh::PolyMeshT::ConstVertexFaceIter
Kernel::ConstVertexFaceIter ConstVertexFaceIter
Circulator.
Definition: PolyMeshT.hh:179
OpenMesh::SingletonT::Instance
static T & Instance()
Definition: SingletonT.hh:94
OpenMesh::PolyMeshT::VertexHandle
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:139
OpenMesh::PolyMeshT::calc_vertex_normal_correct
void calc_vertex_normal_correct(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT.cc:397
OpenMesh::PolyMeshT::update_normals
void update_normals()
Compute normals for all primitives.
Definition: PolyMeshT.cc:241
OpenMesh::Attributes::Normal
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:87
OpenMesh::LoopSchemeMaskT
Definition: LoopSchemeMaskT.hh:71
OpenMesh::PolyMeshT
Definition: PolyMeshT.hh:94
OpenMesh::PolyMeshT::is_estimated_feature_edge
bool is_estimated_feature_edge(HalfedgeHandle _heh, const double _feature_angle) const
Definition: PolyMeshT.cc:342
OpenMesh::PolyMeshT::Normal
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:117
OpenMesh::PolyMeshT::calc_face_normal
virtual Normal calc_face_normal(FaceHandle _fh) const
Definition: PolyMeshT.cc:102