vector_math.h 21 KB

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