Thu, 20 Jun 2019 08:54:35 +0300
uuid things
1319 | 1 | #pragma once |
2 | #include <functional> | |
3 | #include <cmath> | |
4 | #include "../basics.h" | |
5 | ||
6 | using std::abs; | |
1353 | 7 | using std::acos; |
8 | using std::asin; | |
9 | using std::atan; | |
1320
bdb4804bc09c
Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
10 | using std::atan2; |
1319 | 11 | using std::ceil; |
12 | using std::cos; | |
13 | using std::floor; | |
1353 | 14 | using std::function; |
1319 | 15 | using std::hypot; |
1353 | 16 | using std::log; |
17 | using std::log2; | |
18 | using std::log10; | |
1320
bdb4804bc09c
Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
19 | using std::pow; |
1319 | 20 | using std::sin; |
1320
bdb4804bc09c
Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
21 | using std::sort; |
1319 | 22 | using std::sqrt; |
23 | ||
24 | /* | |
25 | * Returns whether the argument is reasonably close to zero. | |
26 | */ | |
27 | template<typename T> | |
1429 | 28 | constexpr bool isZero(T a) |
1319 | 29 | { |
30 | return qFuzzyCompare(a + 1.0, 1.0); | |
31 | } | |
32 | ||
33 | template<typename T> | |
34 | bool isInteger(T a) | |
35 | { | |
36 | return (::abs(a - ::floor(a)) < 0.00001) or (::abs(a - ::ceil(a)) < 0.00001); | |
37 | } | |
38 | ||
1320
bdb4804bc09c
Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
39 | template<typename T> |
1430 | 40 | auto constexpr squared(T value) |
1320
bdb4804bc09c
Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
41 | { |
bdb4804bc09c
Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
42 | return value * value; |
bdb4804bc09c
Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
43 | } |
bdb4804bc09c
Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
44 | |
1319 | 45 | // |
46 | // Returns true if first arg is equal to any of the other args | |
47 | // | |
48 | template<typename T, typename Arg, typename... Args> | |
49 | bool isOneOf(const T& needle, const Arg& arg, const Args&... args) | |
50 | { | |
51 | if (needle == arg) | |
52 | return true; | |
53 | else | |
54 | return isOneOf(needle, args...); | |
55 | } | |
56 | ||
57 | template<typename T> | |
58 | bool isOneOf(const T&) | |
59 | { | |
60 | return false; | |
61 | } | |
62 | ||
63 | // http://stackoverflow.com/a/18204188/3629665 | |
64 | template<typename T> | |
1429 | 65 | constexpr int rotl10(T x) |
1319 | 66 | { |
1430 | 67 | return (x << 10) | ((x >> 22) & 0x000000ff); |
1319 | 68 | } |
69 | ||
70 | template<typename T> | |
1429 | 71 | constexpr int rotl20(T x) |
1319 | 72 | { |
1430 | 73 | return (x << 20) | ((x >> 12) & 0x000000ff); |
1319 | 74 | } |
75 | ||
76 | // | |
77 | // Get the amount of elements in something. | |
78 | // | |
1429 | 79 | template<typename T, int N> |
80 | constexpr int countof(T(&)[N]) | |
1319 | 81 | { |
82 | return N; | |
83 | } | |
84 | ||
1429 | 85 | [[maybe_unused]] |
1319 | 86 | static inline int countof(const QString& string) |
87 | { | |
88 | return string.length(); | |
89 | } | |
90 | ||
91 | template<typename T> | |
92 | int countof(const QVector<T>& vector) | |
93 | { | |
94 | return vector.size(); | |
95 | } | |
96 | ||
97 | template<typename T> | |
1419
f7c53002a990
replaced uses of QList with QVector
Teemu Piippo <teemu@hecknology.net>
parents:
1411
diff
changeset
|
98 | int countof(const QList<T>& list) |
1319 | 99 | { |
1419
f7c53002a990
replaced uses of QList with QVector
Teemu Piippo <teemu@hecknology.net>
parents:
1411
diff
changeset
|
100 | return list.size(); |
1319 | 101 | } |
102 | ||
103 | template<typename T> | |
104 | int countof(const QSet<T>& set) | |
105 | { | |
106 | return set.size(); | |
107 | } | |
108 | ||
109 | template<typename T> | |
110 | int countof(const std::initializer_list<T>& vector) | |
111 | { | |
112 | return vector.size(); | |
113 | } | |
114 | ||
115 | /* | |
1411
b48f3fd2664b
fixed generation of disc negatives
Teemu Piippo <teemu@hecknology.net>
parents:
1390
diff
changeset
|
116 | * Extracts the sign of 'value'. |
b48f3fd2664b
fixed generation of disc negatives
Teemu Piippo <teemu@hecknology.net>
parents:
1390
diff
changeset
|
117 | * From: https://stackoverflow.com/q/1903954 |
1319 | 118 | */ |
119 | template<typename T> | |
1429 | 120 | constexpr int sign(T value) |
1319 | 121 | { |
1411
b48f3fd2664b
fixed generation of disc negatives
Teemu Piippo <teemu@hecknology.net>
parents:
1390
diff
changeset
|
122 | return (0 < value) - (value < 0); |
1319 | 123 | } |
124 | ||
125 | /* | |
126 | * Returns the maximum of a single parameter (the parameter itself). | |
127 | */ | |
128 | template <typename T> | |
1429 | 129 | constexpr T max(T a) |
1319 | 130 | { |
131 | return a; | |
132 | } | |
133 | ||
134 | /* | |
135 | * Returns the maximum of two parameters. | |
136 | */ | |
137 | template <typename T> | |
1429 | 138 | constexpr T max(T a, T b) |
1319 | 139 | { |
140 | return a > b ? a : b; | |
141 | } | |
142 | ||
143 | /* | |
144 | * Returns the maximum of n parameters. | |
145 | */ | |
146 | template <typename T, typename... Rest> | |
1429 | 147 | constexpr T max(T a, Rest&&... rest) |
1319 | 148 | { |
149 | return max(a, max(rest...)); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Returns the minimum of a single parameter (the parameter itself). | |
154 | */ | |
155 | template <typename T> | |
1429 | 156 | constexpr T min(T a) |
1319 | 157 | { |
158 | return a; | |
159 | } | |
160 | ||
161 | /* | |
162 | * Returns the minimum of two parameters. | |
163 | */ | |
164 | template <typename T> | |
1429 | 165 | constexpr T min(T a, T b) |
1319 | 166 | { |
167 | return a < b ? a : b; | |
168 | } | |
169 | ||
170 | /* | |
171 | * Returns the minimum of n parameters. | |
172 | */ | |
173 | template <typename T, typename... Rest> | |
1429 | 174 | constexpr T min(T a, Rest&&... rest) |
1319 | 175 | { |
176 | return min(a, min(rest...)); | |
177 | } | |
178 | ||
179 | /* | |
180 | * Assigns the value of a single flag in a flagset | |
181 | */ | |
182 | template<int Flag, typename T> | |
183 | void assignFlag(QFlags<T>& flagset, bool value) | |
184 | { | |
185 | if (value) | |
186 | flagset |= static_cast<T>(Flag); | |
187 | else | |
188 | flagset &= ~static_cast<T>(Flag); | |
189 | } | |
190 | ||
191 | /* | |
192 | * Returns a singleton of type T, useful for providing a valid but unused | |
193 | * pointer. | |
194 | */ | |
195 | template<typename T> | |
1322
d8935cdb24c0
renamed sink() to singleton()
Teemu Piippo <teemu@hecknology.net>
parents:
1320
diff
changeset
|
196 | inline T& singleton() |
1319 | 197 | { |
198 | static T result; | |
199 | return result; | |
200 | } | |
201 | ||
202 | /* | |
203 | * Rounds the input value to the nearest multiple of the provided interval. | |
204 | */ | |
205 | template<typename T> | |
206 | T roundToInterval(T value, double interval) | |
207 | { | |
208 | return static_cast<T>(round(value / interval) * interval); | |
209 | } | |
210 | ||
1390
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
211 | /* |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
212 | * Returns the empty sum. (recursion base) |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
213 | */ |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
214 | template<typename T> |
1429 | 215 | constexpr T sum() |
1390
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
216 | { |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
217 | return {}; |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
218 | } |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
219 | |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
220 | /* |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
221 | * Returns the sum of n arguments. |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
222 | */ |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
223 | template<typename T, typename... Rest> |
1429 | 224 | constexpr T sum(const T& arg, Rest&&... rest) |
1390
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
225 | { |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
226 | return arg + sum<T>(rest...); |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
227 | } |
3eace926af7f
added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
1353
diff
changeset
|
228 | |
1319 | 229 | // Copy of qOverload so as to drop Qt version requirement from 5.7 to 5.5. |
230 | #if (QT_VERSION < QT_VERSION_CHECK(5, 7, 0)) | |
231 | template <typename... Args> | |
232 | struct QNonConstOverload | |
233 | { | |
234 | template <typename R, typename T> | |
235 | Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...)) const Q_DECL_NOTHROW -> decltype(ptr) | |
236 | { return ptr; } | |
237 | template <typename R, typename T> | |
238 | static Q_DECL_CONSTEXPR auto of(R (T::*ptr)(Args...)) Q_DECL_NOTHROW -> decltype(ptr) | |
239 | { return ptr; } | |
240 | }; | |
241 | template <typename... Args> | |
242 | struct QConstOverload | |
243 | { | |
244 | template <typename R, typename T> | |
245 | Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...) const) const Q_DECL_NOTHROW -> decltype(ptr) | |
246 | { return ptr; } | |
247 | template <typename R, typename T> | |
248 | static Q_DECL_CONSTEXPR auto of(R (T::*ptr)(Args...) const) Q_DECL_NOTHROW -> decltype(ptr) | |
249 | { return ptr; } | |
250 | }; | |
251 | template <typename... Args> | |
252 | struct QOverload : QConstOverload<Args...>, QNonConstOverload<Args...> | |
253 | { | |
254 | using QConstOverload<Args...>::of; | |
255 | using QConstOverload<Args...>::operator(); | |
256 | using QNonConstOverload<Args...>::of; | |
257 | using QNonConstOverload<Args...>::operator(); | |
258 | template <typename R> | |
259 | Q_DECL_CONSTEXPR auto operator()(R (*ptr)(Args...)) const Q_DECL_NOTHROW -> decltype(ptr) | |
260 | { return ptr; } | |
261 | template <typename R> | |
262 | static Q_DECL_CONSTEXPR auto of(R (*ptr)(Args...)) Q_DECL_NOTHROW -> decltype(ptr) | |
263 | { return ptr; } | |
264 | }; | |
265 | template <typename... Args> Q_CONSTEXPR Q_DECL_UNUSED QOverload<Args...> qOverload = {}; | |
266 | template <typename... Args> Q_CONSTEXPR Q_DECL_UNUSED QConstOverload<Args...> qConstOverload = {}; | |
267 | template <typename... Args> Q_CONSTEXPR Q_DECL_UNUSED QNonConstOverload<Args...> qNonConstOverload = {}; | |
268 | #endif |