File: mbl_linear_interpolator.cxx

package info (click to toggle)
vxl 1.17.0.dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 153,280 kB
  • ctags: 105,123
  • sloc: cpp: 747,420; ansic: 209,130; fortran: 34,230; lisp: 14,915; sh: 6,187; python: 5,856; makefile: 340; perl: 294; xml: 160
file content (87 lines) | stat: -rw-r--r-- 1,360 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// This is mul/mbl/mbl_linear_interpolator.cxx
#include "mbl_linear_interpolator.h"

#include <vcl_limits.h>
#include <mbl/mbl_index_sort.h>


mbl_linear_interpolator::mbl_linear_interpolator()
{
  clear();
}

void mbl_linear_interpolator::clear()
{
  x_.resize(0);
  y_.resize(0);
}

bool mbl_linear_interpolator::set(const vcl_vector<double> &x, const vcl_vector<double> &y)
{
  bool ret=false;
  clear();

  if (x.size()==y.size() && x.size()>0)
  {
    x_=x;
    y_=y;
    sort();
    ret=true;
  }

  return ret;
}

void mbl_linear_interpolator::sort()
{
   vcl_vector<int> index;
   mbl_index_sort(x_,index);
   vcl_vector<double> tmp_x=x_;
   vcl_vector<double> tmp_y=y_;

   for (unsigned i=0;i<index.size();++i)
   {
     x_[i]=tmp_x[index[i]];
     y_[i]=tmp_y[index[i]];
   }

}


double mbl_linear_interpolator::y(double x) const
{
  double yval=vcl_numeric_limits<double>::quiet_NaN();

  if (x_.size()>0)
  {
    if (x<=x_.front())
      yval=y_.front();
    else if (x>=x_.back())
      yval=y_.back();
    else
    {
      for (unsigned i=1;i<x_.size();++i)
      {
        if (x<x_[i])
        {
          double x1=x_[i-1];
          double x2=x_[i];
          double y1=y_[i-1];
          double y2=y_[i];
          double f= (x-x1)/(x2-x1);
          yval=y1+f*(y2-y1);
          break;
        }
      }
    }
  }

  return yval;
}