vector_math.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. void RotateFromOrigin(float roll, float pitch, float yaw) {
  219. float temp = y;
  220. y = std::cos(roll) * y - std::sin(roll) * z;
  221. z = std::sin(roll) * temp + std::cos(roll) * z;
  222. temp = x;
  223. x = std::cos(pitch) * x + std::sin(pitch) * z;
  224. z = -std::sin(pitch) * temp + std::cos(pitch) * z;
  225. temp = x;
  226. x = std::cos(yaw) * x - std::sin(yaw) * y;
  227. y = std::sin(yaw) * temp + std::cos(yaw) * y;
  228. }
  229. [[nodiscard]] constexpr T Length2() const {
  230. return x * x + y * y + z * z;
  231. }
  232. // Only implemented for T=float
  233. [[nodiscard]] float Length() const;
  234. [[nodiscard]] Vec3 Normalized() const;
  235. [[nodiscard]] float Normalize(); // returns the previous length, which is often useful
  236. [[nodiscard]] constexpr T& operator[](std::size_t i) {
  237. return *((&x) + i);
  238. }
  239. [[nodiscard]] constexpr const T& operator[](std::size_t i) const {
  240. return *((&x) + i);
  241. }
  242. constexpr void SetZero() {
  243. x = 0;
  244. y = 0;
  245. z = 0;
  246. }
  247. // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates)
  248. [[nodiscard]] constexpr T& u() {
  249. return x;
  250. }
  251. [[nodiscard]] constexpr T& v() {
  252. return y;
  253. }
  254. [[nodiscard]] constexpr T& w() {
  255. return z;
  256. }
  257. [[nodiscard]] constexpr T& r() {
  258. return x;
  259. }
  260. [[nodiscard]] constexpr T& g() {
  261. return y;
  262. }
  263. [[nodiscard]] constexpr T& b() {
  264. return z;
  265. }
  266. [[nodiscard]] constexpr T& s() {
  267. return x;
  268. }
  269. [[nodiscard]] constexpr T& t() {
  270. return y;
  271. }
  272. [[nodiscard]] constexpr T& q() {
  273. return z;
  274. }
  275. [[nodiscard]] constexpr const T& u() const {
  276. return x;
  277. }
  278. [[nodiscard]] constexpr const T& v() const {
  279. return y;
  280. }
  281. [[nodiscard]] constexpr const T& w() const {
  282. return z;
  283. }
  284. [[nodiscard]] constexpr const T& r() const {
  285. return x;
  286. }
  287. [[nodiscard]] constexpr const T& g() const {
  288. return y;
  289. }
  290. [[nodiscard]] constexpr const T& b() const {
  291. return z;
  292. }
  293. [[nodiscard]] constexpr const T& s() const {
  294. return x;
  295. }
  296. [[nodiscard]] constexpr const T& t() const {
  297. return y;
  298. }
  299. [[nodiscard]] constexpr const T& q() const {
  300. return z;
  301. }
  302. // swizzlers - create a subvector of specific components
  303. // e.g. Vec2 uv() { return Vec2(x,y); }
  304. // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all
  305. // component names (x<->r) and permutations (xy<->yx)
  306. #define _DEFINE_SWIZZLER2(a, b, name) \
  307. [[nodiscard]] constexpr Vec2<T> name() const { return Vec2<T>(a, b); }
  308. #define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \
  309. _DEFINE_SWIZZLER2(a, b, a##b); \
  310. _DEFINE_SWIZZLER2(a, b, a2##b2); \
  311. _DEFINE_SWIZZLER2(a, b, a3##b3); \
  312. _DEFINE_SWIZZLER2(a, b, a4##b4); \
  313. _DEFINE_SWIZZLER2(b, a, b##a); \
  314. _DEFINE_SWIZZLER2(b, a, b2##a2); \
  315. _DEFINE_SWIZZLER2(b, a, b3##a3); \
  316. _DEFINE_SWIZZLER2(b, a, b4##a4)
  317. DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t);
  318. DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q);
  319. DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q);
  320. #undef DEFINE_SWIZZLER2
  321. #undef _DEFINE_SWIZZLER2
  322. };
  323. template <typename T, typename V>
  324. [[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) {
  325. using C = std::common_type_t<T, V>;
  326. return Vec3<T>(static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.x)),
  327. static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.y)),
  328. static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.z)));
  329. }
  330. template <>
  331. inline float Vec3<float>::Length() const {
  332. return std::sqrt(x * x + y * y + z * z);
  333. }
  334. template <>
  335. inline Vec3<float> Vec3<float>::Normalized() const {
  336. return *this / Length();
  337. }
  338. template <>
  339. inline float Vec3<float>::Normalize() {
  340. float length = Length();
  341. *this /= length;
  342. return length;
  343. }
  344. using Vec3f = Vec3<float>;
  345. template <typename T>
  346. class Vec4 {
  347. public:
  348. T x{};
  349. T y{};
  350. T z{};
  351. T w{};
  352. constexpr Vec4() = default;
  353. constexpr Vec4(const T& x_, const T& y_, const T& z_, const T& w_)
  354. : x(x_), y(y_), z(z_), w(w_) {}
  355. template <typename T2>
  356. [[nodiscard]] constexpr Vec4<T2> Cast() const {
  357. return Vec4<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z),
  358. static_cast<T2>(w));
  359. }
  360. [[nodiscard]] static constexpr Vec4 AssignToAll(const T& f) {
  361. return Vec4(f, f, f, f);
  362. }
  363. [[nodiscard]] constexpr Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const {
  364. return {x + other.x, y + other.y, z + other.z, w + other.w};
  365. }
  366. constexpr Vec4& operator+=(const Vec4& other) {
  367. x += other.x;
  368. y += other.y;
  369. z += other.z;
  370. w += other.w;
  371. return *this;
  372. }
  373. [[nodiscard]] constexpr Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const {
  374. return {x - other.x, y - other.y, z - other.z, w - other.w};
  375. }
  376. constexpr Vec4& operator-=(const Vec4& other) {
  377. x -= other.x;
  378. y -= other.y;
  379. z -= other.z;
  380. w -= other.w;
  381. return *this;
  382. }
  383. template <typename U = T>
  384. [[nodiscard]] constexpr Vec4<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
  385. return {-x, -y, -z, -w};
  386. }
  387. [[nodiscard]] constexpr Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const {
  388. return {x * other.x, y * other.y, z * other.z, w * other.w};
  389. }
  390. template <typename V>
  391. [[nodiscard]] constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const {
  392. using TV = decltype(T{} * V{});
  393. using C = std::common_type_t<T, V>;
  394. return {
  395. static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)),
  396. static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)),
  397. static_cast<TV>(static_cast<C>(z) * static_cast<C>(f)),
  398. static_cast<TV>(static_cast<C>(w) * static_cast<C>(f)),
  399. };
  400. }
  401. template <typename V>
  402. constexpr Vec4& operator*=(const V& f) {
  403. *this = *this * f;
  404. return *this;
  405. }
  406. template <typename V>
  407. [[nodiscard]] constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const {
  408. using TV = decltype(T{} / V{});
  409. using C = std::common_type_t<T, V>;
  410. return {
  411. static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)),
  412. static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)),
  413. static_cast<TV>(static_cast<C>(z) / static_cast<C>(f)),
  414. static_cast<TV>(static_cast<C>(w) / static_cast<C>(f)),
  415. };
  416. }
  417. template <typename V>
  418. constexpr Vec4& operator/=(const V& f) {
  419. *this = *this / f;
  420. return *this;
  421. }
  422. [[nodiscard]] constexpr T Length2() const {
  423. return x * x + y * y + z * z + w * w;
  424. }
  425. [[nodiscard]] constexpr T& operator[](std::size_t i) {
  426. return *((&x) + i);
  427. }
  428. [[nodiscard]] constexpr const T& operator[](std::size_t i) const {
  429. return *((&x) + i);
  430. }
  431. constexpr void SetZero() {
  432. x = 0;
  433. y = 0;
  434. z = 0;
  435. w = 0;
  436. }
  437. // Common alias: RGBA (colors)
  438. [[nodiscard]] constexpr T& r() {
  439. return x;
  440. }
  441. [[nodiscard]] constexpr T& g() {
  442. return y;
  443. }
  444. [[nodiscard]] constexpr T& b() {
  445. return z;
  446. }
  447. [[nodiscard]] constexpr T& a() {
  448. return w;
  449. }
  450. [[nodiscard]] constexpr const T& r() const {
  451. return x;
  452. }
  453. [[nodiscard]] constexpr const T& g() const {
  454. return y;
  455. }
  456. [[nodiscard]] constexpr const T& b() const {
  457. return z;
  458. }
  459. [[nodiscard]] constexpr const T& a() const {
  460. return w;
  461. }
  462. // Swizzlers - Create a subvector of specific components
  463. // e.g. Vec2 uv() { return Vec2(x,y); }
  464. // _DEFINE_SWIZZLER2 defines a single such function
  465. // DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r)
  466. // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and
  467. // permutations (xy<->yx)
  468. #define _DEFINE_SWIZZLER2(a, b, name) \
  469. [[nodiscard]] constexpr Vec2<T> name() const { return Vec2<T>(a, b); }
  470. #define DEFINE_SWIZZLER2_COMP1(a, a2) \
  471. _DEFINE_SWIZZLER2(a, a, a##a); \
  472. _DEFINE_SWIZZLER2(a, a, a2##a2)
  473. #define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \
  474. _DEFINE_SWIZZLER2(a, b, a##b); \
  475. _DEFINE_SWIZZLER2(a, b, a2##b2); \
  476. _DEFINE_SWIZZLER2(b, a, b##a); \
  477. _DEFINE_SWIZZLER2(b, a, b2##a2)
  478. DEFINE_SWIZZLER2_COMP2(x, y, r, g);
  479. DEFINE_SWIZZLER2_COMP2(x, z, r, b);
  480. DEFINE_SWIZZLER2_COMP2(x, w, r, a);
  481. DEFINE_SWIZZLER2_COMP2(y, z, g, b);
  482. DEFINE_SWIZZLER2_COMP2(y, w, g, a);
  483. DEFINE_SWIZZLER2_COMP2(z, w, b, a);
  484. DEFINE_SWIZZLER2_COMP1(x, r);
  485. DEFINE_SWIZZLER2_COMP1(y, g);
  486. DEFINE_SWIZZLER2_COMP1(z, b);
  487. DEFINE_SWIZZLER2_COMP1(w, a);
  488. #undef DEFINE_SWIZZLER2_COMP1
  489. #undef DEFINE_SWIZZLER2_COMP2
  490. #undef _DEFINE_SWIZZLER2
  491. #define _DEFINE_SWIZZLER3(a, b, c, name) \
  492. [[nodiscard]] constexpr Vec3<T> name() const { return Vec3<T>(a, b, c); }
  493. #define DEFINE_SWIZZLER3_COMP1(a, a2) \
  494. _DEFINE_SWIZZLER3(a, a, a, a##a##a); \
  495. _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2)
  496. #define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \
  497. _DEFINE_SWIZZLER3(a, b, c, a##b##c); \
  498. _DEFINE_SWIZZLER3(a, c, b, a##c##b); \
  499. _DEFINE_SWIZZLER3(b, a, c, b##a##c); \
  500. _DEFINE_SWIZZLER3(b, c, a, b##c##a); \
  501. _DEFINE_SWIZZLER3(c, a, b, c##a##b); \
  502. _DEFINE_SWIZZLER3(c, b, a, c##b##a); \
  503. _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \
  504. _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \
  505. _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \
  506. _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \
  507. _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \
  508. _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2)
  509. DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b);
  510. DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a);
  511. DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a);
  512. DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a);
  513. DEFINE_SWIZZLER3_COMP1(x, r);
  514. DEFINE_SWIZZLER3_COMP1(y, g);
  515. DEFINE_SWIZZLER3_COMP1(z, b);
  516. DEFINE_SWIZZLER3_COMP1(w, a);
  517. #undef DEFINE_SWIZZLER3_COMP1
  518. #undef DEFINE_SWIZZLER3_COMP3
  519. #undef _DEFINE_SWIZZLER3
  520. };
  521. template <typename T, typename V>
  522. [[nodiscard]] constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) {
  523. using TV = decltype(V{} * T{});
  524. using C = std::common_type_t<T, V>;
  525. return {
  526. static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.x)),
  527. static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.y)),
  528. static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.z)),
  529. static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.w)),
  530. };
  531. }
  532. using Vec4f = Vec4<float>;
  533. template <typename T>
  534. constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) {
  535. return a.x * b.x + a.y * b.y;
  536. }
  537. template <typename T>
  538. [[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) {
  539. return a.x * b.x + a.y * b.y + a.z * b.z;
  540. }
  541. template <typename T>
  542. [[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) {
  543. return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  544. }
  545. template <typename T>
  546. [[nodiscard]] constexpr Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a,
  547. const Vec3<T>& b) {
  548. 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};
  549. }
  550. // linear interpolation via float: 0.0=begin, 1.0=end
  551. template <typename X>
  552. [[nodiscard]] constexpr decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end,
  553. const float t) {
  554. return begin * (1.f - t) + end * t;
  555. }
  556. // linear interpolation via int: 0=begin, base=end
  557. template <typename X, int base>
  558. [[nodiscard]] constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin,
  559. const X& end,
  560. const int t) {
  561. return (begin * (base - t) + end * t) / base;
  562. }
  563. // bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second
  564. // interpolation.
  565. template <typename X>
  566. [[nodiscard]] constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11,
  567. const float s, const float t) {
  568. auto y0 = Lerp(x00, x01, s);
  569. auto y1 = Lerp(x10, x11, s);
  570. return Lerp(y0, y1, t);
  571. }
  572. // Utility vector factories
  573. template <typename T>
  574. [[nodiscard]] constexpr Vec2<T> MakeVec(const T& x, const T& y) {
  575. return Vec2<T>{x, y};
  576. }
  577. template <typename T>
  578. [[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const T& y, const T& z) {
  579. return Vec3<T>{x, y, z};
  580. }
  581. template <typename T>
  582. [[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) {
  583. return MakeVec(x, y, zw[0], zw[1]);
  584. }
  585. template <typename T>
  586. [[nodiscard]] constexpr Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) {
  587. return MakeVec(xy[0], xy[1], z);
  588. }
  589. template <typename T>
  590. [[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) {
  591. return MakeVec(x, yz[0], yz[1]);
  592. }
  593. template <typename T>
  594. [[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) {
  595. return Vec4<T>{x, y, z, w};
  596. }
  597. template <typename T>
  598. [[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) {
  599. return MakeVec(xy[0], xy[1], z, w);
  600. }
  601. template <typename T>
  602. [[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) {
  603. return MakeVec(x, yz[0], yz[1], w);
  604. }
  605. // NOTE: This has priority over "Vec2<Vec2<T>> MakeVec(const Vec2<T>& x, const Vec2<T>& y)".
  606. // Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error
  607. // out soon enough due to misuse of the returned structure.
  608. template <typename T>
  609. [[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) {
  610. return MakeVec(xy[0], xy[1], zw[0], zw[1]);
  611. }
  612. template <typename T>
  613. [[nodiscard]] constexpr Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) {
  614. return MakeVec(xyz[0], xyz[1], xyz[2], w);
  615. }
  616. template <typename T>
  617. [[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) {
  618. return MakeVec(x, yzw[0], yzw[1], yzw[2]);
  619. }
  620. } // namespace Common