Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_T4MESH_UTIL_T4MESH_DELAUNAY_TETRAHEDRALIZATION_H
00002 #define OPENTISSUE_CORE_CONTAINERS_T4MESH_UTIL_T4MESH_DELAUNAY_TETRAHEDRALIZATION_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/utility/utility_qhull.h>
00013
00014 #include <iostream>
00015 #include <cassert>
00016 #include <map>
00017
00018 namespace OpenTissue
00019 {
00020 namespace t4mesh
00021 {
00022
00023
00035 template <typename Points, typename t4mesh_type>
00036 void delaunay_tetrahedralization( Points & points, t4mesh_type & output )
00037 {
00038 output.clear();
00039 int N = static_cast<int>( points.size() );
00040 if ( N == 0 )
00041 return ;
00042
00043 assert( N > 3 );
00044 int dim = 3;
00045 coordT *coords = new coordT[ dim * N ];
00046 std::map<unsigned int, typename t4mesh_type::node_iterator > nodeMap;
00047 int i = 0;
00048 int j = 0;
00049 for ( typename Points::iterator point = points.begin();point != points.end();++point )
00050 {
00051 typename t4mesh_type::node_iterator node = output.insert();
00052 node->m_coord = *point;
00053 coords[ j++ ] = ( *point )(0);
00054 coords[ j++ ] = ( *point )(1);
00055 coords[ j++ ] = ( *point )(2);
00056 nodeMap[ i++ ] = node;
00057 }
00058 boolT ismalloc = False;
00059 char flags[] = "qhull d QJ";
00060
00061
00062 FILE *outfile = 0;
00063 FILE *errfile = 0;
00064 int exitcode;
00065 exitcode = qh_new_qhull ( dim, N, coords, ismalloc, flags, outfile, errfile );
00066 if ( !exitcode )
00067 {
00068 facetT * facet;
00069 vertexT *vertex, **vertexp;
00070 FORALLfacets {
00071 if ( !facet->upperdelaunay )
00072 {
00073 assert( qh_setsize ( facet->vertices ) == 4 );
00074 typename t4mesh_type::node_iterator tmp[ 4 ];
00075 int j = 0;
00076 if ( !facet->toporient )
00077 {
00078
00079 FOREACHvertexreverse12_( facet->vertices )
00080 {
00081 int i = qh_pointid ( vertex->point );
00082 tmp[ j++ ] = nodeMap[ i ];
00083 }
00084 }
00085 else
00086 {
00087 FOREACHvertex_( facet->vertices )
00088 {
00089 int i = qh_pointid ( vertex->point );
00090 tmp[ j++ ] = nodeMap[ i ];
00091 }
00092 }
00093 output.insert( tmp[ 0 ], tmp[ 1 ], tmp[ 2 ], tmp[ 3 ] );
00094 }
00095 }
00096 }
00097 qh_freeqhull( !qh_ALL );
00098 int curlong, totlong;
00099 qh_memfreeshort ( &curlong, &totlong );
00100 if ( curlong || totlong )
00101 fprintf ( errfile, "qhull internal warning (main): did not free %d bytes of long memory (%d pieces)\n", totlong, curlong );
00102 delete [] coords;
00103
00104 std::cout << "Delaunay resulted in " << output.size_tetrahedra() << " tetrahedra with " << output.size_nodes() << " nodes" << std::endl;
00105 };
00106
00107
00114 template <typename mesh_type, typename t4mesh_type>
00115 void delaunay_from_surface_tetrahedralization( mesh_type const & input, t4mesh_type & output )
00116 {
00117 typedef typename mesh_type::math_types math_types;
00118 typedef typename math_types::vector3_type vector3_type;
00119
00120 std::vector< vector3_type > points;
00121
00122 output.clear();
00123
00124 if( input.size_vertices() <= 0 )
00125 return;
00126
00127 points.resize( input.size_vertices() );
00128 for( typename mesh_type::const_vertex_iterator v = input.vertex_begin();v!=input.vertex_end();++v)
00129 {
00130 points[v->get_handle().get_idx()] = v->m_coord;
00131 }
00132 delaunay_tetrahedralization( points, output);
00133 }
00134
00135 }
00136 }
00137
00138
00139 #endif