Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_T4MESH_IO_T4MESH_XML_READ_H
00002 #define OPENTISSUE_CORE_CONTAINERS_T4MESH_IO_T4MESH_XML_READ_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
00016 #include <OpenTissue/utility/utility_tag_traits.h>
00017
00018 #include <TinyXML/tinyxml.h>
00019
00020 #include <cassert>
00021 #include <iostream>
00022 #include <fstream>
00023 #include <sstream>
00024
00025 namespace OpenTissue
00026 {
00027 namespace t4mesh
00028 {
00029
00042 template<typename point_container,typename t4mesh_type>
00043 bool xml_read(std::string const & filename, t4mesh_type & mesh, point_container & points)
00044 {
00045 typedef typename point_container::value_type vector3_type;
00046 typedef typename t4mesh_type::node_iterator node_iterator;
00047 typedef typename t4mesh_type::node_iterator const_node_iterator;
00048 typedef typename t4mesh_type::tetrahedron_iterator tetrahedron_iterator;
00049 typedef typename t4mesh_type::tetrahedron_iterator const_tetrahedron_iterator;
00050
00051 mesh.clear();
00052
00053 #ifdef TIXML_USE_STL
00054 TiXmlDocument xml_document(filename);
00055 #else
00056 TiXmlDocument xml_document(filename.c_str());
00057 #endif
00058
00059 if(!xml_document.LoadFile())
00060 {
00061 std::cerr << "file not found" << std::endl;
00062 return false;
00063 }
00064 TiXmlHandle document_handle( &xml_document );
00065
00066 TiXmlElement * xml_t4mesh = document_handle.FirstChild( "T4MESH" ).Element();
00067 assert(xml_t4mesh || !"Oh no, could not find a T4MESH tag?");
00068
00069 int cnt_nodes = 0;
00070 if(xml_t4mesh->Attribute("nodes"))
00071 {
00072 std::istringstream str_stream(xml_t4mesh->Attribute("nodes"));
00073 str_stream >> cnt_nodes;
00074 }
00075 assert(cnt_nodes>0 || !"Node count was not positive");
00076
00077 for(int i=0;i< cnt_nodes;++i)
00078 mesh.insert();
00079
00080 TiXmlElement * xml_tetrahedron = document_handle.FirstChild( "T4MESH" ).FirstChild( "TETRAHEDRON" ).Element();
00081 for( ; xml_tetrahedron; xml_tetrahedron=xml_tetrahedron->NextSiblingElement("TETRAHEDRON") )
00082 {
00083 int idx0=-1,idx1=-1,idx2=-1,idx3=-1;
00084
00085 if(!xml_tetrahedron->Attribute("i"))
00086 {
00087 std::cerr << "t4mesh::xml_read(): Missing i index on tetrahedron" << std::endl;
00088 return false;
00089 }
00090 else
00091 {
00092 std::istringstream str_stream(xml_tetrahedron->Attribute("i"));
00093 str_stream >> idx0;
00094 }
00095 if(!xml_tetrahedron->Attribute("j"))
00096 {
00097 std::cerr << "t4mesh::xml_read(): Missing j index on tetrahedron" << std::endl;
00098 return false;
00099 }
00100 else
00101 {
00102 std::istringstream str_stream(xml_tetrahedron->Attribute("j"));
00103 str_stream >> idx1;
00104 }
00105 if(!xml_tetrahedron->Attribute("k"))
00106 {
00107 std::cerr << "t4mesh::xml_read(): Missing k index on tetrahedron" << std::endl;
00108 return false;
00109 }
00110 else
00111 {
00112 std::istringstream str_stream(xml_tetrahedron->Attribute("k"));
00113 str_stream >> idx2;
00114 }
00115 if(!xml_tetrahedron->Attribute("m"))
00116 {
00117 std::cerr << "t4mesh::xml_read(): Missing m index on tetrahedron" << std::endl;
00118 return false;
00119 }
00120 else
00121 {
00122 std::istringstream str_stream(xml_tetrahedron->Attribute("m"));
00123 str_stream >> idx3;
00124 }
00125 if((idx0<0 ||idx1<0 ||idx2<0 ||idx3<0)||(idx0>=cnt_nodes ||idx1>=cnt_nodes ||idx2>=cnt_nodes ||idx3>=cnt_nodes))
00126 {
00127 std::cerr << "t4mesh::xml_read(): Illegal node index on tetrahedron" << std::endl;
00128 return false;
00129 }
00130 tetrahedron_iterator T = mesh.insert(idx0,idx1,idx2,idx3);
00131
00132
00133 int tag;
00134 if(xml_tetrahedron->Attribute("tag"))
00135 {
00136 std::istringstream str_stream(xml_tetrahedron->Attribute("tag"));
00137 str_stream >> tag;
00138 OpenTissue::utility::set_tag(*T, tag);
00139 }
00140
00141 }
00142
00143
00144 points.resize(cnt_nodes);
00145
00146 TiXmlElement * xml_point = document_handle.FirstChild( "T4MESH" ).FirstChild( "POINT" ).Element();
00147 for( ; xml_point; xml_point=xml_point->NextSiblingElement("POINT") )
00148 {
00149 int idx;
00150
00151 if(!xml_point->Attribute("idx"))
00152 {
00153 std::cerr << "t4mesh::xml_read(): Missing index on point" << std::endl;
00154 return false;
00155 }
00156 else
00157 {
00158 std::istringstream str_stream(xml_point->Attribute("idx"));
00159 str_stream >> idx;
00160 }
00161 if(idx<0 || idx>=cnt_nodes)
00162 {
00163 std::cerr << "t4mesh::xml_read(): Illegal index on point" << std::endl;
00164 return false;
00165 }
00166
00167 vector3_type value;
00168
00169 if(!xml_point->Attribute("coord"))
00170 {
00171 std::cerr << "t4mesh::xml_read(): Missing coord on point" << std::endl;
00172 return false;
00173 }
00174 else
00175 {
00176 std::istringstream str_stream(xml_point->Attribute("coord"));
00177 str_stream >> value;
00178 }
00179
00180 assert(is_number(value(0)) || !"First coordinate was not a number");
00181 assert(is_finite(value(0)) || !"First coordinate was not finite");
00182 assert(is_number(value(1)) || !"Second coordinate was not a number");
00183 assert(is_finite(value(1)) || !"Second coordinate was not finite");
00184 assert(is_number(value(2)) || !"Third coordinate was not a number");
00185 assert(is_finite(value(2)) || !"Third coordinate was not finite");
00186
00187 points[idx](0) = value(0);
00188 points[idx](1) = value(1);
00189 points[idx](2) = value(2);
00190
00191
00192 int tag;
00193 if(xml_point->Attribute("tag"))
00194 {
00195 std::istringstream str_stream(xml_point->Attribute("tag"));
00196 str_stream >> tag;
00197 OpenTissue::utility::set_tag(*mesh.node(idx), tag);
00198 }
00199 }
00200
00201 xml_document.Clear();
00202 return true;
00203 };
00204
00213 inline bool xml_has_tags(std::string const & filename)
00214 {
00215 #ifdef TIXML_USE_STL
00216 TiXmlDocument xml_document(filename);
00217 #else
00218 TiXmlDocument xml_document(filename.c_str());
00219 #endif
00220
00221 if(!xml_document.LoadFile())
00222 {
00223 std::cerr << "file not found" << std::endl;
00224 return false;
00225 }
00226 TiXmlHandle document_handle( &xml_document );
00227
00228 TiXmlElement * xml_t4mesh = document_handle.FirstChild( "T4MESH" ).Element();
00229 assert(xml_t4mesh || !"Oh no, could not find a T4MESH tag?");
00230
00231 bool has_tags = false;
00232
00233 TiXmlElement * xml_point = document_handle.FirstChild( "T4MESH" ).FirstChild( "POINT" ).Element();
00234 for( ; xml_point; xml_point=xml_point->NextSiblingElement("POINT") )
00235 {
00236 if(xml_point->Attribute("tag"))
00237 {
00238 has_tags = true;
00239 break;
00240 }
00241 }
00242
00243 xml_document.Clear();
00244 return has_tags;
00245 }
00246
00257 template<typename t4mesh_type>
00258 bool xml_read(std::string const & filename,t4mesh_type & mesh)
00259 {
00260 default_point_container<t4mesh_type> points(&mesh);
00261 return xml_read(filename, mesh, points);
00262 }
00263
00264 }
00265 }
00266
00267
00268 #endif