Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_POLYMESH_UTIL_POLYMESH_COMPUTE_DELAUNAY2D_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_POLYMESH_UTIL_POLYMESH_COMPUTE_DELAUNAY2D_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <Triangle/triangle.h>
00013 #include <boost/vector_property_map.hpp>
00014 #include <boost/cast.hpp>
00015 #include <cstring>
00016
00017 namespace OpenTissue
00018 {
00019 namespace polymesh
00020 {
00021
00029 template< typename point_container, typename mesh_type >
00030 void compute_delaunay2D(point_container const & vertices, mesh_type & mesh)
00031 {
00032 typedef typename mesh_type::math_types math_types;
00033 typedef typename math_types::real_type real_type;
00034 typedef typename math_types::vector3_type vector3_type;
00035 typedef typename math_types::value_traits value_traits;
00036 typedef typename mesh_type::vertex_handle vertex_handle;
00037 typedef typename mesh_type::size_type size_type;
00038
00039 mesh.clear();
00040
00041 if(vertices.empty())
00042 return;
00043
00044 triangulateio in, out, vorout;
00045 memset(&in, 0, sizeof(triangulateio));
00046 memset(&out, 0, sizeof(triangulateio));
00047 memset(&vorout, 0, sizeof(triangulateio));
00048
00049 in.numberofpoints = boost::numeric_cast<int>(vertices.size());
00050
00051 std::vector<REAL> input_points( in.numberofpoints*2 );
00052
00053 for(size_type i=0;i<vertices.size();++i)
00054 {
00055 real_type x = boost::numeric_cast<real_type>( vertices[i](0) );
00056 real_type y = boost::numeric_cast<real_type>( vertices[i](1) );
00057
00058 input_points[i*2 ] = boost::numeric_cast<REAL>( x );
00059 input_points[i*2+1] = boost::numeric_cast<REAL>( y );
00060 }
00061
00062 in.pointlist = &input_points[0];
00063
00064
00065 char switches[] = "zDQ";
00066 ::triangulate(switches, &in, &out, &vorout);
00067
00068 boost::vector_property_map<vertex_handle> handles;
00069
00070 for (int n = 0; n < out.numberofpoints; ++n)
00071 {
00072 vector3_type coord;
00073 coord[0] = boost::numeric_cast<real_type>( out.pointlist[n*2 ] );
00074 coord[1] = boost::numeric_cast<real_type>( out.pointlist[n*2+1] );
00075 coord[2] = value_traits::zero();
00076 vertex_handle h = mesh.add_vertex( coord );
00077 handles[n] = h;
00078 }
00079
00080 for (int n = 0; n < out.numberoftriangles; ++n)
00081 {
00082 int idx0 = out.trianglelist[n*3 ];
00083 int idx1 = out.trianglelist[n*3 +1 ];
00084 int idx2 = out.trianglelist[n*3 +2 ];
00085 mesh.add_face( handles[idx0],handles[idx1],handles[idx2] );
00086 }
00087
00088
00089 free(vorout.pointlist);
00090 free(vorout.pointattributelist);
00091 free(vorout.edgelist);
00092 free(vorout.normlist);
00093 free(out.pointlist);
00094 free(out.pointmarkerlist);
00095 free(out.trianglelist);
00096
00097 }
00098
00099 }
00100 }
00101
00102
00103 #endif