00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef STICK_STATE_H
00017 #define STICK_STATE_H
00018
00019 #include "angle_state.h"
00020 #include "options.h"
00021 #include "simulator.h"
00022 #include "auxil.h"
00023 #include "cloud_and_stick.h"
00024 #include <OpenTissue/core/math/math_is_number.h>
00025
00026 #include <iostream>
00041 template <class observation_type>
00042 class stick_state : public skeleton_state<observation_type>
00043 {
00044 public:
00045 stick_state (const options &opts, const calibration &_calib, const bool _for_show = true)
00046 : skeleton_state<observation_type> (opts, _calib, _for_show),
00047 lambda (2),
00048 pred_noise (opts.pred_noise ())
00049 {
00050 for (unsigned int k = 0; k < lambda.size (); k++)
00051 lambda [k] = NaN;
00052
00053 this->compute_pose ();
00054
00055
00056 int i = 0;
00057 for (bone_iter iter = this->bone_begin (); iter != this->bone_end (); iter++, i++)
00058 {
00059 if (iter->is_root ())
00060 continue;
00061
00062 chain_type chain;
00063 chain.init (this->skeleton.root (), &(*iter));
00064 this->solver.add_chain (chain);
00065 }
00066
00067
00068 };
00069
00070 double predict (const observation_type &observation, const double variance_scale = 1.0)
00071 {
00072 const double std [] = {0.019905, 0.003157, 0.008050, 0.180706, 0.101031, 0.137931,
00073 0.275119, 0.182599, 0.286416, 0.275119, 0.182599, 0.286416,
00074 0.390496, 0.301062, 0.483356, 0.508283, 0.808703, 0.806479,
00075 0.275119, 0.182599, 0.286416, 0.381652, 0.255074, 0.218130,
00076 0.498532, 0.249264, 0.615678, 0.938598, 0.593349, 1.098928,
00077 0.275119, 0.182599, 0.286416, 0.509799, 0.464794, 0.370578,
00078 0.988121, 0.796506, 0.450776, 1.155469, 1.320700, 0.709060};
00079
00080 this->compute_pose ();
00081 const stick_type stick = observation.get_stick ();
00082
00083 const int num_indices = 2;
00084 const int indices [num_indices+1] = {9, 13, -1};
00085
00086 gaussian1D gauss;
00087
00088
00089 for (unsigned int i = 0; i < lambda.size (); i++)
00090 {
00091 if (!is_number (lambda [i]))
00092 {
00093
00094 const_chain_iter iter = this->solver.chain_begin ();
00095 int k = 0;
00096 while (k++ != indices [i])
00097 iter ++;
00098 const vector3 hand = iter->get_end_effector ()->absolute ().T ();
00099
00100
00101 double alpha;
00102 const double hand_object_dist2 = dist2 (stick, hand, alpha);
00103 if (sqrt (hand_object_dist2) < T_E)
00104 lambda [i] = alpha;
00105 else
00106 lambda [i] = NaN;
00107 }
00108 else
00109 {
00110
00111
00112
00113
00114
00115
00116
00117
00118 {
00119 gaussian1D G (lambda [i], sigma);
00120 while (true)
00121 {
00122 const double l = G.random ();
00123 if (0.0 <= l && l <= 1.0)
00124 {
00125 lambda [i] = l;
00126 break;
00127 }
00128 }
00129 }
00130 }
00131 }
00132
00133
00134
00135
00136
00137
00138 vector3 nu;
00139 int k = 0, i = 0;
00140 for (chain_iter iter = this->solver.chain_begin ();
00141 iter != this->solver.chain_end (); iter++, k++)
00142 {
00143 if (k == indices [i] && is_number (lambda [i]))
00144 {
00145 nu = nearest_point_on_stick (stick, lambda [i]);
00146 i++;
00147 }
00148 else
00149 {
00150 const vector3 curr = iter->get_end_effector ()->absolute ().T ();
00151
00152 for (size_t n = 0; n < 3; n++)
00153 nu [n] = curr [n] + 0.5 * std [k++] * gauss.random ();
00154 }
00155 iter->p_global () = nu;
00156 }
00157
00158 this->solver.solve (&solver_type::default_SD_settings ());
00159 OpenTissue::kinematics::inverse::get_joint_parameters (*(this->solver.skeleton ()),
00160 this->theta);
00161
00162 return 0;
00163 };
00164
00165 stick_state& operator= (stick_state &new_state)
00166 {
00167 skeleton_state<observation_type>::operator= (new_state);
00168 this->lambda = new_state.lambda;
00169
00170 return *this;
00171 };
00172
00173 bool load (const std::string filename)
00174 {
00175 return skeleton_state<observation_type>::load (filename);
00176 };
00177
00178 bool load (std::ifstream &file)
00179 {
00180 return skeleton_state<observation_type>::load (file);
00181 };
00182
00183 protected:
00184 std::vector<double> lambda;
00185 const double pred_noise;
00186
00187 static const double p_letgo = 0.05, T_E = 1.0, sigma = 0.0001;
00188 };
00189
00190 #endif // STICK_STATE_H