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