MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_range.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_RANGE_H_1746895769
12#define INCGUARD_MGL_RANGE_H_1746895769
13
14#include <algorithm>
15#include <limits>
16#include <type_traits>
17
18namespace MGL
19{
20/* ------------------------------------------------------------------------- */
25/* ------------------------------------------------------------------------- */
26template <class T>
27class Range
28{
29public:
30 /* ------------------------------------------------------------------------- */
34 /* ------------------------------------------------------------------------- */
35 constexpr Range() noexcept = default;
36
37 /* ------------------------------------------------------------------------- */
43 /* ------------------------------------------------------------------------- */
44 constexpr Range(T begin, T end) noexcept
45 : _begin(begin)
46 , _end(end)
47 {
48 }
49
50 /* ------------------------------------------------------------------------- */
55 /* ------------------------------------------------------------------------- */
56 [[nodiscard]] constexpr T Begin() const noexcept
57 {
58 return _begin;
59 }
60
61 /* ------------------------------------------------------------------------- */
66 /* ------------------------------------------------------------------------- */
67 [[nodiscard]] constexpr T End() const noexcept
68 {
69 return _end;
70 }
71
72 /* ------------------------------------------------------------------------- */
78 /* ------------------------------------------------------------------------- */
79 [[nodiscard]] constexpr bool IsPositive() const noexcept
80 {
81 return _begin < _end;
82 }
83
84 /* ------------------------------------------------------------------------- */
90 /* ------------------------------------------------------------------------- */
91 [[nodiscard]] constexpr bool IsNegative() const noexcept
92 {
93 return !IsPositive();
94 }
95
96 /* ------------------------------------------------------------------------- */
102 /* ------------------------------------------------------------------------- */
103 [[nodiscard]] constexpr bool IsSingleValue() const noexcept
104 {
105 if constexpr (std::is_floating_point_v<T>)
106 {
107 return _end - _begin <= std::numeric_limits<T>::epsilon();
108 }
109 else
110 {
111 return _begin == _end;
112 }
113 }
114
115 /* ------------------------------------------------------------------------- */
121 /* ------------------------------------------------------------------------- */
122 [[nodiscard]] constexpr bool IsZero() const noexcept
123 {
124 if constexpr (std::is_floating_point_v<T>)
125 {
126 return std::abs(_begin) <= std::numeric_limits<T>::epsilon() &&
127 std::abs(_end) <= std::numeric_limits<T>::epsilon();
128 }
129 else
130 {
131 return _begin == 0 && _end == 0;
132 }
133 }
134
135 /* ------------------------------------------------------------------------- */
141 /* ------------------------------------------------------------------------- */
142 [[nodiscard]] constexpr T Next(T value) const noexcept
143 {
144 if (IsPositive() || IsSingleValue())
145 {
146 return ++value;
147 }
148 else
149 {
150 return --value;
151 }
152 }
153
154 /* ------------------------------------------------------------------------- */
160 /* ------------------------------------------------------------------------- */
161 [[nodiscard]] constexpr T Previous(T value) const noexcept
162 {
163 if (IsPositive() || IsSingleValue())
164 {
165 return --value;
166 }
167 else
168 {
169 return ++value;
170 }
171 }
172
173 /* ------------------------------------------------------------------------- */
180 /* ------------------------------------------------------------------------- */
181 [[nodiscard]] constexpr bool Contains(T value) const noexcept
182 {
183 if (IsPositive() || IsSingleValue())
184 {
185 return (value >= _begin) && (value <= _end);
186 }
187 else
188 {
189 return (value <= _begin) && (value >= _end);
190 }
191 }
192
193 /* ------------------------------------------------------------------------- */
199 /* ------------------------------------------------------------------------- */
200 [[nodiscard]] constexpr T Clamp(T value) const noexcept
201 {
202 if (IsSingleValue())
203 {
204 return _begin;
205 }
206 else if (IsPositive())
207 {
208 return std::clamp(value, _begin, _end);
209 }
210 else
211 {
212 return std::clamp(value, _end, _begin);
213 }
214 }
215
216 /* ------------------------------------------------------------------------- */
222 /* ------------------------------------------------------------------------- */
223 [[nodiscard]] constexpr float Normalize(T value) const noexcept
224 {
225 if (IsSingleValue())
226 {
227 return 1.0f;
228 }
229
230 const auto fBegin = static_cast<float>(_begin);
231 const auto fEnd = static_cast<float>(_end);
232 const auto fValue = static_cast<float>(value);
233
234 return (fValue - fBegin) / (fEnd - fBegin);
235 }
236
237 /* ------------------------------------------------------------------------- */
243 /* ------------------------------------------------------------------------- */
244 [[nodiscard]] constexpr T Scale(float normalizedValue) const noexcept
245 {
246 const auto fBegin = static_cast<float>(_begin);
247 const auto fEnd = static_cast<float>(_end);
248
249 return static_cast<T>(fBegin + ((fEnd - fBegin) * normalizedValue));
250 }
251
252private:
253 T _begin{0};
254 T _end{0};
255};
256} // namespace MGL
257
258#endif // INCGUARD_MGL_RANGE_H_1746895769
259
260// vim: et ts=4 sw=4 sts=4
値の範囲を表現するクラス
Definition mgl_range.h:28
constexpr bool IsSingleValue() const noexcept
値が範囲を持たない単一の値であるかを取得
Definition mgl_range.h:103
constexpr T Clamp(T value) const noexcept
指定した値を範囲内にクランプ
Definition mgl_range.h:200
constexpr bool IsPositive() const noexcept
値が正の方向に進むかを取得
Definition mgl_range.h:79
constexpr bool IsNegative() const noexcept
値が負の方向に進むかを取得
Definition mgl_range.h:91
constexpr bool Contains(T value) const noexcept
指定された値が範囲内に含まれているかを取得
Definition mgl_range.h:181
constexpr float Normalize(T value) const noexcept
値を0.0fから1.0fの値に正規化
Definition mgl_range.h:223
constexpr T Next(T value) const noexcept
指定された値の次の値を取得
Definition mgl_range.h:142
constexpr Range() noexcept=default
コンストラクタ
constexpr T End() const noexcept
終了値を取得
Definition mgl_range.h:67
constexpr T Previous(T value) const noexcept
指定された値の前の値を取得
Definition mgl_range.h:161
constexpr bool IsZero() const noexcept
範囲がゼロであるかを取得
Definition mgl_range.h:122
constexpr T Scale(float normalizedValue) const noexcept
正規化された値からスケーリング
Definition mgl_range.h:244
constexpr T Begin() const noexcept
開始値を取得
Definition mgl_range.h:56