diff options
| author | 2018-08-07 21:32:05 -0400 | |
|---|---|---|
| committer | 2018-08-07 21:32:05 -0400 | |
| commit | 5c323d96e034c49e60dccb966a1ab753239b1ce6 (patch) | |
| tree | 06dbc15b0909acc8dcc5f6457bcac0c1cd6c9a32 /src/common/vector_math.h | |
| parent | vector_math: Convert typedefs to type aliases (diff) | |
| download | yuzu-5c323d96e034c49e60dccb966a1ab753239b1ce6.tar.gz yuzu-5c323d96e034c49e60dccb966a1ab753239b1ce6.tar.xz yuzu-5c323d96e034c49e60dccb966a1ab753239b1ce6.zip | |
vector_math: Make functions constexpr where applicable
Diffstat (limited to 'src/common/vector_math.h')
| -rw-r--r-- | src/common/vector_math.h | 333 |
1 files changed, 179 insertions, 154 deletions
diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 108399ae8..7e5af651a 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h | |||
| @@ -43,70 +43,71 @@ template <typename T> | |||
| 43 | class Vec4; | 43 | class Vec4; |
| 44 | 44 | ||
| 45 | template <typename T> | 45 | template <typename T> |
| 46 | static inline Vec2<T> MakeVec(const T& x, const T& y); | ||
| 47 | template <typename T> | ||
| 48 | static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z); | ||
| 49 | template <typename T> | ||
| 50 | static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w); | ||
| 51 | |||
| 52 | template <typename T> | ||
| 53 | class Vec2 { | 46 | class Vec2 { |
| 54 | public: | 47 | public: |
| 55 | T x{}; | 48 | T x{}; |
| 56 | T y{}; | 49 | T y{}; |
| 57 | 50 | ||
| 58 | Vec2() = default; | 51 | constexpr Vec2() = default; |
| 59 | Vec2(const T& _x, const T& _y) : x(_x), y(_y) {} | 52 | constexpr Vec2(const T& x_, const T& y_) : x(x_), y(y_) {} |
| 60 | 53 | ||
| 61 | template <typename T2> | 54 | template <typename T2> |
| 62 | Vec2<T2> Cast() const { | 55 | constexpr Vec2<T2> Cast() const { |
| 63 | return Vec2<T2>((T2)x, (T2)y); | 56 | return Vec2<T2>(static_cast<T2>(x), static_cast<T2>(y)); |
| 64 | } | 57 | } |
| 65 | 58 | ||
| 66 | static Vec2 AssignToAll(const T& f) { | 59 | static constexpr Vec2 AssignToAll(const T& f) { |
| 67 | return Vec2<T>(f, f); | 60 | return Vec2{f, f}; |
| 68 | } | 61 | } |
| 69 | 62 | ||
| 70 | Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const { | 63 | constexpr Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const { |
| 71 | return MakeVec(x + other.x, y + other.y); | 64 | return {x + other.x, y + other.y}; |
| 72 | } | 65 | } |
| 73 | void operator+=(const Vec2& other) { | 66 | constexpr Vec2& operator+=(const Vec2& other) { |
| 74 | x += other.x; | 67 | x += other.x; |
| 75 | y += other.y; | 68 | y += other.y; |
| 69 | return *this; | ||
| 76 | } | 70 | } |
| 77 | Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const { | 71 | constexpr Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const { |
| 78 | return MakeVec(x - other.x, y - other.y); | 72 | return {x - other.x, y - other.y}; |
| 79 | } | 73 | } |
| 80 | void operator-=(const Vec2& other) { | 74 | constexpr Vec2& operator-=(const Vec2& other) { |
| 81 | x -= other.x; | 75 | x -= other.x; |
| 82 | y -= other.y; | 76 | y -= other.y; |
| 77 | return *this; | ||
| 83 | } | 78 | } |
| 84 | 79 | ||
| 85 | template <typename U = T> | 80 | template <typename U = T> |
| 86 | Vec2<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { | 81 | constexpr Vec2<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { |
| 87 | return MakeVec(-x, -y); | 82 | return {-x, -y}; |
| 88 | } | 83 | } |
| 89 | Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const { | 84 | constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const { |
| 90 | return MakeVec(x * other.x, y * other.y); | 85 | return {x * other.x, y * other.y}; |
| 91 | } | 86 | } |
| 87 | |||
| 92 | template <typename V> | 88 | template <typename V> |
| 93 | Vec2<decltype(T{} * V{})> operator*(const V& f) const { | 89 | constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const { |
| 94 | return MakeVec(x * f, y * f); | 90 | return {x * f, y * f}; |
| 95 | } | 91 | } |
| 92 | |||
| 96 | template <typename V> | 93 | template <typename V> |
| 97 | void operator*=(const V& f) { | 94 | constexpr Vec2& operator*=(const V& f) { |
| 98 | *this = *this * f; | 95 | *this = *this * f; |
| 96 | return *this; | ||
| 99 | } | 97 | } |
| 98 | |||
| 100 | template <typename V> | 99 | template <typename V> |
| 101 | Vec2<decltype(T{} / V{})> operator/(const V& f) const { | 100 | constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const { |
| 102 | return MakeVec(x / f, y / f); | 101 | return {x / f, y / f}; |
| 103 | } | 102 | } |
| 103 | |||
| 104 | template <typename V> | 104 | template <typename V> |
| 105 | void operator/=(const V& f) { | 105 | constexpr Vec2& operator/=(const V& f) { |
| 106 | *this = *this / f; | 106 | *this = *this / f; |
| 107 | return *this; | ||
| 107 | } | 108 | } |
| 108 | 109 | ||
| 109 | T Length2() const { | 110 | constexpr T Length2() const { |
| 110 | return x * x + y * y; | 111 | return x * x + y * y; |
| 111 | } | 112 | } |
| 112 | 113 | ||
| @@ -118,60 +119,59 @@ public: | |||
| 118 | Vec2 Normalized() const; | 119 | Vec2 Normalized() const; |
| 119 | float Normalize(); // returns the previous length, which is often useful | 120 | float Normalize(); // returns the previous length, which is often useful |
| 120 | 121 | ||
| 121 | T& operator[](int i) // allow vector[1] = 3 (vector.y=3) | 122 | constexpr T& operator[](std::size_t i) { |
| 122 | { | ||
| 123 | return *((&x) + i); | 123 | return *((&x) + i); |
| 124 | } | 124 | } |
| 125 | T operator[](const int i) const { | 125 | constexpr const T& operator[](std::size_t i) const { |
| 126 | return *((&x) + i); | 126 | return *((&x) + i); |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | void SetZero() { | 129 | constexpr void SetZero() { |
| 130 | x = 0; | 130 | x = 0; |
| 131 | y = 0; | 131 | y = 0; |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | // Common aliases: UV (texel coordinates), ST (texture coordinates) | 134 | // Common aliases: UV (texel coordinates), ST (texture coordinates) |
| 135 | T& u() { | 135 | constexpr T& u() { |
| 136 | return x; | 136 | return x; |
| 137 | } | 137 | } |
| 138 | T& v() { | 138 | constexpr T& v() { |
| 139 | return y; | 139 | return y; |
| 140 | } | 140 | } |
| 141 | T& s() { | 141 | constexpr T& s() { |
| 142 | return x; | 142 | return x; |
| 143 | } | 143 | } |
| 144 | T& t() { | 144 | constexpr T& t() { |
| 145 | return y; | 145 | return y; |
| 146 | } | 146 | } |
| 147 | 147 | ||
| 148 | const T& u() const { | 148 | constexpr const T& u() const { |
| 149 | return x; | 149 | return x; |
| 150 | } | 150 | } |
| 151 | const T& v() const { | 151 | constexpr const T& v() const { |
| 152 | return y; | 152 | return y; |
| 153 | } | 153 | } |
| 154 | const T& s() const { | 154 | constexpr const T& s() const { |
| 155 | return x; | 155 | return x; |
| 156 | } | 156 | } |
| 157 | const T& t() const { | 157 | constexpr const T& t() const { |
| 158 | return y; | 158 | return y; |
| 159 | } | 159 | } |
| 160 | 160 | ||
| 161 | // swizzlers - create a subvector of specific components | 161 | // swizzlers - create a subvector of specific components |
| 162 | const Vec2 yx() const { | 162 | constexpr Vec2 yx() const { |
| 163 | return Vec2(y, x); | 163 | return Vec2(y, x); |
| 164 | } | 164 | } |
| 165 | const Vec2 vu() const { | 165 | constexpr Vec2 vu() const { |
| 166 | return Vec2(y, x); | 166 | return Vec2(y, x); |
| 167 | } | 167 | } |
| 168 | const Vec2 ts() const { | 168 | constexpr Vec2 ts() const { |
| 169 | return Vec2(y, x); | 169 | return Vec2(y, x); |
| 170 | } | 170 | } |
| 171 | }; | 171 | }; |
| 172 | 172 | ||
| 173 | template <typename T, typename V> | 173 | template <typename T, typename V> |
| 174 | Vec2<T> operator*(const V& f, const Vec2<T>& vec) { | 174 | constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) { |
| 175 | return Vec2<T>(f * vec.x, f * vec.y); | 175 | return Vec2<T>(f * vec.x, f * vec.y); |
| 176 | } | 176 | } |
| 177 | 177 | ||
| @@ -196,64 +196,75 @@ public: | |||
| 196 | T y{}; | 196 | T y{}; |
| 197 | T z{}; | 197 | T z{}; |
| 198 | 198 | ||
| 199 | Vec3() = default; | 199 | constexpr Vec3() = default; |
| 200 | Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {} | 200 | constexpr Vec3(const T& x_, const T& y_, const T& z_) : x(x_), y(y_), z(z_) {} |
| 201 | 201 | ||
| 202 | template <typename T2> | 202 | template <typename T2> |
| 203 | Vec3<T2> Cast() const { | 203 | constexpr Vec3<T2> Cast() const { |
| 204 | return MakeVec<T2>((T2)x, (T2)y, (T2)z); | 204 | return Vec3<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z)); |
| 205 | } | 205 | } |
| 206 | 206 | ||
| 207 | // Only implemented for T=int and T=float | 207 | // Only implemented for T=int and T=float |
| 208 | static Vec3 FromRGB(unsigned int rgb); | 208 | static Vec3 FromRGB(unsigned int rgb); |
| 209 | unsigned int ToRGB() const; // alpha bits set to zero | 209 | unsigned int ToRGB() const; // alpha bits set to zero |
| 210 | 210 | ||
| 211 | static Vec3 AssignToAll(const T& f) { | 211 | static constexpr Vec3 AssignToAll(const T& f) { |
| 212 | return MakeVec(f, f, f); | 212 | return Vec3(f, f, f); |
| 213 | } | 213 | } |
| 214 | 214 | ||
| 215 | Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const { | 215 | constexpr Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const { |
| 216 | return MakeVec(x + other.x, y + other.y, z + other.z); | 216 | return {x + other.x, y + other.y, z + other.z}; |
| 217 | } | 217 | } |
| 218 | void operator+=(const Vec3& other) { | 218 | |
| 219 | constexpr Vec3& operator+=(const Vec3& other) { | ||
| 219 | x += other.x; | 220 | x += other.x; |
| 220 | y += other.y; | 221 | y += other.y; |
| 221 | z += other.z; | 222 | z += other.z; |
| 223 | return *this; | ||
| 222 | } | 224 | } |
| 223 | Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const { | 225 | |
| 224 | return MakeVec(x - other.x, y - other.y, z - other.z); | 226 | constexpr Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const { |
| 227 | return {x - other.x, y - other.y, z - other.z}; | ||
| 225 | } | 228 | } |
| 226 | void operator-=(const Vec3& other) { | 229 | |
| 230 | constexpr Vec3& operator-=(const Vec3& other) { | ||
| 227 | x -= other.x; | 231 | x -= other.x; |
| 228 | y -= other.y; | 232 | y -= other.y; |
| 229 | z -= other.z; | 233 | z -= other.z; |
| 234 | return *this; | ||
| 230 | } | 235 | } |
| 231 | 236 | ||
| 232 | template <typename U = T> | 237 | template <typename U = T> |
| 233 | Vec3<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { | 238 | constexpr Vec3<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { |
| 234 | return MakeVec(-x, -y, -z); | 239 | return {-x, -y, -z}; |
| 235 | } | 240 | } |
| 236 | Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const { | 241 | |
| 237 | return MakeVec(x * other.x, y * other.y, z * other.z); | 242 | constexpr Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const { |
| 243 | return {x * other.x, y * other.y, z * other.z}; | ||
| 238 | } | 244 | } |
| 245 | |||
| 239 | template <typename V> | 246 | template <typename V> |
| 240 | Vec3<decltype(T{} * V{})> operator*(const V& f) const { | 247 | constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const { |
| 241 | return MakeVec(x * f, y * f, z * f); | 248 | return {x * f, y * f, z * f}; |
| 242 | } | 249 | } |
| 250 | |||
| 243 | template <typename V> | 251 | template <typename V> |
| 244 | void operator*=(const V& f) { | 252 | constexpr Vec3& operator*=(const V& f) { |
| 245 | *this = *this * f; | 253 | *this = *this * f; |
| 254 | return *this; | ||
| 246 | } | 255 | } |
| 247 | template <typename V> | 256 | template <typename V> |
| 248 | Vec3<decltype(T{} / V{})> operator/(const V& f) const { | 257 | constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const { |
| 249 | return MakeVec(x / f, y / f, z / f); | 258 | return {x / f, y / f, z / f}; |
| 250 | } | 259 | } |
| 260 | |||
| 251 | template <typename V> | 261 | template <typename V> |
| 252 | void operator/=(const V& f) { | 262 | constexpr Vec3& operator/=(const V& f) { |
| 253 | *this = *this / f; | 263 | *this = *this / f; |
| 264 | return *this; | ||
| 254 | } | 265 | } |
| 255 | 266 | ||
| 256 | T Length2() const { | 267 | constexpr T Length2() const { |
| 257 | return x * x + y * y + z * z; | 268 | return x * x + y * y + z * z; |
| 258 | } | 269 | } |
| 259 | 270 | ||
| @@ -265,78 +276,78 @@ public: | |||
| 265 | Vec3 Normalized() const; | 276 | Vec3 Normalized() const; |
| 266 | float Normalize(); // returns the previous length, which is often useful | 277 | float Normalize(); // returns the previous length, which is often useful |
| 267 | 278 | ||
| 268 | T& operator[](int i) // allow vector[2] = 3 (vector.z=3) | 279 | constexpr T& operator[](std::size_t i) { |
| 269 | { | ||
| 270 | return *((&x) + i); | 280 | return *((&x) + i); |
| 271 | } | 281 | } |
| 272 | T operator[](const int i) const { | 282 | |
| 283 | constexpr const T& operator[](std::size_t i) const { | ||
| 273 | return *((&x) + i); | 284 | return *((&x) + i); |
| 274 | } | 285 | } |
| 275 | 286 | ||
| 276 | void SetZero() { | 287 | constexpr void SetZero() { |
| 277 | x = 0; | 288 | x = 0; |
| 278 | y = 0; | 289 | y = 0; |
| 279 | z = 0; | 290 | z = 0; |
| 280 | } | 291 | } |
| 281 | 292 | ||
| 282 | // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) | 293 | // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) |
| 283 | T& u() { | 294 | constexpr T& u() { |
| 284 | return x; | 295 | return x; |
| 285 | } | 296 | } |
| 286 | T& v() { | 297 | constexpr T& v() { |
| 287 | return y; | 298 | return y; |
| 288 | } | 299 | } |
| 289 | T& w() { | 300 | constexpr T& w() { |
| 290 | return z; | 301 | return z; |
| 291 | } | 302 | } |
| 292 | 303 | ||
| 293 | T& r() { | 304 | constexpr T& r() { |
| 294 | return x; | 305 | return x; |
| 295 | } | 306 | } |
| 296 | T& g() { | 307 | constexpr T& g() { |
| 297 | return y; | 308 | return y; |
| 298 | } | 309 | } |
| 299 | T& b() { | 310 | constexpr T& b() { |
| 300 | return z; | 311 | return z; |
| 301 | } | 312 | } |
| 302 | 313 | ||
| 303 | T& s() { | 314 | constexpr T& s() { |
| 304 | return x; | 315 | return x; |
| 305 | } | 316 | } |
| 306 | T& t() { | 317 | constexpr T& t() { |
| 307 | return y; | 318 | return y; |
| 308 | } | 319 | } |
| 309 | T& q() { | 320 | constexpr T& q() { |
| 310 | return z; | 321 | return z; |
| 311 | } | 322 | } |
| 312 | 323 | ||
| 313 | const T& u() const { | 324 | constexpr const T& u() const { |
| 314 | return x; | 325 | return x; |
| 315 | } | 326 | } |
| 316 | const T& v() const { | 327 | constexpr const T& v() const { |
| 317 | return y; | 328 | return y; |
| 318 | } | 329 | } |
| 319 | const T& w() const { | 330 | constexpr const T& w() const { |
| 320 | return z; | 331 | return z; |
| 321 | } | 332 | } |
| 322 | 333 | ||
| 323 | const T& r() const { | 334 | constexpr const T& r() const { |
| 324 | return x; | 335 | return x; |
| 325 | } | 336 | } |
| 326 | const T& g() const { | 337 | constexpr const T& g() const { |
| 327 | return y; | 338 | return y; |
| 328 | } | 339 | } |
| 329 | const T& b() const { | 340 | constexpr const T& b() const { |
| 330 | return z; | 341 | return z; |
| 331 | } | 342 | } |
| 332 | 343 | ||
| 333 | const T& s() const { | 344 | constexpr const T& s() const { |
| 334 | return x; | 345 | return x; |
| 335 | } | 346 | } |
| 336 | const T& t() const { | 347 | constexpr const T& t() const { |
| 337 | return y; | 348 | return y; |
| 338 | } | 349 | } |
| 339 | const T& q() const { | 350 | constexpr const T& q() const { |
| 340 | return z; | 351 | return z; |
| 341 | } | 352 | } |
| 342 | 353 | ||
| @@ -345,7 +356,7 @@ public: | |||
| 345 | // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all | 356 | // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all |
| 346 | // component names (x<->r) and permutations (xy<->yx) | 357 | // component names (x<->r) and permutations (xy<->yx) |
| 347 | #define _DEFINE_SWIZZLER2(a, b, name) \ | 358 | #define _DEFINE_SWIZZLER2(a, b, name) \ |
| 348 | const Vec2<T> name() const { \ | 359 | constexpr Vec2<T> name() const { \ |
| 349 | return Vec2<T>(a, b); \ | 360 | return Vec2<T>(a, b); \ |
| 350 | } | 361 | } |
| 351 | #define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ | 362 | #define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ |
| @@ -366,7 +377,7 @@ public: | |||
| 366 | }; | 377 | }; |
| 367 | 378 | ||
| 368 | template <typename T, typename V> | 379 | template <typename T, typename V> |
| 369 | Vec3<T> operator*(const V& f, const Vec3<T>& vec) { | 380 | constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) { |
| 370 | return Vec3<T>(f * vec.x, f * vec.y, f * vec.z); | 381 | return Vec3<T>(f * vec.x, f * vec.y, f * vec.z); |
| 371 | } | 382 | } |
| 372 | 383 | ||
| @@ -397,66 +408,80 @@ public: | |||
| 397 | T z{}; | 408 | T z{}; |
| 398 | T w{}; | 409 | T w{}; |
| 399 | 410 | ||
| 400 | Vec4() = default; | 411 | constexpr Vec4() = default; |
| 401 | Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {} | 412 | constexpr Vec4(const T& x_, const T& y_, const T& z_, const T& w_) |
| 413 | : x(x_), y(y_), z(z_), w(w_) {} | ||
| 402 | 414 | ||
| 403 | template <typename T2> | 415 | template <typename T2> |
| 404 | Vec4<T2> Cast() const { | 416 | constexpr Vec4<T2> Cast() const { |
| 405 | return Vec4<T2>((T2)x, (T2)y, (T2)z, (T2)w); | 417 | return Vec4<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z), |
| 418 | static_cast<T2>(w)); | ||
| 406 | } | 419 | } |
| 407 | 420 | ||
| 408 | // Only implemented for T=int and T=float | 421 | // Only implemented for T=int and T=float |
| 409 | static Vec4 FromRGBA(unsigned int rgba); | 422 | static Vec4 FromRGBA(unsigned int rgba); |
| 410 | unsigned int ToRGBA() const; | 423 | unsigned int ToRGBA() const; |
| 411 | 424 | ||
| 412 | static Vec4 AssignToAll(const T& f) { | 425 | static constexpr Vec4 AssignToAll(const T& f) { |
| 413 | return Vec4<T>(f, f, f, f); | 426 | return Vec4(f, f, f, f); |
| 414 | } | 427 | } |
| 415 | 428 | ||
| 416 | Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const { | 429 | constexpr Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const { |
| 417 | return MakeVec(x + other.x, y + other.y, z + other.z, w + other.w); | 430 | return {x + other.x, y + other.y, z + other.z, w + other.w}; |
| 418 | } | 431 | } |
| 419 | void operator+=(const Vec4& other) { | 432 | |
| 433 | constexpr Vec4& operator+=(const Vec4& other) { | ||
| 420 | x += other.x; | 434 | x += other.x; |
| 421 | y += other.y; | 435 | y += other.y; |
| 422 | z += other.z; | 436 | z += other.z; |
| 423 | w += other.w; | 437 | w += other.w; |
| 438 | return *this; | ||
| 424 | } | 439 | } |
| 425 | Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const { | 440 | |
| 426 | return MakeVec(x - other.x, y - other.y, z - other.z, w - other.w); | 441 | constexpr Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const { |
| 442 | return {x - other.x, y - other.y, z - other.z, w - other.w}; | ||
| 427 | } | 443 | } |
| 428 | void operator-=(const Vec4& other) { | 444 | |
| 445 | constexpr Vec4& operator-=(const Vec4& other) { | ||
| 429 | x -= other.x; | 446 | x -= other.x; |
| 430 | y -= other.y; | 447 | y -= other.y; |
| 431 | z -= other.z; | 448 | z -= other.z; |
| 432 | w -= other.w; | 449 | w -= other.w; |
| 450 | return *this; | ||
| 433 | } | 451 | } |
| 434 | 452 | ||
| 435 | template <typename U = T> | 453 | template <typename U = T> |
| 436 | Vec4<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { | 454 | constexpr Vec4<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { |
| 437 | return MakeVec(-x, -y, -z, -w); | 455 | return {-x, -y, -z, -w}; |
| 438 | } | 456 | } |
| 439 | Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const { | 457 | |
| 440 | return MakeVec(x * other.x, y * other.y, z * other.z, w * other.w); | 458 | constexpr Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const { |
| 459 | return {x * other.x, y * other.y, z * other.z, w * other.w}; | ||
| 441 | } | 460 | } |
| 461 | |||
| 442 | template <typename V> | 462 | template <typename V> |
| 443 | Vec4<decltype(T{} * V{})> operator*(const V& f) const { | 463 | constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const { |
| 444 | return MakeVec(x * f, y * f, z * f, w * f); | 464 | return {x * f, y * f, z * f, w * f}; |
| 445 | } | 465 | } |
| 466 | |||
| 446 | template <typename V> | 467 | template <typename V> |
| 447 | void operator*=(const V& f) { | 468 | constexpr Vec4& operator*=(const V& f) { |
| 448 | *this = *this * f; | 469 | *this = *this * f; |
| 470 | return *this; | ||
| 449 | } | 471 | } |
| 472 | |||
| 450 | template <typename V> | 473 | template <typename V> |
| 451 | Vec4<decltype(T{} / V{})> operator/(const V& f) const { | 474 | constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const { |
| 452 | return MakeVec(x / f, y / f, z / f, w / f); | 475 | return {x / f, y / f, z / f, w / f}; |
| 453 | } | 476 | } |
| 477 | |||
| 454 | template <typename V> | 478 | template <typename V> |
| 455 | void operator/=(const V& f) { | 479 | constexpr Vec4& operator/=(const V& f) { |
| 456 | *this = *this / f; | 480 | *this = *this / f; |
| 481 | return *this; | ||
| 457 | } | 482 | } |
| 458 | 483 | ||
| 459 | T Length2() const { | 484 | constexpr T Length2() const { |
| 460 | return x * x + y * y + z * z + w * w; | 485 | return x * x + y * y + z * z + w * w; |
| 461 | } | 486 | } |
| 462 | 487 | ||
| @@ -468,15 +493,15 @@ public: | |||
| 468 | Vec4 Normalized() const; | 493 | Vec4 Normalized() const; |
| 469 | float Normalize(); // returns the previous length, which is often useful | 494 | float Normalize(); // returns the previous length, which is often useful |
| 470 | 495 | ||
| 471 | T& operator[](int i) // allow vector[2] = 3 (vector.z=3) | 496 | constexpr T& operator[](std::size_t i) { |
| 472 | { | ||
| 473 | return *((&x) + i); | 497 | return *((&x) + i); |
| 474 | } | 498 | } |
| 475 | T operator[](const int i) const { | 499 | |
| 500 | constexpr const T& operator[](std::size_t i) const { | ||
| 476 | return *((&x) + i); | 501 | return *((&x) + i); |
| 477 | } | 502 | } |
| 478 | 503 | ||
| 479 | void SetZero() { | 504 | constexpr void SetZero() { |
| 480 | x = 0; | 505 | x = 0; |
| 481 | y = 0; | 506 | y = 0; |
| 482 | z = 0; | 507 | z = 0; |
| @@ -484,29 +509,29 @@ public: | |||
| 484 | } | 509 | } |
| 485 | 510 | ||
| 486 | // Common alias: RGBA (colors) | 511 | // Common alias: RGBA (colors) |
| 487 | T& r() { | 512 | constexpr T& r() { |
| 488 | return x; | 513 | return x; |
| 489 | } | 514 | } |
| 490 | T& g() { | 515 | constexpr T& g() { |
| 491 | return y; | 516 | return y; |
| 492 | } | 517 | } |
| 493 | T& b() { | 518 | constexpr T& b() { |
| 494 | return z; | 519 | return z; |
| 495 | } | 520 | } |
| 496 | T& a() { | 521 | constexpr T& a() { |
| 497 | return w; | 522 | return w; |
| 498 | } | 523 | } |
| 499 | 524 | ||
| 500 | const T& r() const { | 525 | constexpr const T& r() const { |
| 501 | return x; | 526 | return x; |
| 502 | } | 527 | } |
| 503 | const T& g() const { | 528 | constexpr const T& g() const { |
| 504 | return y; | 529 | return y; |
| 505 | } | 530 | } |
| 506 | const T& b() const { | 531 | constexpr const T& b() const { |
| 507 | return z; | 532 | return z; |
| 508 | } | 533 | } |
| 509 | const T& a() const { | 534 | constexpr const T& a() const { |
| 510 | return w; | 535 | return w; |
| 511 | } | 536 | } |
| 512 | 537 | ||
| @@ -518,7 +543,7 @@ public: | |||
| 518 | // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and | 543 | // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and |
| 519 | // permutations (xy<->yx) | 544 | // permutations (xy<->yx) |
| 520 | #define _DEFINE_SWIZZLER2(a, b, name) \ | 545 | #define _DEFINE_SWIZZLER2(a, b, name) \ |
| 521 | const Vec2<T> name() const { \ | 546 | constexpr Vec2<T> name() const { \ |
| 522 | return Vec2<T>(a, b); \ | 547 | return Vec2<T>(a, b); \ |
| 523 | } | 548 | } |
| 524 | #define DEFINE_SWIZZLER2_COMP1(a, a2) \ | 549 | #define DEFINE_SWIZZLER2_COMP1(a, a2) \ |
| @@ -545,7 +570,7 @@ public: | |||
| 545 | #undef _DEFINE_SWIZZLER2 | 570 | #undef _DEFINE_SWIZZLER2 |
| 546 | 571 | ||
| 547 | #define _DEFINE_SWIZZLER3(a, b, c, name) \ | 572 | #define _DEFINE_SWIZZLER3(a, b, c, name) \ |
| 548 | const Vec3<T> name() const { \ | 573 | constexpr Vec3<T> name() const { \ |
| 549 | return Vec3<T>(a, b, c); \ | 574 | return Vec3<T>(a, b, c); \ |
| 550 | } | 575 | } |
| 551 | #define DEFINE_SWIZZLER3_COMP1(a, a2) \ | 576 | #define DEFINE_SWIZZLER3_COMP1(a, a2) \ |
| @@ -579,51 +604,51 @@ public: | |||
| 579 | }; | 604 | }; |
| 580 | 605 | ||
| 581 | template <typename T, typename V> | 606 | template <typename T, typename V> |
| 582 | Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) { | 607 | constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) { |
| 583 | return MakeVec(f * vec.x, f * vec.y, f * vec.z, f * vec.w); | 608 | return {f * vec.x, f * vec.y, f * vec.z, f * vec.w}; |
| 584 | } | 609 | } |
| 585 | 610 | ||
| 586 | using Vec4f = Vec4<float>; | 611 | using Vec4f = Vec4<float>; |
| 587 | 612 | ||
| 588 | template <typename T> | 613 | template <typename T> |
| 589 | static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) { | 614 | constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) { |
| 590 | return a.x * b.x + a.y * b.y; | 615 | return a.x * b.x + a.y * b.y; |
| 591 | } | 616 | } |
| 592 | 617 | ||
| 593 | template <typename T> | 618 | template <typename T> |
| 594 | static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) { | 619 | constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) { |
| 595 | return a.x * b.x + a.y * b.y + a.z * b.z; | 620 | return a.x * b.x + a.y * b.y + a.z * b.z; |
| 596 | } | 621 | } |
| 597 | 622 | ||
| 598 | template <typename T> | 623 | template <typename T> |
| 599 | static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) { | 624 | constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) { |
| 600 | return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; | 625 | return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; |
| 601 | } | 626 | } |
| 602 | 627 | ||
| 603 | template <typename T> | 628 | template <typename T> |
| 604 | static inline Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a, const Vec3<T>& b) { | 629 | constexpr Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a, const Vec3<T>& b) { |
| 605 | return MakeVec(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); | 630 | return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; |
| 606 | } | 631 | } |
| 607 | 632 | ||
| 608 | // linear interpolation via float: 0.0=begin, 1.0=end | 633 | // linear interpolation via float: 0.0=begin, 1.0=end |
| 609 | template <typename X> | 634 | template <typename X> |
| 610 | static inline decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end, | 635 | constexpr decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end, |
| 611 | const float t) { | 636 | const float t) { |
| 612 | return begin * (1.f - t) + end * t; | 637 | return begin * (1.f - t) + end * t; |
| 613 | } | 638 | } |
| 614 | 639 | ||
| 615 | // linear interpolation via int: 0=begin, base=end | 640 | // linear interpolation via int: 0=begin, base=end |
| 616 | template <typename X, int base> | 641 | template <typename X, int base> |
| 617 | static inline decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, const X& end, | 642 | constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, const X& end, |
| 618 | const int t) { | 643 | const int t) { |
| 619 | return (begin * (base - t) + end * t) / base; | 644 | return (begin * (base - t) + end * t) / base; |
| 620 | } | 645 | } |
| 621 | 646 | ||
| 622 | // bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second | 647 | // bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second |
| 623 | // interpolation. | 648 | // interpolation. |
| 624 | template <typename X> | 649 | template <typename X> |
| 625 | inline auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s, | 650 | constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s, |
| 626 | const float t) { | 651 | const float t) { |
| 627 | auto y0 = Lerp(x00, x01, s); | 652 | auto y0 = Lerp(x00, x01, s); |
| 628 | auto y1 = Lerp(x10, x11, s); | 653 | auto y1 = Lerp(x10, x11, s); |
| 629 | return Lerp(y0, y1, t); | 654 | return Lerp(y0, y1, t); |
| @@ -631,42 +656,42 @@ inline auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x1 | |||
| 631 | 656 | ||
| 632 | // Utility vector factories | 657 | // Utility vector factories |
| 633 | template <typename T> | 658 | template <typename T> |
| 634 | static inline Vec2<T> MakeVec(const T& x, const T& y) { | 659 | constexpr Vec2<T> MakeVec(const T& x, const T& y) { |
| 635 | return Vec2<T>{x, y}; | 660 | return Vec2<T>{x, y}; |
| 636 | } | 661 | } |
| 637 | 662 | ||
| 638 | template <typename T> | 663 | template <typename T> |
| 639 | static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z) { | 664 | constexpr Vec3<T> MakeVec(const T& x, const T& y, const T& z) { |
| 640 | return Vec3<T>{x, y, z}; | 665 | return Vec3<T>{x, y, z}; |
| 641 | } | 666 | } |
| 642 | 667 | ||
| 643 | template <typename T> | 668 | template <typename T> |
| 644 | static inline Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) { | 669 | constexpr Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) { |
| 645 | return MakeVec(x, y, zw[0], zw[1]); | 670 | return MakeVec(x, y, zw[0], zw[1]); |
| 646 | } | 671 | } |
| 647 | 672 | ||
| 648 | template <typename T> | 673 | template <typename T> |
| 649 | static inline Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) { | 674 | constexpr Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) { |
| 650 | return MakeVec(xy[0], xy[1], z); | 675 | return MakeVec(xy[0], xy[1], z); |
| 651 | } | 676 | } |
| 652 | 677 | ||
| 653 | template <typename T> | 678 | template <typename T> |
| 654 | static inline Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) { | 679 | constexpr Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) { |
| 655 | return MakeVec(x, yz[0], yz[1]); | 680 | return MakeVec(x, yz[0], yz[1]); |
| 656 | } | 681 | } |
| 657 | 682 | ||
| 658 | template <typename T> | 683 | template <typename T> |
| 659 | static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) { | 684 | constexpr Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) { |
| 660 | return Vec4<T>{x, y, z, w}; | 685 | return Vec4<T>{x, y, z, w}; |
| 661 | } | 686 | } |
| 662 | 687 | ||
| 663 | template <typename T> | 688 | template <typename T> |
| 664 | static inline Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) { | 689 | constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) { |
| 665 | return MakeVec(xy[0], xy[1], z, w); | 690 | return MakeVec(xy[0], xy[1], z, w); |
| 666 | } | 691 | } |
| 667 | 692 | ||
| 668 | template <typename T> | 693 | template <typename T> |
| 669 | static inline Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) { | 694 | constexpr Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) { |
| 670 | return MakeVec(x, yz[0], yz[1], w); | 695 | return MakeVec(x, yz[0], yz[1], w); |
| 671 | } | 696 | } |
| 672 | 697 | ||
| @@ -674,17 +699,17 @@ static inline Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) { | |||
| 674 | // Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error | 699 | // Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error |
| 675 | // out soon enough due to misuse of the returned structure. | 700 | // out soon enough due to misuse of the returned structure. |
| 676 | template <typename T> | 701 | template <typename T> |
| 677 | static inline Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) { | 702 | constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) { |
| 678 | return MakeVec(xy[0], xy[1], zw[0], zw[1]); | 703 | return MakeVec(xy[0], xy[1], zw[0], zw[1]); |
| 679 | } | 704 | } |
| 680 | 705 | ||
| 681 | template <typename T> | 706 | template <typename T> |
| 682 | static inline Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) { | 707 | constexpr Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) { |
| 683 | return MakeVec(xyz[0], xyz[1], xyz[2], w); | 708 | return MakeVec(xyz[0], xyz[1], xyz[2], w); |
| 684 | } | 709 | } |
| 685 | 710 | ||
| 686 | template <typename T> | 711 | template <typename T> |
| 687 | static inline Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) { | 712 | constexpr Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) { |
| 688 | return MakeVec(x, yzw[0], yzw[1], yzw[2]); | 713 | return MakeVec(x, yzw[0], yzw[1], yzw[2]); |
| 689 | } | 714 | } |
| 690 | 715 | ||