(C++)  1.1.0
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  return points.front().y;
50  else if (argument > points.back().x)
51  return points.back().y;
52 
53  auto iter = std::find_if(points.begin(), points.end(), [argument](const Vector2& point) {return point.x > argument; });
54  size_t secondPointIndex = std::distance(points.begin(), iter);
55  Vector2 firstPoint = points[secondPointIndex - 1];
56  Vector2 secondPoint = points[secondPointIndex];
57 
58  float deltaY = secondPoint.y - firstPoint.y;
59  float deltaX = secondPoint.x - firstPoint.x;
60  return firstPoint.y + deltaY * (argument - firstPoint.x) / deltaX;
61  }
62 
63  void SetPointY(std::size_t pointIndex, float y) { points[pointIndex].y = y; }
64 
69  std::vector<Vector2>& GetPoints() { return points; }
74  const std::vector<Vector2>& GetPoints() const { return points; }
75 
76  EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_LINEAR_INTERPOLATED; }
77 
78  private:
79  std::vector<Vector2> points{};
80  };
81  }
82 }
83 
84 #endif // GRAIL_LINEARLY_INTERPOLATED_CURVE_H
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:21
The LinearlyInterpolatedCurve class -Curve consisting of multiple linear segments.
Definition: LinearlyInterpolatedCurve.hh:20
LinearlyInterpolatedCurve(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, const std::vector< Vector2 > &points)
LinearlyInterpolatedCurve - Constructor.
Definition: LinearlyInterpolatedCurve.hh:27
LinearlyInterpolatedCurve(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, std::vector< Vector2 > &&points)
LinearlyInterpolatedCurve - Constructor.
Definition: LinearlyInterpolatedCurve.hh:38
const std::vector< Vector2 > & GetPoints() const
GetPoints.
Definition: LinearlyInterpolatedCurve.hh:74
std::vector< Vector2 > & GetPoints()
GetPoints.
Definition: LinearlyInterpolatedCurve.hh:69
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: LinearlyInterpolatedCurve.hh:44
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:21