vector_math.h 24 KB

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