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 <type_traits>
16#include <limits>
17
18namespace MGL
19{
20/* ------------------------------------------------------------------------- */
25/* ------------------------------------------------------------------------- */
26template <class T>
27class Range
28{
29public:
30 /* ------------------------------------------------------------------------- */
36 /* ------------------------------------------------------------------------- */
37 constexpr Range(T begin, T end) noexcept
38 : _begin(begin)
39 , _end(end)
40 {
41 }
42
43 /* ------------------------------------------------------------------------- */
48 /* ------------------------------------------------------------------------- */
49 [[nodiscard]] constexpr T Begin() const noexcept
50 {
51 return _begin;
52 }
53
54 /* ------------------------------------------------------------------------- */
59 /* ------------------------------------------------------------------------- */
60 [[nodiscard]] constexpr T End() const noexcept
61 {
62 return _end;
63 }
64
65 /* ------------------------------------------------------------------------- */
71 /* ------------------------------------------------------------------------- */
72 [[nodiscard]] constexpr bool IsPositive() const noexcept
73 {
74 return _begin < _end;
75 }
76
77 /* ------------------------------------------------------------------------- */
83 /* ------------------------------------------------------------------------- */
84 [[nodiscard]] constexpr bool IsNegative() const noexcept
85 {
86 return !IsPositive();
87 }
88
89 /* ------------------------------------------------------------------------- */
95 /* ------------------------------------------------------------------------- */
96 [[nodiscard]] constexpr bool IsSingleValue() const noexcept
97 {
98 if constexpr (std::is_floating_point_v<T>)
99 {
100 return _end - _begin <= std::numeric_limits<T>::epsilon();
101 }
102 else
103 {
104 return _begin == _end;
105 }
106 }
107
108 /* ------------------------------------------------------------------------- */
114 /* ------------------------------------------------------------------------- */
115 [[nodiscard]] constexpr T Next(T value) const noexcept
116 {
117 if (IsPositive() || IsSingleValue())
118 {
119 return ++value;
120 }
121 else
122 {
123 return --value;
124 }
125 }
126
127 /* ------------------------------------------------------------------------- */
133 /* ------------------------------------------------------------------------- */
134 [[nodiscard]] constexpr T Previous(T value) const noexcept
135 {
136 if (IsPositive() || IsSingleValue())
137 {
138 return --value;
139 }
140 else
141 {
142 return ++value;
143 }
144 }
145
146 /* ------------------------------------------------------------------------- */
153 /* ------------------------------------------------------------------------- */
154 [[nodiscard]] constexpr bool Contains(T value) const noexcept
155 {
156 if (IsPositive() || IsSingleValue())
157 {
158 return (value >= _begin) && (value <= _end);
159 }
160 else
161 {
162 return (value <= _begin) && (value >= _end);
163 }
164 }
165
166 /* ------------------------------------------------------------------------- */
172 /* ------------------------------------------------------------------------- */
173 [[nodiscard]] constexpr T Clamp(T value) const noexcept
174 {
175 if (IsSingleValue())
176 {
177 return _begin;
178 }
179 else if (IsPositive())
180 {
181 return std::clamp(value, _begin, _end);
182 }
183 else
184 {
185 return std::clamp(value, _end, _begin);
186 }
187 }
188
189 /* ------------------------------------------------------------------------- */
195 /* ------------------------------------------------------------------------- */
196 [[nodiscard]] constexpr float Normalize(T value) const noexcept
197 {
198 if (IsSingleValue())
199 {
200 return 1.0f;
201 }
202
203 const auto fBegin = static_cast<float>(_begin);
204 const auto fEnd = static_cast<float>(_end);
205 const auto fValue = static_cast<float>(value);
206
207 return (fValue - fBegin) / (fEnd - fBegin);
208 }
209
210 /* ------------------------------------------------------------------------- */
216 /* ------------------------------------------------------------------------- */
217 [[nodiscard]] constexpr T Scale(float normalizedValue) const noexcept
218 {
219 const auto fBegin = static_cast<float>(_begin);
220 const auto fEnd = static_cast<float>(_end);
221
222 return static_cast<T>(fBegin + ((fEnd - fBegin) * normalizedValue));
223 }
224
225private:
226 T _begin{0};
227 T _end{0};
228};
229} // namespace MGL
230
231#endif // INCGUARD_MGL_RANGE_H_1746895769
232
233// vim: et ts=4 sw=4 sts=4
値の範囲を表現するクラス
Definition mgl_range.h:28
constexpr bool IsSingleValue() const noexcept
値が範囲を持たない単一の値であるかを取得
Definition mgl_range.h:96
constexpr T Clamp(T value) const noexcept
指定した値を範囲内にクランプ
Definition mgl_range.h:173
constexpr bool IsPositive() const noexcept
値が正の方向に進むかを取得
Definition mgl_range.h:72
constexpr bool IsNegative() const noexcept
値が負の方向に進むかを取得
Definition mgl_range.h:84
constexpr bool Contains(T value) const noexcept
指定された値が範囲内に含まれているかを取得
Definition mgl_range.h:154
constexpr float Normalize(T value) const noexcept
値を0.0fから1.0fの値に正規化
Definition mgl_range.h:196
constexpr T Next(T value) const noexcept
指定された値の次の値を取得
Definition mgl_range.h:115
constexpr Range(T begin, T end) noexcept
コンストラクタ
Definition mgl_range.h:37
constexpr T End() const noexcept
終了値を取得
Definition mgl_range.h:60
constexpr T Previous(T value) const noexcept
指定された値の前の値を取得
Definition mgl_range.h:134
constexpr T Scale(float normalizedValue) const noexcept
正規化された値からスケーリング
Definition mgl_range.h:217
constexpr T Begin() const noexcept
開始値を取得
Definition mgl_range.h:49