function.h
Go to the documentation of this file.
00001 #include <limits>
00002 #include <exception>
00003 
00004 class ESCFunction
00005 {
00006   public:
00007     virtual std::vector<float> init() = 0;
00008     virtual float value(const std::vector<float> &state) const = 0;
00009 };
00010 
00011 class Gauss1D : public ESCFunction
00012 {
00013   private:
00014     float a_, b_, c_, d_;
00015 
00016   public:
00017     Gauss1D(float a=0, float b=1, float c=0, float d=1) : a_(a), b_(b), c_(c), d_(d) { }
00018     
00019     std::vector<float> init()
00020     {
00021       std::vector<float> state;
00022       state.push_back(0);
00023       return state;
00024     }
00025     
00026     float value(const std::vector<float> &state) const
00027     {
00028       if (state.size() != 1)
00029         throw std::runtime_error("invalid state size");
00030         
00031       return a_ + b_*std::exp(-(state[0]-c_)*(state[0]-c_)/(2*d_*d_));
00032     }
00033 };
00034 
00035 class Gauss2D : public ESCFunction
00036 {
00037   private:
00038     float a_, b_;
00039     std::vector<float> c_, d_;
00040 
00041   public:
00042     Gauss2D(float a, float b, std::vector<float> c, std::vector<float> d)
00043     {
00044       if (c.empty())
00045       {
00046         c_.push_back(0);
00047         c_.push_back(0);
00048       }
00049       else
00050         c_ = c;
00051         
00052       if (d.empty())
00053       {
00054         d_.push_back(1);
00055         d_.push_back(1);
00056       }
00057       else
00058         d_ = d;
00059     }
00060     
00061     std::vector<float> init()
00062     {
00063       std::vector<float> state;
00064       state.push_back(0);
00065       state.push_back(0);
00066       return state;
00067     }
00068     
00069     float value(const std::vector<float> &state) const
00070     {
00071       if (state.size() != 1)
00072         throw std::runtime_error("invalid state size");
00073       
00074       double exponent = (state[0]-c_[0])*(state[0]-c_[0])/(2*d_[0]*d_[0]) + 
00075                         (state[1]-c_[1])*(state[1]-c_[1])/(2*d_[1]*d_[1]);
00076       
00077       return a_ + b_*std::exp(-exponent);
00078     }
00079 };
00080 
00081 
00082 class ESCSystem
00083 {
00084   protected:
00085     ESCFunction *function_;
00086     std::vector<float> state_;
00087         
00088   public:
00089     ESCSystem(ESCFunction *function) : function_(function)
00090     {
00091       if (!function)
00092         throw std::runtime_error("no function specified");
00093         
00094       reset();
00095     }
00096     
00097     void reset()
00098     {
00099       state_ = function_->init();
00100     }
00101   
00102     float step(const std::vector<float> &vel)
00103     {
00104       if (state_.size() != vel.size())
00105         throw std::runtime_error("invalid state size");
00106     
00107       for (size_t ii=0; ii < state_.size() && ii < vel.size(); ++ii)
00108         state_[ii] += vel[ii];
00109         
00110       return function_->value(state_);
00111     }
00112     
00113     float set(const std::vector<float> &pos)
00114     {
00115       if (state_.size() != pos.size())
00116         throw std::runtime_error("invalid state size");
00117     
00118       state_ = pos;
00119       return function_->value(state_);
00120     }
00121     
00122     float value() const
00123     {
00124       return function_->value(state_);
00125     }
00126     
00127     const std::vector<float> &state()
00128     {
00129       return state_;
00130     }
00131 };


esc_test
Author(s): Wouter Caarls
autogenerated on Sun Jan 5 2014 11:07:17