Go to the documentation of this file.00001 #ifndef OPENTISSUE_KINEMATICS_INVERSE_IO_INVERSE_XML_WRITE_H
00002 #define OPENTISSUE_KINEMATICS_INVERSE_IO_INVERSE_XML_WRITE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/kinematics/skeleton/io/skeleton_xml_write.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_write(std::string const & filename, solver_type const & solver, bool const & write_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 typedef typename skeleton_type::const_bone_iterator const_bone_iterator;
00045
00046
00047 TiXmlDocument doc;
00048
00049 TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
00050 TiXmlElement * ik_elem = new TiXmlElement( "kinematics" );
00051
00052 doc.LinkEndChild(decl);
00053 doc.LinkEndChild(ik_elem);
00054
00055 if( !solver.skeleton() )
00056 throw std::invalid_argument( "solver has a skeleton null pointer" );
00057
00058 if(write_skeleton)
00059 {
00060 if(! OpenTissue::skeleton::detail::xml_write( doc, *(solver.skeleton()) ) )
00061 throw std::logic_error( "could not write skeleton to xml file" );
00062 }
00063
00064 const_bone_iterator bone = solver.skeleton()->begin();
00065 const_bone_iterator b_end = solver.skeleton()->end();
00066
00067 for(;bone!=b_end;++bone)
00068 {
00069 TiXmlElement * joint_elem = new TiXmlElement( "joint" );
00070
00071 joint_elem->SetAttribute( "name", bone->get_name() );
00072
00073 if(bone->type()==bone_traits::slider_type)
00074 {
00075 joint_elem->SetAttribute( "type", "slider" );
00076 {
00077 vector3_type u = bone->u();
00078 std::stringstream u_stream;
00079 u_stream.precision(15);
00080 u_stream << u;
00081 joint_elem->SetAttribute( "axis", u_stream.str() );
00082 }
00083 {
00084 std::stringstream min_stream;
00085 min_stream.precision(15);
00086 min_stream << bone->min_joint_limit(0);
00087 joint_elem->SetAttribute( "min", min_stream.str() );
00088 }
00089 {
00090 std::stringstream max_stream;
00091 max_stream.precision(15);
00092 max_stream << bone->max_joint_limit(0);
00093 joint_elem->SetAttribute( "max", max_stream.str() );
00094 }
00095 }
00096 if(bone->type()==bone_traits::hinge_type)
00097 {
00098 joint_elem->SetAttribute( "type", "hinge" );
00099 {
00100 vector3_type u = bone->u();
00101 std::stringstream u_stream;
00102 u_stream.precision(15);
00103 u_stream << u;
00104 joint_elem->SetAttribute( "axis", u_stream.str() );
00105 }
00106 {
00107 std::stringstream min_stream;
00108 min_stream.precision(15);
00109 min_stream << bone->min_joint_limit(0);
00110 joint_elem->SetAttribute( "min", min_stream.str() );
00111 }
00112 {
00113 std::stringstream max_stream;
00114 max_stream.precision(15);
00115 max_stream << bone->max_joint_limit(0);
00116 joint_elem->SetAttribute( "max", max_stream.str() );
00117 }
00118 }
00119 if(bone->type()==bone_traits::ball_type)
00120 {
00121 joint_elem->SetAttribute( "type", "ball" );
00122
00123 {
00124 vector3_type min_vector;
00125 min_vector(0) = bone->min_joint_limit(0);
00126 min_vector(1) = bone->min_joint_limit(1);
00127 min_vector(2) = bone->min_joint_limit(2);
00128 std::stringstream min_stream;
00129 min_stream.precision(15);
00130 min_stream << min_vector;
00131 joint_elem->SetAttribute( "min", min_stream.str() );
00132 }
00133
00134 {
00135 vector3_type max_vector;
00136 max_vector(0) = bone->max_joint_limit(0);
00137 max_vector(1) = bone->max_joint_limit(1);
00138 max_vector(2) = bone->max_joint_limit(2);
00139 std::stringstream max_stream;
00140 max_stream.precision(15);
00141 max_stream << max_vector;
00142 joint_elem->SetAttribute( "max", max_stream.str() );
00143 }
00144 }
00145
00146 ik_elem->LinkEndChild( joint_elem );
00147 }
00148
00149 typename solver_type::const_chain_iterator chain = solver.chain_begin();
00150 typename solver_type::const_chain_iterator c_end = solver.chain_end();
00151 for(;chain!=c_end;++chain)
00152 {
00153 TiXmlElement * chain_elem = new TiXmlElement( "chain" );
00154
00155 std::string root_name = chain->get_root()->get_name();
00156 std::string end_effector_name = chain->get_end_effector()->get_name();
00157 chain_elem->SetAttribute( "root", root_name );
00158 chain_elem->SetAttribute( "end", end_effector_name );
00159
00160 chain_elem->SetAttribute( "only_pos", chain->only_position() );
00161 {
00162 vector3_type weight;
00163 weight(0) = chain->weight_p();
00164 weight(1) = chain->weight_x();
00165 weight(2) = chain->weight_y();
00166 std::stringstream weight_stream;
00167 weight_stream.precision(15);
00168 weight_stream << weight;
00169 chain_elem->SetAttribute( "weight", weight_stream.str() );
00170 }
00171 {
00172 std::stringstream stream;
00173 stream.precision(15);
00174 stream << chain->p_global();
00175 chain_elem->SetAttribute( "p_global", stream.str() );
00176 }
00177 {
00178 std::stringstream stream;
00179 stream.precision(15);
00180 stream << chain->p_local();
00181 chain_elem->SetAttribute( "p_local", stream.str() );
00182 }
00183 {
00184 std::stringstream stream;
00185 stream.precision(15);
00186 stream << chain->x_global();
00187 chain_elem->SetAttribute( "x_global", stream.str() );
00188 }
00189 {
00190 std::stringstream stream;
00191 stream.precision(15);
00192 stream << chain->x_local();
00193 chain_elem->SetAttribute( "x_local", stream.str() );
00194 }
00195 {
00196 std::stringstream stream;
00197 stream.precision(15);
00198 stream << chain->y_global();
00199 chain_elem->SetAttribute( "y_global", stream.str() );
00200 }
00201 {
00202 std::stringstream stream;
00203 stream.precision(15);
00204 stream << chain->y_local();
00205 chain_elem->SetAttribute( "y_local", stream.str() );
00206 }
00207 ik_elem->LinkEndChild( chain_elem );
00208 }
00209
00210
00211 #ifdef TIXML_USE_STL
00212 doc.SaveFile(filename);
00213 #else
00214 doc.SaveFile(filename.c_str());
00215 #endif
00216
00217 return true;
00218 }
00219
00220
00221 }
00222 }
00223 }
00224
00225
00226 #endif