OpenMesh
PolyMeshT_impl.hh
1/* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2023, 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
69namespace OpenMesh {
70
71//== IMPLEMENTATION ==========================================================
72
73template <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
95template <class Kernel>
98{
99 return calc_face_normal_impl(_fh, typename GenProg::IF<
101 PointIs3DTag,
102 PointIsNot3DTag
103 >::Result());
104}
105
106template <class Kernel>
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
152template <class Kernel>
154PolyMeshT<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
170template <class Kernel>
173calc_face_normal(const Point& _p0,
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
184template<class Kernel>
187calc_normal(FaceHandle _fh) const
188{
189 return calc_face_normal(_fh);
190}
191
192template <class Kernel>
195calc_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
227template <class Kernel>
229PolyMeshT<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
246template <class Kernel>
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
264template<class Kernel>
267calc_centroid(FaceHandle _fh) const
268{
269 return calc_face_centroid(_fh);
270}
271
272//-----------------------------------------------------------------------------
273
274template<class Kernel>
277calc_centroid(EdgeHandle _eh) const
278{
279 return this->calc_edge_midpoint(_eh);
280}
281
282//-----------------------------------------------------------------------------
283
284template<class Kernel>
288{
289 return this->calc_edge_midpoint(this->edge_handle(_heh));
290}
291
292//-----------------------------------------------------------------------------
293
294template<class Kernel>
298{
299 return this->point(_vh);
300}
301
302//-----------------------------------------------------------------------------
303
304template<class Kernel>
307calc_centroid(MeshHandle /*_mh*/) const
308{
309 return this->vertices().avg([this](VertexHandle vh) { return this->point(vh); });
310}
311
312//-----------------------------------------------------------------------------
313
314template <class Kernel>
315void
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
332template <class Kernel>
333void
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
347template <class Kernel>
348void
350update_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
362template <class Kernel>
365calc_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::has_face_normals() ? Kernel::normal(fhs[i]) : calc_face_normal(fhs[i]);
406
407 return normalize(n);
408 }
409}
410
411
412//-----------------------------------------------------------------------------
413
414
415template <class Kernel>
418calc_normal(HalfedgeHandle _heh, const double _feature_angle) const
419{
420 return calc_halfedge_normal(_heh, _feature_angle);
421}
422
423
424//-----------------------------------------------------------------------------
425
426
427template <class Kernel>
430calc_normal(EdgeHandle _eh) const
431{
432 Normal n(0, 0, 0);
433 for (int i = 0; i < 2; ++i)
434 {
435 const auto heh = this->halfedge_handle(_eh, i);
436 const auto fh = this->face_handle(heh);
437 if (fh.is_valid())
438 n += calc_normal(fh);
439 }
440 const auto length = norm(n);
441 if (length != 0)
442 n /= length;
443 return n;
444}
445
446//-----------------------------------------------------------------------------
447
448
449template <class Kernel>
450bool
452is_estimated_feature_edge(HalfedgeHandle _heh, const double _feature_angle) const
453{
454 EdgeHandle eh = Kernel::edge_handle(_heh);
455
456 if(Kernel::has_edge_status())
457 {
458 if(Kernel::status(eh).feature())
459 return true;
460 }
461
462 if(Kernel::is_boundary(eh))
463 return false;
464
465 // compute angle between faces
466 FaceHandle fh0 = Kernel::face_handle(_heh);
467 FaceHandle fh1 = Kernel::face_handle(Kernel::opposite_halfedge_handle(_heh));
468
469 Normal fn0 = Kernel::has_face_normals() ? Kernel::normal(fh0) : calc_face_normal(fh0);
470 Normal fn1 = Kernel::has_face_normals() ? Kernel::normal(fh1) : calc_face_normal(fh1);
471
472 // dihedral angle above angle threshold
473 return ( dot(fn0,fn1) < cos(_feature_angle) );
474}
475
476
477//-----------------------------------------------------------------------------
478
479
480template <class Kernel>
484{
485 Normal n;
486 calc_vertex_normal_fast(_vh,n);
487
488 Scalar length = norm(n);
489 if (length != 0.0) n *= (Scalar(1.0)/length);
490
491 return n;
492}
493
494//-----------------------------------------------------------------------------
495template <class Kernel>
498{
499 vectorize(_n, Scalar(0));
500 for (ConstVertexFaceIter vf_it = this->cvf_iter(_vh); vf_it.is_valid(); ++vf_it)
501 _n += this->normal(*vf_it);
502}
503
504//-----------------------------------------------------------------------------
505template <class Kernel>
508{
509 vectorize(_n, Scalar(0));
510 ConstVertexIHalfedgeIter cvih_it = this->cvih_iter(_vh);
511 if (! cvih_it.is_valid() )
512 {//don't crash on isolated vertices
513 return;
514 }
515 Normal in_he_vec;
516 calc_edge_vector(*cvih_it, in_he_vec);
517 for ( ; cvih_it.is_valid(); ++cvih_it)
518 {//calculates the sector normal defined by cvih_it and adds it to _n
519 if (this->is_boundary(*cvih_it))
520 {
521 continue;
522 }
523 HalfedgeHandle out_heh(this->next_halfedge_handle(*cvih_it));
524 Normal out_he_vec;
525 calc_edge_vector(out_heh, out_he_vec);
526 _n += cross(in_he_vec, out_he_vec);//sector area is taken into account
527 in_he_vec = out_he_vec;
528 in_he_vec *= -1;//change the orientation
529 }
530 Scalar length = norm(_n);
531 if (length != 0.0)
532 _n *= (Scalar(1.0)/length);
533}
534
535//-----------------------------------------------------------------------------
536template <class Kernel>
539{
540 static const LoopSchemeMaskDouble& loop_scheme_mask__ =
542
543 Normal t_v(0.0,0.0,0.0), t_w(0.0,0.0,0.0);
544 unsigned int vh_val = this->valence(_vh);
545 unsigned int i = 0;
546 for (ConstVertexOHalfedgeIter cvoh_it = this->cvoh_iter(_vh); cvoh_it.is_valid(); ++cvoh_it, ++i)
547 {
548 VertexHandle r1_v( this->to_vertex_handle(*cvoh_it) );
549 t_v += (typename vector_traits<Point>::value_type)(loop_scheme_mask__.tang0_weight(vh_val, i))*this->point(r1_v);
550 t_w += (typename vector_traits<Point>::value_type)(loop_scheme_mask__.tang1_weight(vh_val, i))*this->point(r1_v);
551 }
552 _n = cross(t_w, t_v);//hack: should be cross(t_v, t_w), but then the normals are reversed?
553}
554
555//-----------------------------------------------------------------------------
556
557template<class Kernel>
560calc_normal(VertexHandle _vh) const
561{
562 Normal n;
563 calc_vertex_normal_correct(_vh, n);
564 return n;
565}
566
567//-----------------------------------------------------------------------------
568
569template <class Kernel>
570void
573{
574 VertexIter v_it(Kernel::vertices_begin()), v_end(Kernel::vertices_end());
575
576 for (; v_it!=v_end; ++v_it)
577 this->set_normal(*v_it, calc_vertex_normal(*v_it));
578}
579
580//=============================================================================
581} // namespace OpenMesh
582//=============================================================================
This file provides the streams omlog, omout, and omerr.
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
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
@ Normal
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:82
implements cache for the weights of the original Loop scheme supported:
Definition: LoopSchemeMaskT.hh:67
Handle for a face entity.
Definition: Handles.hh:142
Handle type for meshes to simplify some template programming.
Definition: Handles.hh:149
Base type for a polygonal mesh.
Definition: PolyMeshT.hh:91
void calc_face_centroid(FaceHandle _fh, Point &_pt) const
calculates the average of the vertices defining _fh
Definition: PolyMeshT.hh:289
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:110
Kernel::EdgeHandle EdgeHandle
Scalar type.
Definition: PolyMeshT.hh:138
Kernel::ConstFaceVertexIter ConstFaceVertexIter
Circulator.
Definition: PolyMeshT.hh:177
void calc_vertex_normal_fast(VertexHandle _vh, Normal &_n) const
Different methods for calculation of the normal at _vh:
Definition: PolyMeshT_impl.hh:497
void calc_vertex_normal_correct(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:507
Kernel::FaceIter FaceIter
Scalar type.
Definition: PolyMeshT.hh:146
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
void update_face_normals()
Update normal vectors for all faces.
Definition: PolyMeshT_impl.hh:335
void update_halfedge_normals(const double _feature_angle=0.8)
Update normal vectors for all halfedges.
Definition: PolyMeshT_impl.hh:350
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
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
void update_normals()
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:317
Kernel::ConstVertexFaceIter ConstVertexFaceIter
Circulator.
Definition: PolyMeshT.hh:176
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
Kernel::ConstVertexIHalfedgeIter ConstVertexIHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:174
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:452
Kernel::FaceHandle FaceHandle
Scalar type.
Definition: PolyMeshT.hh:139
void calc_vertex_normal_loop(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:538
Kernel::HalfedgeHandle HalfedgeHandle
Scalar type.
Definition: PolyMeshT.hh:137
Kernel::EdgeIter EdgeIter
Scalar type.
Definition: PolyMeshT.hh:145
void update_vertex_normals()
Update normal vectors for all vertices.
Definition: PolyMeshT_impl.hh:572
Kernel::ConstVertexOHalfedgeIter ConstVertexOHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:173
Kernel::HalfedgeIter HalfedgeIter
Scalar type.
Definition: PolyMeshT.hh:144
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
Normal calc_normal(FaceHandle _fh) const
same as calc_face_normal
Definition: PolyMeshT_impl.hh:187
Kernel::VertexIter VertexIter
Scalar type.
Definition: PolyMeshT.hh:143
Normal calc_vertex_normal(VertexHandle _vh) const
Calculate vertex normal for one specific vertex.
Definition: PolyMeshT_impl.hh:483
static T & Instance()
Singleton access function.
Definition: SingletonT.hh:86
Helper class providing information about a vector type.
Definition: vector_traits.hh:89
T::value_type value_type
Type of the scalar value.
Definition: vector_traits.hh:94

Project OpenMesh, ©  Visual Computing Institute, RWTH Aachen. Documentation generated using doxygen .