easing.inl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /// @ref gtx_easing
  2. #include <cmath>
  3. namespace glm{
  4. template <typename genType>
  5. GLM_FUNC_QUALIFIER genType linearInterpolation(genType const& a)
  6. {
  7. // Only defined in [0, 1]
  8. assert(a >= zero<genType>());
  9. assert(a <= one<genType>());
  10. return a;
  11. }
  12. template <typename genType>
  13. GLM_FUNC_QUALIFIER genType quadraticEaseIn(genType const& a)
  14. {
  15. // Only defined in [0, 1]
  16. assert(a >= zero<genType>());
  17. assert(a <= one<genType>());
  18. return a * a;
  19. }
  20. template <typename genType>
  21. GLM_FUNC_QUALIFIER genType quadraticEaseOut(genType const& a)
  22. {
  23. // Only defined in [0, 1]
  24. assert(a >= zero<genType>());
  25. assert(a <= one<genType>());
  26. return -(a * (a - static_cast<genType>(2)));
  27. }
  28. template <typename genType>
  29. GLM_FUNC_QUALIFIER genType quadraticEaseInOut(genType const& a)
  30. {
  31. // Only defined in [0, 1]
  32. assert(a >= zero<genType>());
  33. assert(a <= one<genType>());
  34. if(a < static_cast<genType>(0.5))
  35. {
  36. return static_cast<genType>(2) * a * a;
  37. }
  38. else
  39. {
  40. return (-static_cast<genType>(2) * a * a) + (4 * a) - one<genType>();
  41. }
  42. }
  43. template <typename genType>
  44. GLM_FUNC_QUALIFIER genType cubicEaseIn(genType const& a)
  45. {
  46. // Only defined in [0, 1]
  47. assert(a >= zero<genType>());
  48. assert(a <= one<genType>());
  49. return a * a * a;
  50. }
  51. template <typename genType>
  52. GLM_FUNC_QUALIFIER genType cubicEaseOut(genType const& a)
  53. {
  54. // Only defined in [0, 1]
  55. assert(a >= zero<genType>());
  56. assert(a <= one<genType>());
  57. genType const f = a - one<genType>();
  58. return f * f * f + one<genType>();
  59. }
  60. template <typename genType>
  61. GLM_FUNC_QUALIFIER genType cubicEaseInOut(genType const& a)
  62. {
  63. // Only defined in [0, 1]
  64. assert(a >= zero<genType>());
  65. assert(a <= one<genType>());
  66. if (a < static_cast<genType>(0.5))
  67. {
  68. return static_cast<genType>(4) * a * a * a;
  69. }
  70. else
  71. {
  72. genType const f = ((static_cast<genType>(2) * a) - static_cast<genType>(2));
  73. return static_cast<genType>(0.5) * f * f * f + one<genType>();
  74. }
  75. }
  76. template <typename genType>
  77. GLM_FUNC_QUALIFIER genType quarticEaseIn(genType const& a)
  78. {
  79. // Only defined in [0, 1]
  80. assert(a >= zero<genType>());
  81. assert(a <= one<genType>());
  82. return a * a * a * a;
  83. }
  84. template <typename genType>
  85. GLM_FUNC_QUALIFIER genType quarticEaseOut(genType const& a)
  86. {
  87. // Only defined in [0, 1]
  88. assert(a >= zero<genType>());
  89. assert(a <= one<genType>());
  90. genType const f = (a - one<genType>());
  91. return f * f * f * (one<genType>() - a) + one<genType>();
  92. }
  93. template <typename genType>
  94. GLM_FUNC_QUALIFIER genType quarticEaseInOut(genType const& a)
  95. {
  96. // Only defined in [0, 1]
  97. assert(a >= zero<genType>());
  98. assert(a <= one<genType>());
  99. if(a < static_cast<genType>(0.5))
  100. {
  101. return static_cast<genType>(8) * a * a * a * a;
  102. }
  103. else
  104. {
  105. genType const f = (a - one<genType>());
  106. return -static_cast<genType>(8) * f * f * f * f + one<genType>();
  107. }
  108. }
  109. template <typename genType>
  110. GLM_FUNC_QUALIFIER genType quinticEaseIn(genType const& a)
  111. {
  112. // Only defined in [0, 1]
  113. assert(a >= zero<genType>());
  114. assert(a <= one<genType>());
  115. return a * a * a * a * a;
  116. }
  117. template <typename genType>
  118. GLM_FUNC_QUALIFIER genType quinticEaseOut(genType const& a)
  119. {
  120. // Only defined in [0, 1]
  121. assert(a >= zero<genType>());
  122. assert(a <= one<genType>());
  123. genType const f = (a - one<genType>());
  124. return f * f * f * f * f + one<genType>();
  125. }
  126. template <typename genType>
  127. GLM_FUNC_QUALIFIER genType quinticEaseInOut(genType const& a)
  128. {
  129. // Only defined in [0, 1]
  130. assert(a >= zero<genType>());
  131. assert(a <= one<genType>());
  132. if(a < static_cast<genType>(0.5))
  133. {
  134. return static_cast<genType>(16) * a * a * a * a * a;
  135. }
  136. else
  137. {
  138. genType const f = ((static_cast<genType>(2) * a) - static_cast<genType>(2));
  139. return static_cast<genType>(0.5) * f * f * f * f * f + one<genType>();
  140. }
  141. }
  142. template <typename genType>
  143. GLM_FUNC_QUALIFIER genType sineEaseIn(genType const& a)
  144. {
  145. // Only defined in [0, 1]
  146. assert(a >= zero<genType>());
  147. assert(a <= one<genType>());
  148. return sin((a - one<genType>()) * half_pi<genType>()) + one<genType>();
  149. }
  150. template <typename genType>
  151. GLM_FUNC_QUALIFIER genType sineEaseOut(genType const& a)
  152. {
  153. // Only defined in [0, 1]
  154. assert(a >= zero<genType>());
  155. assert(a <= one<genType>());
  156. return sin(a * half_pi<genType>());
  157. }
  158. template <typename genType>
  159. GLM_FUNC_QUALIFIER genType sineEaseInOut(genType const& a)
  160. {
  161. // Only defined in [0, 1]
  162. assert(a >= zero<genType>());
  163. assert(a <= one<genType>());
  164. return static_cast<genType>(0.5) * (one<genType>() - cos(a * pi<genType>()));
  165. }
  166. template <typename genType>
  167. GLM_FUNC_QUALIFIER genType circularEaseIn(genType const& a)
  168. {
  169. // Only defined in [0, 1]
  170. assert(a >= zero<genType>());
  171. assert(a <= one<genType>());
  172. return one<genType>() - sqrt(one<genType>() - (a * a));
  173. }
  174. template <typename genType>
  175. GLM_FUNC_QUALIFIER genType circularEaseOut(genType const& a)
  176. {
  177. // Only defined in [0, 1]
  178. assert(a >= zero<genType>());
  179. assert(a <= one<genType>());
  180. return sqrt((static_cast<genType>(2) - a) * a);
  181. }
  182. template <typename genType>
  183. GLM_FUNC_QUALIFIER genType circularEaseInOut(genType const& a)
  184. {
  185. // Only defined in [0, 1]
  186. assert(a >= zero<genType>());
  187. assert(a <= one<genType>());
  188. if(a < static_cast<genType>(0.5))
  189. {
  190. return static_cast<genType>(0.5) * (one<genType>() - std::sqrt(one<genType>() - static_cast<genType>(4) * (a * a)));
  191. }
  192. else
  193. {
  194. return static_cast<genType>(0.5) * (std::sqrt(-((static_cast<genType>(2) * a) - static_cast<genType>(3)) * ((static_cast<genType>(2) * a) - one<genType>())) + one<genType>());
  195. }
  196. }
  197. template <typename genType>
  198. GLM_FUNC_QUALIFIER genType exponentialEaseIn(genType const& a)
  199. {
  200. // Only defined in [0, 1]
  201. assert(a >= zero<genType>());
  202. assert(a <= one<genType>());
  203. if(a <= zero<genType>())
  204. return a;
  205. else
  206. {
  207. genType const Complementary = a - one<genType>();
  208. genType const Two = static_cast<genType>(2);
  209. return glm::pow(Two, Complementary * static_cast<genType>(10));
  210. }
  211. }
  212. template <typename genType>
  213. GLM_FUNC_QUALIFIER genType exponentialEaseOut(genType const& a)
  214. {
  215. // Only defined in [0, 1]
  216. assert(a >= zero<genType>());
  217. assert(a <= one<genType>());
  218. if(a >= one<genType>())
  219. return a;
  220. else
  221. {
  222. return one<genType>() - glm::pow(static_cast<genType>(2), -static_cast<genType>(10) * a);
  223. }
  224. }
  225. template <typename genType>
  226. GLM_FUNC_QUALIFIER genType exponentialEaseInOut(genType const& a)
  227. {
  228. // Only defined in [0, 1]
  229. assert(a >= zero<genType>());
  230. assert(a <= one<genType>());
  231. if(a < static_cast<genType>(0.5))
  232. return static_cast<genType>(0.5) * glm::pow(static_cast<genType>(2), (static_cast<genType>(20) * a) - static_cast<genType>(10));
  233. else
  234. return -static_cast<genType>(0.5) * glm::pow(static_cast<genType>(2), (-static_cast<genType>(20) * a) + static_cast<genType>(10)) + one<genType>();
  235. }
  236. template <typename genType>
  237. GLM_FUNC_QUALIFIER genType elasticEaseIn(genType const& a)
  238. {
  239. // Only defined in [0, 1]
  240. assert(a >= zero<genType>());
  241. assert(a <= one<genType>());
  242. return std::sin(static_cast<genType>(13) * half_pi<genType>() * a) * glm::pow(static_cast<genType>(2), static_cast<genType>(10) * (a - one<genType>()));
  243. }
  244. template <typename genType>
  245. GLM_FUNC_QUALIFIER genType elasticEaseOut(genType const& a)
  246. {
  247. // Only defined in [0, 1]
  248. assert(a >= zero<genType>());
  249. assert(a <= one<genType>());
  250. return std::sin(-static_cast<genType>(13) * half_pi<genType>() * (a + one<genType>())) * glm::pow(static_cast<genType>(2), -static_cast<genType>(10) * a) + one<genType>();
  251. }
  252. template <typename genType>
  253. GLM_FUNC_QUALIFIER genType elasticEaseInOut(genType const& a)
  254. {
  255. // Only defined in [0, 1]
  256. assert(a >= zero<genType>());
  257. assert(a <= one<genType>());
  258. if(a < static_cast<genType>(0.5))
  259. return static_cast<genType>(0.5) * std::sin(static_cast<genType>(13) * half_pi<genType>() * (static_cast<genType>(2) * a)) * glm::pow(static_cast<genType>(2), static_cast<genType>(10) * ((static_cast<genType>(2) * a) - one<genType>()));
  260. else
  261. return static_cast<genType>(0.5) * (std::sin(-static_cast<genType>(13) * half_pi<genType>() * ((static_cast<genType>(2) * a - one<genType>()) + one<genType>())) * glm::pow(static_cast<genType>(2), -static_cast<genType>(10) * (static_cast<genType>(2) * a - one<genType>())) + static_cast<genType>(2));
  262. }
  263. template <typename genType>
  264. GLM_FUNC_QUALIFIER genType backEaseIn(genType const& a, genType const& o)
  265. {
  266. // Only defined in [0, 1]
  267. assert(a >= zero<genType>());
  268. assert(a <= one<genType>());
  269. genType z = ((o + one<genType>()) * a) - o;
  270. return (a * a * z);
  271. }
  272. template <typename genType>
  273. GLM_FUNC_QUALIFIER genType backEaseOut(genType const& a, genType const& o)
  274. {
  275. // Only defined in [0, 1]
  276. assert(a >= zero<genType>());
  277. assert(a <= one<genType>());
  278. genType n = a - one<genType>();
  279. genType z = ((o + one<genType>()) * n) + o;
  280. return (n * n * z) + one<genType>();
  281. }
  282. template <typename genType>
  283. GLM_FUNC_QUALIFIER genType backEaseInOut(genType const& a, genType const& o)
  284. {
  285. // Only defined in [0, 1]
  286. assert(a >= zero<genType>());
  287. assert(a <= one<genType>());
  288. genType s = o * static_cast<genType>(1.525);
  289. genType x = static_cast<genType>(0.5);
  290. genType n = a / static_cast<genType>(0.5);
  291. if (n < static_cast<genType>(1))
  292. {
  293. genType z = ((s + static_cast<genType>(1)) * n) - s;
  294. genType m = n * n * z;
  295. return x * m;
  296. }
  297. else
  298. {
  299. n -= static_cast<genType>(2);
  300. genType z = ((s + static_cast<genType>(1)) * n) + s;
  301. genType m = (n*n*z) + static_cast<genType>(2);
  302. return x * m;
  303. }
  304. }
  305. template <typename genType>
  306. GLM_FUNC_QUALIFIER genType backEaseIn(genType const& a)
  307. {
  308. return backEaseIn(a, static_cast<genType>(1.70158));
  309. }
  310. template <typename genType>
  311. GLM_FUNC_QUALIFIER genType backEaseOut(genType const& a)
  312. {
  313. return backEaseOut(a, static_cast<genType>(1.70158));
  314. }
  315. template <typename genType>
  316. GLM_FUNC_QUALIFIER genType backEaseInOut(genType const& a)
  317. {
  318. return backEaseInOut(a, static_cast<genType>(1.70158));
  319. }
  320. template <typename genType>
  321. GLM_FUNC_QUALIFIER genType bounceEaseOut(genType const& a)
  322. {
  323. // Only defined in [0, 1]
  324. assert(a >= zero<genType>());
  325. assert(a <= one<genType>());
  326. if(a < static_cast<genType>(4.0 / 11.0))
  327. {
  328. return (static_cast<genType>(121) * a * a) / static_cast<genType>(16);
  329. }
  330. else if(a < static_cast<genType>(8.0 / 11.0))
  331. {
  332. return (static_cast<genType>(363.0 / 40.0) * a * a) - (static_cast<genType>(99.0 / 10.0) * a) + static_cast<genType>(17.0 / 5.0);
  333. }
  334. else if(a < static_cast<genType>(9.0 / 10.0))
  335. {
  336. return (static_cast<genType>(4356.0 / 361.0) * a * a) - (static_cast<genType>(35442.0 / 1805.0) * a) + static_cast<genType>(16061.0 / 1805.0);
  337. }
  338. else
  339. {
  340. return (static_cast<genType>(54.0 / 5.0) * a * a) - (static_cast<genType>(513.0 / 25.0) * a) + static_cast<genType>(268.0 / 25.0);
  341. }
  342. }
  343. template <typename genType>
  344. GLM_FUNC_QUALIFIER genType bounceEaseIn(genType const& a)
  345. {
  346. // Only defined in [0, 1]
  347. assert(a >= zero<genType>());
  348. assert(a <= one<genType>());
  349. return one<genType>() - bounceEaseOut(one<genType>() - a);
  350. }
  351. template <typename genType>
  352. GLM_FUNC_QUALIFIER genType bounceEaseInOut(genType const& a)
  353. {
  354. // Only defined in [0, 1]
  355. assert(a >= zero<genType>());
  356. assert(a <= one<genType>());
  357. if(a < static_cast<genType>(0.5))
  358. {
  359. return static_cast<genType>(0.5) * (one<genType>() - bounceEaseOut(a * static_cast<genType>(2)));
  360. }
  361. else
  362. {
  363. return static_cast<genType>(0.5) * bounceEaseOut(a * static_cast<genType>(2) - one<genType>()) + static_cast<genType>(0.5);
  364. }
  365. }
  366. }//namespace glm