BsMath.h 21 KB

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