Go to the documentation of this file.00001 #ifndef OPENTISSUE_KINEMATICS_INVERSE_UTILITY_SAMPLE_MOTION_H
00002 #define OPENTISSUE_KINEMATICS_INVERSE_UTILITY_SAMPLE_MOTION_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_covariance.h>
00013 #include <OpenTissue/core/math/math_eigen_system_decomposition.h>
00014 #include <OpenTissue/core/math/math_compute_contiguous_angle_interval.h>
00015
00016
00017
00018 #include <OpenTissue/core/math/big/io/big_matlab_write.h>
00019
00020 #include <boost/cast.hpp>
00021
00022 #include <cmath>
00023 #include <vector>
00024
00025
00026 namespace OpenTissue
00027 {
00028 namespace kinematics
00029 {
00030 namespace inverse
00031 {
00032
00033
00034
00035
00036
00037
00038
00039 template <typename blend_scheduler_type, typename skeleton_type >
00040 inline std::vector < std::vector<typename skeleton_type::bone_traits::transform_type> >
00041 sample_motion(blend_scheduler_type & scheduler
00042 , skeleton_type & skeleton )
00043 {
00044 size_t const N = 100u;
00045 typedef typename skeleton_type::bone_traits::transform_type transform_type;
00046 typedef typename skeleton_type::math_types math_types;
00047 typedef typename math_types::vector3_type V;
00048 typedef typename math_types::real_type T;
00049 typedef typename math_types::quaternion_type Q;
00050 typedef typename math_types::matrix3x3_type M;
00051 typedef typename math_types::value_traits value_traits;
00052
00053 typedef typename std::vector< transform_type > transform_container;
00054
00055 std::vector< transform_container > transforms;
00056
00057 transforms.resize( skeleton.size() );
00058 for(skeleton_type::bone_iterator bone = skeleton.begin();bone!=skeleton.end();++bone)
00059 {
00060 transforms[bone->get_number()].resize( N );;
00061 }
00062
00063
00064
00065
00066 T duration = scheduler.compute_duration();
00067
00068 T time_step = duration / (N-1);
00069 T time = value_traits::zero();
00070 for(size_t i = 0;i < N;++i)
00071 {
00072
00073 scheduler.compute_pose(skeleton, time);
00074
00075 for(skeleton_type::bone_iterator bone = skeleton.begin();bone!=skeleton.end();++bone)
00076 {
00077
00078 transforms[bone->get_number()][i] = bone->relative();
00079 }
00080
00081 time += time_step;
00082 }
00083
00084 return transforms;
00085
00086
00087 }
00088
00089
00090
00099 template <typename blend_scheduler_type, typename skeleton_type >
00100 inline void
00101 sample_motion_bones(
00102 blend_scheduler_type & scheduler
00103 , skeleton_type & skeleton
00104 , std::vector < std::vector < typename skeleton_type::bone_type> > & transforms
00105 )
00106 {
00107 size_t const N = 100u;
00108 typedef typename skeleton_type::bone_traits::transform_type transform_type;
00109 typedef typename skeleton_type::math_types math_types;
00110 typedef typename math_types::vector3_type V;
00111 typedef typename math_types::real_type T;
00112 typedef typename math_types::quaternion_type Q;
00113 typedef typename math_types::matrix3x3_type M;
00114 typedef typename math_types::value_traits value_traits;
00115
00116
00117
00118
00119
00120 transforms.resize( skeleton.size() );
00121 for(skeleton_type::bone_iterator bone = skeleton.begin();bone!=skeleton.end();++bone)
00122 {
00123 transforms[bone->get_number()].resize( N );;
00124 }
00125
00126
00127
00128
00129 T duration = scheduler.compute_duration();
00130
00131 T time_step = duration / (N-1);
00132 T time = value_traits::zero();
00133 for(size_t i = 0;i < N;++i)
00134 {
00135
00136 scheduler.compute_pose(skeleton, time);
00137 OpenTissue::kinematics::inverse::set_default_joint_settings( skeleton );
00138
00139 for(skeleton_type::bone_iterator bone = skeleton.begin();bone!=skeleton.end();++bone)
00140 {
00141 transforms[bone->get_number()][i] = *bone;
00142 }
00143
00144 time += time_step;
00145 }
00146
00147
00148
00149
00150 }
00151
00152
00167 template<typename pose_iterator>
00168 inline void write_sampled_transforms(
00169 std::string const & prefix
00170 , std::string const & matlab_filename
00171 , std::string const & latex_filename
00172 , pose_iterator begin
00173 , pose_iterator end)
00174 {
00175 using namespace OpenTissue::math::big;
00176 using std::min;
00177 using std::max;
00178
00179 std::ofstream matlab_file( matlab_filename.c_str(), std::ios::out);
00180 if (!matlab_file)
00181 {
00182 std::cerr << "Error unable to create file: "<< matlab_filename << std::endl;
00183 return;
00184 }
00185
00186 std::ofstream latex_file( latex_filename.c_str(), std::ios::out);
00187 if (!latex_file)
00188 {
00189 std::cerr << "Error unable to create file: "<< latex_filename << std::endl;
00190 return;
00191 }
00192
00193
00194
00195
00196 matlab_file << "close all;" << std::endl;
00197 matlab_file << "clear all;" << std::endl;
00198
00199
00200
00201
00202 matlab_file << "phi = [";
00203 for(pose_iterator output=begin;output!=end;++output)
00204 {
00205 matlab_file << " " << output->theta(0);
00206 }
00207 matlab_file << " ];" << std::endl;
00208
00209
00210 matlab_file << "psi = [";
00211 for(pose_iterator output=begin;output!=end;++output)
00212 {
00213 matlab_file << " " << output->theta(1);
00214 }
00215 matlab_file << " ];" << std::endl;
00216
00217 matlab_file << "theta = [";
00218 for(pose_iterator output=begin;output!=end;++output)
00219 {
00220 matlab_file << " " << output->theta(2);
00221 }
00222 matlab_file << " ];" << std::endl;
00223
00224
00225 matlab_file << "min_phi = min(phi)" << std::endl;
00226 matlab_file << "max_phi = max(phi)" << std::endl;
00227 matlab_file << "min_psi = min(psi)" << std::endl;
00228 matlab_file << "max_psi = max(psi)" << std::endl;
00229 matlab_file << "min_theta = min(theta)" << std::endl;
00230 matlab_file << "max_theta = max(theta)" << std::endl;
00231
00232
00233 {
00234 matlab_file << "filename1 = '" << prefix << "_parameters';" << std::endl;
00235 matlab_file << "figure(1);" << std::endl;
00236 matlab_file << "clf" << std::endl;
00237 matlab_file << "set(gca,'fontsize',18);" << std::endl;
00238 matlab_file << "hold on;" << std::endl;
00239
00240 matlab_file << "plot(phi,'r');" << std::endl;
00241 matlab_file << "plot(psi,'g');" << std::endl;
00242 matlab_file << "plot(theta,'b');" << std::endl;
00243 matlab_file << "legend('phi ','psi ','theta','Location','Best');" << std::endl;
00244 matlab_file << "axis tight;" << std::endl;
00245 matlab_file << "xlabel('Samples','fontsize',18);" << std::endl;
00246 matlab_file << "ylabel('Value of the parameters','fontsize',18);" << std::endl;
00247 matlab_file << "hold off;" << std::endl;
00248 matlab_file << "print('-f1','-depsc2', filename1);" << std::endl;
00249 matlab_file << "print('-f1','-dpng', filename1);" << std::endl;
00250 }
00251
00252
00253
00254
00255 matlab_file.flush();
00256 matlab_file.close();
00257
00258 latex_file.flush();
00259 latex_file.close();
00260 std::cout << "done writting sampled datafiles..." << std::endl;
00261
00262 }
00263 }
00264 }
00265 }
00266
00267
00268
00269 #endif