Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_UTIL_MESH_CONVEX_HULL_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_UTIL_MESH_CONVEX_HULL_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/utility/utility_qhull.h>
00013
00014 namespace OpenTissue
00015 {
00016 namespace mesh
00017 {
00018
00019
00020 template<
00021 typename mesh_type
00022 , typename vector3_iterator
00023 >
00024 void convex_hull(vector3_iterator begin, vector3_iterator end, mesh_type & mesh,bool print_summary = false)
00025 {
00026 typedef typename mesh_type::math_types math_types;
00027 typedef typename math_types::value_traits value_traits;
00028 typedef typename math_types::vector3_type vector3_type;
00029 typedef typename math_types::real_type real_type;
00030
00031 typedef typename mesh_type::vertex_handle vertex_handle;
00032
00033 mesh.clear();
00034 size_t N = std::distance(begin,end);
00035 int dim = 3;
00036 coordT *coords = new coordT[dim*N];
00037 boolT ismalloc = False;
00038 char flags[] = "qhull QJ";
00039 FILE *outfile = print_summary ? stdout : 0;
00040 FILE *errfile = stderr;
00041
00042
00043 int exitcode;
00044 int j = 0;
00045 for(vector3_iterator p=begin;p!=end;++p)
00046 {
00047 coords[j++] = (*p)(0);
00048 coords[j++] = (*p)(1);
00049 coords[j++] = (*p)(2);
00050 }
00051 std::vector<vertex_handle> handles(N);
00052 int iN = boost::numeric_cast<int,size_t>(N);
00053 exitcode= qh_new_qhull (dim, iN, coords, ismalloc, flags, outfile, errfile);
00054 if(!exitcode)
00055 {
00056 facetT *facet;
00057 vertexT *vertex, **vertexp;
00058 qh visit_id++;
00059 FORALLfacets
00060 {
00061 facet->visitid = qh visit_id;
00062 std::list<vertex_handle> tmp;
00063 FOREACHvertex_(facet->vertices)
00064 {
00065 int idx = qh_pointid(vertex->point);
00066 if(handles[idx].is_null())
00067 handles[idx] = mesh.add_vertex( vector3_type( vertex->point[0],vertex->point[1],vertex->point[2] ) );
00068 tmp.push_back(handles[idx]);
00069 }
00070 if(facet->toporient)
00071 tmp.reverse();
00072 mesh.add_face(tmp.begin(),tmp.end());
00073 }
00074 }
00075 qh_freeqhull(!qh_ALL);
00076 int curlong, totlong;
00077 qh_memfreeshort (&curlong, &totlong);
00078 if (curlong || totlong)
00079 fprintf (errfile, "qhull internal warning (main): did not free %d bytes of long memory (%d pieces)\n", totlong, curlong);
00080 delete [] coords;
00081 }
00082
00083 }
00084 }
00085
00086
00087 #endif