Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef STICK2D_STATE_H
00017 #define STICK2D_STATE_H
00018
00019 #include "angle_state.h"
00020 #include "options.h"
00021 #include "simulator.h"
00022 #include "auxil.h"
00023 #include "math_types.h"
00024 #include "cloud_and_stick.h"
00025 #include <OpenTissue/core/math/math_is_number.h>
00026
00041 template <class observation_type>
00042 class stick2d_state : public skeleton_state<observation_type>
00043 {
00044 public:
00045 stick2d_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 double predict (const observation_type &observation, const double variance_scale = 1.0)
00069 {
00070 this->compute_pose ();
00071 const stick_type stick = observation.get_stick ();
00072 const vector3 origin = this->calib.camera_centre ();
00073
00074 const int num_indices = 2;
00075 const int indices [num_indices+1] = {9, 13, -1};
00076
00077 gaussian1D gauss;
00078
00079
00080 for (unsigned int i = 0; i < lambda.size (); i++)
00081 {
00082 if (!is_number (lambda [i]))
00083 {
00084
00085 const_chain_iter iter = this->solver.chain_begin ();
00086 int k = 0;
00087 while (k++ != indices [i])
00088 iter ++;
00089 const vector3 hand = iter->get_end_effector ()->absolute ().T ();
00090
00091
00092 double alpha;
00093 const double hand_object_dist2 = dist2 (stick, hand, alpha);
00094 if (hand_object_dist2 < T_E)
00095 lambda [i] = alpha;
00096 else
00097 lambda [i] = NaN;
00098 }
00099 else
00100 {
00101
00102
00103
00104
00105
00106
00107
00108
00109 {
00110 gaussian1D G (lambda [i], sigma);
00111 while (true)
00112 {
00113 const double l = G.random ();
00114 if (0.0 <= l && l <= 1.0)
00115 {
00116 lambda [i] = l;
00117 break;
00118 }
00119 }
00120 }
00121 }
00122 }
00123
00124
00125
00126
00127
00128
00129 this->compute_pose ();
00130
00131 vector3 nu;
00132 int k = 0, i = 0;
00133 for (chain_iter iter = this->solver.chain_begin ();
00134 iter != this->solver.chain_end (); iter++, k++)
00135 {
00136 const vector3 curr = iter->get_end_effector ()->absolute ().T ();
00137 if (k == indices [i] && is_number (lambda [i]))
00138 {
00139 const vector3 line = nearest_point_on_stick (stick, lambda [i]);
00140 nu = project_point_on_stick_line (curr, line, origin);
00141 i++;
00142 }
00143 else
00144 {
00145 const double s = (k >= 7) ? 3.0 * pred_noise : pred_noise;
00146 for (size_t n = 0; n < 3; n++)
00147 nu [n] = curr [n] + s * gauss.random ();
00148 }
00149 iter->p_global () = nu;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 this->solver.solve (&solver_type::default_SD_settings ());
00173 OpenTissue::kinematics::inverse::get_joint_parameters (*(this->solver.skeleton ()),
00174 this->theta);
00175
00176 return 0;
00177 }
00178
00179 stick2d_state& operator= (stick2d_state &new_state)
00180 {
00181 skeleton_state<observation_type>::operator= (new_state);
00182
00183 return *this;
00184 }
00185
00186 bool load (const std::string filename)
00187 {
00188 return skeleton_state<observation_type>::load (filename);
00189 }
00190
00191 bool load (std::ifstream &file)
00192 {
00193 return skeleton_state<observation_type>::load (file);
00194 }
00195
00196 protected:
00197 std::vector<double> lambda;
00198 const double pred_noise;
00199
00200 static const double p_letgo = 0.01, T_E = 0.2, sigma = 0.0001;
00201
00202 vector3 project_point_on_stick_line (const vector3 &p, const vector3 &line,
00203 const vector3 &origin) const
00204 {
00205 const vector3 lineo = unit (line - origin);
00206 const vector3 po = p - origin;
00207 const vector3 proj = lineo * (lineo * po);
00208
00209 return proj + origin;
00210 }
00211 };
00212
00213 #endif // STICK2D_STATE_H