vector_math.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // SPDX-FileCopyrightText: 2014 Tony Wasserka
  2. // SPDX-FileCopyrightText: 2014 Dolphin Emulator Project
  3. // SPDX-License-Identifier: BSD-3-Clause AND GPL-2.0-or-later
  4. #pragma once
  5. #include <cmath>
  6. #include <type_traits>
  7. namespace Common {
  8. template <typename T>
  9. class Vec2;
  10. template <typename T>
  11. class Vec3;
  12. template <typename T>
  13. class Vec4;
  14. template <typename T>
  15. class Vec2 {
  16. public:
  17. T x{};
  18. T y{};
  19. constexpr Vec2() = default;
  20. constexpr Vec2(const T& x_, const T& y_) : x(x_), y(y_) {}
  21. template <typename T2>
  22. [[nodiscard]] constexpr Vec2<T2> Cast() const {
  23. return Vec2<T2>(static_cast<T2>(x), static_cast<T2>(y));
  24. }
  25. [[nodiscard]] static constexpr Vec2 AssignToAll(const T& f) {
  26. return Vec2{f, f};
  27. }
  28. [[nodiscard]] constexpr Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const {
  29. return {x + other.x, y + other.y};
  30. }
  31. constexpr Vec2& operator+=(const Vec2& other) {
  32. x += other.x;
  33. y += other.y;
  34. return *this;
  35. }
  36. [[nodiscard]] constexpr Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const {
  37. return {x - other.x, y - other.y};
  38. }
  39. constexpr Vec2& operator-=(const Vec2& other) {
  40. x -= other.x;
  41. y -= other.y;
  42. return *this;
  43. }
  44. template <typename U = T>
  45. [[nodiscard]] constexpr Vec2<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
  46. return {-x, -y};
  47. }
  48. [[nodiscard]] constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const {
  49. return {x * other.x, y * other.y};
  50. }
  51. template <typename V>
  52. [[nodiscard]] constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const {
  53. using TV = decltype(T{} * V{});
  54. using C = std::common_type_t<T, V>;
  55. return {
  56. static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)),
  57. static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)),
  58. };
  59. }
  60. template <typename V>
  61. constexpr Vec2& operator*=(const V& f) {
  62. *this = *this * f;
  63. return *this;
  64. }
  65. template <typename V>
  66. [[nodiscard]] constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const {
  67. using TV = decltype(T{} / V{});
  68. using C = std::common_type_t<T, V>;
  69. return {
  70. static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)),
  71. static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)),
  72. };
  73. }
  74. template <typename V>
  75. constexpr Vec2& operator/=(const V& f) {
  76. *this = *this / f;
  77. return *this;
  78. }
  79. [[nodiscard]] constexpr T Length2() const {
  80. return x * x + y * y;
  81. }
  82. // Only implemented for T=float
  83. [[nodiscard]] float Length() const;
  84. [[nodiscard]] float Normalize(); // returns the previous length, which is often useful
  85. [[nodiscard]] constexpr T& operator[](std::size_t i) {
  86. return *((&x) + i);
  87. }
  88. [[nodiscard]] constexpr const T& operator[](std::size_t i) const {
  89. return *((&x) + i);
  90. }
  91. constexpr void SetZero() {
  92. x = 0;
  93. y = 0;
  94. }
  95. // Common aliases: UV (texel coordinates), ST (texture coordinates)
  96. [[nodiscard]] constexpr T& u() {
  97. return x;
  98. }
  99. [[nodiscard]] constexpr T& v() {
  100. return y;
  101. }
  102. [[nodiscard]] constexpr T& s() {
  103. return x;
  104. }
  105. [[nodiscard]] constexpr T& t() {
  106. return y;
  107. }
  108. [[nodiscard]] constexpr const T& u() const {
  109. return x;
  110. }
  111. [[nodiscard]] constexpr const T& v() const {
  112. return y;
  113. }
  114. [[nodiscard]] constexpr const T& s() const {
  115. return x;
  116. }
  117. [[nodiscard]] constexpr const T& t() const {
  118. return y;
  119. }
  120. // swizzlers - create a subvector of specific components
  121. [[nodiscard]] constexpr Vec2 yx() const {
  122. return Vec2(y, x);
  123. }
  124. [[nodiscard]] constexpr Vec2 vu() const {
  125. return Vec2(y, x);
  126. }
  127. [[nodiscard]] constexpr Vec2 ts() const {
  128. return Vec2(y, x);
  129. }
  130. };
  131. template <typename T, typename V>
  132. [[nodiscard]] constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) {
  133. using C = std::common_type_t<T, V>;
  134. return Vec2<T>(static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.x)),
  135. static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.y)));
  136. }
  137. using Vec2f = Vec2<float>;
  138. template <>
  139. inline float Vec2<float>::Length() const {
  140. return std::sqrt(x * x + y * y);
  141. }
  142. template <>
  143. inline float Vec2<float>::Normalize() {
  144. float length = Length();
  145. *this /= length;
  146. return length;
  147. }
  148. template <typename T>
  149. class Vec3 {
  150. public:
  151. T x{};
  152. T y{};
  153. T z{};
  154. constexpr Vec3() = default;
  155. constexpr Vec3(const T& x_, const T& y_, const T& z_) : x(x_), y(y_), z(z_) {}
  156. template <typename T2>
  157. [[nodiscard]] constexpr Vec3<T2> Cast() const {
  158. return Vec3<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z));
  159. }
  160. [[nodiscard]] static constexpr Vec3 AssignToAll(const T& f) {
  161. return Vec3(f, f, f);
  162. }
  163. [[nodiscard]] constexpr Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const {
  164. return {x + other.x, y + other.y, z + other.z};
  165. }
  166. constexpr Vec3& operator+=(const Vec3& other) {
  167. x += other.x;
  168. y += other.y;
  169. z += other.z;
  170. return *this;
  171. }
  172. [[nodiscard]] constexpr Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const {
  173. return {x - other.x, y - other.y, z - other.z};
  174. }
  175. constexpr Vec3& operator-=(const Vec3& other) {
  176. x -= other.x;
  177. y -= other.y;
  178. z -= other.z;
  179. return *this;
  180. }
  181. template <typename U = T>
  182. [[nodiscard]] constexpr Vec3<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
  183. return {-x, -y, -z};
  184. }
  185. [[nodiscard]] constexpr Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const {
  186. return {x * other.x, y * other.y, z * other.z};
  187. }
  188. template <typename V>
  189. [[nodiscard]] constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const {
  190. using TV = decltype(T{} * V{});
  191. using C = std::common_type_t<T, V>;
  192. return {
  193. static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)),
  194. static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)),
  195. static_cast<TV>(static_cast<C>(z) * static_cast<C>(f)),
  196. };
  197. }
  198. template <typename V>
  199. constexpr Vec3& operator*=(const V& f) {
  200. *this = *this * f;
  201. return *this;
  202. }
  203. template <typename V>
  204. [[nodiscard]] constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const {
  205. using TV = decltype(T{} / V{});
  206. using C = std::common_type_t<T, V>;
  207. return {
  208. static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)),
  209. static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)),
  210. static_cast<TV>(static_cast<C>(z) / static_cast<C>(f)),
  211. };
  212. }
  213. template <typename V>
  214. constexpr Vec3& operator/=(const V& f) {
  215. *this = *this / f;
  216. return *this;
  217. }
  218. [[nodiscard]] constexpr T Length2() const {
  219. return x * x + y * y + z * z;
  220. }
  221. // Only implemented for T=float
  222. [[nodiscard]] float Length() const;
  223. [[nodiscard]] Vec3 Normalized() const;
  224. [[nodiscard]] float Normalize(); // returns the previous length, which is often useful
  225. [[nodiscard]] constexpr T& operator[](std::size_t i) {
  226. return *((&x) + i);
  227. }
  228. [[nodiscard]] constexpr const T& operator[](std::size_t i) const {
  229. return *((&x) + i);
  230. }
  231. constexpr void SetZero() {
  232. x = 0;
  233. y = 0;
  234. z = 0;
  235. }
  236. // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates)
  237. [[nodiscard]] constexpr T& u() {
  238. return x;
  239. }
  240. [[nodiscard]] constexpr T& v() {
  241. return y;
  242. }
  243. [[nodiscard]] constexpr T& w() {
  244. return z;
  245. }
  246. [[nodiscard]] constexpr T& r() {
  247. return x;
  248. }
  249. [[nodiscard]] constexpr T& g() {
  250. return y;
  251. }
  252. [[nodiscard]] constexpr T& b() {
  253. return z;
  254. }
  255. [[nodiscard]] constexpr T& s() {
  256. return x;
  257. }
  258. [[nodiscard]] constexpr T& t() {
  259. return y;
  260. }
  261. [[nodiscard]] constexpr T& q() {
  262. return z;
  263. }
  264. [[nodiscard]] constexpr const T& u() const {
  265. return x;
  266. }
  267. [[nodiscard]] constexpr const T& v() const {
  268. return y;
  269. }
  270. [[nodiscard]] constexpr const T& w() const {
  271. return z;
  272. }
  273. [[nodiscard]] constexpr const T& r() const {
  274. return x;
  275. }
  276. [[nodiscard]] constexpr const T& g() const {
  277. return y;
  278. }
  279. [[nodiscard]] constexpr const T& b() const {
  280. return z;
  281. }
  282. [[nodiscard]] constexpr const T& s() const {
  283. return x;
  284. }
  285. [[nodiscard]] constexpr const T& t() const {
  286. return y;
  287. }
  288. [[nodiscard]] constexpr const T& q() const {
  289. return z;
  290. }
  291. // swizzlers - create a subvector of specific components
  292. // e.g. Vec2 uv() { return Vec2(x,y); }
  293. // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all
  294. // component names (x<->r) and permutations (xy<->yx)
  295. #define _DEFINE_SWIZZLER2(a, b, name) \
  296. [[nodiscard]] constexpr Vec2<T> name() const { \
  297. return Vec2<T>(a, b); \
  298. }
  299. #define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \
  300. _DEFINE_SWIZZLER2(a, b, a##b); \
  301. _DEFINE_SWIZZLER2(a, b, a2##b2); \
  302. _DEFINE_SWIZZLER2(a, b, a3##b3); \
  303. _DEFINE_SWIZZLER2(a, b, a4##b4); \
  304. _DEFINE_SWIZZLER2(b, a, b##a); \
  305. _DEFINE_SWIZZLER2(b, a, b2##a2); \
  306. _DEFINE_SWIZZLER2(b, a, b3##a3); \
  307. _DEFINE_SWIZZLER2(b, a, b4##a4)
  308. DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t);
  309. DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q);
  310. DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q);
  311. #undef DEFINE_SWIZZLER2
  312. #undef _DEFINE_SWIZZLER2
  313. };
  314. template <typename T, typename V>
  315. [[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) {
  316. using C = std::common_type_t<T, V>;
  317. return Vec3<T>(static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.x)),
  318. static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.y)),
  319. static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.z)));
  320. }
  321. template <>
  322. inline float Vec3<float>::Length() const {
  323. return std::sqrt(x * x + y * y + z * z);
  324. }
  325. template <>
  326. inline Vec3<float> Vec3<float>::Normalized() const {
  327. return *this / Length();
  328. }
  329. template <>
  330. inline float Vec3<float>::Normalize() {
  331. float length = Length();
  332. *this /= length;
  333. return length;
  334. }
  335. using Vec3f = Vec3<float>;
  336. template <typename T>
  337. class Vec4 {
  338. public:
  339. T x{};
  340. T y{};
  341. T z{};
  342. T w{};
  343. constexpr Vec4() = default;
  344. constexpr Vec4(const T& x_, const T& y_, const T& z_, const T& w_)
  345. : x(x_), y(y_), z(z_), w(w_) {}
  346. template <typename T2>
  347. [[nodiscard]] constexpr Vec4<T2> Cast() const {
  348. return Vec4<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z),
  349. static_cast<T2>(w));
  350. }
  351. [[nodiscard]] static constexpr Vec4 AssignToAll(const T& f) {
  352. return Vec4(f, f, f, f);
  353. }
  354. [[nodiscard]] constexpr Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const {
  355. return {x + other.x, y + other.y, z + other.z, w + other.w};
  356. }
  357. constexpr Vec4& operator+=(const Vec4& other) {
  358. x += other.x;
  359. y += other.y;
  360. z += other.z;
  361. w += other.w;
  362. return *this;
  363. }
  364. [[nodiscard]] constexpr Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const {
  365. return {x - other.x, y - other.y, z - other.z, w - other.w};
  366. }
  367. constexpr Vec4& operator-=(const Vec4& other) {
  368. x -= other.x;
  369. y -= other.y;
  370. z -= other.z;
  371. w -= other.w;
  372. return *this;
  373. }
  374. template <typename U = T>
  375. [[nodiscard]] constexpr Vec4<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
  376. return {-x, -y, -z, -w};
  377. }
  378. [[nodiscard]] constexpr Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const {
  379. return {x * other.x, y * other.y, z * other.z, w * other.w};
  380. }
  381. template <typename V>
  382. [[nodiscard]] constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const {
  383. using TV = decltype(T{} * V{});
  384. using C = std::common_type_t<T, V>;
  385. return {
  386. static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)),
  387. static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)),
  388. static_cast<TV>(static_cast<C>(z) * static_cast<C>(f)),
  389. static_cast<TV>(static_cast<C>(w) * static_cast<C>(f)),
  390. };
  391. }
  392. template <typename V>
  393. constexpr Vec4& operator*=(const V& f) {
  394. *this = *this * f;
  395. return *this;
  396. }
  397. template <typename V>
  398. [[nodiscard]] constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const {
  399. using TV = decltype(T{} / V{});
  400. using C = std::common_type_t<T, V>;
  401. return {
  402. static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)),
  403. static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)),
  404. static_cast<TV>(static_cast<C>(z) / static_cast<C>(f)),
  405. static_cast<TV>(static_cast<C>(w) / static_cast<C>(f)),
  406. };
  407. }
  408. template <typename V>
  409. constexpr Vec4& operator/=(const V& f) {
  410. *this = *this / f;
  411. return *this;
  412. }
  413. [[nodiscard]] constexpr T Length2() const {
  414. return x * x + y * y + z * z + w * w;
  415. }
  416. [[nodiscard]] constexpr T& operator[](std::size_t i) {
  417. return *((&x) + i);
  418. }
  419. [[nodiscard]] constexpr const T& operator[](std::size_t i) const {
  420. return *((&x) + i);
  421. }
  422. constexpr void SetZero() {
  423. x = 0;
  424. y = 0;
  425. z = 0;
  426. w = 0;
  427. }
  428. // Common alias: RGBA (colors)
  429. [[nodiscard]] constexpr T& r() {
  430. return x;
  431. }
  432. [[nodiscard]] constexpr T& g() {
  433. return y;
  434. }
  435. [[nodiscard]] constexpr T& b() {
  436. return z;
  437. }
  438. [[nodiscard]] constexpr T& a() {
  439. return w;
  440. }
  441. [[nodiscard]] constexpr const T& r() const {
  442. return x;
  443. }
  444. [[nodiscard]] constexpr const T& g() const {
  445. return y;
  446. }
  447. [[nodiscard]] constexpr const T& b() const {
  448. return z;
  449. }
  450. [[nodiscard]] constexpr const T& a() const {
  451. return w;
  452. }
  453. // Swizzlers - Create a subvector of specific components
  454. // e.g. Vec2 uv() { return Vec2(x,y); }
  455. // _DEFINE_SWIZZLER2 defines a single such function
  456. // DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r)
  457. // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and
  458. // permutations (xy<->yx)
  459. #define _DEFINE_SWIZZLER2(a, b, name) \
  460. [[nodiscard]] constexpr Vec2<T> name() const { \
  461. return Vec2<T>(a, b); \
  462. }
  463. #define DEFINE_SWIZZLER2_COMP1(a, a2) \
  464. _DEFINE_SWIZZLER2(a, a, a##a); \
  465. _DEFINE_SWIZZLER2(a, a, a2##a2)
  466. #define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \
  467. _DEFINE_SWIZZLER2(a, b, a##b); \
  468. _DEFINE_SWIZZLER2(a, b, a2##b2); \
  469. _DEFINE_SWIZZLER2(b, a, b##a); \
  470. _DEFINE_SWIZZLER2(b, a, b2##a2)
  471. DEFINE_SWIZZLER2_COMP2(x, y, r, g);
  472. DEFINE_SWIZZLER2_COMP2(x, z, r, b);
  473. DEFINE_SWIZZLER2_COMP2(x, w, r, a);
  474. DEFINE_SWIZZLER2_COMP2(y, z, g, b);
  475. DEFINE_SWIZZLER2_COMP2(y, w, g, a);
  476. DEFINE_SWIZZLER2_COMP2(z, w, b, a);
  477. DEFINE_SWIZZLER2_COMP1(x, r);
  478. DEFINE_SWIZZLER2_COMP1(y, g);
  479. DEFINE_SWIZZLER2_COMP1(z, b);
  480. DEFINE_SWIZZLER2_COMP1(w, a);
  481. #undef DEFINE_SWIZZLER2_COMP1
  482. #undef DEFINE_SWIZZLER2_COMP2
  483. #undef _DEFINE_SWIZZLER2
  484. #define _DEFINE_SWIZZLER3(a, b, c, name) \
  485. [[nodiscard]] constexpr Vec3<T> name() const { \
  486. return Vec3<T>(a, b, c); \
  487. }
  488. #define DEFINE_SWIZZLER3_COMP1(a, a2) \
  489. _DEFINE_SWIZZLER3(a, a, a, a##a##a); \
  490. _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2)
  491. #define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \
  492. _DEFINE_SWIZZLER3(a, b, c, a##b##c); \
  493. _DEFINE_SWIZZLER3(a, c, b, a##c##b); \
  494. _DEFINE_SWIZZLER3(b, a, c, b##a##c); \
  495. _DEFINE_SWIZZLER3(b, c, a, b##c##a); \
  496. _DEFINE_SWIZZLER3(c, a, b, c##a##b); \
  497. _DEFINE_SWIZZLER3(c, b, a, c##b##a); \
  498. _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \
  499. _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \
  500. _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \
  501. _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \
  502. _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \
  503. _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2)
  504. DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b);
  505. DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a);
  506. DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a);
  507. DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a);
  508. DEFINE_SWIZZLER3_COMP1(x, r);
  509. DEFINE_SWIZZLER3_COMP1(y, g);
  510. DEFINE_SWIZZLER3_COMP1(z, b);
  511. DEFINE_SWIZZLER3_COMP1(w, a);
  512. #undef DEFINE_SWIZZLER3_COMP1
  513. #undef DEFINE_SWIZZLER3_COMP3
  514. #undef _DEFINE_SWIZZLER3
  515. };
  516. template <typename T, typename V>
  517. [[nodiscard]] constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) {
  518. using TV = decltype(V{} * T{});
  519. using C = std::common_type_t<T, V>;
  520. return {
  521. static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.x)),
  522. static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.y)),
  523. static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.z)),
  524. static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.w)),
  525. };
  526. }
  527. using Vec4f = Vec4<float>;
  528. template <typename T>
  529. constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) {
  530. return a.x * b.x + a.y * b.y;
  531. }
  532. template <typename T>
  533. [[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) {
  534. return a.x * b.x + a.y * b.y + a.z * b.z;
  535. }
  536. template <typename T>
  537. [[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) {
  538. return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  539. }
  540. template <typename T>
  541. [[nodiscard]] constexpr Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a,
  542. const Vec3<T>& b) {
  543. 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};
  544. }
  545. // linear interpolation via float: 0.0=begin, 1.0=end
  546. template <typename X>
  547. [[nodiscard]] constexpr decltype(X{} * float{} + X{} * float{})
  548. Lerp(const X& begin, const X& end, const float t) {
  549. return begin * (1.f - t) + end * t;
  550. }
  551. // linear interpolation via int: 0=begin, base=end
  552. template <typename X, int base>
  553. [[nodiscard]] constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin,
  554. const X& end,
  555. const int t) {
  556. return (begin * (base - t) + end * t) / base;
  557. }
  558. // bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second
  559. // interpolation.
  560. template <typename X>
  561. [[nodiscard]] constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11,
  562. const float s, const float t) {
  563. auto y0 = Lerp(x00, x01, s);
  564. auto y1 = Lerp(x10, x11, s);
  565. return Lerp(y0, y1, t);
  566. }
  567. // Utility vector factories
  568. template <typename T>
  569. [[nodiscard]] constexpr Vec2<T> MakeVec(const T& x, const T& y) {
  570. return Vec2<T>{x, y};
  571. }
  572. template <typename T>
  573. [[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const T& y, const T& z) {
  574. return Vec3<T>{x, y, z};
  575. }
  576. template <typename T>
  577. [[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) {
  578. return MakeVec(x, y, zw[0], zw[1]);
  579. }
  580. template <typename T>
  581. [[nodiscard]] constexpr Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) {
  582. return MakeVec(xy[0], xy[1], z);
  583. }
  584. template <typename T>
  585. [[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) {
  586. return MakeVec(x, yz[0], yz[1]);
  587. }
  588. template <typename T>
  589. [[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) {
  590. return Vec4<T>{x, y, z, w};
  591. }
  592. template <typename T>
  593. [[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) {
  594. return MakeVec(xy[0], xy[1], z, w);
  595. }
  596. template <typename T>
  597. [[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) {
  598. return MakeVec(x, yz[0], yz[1], w);
  599. }
  600. // NOTE: This has priority over "Vec2<Vec2<T>> MakeVec(const Vec2<T>& x, const Vec2<T>& y)".
  601. // Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error
  602. // out soon enough due to misuse of the returned structure.
  603. template <typename T>
  604. [[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) {
  605. return MakeVec(xy[0], xy[1], zw[0], zw[1]);
  606. }
  607. template <typename T>
  608. [[nodiscard]] constexpr Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) {
  609. return MakeVec(xyz[0], xyz[1], xyz[2], w);
  610. }
  611. template <typename T>
  612. [[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) {
  613. return MakeVec(x, yzw[0], yzw[1], yzw[2]);
  614. }
  615. } // namespace Common