(C++)  1.1.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
StaircaseFunction.hh
1 #ifndef GRAIL_STEP_FUNCTION_H
2 #define GRAIL_STEP_FUNCTION_H
3 
4 #include "Curve.hh"
5 
6 namespace grail
7 {
8  namespace curves
9  {
13  struct StepData
14  {
18  float value = 0.0f;
22  float startPoint = 0.0f;
26  bool inclusiveStartPoint = false;
27  };
28 
29  template <typename ContextType>
34  class StaircaseFunction final : public Curve<ContextType>
35  {
36  public:
42  StaircaseFunction(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator,
43  const std::vector<StepData>& data)
44  : Curve<ContextType>{childEvaluator}, data{data}
45  {
46 #ifndef NDEBUG
47  assert(data.size() >= 1);
48  float previousX = data[0].startPoint;
49  for (size_t i = 1; i < data.size(); ++i)
50  {
51  assert(previousX < data[i].startPoint);
52  previousX = data[i].startPoint;
53  }
54 #endif //NDEBUG
55  }
56 
57  virtual float Sample(float argument) const override final
58  {
59  for (std::size_t i = data.size() - 1; i > 0; --i)
60  {
61  if (argument > data[i].startPoint || (argument == data[i].startPoint && data[i].inclusiveStartPoint))
62  {
63  return data[i].value;
64  }
65  }
66  return data[0].value;
67  }
68 
73  std::vector<StepData>& GetData() { return data; }
78  const std::vector<StepData>& GetData() const { return data; }
79 
80  EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_STAIRCASE; }
81 
82  private:
83  std::vector<StepData> data{};
84  };
85  }
86 }
87 #endif // GRAIL_STEP_FUNCTION_H
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:21
The StaircaseFunction class - Function consisting of multiple discrete values.
Definition: StaircaseFunction.hh:35
const std::vector< StepData > & GetData() const
GetData.
Definition: StaircaseFunction.hh:78
std::vector< StepData > & GetData()
GetData.
Definition: StaircaseFunction.hh:73
StaircaseFunction(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, const std::vector< StepData > &data)
StaircaseFunction - Constructor.
Definition: StaircaseFunction.hh:42
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: StaircaseFunction.hh:57
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:21
The StepData struct - Helper structure describing discrete values used in Staircase function.
Definition: StaircaseFunction.hh:14
float startPoint
startPoint - Start point of half-line extending to the right and having constant given value.
Definition: StaircaseFunction.hh:22
float value
value - Y-axis value.
Definition: StaircaseFunction.hh:18
bool inclusiveStartPoint
inclusiveStartPoint - Determines whether exact start point also has given value. If not,...
Definition: StaircaseFunction.hh:26