Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_UTIL_COMPUTE_ANGLE_WEIGHTED_VERTEX_NORMALS_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_UTIL_COMPUTE_ANGLE_WEIGHTED_VERTEX_NORMALS_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <cassert>
00013 #include <cmath>
00014 #include <vector>
00015
00016 namespace OpenTissue
00017 {
00018 namespace mesh
00019 {
00020
00021 template<typename mesh_type>
00022 void compute_angle_weighted_vertex_normals(mesh_type & mesh)
00023 {
00024 typedef typename mesh_type::math_types math_types;
00025 typedef typename math_types::value_traits value_traits;
00026 typedef typename math_types::vector3_type vector3_type;
00027 typedef typename math_types::real_type real_type;
00028
00029 typedef typename mesh_type::vertex_iterator vertex_iterator;
00030 typedef typename mesh_type::face_iterator face_iterator;
00031 typedef typename mesh_type::face_vertex_circulator face_vertex_circulator;
00032 {
00033 vertex_iterator end = mesh.vertex_end();
00034 vertex_iterator v = mesh.vertex_begin();
00035 for(;v!=end;++v)
00036 v->m_normal.clear();
00037 }
00038 {
00039 face_iterator end = mesh.face_end();
00040 face_iterator f = mesh.face_begin();
00041 for(;f!=end;++f)
00042 {
00043 face_vertex_circulator i ( *f ),vend;
00044 face_vertex_circulator j ( *f );++j;
00045 face_vertex_circulator k ( *f );++k;++k;
00046 for(;i!=vend;++i,++j,++k)
00047 {
00048 vector3_type u0 = normalize(j->m_coord - i->m_coord);
00049 vector3_type u1 = normalize(k->m_coord - j->m_coord);
00050 real_type dot = -( u0*u1 );
00051 if(dot<-1)
00052 dot = -1;
00053 if(dot>1)
00054 dot = 1;
00055 real_type alpha = std::acos( dot );
00056 vector3_type n = normalize(u0 % u1);
00057 j->m_normal += alpha*n;
00058 }
00059 }
00060 }
00061 {
00062 vertex_iterator end = mesh.vertex_end();
00063 vertex_iterator v = mesh.vertex_begin();
00064 for(;v!=end;++v)
00065 v->m_normal = normalize(v->m_normal);
00066 }
00067 }
00068
00069
00070 }
00071 }
00072
00073
00074 #endif