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