diff options
Diffstat (limited to 'src/common/vector_math.h')
| -rw-r--r-- | src/common/vector_math.h | 75 |
1 files changed, 66 insertions, 9 deletions
diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 2a0fcf541..22dba3c2d 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h | |||
| @@ -87,7 +87,13 @@ public: | |||
| 87 | 87 | ||
| 88 | template <typename V> | 88 | template <typename V> |
| 89 | [[nodiscard]] constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const { | 89 | [[nodiscard]] constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const { |
| 90 | return {x * f, y * f}; | 90 | using TV = decltype(T{} * V{}); |
| 91 | using C = std::common_type_t<T, V>; | ||
| 92 | |||
| 93 | return { | ||
| 94 | static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)), | ||
| 95 | static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)), | ||
| 96 | }; | ||
| 91 | } | 97 | } |
| 92 | 98 | ||
| 93 | template <typename V> | 99 | template <typename V> |
| @@ -98,7 +104,13 @@ public: | |||
| 98 | 104 | ||
| 99 | template <typename V> | 105 | template <typename V> |
| 100 | [[nodiscard]] constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const { | 106 | [[nodiscard]] constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const { |
| 101 | return {x / f, y / f}; | 107 | using TV = decltype(T{} / V{}); |
| 108 | using C = std::common_type_t<T, V>; | ||
| 109 | |||
| 110 | return { | ||
| 111 | static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)), | ||
| 112 | static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)), | ||
| 113 | }; | ||
| 102 | } | 114 | } |
| 103 | 115 | ||
| 104 | template <typename V> | 116 | template <typename V> |
| @@ -168,7 +180,10 @@ public: | |||
| 168 | 180 | ||
| 169 | template <typename T, typename V> | 181 | template <typename T, typename V> |
| 170 | [[nodiscard]] constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) { | 182 | [[nodiscard]] constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) { |
| 171 | return Vec2<T>(f * vec.x, f * vec.y); | 183 | using C = std::common_type_t<T, V>; |
| 184 | |||
| 185 | return Vec2<T>(static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.x)), | ||
| 186 | static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.y))); | ||
| 172 | } | 187 | } |
| 173 | 188 | ||
| 174 | using Vec2f = Vec2<float>; | 189 | using Vec2f = Vec2<float>; |
| @@ -237,7 +252,14 @@ public: | |||
| 237 | 252 | ||
| 238 | template <typename V> | 253 | template <typename V> |
| 239 | [[nodiscard]] constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const { | 254 | [[nodiscard]] constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const { |
| 240 | return {x * f, y * f, z * f}; | 255 | using TV = decltype(T{} * V{}); |
| 256 | using C = std::common_type_t<T, V>; | ||
| 257 | |||
| 258 | return { | ||
| 259 | static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)), | ||
| 260 | static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)), | ||
| 261 | static_cast<TV>(static_cast<C>(z) * static_cast<C>(f)), | ||
| 262 | }; | ||
| 241 | } | 263 | } |
| 242 | 264 | ||
| 243 | template <typename V> | 265 | template <typename V> |
| @@ -247,7 +269,14 @@ public: | |||
| 247 | } | 269 | } |
| 248 | template <typename V> | 270 | template <typename V> |
| 249 | [[nodiscard]] constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const { | 271 | [[nodiscard]] constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const { |
| 250 | return {x / f, y / f, z / f}; | 272 | using TV = decltype(T{} / V{}); |
| 273 | using C = std::common_type_t<T, V>; | ||
| 274 | |||
| 275 | return { | ||
| 276 | static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)), | ||
| 277 | static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)), | ||
| 278 | static_cast<TV>(static_cast<C>(z) / static_cast<C>(f)), | ||
| 279 | }; | ||
| 251 | } | 280 | } |
| 252 | 281 | ||
| 253 | template <typename V> | 282 | template <typename V> |
| @@ -367,7 +396,11 @@ public: | |||
| 367 | 396 | ||
| 368 | template <typename T, typename V> | 397 | template <typename T, typename V> |
| 369 | [[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) { | 398 | [[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) { |
| 370 | return Vec3<T>(f * vec.x, f * vec.y, f * vec.z); | 399 | using C = std::common_type_t<T, V>; |
| 400 | |||
| 401 | return Vec3<T>(static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.x)), | ||
| 402 | static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.y)), | ||
| 403 | static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.z))); | ||
| 371 | } | 404 | } |
| 372 | 405 | ||
| 373 | template <> | 406 | template <> |
| @@ -446,7 +479,15 @@ public: | |||
| 446 | 479 | ||
| 447 | template <typename V> | 480 | template <typename V> |
| 448 | [[nodiscard]] constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const { | 481 | [[nodiscard]] constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const { |
| 449 | return {x * f, y * f, z * f, w * f}; | 482 | using TV = decltype(T{} * V{}); |
| 483 | using C = std::common_type_t<T, V>; | ||
| 484 | |||
| 485 | return { | ||
| 486 | static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)), | ||
| 487 | static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)), | ||
| 488 | static_cast<TV>(static_cast<C>(z) * static_cast<C>(f)), | ||
| 489 | static_cast<TV>(static_cast<C>(w) * static_cast<C>(f)), | ||
| 490 | }; | ||
| 450 | } | 491 | } |
| 451 | 492 | ||
| 452 | template <typename V> | 493 | template <typename V> |
| @@ -457,7 +498,15 @@ public: | |||
| 457 | 498 | ||
| 458 | template <typename V> | 499 | template <typename V> |
| 459 | [[nodiscard]] constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const { | 500 | [[nodiscard]] constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const { |
| 460 | return {x / f, y / f, z / f, w / f}; | 501 | using TV = decltype(T{} / V{}); |
| 502 | using C = std::common_type_t<T, V>; | ||
| 503 | |||
| 504 | return { | ||
| 505 | static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)), | ||
| 506 | static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)), | ||
| 507 | static_cast<TV>(static_cast<C>(z) / static_cast<C>(f)), | ||
| 508 | static_cast<TV>(static_cast<C>(w) / static_cast<C>(f)), | ||
| 509 | }; | ||
| 461 | } | 510 | } |
| 462 | 511 | ||
| 463 | template <typename V> | 512 | template <typename V> |
| @@ -582,7 +631,15 @@ public: | |||
| 582 | 631 | ||
| 583 | template <typename T, typename V> | 632 | template <typename T, typename V> |
| 584 | [[nodiscard]] constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) { | 633 | [[nodiscard]] constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) { |
| 585 | return {f * vec.x, f * vec.y, f * vec.z, f * vec.w}; | 634 | using TV = decltype(V{} * T{}); |
| 635 | using C = std::common_type_t<T, V>; | ||
| 636 | |||
| 637 | return { | ||
| 638 | static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.x)), | ||
| 639 | static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.y)), | ||
| 640 | static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.z)), | ||
| 641 | static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.w)), | ||
| 642 | }; | ||
| 586 | } | 643 | } |
| 587 | 644 | ||
| 588 | using Vec4f = Vec4<float>; | 645 | using Vec4f = Vec4<float>; |