Developer Documentation

NormalAttribT_impl.hh
1 /*===========================================================================*\
2 * *
3 * OpenVolumeMesh *
4 * Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen *
5 * www.openvolumemesh.org *
6 * *
7 *---------------------------------------------------------------------------*
8 * This file is part of OpenVolumeMesh. *
9 * *
10 * OpenVolumeMesh is free software: you can redistribute it and/or modify *
11 * it under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 3 of *
13 * the License, or (at your option) any later version with the *
14 * following exceptions: *
15 * *
16 * If other files instantiate templates or use macros *
17 * or inline functions from this file, or you compile this file and *
18 * link it with other files to produce an executable, this file does *
19 * not by itself cause the resulting executable to be covered by the *
20 * GNU Lesser General Public License. This exception does not however *
21 * invalidate any other reasons why the executable file might be *
22 * covered by the GNU Lesser General Public License. *
23 * *
24 * OpenVolumeMesh is distributed in the hope that it will be useful, *
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27 * GNU Lesser General Public License for more details. *
28 * *
29 * You should have received a copy of the GNU LesserGeneral Public *
30 * License along with OpenVolumeMesh. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*===========================================================================*/
34
35 #define NORMALATTRIBT_CC
36
37 #include <set>
38
39 #include "NormalAttrib.hh"
40
41 #include "../Core/GeometryKernel.hh"
42
43 namespace OpenVolumeMesh {
44
45 template <class GeomKernelT>
46 NormalAttrib<GeomKernelT>::NormalAttrib(GeomKernelT& _kernel) :
47 kernel_(_kernel),
48 v_normals_(_kernel.template request_vertex_property<typename GeomKernelT::PointT>("vertex_normals", typename GeomKernelT::PointT(0.0))),
49 f_normals_(_kernel.template request_face_property<typename GeomKernelT::PointT>("face_normals", typename GeomKernelT::PointT(0.0)))
50 {
51
52 }
53
54 template <class GeomKernelT>
55 NormalAttrib<GeomKernelT>::~NormalAttrib() {
56
57 }
58
59 template <class GeomKernelT>
60 void NormalAttrib<GeomKernelT>::update_vertex_normals() {
61
62 if(!kernel_.has_face_bottom_up_incidences()) {
63 std::cerr << "Error: update_vertex_normals() needs bottom-up incidences!" << std::endl;
64 return;
65 }
66
67 // Compute face normals
68 update_face_normals();
69
70 for(VertexIter v_it = kernel_.v_iter(); v_it.valid(); ++v_it) {
71 compute_vertex_normal(*v_it);
72 }
73 }
74
75 template <class GeomKernelT>
76 void NormalAttrib<GeomKernelT>::update_face_normals() {
77
78 if(!kernel_.has_face_bottom_up_incidences()) {
79 std::cerr << "Error: update_normals() needs bottom-up incidences!" << std::endl;
80 return;
81 }
82
83 for(FaceIter f_it = kernel_.f_iter(); f_it.valid(); ++f_it) {
84 // Assume the face is planar, so just take the
85 // first two edges
86 compute_face_normal(*f_it);
87 }
88 }
89
90 template <class GeomKernelT>
91 void NormalAttrib<GeomKernelT>::compute_vertex_normal(const VertexHandle& _vh) {
92
93 std::set<HalfFaceHandle> halffaces;
94 for(VertexOHalfEdgeIter voh_it = kernel_.voh_iter(_vh);
95 voh_it.valid(); ++voh_it) {
96
97 for(HalfEdgeHalfFaceIter hehf_it = kernel_.hehf_iter(*voh_it);
98 hehf_it.valid(); ++hehf_it) {
99 if(kernel_.is_boundary(*hehf_it)) {
100 halffaces.insert(*hehf_it);
101 }
102 }
103 }
104 typename GeomKernelT::PointT normal = typename GeomKernelT::PointT(0.0);
105 for(std::set<HalfFaceHandle>::const_iterator hf_it = halffaces.begin();
106 hf_it != halffaces.end(); ++hf_it) {
107 normal += (*this)[*hf_it];
108 }
109
110 normal.normalize();
111
112 v_normals_[_vh] = normal;
113 }
114
115 template <class GeomKernelT>
116 void NormalAttrib<GeomKernelT>::compute_face_normal(const FaceHandle& _fh) {
117
118 if(kernel_.face(_fh).halfedges().size() < 3) {
119 std::cerr << "Warning: Degenerate face detected!" << std::endl;
120 return;
121 }
122
123 const std::vector<HalfEdgeHandle>& halfedges = kernel_.face(_fh).halfedges();
124 std::vector<HalfEdgeHandle>::const_iterator he_it = halfedges.begin();
125
126 typename GeomKernelT::PointT p1 = kernel_.vertex(kernel_.halfedge(*he_it).from_vertex());
127 typename GeomKernelT::PointT p2 = kernel_.vertex(kernel_.halfedge(*he_it).to_vertex());
128 ++he_it;
129 typename GeomKernelT::PointT p3 = kernel_.vertex(kernel_.halfedge(*he_it).to_vertex());
130
131 typename GeomKernelT::PointT n = (p2 - p1) % (p3 - p2);
132 n.normalize();
133
134 f_normals_[_fh] = n;
135 }
136
137 } // Namespace OpenVolumeMesh
OpenVolumeMesh
Definition: ColorAttrib.hh:53
OpenVolumeMesh::VertexIter
Definition: Iterators.hh:837
OpenVolumeMesh::HandleT< Entity::Vertex >
OpenVolumeMesh::HalfEdgeHalfFaceIter
Definition: Iterators.hh:369
OpenVolumeMesh::VertexOHalfEdgeIter
Definition: Iterators.hh:245
OpenVolumeMesh::NormalAttrib
Definition: NormalAttrib.hh:55
OpenVolumeMesh::NormalAttrib::update_vertex_normals
void update_vertex_normals()
A simple heuristic to estimate the vertex normals.
Definition: NormalAttribT_impl.hh:60
OpenVolumeMesh::FaceIter
Definition: Iterators.hh:999
OpenVolumeMesh::NormalAttrib::update_face_normals
void update_face_normals()
Compute face normals.
Definition: NormalAttribT_impl.hh:76