Go to the documentation of this file.00001 #ifndef OPENTISSUE_KINEMATICS_SKINNING_IO_SKINNING_XML_READ_H
00002 #define OPENTISSUE_KINEMATICS_SKINNING_IO_SKINNING_XML_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <TinyXML/tinyxml.h>
00013
00014 #include <list>
00015 #include <cassert>
00016 #include <string>
00017 #include <sstream>
00018 #include <iostream>
00019
00020 namespace OpenTissue
00021 {
00022 namespace skinning
00023 {
00024
00025 template<typename skin_type>
00026 bool xml_read(std::string const & filename,skin_type & skin)
00027 {
00028 typedef typename skin_type::vector3_type vector3_type;
00029 typedef typename skin_type::real_type real_type;
00030 typedef typename skin_type::vertex_handle vertex_handle;
00031 typedef typename skin_type::vertex_iterator vertex_iterator;
00032 typedef typename skin_type::face_handle face_handle;
00033 skin.clear();
00034 #ifdef TIXML_USE_STL
00035 TiXmlDocument xml_document(filename);
00036 #else
00037 TiXmlDocument xml_document(filename.c_str());
00038 #endif
00039 if(!xml_document.LoadFile())
00040 {
00041 std::cerr << "file: " << filename << " not found" << std::endl;
00042 return false;
00043 }
00044 TiXmlHandle document_handle( &xml_document );
00045
00046 TiXmlElement * xml_mesh = document_handle.FirstChild( "mesh" ).Element();
00047 if(xml_mesh)
00048 {
00049 skin.m_material_idx = -1;
00050 if(xml_mesh->Attribute("material"))
00051 {
00052 std::istringstream str_stream(xml_mesh->Attribute("material"));
00053 str_stream >> skin.m_material_idx;
00054 }
00055 }
00056 TiXmlElement * xml_vertex = document_handle.FirstChild( "mesh" ).FirstChild( "vertex" ).Element();
00057 for( ; xml_vertex; xml_vertex=xml_vertex->NextSiblingElement("vertex") )
00058 {
00059 vertex_handle h = skin.add_vertex();
00060 assert(!h.is_null() || !"xml_read(): Could not create vertex");
00061 vertex_iterator vertex = skin.get_vertex_iterator(h);
00062 if(xml_vertex->Attribute("idx"))
00063 {
00064 unsigned int idx;
00065 std::istringstream str_stream(xml_vertex->Attribute("idx"));
00066 str_stream >> idx;
00067 if(h.get_idx()!=idx)
00068 {
00069 std::cerr << "xml_read(): Error in vertex indices" << std::endl;
00070 }
00071 }
00072 if(xml_vertex->Attribute("p"))
00073 {
00074 vector3_type p;
00075 std::istringstream str_stream(xml_vertex->Attribute("p"));
00076 str_stream >> p;
00077 vertex->m_coord(0) = p(0);
00078 vertex->m_coord(1) = p(1);
00079 vertex->m_coord(2) = p(2);
00080 }
00081 if(xml_vertex->Attribute("n"))
00082 {
00083 vector3_type n;
00084 std::istringstream str_stream(xml_vertex->Attribute("n"));
00085 str_stream >> n;
00086 vertex->m_normal(0) = n(0);
00087 vertex->m_normal(1) = n(1);
00088 vertex->m_normal(2) = n(2);
00089 }
00090 if(xml_vertex->Attribute("c"))
00091 {
00092 vector3_type c;
00093 std::istringstream str_stream(xml_vertex->Attribute("c"));
00094 str_stream >> c;
00095 vertex->m_color(0) = c(0);
00096 vertex->m_color(1) = c(1);
00097 vertex->m_color(2) = c(2);
00098 }
00099 if(xml_vertex->Attribute("u"))
00100 {
00101 real_type u;
00102 std::istringstream str_stream(xml_vertex->Attribute("u"));
00103 str_stream >> u;
00104 vertex->m_u = u;
00105 }
00106 if(xml_vertex->Attribute("v"))
00107 {
00108 real_type v;
00109 std::istringstream str_stream(xml_vertex->Attribute("v"));
00110 str_stream >> v;
00111 vertex->m_v = v;
00112 }
00113
00114 vertex->m_original_coord = vertex->m_coord;
00115 vertex->m_original_normal = vertex->m_normal;
00116
00117 vertex->m_influences = 0;
00118 if(xml_vertex->Attribute("influences"))
00119 {
00120 std::istringstream str_stream(xml_vertex->Attribute("influences"));
00121 str_stream >> vertex->m_influences;
00122 }
00123
00124
00125
00126 for(unsigned int i = 0; i<4; ++i)
00127 {
00128 vertex->m_bone[i] = -1;
00129 vertex->m_weight[i] = 0;
00130 }
00131
00132 TiXmlElement * xml_influence = xml_vertex->FirstChildElement( "influence" );
00133 for(unsigned int cnt = 0 ; xml_influence; xml_influence=xml_influence->NextSiblingElement("influence"),++cnt )
00134 {
00135 if(cnt>=6)
00136 {
00137 std::cout << "xml_read(): exceeded available space for influences" << std::endl;
00138 break;
00139 }
00140
00141
00142
00143 if(xml_influence->Attribute("bone"))
00144 {
00145 std::istringstream str_stream(xml_influence->Attribute("bone"));
00146 str_stream >> vertex->m_bone[cnt];
00147 }
00148 if(xml_influence->Attribute("weight"))
00149 {
00150 std::istringstream str_stream(xml_influence->Attribute("weight"));
00151 str_stream >> vertex->m_weight[cnt];
00152 }
00153 }
00154 }
00155 TiXmlElement * xml_face = document_handle.FirstChild( "mesh" ).FirstChild( "face" ).Element();
00156 for( ; xml_face; xml_face=xml_face->NextSiblingElement("face") )
00157 {
00158 if(!xml_face->Attribute("vertices"))
00159 {
00160 std::cerr << "xml_read(): Error missing vertices in face tag" << std::endl;
00161 }
00162 std::istringstream str_stream(xml_face->Attribute("vertices"));
00163 std::list<vertex_handle> handles;
00164 while(!str_stream.eof())
00165 {
00166 unsigned int idx;
00167 str_stream >> idx;
00168 vertex_handle vh = skin.get_vertex_handle( idx );
00169 assert(!vh.is_null() || !"xml_read(): Could not find vertex");
00170 handles.push_back( vh );
00171 }
00172 face_handle h = skin.add_face(handles.begin(),handles.end());
00173 assert(!h.is_null() || !"xml_read() : Internal error, could not create face");
00174 }
00175 xml_document.Clear();
00176 return true;
00177 }
00178
00179 }
00180
00181 }
00182
00183
00184 #endif