Virtual Testbed
Ship dynamics simulator for extreme conditions
linear_interpolation.hh
1 #ifndef VTESTBED_GEOMETRY_LINEAR_INTERPOLATION_HH
2 #define VTESTBED_GEOMETRY_LINEAR_INTERPOLATION_HH
3 
4 #include <vector>
5 
6 #include <vtestbed/geometry/curve_segments.hh>
7 #include <vtestbed/geometry/math.hh>
8 
9 namespace vtb {
10 
11  namespace geometry {
12 
13  template <class T,int N=3>
15 
16  public:
17  using value_type = Vertex<T,N>;
18  using reference = value_type&;
19  using const_reference = const value_type&;
21  using size_type = size_t;
23 
24  private:
25  vertex_array _points;
26  segments_type _segments;
27 
28  public:
29  inline explicit
30  Linear_interpolation(const vertex_array& values):
31  _points(values),
32  _segments(values) {
33  this->_segments.normalise();
34  }
35 
36  inline value_type
37  operator()(T t) const {
38  const auto& segments = this->_segments;
39  const auto& points = this->_points;
40  size_type i = 0;
41  segments.locate(t, i);
42  if (i == segments.size()) {
43  return points.back();
44  }
45  const auto& a = points[i];
46  const auto& b = points[i+1];
47  return a + t*(b - a);
48  }
49 
50  inline const_reference
51  front() const noexcept {
52  return this->_points.front();
53  }
54 
55  inline const_reference
56  back() const noexcept {
57  return this->_points.back();
58  }
59 
60  inline size_type
61  size() const {
62  return this->_points.size();
63  }
64 
65  inline const segments_type&
66  segments() const {
67  return this->_segments;
68  }
69 
70  inline segments_type&
71  segments() {
72  return this->_segments;
73  }
74 
75  inline void
76  range(T min, T max) {
77  this->_segments.range(min, max);
78  }
79 
80  };
81 
82  }
83 
84 }
85 
86 #endif // vim:filetype=cpp
Main namespace.
Definition: convert.hh:9