EaseMotion 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /* -*-c++-*-
  2. * Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
  3. *
  4. * This library is open source and may be redistributed and/or modified under
  5. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  6. * (at your option) any later version. The full license is in LICENSE file
  7. * included with this distribution, and on the openscenegraph.org website.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * OpenSceneGraph Public License for more details.
  13. */
  14. #ifndef OSGANIMATION_EASE_MOTION
  15. #define OSGANIMATION_EASE_MOTION 1
  16. #include <osg/Referenced>
  17. #include <osg/ref_ptr>
  18. #include <osg/Notify>
  19. #include <osg/Math>
  20. #include <vector>
  21. namespace osgAnimation
  22. {
  23. struct OutBounceFunction
  24. {
  25. inline static void getValueAt(float t, float& result)
  26. {
  27. if ((t) < (1/2.75))
  28. {
  29. result = 7.5625 * t * t;
  30. }
  31. else if (t < (2/2.75))
  32. {
  33. t = t - (1.5/2.75);
  34. result = 7.5625* t * t + .75;
  35. }
  36. else if (t < (2.5/2.75))
  37. {
  38. t = t - (2.25/2.75);
  39. result = 7.5625 * t * t + .9375;
  40. }
  41. else
  42. {
  43. t = t - (2.625/2.75);
  44. result = 7.5625* t * t + .984375;
  45. }
  46. }
  47. };
  48. struct InBounceFunction
  49. {
  50. inline static void getValueAt(float t, float& result)
  51. {
  52. OutBounceFunction::getValueAt(1-t, result);
  53. result = 1 - result;
  54. }
  55. };
  56. struct InOutBounceFunction
  57. {
  58. inline static void getValueAt(float t, float& result)
  59. {
  60. if (t < 0.5)
  61. {
  62. InBounceFunction::getValueAt(t * 2, result);
  63. result *= 0.5;
  64. }
  65. else
  66. {
  67. OutBounceFunction::getValueAt(t * 2 - 1 , result);
  68. result = result * 0.5 + 0.5;
  69. }
  70. }
  71. };
  72. /// Linear function
  73. struct LinearFunction
  74. {
  75. inline static void getValueAt(float t, float& result) { result = t;}
  76. };
  77. /// Quad function
  78. struct OutQuadFunction
  79. {
  80. inline static void getValueAt(float t, float& result) { result = - (t * (t -2.0));}
  81. };
  82. struct InQuadFunction
  83. {
  84. inline static void getValueAt(float t, float& result) { result = t*t;}
  85. };
  86. struct InOutQuadFunction
  87. {
  88. inline static void getValueAt(float t, float& result)
  89. {
  90. t *= 2.0;
  91. if (t < 1.0)
  92. result = 0.5 * t * t;
  93. else
  94. {
  95. t -= 1.0;
  96. result = - 0.5 * (t * ( t - 2) - 1);
  97. }
  98. }
  99. };
  100. /// Cubic function
  101. struct OutCubicFunction
  102. {
  103. inline static void getValueAt(float t, float& result) { t = t-1.0; result = t*t*t + 1;}
  104. };
  105. struct InCubicFunction
  106. {
  107. inline static void getValueAt(float t, float& result) { result = t*t*t;}
  108. };
  109. struct InOutCubicFunction
  110. {
  111. inline static void getValueAt(float t, float& result)
  112. {
  113. t *= 2.0f;
  114. if (t < 1.0f)
  115. result = 0.5f * t * t * t;
  116. else {
  117. t -= 2.0f;
  118. result = 0.5 * (t * t * t + 2.0f);
  119. }
  120. }
  121. };
  122. /// Quart function
  123. struct InQuartFunction
  124. {
  125. inline static void getValueAt(float t, float& result) { result = t*t*t*t*t;}
  126. };
  127. struct OutQuartFunction
  128. {
  129. inline static void getValueAt(float t, float& result) { t = t - 1; result = - (t*t*t*t -1); }
  130. };
  131. struct InOutQuartFunction
  132. {
  133. inline static void getValueAt(float t, float& result)
  134. {
  135. t = t * 2.0;
  136. if ( t < 1)
  137. result = 0.5*t*t*t*t;
  138. else
  139. {
  140. t -= 2.0;
  141. result = -0.5 * (t*t*t*t -2);
  142. }
  143. }
  144. };
  145. /// Elastic function
  146. struct OutElasticFunction
  147. {
  148. inline static void getValueAt(float t, float& result)
  149. {
  150. result = pow(2.0f, -10.0f * t) * sinf((t - 0.3f / 4.0f) * (2.0f * osg::PI) / 0.3f) + 1.0f;
  151. }
  152. };
  153. struct InElasticFunction
  154. {
  155. inline static void getValueAt(float t, float& result)
  156. {
  157. OutElasticFunction::getValueAt(1.0f - t, result);
  158. result = 1.0f - result;
  159. }
  160. };
  161. struct InOutElasticFunction
  162. {
  163. inline static void getValueAt(float t, float& result)
  164. {
  165. t *= 2.0f;
  166. if (t < 1.0f)
  167. {
  168. t -= 1.0f;
  169. result = -0.5 * (1.0f * pow(2.0f, 10.0f * t) * sinf((t - 0.45f / 4.0f) * (2.0f * osg::PI) / 0.45f));
  170. }
  171. else
  172. {
  173. t -= 1.0f;
  174. result = pow(2.0f, -10.0f * t) * sinf((t - 0.45f / 4.0f) * (2.0f * osg::PI) / 0.45f) * 0.5f + 1.0f;
  175. }
  176. }
  177. };
  178. // Sine function
  179. struct OutSineFunction
  180. {
  181. inline static void getValueAt(float t, float& result)
  182. {
  183. result = sinf(t * (osg::PI / 2.0f));
  184. }
  185. };
  186. struct InSineFunction
  187. {
  188. inline static void getValueAt(float t, float& result)
  189. {
  190. result = -cosf(t * (osg::PI / 2.0f)) + 1.0f;
  191. }
  192. };
  193. struct InOutSineFunction
  194. {
  195. inline static void getValueAt(float t, float& result)
  196. {
  197. result = -0.5f * (cosf((osg::PI * t)) - 1.0f);
  198. }
  199. };
  200. // Back function
  201. struct OutBackFunction
  202. {
  203. inline static void getValueAt(float t, float& result)
  204. {
  205. t -= 1.0f;
  206. result = t * t * ((1.70158 + 1.0f) * t + 1.70158) + 1.0f;
  207. }
  208. };
  209. struct InBackFunction
  210. {
  211. inline static void getValueAt(float t, float& result)
  212. {
  213. result = t * t * ((1.70158 + 1.0f) * t - 1.70158);
  214. }
  215. };
  216. struct InOutBackFunction
  217. {
  218. inline static void getValueAt(float t, float& result)
  219. {
  220. float s = 1.70158 * 1.525f;
  221. t *= 2.0f;
  222. if (t < 1.0f)
  223. {
  224. result = 0.5f * (t * t * ((s + 1.0f) * t - s));
  225. }
  226. else
  227. {
  228. float p = t -= 2.0f;
  229. result = 0.5f * ((p) * t * ((s + 1.0f) * t + s) + 2.0f);
  230. }
  231. }
  232. };
  233. // Circ function
  234. struct OutCircFunction
  235. {
  236. inline static void getValueAt(float t, float& result)
  237. {
  238. t -= 1.0f;
  239. result = sqrt(1.0f - t * t);
  240. }
  241. };
  242. struct InCircFunction
  243. {
  244. inline static void getValueAt(float t, float& result)
  245. {
  246. result = -(sqrt(1.0f - (t * t)) - 1.0f);
  247. }
  248. };
  249. struct InOutCircFunction
  250. {
  251. inline static void getValueAt(float t, float& result)
  252. {
  253. t *= 2.0f;
  254. if (t < 1.0f)
  255. {
  256. result = -0.5f * (sqrt(1.0f - t * t) - 1.0f);
  257. }
  258. else
  259. {
  260. t -= 2.0f;
  261. result = 0.5f * (sqrt(1 - t * t) + 1.0f);
  262. }
  263. }
  264. };
  265. // Expo function
  266. struct OutExpoFunction
  267. {
  268. inline static void getValueAt(float t, float& result)
  269. {
  270. if(t == 1.0f)
  271. {
  272. result = 0.0f;
  273. }
  274. else
  275. {
  276. result = -powf(2.0f, -10.0f * t) + 1.0f;
  277. }
  278. }
  279. };
  280. struct InExpoFunction
  281. {
  282. inline static void getValueAt(float t, float& result)
  283. {
  284. if(t == 0.0f)
  285. {
  286. result = 0.0f;
  287. }
  288. else
  289. {
  290. result = powf(2.0f, 10.0f * (t - 1.0f));
  291. }
  292. }
  293. };
  294. struct InOutExpoFunction
  295. {
  296. inline static void getValueAt(float t, float& result)
  297. {
  298. if(t == 0.0f || t == 1.0f)
  299. {
  300. result = 0.0f;
  301. }
  302. else
  303. {
  304. t *= 2.0f;
  305. if(t < 1.0f)
  306. {
  307. result = 0.5f * powf(2.0f, 10.0f * (t - 1.0f));
  308. }
  309. else
  310. {
  311. result = 0.5f * (-powf(2.0f, -10.0f * (t - 1.0f)) + 2.0f);
  312. }
  313. }
  314. }
  315. };
  316. class Motion : public osg::Referenced
  317. {
  318. public:
  319. typedef float value_type;
  320. enum TimeBehaviour
  321. {
  322. CLAMP,
  323. LOOP
  324. };
  325. Motion(float startValue = 0, float duration = 1, float changeValue = 1, TimeBehaviour tb = CLAMP) : _time(0), _startValue(startValue), _changeValue(changeValue), _duration(duration), _behaviour(tb) {}
  326. virtual ~Motion() {}
  327. void reset() { setTime(0);}
  328. float getTime() const { return _time; }
  329. float evaluateTime(float time) const
  330. {
  331. switch (_behaviour)
  332. {
  333. case CLAMP:
  334. if (time > _duration)
  335. time = _duration;
  336. else if (time < 0.0)
  337. time = 0.0;
  338. break;
  339. case LOOP:
  340. if (time <= 0)
  341. time = 0;
  342. else
  343. time = fmodf(time, _duration);
  344. break;
  345. }
  346. return time;
  347. }
  348. void update(float dt)
  349. {
  350. _time = evaluateTime(_time + dt);
  351. }
  352. void setTime(float time) { _time = evaluateTime(time);}
  353. void getValue(value_type& result) const { getValueAt(_time, result); }
  354. value_type getValue() const
  355. {
  356. value_type result;
  357. getValueAt(_time, result);
  358. return result;
  359. }
  360. void getValueAt(float time, value_type& result) const
  361. {
  362. getValueInNormalizedRange(evaluateTime(time)/_duration, result);
  363. result = result * _changeValue + _startValue;
  364. }
  365. value_type getValueAt(float time) const
  366. {
  367. value_type result;
  368. getValueAt(evaluateTime(time), result);
  369. return result;
  370. }
  371. virtual void getValueInNormalizedRange(float t, value_type& result) const = 0;
  372. float getDuration() const { return _duration;}
  373. protected:
  374. float _time;
  375. float _startValue;
  376. float _changeValue;
  377. float _duration;
  378. TimeBehaviour _behaviour;
  379. };
  380. template <typename T>
  381. struct MathMotionTemplate : public Motion
  382. {
  383. MathMotionTemplate(float startValue = 0, float duration = 1, float changeValue = 1, TimeBehaviour tb = CLAMP) : Motion(startValue, duration, changeValue, tb) {}
  384. virtual void getValueInNormalizedRange(float t, value_type& result) const { T::getValueAt(t, result); }
  385. };
  386. template <class T>
  387. struct SamplerMotionTemplate : public Motion
  388. {
  389. T _sampler;
  390. SamplerMotionTemplate(float startValue = 0, float duration = 1, float changeValue = 1, TimeBehaviour tb = CLAMP) : Motion(startValue, duration, changeValue, tb) {}
  391. T& getSampler() { return _sampler;}
  392. const T& getSampler() const { return _sampler;}
  393. virtual void getValueInNormalizedRange(float t, value_type& result) const
  394. {
  395. if (!_sampler.getKeyframeContainer())
  396. {
  397. result = 0;
  398. return;
  399. }
  400. float size = _sampler.getEndTime() - _sampler.getStartTime();
  401. t = t * size + _sampler.getStartTime();
  402. _sampler.getValueAt(t, result);
  403. }
  404. };
  405. struct CompositeMotion : public Motion
  406. {
  407. typedef std::vector<osg::ref_ptr<Motion> > MotionList;
  408. MotionList _motions;
  409. MotionList& getMotionList() { return _motions; }
  410. const MotionList& getMotionList() const { return _motions; }
  411. CompositeMotion(float startValue = 0, float duration = 1, float changeValue = 1, TimeBehaviour tb = CLAMP) : Motion(startValue, duration, changeValue, tb) {}
  412. virtual void getValueInNormalizedRange(float t, value_type& result) const
  413. {
  414. if (_motions.empty())
  415. {
  416. result = 0;
  417. osg::notify(osg::WARN) << "CompositeMotion::getValueInNormalizedRange no Motion in the CompositeMotion, add motion to have result" << std::endl;
  418. return;
  419. }
  420. for (MotionList::const_iterator it = _motions.begin(); it != _motions.end(); ++it)
  421. {
  422. const Motion* motion = static_cast<const Motion*>(it->get());
  423. float durationInRange = motion->getDuration() / getDuration();
  424. if (t < durationInRange)
  425. {
  426. float tInRange = t/durationInRange * motion->getDuration();
  427. motion->getValueAt( tInRange, result);
  428. return;
  429. } else
  430. t = t - durationInRange;
  431. }
  432. osg::notify(osg::WARN) << "CompositeMotion::getValueInNormalizedRange did find the value in range, something wrong" << std::endl;
  433. result = 0;
  434. }
  435. };
  436. // linear
  437. typedef MathMotionTemplate<LinearFunction > LinearMotion;
  438. // quad
  439. typedef MathMotionTemplate<OutQuadFunction > OutQuadMotion;
  440. typedef MathMotionTemplate<InQuadFunction> InQuadMotion;
  441. typedef MathMotionTemplate<InOutQuadFunction> InOutQuadMotion;
  442. // cubic
  443. typedef MathMotionTemplate<OutCubicFunction > OutCubicMotion;
  444. typedef MathMotionTemplate<InCubicFunction> InCubicMotion;
  445. typedef MathMotionTemplate<InOutCubicFunction> InOutCubicMotion;
  446. // quart
  447. typedef MathMotionTemplate<OutQuartFunction > OutQuartMotion;
  448. typedef MathMotionTemplate<InQuartFunction> InQuartMotion;
  449. typedef MathMotionTemplate<InOutQuartFunction> InOutQuartMotion;
  450. // bounce
  451. typedef MathMotionTemplate<OutBounceFunction > OutBounceMotion;
  452. typedef MathMotionTemplate<InBounceFunction> InBounceMotion;
  453. typedef MathMotionTemplate<InOutBounceFunction> InOutBounceMotion;
  454. // elastic
  455. typedef MathMotionTemplate<OutElasticFunction > OutElasticMotion;
  456. typedef MathMotionTemplate<InElasticFunction > InElasticMotion;
  457. typedef MathMotionTemplate<InOutElasticFunction > InOutElasticMotion;
  458. // sine
  459. typedef MathMotionTemplate<OutSineFunction > OutSineMotion;
  460. typedef MathMotionTemplate<InSineFunction > InSineMotion;
  461. typedef MathMotionTemplate<InOutSineFunction > InOutSineMotion;
  462. // back
  463. typedef MathMotionTemplate<OutBackFunction > OutBackMotion;
  464. typedef MathMotionTemplate<InBackFunction > InBackMotion;
  465. typedef MathMotionTemplate<InOutBackFunction > InOutBackMotion;
  466. // circ
  467. typedef MathMotionTemplate<OutCircFunction > OutCircMotion;
  468. typedef MathMotionTemplate<InCircFunction > InCircMotion;
  469. typedef MathMotionTemplate<InOutCircFunction > InOutCircMotion;
  470. // expo
  471. typedef MathMotionTemplate<OutExpoFunction > OutExpoMotion;
  472. typedef MathMotionTemplate<InExpoFunction > InExpoMotion;
  473. typedef MathMotionTemplate<InOutExpoFunction > InOutExpoMotion;
  474. }
  475. #endif