BsMath.h 23 KB

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