Go to the documentation of this file.00001 #ifndef OPENTISSUE_KINEMATICS_INVERSE_IO_INVERSE_XML_READ_H
00002 #define OPENTISSUE_KINEMATICS_INVERSE_IO_INVERSE_XML_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/kinematics/skeleton/io/skeleton_xml_read.h>
00013
00014 #include <TinyXML/tinyxml.h>
00015
00016 #include <stdexcept>
00017
00018 namespace OpenTissue
00019 {
00020 namespace kinematics
00021 {
00022 namespace inverse
00023 {
00024
00035 template<typename solver_type>
00036 bool xml_read(std::string const & filename, solver_type & solver, bool const & read_skeleton = true)
00037 {
00038 typedef typename solver_type::skeleton_type skeleton_type;
00039 typedef typename solver_type::math_types math_types;
00040 typedef typename math_types::vector3_type vector3_type;
00041 typedef typename math_types::real_type real_type;
00042 typedef typename skeleton_type::bone_type bone_type;
00043 typedef typename bone_type::bone_traits bone_traits;
00044
00045 if( !solver.skeleton() )
00046 throw std::invalid_argument( "solver has a skeleton null pointer" );
00047
00048 #ifdef TIXML_USE_STL
00049 TiXmlDocument xml_document(filename);
00050 #else
00051 TiXmlDocument xml_document(filename.c_str());
00052 #endif
00053
00054 if(!xml_document.LoadFile())
00055 {
00056 std::cerr << "file not found" << std::endl;
00057 return false;
00058 }
00059 TiXmlHandle document_handle( &xml_document );
00060
00061 if(read_skeleton)
00062 {
00063 if ( ! OpenTissue::skeleton::detail::xml_read( document_handle, *(solver.skeleton()) ) )
00064 return false;
00065 }
00066
00067 solver.init( *(solver.skeleton()) );
00068
00069 TiXmlElement * xml_joint = document_handle.FirstChild( "kinematics" ).FirstChild( "joint" ).Element();
00070 for( ; xml_joint; xml_joint=xml_joint->NextSiblingElement("joint") )
00071 {
00072 bone_type * bone = 0;
00073
00074 if(xml_joint->Attribute("name"))
00075 {
00076 std::string name = xml_joint->Attribute("name");
00077 bone = solver.skeleton()->get_bone( name );
00078 }
00079 else
00080 {
00081 throw std::logic_error( "name attribute was missing in xml tag" );
00082 }
00083
00084 if(!bone)
00085 throw std::logic_error( "could not find any bone of that name" );
00086
00087 if(xml_joint->Attribute("type"))
00088 {
00089 std::string type = xml_joint->Attribute("type");
00090
00091 if( type == "slider")
00092 bone->type() = bone_traits::slider_type;
00093 else if( type == "hinge")
00094 bone->type() = bone_traits::hinge_type;
00095 else if( type == "ball")
00096 bone->type() = bone_traits::ball_type;
00097 }
00098
00099 if(xml_joint->Attribute("axis"))
00100 {
00101 vector3_type u;
00102 std::istringstream str_stream(xml_joint->Attribute("axis"));
00103 str_stream >> u;
00104 bone->u() = u ;
00105 }
00106
00107 if(xml_joint->Attribute("min"))
00108 {
00109 std::istringstream str_stream(xml_joint->Attribute("min"));
00110 if(bone->type() == bone_traits::ball_type)
00111 {
00112 vector3_type min_values;
00113 str_stream >> min_values;
00114 bone->min_joint_limit(0) = min_values(0);
00115 bone->min_joint_limit(1) = min_values(1);
00116 bone->min_joint_limit(2) = min_values(2);
00117 }
00118 else
00119 {
00120 real_type min_value;
00121 str_stream >> min_value;
00122 bone->min_joint_limit(0) = min_value;
00123 }
00124 }
00125
00126 if(xml_joint->Attribute("max"))
00127 {
00128 std::istringstream str_stream(xml_joint->Attribute("max"));
00129 if(bone->type() == bone_traits::ball_type)
00130 {
00131 vector3_type max_values;
00132 str_stream >> max_values;
00133 bone->max_joint_limit(0) = max_values(0);
00134 bone->max_joint_limit(1) = max_values(1);
00135 bone->max_joint_limit(2) = max_values(2);
00136 }
00137 else
00138 {
00139 real_type max_value;
00140 str_stream >> max_value;
00141 bone->max_joint_limit(0) = max_value;
00142 }
00143 }
00144 }
00145
00146 TiXmlElement * xml_chain = document_handle.FirstChild( "kinematics" ).FirstChild( "chain" ).Element();
00147 for( ; xml_chain; xml_chain=xml_chain->NextSiblingElement("chain") )
00148 {
00149 bone_type const * root = 0;
00150 bone_type const * end_effector = 0;
00151
00152 if(xml_chain->Attribute("root"))
00153 {
00154 std::string name = xml_chain->Attribute("root");
00155 root = solver.skeleton()->get_bone( name );
00156 }
00157 else
00158 {
00159 throw std::logic_error( "root name attribute was missing in xml tag" );
00160 }
00161 if(xml_chain->Attribute("end"))
00162 {
00163 std::string name = xml_chain->Attribute("end");
00164 end_effector = solver.skeleton()->get_bone( name );
00165 }
00166 else
00167 {
00168 throw std::logic_error( "end effector name attribute was missing in xml tag" );
00169 }
00170
00171 if(!root)
00172 throw std::logic_error( "could not find any bone of that name" );
00173
00174 if(!end_effector)
00175 throw std::logic_error( "could not find any bone of that name" );
00176
00177 typename solver_type::chain_type chain;
00178 chain.init( root, end_effector );
00179
00180 if(xml_chain->Attribute("only_pos"))
00181 {
00182 bool value;
00183 std::istringstream str_stream(xml_chain->Attribute("only_pos"));
00184 str_stream >> value;
00185 chain.only_position() = value;
00186 }
00187 if(xml_chain->Attribute("weight"))
00188 {
00189 vector3_type weight;
00190 std::istringstream str_stream(xml_chain->Attribute("weight"));
00191 str_stream >> weight;
00192 chain.set_weight_p( weight(0) );
00193 chain.set_weight_x( weight(1) );
00194 chain.set_weight_y( weight(2) );
00195 }
00196
00197 if(xml_chain->Attribute("p_global"))
00198 {
00199 vector3_type p_global;
00200 std::istringstream str_stream(xml_chain->Attribute("p_global"));
00201 str_stream >> p_global;
00202 chain.p_global() = p_global;
00203 }
00204
00205 if(xml_chain->Attribute("p_local"))
00206 {
00207 vector3_type p_local;
00208 std::istringstream str_stream(xml_chain->Attribute("p_local"));
00209 str_stream >> p_local;
00210 chain.p_local() = p_local;
00211 }
00212
00213 if(xml_chain->Attribute("x_global"))
00214 {
00215 vector3_type x_global;
00216 std::istringstream str_stream(xml_chain->Attribute("x_global"));
00217 str_stream >> x_global;
00218 chain.x_global() = x_global;
00219 }
00220
00221 if(xml_chain->Attribute("x_local"))
00222 {
00223 vector3_type x_local;
00224 std::istringstream str_stream(xml_chain->Attribute("x_local"));
00225 str_stream >> x_local;
00226 chain.x_local() = x_local;
00227 }
00228
00229 if(xml_chain->Attribute("y_global"))
00230 {
00231 vector3_type y_global;
00232 std::istringstream str_stream(xml_chain->Attribute("y_global"));
00233 str_stream >> y_global;
00234 chain.y_global() = y_global;
00235 }
00236
00237 if(xml_chain->Attribute("y_local"))
00238 {
00239 vector3_type y_local;
00240 std::istringstream str_stream(xml_chain->Attribute("y_local"));
00241 str_stream >> y_local;
00242 chain.y_local() = y_local;
00243 }
00244
00245 solver.add_chain( chain );
00246 }
00247
00248 xml_document.Clear();
00249 return true;
00250 }
00251
00252 }
00253 }
00254 }
00255
00256
00257 #endif