BsMath.h 22 KB

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