BsMath.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Math/BsDegree.h"
  6. #include "Math/BsRadian.h"
  7. #include "Math/BsVector3.h"
  8. namespace bs
  9. {
  10. /** @addtogroup Math
  11. * @{
  12. */
  13. /** Utility class providing common scalar math operations. */
  14. class BS_UTILITY_EXPORT Math
  15. {
  16. public:
  17. /** Inverse cosine. */
  18. static Radian acos(float val);
  19. /** Inverse sine. */
  20. static Radian asin(float val);
  21. /** Inverse tangent. */
  22. static Radian atan(float val) { return Radian(std::atan(val)); }
  23. /** Inverse tangent with two arguments, returns angle between the X axis and the point. */
  24. static Radian atan2(float y, float x) { return Radian(std::atan2(y,x)); }
  25. /** Cosine. */
  26. static float cos(const Radian& val) { return (float)std::cos(val.valueRadians()); }
  27. /** Cosine. */
  28. static float cos(float val) { return (float)std::cos(val); }
  29. /** Sine. */
  30. static float sin(const Radian& val) { return (float)std::sin(val.valueRadians()); }
  31. /** Sine. */
  32. static float sin(float val) { return (float)std::sin(val); }
  33. /** Tangent. */
  34. static float tan(const Radian& val) { return (float)std::tan(val.valueRadians()); }
  35. /** Tangent. */
  36. static float tan(float val) { return (float)std::tan(val); }
  37. /** Square root. */
  38. static float sqrt(float val) { return (float)std::sqrt(val); }
  39. /** Square root. */
  40. static Radian sqrt(const Radian& val) { return Radian(std::sqrt(val.valueRadians())); }
  41. /** Square root. */
  42. static Degree sqrt(const Degree& val) { return Degree(std::sqrt(val.valueDegrees())); }
  43. /** Square root followed by an inverse. */
  44. static float invSqrt(float val);
  45. /** Returns square of the provided value. */
  46. static float sqr(float val) { return val*val; }
  47. /** Returns base raised to the provided power. */
  48. static float pow(float base, float exponent) { return (float)std::pow(base, exponent); }
  49. /** Returns euler number (e) raised to the provided power. */
  50. static float exp(float val) { return (float)std::exp(val); }
  51. /** Returns natural (base e) logarithm of the provided value. */
  52. static float log(float val) { return (float)std::log(val); }
  53. /** Returns base 2 logarithm of the provided value. */
  54. static float log2(float val) { return (float)(std::log(val)/LOG2); }
  55. /** Returns base N logarithm of the provided value. */
  56. static float logN(float base, float val) { return (float)(std::log(val)/std::log(base)); }
  57. /** Returns the sign of the provided value as 1 or -1. */
  58. static float sign(float val);
  59. /** Returns the sign of the provided value as 1 or -1. */
  60. static Radian sign(const Radian& val) { return Radian(sign(val.valueRadians())); }
  61. /** Returns the sign of the provided value as 1 or -1. */
  62. static Degree sign(const Degree& val) { return Degree(sign(val.valueDegrees())); }
  63. /** Returns the absolute value. */
  64. static float abs(float val) { return float(std::fabs(val)); }
  65. /** Returns the absolute value. */
  66. static Degree abs(const Degree& val) { return Degree(std::fabs(val.valueDegrees())); }
  67. /** Returns the absolute value. */
  68. static Radian abs(const Radian& val) { return Radian(std::fabs(val.valueRadians())); }
  69. /** Returns the nearest integer equal or higher to the provided value. */
  70. static float ceil(float val) { return (float)std::ceil(val); }
  71. /** Returns the nearest integer equal or higher to the provided value. */
  72. static int ceilToInt(float val) { return (int)std::ceil(val); }
  73. /** Returns the integer nearest to the provided value. */
  74. static float round(float val) { return (float)std::floor(val + 0.5f); }
  75. /** Returns the integer nearest to the provided value. */
  76. static int roundToInt(float val) { return (int)std::floor(val + 0.5f); }
  77. /**
  78. * Divides an integer by another integer and returns the result, rounded up. Only works if both integers are
  79. * positive.
  80. */
  81. template<class T>
  82. static T divideAndRoundUp(T n, T d) { return (n + d - 1) / d; }
  83. /** Returns the nearest integer equal or lower of the provided value. */
  84. static float floor(float val) { return (float)std::floor(val); }
  85. /** Returns the nearest integer equal or lower of the provided value. */
  86. static int floorToInt(float val) { return (int)std::floor(val); }
  87. /** Clamp a value within an inclusive range. */
  88. template <typename T>
  89. static T clamp(T val, T minval, T maxval)
  90. {
  91. assert (minval <= maxval && "Invalid clamp range");
  92. return std::max(std::min(val, maxval), minval);
  93. }
  94. /** Clamp a value within an inclusive range [0..1]. */
  95. template <typename T>
  96. static T clamp01(T val)
  97. {
  98. return std::max(std::min(val, (T)1), (T)0);
  99. }
  100. /** Checks if the value is a valid number. */
  101. static bool isNaN(float f)
  102. {
  103. return f != f;
  104. }
  105. /** Compare two floats, using tolerance for inaccuracies. */
  106. static bool approxEquals(float a, float b,
  107. float tolerance = std::numeric_limits<float>::epsilon())
  108. {
  109. return fabs(b - a) <= tolerance;
  110. }
  111. /** Compare two doubles, using tolerance for inaccuracies. */
  112. static bool approxEquals(double a, double b,
  113. double tolerance = std::numeric_limits<double>::epsilon())
  114. {
  115. return fabs(b - a) <= tolerance;
  116. }
  117. /** Compare two 2D vectors, using tolerance for inaccuracies. */
  118. static bool approxEquals(const Vector2& a, const Vector2& b,
  119. float tolerance = std::numeric_limits<float>::epsilon());
  120. /** Compare two 3D vectors, using tolerance for inaccuracies. */
  121. static bool approxEquals(const Vector3& a, const Vector3& b,
  122. float tolerance = std::numeric_limits<float>::epsilon());
  123. /** Compare two 4D vectors, using tolerance for inaccuracies. */
  124. static bool approxEquals(const Vector4& a, const Vector4& b,
  125. float tolerance = std::numeric_limits<float>::epsilon());
  126. /** Compare two quaternions, using tolerance for inaccuracies. */
  127. static bool approxEquals(const Quaternion& a, const Quaternion& b,
  128. float tolerance = std::numeric_limits<float>::epsilon());
  129. /** Calculates the tangent space vector for a given set of positions / texture coords. */
  130. static Vector3 calculateTriTangent(const Vector3& position1, const Vector3& position2,
  131. const Vector3& position3, float u1, float v1, float u2, float v2, float u3, float v3);
  132. /************************************************************************/
  133. /* TRIG APPROXIMATIONS */
  134. /************************************************************************/
  135. /**
  136. * Sine function approximation.
  137. *
  138. * @param[in] val Angle in range [0, pi/2].
  139. *
  140. * @note Evaluates trigonometric functions using polynomial approximations.
  141. */
  142. static float fastSin0(const Radian& val) { return (float)fastASin0(val.valueRadians()); }
  143. /**
  144. * Sine function approximation.
  145. *
  146. * @param[in] val Angle in range [0, pi/2].
  147. *
  148. * @note Evaluates trigonometric functions using polynomial approximations.
  149. */
  150. static float fastSin0(float val);
  151. /**
  152. * Sine function approximation.
  153. *
  154. * @param[in] val Angle in range [0, pi/2].
  155. *
  156. * @note
  157. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastSin0.
  158. */
  159. static float fastSin1(const Radian& val) { return (float)fastASin1(val.valueRadians()); }
  160. /**
  161. * Sine function approximation.
  162. *
  163. * @param[in] val Angle in range [0, pi/2].
  164. *
  165. * @note
  166. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastSin0.
  167. */
  168. static float fastSin1(float val);
  169. /**
  170. * Cosine function approximation.
  171. *
  172. * @param[in] val Angle in range [0, pi/2].
  173. *
  174. * @note Evaluates trigonometric functions using polynomial approximations.
  175. */
  176. static float fastCos0(const Radian& val) { return (float)fastACos0(val.valueRadians()); }
  177. /**
  178. * Cosine function approximation.
  179. *
  180. * @param[in] val Angle in range [0, pi/2].
  181. *
  182. * @note Evaluates trigonometric functions using polynomial approximations.
  183. */
  184. static float fastCos0(float val);
  185. /**
  186. * Cosine function approximation.
  187. *
  188. * @param[in] val Angle in range [0, pi/2].
  189. *
  190. * @note
  191. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastCos0.
  192. */
  193. static float fastCos1(const Radian& val) { return (float)fastACos1(val.valueRadians()); }
  194. /**
  195. * Cosine function approximation.
  196. *
  197. * @param[in] val Angle in range [0, pi/2].
  198. *
  199. * @note
  200. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastCos0.
  201. */
  202. static float fastCos1(float val);
  203. /**
  204. * Tangent function approximation.
  205. *
  206. * @param[in] val Angle in range [0, pi/4].
  207. *
  208. * @note Evaluates trigonometric functions using polynomial approximations.
  209. */
  210. static float fastTan0(const Radian& val) { return (float)fastATan0(val.valueRadians()); }
  211. /**
  212. * Tangent function approximation.
  213. *
  214. * @param[in] val Angle in range [0, pi/4].
  215. *
  216. * @note Evaluates trigonometric functions using polynomial approximations.
  217. */
  218. static float fastTan0(float val);
  219. /**
  220. * Tangent function approximation.
  221. *
  222. * @param[in] val Angle in range [0, pi/4].
  223. *
  224. * @note
  225. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastTan0.
  226. */
  227. static float fastTan1(const Radian& val) { return (float)fastATan1(val.valueRadians()); }
  228. /**
  229. * Tangent function approximation.
  230. *
  231. * @param[in] val Angle in range [0, pi/4].
  232. *
  233. * @note
  234. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastTan0.
  235. */
  236. static float fastTan1(float val);
  237. /**
  238. * Inverse sine function approximation.
  239. *
  240. * @param[in] val Angle in range [0, 1].
  241. *
  242. * @note Evaluates trigonometric functions using polynomial approximations.
  243. */
  244. static float fastASin0(const Radian& val) { return (float)fastASin0(val.valueRadians()); }
  245. /**
  246. * Inverse sine function approximation.
  247. *
  248. * @param[in] val Angle in range [0, 1].
  249. *
  250. * @note Evaluates trigonometric functions using polynomial approximations.
  251. */
  252. static float fastASin0(float val);
  253. /**
  254. * Inverse sine function approximation.
  255. *
  256. * @param[in] val Angle in range [0, 1].
  257. *
  258. * @note
  259. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastASin0.
  260. */
  261. static float fastASin1(const Radian& val) { return (float)fastASin1(val.valueRadians()); }
  262. /**
  263. * Inverse sine function approximation.
  264. *
  265. * @param[in] val Angle in range [0, 1].
  266. *
  267. * @note
  268. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastASin0.
  269. */
  270. static float fastASin1(float val);
  271. /**
  272. * Inverse cosine function approximation.
  273. *
  274. * @param[in] val Angle in range [0, 1].
  275. *
  276. * @note Evaluates trigonometric functions using polynomial approximations.
  277. */
  278. static float fastACos0(const Radian& val) { return (float)fastACos0(val.valueRadians()); }
  279. /**
  280. * Inverse cosine function approximation.
  281. *
  282. * @param[in] val Angle in range [0, 1].
  283. *
  284. * @note Evaluates trigonometric functions using polynomial approximations.
  285. */
  286. static float fastACos0(float val);
  287. /**
  288. * Inverse cosine function approximation.
  289. *
  290. * @param[in] val Angle in range [0, 1].
  291. *
  292. * @note
  293. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastACos0.
  294. */
  295. static float fastACos1(const Radian& val) { return (float)fastACos1(val.valueRadians()); }
  296. /**
  297. * Inverse cosine function approximation.
  298. *
  299. * @param[in] val Angle in range [0, 1].
  300. *
  301. * @note
  302. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastACos0.
  303. */
  304. static float fastACos1(float val);
  305. /**
  306. * Inverse tangent function approximation.
  307. *
  308. * @param[in] val Angle in range [-1, 1].
  309. *
  310. * @note Evaluates trigonometric functions using polynomial approximations.
  311. */
  312. static float fastATan0(const Radian& val) { return (float)fastATan0(val.valueRadians()); }
  313. /**
  314. * Inverse tangent function approximation.
  315. *
  316. * @param[in] val Angle in range [-1, 1].
  317. *
  318. * @note Evaluates trigonometric functions using polynomial approximations.
  319. */
  320. static float fastATan0(float val);
  321. /**
  322. * Inverse tangent function approximation.
  323. *
  324. * @param[in] val Angle in range [-1, 1].
  325. *
  326. * @note
  327. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastATan0.
  328. */
  329. static float fastATan1(const Radian& val) { return (float)fastATan1(val.valueRadians()); }
  330. /**
  331. * Inverse tangent function approximation.
  332. *
  333. * @param[in] val Angle in range [-1, 1].
  334. *
  335. * @note
  336. * Evaluates trigonometric functions using polynomial approximations. Slightly better (and slower) than fastATan0.
  337. */
  338. static float fastATan1(float val);
  339. /**
  340. * Interpolates between min and max. Returned value is in [0, 1] range where min = 0, max = 1 and 0.5 is
  341. * the average of min and max.
  342. */
  343. template <typename T>
  344. static float lerp01(T val, T min, T max)
  345. {
  346. return clamp01((val - min) / std::max(max - min, 0.0001F));
  347. }
  348. /**
  349. * Solves the linear equation with the parameters A, B. Returns number of roots found and the roots themselves will
  350. * be output in the @p roots array.
  351. *
  352. * @param[in] A First variable.
  353. * @param[in] B Second variable.
  354. * @param[out] roots Must be at least size of 1.
  355. *
  356. * @note Only returns real roots.
  357. */
  358. template <typename T>
  359. static UINT32 solveLinear(T A, T B, T* roots)
  360. {
  361. if (!approxEquals(A, (T)0))
  362. {
  363. roots[0] = -B / A;
  364. return 1;
  365. }
  366. roots[0] = 0.0f;
  367. return 1;
  368. }
  369. /**
  370. * Solves the quadratic equation with the parameters A, B, C. Returns number of roots found and the roots themselves
  371. * will be output in the @p roots array.
  372. *
  373. * @param[in] A First variable.
  374. * @param[in] B Second variable.
  375. * @param[in] C Third variable.
  376. * @param[out] roots Must be at least size of 2.
  377. *
  378. * @note Only returns real roots.
  379. */
  380. template <typename T>
  381. static UINT32 solveQuadratic(T A, T B, T C, T* roots)
  382. {
  383. if (!approxEquals(A, (T)0))
  384. {
  385. T p = B / (2 * A);
  386. T q = C / A;
  387. T D = p * p - q;
  388. if (!approxEquals(D, (T)0))
  389. {
  390. if (D < (T)0)
  391. return 0;
  392. T sqrtD = sqrt(D);
  393. roots[0] = sqrtD - p;
  394. roots[1] = -sqrtD - p;
  395. return 2;
  396. }
  397. else
  398. {
  399. roots[0] = -p;
  400. roots[1] = -p;
  401. return 1;
  402. }
  403. }
  404. else
  405. {
  406. return solveLinear(B, C, roots);
  407. }
  408. }
  409. /**
  410. * Solves the cubic equation with the parameters A, B, C, D. Returns number of roots found and the roots themselves
  411. * will be output in the @p roots array.
  412. *
  413. * @param[in] A First variable.
  414. * @param[in] B Second variable.
  415. * @param[in] C Third variable.
  416. * @param[in] D Fourth variable.
  417. * @param[out] roots Must be at least size of 3.
  418. *
  419. * @note Only returns real roots.
  420. */
  421. template <typename T>
  422. static UINT32 solveCubic(T A, T B, T C, T D, T* roots)
  423. {
  424. static const T THIRD = (1 / (T)3);
  425. T invA = 1 / A;
  426. A = B * invA;
  427. B = C * invA;
  428. C = D * invA;
  429. T sqA = A * A;
  430. T p = THIRD * (-THIRD * sqA + B);
  431. T q = ((T)0.5) * ((2 / (T)27) * A * sqA - THIRD * A * B + C);
  432. T cbp = p * p * p;
  433. D = q * q + cbp;
  434. UINT32 numRoots = 0;
  435. if (!approxEquals(D, (T)0))
  436. {
  437. if (D < 0.0)
  438. {
  439. T phi = THIRD * ::acos(-q / sqrt(-cbp));
  440. T t = 2 * sqrt(-p);
  441. roots[0] = t * cos(phi);
  442. roots[1] = -t * cos(phi + PI * THIRD);
  443. roots[2] = -t * cos(phi - PI * THIRD);
  444. numRoots = 3;
  445. }
  446. else
  447. {
  448. T sqrtD = sqrt(D);
  449. T u = cbrt(sqrtD + fabs(q));
  450. if (q > (T)0)
  451. roots[0] = -u + p / u;
  452. else
  453. roots[0] = u - p / u;
  454. numRoots = 1;
  455. }
  456. }
  457. else
  458. {
  459. if (!approxEquals(q, (T)0))
  460. {
  461. T u = cbrt(-q);
  462. roots[0] = 2 * u;
  463. roots[1] = -u;
  464. numRoots = 2;
  465. }
  466. else
  467. {
  468. roots[0] = 0.0f;
  469. numRoots = 1;
  470. }
  471. }
  472. T sub = THIRD * A;
  473. for (UINT32 i = 0; i < numRoots; i++)
  474. roots[i] -= sub;
  475. return numRoots;
  476. }
  477. /**
  478. * Solves the quartic equation with the parameters A, B, C, D, E. Returns number of roots found and the roots
  479. * themselves will be output in the @p roots array.
  480. *
  481. * @param[in] A First variable.
  482. * @param[in] B Second variable.
  483. * @param[in] C Third variable.
  484. * @param[in] D Fourth variable.
  485. * @param[in] E Fifth variable.
  486. * @param[out] roots Must be at least size of 4.
  487. *
  488. * @note Only returns real roots.
  489. */
  490. template <typename T>
  491. static UINT32 solveQuartic(T A, T B, T C, T D, T E, T* roots)
  492. {
  493. T invA = 1 / A;
  494. A = B * invA;
  495. B = C * invA;
  496. C = D * invA;
  497. D = E * invA;
  498. T sqA = A*A;
  499. T p = -(3 / (T)8) * sqA + B;
  500. T q = (1 / (T)8) * sqA * A - (T)0.5 * A * B + C;
  501. T r = -(3 / (T)256) * sqA * sqA + (1 / (T)16) * sqA * B - (1 / (T)4) * A * C + D;
  502. UINT32 numRoots = 0;
  503. if (!approxEquals(r, (T)0))
  504. {
  505. T cubicA = 1;
  506. T cubicB = -(T)0.5 * p ;
  507. T cubicC = -r;
  508. T cubicD = (T)0.5 * r * p - (1 / (T)8) * q * q;
  509. solveCubic(cubicA, cubicB, cubicC, cubicD, roots);
  510. T z = roots[0];
  511. T u = z * z - r;
  512. T v = 2 * z - p;
  513. if (approxEquals(u, T(0)))
  514. u = 0;
  515. else if (u > 0)
  516. u = sqrt(u);
  517. else
  518. return 0;
  519. if (approxEquals(v, T(0)))
  520. v = 0;
  521. else if (v > 0)
  522. v = sqrt(v);
  523. else
  524. return 0;
  525. T quadraticA = 1;
  526. T quadraticB = q < 0 ? -v : v;
  527. T quadraticC = z - u;
  528. numRoots = solveQuadratic(quadraticA, quadraticB, quadraticC, roots);
  529. quadraticA = 1;
  530. quadraticB = q < 0 ? v : -v;
  531. quadraticC = z + u;
  532. numRoots += solveQuadratic(quadraticA, quadraticB, quadraticC, roots + numRoots);
  533. }
  534. else
  535. {
  536. numRoots = solveCubic(q, p, (T)0, (T)1, roots);
  537. roots[numRoots++] = 0;
  538. }
  539. T sub = (1/(T)4) * A;
  540. for (UINT32 i = 0; i < numRoots; i++)
  541. roots[i] -= sub;
  542. return numRoots;
  543. }
  544. /**
  545. * Evaluates a cubic Hermite curve at a specific point.
  546. *
  547. * @param[in] t Parameter that at which to evaluate the curve, in range [0, 1].
  548. * @param[in] pointA Starting point (at t=0).
  549. * @param[in] pointB Ending point (at t=1).
  550. * @param[in] tangentA Starting tangent (at t=0).
  551. * @param[in] tangentB Ending tangent (at t = 1).
  552. * @return Evaluated value at @p t.
  553. */
  554. template<class T>
  555. static T cubicHermite(float t, const T& pointA, const T& pointB, const T& tangentA, const T& tangentB)
  556. {
  557. float t2 = t * t;
  558. float t3 = t2 * t;
  559. float a = 2 * t3 - 3 * t2 + 1;
  560. float b = t3 - 2 * t2 + t;
  561. float c = -2 * t3 + 3 * t2;
  562. float d = t3 - t2;
  563. return a * pointA + b * tangentA + c * pointB + d * tangentB;
  564. }
  565. /**
  566. * Evaluates the first derivative of a cubic Hermite curve at a specific point.
  567. *
  568. * @param[in] t Parameter that at which to evaluate the curve, in range [0, 1].
  569. * @param[in] pointA Starting point (at t=0).
  570. * @param[in] pointB Ending point (at t=1).
  571. * @param[in] tangentA Starting tangent (at t=0).
  572. * @param[in] tangentB Ending tangent (at t = 1).
  573. * @return Evaluated value at @p t.
  574. */
  575. template<class T>
  576. static T cubicHermiteD1(float t, const T& pointA, const T& pointB, const T& tangentA, const T& tangentB)
  577. {
  578. float t2 = t * t;
  579. float a = 6 * t2 - 6 * t;
  580. float b = 3 * t2 - 4 * t + 1;
  581. float c = -6 * t2 + 6 * t;
  582. float d = 3 * t2 - 2 * t;
  583. return a * pointA + b * tangentA + c * pointB + d * tangentB;
  584. }
  585. /**
  586. * Calculates coefficients needed for evaluating a cubic curve in Hermite form. Assumes @p t has been normalized is
  587. * in range [0, 1]. Tangents must be scaled by the length of the curve (length is the maximum value of @p t before
  588. * it was normalized).
  589. *
  590. * @param[in] pointA Starting point (at t=0).
  591. * @param[in] pointB Ending point (at t=1).
  592. * @param[in] tangentA Starting tangent (at t=0).
  593. * @param[in] tangentB Ending tangent (at t = 1).
  594. * @param[out] coefficients Four coefficients for the cubic curve, in order [t^3, t^2, t, 1].
  595. */
  596. template<class T>
  597. static void cubicHermiteCoefficients(const T& pointA, const T& pointB, const T& tangentA, const T& tangentB,
  598. T (&coefficients)[4])
  599. {
  600. T diff = pointA - pointB;
  601. coefficients[0] = 2 * diff + tangentA + tangentB;
  602. coefficients[1] = -3 * diff - 2 * tangentA - tangentB;
  603. coefficients[2] = tangentA;
  604. coefficients[3] = pointA;
  605. }
  606. /**
  607. * Calculates coefficients needed for evaluating a cubic curve in Hermite form. Assumes @p t is in range
  608. * [0, @p length]. Tangents must not be scaled by @p length.
  609. *
  610. * @param[in] pointA Starting point (at t=0).
  611. * @param[in] pointB Ending point (at t=length).
  612. * @param[in] tangentA Starting tangent (at t=0).
  613. * @param[in] tangentB Ending tangent (at t=length).
  614. * @param[in] length Maximum value the curve will be evaluated at.
  615. * @param[out] coefficients Four coefficients for the cubic curve, in order [t^3, t^2, t, 1].
  616. */
  617. template<class T>
  618. static void cubicHermiteCoefficients(const T& pointA, const T& pointB, const T& tangentA, const T& tangentB,
  619. float length, T (&coefficients)[4])
  620. {
  621. float length2 = length * length;
  622. float invLength2 = 1.0f / length2;
  623. float invLength3 = 1.0f / (length2 * length);
  624. T scaledTangentA = tangentA * length;
  625. T scaledTangentB = tangentB * length;
  626. T diff = pointA - pointB;
  627. coefficients[0] = (2 * diff + scaledTangentA + scaledTangentB) * invLength3;
  628. coefficients[1] = (-3 * diff - 2 * scaledTangentA - scaledTangentB) * invLength2;
  629. coefficients[2] = tangentA;
  630. coefficients[3] = pointA;
  631. }
  632. static constexpr float POS_INFINITY = std::numeric_limits<float>::infinity();
  633. static constexpr float NEG_INFINITY = -std::numeric_limits<float>::infinity();
  634. static constexpr float PI = 3.14159265358979323846f;
  635. static constexpr float TWO_PI = (float)(2.0f * PI);
  636. static constexpr float HALF_PI = (float)(0.5f * PI);
  637. static constexpr float DEG2RAD = PI / 180.0f;
  638. static constexpr float RAD2DEG = 180.0f / PI;
  639. static const float LOG2;
  640. };
  641. /** @} */
  642. }