Grail (C++)  1.3.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
LinearFunction.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_LINEAR_FUNCTION_H
4 #define GRAIL_LINEAR_FUNCTION_H
5 
6 #include "Curve.hh"
7 
8 namespace grail
9 {
10 namespace evaluator
11 {
12  template <typename ContextType>
17  class LinearFunction final : public Curve<ContextType>
18  {
19  public:
26  LinearFunction(std::shared_ptr<Evaluator<ContextType>> childEvaluator,
27  float slope,
28  float intercept)
29  : Curve<ContextType>{childEvaluator}, slope{slope}, intercept{intercept}
30  {
31  }
32 
39  LinearFunction(std::shared_ptr<Evaluator<ContextType>> childEvaluator,
40  std::pair<float, float> firstPoint,
41  std::pair<float, float> secondPoint)
42  : Curve<ContextType>{childEvaluator}
43  {
44  assert(firstPoint.first != secondPoint.first);
45  if(firstPoint.second == secondPoint.second)
46  {
47  slope = 0.0f;
48  intercept = firstPoint.second;
49  }
50  else
51  {
52  if(firstPoint.first > secondPoint.first)
53  {
54  float x = firstPoint.first;
55  float y = firstPoint.second;
56  firstPoint.first = secondPoint.first;
57  firstPoint.second = secondPoint.second;
58  secondPoint.first = x;
59  secondPoint.second = y;
60  }
61  slope = (secondPoint.second - firstPoint.second) / (secondPoint.first - firstPoint.first);
62  intercept = firstPoint.second - (slope * firstPoint.first);
63  }
64  }
65 
66  virtual float Sample(float argument) const override final
67  {
68  return slope * argument + intercept;
69  }
70 
75  float GetSlope() const { return slope; }
80  float GetIntercept() const { return intercept; }
81 
82  virtual data::EvaluatorType GetEvaluatorType() const override final { return data::EvaluatorType::CURVE_LINEAR; }
83 
84  private:
85  float slope = 0.0f;
86  float intercept = 0.0f;
87  };
88 }
89 }
90 
91 #endif // GRAIL_LINEAR_FUNCTION_H
grail::evaluator::LinearFunction
The LinearFunction class - Linear Function.
Definition: LinearFunction.hh:17
grail::evaluator::LinearFunction::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: LinearFunction.hh:66
grail::evaluator::LinearFunction::GetIntercept
float GetIntercept() const
GetIntercept.
Definition: LinearFunction.hh:80
grail::evaluator::LinearFunction::LinearFunction
LinearFunction(std::shared_ptr< Evaluator< ContextType >> childEvaluator, float slope, float intercept)
LinearFunction - Constructor.
Definition: LinearFunction.hh:26
grail::evaluator::Evaluator
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:22
grail::evaluator::LinearFunction::LinearFunction
LinearFunction(std::shared_ptr< Evaluator< ContextType >> childEvaluator, std::pair< float, float > firstPoint, std::pair< float, float > secondPoint)
LinearFunction - Constructor.
Definition: LinearFunction.hh:39
grail::evaluator::LinearFunction::GetSlope
float GetSlope() const
GetSlope.
Definition: LinearFunction.hh:75
grail::evaluator::Curve
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:21