Curve.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. // Purposely not including Base.h here, or any other gameplay dependencies
  2. // so this class can be reused between gameplay and gameplay-encoder.
  3. #include "Curve.h"
  4. #include "Quaternion.h"
  5. #include <cassert>
  6. #include <cmath>
  7. #include <memory>
  8. using std::memcpy;
  9. using std::fabs;
  10. using std::sqrt;
  11. using std::cos;
  12. using std::sin;
  13. using std::exp;
  14. using std::strcmp;
  15. #ifndef NULL
  16. #define NULL 0
  17. #endif
  18. #ifndef MATH_PI
  19. #define MATH_PI 3.14159265358979323846f
  20. #endif
  21. #ifndef MATH_PIOVER2
  22. #define MATH_PIOVER2 1.57079632679489661923f
  23. #endif
  24. #ifndef MATH_PIX2
  25. #define MATH_PIX2 6.28318530717958647693f
  26. #endif
  27. // Object deletion macro
  28. #ifndef SAFE_DELETE
  29. #define SAFE_DELETE(x) \
  30. if (x) \
  31. { \
  32. delete x; \
  33. x = NULL; \
  34. }
  35. #endif
  36. // Array deletion macro
  37. #ifndef SAFE_DELETE_ARRAY
  38. #define SAFE_DELETE_ARRAY(x) \
  39. if (x) \
  40. { \
  41. delete[] x; \
  42. x = NULL; \
  43. }
  44. #endif
  45. static inline float bezier(float eq0, float eq1, float eq2, float eq3, float from, float out, float to, float in)
  46. {
  47. return from * eq0 + out * eq1 + in * eq2 + to * eq3;
  48. }
  49. static inline float bspline(float eq0, float eq1, float eq2, float eq3, float c0, float c1, float c2, float c3)
  50. {
  51. return c0 * eq0 + c1 * eq1 + c2 * eq2 + c3 * eq3;
  52. }
  53. static inline float hermite(float h00, float h01, float h10, float h11, float from, float out, float to, float in)
  54. {
  55. return h00 * from + h01 * to + h10 * out + h11 * in;
  56. }
  57. static inline float hermiteFlat(float h00, float h01, float from, float to)
  58. {
  59. return h00 * from + h01 * to;
  60. }
  61. static inline float hermiteSmooth(float h00, float h01, float h10, float h11, float from, float out, float to, float in)
  62. {
  63. return h00 * from + h01 * to + h10 * out + h11 * in;
  64. }
  65. static inline float lerpInl(float s, float from, float to)
  66. {
  67. return from + (to - from) * s;
  68. }
  69. namespace gameplay
  70. {
  71. Curve* Curve::create(unsigned int pointCount, unsigned int componentCount)
  72. {
  73. return new Curve(pointCount, componentCount);
  74. }
  75. Curve::Curve(unsigned int pointCount, unsigned int componentCount)
  76. : _pointCount(pointCount), _componentCount(componentCount), _componentSize(sizeof(float)*componentCount), _quaternionOffset(NULL), _points(NULL)
  77. {
  78. _points = new Point[_pointCount];
  79. for (unsigned int i = 0; i < _pointCount; i++)
  80. {
  81. _points[i].time = 0.0f;
  82. _points[i].value = new float[_componentCount];
  83. _points[i].inValue = new float[_componentCount];
  84. _points[i].outValue = new float[_componentCount];
  85. _points[i].type = LINEAR;
  86. }
  87. _points[_pointCount - 1].time = 1.0f;
  88. }
  89. Curve::~Curve()
  90. {
  91. SAFE_DELETE_ARRAY(_points);
  92. SAFE_DELETE_ARRAY(_quaternionOffset);
  93. }
  94. Curve::Point::Point()
  95. : time(0.0f), value(NULL), inValue(NULL), outValue(NULL)
  96. {
  97. }
  98. Curve::Point::~Point()
  99. {
  100. SAFE_DELETE_ARRAY(value);
  101. SAFE_DELETE_ARRAY(inValue);
  102. SAFE_DELETE_ARRAY(outValue);
  103. }
  104. unsigned int Curve::getPointCount() const
  105. {
  106. return _pointCount;
  107. }
  108. unsigned int Curve::getComponentCount() const
  109. {
  110. return _componentCount;
  111. }
  112. float Curve::getStartTime() const
  113. {
  114. return _points[0].time;
  115. }
  116. float Curve::getEndTime() const
  117. {
  118. return _points[_pointCount-1].time;
  119. }
  120. void Curve::setPoint(unsigned int index, float time, float* value, InterpolationType type)
  121. {
  122. setPoint(index, time, value, type, NULL, NULL);
  123. }
  124. void Curve::setPoint(unsigned int index, float time, float* value, InterpolationType type, float* inValue, float* outValue)
  125. {
  126. assert(index < _pointCount && time >= 0.0f && time <= 1.0f && !(_pointCount > 1 && index == 0 && time != 0.0f) && !(_pointCount != 1 && index == _pointCount - 1 && time != 1.0f));
  127. _points[index].time = time;
  128. _points[index].type = type;
  129. if (value)
  130. memcpy(_points[index].value, value, _componentSize);
  131. if (inValue)
  132. memcpy(_points[index].inValue, inValue, _componentSize);
  133. if (outValue)
  134. memcpy(_points[index].outValue, outValue, _componentSize);
  135. }
  136. void Curve::setTangent(unsigned int index, InterpolationType type, float* inValue, float* outValue)
  137. {
  138. assert(index < _pointCount);
  139. _points[index].type = type;
  140. if (inValue)
  141. memcpy(_points[index].inValue, inValue, _componentSize);
  142. if (outValue)
  143. memcpy(_points[index].outValue, outValue, _componentSize);
  144. }
  145. void Curve::evaluate(float time, float* dst) const
  146. {
  147. assert(dst && time >= 0 && time <= 1.0f);
  148. // Check if the point count is 1.
  149. // Check if we are at or beyond the bounds of the curve.
  150. if (_pointCount == 1 || time <= _points[0].time)
  151. {
  152. memcpy(dst, _points[0].value, _componentSize);
  153. return;
  154. }
  155. else if (time >= _points[_pointCount - 1].time)
  156. {
  157. memcpy(dst, _points[_pointCount - 1].value, _componentSize);
  158. return;
  159. }
  160. // Locate the points we are interpolating between using a binary search.
  161. unsigned int index = determineIndex(time);
  162. Point* from = _points + index;
  163. Point* to = _points + (index + 1);
  164. // Calculate the fractional time between the two points.
  165. float scale = (to->time - from->time);
  166. float t = (time - from->time) / scale;
  167. // Calculate the value of the curve discretely if appropriate.
  168. switch (from->type)
  169. {
  170. case BEZIER:
  171. {
  172. interpolateBezier(t, from, to, dst);
  173. return;
  174. }
  175. case BSPLINE:
  176. {
  177. Point* c0;
  178. Point* c1;
  179. if (index == 0)
  180. {
  181. c0 = from;
  182. }
  183. else
  184. {
  185. c0 = (_points + index - 1);
  186. }
  187. if (index == _pointCount - 2)
  188. {
  189. c1 = to;
  190. }
  191. else
  192. {
  193. c1 = (_points + index + 2);
  194. }
  195. interpolateBSpline(t, c0, from, to, c1, dst);
  196. return;
  197. }
  198. case FLAT:
  199. {
  200. interpolateHermiteFlat(t, from, to, dst);
  201. return;
  202. }
  203. case HERMITE:
  204. {
  205. interpolateHermite(t, from, to, dst);
  206. return;
  207. }
  208. case LINEAR:
  209. {
  210. // Can just break here because linear formula follows switch
  211. break;
  212. }
  213. case SMOOTH:
  214. {
  215. interpolateHermiteSmooth(t, index, from, to, dst);
  216. return;
  217. }
  218. case STEP:
  219. {
  220. memcpy(dst, from->value, _componentSize);
  221. return;
  222. }
  223. case QUADRATIC_IN:
  224. {
  225. t *= t;
  226. break;
  227. }
  228. case QUADRATIC_OUT:
  229. {
  230. t *= -(t - 2.0f);
  231. break;
  232. }
  233. case QUADRATIC_IN_OUT:
  234. {
  235. float tx2 = t * 2.0f;
  236. if (tx2 < 1.0f)
  237. t = 0.5f * (tx2 * tx2);
  238. else
  239. {
  240. float temp = tx2 - 1.0f;
  241. t = 0.5f * (-( temp * (temp - 2.0f)) + 1.0f);
  242. }
  243. break;
  244. }
  245. case QUADRATIC_OUT_IN:
  246. {
  247. if (t < 0.5f)
  248. {
  249. t = 2.0f * t * (1.0f - t);
  250. }
  251. else
  252. {
  253. t = 1.0f + 2.0f * t * (t - 1.0f);
  254. }
  255. break;
  256. }
  257. case CUBIC_IN:
  258. {
  259. t *= t * t;
  260. break;
  261. }
  262. case CUBIC_OUT:
  263. {
  264. t--;
  265. t = t * t * t + 1;
  266. break;
  267. }
  268. case CUBIC_IN_OUT:
  269. {
  270. if ((t *= 2.0f) < 1.0f)
  271. {
  272. t = t * t * t * 0.5f;
  273. }
  274. else
  275. {
  276. t -= 2.0f;
  277. t = (t * t * t + 2.0f) * 0.5f;
  278. }
  279. break;
  280. }
  281. case CUBIC_OUT_IN:
  282. {
  283. t = (2.0f * t - 1.0f);
  284. t = (t * t * t + 1) * 0.5f;
  285. break;
  286. }
  287. case QUARTIC_IN:
  288. {
  289. t *= t * t * t;
  290. break;
  291. }
  292. case QUARTIC_OUT:
  293. {
  294. t--;
  295. t = -(t * t * t * t) + 1.0f;
  296. break;
  297. }
  298. case QUARTIC_IN_OUT:
  299. {
  300. t *= 2.0f;
  301. if (t < 1.0f)
  302. {
  303. t = 0.5f * t * t * t * t;
  304. }
  305. else
  306. {
  307. t -= 2.0f;
  308. t = -0.5f * (t * t * t * t - 2.0f);
  309. }
  310. break;
  311. }
  312. case QUARTIC_OUT_IN:
  313. {
  314. t = 2.0f * t - 1.0f;
  315. if (t < 0.0f)
  316. {
  317. t = 0.5f * (-(t * t) * t * t + 1.0f);
  318. }
  319. else
  320. {
  321. t = 0.5f * (t * t * t * t + 1.0f);
  322. }
  323. break;
  324. }
  325. case QUINTIC_IN:
  326. {
  327. t *= t * t * t * t;
  328. break;
  329. }
  330. case QUINTIC_OUT:
  331. {
  332. t--;
  333. t = t * t * t * t * t + 1.0f;
  334. break;
  335. }
  336. case QUINTIC_IN_OUT:
  337. {
  338. t *= 2.0f;
  339. if (t < 1.0f)
  340. {
  341. t = 0.5f * t * t * t * t * t;
  342. }
  343. else
  344. {
  345. t -= 2.0f;
  346. t = 0.5f * (t * t * t * t * t + 2.0f);
  347. }
  348. break;
  349. }
  350. case QUINTIC_OUT_IN:
  351. {
  352. t = 2.0f * t - 1.0f;
  353. t = 0.5f * (t * t * t * t * t + 1.0f);
  354. break;
  355. }
  356. case SINE_IN:
  357. {
  358. t = -(cos(t * MATH_PIOVER2) - 1.0f);
  359. break;
  360. }
  361. case SINE_OUT:
  362. {
  363. t = sin(t * MATH_PIOVER2);
  364. break;
  365. }
  366. case SINE_IN_OUT:
  367. {
  368. t = -0.5f * (cos(MATH_PI * t) - 1.0f);
  369. break;
  370. }
  371. case SINE_OUT_IN:
  372. {
  373. if (t < 0.5f)
  374. {
  375. t = 0.5f * sin(MATH_PI * t);
  376. }
  377. else
  378. {
  379. t = -0.5f * cos(MATH_PIOVER2 * (2.0f * t - 1.0f)) + 1.0f;
  380. }
  381. break;
  382. }
  383. case EXPONENTIAL_IN:
  384. {
  385. if (t != 0.0f)
  386. {
  387. t = exp(10.0f * (t - 1.0f));
  388. }
  389. break;
  390. }
  391. case EXPONENTIAL_OUT:
  392. {
  393. if (t != 1.0f)
  394. {
  395. t = -exp(-10.0f * t) + 1.0f;
  396. }
  397. break;
  398. }
  399. case EXPONENTIAL_IN_OUT:
  400. {
  401. if (t != 0.0f && t != 1.0f)
  402. {
  403. if (t < 0.5f)
  404. {
  405. t = 0.5f * exp(10.0f * (2.0f * t - 1.0f));
  406. }
  407. else
  408. {
  409. t = -0.5f * exp(10.0f * (-2.0f * t + 1.0f)) + 1.0f;
  410. }
  411. }
  412. break;
  413. }
  414. case EXPONENTIAL_OUT_IN:
  415. {
  416. if (t != 0.0f && t != 1.0f)
  417. {
  418. if (t < 0.5f)
  419. {
  420. t = -0.5f * exp(-20.0f * t) + 0.5f;
  421. }
  422. else
  423. {
  424. t = 0.5f * exp(20.0f * (t - 1.0f)) + 0.5f;
  425. }
  426. }
  427. break;
  428. }
  429. case CIRCULAR_IN:
  430. {
  431. t = -(sqrt(1.0f - t * t) - 1.0f);
  432. break;
  433. }
  434. case CIRCULAR_OUT:
  435. {
  436. t--;
  437. t = sqrt(1.0f - t * t);
  438. break;
  439. }
  440. case CIRCULAR_IN_OUT:
  441. {
  442. t *= 2.0f;
  443. if (t < 1.0f)
  444. {
  445. t = 0.5f * (-sqrt((1.0f - t * t)) + 1.0f);
  446. }
  447. else
  448. {
  449. t -= 2.0f;
  450. t = 0.5f * (sqrt((1.0f - t * t)) + 1.0f);
  451. }
  452. break;
  453. }
  454. case CIRCULAR_OUT_IN:
  455. {
  456. t = 2.0f * t - 1.0f;
  457. if (t < 0.0f)
  458. {
  459. t = 0.5f * sqrt(1.0f - t * t);
  460. }
  461. else
  462. {
  463. t = 0.5f * (2.0f - sqrt(1.0f - t * t));
  464. }
  465. break;
  466. }
  467. case ELASTIC_IN:
  468. {
  469. if (t != 0.0f && t != 1.0f)
  470. {
  471. t = t - 1.0f;
  472. t = -1.0f * ( exp(10.0f * t) * sin( (t - 0.075f) * MATH_PIX2 / 0.3f ) );
  473. }
  474. break;
  475. }
  476. case ELASTIC_OUT:
  477. {
  478. if (t != 0.0f && t != 1.0f)
  479. {
  480. t = exp(-10.0f * t) * sin((t - 0.075f) * MATH_PIX2 / 0.3f) + 1.0f;
  481. }
  482. break;
  483. }
  484. case ELASTIC_IN_OUT:
  485. {
  486. if (t != 0.0f && t != 1.0f)
  487. {
  488. t = 2.0f * t - 1.0f;
  489. if (t < 0.0f)
  490. {
  491. t = -0.5f * (exp((10 * t)) * sin(((t - 0.1125f) * MATH_PIX2 / 0.45f)));
  492. }
  493. else
  494. {
  495. t = 0.5f * exp((-10 * t)) * sin(((t - 0.1125f) * MATH_PIX2 / 0.45f)) + 1.0f;
  496. }
  497. }
  498. break;
  499. }
  500. case ELASTIC_OUT_IN:
  501. {
  502. if (t != 0.0f && t != 1.0f)
  503. {
  504. t *= 2.0f;
  505. if (t < 1.0f)
  506. {
  507. t = 0.5f * (exp((-10 * t)) * sin(((t - 0.1125f) * (MATH_PIX2) / 0.45f))) + 0.5f;
  508. }
  509. else
  510. {
  511. t = 0.5f * (exp((10 *(t - 2))) * sin(((t - 0.1125f) * (MATH_PIX2) / 0.45f))) + 0.5f;
  512. }
  513. }
  514. break;
  515. }
  516. case OVERSHOOT_IN:
  517. {
  518. t = t * t * (2.70158f * t - 1.70158f);
  519. break;
  520. }
  521. case OVERSHOOT_OUT:
  522. {
  523. t--;
  524. t = t * t * (2.70158f * t + 1.70158f) + 1;
  525. break;
  526. }
  527. case OVERSHOOT_IN_OUT:
  528. {
  529. t *= 2.0f;
  530. if (t < 1.0f)
  531. {
  532. t = 0.5f * t * t * (3.5949095f * t - 2.5949095f);
  533. }
  534. else
  535. {
  536. t -= 2.0f;
  537. t = 0.5f * (t * t * (3.5949095f * t + 2.5949095f) + 2.0f);
  538. }
  539. break;
  540. }
  541. case OVERSHOOT_OUT_IN:
  542. {
  543. t = 2.0f * t - 1.0f;
  544. if (t < 0.0f)
  545. {
  546. t = 0.5f * (t * t * (3.5949095f * t + 2.5949095f) + 1.0f);
  547. }
  548. else
  549. {
  550. t = 0.5f * (t * t * (3.5949095f * t - 2.5949095f) + 1.0f);
  551. }
  552. break;
  553. }
  554. case BOUNCE_IN:
  555. {
  556. t = 1.0f - t;
  557. if (t < 0.36363636363636365f)
  558. {
  559. t = 7.5625f * t * t;
  560. }
  561. else if (t < 0.7272727272727273f)
  562. {
  563. t -= 0.5454545454545454f;
  564. t = 7.5625f * t * t + 0.75f;
  565. }
  566. else if (t < 0.9090909090909091f)
  567. {
  568. t -= 0.8181818181818182f;
  569. t = 7.5625f * t * t + 0.9375f;
  570. }
  571. else
  572. {
  573. t -= 0.9545454545454546f;
  574. t = 7.5625f * t * t + 0.984375f;
  575. }
  576. t = 1.0f - t;
  577. break;
  578. }
  579. case BOUNCE_OUT:
  580. {
  581. if (t < 0.36363636363636365f)
  582. {
  583. t = 7.5625f * t * t;
  584. }
  585. else if (t < 0.7272727272727273f)
  586. {
  587. t -= 0.5454545454545454f;
  588. t = 7.5625f * t * t + 0.75f;
  589. }
  590. else if (t < 0.9090909090909091f)
  591. {
  592. t -= 0.8181818181818182f;
  593. t = 7.5625f * t * t + 0.9375f;
  594. }
  595. else
  596. {
  597. t -= 0.9545454545454546f;
  598. t = 7.5625f * t * t + 0.984375f;
  599. }
  600. break;
  601. }
  602. case BOUNCE_IN_OUT:
  603. {
  604. if (t < 0.5f)
  605. {
  606. t = 1.0f - t * 2.0f;
  607. if (t < 0.36363636363636365f)
  608. {
  609. t = 7.5625f * t * t;
  610. }
  611. else if (t < 0.7272727272727273f)
  612. {
  613. t -= 0.5454545454545454f;
  614. t = 7.5625f * t * t + 0.75f;
  615. }
  616. else if (t < 0.9090909090909091f)
  617. {
  618. t -= 0.8181818181818182f;
  619. t = 7.5625f * t * t + 0.9375f;
  620. }
  621. else
  622. {
  623. t -= 0.9545454545454546f;
  624. t = 7.5625f * t * t + 0.984375f;
  625. }
  626. t = (1.0f - t) * 0.5f;
  627. }
  628. else
  629. {
  630. t = t * 2.0f - 1.0f;
  631. if (t < 0.36363636363636365f)
  632. {
  633. t = 7.5625f * t * t;
  634. }
  635. else if (t < 0.7272727272727273f)
  636. {
  637. t -= 0.5454545454545454f;
  638. t = 7.5625f * t * t + 0.75f;
  639. }
  640. else if (t < 0.9090909090909091f)
  641. {
  642. t -= 0.8181818181818182f;
  643. t = 7.5625f * t * t + 0.9375f;
  644. }
  645. else
  646. {
  647. t -= 0.9545454545454546f;
  648. t = 7.5625f * t * t + 0.984375f;
  649. }
  650. t = 0.5f * t + 0.5f;
  651. }
  652. break;
  653. }
  654. case BOUNCE_OUT_IN:
  655. {
  656. if (t < 0.1818181818f)
  657. {
  658. t = 15.125f * t * t;
  659. }
  660. else if (t < 0.3636363636f)
  661. {
  662. t = 1.5f + (-8.250000001f + 15.125f * t) * t;
  663. }
  664. else if (t < 0.4545454546f)
  665. {
  666. t = 3.0f + (-12.375f + 15.125f * t) * t;
  667. }
  668. else if (t < 0.5f)
  669. {
  670. t = 3.9375f + (-14.4375f + 15.125f * t) * t;
  671. }
  672. else if (t <= 0.5454545455f)
  673. {
  674. t = -3.625000004f + (15.81250001f - 15.125f * t) * t;
  675. }
  676. else if (t <= 0.6363636365f)
  677. {
  678. t = -4.75f + (17.875f - 15.125f * t) * t;
  679. }
  680. else if (t <= 0.8181818180f)
  681. {
  682. t = -7.374999995f + (21.99999999f - 15.125f * t) * t;
  683. }
  684. else
  685. {
  686. t = -14.125f + (30.25f - 15.125f * t) * t;
  687. }
  688. break;
  689. }
  690. }
  691. interpolateLinear(t, from, to, dst);
  692. }
  693. float Curve::lerp(float t, float from, float to)
  694. {
  695. return lerpInl(t, from, to);
  696. }
  697. void Curve::setQuaternionOffset(unsigned int offset)
  698. {
  699. assert(offset <= (_componentCount - 4));
  700. if (!_quaternionOffset)
  701. _quaternionOffset = new unsigned int[1];
  702. *_quaternionOffset = offset;
  703. }
  704. void Curve::interpolateBezier(float s, Point* from, Point* to, float* dst) const
  705. {
  706. float s_2 = s * s;
  707. float eq0 = 1 - s;
  708. float eq0_2 = eq0 * eq0;
  709. float eq1 = eq0_2 * eq0;
  710. float eq2 = 3 * s * eq0_2;
  711. float eq3 = 3 * s_2 * eq0;
  712. float eq4 = s_2 * s;
  713. float* fromValue = from->value;
  714. float* toValue = to->value;
  715. float* outValue = from->outValue;
  716. float* inValue = to->inValue;
  717. if (!_quaternionOffset)
  718. {
  719. for (unsigned int i = 0; i < _componentCount; i++)
  720. {
  721. if (fromValue[i] == toValue[i])
  722. dst[i] = fromValue[i];
  723. else
  724. dst[i] = bezier(eq1, eq2, eq3, eq4, fromValue[i], outValue[i], toValue[i], inValue[i]);
  725. }
  726. }
  727. else
  728. {
  729. // Interpolate any values up to the quaternion offset as scalars.
  730. unsigned int quaternionOffset = *_quaternionOffset;
  731. unsigned int i = 0;
  732. for (i = 0; i < quaternionOffset; i++)
  733. {
  734. if (fromValue[i] == toValue[i])
  735. dst[i] = fromValue[i];
  736. else
  737. dst[i] = bezier(eq1, eq2, eq3, eq4, fromValue[i], outValue[i], toValue[i], inValue[i]);
  738. }
  739. // Handle quaternion component.
  740. float interpTime = bezier(eq1, eq2, eq3, eq4, from->time, outValue[i], to->time, inValue[i]);
  741. interpolateQuaternion(interpTime, (fromValue + i), (toValue + i), (dst + i));
  742. // Handle remaining components (if any) as scalars
  743. for (i += 4; i < _componentCount; i++)
  744. {
  745. if (fromValue[i] == toValue[i])
  746. dst[i] = fromValue[i];
  747. else
  748. dst[i] = bezier(eq1, eq2, eq3, eq4, fromValue[i], outValue[i], toValue[i], inValue[i]);
  749. }
  750. }
  751. }
  752. void Curve::interpolateBSpline(float s, Point* c0, Point* c1, Point* c2, Point* c3, float* dst) const
  753. {
  754. float s_2 = s * s;
  755. float s_3 = s_2 * s;
  756. float eq0 = (-s_3 + 3 * s_2 - 3 * s + 1) / 6.0f;
  757. float eq1 = (3 * s_3 - 6 * s_2 + 4) / 6.0f;
  758. float eq2 = (-3 * s_3 + 3 * s_2 + 3 * s + 1) / 6.0f;
  759. float eq3 = s_3 / 6.0f;
  760. float* c0Value = c0->value;
  761. float* c1Value = c1->value;
  762. float* c2Value = c2->value;
  763. float* c3Value = c3->value;
  764. if (!_quaternionOffset)
  765. {
  766. for (unsigned int i = 0; i < _componentCount; i++)
  767. {
  768. if (c1Value[i] == c2Value[i])
  769. dst[i] = c1Value[i];
  770. else
  771. dst[i] = bspline(eq0, eq1, eq2, eq3, c0Value[i], c1Value[i], c2Value[i], c3Value[i]);
  772. }
  773. }
  774. else
  775. {
  776. // Interpolate any values up to the quaternion offset as scalars.
  777. unsigned int quaternionOffset = *_quaternionOffset;
  778. unsigned int i = 0;
  779. for (i = 0; i < quaternionOffset; i++)
  780. {
  781. if (c1Value[i] == c2Value[i])
  782. dst[i] = c1Value[i];
  783. else
  784. dst[i] = bspline(eq0, eq1, eq2, eq3, c0Value[i], c1Value[i], c2Value[i], c3Value[i]);
  785. }
  786. // Handle quaternion component.
  787. float interpTime;
  788. if (c0->time == c1->time)
  789. interpTime = bspline(eq0, eq1, eq2, eq3, -c0->time, c1->time, c2->time, c3->time);
  790. else if (c2->time == c3->time)
  791. interpTime = bspline(eq0, eq1, eq2, eq3, c0->time, c1->time, c2->time, -c3->time);
  792. else
  793. interpTime = bspline(eq0, eq1, eq2, eq3, c0->time, c1->time, c2->time, c3->time);
  794. interpolateQuaternion(s, (c1Value + i) , (c2Value + i), (dst + i));
  795. // Handle remaining components (if any) as scalars
  796. for (i += 4; i < _componentCount; i++)
  797. {
  798. if (c1Value[i] == c2Value[i])
  799. dst[i] = c1Value[i];
  800. else
  801. dst[i] = bspline(eq0, eq1, eq2, eq3, c0Value[i], c1Value[i], c2Value[i], c3Value[i]);
  802. }
  803. }
  804. }
  805. void Curve::interpolateHermite(float s, Point* from, Point* to, float* dst) const
  806. {
  807. // Calculate the hermite basis functions.
  808. float s_2 = s * s; // t^2
  809. float s_3 = s_2 * s; // t^3
  810. float h00 = 2 * s_3 - 3 * s_2 + 1; // basis function 0
  811. float h01 = -2 * s_3 + 3 * s_2; // basis function 1
  812. float h10 = s_3 - 2 * s_2 + s; // basis function 2
  813. float h11 = s_3 - s_2; // basis function 3
  814. float* fromValue = from->value;
  815. float* toValue = to->value;
  816. float* outValue = from->outValue;
  817. float* inValue = to->inValue;
  818. if (!_quaternionOffset)
  819. {
  820. for (unsigned int i = 0; i < _componentCount; i++)
  821. {
  822. if (fromValue[i] == toValue[i])
  823. dst[i] = fromValue[i];
  824. else
  825. dst[i] = hermite(h00, h01, h10, h11, fromValue[i], outValue[i], toValue[i], inValue[i]);
  826. }
  827. }
  828. else
  829. {
  830. // Interpolate any values up to the quaternion offset as scalars.
  831. unsigned int quaternionOffset = *_quaternionOffset;
  832. unsigned int i = 0;
  833. for (i = 0; i < quaternionOffset; i++)
  834. {
  835. if (fromValue[i] == toValue[i])
  836. dst[i] = fromValue[i];
  837. else
  838. dst[i] = hermite(h00, h01, h10, h11, fromValue[i], outValue[i], toValue[i], inValue[i]);
  839. }
  840. // Handle quaternion component.
  841. float interpTime = hermite(h00, h01, h10, h11, from->time, outValue[i], to->time, inValue[i]);
  842. interpolateQuaternion(interpTime, (fromValue + i), (toValue + i), (dst + i));
  843. // Handle remaining components (if any) as scalars
  844. for (i += 4; i < _componentCount; i++)
  845. {
  846. if (fromValue[i] == toValue[i])
  847. dst[i] = fromValue[i];
  848. else
  849. dst[i] = hermite(h00, h01, h10, h11, fromValue[i], outValue[i], toValue[i], inValue[i]);
  850. }
  851. }
  852. }
  853. void Curve::interpolateHermiteFlat(float s, Point* from, Point* to, float* dst) const
  854. {
  855. // Calculate the hermite basis functions.
  856. float s_2 = s * s; // t^2
  857. float s_3 = s_2 * s; // t^3
  858. float h00 = 2 * s_3 - 3 * s_2 + 1; // basis function 0
  859. float h01 = -2 * s_3 + 3 * s_2; // basis function 1
  860. float* fromValue = from->value;
  861. float* toValue = to->value;
  862. if (!_quaternionOffset)
  863. {
  864. for (unsigned int i = 0; i < _componentCount; i++)
  865. {
  866. if (fromValue[i] == toValue[i])
  867. dst[i] = fromValue[i];
  868. else
  869. dst[i] = hermiteFlat(h00, h01, fromValue[i], toValue[i]);
  870. }
  871. }
  872. else
  873. {
  874. // Interpolate any values up to the quaternion offset as scalars.
  875. unsigned int quaternionOffset = *_quaternionOffset;
  876. unsigned int i = 0;
  877. for (i = 0; i < quaternionOffset; i++)
  878. {
  879. if (fromValue[i] == toValue[i])
  880. dst[i] = fromValue[i];
  881. else
  882. dst[i] = hermiteFlat(h00, h01, fromValue[i], toValue[i]);
  883. }
  884. // Handle quaternion component.
  885. float interpTime = hermiteFlat(h00, h01, from->time, to->time);
  886. interpolateQuaternion(interpTime, (fromValue + i), (toValue + i), (dst + i));
  887. // Handle remaining components (if any) as scalars
  888. for (i += 4; i < _componentCount; i++)
  889. {
  890. if (fromValue[i] == toValue[i])
  891. dst[i] = fromValue[i];
  892. else
  893. dst[i] = hermiteFlat(h00, h01, fromValue[i], toValue[i]);
  894. }
  895. }
  896. }
  897. void Curve::interpolateHermiteSmooth(float s, unsigned int index, Point* from, Point* to, float* dst) const
  898. {
  899. // Calculate the hermite basis functions.
  900. float s_2 = s * s; // t^2
  901. float s_3 = s_2 * s; // t^3
  902. float h00 = 2 * s_3 - 3 * s_2 + 1; // basis function 0
  903. float h01 = -2 * s_3 + 3 * s_2; // basis function 1
  904. float h10 = s_3 - 2 * s_2 + s; // basis function 2
  905. float h11 = s_3 - s_2; // basis function 3
  906. float inValue;
  907. float outValue;
  908. float* fromValue = from->value;
  909. float* toValue = to->value;
  910. if (!_quaternionOffset)
  911. {
  912. for (unsigned int i = 0; i < _componentCount; i++)
  913. {
  914. if (fromValue[i] == toValue[i])
  915. {
  916. dst[i] = fromValue[i];
  917. }
  918. else
  919. {
  920. if (index == 0)
  921. {
  922. outValue = toValue[i] - fromValue[i];
  923. }
  924. else
  925. {
  926. outValue = (toValue[i] - (from - 1)->value[i]) * ((from->time - (from - 1)->time) / (to->time - (from - 1)->time));
  927. }
  928. if (index == _pointCount - 2)
  929. {
  930. inValue = toValue[i] - fromValue[i];
  931. }
  932. else
  933. {
  934. inValue = ((to + 1)->value[i] - fromValue[i]) * ((to->time - from->time) / ((to + 1)->time - from->time));
  935. }
  936. dst[i] = hermiteSmooth(h00, h01, h10, h11, fromValue[i], outValue, toValue[i], inValue);
  937. }
  938. }
  939. }
  940. else
  941. {
  942. // Interpolate any values up to the quaternion offset as scalars.
  943. unsigned int quaternionOffset = *_quaternionOffset;
  944. unsigned int i = 0;
  945. for (i = 0; i < quaternionOffset; i++)
  946. {
  947. if (fromValue[i] == toValue[i])
  948. {
  949. dst[i] = fromValue[i];
  950. }
  951. else
  952. {
  953. if (index == 0)
  954. {
  955. outValue = toValue[i] - fromValue[i];
  956. }
  957. else
  958. {
  959. outValue = (toValue[i] - (from - 1)->value[i]) * ((from->time - (from - 1)->time) / (to->time - (from - 1)->time));
  960. }
  961. if (index == _pointCount - 2)
  962. {
  963. inValue = toValue[i] - fromValue[i];
  964. }
  965. else
  966. {
  967. inValue = ((to + 1)->value[i] - fromValue[i]) * ((to->time - from->time) / ((to + 1)->time - from->time));
  968. }
  969. dst[i] = hermiteSmooth(h00, h01, h10, h11, fromValue[i], outValue, toValue[i], inValue);
  970. }
  971. }
  972. // Handle quaternion component.
  973. if (index == 0)
  974. {
  975. outValue = to->time - from->time;
  976. }
  977. else
  978. {
  979. outValue = (to->time - (from - 1)->time) * ((from->time - (from - 1)->time) / (to->time - (from - 1)->time));
  980. }
  981. if (index == _pointCount - 2)
  982. {
  983. inValue = to->time - from->time;
  984. }
  985. else
  986. {
  987. inValue = ((to + 1)->time - from->time) * ((to->time - from->time) / ((to + 1)->time - from->time));
  988. }
  989. float interpTime = hermiteSmooth(h00, h01, h10, h11, from->time, outValue, to->time, inValue);
  990. interpolateQuaternion(interpTime, (fromValue + i), (toValue + i), (dst + i));
  991. // Handle remaining components (if any) as scalars
  992. for (i += 4; i < _componentCount; i++)
  993. {
  994. if (fromValue[i] == toValue[i])
  995. {
  996. dst[i] = fromValue[i];
  997. }
  998. else
  999. {
  1000. // Interpolate as scalar.
  1001. if (index == 0)
  1002. {
  1003. outValue = toValue[i] - fromValue[i];
  1004. }
  1005. else
  1006. {
  1007. outValue = (toValue[i] - (from - 1)->value[i]) * ((from->time - (from - 1)->time) / (to->time - (from - 1)->time));
  1008. }
  1009. if (index == _pointCount - 2)
  1010. {
  1011. inValue = toValue[i] - fromValue[i];
  1012. }
  1013. else
  1014. {
  1015. inValue = ((to + 1)->value[i] - fromValue[i]) * ((to->time - from->time) / ((to + 1)->time - from->time));
  1016. }
  1017. dst[i] = hermiteSmooth(h00, h01, h10, h11, fromValue[i], outValue, toValue[i], inValue);
  1018. }
  1019. }
  1020. }
  1021. }
  1022. void Curve::interpolateLinear(float s, Point* from, Point* to, float* dst) const
  1023. {
  1024. float* fromValue = from->value;
  1025. float* toValue = to->value;
  1026. if (!_quaternionOffset)
  1027. {
  1028. for (unsigned int i = 0; i < _componentCount; i++)
  1029. {
  1030. if (fromValue[i] == toValue[i])
  1031. dst[i] = fromValue[i];
  1032. else
  1033. dst[i] = lerpInl(s, fromValue[i], toValue[i]);
  1034. }
  1035. }
  1036. else
  1037. {
  1038. // Interpolate any values up to the quaternion offset as scalars.
  1039. unsigned int quaternionOffset = *_quaternionOffset;
  1040. unsigned int i = 0;
  1041. for (i = 0; i < quaternionOffset; i++)
  1042. {
  1043. if (fromValue[i] == toValue[i])
  1044. dst[i] = fromValue[i];
  1045. else
  1046. dst[i] = lerpInl(s, fromValue[i], toValue[i]);
  1047. }
  1048. // Handle quaternion component.
  1049. interpolateQuaternion(s, (fromValue + i), (toValue + i), (dst + i));
  1050. // handle any remaining components as scalars
  1051. for (i += 4; i < _componentCount; i++)
  1052. {
  1053. if (fromValue[i] == toValue[i])
  1054. dst[i] = fromValue[i];
  1055. else
  1056. dst[i] = lerpInl(s, fromValue[i], toValue[i]);
  1057. }
  1058. }
  1059. }
  1060. void Curve::interpolateQuaternion(float s, float* from, float* to, float* dst) const
  1061. {
  1062. // Evaluate.
  1063. if (s >= 0)
  1064. Quaternion::slerp(from[0], from[1], from[2], from[3], to[0], to[1], to[2], to[3], s, dst, dst + 1, dst + 2, dst + 3);
  1065. else
  1066. Quaternion::slerp(to[0], to[1], to[2], to[3], from[0], from[1], from[2], from[3], s, dst, dst + 1, dst + 2, dst + 3);
  1067. }
  1068. int Curve::determineIndex(float time) const
  1069. {
  1070. unsigned int min = 0;
  1071. unsigned int max = _pointCount - 1;
  1072. unsigned int mid = 0;
  1073. // Do a binary search to determine the index.
  1074. do
  1075. {
  1076. mid = (min + max) >> 1;
  1077. if (time >= _points[mid].time && time <= _points[mid + 1].time)
  1078. return mid;
  1079. else if (time < _points[mid].time)
  1080. max = mid - 1;
  1081. else
  1082. min = mid + 1;
  1083. } while (min <= max);
  1084. // We should never hit this!
  1085. return -1;
  1086. }
  1087. int Curve::getInterpolationType(const char* curveId)
  1088. {
  1089. if (strcmp(curveId, "BEZIER") == 0)
  1090. {
  1091. return Curve::BEZIER;
  1092. }
  1093. else if (strcmp(curveId, "BSPLINE") == 0)
  1094. {
  1095. return Curve::BSPLINE;
  1096. }
  1097. else if (strcmp(curveId, "FLAT") == 0)
  1098. {
  1099. return Curve::FLAT;
  1100. }
  1101. else if (strcmp(curveId, "HERMITE") == 0)
  1102. {
  1103. return Curve::HERMITE;
  1104. }
  1105. else if (strcmp(curveId, "LINEAR") == 0)
  1106. {
  1107. return Curve::LINEAR;
  1108. }
  1109. else if (strcmp(curveId, "SMOOTH") == 0)
  1110. {
  1111. return Curve::SMOOTH;
  1112. }
  1113. else if (strcmp(curveId, "STEP") == 0)
  1114. {
  1115. return Curve::STEP;
  1116. }
  1117. else if (strcmp(curveId, "QUADRATIC_IN") == 0)
  1118. {
  1119. return Curve::QUADRATIC_IN;
  1120. }
  1121. else if (strcmp(curveId, "QUADRATIC_OUT") == 0)
  1122. {
  1123. return Curve::QUADRATIC_OUT;
  1124. }
  1125. else if (strcmp(curveId, "QUADRATIC_IN_OUT") == 0)
  1126. {
  1127. return Curve::QUADRATIC_IN_OUT;
  1128. }
  1129. else if (strcmp(curveId, "QUADRATIC_OUT_IN") == 0)
  1130. {
  1131. return Curve::QUADRATIC_OUT_IN;
  1132. }
  1133. else if (strcmp(curveId, "CUBIC_IN") == 0)
  1134. {
  1135. return Curve::CUBIC_IN;
  1136. }
  1137. else if (strcmp(curveId, "CUBIC_OUT") == 0)
  1138. {
  1139. return Curve::CUBIC_OUT;
  1140. }
  1141. else if (strcmp(curveId, "CUBIC_IN_OUT") == 0)
  1142. {
  1143. return Curve::CUBIC_IN_OUT;
  1144. }
  1145. else if (strcmp(curveId, "CUBIC_OUT_IN") == 0)
  1146. {
  1147. return Curve::CUBIC_OUT_IN;
  1148. }
  1149. else if (strcmp(curveId, "QUARTIC_IN") == 0)
  1150. {
  1151. return Curve::QUARTIC_IN;
  1152. }
  1153. else if (strcmp(curveId, "QUARTIC_OUT") == 0)
  1154. {
  1155. return Curve::QUARTIC_OUT;
  1156. }
  1157. else if (strcmp(curveId, "QUARTIC_IN_OUT") == 0)
  1158. {
  1159. return Curve::QUARTIC_IN_OUT;
  1160. }
  1161. else if (strcmp(curveId, "QUARTIC_OUT_IN") == 0)
  1162. {
  1163. return Curve::QUARTIC_OUT_IN;
  1164. }
  1165. else if (strcmp(curveId, "QUINTIC_IN") == 0)
  1166. {
  1167. return Curve::QUINTIC_IN;
  1168. }
  1169. else if (strcmp(curveId, "QUINTIC_OUT") == 0)
  1170. {
  1171. return Curve::QUINTIC_OUT;
  1172. }
  1173. else if (strcmp(curveId, "QUINTIC_IN_OUT") == 0)
  1174. {
  1175. return Curve::QUINTIC_IN_OUT;
  1176. }
  1177. else if (strcmp(curveId, "QUINTIC_OUT_IN") == 0)
  1178. {
  1179. return Curve::QUINTIC_OUT_IN;
  1180. }
  1181. else if (strcmp(curveId, "SINE_IN") == 0)
  1182. {
  1183. return Curve::SINE_IN;
  1184. }
  1185. else if (strcmp(curveId, "SINE_OUT") == 0)
  1186. {
  1187. return Curve::SINE_OUT;
  1188. }
  1189. else if (strcmp(curveId, "SINE_IN_OUT") == 0)
  1190. {
  1191. return Curve::SINE_IN_OUT;
  1192. }
  1193. else if (strcmp(curveId, "SINE_OUT_IN") == 0)
  1194. {
  1195. return Curve::SINE_OUT_IN;
  1196. }
  1197. else if (strcmp(curveId, "EXPONENTIAL_IN") == 0)
  1198. {
  1199. return Curve::EXPONENTIAL_IN;
  1200. }
  1201. else if (strcmp(curveId, "EXPONENTIAL_OUT") == 0)
  1202. {
  1203. return Curve::EXPONENTIAL_OUT;
  1204. }
  1205. else if (strcmp(curveId, "EXPONENTIAL_IN_OUT") == 0)
  1206. {
  1207. return Curve::EXPONENTIAL_IN_OUT;
  1208. }
  1209. else if (strcmp(curveId, "EXPONENTIAL_OUT_IN") == 0)
  1210. {
  1211. return Curve::EXPONENTIAL_OUT_IN;
  1212. }
  1213. else if (strcmp(curveId, "CIRCULAR_IN") == 0)
  1214. {
  1215. return Curve::CIRCULAR_IN;
  1216. }
  1217. else if (strcmp(curveId, "CIRCULAR_OUT") == 0)
  1218. {
  1219. return Curve::CIRCULAR_OUT;
  1220. }
  1221. else if (strcmp(curveId, "CIRCULAR_IN_OUT") == 0)
  1222. {
  1223. return Curve::CIRCULAR_IN_OUT;
  1224. }
  1225. else if (strcmp(curveId, "CIRCULAR_OUT_IN") == 0)
  1226. {
  1227. return Curve::CIRCULAR_OUT_IN;
  1228. }
  1229. else if (strcmp(curveId, "ELASTIC_IN") == 0)
  1230. {
  1231. return Curve::ELASTIC_IN;
  1232. }
  1233. else if (strcmp(curveId, "ELASTIC_OUT") == 0)
  1234. {
  1235. return Curve::ELASTIC_OUT;
  1236. }
  1237. else if (strcmp(curveId, "ELASTIC_IN_OUT") == 0)
  1238. {
  1239. return Curve::ELASTIC_IN_OUT;
  1240. }
  1241. else if (strcmp(curveId, "ELASTIC_OUT_IN") == 0)
  1242. {
  1243. return Curve::ELASTIC_OUT_IN;
  1244. }
  1245. else if (strcmp(curveId, "OVERSHOOT_IN") == 0)
  1246. {
  1247. return Curve::OVERSHOOT_IN;
  1248. }
  1249. else if (strcmp(curveId, "OVERSHOOT_OUT") == 0)
  1250. {
  1251. return Curve::OVERSHOOT_OUT;
  1252. }
  1253. else if (strcmp(curveId, "OVERSHOOT_IN_OUT") == 0)
  1254. {
  1255. return Curve::OVERSHOOT_IN_OUT;
  1256. }
  1257. else if (strcmp(curveId, "OVERSHOOT_OUT_IN") == 0)
  1258. {
  1259. return Curve::OVERSHOOT_OUT_IN;
  1260. }
  1261. else if (strcmp(curveId, "BOUNCE_IN") == 0)
  1262. {
  1263. return Curve::BOUNCE_IN;
  1264. }
  1265. else if (strcmp(curveId, "BOUNCE_OUT") == 0)
  1266. {
  1267. return Curve::BOUNCE_OUT;
  1268. }
  1269. else if (strcmp(curveId, "BOUNCE_IN_OUT") == 0)
  1270. {
  1271. return Curve::BOUNCE_IN_OUT;
  1272. }
  1273. else if (strcmp(curveId, "BOUNCE_OUT_IN") == 0)
  1274. {
  1275. return Curve::BOUNCE_OUT_IN;
  1276. }
  1277. return -1;
  1278. }
  1279. }