Go to the documentation of this file.00001 #ifndef OPENTISSUE_DYNAMICS_MBD_UTIL_MBD_MEL_GEOMETRY_STRING_H
00002 #define OPENTISSUE_DYNAMICS_MBD_UTIL_MBD_MEL_GEOMETRY_STRING_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include<string>
00013 #include<sstream>
00014
00015 namespace OpenTissue
00016 {
00017 namespace mbd
00018 {
00019 namespace mel
00020 {
00021
00022 namespace detail
00023 {
00024
00025 template<typename mesh_type,typename index_type>
00026 void polygon_geometry(std::stringstream & stream,mesh_type * mesh, index_type idx)
00027 {
00028 typedef typename mesh_type::face_iterator face_iterator;
00029 typedef typename mesh_type::vertex_type vertex_type;
00030 typedef typename mesh_type::halfedge_type halfedge_type;
00031
00032
00033 int unused = static_cast<int>(mesh->size_vertices() +1);
00034
00035 mesh::clear_vertex_tags(*mesh, unused);
00036 int cnt = 0;
00037
00038 bool firstFace = true;
00039 for(face_iterator face = mesh->face_begin();face!=mesh->face_end();++face)
00040 {
00041 assert( valency(*face)==3);
00042
00043 typename mesh_type::face_halfedge_circulator h(*face);
00044 halfedge_type * edge0 = &(*h);++h;
00045 halfedge_type * edge1 = &(*h);++h;
00046 halfedge_type * edge2 = &(*h);
00047
00048 assert(edge0->get_face_handle() == edge1->get_face_handle());
00049 assert(edge0->get_face_handle() == edge2->get_face_handle());
00050
00051 vertex_type * v0 = &(*edge0->get_origin_iterator());
00052 vertex_type * v1 = &(*edge1->get_origin_iterator());
00053 vertex_type * v2 = &(*edge2->get_origin_iterator());
00054
00055 if(v0->m_tag==unused && v1->m_tag==unused && v2->m_tag==unused)
00056 {
00057 v0->m_tag = cnt++;
00058 v1->m_tag = cnt++;
00059 v2->m_tag = cnt++;
00060 if(firstFace)
00061 {
00062 stream << "polyCreateFacet -ch off"
00063 << " -p " << v0->m_coord(0) << " " << v0->m_coord(1) << " "<< v0->m_coord(2)
00064 << " -p " << v1->m_coord(0) << " " << v1->m_coord(1) << " "<< v1->m_coord(2)
00065 << " -p " << v2->m_coord(0) << " " << v2->m_coord(1) << " "<< v2->m_coord(2)
00066 << " -n body" << idx
00067 << ";" << std::endl;
00068 firstFace = false;
00069 }
00070 else
00071 {
00072 stream << "polyAppendVertex -ch off"
00073 << " -p " << v0->m_coord(0) << " " << v0->m_coord(1) << " "<< v0->m_coord(2)
00074 << " -p " << v1->m_coord(0) << " " << v1->m_coord(1) << " "<< v1->m_coord(2)
00075 << " -p " << v2->m_coord(0) << " " << v2->m_coord(1) << " "<< v2->m_coord(2)
00076 << ";" << std::endl;
00077 }
00078 }
00079
00081 else if(v0->m_tag!=unused && v1->m_tag==unused && v2->m_tag==unused)
00082 {
00083 v1->m_tag = cnt++;
00084 v2->m_tag = cnt++;
00085 stream << "polyAppendVertex -ch off"
00086 << " -v " << v0->m_tag
00087 << " -p " << v1->m_coord(0) << " " << v1->m_coord(1) << " "<< v1->m_coord(2)
00088 << " -p " << v2->m_coord(0) << " " << v2->m_coord(1) << " "<< v2->m_coord(2)
00089 << ";" << std::endl;
00090 }
00092 else if(v0->m_tag==unused && v1->m_tag!=unused && v2->m_tag==unused)
00093 {
00094 v0->m_tag = cnt++;
00095 v2->m_tag = cnt++;
00096 stream << "polyAppendVertex -ch off"
00097 << " -p " << v0->m_coord(0) << " " << v0->m_coord(1) << " "<< v0->m_coord(2)
00098 << " -v " << v1->m_tag
00099 << " -p " << v2->m_coord(0) << " " << v2->m_coord(1) << " "<< v2->m_coord(2)
00100 << ";" << std::endl;
00101 }
00103 else if(v0->m_tag==unused && v1->m_tag==unused && v2->m_tag!=unused)
00104 {
00105 v0->m_tag = cnt++;
00106 v1->m_tag = cnt++;
00107 stream << "polyAppendVertex -ch off"
00108 << " -p " << v0->m_coord(0) << " " << v0->m_coord(1) << " "<< v0->m_coord(2)
00109 << " -p " << v1->m_coord(0) << " " << v1->m_coord(1) << " "<< v1->m_coord(2)
00110 << " -v " << v2->m_tag
00111 << ";" << std::endl;
00112 }
00114 else if(v0->m_tag!=unused && v1->m_tag!=unused && v2->m_tag==unused)
00115 {
00116 v2->m_tag = cnt++;
00117 stream << "polyAppendVertex -ch off"
00118 << " -v " << v0->m_tag
00119 << " -v " << v1->m_tag
00120 << " -p " << v2->m_coord(0) << " " << v2->m_coord(1) << " "<< v2->m_coord(2)
00121 << ";" << std::endl;
00122 }
00123 else if(v0->m_tag==unused && v1->m_tag!=unused && v2->m_tag!=unused)
00124 {
00125 v0->m_tag = cnt++;
00126 stream << "polyAppendVertex -ch off"
00127 << " -p " << v0->m_coord(0) << " " << v0->m_coord(1) << " "<< v0->m_coord(2)
00128 << " -v " << v1->m_tag
00129 << " -v " << v2->m_tag
00130 << ";" << std::endl;
00131 }
00132 else if(v0->m_tag!=unused && v1->m_tag==unused && v2->m_tag!=unused)
00133 {
00134 v1->m_tag = cnt++;
00135 stream << "polyAppendVertex -ch off"
00136 << " -v " << v0->m_tag
00137 << " -p " << v1->m_coord(0) << " " << v1->m_coord(1) << " "<< v1->m_coord(2)
00138 << " -v " << v2->m_tag
00139 << ";" << std::endl;
00140 }
00141 else if(v0->m_tag!=unused && v1->m_tag!=unused && v2->m_tag!=unused)
00142 {
00143 stream << "polyAppendVertex -ch off"
00144 << " -v " << v0->m_tag
00145 << " -v " << v1->m_tag
00146 << " -v " << v2->m_tag
00147 << ";" << std::endl;
00148 }
00149 }
00150 stream << std::endl;
00151 stream << std::endl;
00152 }
00153
00154
00155
00156 template<typename sphere_type, typename index_type>
00157 void sphere_geometry(std::stringstream & stream,sphere_type const & sphere,index_type idx)
00158 {
00159 stream << "polySphere -r " << sphere.radius() << " -ch 0 -n body" << idx << ";" << std::endl;
00160 }
00161
00162 template<typename box_type, typename index_type>
00163 void box_geometry(std::stringstream & stream,box_type const & box,index_type idx)
00164 {
00165 stream << " polyCube "
00166 << "-w " << 2.*box.ext()(0) << " "
00167 << "-h " << 2.*box.ext()(1) << " "
00168 << "-d " << 2.*box.ext()(2) << " "
00169 << "-ch 0 -n body" << idx << ";" <<std::endl;
00170 }
00171
00172 template<typename plane_type, typename index_type>
00173 void plane_geometry(std::stringstream & stream, plane_type const & , index_type idx)
00174 {
00175 stream << "polyPlane "
00176 << "-w " << 1000 << " "
00177 << "-h " << 1000 << " "
00178 << "-ch 0 -n body" << idx << ";" << std::endl;
00179 }
00180
00181 }
00182
00190 template< typename indirect_body_iterator>
00191 std::string geometry_string(indirect_body_iterator begin, indirect_body_iterator end)
00192 {
00193 typedef typename indirect_body_iterator::value_type body_type;
00194
00195 typedef typename body_type::math_policy math_policy;
00196
00197 typedef OpenTissue::geometry::Sphere<math_policy> sphere_type;
00198 typedef OpenTissue::geometry::Plane<math_policy> plane_type;
00199 typedef OpenTissue::geometry::OBB<math_policy> box_type;
00200 typedef OpenTissue::polymesh::PolyMesh<math_policy> mesh_type;
00201 typedef OpenTissue::grid::Grid<float,math_policy> grid_type;
00202 typedef OpenTissue::sdf::Geometry<mesh_type,grid_type> sdf_geometry_type;
00203
00204
00205 std::stringstream stream;
00206 for(indirect_body_iterator body=begin;body!=end;++body)
00207 {
00208
00209
00210
00211
00212
00213 if(body->get_geometry()->class_id()==sdf_geometry_type::id() )
00214 {
00215 sdf_geometry_type * sdf = static_cast<sdf_geometry_type*>(body->get_geometry() );
00216 detail::polygon_geometry(stream,&(sdf->m_mesh),body->get_index());
00217 }
00218 else if(body->get_geometry()->class_id()==box_type::id() )
00219 {
00220 box_type* box = static_cast<box_type *>(body->get_geometry() );
00221 detail::box_geometry(stream,*box,body->get_index());
00222 }
00223 else if(body->get_geometry()->class_id()==sphere_type::id() )
00224 {
00225 sphere_type* sphere = static_cast<sphere_type *>(body->get_geometry() );
00226 detail::sphere_geometry(stream,*sphere,body->get_index());
00227 }
00228 else if(body->get_geometry()->class_id()==plane_type::id() )
00229 {
00230 plane_type* plane = static_cast<plane_type *>(body->get_geometry() );
00231 detail::plane_geometry(stream,*plane,body->get_index());
00232 }
00233 else
00234 {
00235 assert(!"mbd::mel::geometry_string(): geometry type not handled, sorry");
00236 }
00237 }
00238 return stream.str();
00239 }
00240
00241 }
00242 }
00243 }
00244
00245
00246 #endif