• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

/home/hauberg/Dokumenter/Capture/humim-tracker-0.1/src/bgsub_state.h

Go to the documentation of this file.
00001 // Copyright (C) 2011 Soren Hauberg
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 3
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful, but
00009 // WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00015  
00016 #ifndef BGSUB_STATE_H
00017 #define BGSUB_STATE_H
00018 
00019 #include "skeleton_state.h"
00020 #include "options.h"
00021 #include "simulator.h"
00022 
00039 template<class observation_type>
00040 class bgsub_state : public skeleton_state<observation_type>
00041 {
00042 public:
00043   bgsub_state (const options &opts, const calibration &_calib, const bool _for_show = true)
00044     : skeleton_state<observation_type> (opts, _calib, _for_show),
00045       pred_noise (opts.pred_noise ())
00046     {
00047       this->compute_pose ();
00048   
00049       // Store default chains temporarely
00050       std::list<vector3> chain_list;
00051       for (chain_iter iter = this->solver.chain_begin (); iter != this->solver.chain_end (); iter++)
00052         chain_list.push_back (iter->get_end_effector ()->absolute ().T ());
00053   
00054       // Add everything as end-effectors
00055       int i = 0;
00056       for (bone_iter iter = this->bone_begin (); iter != this->bone_end (); iter++, i++)
00057         {
00058           if (iter->is_root ())
00059             continue;
00060         
00061           const vector3 absolute = iter->absolute ().T ();
00062           const vector3 pabsolute = iter->parent ()->absolute ().T ();
00063 
00064           // Make sure the bone isn't already an end-effector
00065           bool accept = true; //(absolute != pabsolute);
00066           for (std::list<vector3>::const_iterator ci = chain_list.begin ();
00067                ci != chain_list.end (); ci ++)
00068             {
00069               if (*ci == absolute)
00070                 {
00071                   accept = false;
00072                   break;
00073                 }
00074             }
00075       
00076           if (accept)
00077             {
00078               chain_type chain;
00079               chain.init (this->skeleton.root (), &(*iter));
00080               this->solver.add_chain (chain);
00081             }
00082         }
00083     };
00084 
00085   double predict (const observation_type &observation, const double variance_scale = 1.0)
00086     {
00087       const size_t max_rejects = 10;
00088       
00089       this->compute_pose ();
00090 
00091       gaussian1D gauss;
00092       const project2d &proj2d = observation.get_project2d ();
00093       const vector3 origin = this->calib.camera_centre ();
00094 
00095       int k = 0;
00096       for (chain_iter iter = this->solver.chain_begin (); iter != this->solver.chain_end (); iter++)
00097         {
00098           vector3 nu, nu_valid;
00099           CvPoint nu2d;
00100     
00101           // Get noise parameters for limb
00102           const double s = (k >= 7) ? 3.0 * pred_noise :  pred_noise;
00103           k++;
00104 
00105           // Rejection Sampling of points in embedding space
00106           bool inside = false;
00107           for (size_t trial = 0; trial < max_rejects; trial++)
00108             {
00109               // Generate a random 3-vector
00110               for (size_t n = 0; n < 3; n++)
00111                 nu [n] = s * gauss.random ();
00112               nu = iter->get_end_effector ()->absolute ().T () + nu; // - this->root;
00113           
00114               // Ensure that the prediction point is in tune with the observation
00115               nu2d = this->calib.world_to_pixel (nu, PROJ2D_SCALE);
00116               if (proj2d.is_inside (nu2d))
00117                 {
00118                   nu_valid = nu;
00119                   inside = true;
00120                   break;
00121                 }
00122             }
00123           
00124           // If the rejection sampling kept going for too long we project the point onto the segment
00125           if (!inside)
00126             {   
00127               const CvPoint goal2d = proj2d.project (nu2d);
00128               const vector3 goal_line = this->calib.pixel_to_world (goal2d, PROJ2D_SCALE);
00129               nu_valid = project_point_on_line (nu, origin, goal_line);
00130             }
00131           
00132           // Set end-effector goal
00133           iter->p_global () = nu_valid;
00134         }
00135 
00136       this->solver.solve (&solver_type::default_SD_settings ());
00137       OpenTissue::kinematics::inverse::get_joint_parameters (*(this->solver.skeleton ()),
00138                                                              this->theta);
00139 
00140       return 0;
00141     };
00142   
00143   bgsub_state& operator= (bgsub_state &new_state)
00144     {
00145       skeleton_state<observation_type>::operator= (new_state);
00146 
00147       return *this;
00148     };
00149     
00150   bool load (const std::string filename)
00151     {
00152       return skeleton_state<observation_type>::load (filename);
00153     };
00154 
00155   bool load (std::ifstream &file)
00156     {
00157       return skeleton_state<observation_type>::load (file);
00158     };
00159   private:
00160     const double pred_noise;
00161 };
00162 
00163 #endif // BGSUB_STATE_H

Generated on Thu Dec 1 2011 12:49:46 for HUMIM Tracker by  doxygen 1.7.1