Go to the documentation of this file.00001 #ifndef OPENTISSUE_KINEMATICS_SKELETON_IO_SKELETON_CAL3D_READ_H
00002 #define OPENTISSUE_KINEMATICS_SKELETON_IO_SKELETON_CAL3D_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <TinyXML/tinyxml.h>
00013
00014 #include <sstream>
00015
00016 namespace OpenTissue
00017 {
00018 namespace skeleton
00019 {
00020
00030 template<typename skeleton_type>
00031 bool cal3d_xml_read(std::string const & filename,skeleton_type & skeleton)
00032 {
00033 typedef typename skeleton_type::math_types::coordsys_type coordsys_type;
00034 typedef typename skeleton_type::math_types::vector3_type vector3_type;
00035 typedef typename skeleton_type::math_types::quaternion_type quaternion_type;
00036 typedef typename skeleton_type::bone_type bone_type;
00037 typedef typename bone_type::bone_traits bone_traits;
00038
00039 skeleton.clear();
00040
00041 #ifdef TIXML_USE_STL
00042 TiXmlDocument xml_document(filename);
00043 #else
00044 TiXmlDocument xml_document(filename.c_str());
00045 #endif
00046
00047 if(!xml_document.LoadFile())
00048 {
00049 std::cerr << "file not found:" << filename << std::endl;
00050 return false;
00051 }
00052
00053 std::stringstream str;
00054 TiXmlNode* node;
00055 TiXmlElement * xml_skelton = xml_document.FirstChildElement( "SKELETON" );
00056 TiXmlElement * xml_bone = xml_skelton->FirstChildElement();
00057
00058 for( ; xml_bone; xml_bone=xml_bone->NextSiblingElement() )
00059 {
00060 bone_type * bone = 0;
00061
00062 TiXmlElement * parentid = xml_bone->FirstChildElement("PARENTID");
00063
00064 if( parentid )
00065 {
00066 node = parentid->FirstChild();
00067 TiXmlText* parentiddata = node->ToText();
00068 std::string parent_name = parentiddata->Value();
00069 if( parent_name.compare( "-1" ) == 0 )
00070 bone = skeleton.create_bone();
00071 else
00072 {
00073 bone_type * parent = skeleton.get_bone( parent_name );
00074 if(!parent)
00075 {
00076 std::cerr << "Could not find parent!" << std::endl;
00077 return false;
00078 }
00079 bone = skeleton.create_bone(parent);
00080 }
00081 }
00082 else
00083 {
00084 std::cerr << "Missing xml attribute on parent!" << std::endl;
00085 return false;
00086 }
00087
00088 if(xml_bone->Attribute("ID"))
00089 {
00090 std::string id = xml_bone->Attribute("ID");
00091 bone->set_name( id );
00092 }
00093
00094 TiXmlElement * translation = xml_bone->FirstChildElement("TRANSLATION");
00095 TiXmlElement * rotation = xml_bone->FirstChildElement("ROTATION");
00096
00097 float tx, ty, tz;
00098 float rx, ry, rz, s;
00099
00100 float tx_bs, ty_bs, tz_bs;
00101 float rx_bs, ry_bs, rz_bs, s_bs;
00102
00103 if( translation && rotation )
00104 {
00105 node = translation->FirstChild();
00106 TiXmlText* translationdata = node->ToText();
00107 str.clear();
00108 str << translationdata->Value();
00109 str >> tx >> ty >> tz;
00110
00111 node = rotation->FirstChild();
00112 TiXmlText* rotationdata = node->ToText();
00113 str.clear();
00114 str << rotationdata->Value();
00115 str >> rx >> ry >> rz >> s;
00116
00117 coordsys_type bind_pose( vector3_type( tx, ty, tz ),
00118 quaternion_type( s, -rx, -ry, -rz ) );
00119 bone->bind_pose() = bone_traits::convert( bind_pose );
00120 }
00121
00122 TiXmlElement * translation_bs = xml_bone->FirstChildElement("LOCALTRANSLATION");
00123
00124 TiXmlElement * rotation_bs = xml_bone->FirstChildElement("LOCALROTATION");
00125
00126 if( translation_bs && rotation_bs )
00127 {
00128 node = translation_bs->FirstChild();
00129 TiXmlText* translationdata_bs = node->ToText();
00130 str.clear();
00131 str << translationdata_bs->Value();
00132 str >> tx_bs >> ty_bs >> tz_bs;
00133
00134 node = rotation_bs->FirstChild();
00135 TiXmlText* rotationdata_bs = node->ToText();
00136 str.clear();
00137 str << rotationdata_bs->Value();
00138 str >> rx_bs >> ry_bs >> rz_bs >> s_bs;
00139
00140 coordsys_type bone_space( vector3_type( tx_bs, ty_bs, tz_bs ),
00141 quaternion_type( s_bs, -rx_bs, -ry_bs, -rz_bs ) );
00142 bone->bone_space() = bone_traits::convert( bone_space );
00143 }
00144 }
00145
00146 xml_document.Clear();
00147 skeleton.set_bind_pose();
00148 return true;
00149 };
00150
00151 }
00152 }
00153
00154
00155 #endif