Developer Documentation
NormalAttribT.cc
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 /*===========================================================================*\
36  * *
37  * $Revision$ *
38  * $Date$ *
39  * $LastChangedBy$ *
40  * *
41 \*===========================================================================*/
42 
43 #define NORMALATTRIBT_CC
44 
45 #include <set>
46 
47 #include "NormalAttrib.hh"
48 
49 #include "../Core/GeometryKernel.hh"
50 
51 namespace OpenVolumeMesh {
52 
53 template <class GeomKernelT>
54 NormalAttrib<GeomKernelT>::NormalAttrib(GeomKernelT& _kernel) :
55 kernel_(_kernel),
56 v_normals_(_kernel.template request_vertex_property<typename GeomKernelT::PointT>("vertex_normals")),
57 f_normals_(_kernel.template request_face_property<typename GeomKernelT::PointT>("face_normals"))
58 {
59 
60 }
61 
62 template <class GeomKernelT>
63 NormalAttrib<GeomKernelT>::~NormalAttrib() {
64 
65 }
66 
67 template <class GeomKernelT>
69 
70  if(!kernel_.has_face_bottom_up_incidences()) {
71  std::cerr << "Error: update_vertex_normals() needs bottom-up incidences!" << std::endl;
72  return;
73  }
74 
75  // Compute face normals
77 
78  for(VertexIter v_it = kernel_.v_iter(); v_it.valid(); ++v_it) {
79  compute_vertex_normal(*v_it);
80  }
81 }
82 
83 template <class GeomKernelT>
85 
86  if(!kernel_.has_face_bottom_up_incidences()) {
87  std::cerr << "Error: update_normals() needs bottom-up incidences!" << std::endl;
88  return;
89  }
90 
91  for(FaceIter f_it = kernel_.f_iter(); f_it.valid(); ++f_it) {
92  // Assume the face is planar, so just take the
93  // first two edges
94  compute_face_normal(*f_it);
95  }
96 }
97 
98 template <class GeomKernelT>
100 
101  std::set<HalfFaceHandle> halffaces;
102  for(VertexOHalfEdgeIter voh_it = kernel_.voh_iter(_vh);
103  voh_it.valid(); ++voh_it) {
104 
105  for(HalfEdgeHalfFaceIter hehf_it = kernel_.hehf_iter(*voh_it);
106  hehf_it.valid(); ++hehf_it) {
107  if(kernel_.is_boundary(*hehf_it)) {
108  halffaces.insert(*hehf_it);
109  }
110  }
111  }
112  typename GeomKernelT::PointT normal = typename GeomKernelT::PointT(0.0);
113  for(std::set<HalfFaceHandle>::const_iterator hf_it = halffaces.begin();
114  hf_it != halffaces.end(); ++hf_it) {
115  normal += (*this)[*hf_it];
116  }
117 
118  normal.normalize();
119 
120  v_normals_[_vh.idx()] = normal;
121 }
122 
123 template <class GeomKernelT>
125 
126  if(kernel_.face(_fh).halfedges().size() < 3) {
127  std::cerr << "Warning: Degenerate face detected!" << std::endl;
128  return;
129  }
130 
131  const std::vector<HalfEdgeHandle>& halfedges = kernel_.face(_fh).halfedges();
132  std::vector<HalfEdgeHandle>::const_iterator he_it = halfedges.begin();
133 
134  typename GeomKernelT::PointT p1 = kernel_.vertex(kernel_.halfedge(*he_it).from_vertex());
135  typename GeomKernelT::PointT p2 = kernel_.vertex(kernel_.halfedge(*he_it).to_vertex());
136  ++he_it;
137  typename GeomKernelT::PointT p3 = kernel_.vertex(kernel_.halfedge(*he_it).to_vertex());
138 
139  typename GeomKernelT::PointT n = (p2 - p1) % (p3 - p2);
140  n.normalize();
141 
142  f_normals_[_fh.idx()] = n;
143 }
144 
145 } // Namespace OpenVolumeMesh
void update_face_normals()
Compute face normals.
void update_vertex_normals()
A simple heuristic to estimate the vertex normals.