vector_math.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. namespace Math {
  32. template <typename T>
  33. class Vec2;
  34. template <typename T>
  35. class Vec3;
  36. template <typename T>
  37. class Vec4;
  38. template <typename T>
  39. static inline Vec2<T> MakeVec(const T& x, const T& y);
  40. template <typename T>
  41. static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z);
  42. template <typename T>
  43. static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w);
  44. template <typename T>
  45. class Vec2 {
  46. public:
  47. T x;
  48. T y;
  49. T* AsArray() {
  50. return &x;
  51. }
  52. Vec2() = default;
  53. Vec2(const T& _x, const T& _y) : x(_x), y(_y) {}
  54. template <typename T2>
  55. Vec2<T2> Cast() const {
  56. return Vec2<T2>((T2)x, (T2)y);
  57. }
  58. static Vec2 AssignToAll(const T& f) {
  59. return Vec2<T>(f, f);
  60. }
  61. void Write(T a[2]) {
  62. a[0] = x;
  63. a[1] = y;
  64. }
  65. Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const {
  66. return MakeVec(x + other.x, y + other.y);
  67. }
  68. void operator+=(const Vec2& other) {
  69. x += other.x;
  70. y += other.y;
  71. }
  72. Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const {
  73. return MakeVec(x - other.x, y - other.y);
  74. }
  75. void operator-=(const Vec2& other) {
  76. x -= other.x;
  77. y -= other.y;
  78. }
  79. Vec2<decltype(-T{})> operator-() const {
  80. return MakeVec(-x, -y);
  81. }
  82. Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const {
  83. return MakeVec(x * other.x, y * other.y);
  84. }
  85. template <typename V>
  86. Vec2<decltype(T{} * V{})> operator*(const V& f) const {
  87. return MakeVec(x * f, y * f);
  88. }
  89. template <typename V>
  90. void operator*=(const V& f) {
  91. x *= f;
  92. y *= f;
  93. }
  94. template <typename V>
  95. Vec2<decltype(T{} / V{})> operator/(const V& f) const {
  96. return MakeVec(x / f, y / f);
  97. }
  98. template <typename V>
  99. void operator/=(const V& f) {
  100. *this = *this / f;
  101. }
  102. T Length2() const {
  103. return x * x + y * y;
  104. }
  105. // Only implemented for T=float
  106. float Length() const;
  107. void SetLength(const float l);
  108. Vec2 WithLength(const float l) const;
  109. float Distance2To(Vec2& other);
  110. Vec2 Normalized() const;
  111. float Normalize(); // returns the previous length, which is often useful
  112. T& operator[](int i) // allow vector[1] = 3 (vector.y=3)
  113. {
  114. return *((&x) + i);
  115. }
  116. T operator[](const int i) const {
  117. return *((&x) + i);
  118. }
  119. void SetZero() {
  120. x = 0;
  121. y = 0;
  122. }
  123. // Common aliases: UV (texel coordinates), ST (texture coordinates)
  124. T& u() {
  125. return x;
  126. }
  127. T& v() {
  128. return y;
  129. }
  130. T& s() {
  131. return x;
  132. }
  133. T& t() {
  134. return y;
  135. }
  136. const T& u() const {
  137. return x;
  138. }
  139. const T& v() const {
  140. return y;
  141. }
  142. const T& s() const {
  143. return x;
  144. }
  145. const T& t() const {
  146. return y;
  147. }
  148. // swizzlers - create a subvector of specific components
  149. const Vec2 yx() const {
  150. return Vec2(y, x);
  151. }
  152. const Vec2 vu() const {
  153. return Vec2(y, x);
  154. }
  155. const Vec2 ts() const {
  156. return Vec2(y, x);
  157. }
  158. };
  159. template <typename T, typename V>
  160. Vec2<T> operator*(const V& f, const Vec2<T>& vec) {
  161. return Vec2<T>(f * vec.x, f * vec.y);
  162. }
  163. typedef Vec2<float> Vec2f;
  164. template <>
  165. inline float Vec2<float>::Length() const {
  166. return std::sqrt(x * x + y * y);
  167. }
  168. template <>
  169. inline float Vec2<float>::Normalize() {
  170. float length = Length();
  171. *this /= length;
  172. return length;
  173. }
  174. template <typename T>
  175. class Vec3 {
  176. public:
  177. T x;
  178. T y;
  179. T z;
  180. T* AsArray() {
  181. return &x;
  182. }
  183. Vec3() = default;
  184. Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {}
  185. template <typename T2>
  186. Vec3<T2> Cast() const {
  187. return MakeVec<T2>((T2)x, (T2)y, (T2)z);
  188. }
  189. // Only implemented for T=int and T=float
  190. static Vec3 FromRGB(unsigned int rgb);
  191. unsigned int ToRGB() const; // alpha bits set to zero
  192. static Vec3 AssignToAll(const T& f) {
  193. return MakeVec(f, f, f);
  194. }
  195. void Write(T a[3]) {
  196. a[0] = x;
  197. a[1] = y;
  198. a[2] = z;
  199. }
  200. Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const {
  201. return MakeVec(x + other.x, y + other.y, z + other.z);
  202. }
  203. void operator+=(const Vec3& other) {
  204. x += other.x;
  205. y += other.y;
  206. z += other.z;
  207. }
  208. Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const {
  209. return MakeVec(x - other.x, y - other.y, z - other.z);
  210. }
  211. void operator-=(const Vec3& other) {
  212. x -= other.x;
  213. y -= other.y;
  214. z -= other.z;
  215. }
  216. Vec3<decltype(-T{})> operator-() const {
  217. return MakeVec(-x, -y, -z);
  218. }
  219. Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const {
  220. return MakeVec(x * other.x, y * other.y, z * other.z);
  221. }
  222. template <typename V>
  223. Vec3<decltype(T{} * V{})> operator*(const V& f) const {
  224. return MakeVec(x * f, y * f, z * f);
  225. }
  226. template <typename V>
  227. void operator*=(const V& f) {
  228. x *= f;
  229. y *= f;
  230. z *= 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 Q = T>
  406. Vec4<decltype(-T{})> 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. x *= f;
  419. y *= f;
  420. z *= f;
  421. w *= f;
  422. }
  423. template <typename V>
  424. Vec4<decltype(T{} / V{})> operator/(const V& f) const {
  425. return MakeVec(x / f, y / f, z / f, w / f);
  426. }
  427. template <typename V>
  428. void operator/=(const V& f) {
  429. *this = *this / f;
  430. }
  431. T Length2() const {
  432. return x * x + y * y + z * z + w * w;
  433. }
  434. // Only implemented for T=float
  435. float Length() const;
  436. void SetLength(const float l);
  437. Vec4 WithLength(const float l) const;
  438. float Distance2To(Vec4& other);
  439. Vec4 Normalized() const;
  440. float Normalize(); // returns the previous length, which is often useful
  441. T& operator[](int i) // allow vector[2] = 3 (vector.z=3)
  442. {
  443. return *((&x) + i);
  444. }
  445. T operator[](const int i) const {
  446. return *((&x) + i);
  447. }
  448. void SetZero() {
  449. x = 0;
  450. y = 0;
  451. z = 0;
  452. w = 0;
  453. }
  454. // Common alias: RGBA (colors)
  455. T& r() {
  456. return x;
  457. }
  458. T& g() {
  459. return y;
  460. }
  461. T& b() {
  462. return z;
  463. }
  464. T& a() {
  465. return w;
  466. }
  467. const T& r() const {
  468. return x;
  469. }
  470. const T& g() const {
  471. return y;
  472. }
  473. const T& b() const {
  474. return z;
  475. }
  476. const T& a() const {
  477. return w;
  478. }
  479. // Swizzlers - Create a subvector of specific components
  480. // e.g. Vec2 uv() { return Vec2(x,y); }
  481. // _DEFINE_SWIZZLER2 defines a single such function
  482. // DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r)
  483. // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and
  484. // permutations (xy<->yx)
  485. #define _DEFINE_SWIZZLER2(a, b, name) \
  486. const Vec2<T> name() const { \
  487. return Vec2<T>(a, b); \
  488. }
  489. #define DEFINE_SWIZZLER2_COMP1(a, a2) \
  490. _DEFINE_SWIZZLER2(a, a, a##a); \
  491. _DEFINE_SWIZZLER2(a, a, a2##a2)
  492. #define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \
  493. _DEFINE_SWIZZLER2(a, b, a##b); \
  494. _DEFINE_SWIZZLER2(a, b, a2##b2); \
  495. _DEFINE_SWIZZLER2(b, a, b##a); \
  496. _DEFINE_SWIZZLER2(b, a, b2##a2)
  497. DEFINE_SWIZZLER2_COMP2(x, y, r, g);
  498. DEFINE_SWIZZLER2_COMP2(x, z, r, b);
  499. DEFINE_SWIZZLER2_COMP2(x, w, r, a);
  500. DEFINE_SWIZZLER2_COMP2(y, z, g, b);
  501. DEFINE_SWIZZLER2_COMP2(y, w, g, a);
  502. DEFINE_SWIZZLER2_COMP2(z, w, b, a);
  503. DEFINE_SWIZZLER2_COMP1(x, r);
  504. DEFINE_SWIZZLER2_COMP1(y, g);
  505. DEFINE_SWIZZLER2_COMP1(z, b);
  506. DEFINE_SWIZZLER2_COMP1(w, a);
  507. #undef DEFINE_SWIZZLER2_COMP1
  508. #undef DEFINE_SWIZZLER2_COMP2
  509. #undef _DEFINE_SWIZZLER2
  510. #define _DEFINE_SWIZZLER3(a, b, c, name) \
  511. const Vec3<T> name() const { \
  512. return Vec3<T>(a, b, c); \
  513. }
  514. #define DEFINE_SWIZZLER3_COMP1(a, a2) \
  515. _DEFINE_SWIZZLER3(a, a, a, a##a##a); \
  516. _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2)
  517. #define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \
  518. _DEFINE_SWIZZLER3(a, b, c, a##b##c); \
  519. _DEFINE_SWIZZLER3(a, c, b, a##c##b); \
  520. _DEFINE_SWIZZLER3(b, a, c, b##a##c); \
  521. _DEFINE_SWIZZLER3(b, c, a, b##c##a); \
  522. _DEFINE_SWIZZLER3(c, a, b, c##a##b); \
  523. _DEFINE_SWIZZLER3(c, b, a, c##b##a); \
  524. _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \
  525. _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \
  526. _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \
  527. _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \
  528. _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \
  529. _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2)
  530. DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b);
  531. DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a);
  532. DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a);
  533. DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a);
  534. DEFINE_SWIZZLER3_COMP1(x, r);
  535. DEFINE_SWIZZLER3_COMP1(y, g);
  536. DEFINE_SWIZZLER3_COMP1(z, b);
  537. DEFINE_SWIZZLER3_COMP1(w, a);
  538. #undef DEFINE_SWIZZLER3_COMP1
  539. #undef DEFINE_SWIZZLER3_COMP3
  540. #undef _DEFINE_SWIZZLER3
  541. };
  542. template <typename T, typename V>
  543. Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) {
  544. return MakeVec(f * vec.x, f * vec.y, f * vec.z, f * vec.w);
  545. }
  546. typedef Vec4<float> Vec4f;
  547. template <typename T>
  548. static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) {
  549. return a.x * b.x + a.y * b.y;
  550. }
  551. template <typename T>
  552. static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) {
  553. return a.x * b.x + a.y * b.y + a.z * b.z;
  554. }
  555. template <typename T>
  556. static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) {
  557. return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  558. }
  559. template <typename T>
  560. static inline Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a, const Vec3<T>& b) {
  561. 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);
  562. }
  563. // linear interpolation via float: 0.0=begin, 1.0=end
  564. template <typename X>
  565. static inline decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end,
  566. const float t) {
  567. return begin * (1.f - t) + end * t;
  568. }
  569. // linear interpolation via int: 0=begin, base=end
  570. template <typename X, int base>
  571. static inline decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, const X& end,
  572. const int t) {
  573. return (begin * (base - t) + end * t) / base;
  574. }
  575. // bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second
  576. // interpolation.
  577. template <typename X>
  578. inline auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s,
  579. const float t) {
  580. auto y0 = Lerp(x00, x01, s);
  581. auto y1 = Lerp(x10, x11, s);
  582. return Lerp(y0, y1, t);
  583. }
  584. // Utility vector factories
  585. template <typename T>
  586. static inline Vec2<T> MakeVec(const T& x, const T& y) {
  587. return Vec2<T>{x, y};
  588. }
  589. template <typename T>
  590. static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z) {
  591. return Vec3<T>{x, y, z};
  592. }
  593. template <typename T>
  594. static inline Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) {
  595. return MakeVec(x, y, zw[0], zw[1]);
  596. }
  597. template <typename T>
  598. static inline Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) {
  599. return MakeVec(xy[0], xy[1], z);
  600. }
  601. template <typename T>
  602. static inline Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) {
  603. return MakeVec(x, yz[0], yz[1]);
  604. }
  605. template <typename T>
  606. static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) {
  607. return Vec4<T>{x, y, z, w};
  608. }
  609. template <typename T>
  610. static inline Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) {
  611. return MakeVec(xy[0], xy[1], z, w);
  612. }
  613. template <typename T>
  614. static inline Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) {
  615. return MakeVec(x, yz[0], yz[1], w);
  616. }
  617. // NOTE: This has priority over "Vec2<Vec2<T>> MakeVec(const Vec2<T>& x, const Vec2<T>& y)".
  618. // Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error
  619. // out soon enough due to misuse of the returned structure.
  620. template <typename T>
  621. static inline Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) {
  622. return MakeVec(xy[0], xy[1], zw[0], zw[1]);
  623. }
  624. template <typename T>
  625. static inline Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) {
  626. return MakeVec(xyz[0], xyz[1], xyz[2], w);
  627. }
  628. template <typename T>
  629. static inline Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) {
  630. return MakeVec(x, yzw[0], yzw[1], yzw[2]);
  631. }
  632. } // namespace