💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

398 lines
10 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * Easing Functions (Equations)
  21. *
  22. * Copyright (C) 2001 Robert Penner
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions are met:
  26. *
  27. * * Redistributions of source code must retain the above copyright notice, this
  28. * list of conditions and the following disclaimer.
  29. *
  30. * * Redistributions in binary form must reproduce the above copyright notice,
  31. * this list of conditions and the following disclaimer in the documentation
  32. * and/or other materials provided with the distribution.
  33. *
  34. * * Neither the name of the author nor the names of contributors may be used to
  35. * endorse or promote products derived from this software without specific
  36. * prior written permission.
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  38. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  41. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  43. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. #ifndef ANTKEEPER_EASE_HPP
  49. #define ANTKEEPER_EASE_HPP
  50. #include "math/math.hpp"
  51. #include <cmath>
  52. /**
  53. * Container for templated easing functions.
  54. *
  55. * All easing functions require the following operators to be defined:
  56. *
  57. * value_type operator+(const value_type&, const value_type&);
  58. * value_type operator-(const value_type&, const value_type&);
  59. * value_type operator*(const value_type&, scalar_type);
  60. *
  61. * @tparam T Value type.
  62. * @tparam S Scalar type.
  63. */
  64. template <typename T, typename S = float>
  65. struct ease
  66. {
  67. typedef T value_type;
  68. typedef S scalar_type;
  69. static T in_sine(const T& x, const T& y, S a);
  70. static T out_sine(const T& x, const T& y, S a);
  71. static T in_out_sine(const T& x, const T& y, S a);
  72. static T in_quad(const T& x, const T& y, S a);
  73. static T out_quad(const T& x, const T& y, S a);
  74. static T in_out_quad(const T& x, const T& y, S a);
  75. static T in_cubic(const T& x, const T& y, S a);
  76. static T out_cubic(const T& x, const T& y, S a);
  77. static T in_out_cubic(const T& x, const T& y, S a);
  78. static T in_quart(const T& x, const T& y, S a);
  79. static T out_quart(const T& x, const T& y, S a);
  80. static T in_out_quart(const T& x, const T& y, S a);
  81. static T in_quint(const T& x, const T& y, S a);
  82. static T out_quint(const T& x, const T& y, S a);
  83. static T in_out_quint(const T& x, const T& y, S a);
  84. static T in_expo(const T& x, const T& y, S a);
  85. static T out_expo(const T& x, const T& y, S a);
  86. static T in_out_expo(const T& x, const T& y, S a);
  87. static T in_circ(const T& x, const T& y, S a);
  88. static T out_circ(const T& x, const T& y, S a);
  89. static T in_out_circ(const T& x, const T& y, S a);
  90. static T in_back(const T& x, const T& y, S a);
  91. static T out_back(const T& x, const T& y, S a);
  92. static T in_out_back(const T& x, const T& y, S a);
  93. static T in_elastic(const T& x, const T& y, S a);
  94. static T out_elastic(const T& x, const T& y, S a);
  95. static T in_out_elastic(const T& x, const T& y, S a);
  96. static T in_bounce(const T& x, const T& y, S a);
  97. static T out_bounce(const T& x, const T& y, S a);
  98. static T in_out_bounce(const T& x, const T& y, S a);
  99. };
  100. template <typename T, typename S>
  101. T ease<T, S>::in_sine(const T& x, const T& y, S a)
  102. {
  103. return math::lerp(y, x, std::cos(a * math::half_pi<S>));
  104. }
  105. template <typename T, typename S>
  106. T ease<T, S>::out_sine(const T& x, const T& y, S a)
  107. {
  108. return math::lerp(x, y, std::sin(a * math::half_pi<S>));
  109. }
  110. template <typename T, typename S>
  111. T ease<T, S>::in_out_sine(const T& x, const T& y, S a)
  112. {
  113. return math::lerp(x, y, -(std::cos(a * math::pi<S>) - S(1)) * S(0.5));
  114. }
  115. template <typename T, typename S>
  116. T ease<T, S>::in_quad(const T& x, const T& y, S a)
  117. {
  118. return math::lerp(x, y, a * a);
  119. }
  120. template <typename T, typename S>
  121. T ease<T, S>::out_quad(const T& x, const T& y, S a)
  122. {
  123. return math::lerp(x, y, (S(2) - a) * a);
  124. }
  125. template <typename T, typename S>
  126. T ease<T, S>::in_out_quad(const T& x, const T& y, S a)
  127. {
  128. return math::lerp(x, y, (a < S(0.5)) ? S(2) * a * a : -(S(2) * a * a - S(4) * a + S(1)));
  129. }
  130. template <typename T, typename S>
  131. T ease<T, S>::in_cubic(const T& x, const T& y, S a)
  132. {
  133. return math::lerp(x, y, a * a * a);
  134. }
  135. template <typename T, typename S>
  136. T ease<T, S>::out_cubic(const T& x, const T& y, S a)
  137. {
  138. return math::lerp(x, y, a * ((a - S(3)) * a + S(3)));
  139. }
  140. template <typename T, typename S>
  141. T ease<T, S>::in_out_cubic(const T& x, const T& y, S a)
  142. {
  143. return math::lerp(x, y, (a < S(0.5)) ? S(4) * a * a * a : S(4) * a * a * a - S(12) * a * a + S(12) * a - 3);
  144. }
  145. template <typename T, typename S>
  146. T ease<T, S>::in_quart(const T& x, const T& y, S a)
  147. {
  148. return math::lerp(x, y, a * a * a * a);
  149. }
  150. template <typename T, typename S>
  151. T ease<T, S>::out_quart(const T& x, const T& y, S a)
  152. {
  153. return math::lerp(x, y, a * (a * ((S(4) - a) * a - S(6)) + S(4)));
  154. }
  155. template <typename T, typename S>
  156. T ease<T, S>::in_out_quart(const T& x, const T& y, S a)
  157. {
  158. return math::lerp(x, y, (a < S(0.5)) ? S(8) * a * a * a * a : a * (a * ((S(32) - S(8) * a) * a - S(48)) + S(32)) - S(7));
  159. }
  160. template <typename T, typename S>
  161. T ease<T, S>::in_quint(const T& x, const T& y, S a)
  162. {
  163. return math::lerp(x, y, a * a * a * a * a);
  164. }
  165. template <typename T, typename S>
  166. T ease<T, S>::out_quint(const T& x, const T& y, S a)
  167. {
  168. return math::lerp(x, y, a * (a * (a * ((a - S(5)) * a + S(10)) - S(10)) + S(5)));
  169. }
  170. template <typename T, typename S>
  171. T ease<T, S>::in_out_quint(const T& x, const T& y, S a)
  172. {
  173. if (a < S(0.5))
  174. {
  175. return math::lerp(x, y, S(16) * a * a * a * a * a);
  176. }
  177. else
  178. {
  179. a = S(2) * (S(1) - a);
  180. return math::lerp(x, y, S(0.5) * (S(2) - a * a * a * a * a));
  181. }
  182. }
  183. template <typename T, typename S>
  184. T ease<T, S>::in_expo(const T& x, const T& y, S a)
  185. {
  186. return (a == S(0)) ? x : math::lerp(x, y, std::pow(S(1024), a - S(1)));
  187. }
  188. template <typename T, typename S>
  189. T ease<T, S>::out_expo(const T& x, const T& y, S a)
  190. {
  191. return (a == S(1)) ? y : math::lerp(y, x, std::pow(S(2), S(-10) * a));
  192. }
  193. template <typename T, typename S>
  194. T ease<T, S>::in_out_expo(const T& x, const T& y, S a)
  195. {
  196. if (a == S(0))
  197. {
  198. return x;
  199. }
  200. else if (a == S(1))
  201. {
  202. return y;
  203. }
  204. return math::lerp(x, y, (a < S(0.5)) ? std::pow(S(2), S(20) * a - S(11)) : S(1) - std::pow(S(2), S(9) - S(20) * a));
  205. }
  206. template <typename T, typename S>
  207. T ease<T, S>::in_circ(const T& x, const T& y, S a)
  208. {
  209. return math::lerp(y, x, std::sqrt(S(1) - a * a));
  210. }
  211. template <typename T, typename S>
  212. T ease<T, S>::out_circ(const T& x, const T& y, S a)
  213. {
  214. return math::lerp(x, y, std::sqrt(-(a - S(2)) * a));
  215. }
  216. template <typename T, typename S>
  217. T ease<T, S>::in_out_circ(const T& x, const T& y, S a)
  218. {
  219. if (a < S(0.5))
  220. {
  221. return math::lerp(x, y, S(0.5) - S(0.5) * std::sqrt(S(1) - S(4) * a * a));
  222. }
  223. else
  224. {
  225. return math::lerp(x, y, S(0.5) * (std::sqrt(S(-4) * (a - S(2)) * a - S(3)) + S(1)));
  226. }
  227. }
  228. template <typename T, typename S>
  229. T ease<T, S>::in_back(const T& x, const T& y, S a)
  230. {
  231. const S c = S(1.70158);
  232. return math::lerp(x, y, a * a * (a * c + a - c));
  233. }
  234. template <typename T, typename S>
  235. T ease<T, S>::out_back(const T& x, const T& y, S a)
  236. {
  237. const S c = S(1.70158);
  238. a -= S(1);
  239. return math::lerp(x, y, a * a * (a * c + a + c) + S(1));
  240. }
  241. template <typename T, typename S>
  242. T ease<T, S>::in_out_back(const T& x, const T& y, S a)
  243. {
  244. const S c = S(1.70158) * S(1.525f);
  245. if (a < S(0.5))
  246. {
  247. return math::lerp(x, y, a * a * (a * (S(4) * c + S(4)) - S(2) * c));
  248. }
  249. else
  250. {
  251. S b = S(1) - S(2) * a;
  252. return math::lerp(x, y, b * b * (a * c + a - c * S(0.5) - S(1)) + S(1));
  253. }
  254. }
  255. template <typename T, typename S>
  256. T ease<T, S>::in_elastic(const T& x, const T& y, S a)
  257. {
  258. if (a == S(0))
  259. {
  260. return x;
  261. }
  262. else if (a == S(1))
  263. {
  264. return y;
  265. }
  266. return math::lerp(x, y, -std::pow(S(1024), a - S(1)) * std::sin(S(20.944) * (a - S(1.075))));
  267. }
  268. template <typename T, typename S>
  269. T ease<T, S>::out_elastic(const T& x, const T& y, S a)
  270. {
  271. if (a == S(0))
  272. {
  273. return x;
  274. }
  275. else if (a == S(1))
  276. {
  277. return y;
  278. }
  279. return math::lerp(x, y, std::pow(S(2), S(-10) * a) * std::sin(S(20.944) * (a - S(0.075))) + S(1));
  280. }
  281. template <typename T, typename S>
  282. T ease<T, S>::in_out_elastic(const T& x, const T& y, S a)
  283. {
  284. if (a == S(0))
  285. {
  286. return x;
  287. }
  288. else if (a == S(1))
  289. {
  290. return y;
  291. }
  292. if (a < S(0.5))
  293. {
  294. return math::lerp(x, y, std::pow(S(2), S(20) * a - S(11)) * std::sin(S(15.5334) - S(27.5293) * a));
  295. }
  296. else
  297. {
  298. return math::lerp(y, x, std::pow(2, S(9) - S(20) * a) * std::sin(S(15.5334) - S(27.5293) * a));
  299. }
  300. }
  301. template <typename T, typename S>
  302. T ease<T, S>::in_bounce(const T& x, const T& y, S a)
  303. {
  304. return math::lerp(x, y, S(1) - ease<S, S>::out_bounce(S(0), S(1), S(1) - a));
  305. }
  306. template <typename T, typename S>
  307. T ease<T, S>::out_bounce(const T& x, const T& y, S a)
  308. {
  309. const S n = S(7.5625);
  310. const S d = S(2.75);
  311. if (a < S(1) / d)
  312. {
  313. a = n * a * a;
  314. }
  315. else if (a < S(2) / d)
  316. {
  317. a -= S(1.5) / d;
  318. a = n * a * a + S(0.75);
  319. }
  320. else if (a < S(2.5) / d)
  321. {
  322. a -= S(2.25) / d;
  323. a = n * a * a + S(0.9375);
  324. }
  325. else
  326. {
  327. a -= S(2.625) / d;
  328. a = n * a * a + S(0.984375);
  329. }
  330. return math::lerp(x, y, a);
  331. }
  332. template <typename T, typename S>
  333. T ease<T, S>::in_out_bounce(const T& x, const T& y, S a)
  334. {
  335. if (a < S(0.5))
  336. {
  337. return math::lerp(x, y, (S(1) - ease<S, S>::out_bounce(S(0), S(1), S(1) - S(2) * a)) * S(0.5));
  338. }
  339. else
  340. {
  341. return math::lerp(x, y, (S(1) + ease<S, S>::out_bounce(S(0), S(1), S(2) * a - S(1))) * S(0.5));
  342. }
  343. }
  344. #endif // ANTKEEPER_EASE_HPP