Euphoria
range.h
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
5#include "base/angle.h"
6#include "assert/assert.h"
7
8
9namespace eu
10{
17 template <typename T>
18 struct R
19 {
22
28
29 [[nodiscard]] T
31 {
32 return upper_bound - lower_bound;
33 }
34 };
35
36
38 template <typename T>
40 {
41 return {min, max};
42 }
43
45 template <typename T>
47 {
48 return R<T>(0, max);
49 }
50
52 constexpr R<float> r01 = { 0.0f, 1.0f};
53
54 // A range going from -1 to 1
55 constexpr R<float> r11 = { -1.0f, 1.0};
56
57 float from_01f(float lower_bound, float upper_bound, float value);
58
60 template <typename T>
61 T from_01(const R<T>& range, float value)
62 {
63 const float r = from_01f
64 (
65 static_cast<float>(range.lower_bound),
66 static_cast<float>(range.upper_bound),
67 value
68 );
69
70 if constexpr (std::is_unsigned<T>::value)
71 {
72 ASSERT(r >= 0.0f);
73 }
74
75 return static_cast<T>(r);
76 }
77
78 template <>
79 float from_01(const R<float>& range, float value);
80
82 template <typename T>
83 float to01(const R<T>& range, T value)
84 {
85 return (value - range.lower_bound)
87 }
88
89 // inclusive
90 template <typename T>
91 T get360_angular(const R<T>& range, float value)
92 {
93 const float half_difference
97 }
98
100 template <typename T, typename F>
101 T remap_to(const R<F>& from, const R<T>& to, F value)
102 {
103 return from_01(to, to01(from, value));
104 }
105
107 template <typename T>
108 bool is_within(const R<T>& range, T value)
109 {
110 return value >= range.lower_bound && value <= range.upper_bound;
111 }
112
116 template <typename T>
117 T keep_within(const R<T>& range, T value)
118 {
119 if(value > range.upper_bound)
120 {
121 return range.upper_bound;
122 }
123 if(value < range.lower_bound)
124 {
125 return range.lower_bound;
126 }
127
128 return value;
129 }
130
133 template <typename T>
134 T wrap(const R<T>& range, T value)
135 {
137 ASSERT(diff > 0);
138 T wrapped = value - range.lower_bound;
139 while(wrapped < 0)
140 {
141 wrapped += diff;
142 }
143 while(wrapped > diff)
144 {
145 wrapped -= diff;
146 }
147 return range.lower_bound + wrapped;
148 }
149
151}
152
#define ASSERTX(x,...)
Assert that a value is true.
Definition assert.h:59
#define ASSERT(x)
Assert that a value is true.
Definition assert.h:36
constexpr R< float > r01
A range going from 0 to 1.
Definition range.h:52
float from_01f(float lower_bound, float upper_bound, float value)
T get360_angular(const R< T > &range, float value)
Definition range.h:91
T keep_within(const R< T > &range, T value)
Returns a value that is kept within the range.
Definition range.h:117
bool is_within(const R< T > &range, T value)
Returns true if a value is withing a range.
Definition range.h:108
T remap_to(const R< F > &from, const R< T > &to, F value)
Remaps a value from one range to another.
Definition range.h:101
float to01(const R< T > &range, T value)
Converts a value in a range to the 0-1 range.
Definition range.h:83
R< T > make_range(T min, T max)
Create a range from min to max (inclusive)
Definition range.h:39
T wrap(const R< T > &range, T value)
Returns a value that wraps around the range.
Definition range.h:134
T from_01(const R< T > &range, float value)
Converts a value in 0-1 range to a custom range.
Definition range.h:61
constexpr float min(float lhs, float rhs)
Definition numeric.h:78
constexpr R< float > r11
Definition range.h:55
float cos(const An &ang)
constexpr float max(float lhs, float rhs)
Definition numeric.h:79
Definition assert.h:109
constexpr float from_percent_of_360() const
Definition angle.h:55
A (inclusive) range between two values.
Definition range.h:19
T upper_bound
Definition range.h:21
T lower_bound
Definition range.h:20
constexpr R(T min, T max)
Asserts if max is below min.
Definition range.h:24
T get_distance() const
Definition range.h:30