(C++)  1.0.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 <utility>
5 #include <vector>
6 #include "../../libs/vector2/vector2.h"
7 #include "../../GrailData/UtilityModel/CurveTypeId.h"
8 
9 namespace grail
10 {
11  namespace curves
12  {
16  class Curve
17  {
18  public:
19  Curve() = default;
20  Curve(const Curve& other) = default;
21  Curve(Curve&& other) = default;
22  virtual ~Curve() = default;
23 
29  virtual float Sample(float argument) const = 0;
30 
31  virtual CurveTypeId GetTypeId() const = 0;
32  };
33 
34  template <typename T>
38  class LowerBound final : public Curve
39  {
40  public:
41  template <typename... ConstructorArguments>
42  LowerBound(float initLowerBound, ConstructorArguments&&... arguments)
43  : curve{ std::forward<ConstructorArguments>(arguments)... }, lowerBound{ initLowerBound }
44  {
45  }
46 
47  virtual float Sample(float argument) const override
48  {
49  float result = curve.Sample(argument);
50  return result < lowerBound ? lowerBound : result;
51  }
52 
53  const T& GetCurve() const
54  {
55  return curve;
56  }
57 
58  float GetLowerBound() const
59  {
60  return lowerBound;
61  }
62 
63  CurveTypeId GetTypeId() const override
64  {
65  return curve.GetTypeId();
66  }
67 
68  private:
69  T curve{};
70  float lowerBound = 0.0f;
71  };
72 
73  template <typename T>
77  class UpperBound final : public Curve
78  {
79  public:
80  template <typename... ConstructorArguments>
81  UpperBound(float initUpperBound, ConstructorArguments&&... arguments)
82  : curve{ std::forward<ConstructorArguments>(arguments)... }, upperBound{ initUpperBound }
83  {
84  }
85 
86  virtual float Sample(float argument) const override
87  {
88  float result = curve.Sample(argument);
89  return result > upperBound ? upperBound : result;
90  }
91 
92  const T& GetCurve() const
93  {
94  return curve;
95  }
96 
97  float GetUpperBound() const
98  {
99  return upperBound;
100  }
101 
102  CurveTypeId GetTypeId() const override
103  {
104  return curve.GetTypeId();
105  }
106 
107  private:
108  T curve{};
109  float upperBound = 0.0f;
110  };
111 
112  template <typename T>
113  using BoundedCurve = LowerBound<UpperBound<T>>;
114 
115  std::vector<Vector2> DiscretizeCurve(const Curve& curve, std::size_t samplesQuantity);
116  }
117 }
118 #endif //GRAIL_CURVE_H
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:17
virtual float Sample(float argument) const =0
Sample - User-defined method which processes provided value (currently vector of values - multidemens...
The LowerBound class - Can hold arbitrary curve, but lowerbounds it to provided value.
Definition: Curve.hh:39
virtual float Sample(float argument) const override
Sample - User-defined method which processes provided value (currently vector of values - multidemens...
Definition: Curve.hh:47
The UpperBound class - Can hold arbitrary curve, but upperbounds it to provided value.
Definition: Curve.hh:78
virtual float Sample(float argument) const override
Sample - User-defined method which processes provided value (currently vector of values - multidemens...
Definition: Curve.hh:86