Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_TRIMESH_UTIL_TRIMESH_GRID_ORIENTATOR_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_TRIMESH_UTIL_TRIMESH_GRID_ORIENTATOR_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/containers/mesh/trimesh/trimesh.h>
00013 #include <OpenTissue/core/containers/grid/util/grid_gradient_at_point.h>
00014 #include <OpenTissue/core/containers/mesh/trimesh/util/trimesh_compute_face_normal.h>
00015
00016 namespace OpenTissue
00017 {
00018 namespace trimesh
00019 {
00020
00021 template<
00022 typename M
00023 , typename V
00024 , typename F
00025 , template <typename, typename> class K
00026 , typename grid_type
00027 >
00028 void grid_orientator(grid_type const & grid, TriMesh<M,V,F,K> & mesh, bool same_direction = true)
00029 {
00030 typedef TriMesh<M,V,F,K> mesh_type;
00031 typedef typename mesh_type::face_iterator face_iterator;
00032 typedef typename mesh_type::vertex_iterator vertex_iterator;
00033 typedef typename mesh_type::vertex_handle vertex_handle;
00034 typedef typename mesh_type::math_types math_types;
00035 typedef typename math_types::vector3_type vector3_type;
00036 typedef typename math_types::real_type real_type;
00037
00038
00039 face_iterator end = mesh.face_end();
00040 face_iterator f = mesh.face_begin();
00041 for (;f!=end;++f)
00042 {
00043 vertex_iterator v0 = f->get_vertex0_iterator();
00044 vertex_iterator v1 = f->get_vertex1_iterator();
00045 vertex_iterator v2 = f->get_vertex2_iterator();
00046 vector3_type normal;
00047 compute_face_normal(*f,normal);
00048 vector3_type grad0 = OpenTissue::grid::gradient_at_point(grid, v0->m_coord);
00049 vector3_type grad1 = OpenTissue::grid::gradient_at_point(grid, v1->m_coord);
00050 vector3_type grad2 = OpenTissue::grid::gradient_at_point(grid, v2->m_coord);
00051 vector3_type avg_grad = (grad0 + grad1 + grad2)/static_cast<real_type>(3.0);
00052 real_type g_dot_n = avg_grad * normal;
00053 bool reorder = false;
00054 if (same_direction && g_dot_n < static_cast<real_type>(0.0) )
00055 reorder = true;
00056 if (!same_direction && g_dot_n > static_cast<real_type>(0.0) )
00057 reorder = true;
00058 if(reorder)
00059 {
00060 vertex_handle h0 = f->get_vertex0_handle();
00061 vertex_handle h1 = f->get_vertex1_handle();
00062 vertex_handle h2 = f->get_vertex2_handle();
00063 trimesh_core_access::set_vertex0_handle(f,h1);
00064 trimesh_core_access::set_vertex0_handle(f,h0);
00065 trimesh_core_access::set_vertex0_handle(f,h2);
00066 }
00067 }
00068 }
00069
00070 }
00071 }
00072
00073
00074 #endif