Go to the documentation of this file.00001 #ifndef OPENTISSUE_COLLISION_VCLIP_VCLIP_MESH_H
00002 #define OPENTISSUE_COLLISION_VCLIP_VCLIP_MESH_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_basic_types.h>
00013
00014 #include <OpenTissue/core/geometry/geometry_plane.h>
00015 #include <OpenTissue/core/containers/mesh/polymesh/polymesh.h>
00016 #include <OpenTissue/core/containers/mesh/common/util/mesh_convex_hull.h>
00017 #include <OpenTissue/core/containers/mesh/common/util/mesh_compute_face_plane.h>
00018
00019 #include <OpenTissue/core/containers/mesh/polymesh/util/polymesh_compute_vertex_edge_voronoi_plane.h>
00020 #include <OpenTissue/core/containers/mesh/polymesh/util/polymesh_compute_edge_face_voronoi_plane.h>
00021
00022
00023 namespace OpenTissue
00024 {
00025 namespace vclip
00026 {
00027
00028 class Feature
00029 {
00030 public:
00031
00032 typedef enum {UNDEFINED = -1, VERTEX = 0, EDGE = 1, FACE = 2} feature_type;
00033
00034 public:
00035
00036 feature_type m_type;
00037
00038 public:
00039
00040 Feature(feature_type const & type) : m_type(type)
00041 {}
00042
00043 };
00044
00045 template<typename M>
00046 class VoronoiClipVertexTraits
00047 : public mesh::DefaultVertexTraits<M>
00048 , public Feature
00049 {
00050 public:
00051
00052 VoronoiClipVertexTraits()
00053 : Feature(Feature::VERTEX)
00054 {}
00055
00056 };
00057
00058 template<typename M>
00059 class VoronoiClipHalfEdgeTraits
00060 : public mesh::DefaultHalfEdgeTraits
00061 , public Feature
00062 {
00063 public:
00064
00065 typedef M math_types;
00066 typedef typename math_types::real_type real_type;
00067 typedef typename math_types::vector3_type vector3_type;
00068 typedef geometry::Plane<math_types> plane_type;
00069
00070 plane_type m_voronoi_plane_VE;
00071 plane_type m_voronoi_plane_EF;
00072
00073 vector3_type m_u;
00074 real_type m_length;
00075 int m_tag;
00076
00077 public:
00078
00079 VoronoiClipHalfEdgeTraits()
00080 : Feature(Feature::EDGE)
00081 {}
00082
00083 };
00084
00085 class VoronoiClipEdgeTraits
00086 : public mesh::DefaultEdgeTraits
00087 , public Feature
00088 {
00089 public:
00090
00091 VoronoiClipEdgeTraits()
00092 : Feature(Feature::UNDEFINED)
00093 {}
00094
00095 };
00096
00097 template<typename M>
00098 class VoronoiClipFaceTraits
00099 : public mesh::DefaultFaceTraits
00100 , public Feature
00101 {
00102 public:
00103
00104 typedef M math_types;
00105 typedef geometry::Plane<math_types> plane_type;
00106
00107 public:
00108
00109 plane_type m_plane;
00110
00111 public:
00112
00113 VoronoiClipFaceTraits()
00114 : Feature(Feature::FACE)
00115 {}
00116
00117 };
00118
00119 template<typename M>
00120 class VClipMesh
00121 : public polymesh::PolyMesh<
00122 M
00123 , VoronoiClipVertexTraits<M>
00124 , VoronoiClipHalfEdgeTraits<M>
00125 , VoronoiClipEdgeTraits
00126 , VoronoiClipFaceTraits<M>
00127 , polymesh::PolyMeshListKernel
00128 >
00129 {};
00130
00131
00132 typedef VClipMesh< OpenTissue::math::BasicMathTypes<double,size_t> > vclip_mesh_type;
00133
00139 void update_voronoi_regions(vclip_mesh_type & mesh)
00140 {
00141 vclip_mesh_type::halfedge_iterator h = mesh.halfedge_begin();
00142 vclip_mesh_type::halfedge_iterator hend = mesh.halfedge_end();
00143 for(;h!=hend;++h)
00144 {
00145 polymesh::compute_vertex_edge_voronoi_plane( *(h->get_destination_iterator()), *h, h->m_voronoi_plane_VE );
00146 polymesh::compute_edge_face_voronoi_plane( (*h), *(h->get_face_iterator()), h->m_voronoi_plane_EF );
00147 h->m_u = h->get_destination_iterator()->m_coord - h->get_origin_iterator()->m_coord;
00148 h->m_length = std::sqrt(h->m_u*h->m_u);
00149 h->m_u /= h->m_length;
00150 h->m_tag = -1;
00151 }
00152 vclip_mesh_type::face_iterator f = mesh.face_begin();
00153 vclip_mesh_type::face_iterator fend = mesh.face_end();
00154 for(;f!=fend;++f)
00155 mesh::compute_face_plane(*f,f->m_plane);
00156 }
00157
00166 template< typename mesh_type >
00167 bool convert( mesh_type const & in, vclip_mesh_type & out)
00168 {
00169 typedef typename vclip_mesh_type::math_types math_types;
00170 typedef typename math_types::vector3_type vector3_type;
00171
00172 unsigned int N = static_cast<unsigned int>(in.size_vertices());
00173
00174 std::vector<vector3_type> points(N);
00175
00176 typename mesh_type::const_vertex_iterator v = in.vertex_begin();
00177
00178 for(unsigned int i=0;i<N;++i,++v)
00179 points[i] = v->m_coord;
00180
00181 out.clear();
00182 mesh::convex_hull(points.begin(),points.end(),out);
00183 update_voronoi_regions(out);
00184 return true;
00185 }
00186
00187 }
00188
00189 }
00190
00191
00192 #endif