source: trunk/src/BufferedLinearInterpolator1D.cpp @ 2730

Last change on this file since 2730 was 2730, checked in by Takeshi Nakazato, 11 years ago

New Development: No

JIRA Issue: Yes CAS-4770

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...

Rewrite implementations for locator and interpolator.
Documentation (doxygen format) is added to header files.


File size: 1.3 KB
Line 
1//
2// C++ Implementation: BufferedLinearInterpolator1D
3//
4// Description:
5//
6//
7// Author: Takeshi Nakazato <takeshi.nakazato@nao.ac.jp>, (C) 2012
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12#include <assert.h>
13
14#include "BufferedLinearInterpolator1D.h"
15
16namespace asap {
17
18BufferedLinearInterpolator1D::BufferedLinearInterpolator1D()
19  : Interpolator1D(),
20    reusable_(false)
21{}
22
23BufferedLinearInterpolator1D::~BufferedLinearInterpolator1D()
24{}
25
26void BufferedLinearInterpolator1D::setData(double *x, float *y, unsigned int n)
27{
28  Interpolator1D::setData(x, y, n);
29  reusable_ = false;
30}
31
32void BufferedLinearInterpolator1D::setX(double *x, unsigned int n)
33{
34  Interpolator1D::setX(x, n);
35  reusable_ = false;
36}
37
38float BufferedLinearInterpolator1D::interpolate(double x)
39{
40  assert(isready());
41  if (n_ == 1)
42    return y_[0];
43
44  unsigned int i;
45  bool b = (reusable_ && x == xold_);
46  if (b) {
47    i = prev_;
48  }
49  else {
50    i = locator_->locate(x);
51    prev_ = i;
52    xold_ = x;
53  }
54
55  // do not perform extrapolation
56  if (i == 0) {
57    return y_[i];
58  }
59  else if (i == n_) {
60    return y_[i-1];
61  }
62
63  // linear interpolation
64  if (!b)
65    factor_ = (x - x_[i-1]) / (x_[i] - x_[i-1]);
66  float y = y_[i-1] + (y_[i] - y_[i-1]) * factor_;
67  reusable_ = true;
68  return y;
69}
70
71}
Note: See TracBrowser for help on using the repository browser.