linear_approx.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2012, Scott Niekum
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Robert Bosch nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *********************************************************************/
00036 
00042 #include "dmp/linear_approx.h"
00043 #include<stdio.h>
00044 using namespace std;
00045 
00046 namespace dmp{
00047     
00048     
00049 bool sort_pt_pair(const pt_pair& left, const pt_pair& right)
00050 {
00051     return left.first < right.first;
00052 }    
00053 
00054 
00055 LinearApprox::LinearApprox()
00056 {
00057         n_bases = 0;
00058 }
00059 
00060 
00061 LinearApprox::LinearApprox(std::vector<double> X, std::vector<double> Y)
00062 {
00063         n_bases = X.size();
00064         
00065         // Read in points and sort them by X-value
00066         for(int i=0; i<n_bases; i++){
00067             points.push_back(pt_pair(X[i], Y[i]));
00068         }
00069         
00070         std::sort(points.begin(), points.end(), sort_pt_pair);
00071         
00072 }
00073 
00074 
00075 LinearApprox::~LinearApprox(){};
00076 
00077 
00078 //Assumes that x is between 0 and 1 inclusive and that fxn is zero at x=0 and x=1 if not specified
00079 //Perform linear interpolation between saved points
00080 double LinearApprox::evalAt(double x)
00081 {
00082         // If out of bounds, or x=0, return 0
00083         if(x <= 0.0 || x > 1.0) 
00084             return 0.0;
00085     
00086         //If not points to interp, return 0
00087         if(n_bases <= 0)
00088             return 0.0;
00089                 
00090         // If less than the smallest entry, interp with x=0
00091         if(x < points[0].first){
00092             double slope = points[0].second / points[0].first;
00093             return slope * x;
00094         }    
00095         // If greater than largest entry, interp with fxn=0 at x=1
00096         else if(x > points[n_bases-1].first){    
00097             double inv_slope = points[n_bases].second / points[n_bases].first; 
00098             return inv_slope * (1.0 - x);
00099         }
00100         // Otherwise, normal interp
00101         else{
00102             double curr = 0.0;
00103             int i = 0;
00104         
00105             while(x > curr && i < n_bases){
00106                 i++;
00107                 curr = points[i].first;
00108             }
00109             
00110             double diffx = points[i].first - points[i-1].first;
00111             double diffy = points[i].second - points[i-1].second;
00112             double slope = diffy/diffx;
00113                 
00114             double y_start =  points[i-1].second;
00115             double x_dist = x - points[i-1].first;
00116             return y_start + (x_dist * slope);
00117             
00118         }
00119             
00120             
00121 }
00122 
00123 // Nothing to do here, weights are not used
00124 void LinearApprox::leastSquaresWeights(double *X, double *Y, int n_pts){};
00125 
00126 }
00127 
00128 


dmp
Author(s): Scott Niekum
autogenerated on Sun Oct 5 2014 23:29:12