Utilities
Utilities header only library
Interval.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_INTERVAL_H
7 #define UTILS_INTERVAL_H
8 
9 #include "ExtGLM.h"
10 
11 
12 
13 namespace utils
14 {
15 
17  class Interval
18  {
19  public:
22  : Interval(0.0f, 1.0f)
23  {
24  }
25 
27  Interval(const float a, const float b, const float epsilon)
28  : min_(glm::min(a, b))
29  , max_(glm::max(a, b))
30  , epsilon_(epsilon)
31  {
32  }
33 
35  Interval(const float left, const float right)
36  : Interval(left, right, 0.0001f)
37  {
38  }
39 
41  bool operator==(const Interval &o) const
42  {
43  const float max_common_epsilon = std_ext::Max(epsilon_, o.epsilon_);
44  return
45  glm_ext::AreEqual(min_, o.min_, max_common_epsilon) &&
46  glm_ext::AreEqual(max_, o.max_, max_common_epsilon);
47  }
48 
50  bool operator!=(const Interval &o) const
51  {
52  return !(*this == o);
53  }
54 
56  float GetClamped(const float val) const
57  {
58  return glm::clamp(val, min_, max_);
59  }
60 
62  float NormToValue(const float t) const
63  {
64  return min_ + t * GetRange();
65  }
66 
68  float ValueToNorm(const float val) const
69  {
70  return (val - min_) / GetRange();
71  }
72 
74  float GetRange() const
75  {
76  return max_ - min_;
77  }
78 
80  bool IsEmpty() const
81  {
82  return glm_ext::AreEqual(min_, max_, epsilon_);
83  }
84 
86  float GetMin() const
87  {
88  return min_;
89  }
90 
92  float GetMax() const
93  {
94  return max_;
95  }
96 
98  float GetEpsilon() const
99  {
100  return epsilon_;
101  }
102 
103  protected:
104  float min_;
105  float max_;
106  float epsilon_;
107  };
108 
109 }
110 
111 #endif /* !UTILS_INTERVAL_H */
Holds float interval and performs operations on it.
Definition: Interval.h:17
Interval(const float a, const float b, const float epsilon)
Creates an interval <a,b> with custom numerical precision (epsilon).
Definition: Interval.h:27
bool operator!=(const Interval &o) const
Compares if two intervals are different.
Definition: Interval.h:50
float GetMax() const
Returns value of a maximal endpoint of the interval.
Definition: Interval.h:92
bool AreEqual(const T val1, const T val2, const T epsilon=std::numeric_limits< T >::epsilon())
Returns true if two values are equal within defined epsilon threshold.
Definition: ExtGLM.h:255
Interval()
Creates a default interval <0,1>.
Definition: Interval.h:21
Interval(const float left, const float right)
Creates an interval <a,b>.
Definition: Interval.h:35
T Max(const std::vector< T > &vector)
Finds a maximum value in a vector.
Definition: ExtSTD.h:509
bool IsEmpty() const
Check whether the range of interval is zero.
Definition: Interval.h:80
bool operator==(const Interval &o) const
Compares if two intervals are identical.
Definition: Interval.h:41
General namespace for utilities.
Definition: BulletConversions.h:9
float ValueToNorm(const float val) const
Returns normalized interval parameter from the real value.
Definition: Interval.h:68
float GetMin() const
Returns value of a minimal endpoint of the interval.
Definition: Interval.h:86
float GetClamped(const float val) const
Clamps a value to the interval endpoints.
Definition: Interval.h:56
float GetRange() const
Returns a distance between interval endpoints.
Definition: Interval.h:74
float NormToValue(const float t) const
Returns real value from the normalized interval parameter.
Definition: Interval.h:62
float GetEpsilon() const
Returns numerical precision of the interval.
Definition: Interval.h:98