Grail (C++)  1.3.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
PowerFunction.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_POWER_FUNCTION_H
4 #define GRAIL_POWER_FUNCTION_H
5 
6 #include <cmath>
7 #include "Curve.hh"
8 
9 namespace grail
10 {
11 namespace evaluator
12 {
13  template <typename ContextType>
18  class PowerFunction final : public Curve<ContextType>
19  {
20  public:
28  PowerFunction(std::shared_ptr<Evaluator<ContextType>> childEvaluator,
29  float slope,
30  float intercept,
31  float exponent)
32  : Curve<ContextType>{childEvaluator}, slope(slope), intercept(intercept), exponent(exponent)
33  {
34  }
35 
43  PowerFunction(std::shared_ptr<Evaluator<ContextType>> childEvaluator,
44  std::pair<float, float> firstPoint,
45  std::pair<float, float> secondPoint,
46  float exponent)
47  : Curve<ContextType>{childEvaluator}, exponent{exponent}
48  {
49  if(firstPoint.first > secondPoint.first)
50  {
51  float x = firstPoint.first;
52  float y = firstPoint.second;
53  firstPoint.first = secondPoint.first;
54  firstPoint.second = secondPoint.second;
55  secondPoint.first = x;
56  secondPoint.second = y;
57  }
58  slope = (std::pow(secondPoint.second, 1 / exponent) - std::pow(firstPoint.second, 1 / exponent)) / (
59  secondPoint.first - firstPoint.first);
60  intercept = std::pow(firstPoint.second, 1 / exponent) - (slope * firstPoint.first);
61  }
62 
63  virtual float Sample(float argument) const override final
64  {
65  float linearPart = slope * argument + intercept;
66  if(linearPart < 0.0f)
67  {
68  int intExponent = static_cast<int>(exponent);
69  if(exponent - intExponent > 0.0f)
70  {
71  return 0.0f;
72  }
73  }
74 
75  return std::pow(linearPart, exponent);
76  }
77 
82  float GetSlope() const { return slope; }
87  float GetIntercept() const { return intercept; }
92  float GetExponent() const { return exponent; }
93 
94  virtual data::EvaluatorType GetEvaluatorType() const override final { return data::EvaluatorType::CURVE_POWER; }
95 
96  private:
97  float slope = 0.0f;
98  float intercept = 0.0f;
99  float exponent = 0.0f;
100  };
101 }
102 }
103 
104 #endif // GRAIL_POWER_FUNCTION_H
grail::evaluator::PowerFunction::GetExponent
float GetExponent() const
GetExponent.
Definition: PowerFunction.hh:92
grail::evaluator::PowerFunction::PowerFunction
PowerFunction(std::shared_ptr< Evaluator< ContextType >> childEvaluator, float slope, float intercept, float exponent)
PowerFunction - Constructor.
Definition: PowerFunction.hh:28
grail::evaluator::PowerFunction::GetSlope
float GetSlope() const
GetSlope.
Definition: PowerFunction.hh:82
grail::evaluator::PowerFunction::PowerFunction
PowerFunction(std::shared_ptr< Evaluator< ContextType >> childEvaluator, std::pair< float, float > firstPoint, std::pair< float, float > secondPoint, float exponent)
PowerFunction - Constructor.
Definition: PowerFunction.hh:43
grail::evaluator::Evaluator
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:22
grail::evaluator::PowerFunction::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: PowerFunction.hh:63
grail::evaluator::PowerFunction::GetIntercept
float GetIntercept() const
GetIntercept.
Definition: PowerFunction.hh:87
grail::evaluator::PowerFunction
The PowerFunction class - Power function.
Definition: PowerFunction.hh:18
grail::evaluator::Curve
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:21