vector_math.h 22 KB

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