Grail (C++)  1.1.1
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 "../Evaluator.hh"
5 #include "../../libs/vector2/vector2.h"
6 
7 #include <utility>
8 #include <vector>
9 
10 namespace grail
11 {
12  namespace curves
13  {
18  template <typename ContextType>
19  class Curve : public utility::Evaluator<ContextType>
20  {
21  public:
26  explicit Curve(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator)
27  : childEvaluator{childEvaluator}
28  {
29  }
30 
31  Curve(const Curve<ContextType>& other) = default;
32  Curve(Curve<ContextType>&& other) = default;
33  virtual ~Curve() = default;
34 
40  virtual float Sample(float argument) const = 0;
41 
42  virtual float
43  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,
50  EvaluationDebugData& debugData) const override final
51  {
52  debugData.AddChildNode(nodeMapping.at(childEvaluator.get()));
53  }
54 
55  private:
56  std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator{};
57  };
58 
59  template <typename ContextType>
64  class LowerBound final : public Curve<ContextType>
65  {
66  public:
72  LowerBound(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator, float lowerBound)
73  : Curve<ContextType>(childEvaluator), lowerBound{lowerBound}
74  {
75  }
76 
77  virtual float Sample(float argument) const override final
78  {
79  return std::max(lowerBound, argument);
80  }
81 
82  virtual EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_LOWER_BOUND; }
83 
84  private:
85  float lowerBound = 0.0f;
86  };
87 
88  template <typename ContextType>
93  class UpperBound final : public Curve<ContextType>
94  {
95  public:
101  UpperBound(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator, float upperBound)
102  : Curve<ContextType>(childEvaluator), upperBound{upperBound}
103  {
104  }
105 
106  virtual float Sample(float argument) const override final
107  {
108  return std::min(upperBound, argument);
109  }
110 
111  virtual EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_UPPER_BOUND; }
112 
113  private:
114  float upperBound = 0.0f;
115  };
116 
117  template <typename ContextType>
122  class DoubleSidedBound final : public Curve<ContextType>
123  {
124  public:
131  DoubleSidedBound(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator,
132  float lowerBound,
133  float upperBound)
134  : Curve<ContextType>{childEvaluator}, lowerBound{lowerBound}, upperBound{upperBound}
135  {
136  }
137 
138  virtual float Sample(float argument) const override final
139  {
140  return std::min(upperBound, std::max(lowerBound, argument));
141  }
142 
143  virtual EvaluatorType GetEvaluatorType() const override final
144  {
145  return EvaluatorType::CURVE_DOUBLE_SIDED_BOUND;
146  }
147 
148  private:
149  float lowerBound = 0.0f;
150  float upperBound = 0.0f;
151  };
152 
153  template <typename ContextType>
160  std::vector<Vector2> DiscretizeCurve(const Curve<ContextType>& curve, std::size_t samplesQuantity)
161  {
162  std::vector<Vector2> controlPoints;
163  float increment = 1.0f / (samplesQuantity - 1);
164  for(float i = 0; i <= 1; i += increment)
165  {
166  controlPoints.push_back({i, curve.Sample(i)});
167  }
168  return controlPoints;
169  }
170  }
171 }
172 #endif //GRAIL_CURVE_H
grail::curves::DoubleSidedBound::DoubleSidedBound
DoubleSidedBound(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float lowerBound, float upperBound)
DoubleSidedBound - Constructor.
Definition: Curve.hh:131
grail::curves::Curve
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:19
grail::curves::LowerBound::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: Curve.hh:77
grail::curves::UpperBound::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: Curve.hh:106
grail::curves::Curve::Evaluate
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
grail::UtilityEvaluatorSnapshot
The UtilityEvaluatorSnapshot class - debug snapshot of whole evaluator tree assigned to evaluated obj...
Definition: UtilityEvaluatorSnapshot.h:21
grail::curves::DoubleSidedBound::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: Curve.hh:138
grail::curves::Curve::DebugDump
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
grail::curves::UpperBound
The UpperBound class - Upper-bounds output of provided Evaluator.
Definition: Curve.hh:93
grail::curves::Curve::Curve
Curve(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator)
Curve - Constructor.
Definition: Curve.hh:26
grail::EvaluationDebugData
The EvaluationDebugData class - debug data describing singular evaluator.
Definition: EvaluationDebugData.h:19
grail::curves::LowerBound
The LowerBound class - Lower-bounds output of provided Evaluator.
Definition: Curve.hh:64
grail::utility::Evaluator
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:20
grail::curves::DoubleSidedBound
The DoubleSidedBound class - Bounds output of provided Evaluator from both sides.
Definition: Curve.hh:122
grail::curves::UpperBound::UpperBound
UpperBound(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float upperBound)
UpperBound - Constructor.
Definition: Curve.hh:101
grail::curves::LowerBound::LowerBound
LowerBound(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float lowerBound)
LowerBound - Constructor.
Definition: Curve.hh:72
grail::curves::Curve::Sample
virtual float Sample(float argument) const =0
Sample - Transforms argument into output value depending on the type of Curve.