Grail (C++)  1.1.1
A multi-platform, modular, universal engine for embedding advanced AI in games.
LinearlyInterpolatedCurve.hh
1 #ifndef GRAIL_LINEARLY_INTERPOLATED_CURVE_H
2 #define GRAIL_LINEARLY_INTERPOLATED_CURVE_H
3 
4 #include "Curve.hh"
5 #include "../../libs/vector2/vector2.h"
6 
7 #include <algorithm>
8 #include <cassert>
9 
10 namespace grail
11 {
12  namespace curves
13  {
14  template <typename ContextType>
19  class LinearlyInterpolatedCurve final : public Curve<ContextType>
20  {
21  public:
28  const std::vector<Vector2>& points)
29  : Curve<ContextType>{childEvaluator}, points(points)
30  {
31  }
32 
39  std::vector<Vector2>&& points)
40  : Curve<ContextType>{childEvaluator}, points(std::move(points))
41  {
42  }
43 
44  virtual float Sample(float argument) const override final
45  {
46  assert(points.size() > 1);
47 
48  if(argument < points.front().x)
49  {
50  return points.front().y;
51  }
52  else if(argument > points.back().x)
53  {
54  return points.back().y;
55  }
56 
57  auto iter = std::find_if(points.begin(),
58  points.end(),
59  [argument](const Vector2& point) { return point.x > argument; });
60  size_t secondPointIndex = std::distance(points.begin(), iter);
61  Vector2 firstPoint = points[secondPointIndex - 1];
62  Vector2 secondPoint = points[secondPointIndex];
63 
64  float deltaY = secondPoint.y - firstPoint.y;
65  float deltaX = secondPoint.x - firstPoint.x;
66  return firstPoint.y + deltaY * (argument - firstPoint.x) / deltaX;
67  }
68 
69  void SetPointY(std::size_t pointIndex, float y) { points[pointIndex].y = y; }
70 
75  std::vector<Vector2>& GetPoints() { return points; }
80  const std::vector<Vector2>& GetPoints() const { return points; }
81 
82  virtual EvaluatorType GetEvaluatorType() const override final
83  {
84  return EvaluatorType::CURVE_LINEAR_INTERPOLATED;
85  }
86 
87  private:
88  std::vector<Vector2> points{};
89  };
90  }
91 }
92 
93 #endif // GRAIL_LINEARLY_INTERPOLATED_CURVE_H
grail::curves::Curve
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:19
grail::curves::LinearlyInterpolatedCurve::LinearlyInterpolatedCurve
LinearlyInterpolatedCurve(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, std::vector< Vector2 > &&points)
LinearlyInterpolatedCurve - Constructor.
Definition: LinearlyInterpolatedCurve.hh:38
grail::curves::LinearlyInterpolatedCurve::GetPoints
std::vector< Vector2 > & GetPoints()
GetPoints.
Definition: LinearlyInterpolatedCurve.hh:75
grail::curves::LinearlyInterpolatedCurve::GetPoints
const std::vector< Vector2 > & GetPoints() const
GetPoints.
Definition: LinearlyInterpolatedCurve.hh:80
grail::curves::LinearlyInterpolatedCurve
The LinearlyInterpolatedCurve class -Curve consisting of multiple linear segments.
Definition: LinearlyInterpolatedCurve.hh:19
grail::utility::Evaluator
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:20
grail::curves::LinearlyInterpolatedCurve::LinearlyInterpolatedCurve
LinearlyInterpolatedCurve(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, const std::vector< Vector2 > &points)
LinearlyInterpolatedCurve - Constructor.
Definition: LinearlyInterpolatedCurve.hh:27
grail::curves::LinearlyInterpolatedCurve::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: LinearlyInterpolatedCurve.hh:44