(C++)  1.1.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
Curve.hh
1 #ifndef GRAIL_CURVE_H
2 #define GRAIL_CURVE_H
3 
4 #include "../../libs/vector2/vector2.h"
5 #include "../Evaluator.hh"
6 
7 #include <utility>
8 #include <vector>
9 
10 namespace grail
11 {
12  namespace curves
13  {
14 
19  template <typename ContextType>
20  class Curve : public utility::Evaluator<ContextType>
21  {
22  public:
27  explicit Curve(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator)
28  : childEvaluator{childEvaluator}
29  {
30  }
31 
32  Curve(const Curve<ContextType>& other) = default;
33  Curve(Curve<ContextType>&& other) = default;
34  virtual ~Curve() = default;
35 
41  virtual float Sample(float argument) const = 0;
42 
43  virtual float Evaluate(const ContextType& context, UtilityEvaluatorSnapshot* const snapshot) const override final
44  {
45  return Sample(childEvaluator->EvaluateContext(context, snapshot));
46  }
47 
48  protected:
49  virtual void DebugDump(const std::map<const void*, std::size_t>& nodeMapping, EvaluationDebugData& debugData) const override final
50  {
51  debugData.AddChildNode(nodeMapping.at(childEvaluator.get()));
52  }
53 
54  private:
55  std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator{};
56  };
57 
58  template <typename ContextType>
63  class LowerBound final : public Curve<ContextType>
64  {
65  public:
71  LowerBound(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator, float lowerBound)
72  : Curve<ContextType>(childEvaluator), lowerBound{ lowerBound }
73  {
74  }
75 
76  virtual float Sample(float argument) const override final
77  {
78  return std::max(lowerBound, argument);
79  }
80 
81  EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_LOWER_BOUND; }
82 
83  private:
84  float lowerBound = 0.0f;
85  };
86 
87  template <typename ContextType>
92  class UpperBound final : public Curve<ContextType>
93  {
94  public:
100  UpperBound(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator, float upperBound)
101  : Curve<ContextType>(childEvaluator), upperBound{ upperBound }
102  {
103  }
104 
105  virtual float Sample(float argument) const override final
106  {
107  return std::min(upperBound, argument);
108  }
109 
110  EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_UPPER_BOUND; }
111 
112  private:
113  float upperBound = 0.0f;
114  };
115 
116  template <typename ContextType>
121  class DoubleSidedBound final : public Curve<ContextType>
122  {
123  public:
130  DoubleSidedBound(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator,
131  float lowerBound,
132  float upperBound)
133  : Curve<ContextType>{childEvaluator}, lowerBound{lowerBound}, upperBound{upperBound}
134  {
135  }
136 
137  virtual float Sample(float argument) const override final
138  {
139  return std::min(upperBound, std::max(lowerBound, argument));
140  }
141 
142  EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_DOUBLE_SIDED_BOUND; }
143 
144  private:
145  float lowerBound = 0.0f;
146  float upperBound = 0.0f;
147  };
148 
149  template<typename ContextType>
156  std::vector<Vector2> DiscretizeCurve(const Curve<ContextType>& curve, std::size_t samplesQuantity)
157  {
158  std::vector<Vector2> controlPoints;
159  float increment = 1.0f / (samplesQuantity - 1);
160  for (float i = 0; i <= 1; i += increment)
161  {
162  controlPoints.push_back({ i, curve.Sample(i) });
163  }
164  return controlPoints;
165  }
166  }
167 }
168 #endif //GRAIL_CURVE_H
The EvaluationDebugData class - debug data describing singular evaluator.
Definition: EvaluationDebugData.h:20
The UtilityEvaluatorSnapshot class - debug snapshot of whole evaluator tree assigned to evaluated obj...
Definition: UtilityEvaluatorSnapshot.h:22
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:21
virtual float Evaluate(const ContextType &context, UtilityEvaluatorSnapshot *const snapshot) const override final
Evaluate - Called from EvaluateContext which also evaluates context, but without automatically fillin...
Definition: Curve.hh:43
virtual float Sample(float argument) const =0
Sample - Transforms argument into output value depending on the type of Curve.
Curve(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator)
Curve - Constructor.
Definition: Curve.hh:27
virtual void DebugDump(const std::map< const void *, std::size_t > &nodeMapping, EvaluationDebugData &debugData) const override final
DebugDump - Called from EvaluateContext, which generates additional debug data for each evaluator....
Definition: Curve.hh:49
The DoubleSidedBound class - Bounds output of provided Evaluator from both sides.
Definition: Curve.hh:122
DoubleSidedBound(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float lowerBound, float upperBound)
DoubleSidedBound - Constructor.
Definition: Curve.hh:130
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: Curve.hh:137
The LowerBound class - Lower-bounds output of provided Evaluator.
Definition: Curve.hh:64
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: Curve.hh:76
LowerBound(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float lowerBound)
LowerBound - Constructor.
Definition: Curve.hh:71
The UpperBound class - Upper-bounds output of provided Evaluator.
Definition: Curve.hh:93
UpperBound(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float upperBound)
UpperBound - Constructor.
Definition: Curve.hh:100
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: Curve.hh:105
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:21