vector_math.h 23 KB

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