Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_POLYMESH_UTIL_POLYMESH_SUBDIVIDE_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_POLYMESH_UTIL_POLYMESH_SUBDIVIDE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/containers/mesh/polymesh/polymesh_face.h>
00013
00014 #include <map>
00015
00016 namespace OpenTissue
00017 {
00018 namespace polymesh
00019 {
00020
00066 template< typename mesh_type >
00067 unsigned int subdivide( mesh_type & mesh, double max_area = 0.01 )
00068 {
00069 typedef typename mesh_type::vertex_handle vertex_handle;
00070 typedef typename mesh_type::edge_handle edge_handle;
00071
00072 typedef typename mesh_type::face_iterator face_iterator;
00073 typedef typename mesh_type::face_vertex_circulator face_vertex_circulator;
00074 typedef typename mesh_type::face_halfedge_circulator face_halfedge_circulator;
00075 typedef typename mesh_type::halfedge_iterator halfedge_iterator;
00076 typedef typename mesh_type::vertex_iterator vertex_iterator;
00077 typedef typename mesh_type::edge_iterator edge_iterator;
00078
00079 typedef typename mesh_type::math_types math_types;
00080 typedef typename math_types::value_traits value_traits;
00081 typedef typename math_types::vector3_type vector3_type;
00082 typedef typename math_types::real_type real_type;
00083
00084 std::map<edge_handle, vertex_handle> lut;
00085
00086 real_type max_area_sqr = static_cast<real_type>( max_area*max_area );
00087
00088 unsigned int cnt = 0;
00089
00090 for(face_iterator f = mesh.face_begin();f!=mesh.face_end();++f)
00091 {
00092 face_halfedge_circulator h0(*f);
00093 face_halfedge_circulator h1(*f);++h1;
00094 face_halfedge_circulator h2(*f);--h2;
00095
00096 edge_handle e0 = h0->get_edge_handle();
00097 edge_handle e1 = h1->get_edge_handle();
00098 edge_handle e2 = h2->get_edge_handle();
00099
00100 vertex_handle v0 = h0->get_origin_handle();
00101 vertex_handle v1 = h1->get_origin_handle();
00102 vertex_handle v2 = h2->get_origin_handle();
00103
00104 vector3_type p0 = h0->get_origin_iterator()->m_coord;
00105 vector3_type p1 = h1->get_origin_iterator()->m_coord;
00106 vector3_type p2 = h2->get_origin_iterator()->m_coord;
00107
00108 vector3_type u1 = p2 - p1;
00109 vector3_type u2 = p1 - p0;
00110 vector3_type u1xu2 = u1 % u2;
00111 real_type area_sqr = u1xu2*u1xu2;
00112 if(area_sqr > max_area_sqr)
00113 {
00114 ++cnt;
00115 if(lut.find(e0) == lut.end())
00116 lut[e0] = mesh.add_vertex((p0+p1)*.5);
00117 if(lut.find(e1) == lut.end())
00118 lut[e1] = mesh.add_vertex((p1+p2)*.5);
00119 if(lut.find(e2) == lut.end())
00120 lut[e2] = mesh.add_vertex((p2+p0)*.5);
00121 }
00122 }
00123
00124 for(face_iterator f = mesh.face_begin();f!=mesh.face_end();)
00125 {
00126 face_halfedge_circulator h0(*f);
00127 face_halfedge_circulator h1(*f);++h1;
00128 face_halfedge_circulator h2(*f);--h2;
00129
00130 edge_handle e0 = h0->get_edge_handle();
00131 edge_handle e1 = h1->get_edge_handle();
00132 edge_handle e2 = h2->get_edge_handle();
00133
00134 vertex_handle v0 = h0->get_origin_handle();
00135 vertex_handle v1 = h1->get_origin_handle();
00136 vertex_handle v2 = h2->get_origin_handle();
00137
00138 vertex_handle v01 = lut[e0];
00139 vertex_handle v12 = lut[e1];
00140 vertex_handle v20 = lut[e2];
00141
00142 if( !v01.is_null() && v12.is_null() && v20.is_null())
00143 {
00144 face_iterator tmp = f;++f;
00145 mesh.remove_face( tmp->get_handle());
00146 mesh.add_face( v0, v01, v2 );
00147 mesh.add_face( v01, v1, v2 );
00148 }
00149 else if( v01.is_null() && !v12.is_null() && v20.is_null())
00150 {
00151 face_iterator tmp = f;++f;
00152 mesh.remove_face( tmp->get_handle());
00153 mesh.add_face( v0, v1, v12 );
00154 mesh.add_face( v0, v12, v2 );
00155 }
00156 else if( v01.is_null() && v12.is_null() && !v20.is_null())
00157 {
00158 face_iterator tmp = f;++f;
00159 mesh.remove_face( tmp->get_handle());
00160 mesh.add_face( v0, v1, v20);
00161 mesh.add_face( v1, v2, v20);
00162 }
00163 else if( v01.is_null() && !v12.is_null() && !v20.is_null())
00164 {
00165 face_iterator tmp = f;++f;
00166 mesh.remove_face( tmp->get_handle());
00167 mesh.add_face( v2, v20, v12 );
00168 mesh.add_face( v0, v1, v20 );
00169 mesh.add_face( v20, v1, v12 );
00170 }
00171 else if( !v01.is_null() && v12.is_null() && !v20.is_null())
00172 {
00173 face_iterator tmp = f;++f;
00174 mesh.remove_face( tmp->get_handle());
00175 mesh.add_face( v0, v01, v20 );
00176 mesh.add_face( v01, v1, v2 );
00177 mesh.add_face( v2, v20, v01 );
00178 }
00179 else if( !v01.is_null() && !v12.is_null() && v20.is_null())
00180 {
00181 face_iterator tmp = f;++f;
00182 mesh.remove_face( tmp->get_handle());
00183 mesh.add_face( v1, v12, v01 );
00184 mesh.add_face( v0, v01, v2 );
00185 mesh.add_face( v01, v12, v2 );
00186 }
00187 else if( !v01.is_null() && !v12.is_null() && !v20.is_null())
00188 {
00189 face_iterator tmp = f;++f;
00190 mesh.remove_face( tmp->get_handle());
00191 mesh.add_face( v0, v01, v20 );
00192 mesh.add_face( v1, v12, v01 );
00193 mesh.add_face( v2, v20, v12 );
00194 mesh.add_face( v01, v12, v20 );
00195 }
00196 else
00197 {
00198 ++f;
00199 }
00200 }
00201 return cnt;
00202 }
00203
00204 }
00205 }
00206
00207
00208 #endif