vector_math.h 21 KB

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