1 #ifndef GRAIL_LINEARLY_INTERPOLATED_CURVE_H
2 #define GRAIL_LINEARLY_INTERPOLATED_CURVE_H
5 #include "../../libs/vector2/vector2.h"
14 template <
typename ContextType>
28 const std::vector<Vector2>& points)
29 :
Curve<ContextType>{childEvaluator}, points(points)
39 std::vector<Vector2>&& points)
40 :
Curve<ContextType>{childEvaluator}, points(std::move(points))
44 virtual float Sample(
float argument)
const override final
46 assert(points.size() > 1);
48 if (argument < points.front().x)
49 return points.front().y;
50 else if (argument > points.back().x)
51 return points.back().y;
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];
58 float deltaY = secondPoint.y - firstPoint.y;
59 float deltaX = secondPoint.x - firstPoint.x;
60 return firstPoint.y + deltaY * (argument - firstPoint.x) / deltaX;
63 void SetPointY(std::size_t pointIndex,
float y) { points[pointIndex].y = y; }
69 std::vector<Vector2>&
GetPoints() {
return points; }
74 const std::vector<Vector2>&
GetPoints()
const {
return points; }
76 EvaluatorType GetEvaluatorType() const override final {
return EvaluatorType::CURVE_LINEAR_INTERPOLATED; }
79 std::vector<Vector2> points{};
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