Grail (C++)  1.1.1
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 <cmath>
5 #include "Curve.hh"
6 
7 namespace grail
8 {
9  namespace curves
10  {
11  template <typename ContextType>
16  class PowerFunction final : public Curve<ContextType>
17  {
18  public:
26  PowerFunction(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator,
27  float slope,
28  float intercept,
29  float exponent)
30  : Curve<ContextType>{childEvaluator}, slope(slope), intercept(intercept), exponent(exponent)
31  {
32  }
33 
41  PowerFunction(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator,
42  std::pair<float, float> firstPoint,
43  std::pair<float, float> secondPoint,
44  float exponent)
45  : Curve<ContextType>{childEvaluator}, exponent{exponent}
46  {
47  if(firstPoint.first > secondPoint.first)
48  {
49  float x = firstPoint.first;
50  float y = firstPoint.second;
51  firstPoint.first = secondPoint.first;
52  firstPoint.second = secondPoint.second;
53  secondPoint.first = x;
54  secondPoint.second = y;
55  }
56  slope = (std::pow(secondPoint.second, 1 / exponent) - std::pow(firstPoint.second, 1 / exponent)) / (
57  secondPoint.first - firstPoint.first);
58  intercept = std::pow(firstPoint.second, 1 / exponent) - (slope * firstPoint.first);
59  }
60 
61  virtual float Sample(float argument) const override final
62  {
63  float linearPart = slope * argument + intercept;
64  if(linearPart < 0.0f)
65  {
66  int intExponent = static_cast<int>(exponent);
67  if(exponent - intExponent > 0.0f)
68  {
69  return 0.0f;
70  }
71  }
72 
73  return std::pow(linearPart, exponent);
74  }
75 
80  float GetSlope() const { return slope; }
85  float GetIntercept() const { return intercept; }
90  float GetExponent() const { return exponent; }
91 
92  virtual EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_POWER; }
93 
94  private:
95  float slope = 0.0f;
96  float intercept = 0.0f;
97  float exponent = 0.0f;
98  };
99  }
100 }
101 
102 #endif // GRAIL_POWER_FUNCTION_H
grail::curves::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:61
grail::curves::Curve
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:19
grail::curves::PowerFunction::GetSlope
float GetSlope() const
GetSlope.
Definition: PowerFunction.hh:80
grail::curves::PowerFunction
The PowerFunction class - Power function.
Definition: PowerFunction.hh:16
grail::curves::PowerFunction::PowerFunction
PowerFunction(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float slope, float intercept, float exponent)
PowerFunction - Constructor.
Definition: PowerFunction.hh:26
grail::curves::PowerFunction::PowerFunction
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:41
grail::utility::Evaluator
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:20
grail::curves::PowerFunction::GetExponent
float GetExponent() const
GetExponent.
Definition: PowerFunction.hh:90
grail::curves::PowerFunction::GetIntercept
float GetIntercept() const
GetIntercept.
Definition: PowerFunction.hh:85