Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_T4MESH_IO_T4MESH_XML_WRITE_H
00002 #define OPENTISSUE_CORE_CONTAINERS_T4MESH_IO_T4MESH_XML_WRITE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_vector3.h>
00013 #include <OpenTissue/core/math/math_is_finite.h>
00014 #include <OpenTissue/core/math/math_is_number.h>
00015 #include <OpenTissue/utility/utility_tag_traits.h>
00016
00017 #include <TinyXML/tinyxml.h>
00018
00019 #include <boost/type_traits.hpp>
00020
00021 #include <cassert>
00022 #include <iostream>
00023 #include <fstream>
00024 #include <sstream>
00025
00026 namespace OpenTissue
00027 {
00028 namespace t4mesh
00029 {
00030
00070 template<typename point_container,typename t4mesh_type>
00071 bool xml_make_doc(TiXmlDocument & doc, t4mesh_type const & mesh, point_container & points)
00072 {
00073 typedef typename point_container::value_type vector3_type;
00074 typedef typename t4mesh_type::node_iterator node_iterator;
00075 typedef typename t4mesh_type::node_iterator const_node_iterator;
00076 typedef typename t4mesh_type::tetrahedron_iterator tetrahedron_iterator;
00077 typedef typename t4mesh_type::tetrahedron_iterator const_tetrahedron_iterator;
00078
00079 assert(points.size()==mesh.size_nodes() || !"node size mismatch between point container and mesh");
00080
00081 TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
00082 TiXmlElement * meshelem = new TiXmlElement( "T4MESH" );
00083 meshelem->SetAttribute( "nodes", mesh.size_nodes() );
00084
00085 doc.LinkEndChild(decl);
00086 doc.LinkEndChild(meshelem);
00087
00088 for(const_tetrahedron_iterator tetrahedron=mesh.tetrahedron_begin();tetrahedron!=mesh.tetrahedron_end();++tetrahedron)
00089 {
00090 TiXmlElement * elem = new TiXmlElement( "TETRAHEDRON" );
00091 elem->SetAttribute( "i", tetrahedron->i()->idx() );
00092 elem->SetAttribute( "j", tetrahedron->j()->idx() );
00093 elem->SetAttribute( "k", tetrahedron->k()->idx() );
00094 elem->SetAttribute( "m", tetrahedron->m()->idx() );
00095
00096 if (OpenTissue::utility::has_tag(*tetrahedron))
00097 elem->SetAttribute( "tag", OpenTissue::utility::tag_value(*tetrahedron));
00098
00099 meshelem->LinkEndChild( elem );
00100 }
00101
00102 for(unsigned int i=0;i<mesh.size_nodes();++i)
00103 {
00104 vector3_type coord;
00105 coord(0) = points[i](0);
00106 coord(1) = points[i](1);
00107 coord(2) = points[i](2);
00108
00109 TiXmlElement * elem = new TiXmlElement( "POINT" );
00110
00111 std::stringstream s;
00112 s.precision(15);
00113 s << coord;
00114 elem->SetAttribute( "idx", i );
00115 elem->SetAttribute( "coord", s.str() );
00116 if (OpenTissue::utility::has_tag(*mesh.const_node(i)))
00117 elem->SetAttribute( "tag", OpenTissue::utility::tag_value(*mesh.const_node(i)));
00118
00119 meshelem->LinkEndChild( elem );
00120 }
00121
00122 return true;
00123 }
00124
00135 template<typename point_container,typename t4mesh>
00136 bool xml_write(std::string const & filename,t4mesh const & mesh,point_container & points)
00137 {
00138 assert(points.size()==mesh.size_nodes() || !"node size mismatch between point container and input mesh");
00139
00140
00141 TiXmlDocument doc;
00142 if (!OpenTissue::t4mesh::xml_make_doc(doc, mesh, points))
00143 return false;
00144
00145
00146 #ifdef TIXML_USE_STL
00147 doc.SaveFile(filename);
00148 #else
00149 doc.SaveFile(filename.c_str());
00150 #endif
00151
00152 return true;
00153 }
00154
00165 template<typename t4mesh_type>
00166 bool xml_write(std::string const & filename,t4mesh_type const & mesh)
00167 {
00168 default_point_container<t4mesh_type> points(const_cast<t4mesh_type*>(&mesh));
00169 return xml_write(filename,mesh,points);
00170 }
00171
00172 }
00173 }
00174
00175
00176 #endif