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