Go to the documentation of this file.00001 #ifndef OPENTISSUE_KINEMATICS_SKELETON_IO_SKELETON_XML_READ_H
00002 #define OPENTISSUE_KINEMATICS_SKELETON_IO_SKELETON_XML_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <TinyXML/tinyxml.h>
00013
00014 namespace OpenTissue
00015 {
00016 namespace skeleton
00017 {
00018 namespace detail
00019 {
00020
00021
00031 template<typename skeleton_type>
00032 bool xml_read(TiXmlHandle & document_handle, skeleton_type & skeleton)
00033 {
00034 typedef typename skeleton_type::math_types::coordsys_type coordsys_type;
00035 typedef typename skeleton_type::bone_type bone_type;
00036 typedef typename bone_type::bone_traits bone_traits;
00037
00038 skeleton.clear();
00039
00040 TiXmlElement * xml_bone = document_handle.FirstChild( "skeleton" ).FirstChild( "bone" ).Element();
00041 for( ; xml_bone; xml_bone=xml_bone->NextSiblingElement("bone") )
00042 {
00043 bone_type * bone = 0;
00044
00045 if(xml_bone->Attribute("parent"))
00046 {
00047 std::string parent_name = xml_bone->Attribute("parent");
00048 bone_type * parent = skeleton.get_bone(parent_name);
00049 if(!parent)
00050 {
00051 std::cerr << "Could not find parent!" << std::endl;
00052 return false;
00053 }
00054 bone = skeleton.create_bone(parent);
00055 }
00056 else
00057 {
00058 bone = skeleton.create_bone();
00059 }
00060 if(xml_bone->Attribute("name"))
00061 {
00062 std::string name = xml_bone->Attribute("name");
00063 bone->set_name(name);
00064 }
00065 if(xml_bone->Attribute("bindpose"))
00066 {
00067 coordsys_type bind_pose;
00068 std::istringstream str_stream(xml_bone->Attribute("bindpose"));
00069 str_stream >> bind_pose;
00070 bone->bind_pose() = bone_traits::convert( bind_pose );
00071 }
00072 if(xml_bone->Attribute("bonespace"))
00073 {
00074 coordsys_type bone_space;
00075 std::istringstream str_stream(xml_bone->Attribute("bonespace"));
00076 str_stream >> bone_space;
00077 bone->bone_space() = bone_traits::convert( bone_space );
00078 }
00079 }
00080
00081 skeleton.set_bind_pose();
00082
00083 return true;
00084 }
00085
00086 }
00087
00097 template<typename skeleton_type>
00098 bool xml_read(std::string const & filename,skeleton_type & skeleton)
00099 {
00100
00101 #ifdef TIXML_USE_STL
00102 TiXmlDocument xml_document(filename);
00103 #else
00104 TiXmlDocument xml_document(filename.c_str());
00105 #endif
00106
00107 if(!xml_document.LoadFile())
00108 {
00109 std::cerr << "file not found" << std::endl;
00110 return false;
00111 }
00112 TiXmlHandle document_handle( &xml_document );
00113
00114 if ( ! OpenTissue::skeleton::detail::xml_read( document_handle, skeleton ) )
00115 return false;
00116
00117 xml_document.Clear();
00118
00119 return true;
00120 }
00121
00122 }
00123 }
00124
00125
00126 #endif