💿🐜 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.

418 lines
11 KiB

  1. /*
  2. * Copyright (C) 2020 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 <vmq/vmq.hpp>
  51. #include <cmath>
  52. #include <type_traits>
  53. /**
  54. * Container for templated easing functions.
  55. *
  56. * All easing functions require the following operators to be defined:
  57. *
  58. * value_type operator+(const value_type&, const value_type&);
  59. * value_type operator-(const value_type&, const value_type&);
  60. * value_type operator*(const value_type&, scalar_type) const;
  61. *
  62. * @tparam T Value type.
  63. * @tparam S Scalar type.
  64. */
  65. template <typename T, typename S = float>
  66. struct ease
  67. {
  68. typedef T value_type;
  69. typedef S scalar_type;
  70. /// Linearly interpolates between @p x and @p y.
  71. static T linear(const T& x, const T& y, S a);
  72. /// Logarithmically interpolates between @p x and @p y.
  73. static T log(const T& x, const T& y, S a);
  74. static T in_sine(const T& x, const T& y, S a);
  75. static T out_sine(const T& x, const T& y, S a);
  76. static T in_out_sine(const T& x, const T& y, S a);
  77. static T in_quad(const T& x, const T& y, S a);
  78. static T out_quad(const T& x, const T& y, S a);
  79. static T in_out_quad(const T& x, const T& y, S a);
  80. static T in_cubic(const T& x, const T& y, S a);
  81. static T out_cubic(const T& x, const T& y, S a);
  82. static T in_out_cubic(const T& x, const T& y, S a);
  83. static T in_quart(const T& x, const T& y, S a);
  84. static T out_quart(const T& x, const T& y, S a);
  85. static T in_out_quart(const T& x, const T& y, S a);
  86. static T in_quint(const T& x, const T& y, S a);
  87. static T out_quint(const T& x, const T& y, S a);
  88. static T in_out_quint(const T& x, const T& y, S a);
  89. static T in_expo(const T& x, const T& y, S a);
  90. static T out_expo(const T& x, const T& y, S a);
  91. static T in_out_expo(const T& x, const T& y, S a);
  92. static T in_circ(const T& x, const T& y, S a);
  93. static T out_circ(const T& x, const T& y, S a);
  94. static T in_out_circ(const T& x, const T& y, S a);
  95. static T in_back(const T& x, const T& y, S a);
  96. static T out_back(const T& x, const T& y, S a);
  97. static T in_out_back(const T& x, const T& y, S a);
  98. static T in_elastic(const T& x, const T& y, S a);
  99. static T out_elastic(const T& x, const T& y, S a);
  100. static T in_out_elastic(const T& x, const T& y, S a);
  101. static T in_bounce(const T& x, const T& y, S a);
  102. static T out_bounce(const T& x, const T& y, S a);
  103. static T in_out_bounce(const T& x, const T& y, S a);
  104. };
  105. template <typename T, typename S>
  106. inline T ease<T, S>::linear(const T& x, const T& y, S a)
  107. {
  108. return (y - x) * a + x;
  109. }
  110. template <typename T, typename S>
  111. inline T ease<T, S>::log(const T& x, const T& y, S a)
  112. {
  113. //return std::exp(linear(std::log(x), std::log(y), a));
  114. return x * std::pow(y / x, a);
  115. }
  116. template <typename T, typename S>
  117. T ease<T, S>::in_sine(const T& x, const T& y, S a)
  118. {
  119. return linear(y, x, std::cos(a * vmq::half_pi<S>));
  120. }
  121. template <typename T, typename S>
  122. T ease<T, S>::out_sine(const T& x, const T& y, S a)
  123. {
  124. return linear(x, y, std::sin(a * vmq::half_pi<S>));
  125. }
  126. template <typename T, typename S>
  127. T ease<T, S>::in_out_sine(const T& x, const T& y, S a)
  128. {
  129. return linear(x, y, -(std::cos(a * vmq::pi<S>) - S(1)) * S(0.5));
  130. }
  131. template <typename T, typename S>
  132. T ease<T, S>::in_quad(const T& x, const T& y, S a)
  133. {
  134. return linear(x, y, a * a);
  135. }
  136. template <typename T, typename S>
  137. T ease<T, S>::out_quad(const T& x, const T& y, S a)
  138. {
  139. return linear(x, y, (S(2) - a) * a);
  140. }
  141. template <typename T, typename S>
  142. T ease<T, S>::in_out_quad(const T& x, const T& y, S a)
  143. {
  144. return linear(x, y, (a < S(0.5)) ? S(2) * a * a : -(S(2) * a * a - S(4) * a + S(1)));
  145. }
  146. template <typename T, typename S>
  147. T ease<T, S>::in_cubic(const T& x, const T& y, S a)
  148. {
  149. return linear(x, y, a * a * a);
  150. }
  151. template <typename T, typename S>
  152. T ease<T, S>::out_cubic(const T& x, const T& y, S a)
  153. {
  154. return linear(x, y, a * ((a - S(3)) * a + S(3)));
  155. }
  156. template <typename T, typename S>
  157. T ease<T, S>::in_out_cubic(const T& x, const T& y, S a)
  158. {
  159. return linear(x, y, (a < S(0.5)) ? S(4) * a * a * a : S(4) * a * a * a - S(12) * a * a + S(12) * a - 3);
  160. }
  161. template <typename T, typename S>
  162. T ease<T, S>::in_quart(const T& x, const T& y, S a)
  163. {
  164. return linear(x, y, a * a * a * a);
  165. }
  166. template <typename T, typename S>
  167. T ease<T, S>::out_quart(const T& x, const T& y, S a)
  168. {
  169. return linear(x, y, a * (a * ((S(4) - a) * a - S(6)) + S(4)));
  170. }
  171. template <typename T, typename S>
  172. T ease<T, S>::in_out_quart(const T& x, const T& y, S a)
  173. {
  174. return linear(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));
  175. }
  176. template <typename T, typename S>
  177. T ease<T, S>::in_quint(const T& x, const T& y, S a)
  178. {
  179. return linear(x, y, a * a * a * a * a);
  180. }
  181. template <typename T, typename S>
  182. T ease<T, S>::out_quint(const T& x, const T& y, S a)
  183. {
  184. return linear(x, y, a * (a * (a * ((a - S(5)) * a + S(10)) - S(10)) + S(5)));
  185. }
  186. template <typename T, typename S>
  187. T ease<T, S>::in_out_quint(const T& x, const T& y, S a)
  188. {
  189. if (a < S(0.5))
  190. {
  191. return linear(x, y, S(16) * a * a * a * a * a);
  192. }
  193. else
  194. {
  195. a = S(2) * (S(1) - a);
  196. return linear(x, y, S(0.5) * (S(2) - a * a * a * a * a));
  197. }
  198. }
  199. template <typename T, typename S>
  200. T ease<T, S>::in_expo(const T& x, const T& y, S a)
  201. {
  202. return (a == S(0)) ? x : linear(x, y, std::pow(S(1024), a - S(1)));
  203. }
  204. template <typename T, typename S>
  205. T ease<T, S>::out_expo(const T& x, const T& y, S a)
  206. {
  207. return (a == S(1)) ? y : linear(y, x, std::pow(S(2), S(-10) * a));
  208. }
  209. template <typename T, typename S>
  210. T ease<T, S>::in_out_expo(const T& x, const T& y, S a)
  211. {
  212. if (a == S(0))
  213. {
  214. return x;
  215. }
  216. else if (a == S(1))
  217. {
  218. return y;
  219. }
  220. return linear(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));
  221. }
  222. template <typename T, typename S>
  223. T ease<T, S>::in_circ(const T& x, const T& y, S a)
  224. {
  225. return linear(y, x, std::sqrt(S(1) - a * a));
  226. }
  227. template <typename T, typename S>
  228. T ease<T, S>::out_circ(const T& x, const T& y, S a)
  229. {
  230. return linear(x, y, std::sqrt(-(a - S(2)) * a));
  231. }
  232. template <typename T, typename S>
  233. T ease<T, S>::in_out_circ(const T& x, const T& y, S a)
  234. {
  235. if (a < S(0.5))
  236. {
  237. return linear(x, y, S(0.5) - S(0.5) * std::sqrt(S(1) - S(4) * a * a));
  238. }
  239. else
  240. {
  241. return linear(x, y, S(0.5) * (std::sqrt(S(-4) * (a - S(2)) * a - S(3)) + S(1)));
  242. }
  243. }
  244. template <typename T, typename S>
  245. T ease<T, S>::in_back(const T& x, const T& y, S a)
  246. {
  247. const S c = S(1.70158);
  248. return linear(x, y, a * a * (a * c + a - c));
  249. }
  250. template <typename T, typename S>
  251. T ease<T, S>::out_back(const T& x, const T& y, S a)
  252. {
  253. const S c = S(1.70158);
  254. a -= S(1);
  255. return linear(x, y, a * a * (a * c + a + c) + S(1));
  256. }
  257. template <typename T, typename S>
  258. T ease<T, S>::in_out_back(const T& x, const T& y, S a)
  259. {
  260. const S c = S(1.70158) * S(1.525f);
  261. if (a < S(0.5))
  262. {
  263. return linear(x, y, a * a * (a * (S(4) * c + S(4)) - S(2) * c));
  264. }
  265. else
  266. {
  267. S b = S(1) - S(2) * a;
  268. return linear(x, y, b * b * (a * c + a - c * S(0.5) - S(1)) + S(1));
  269. }
  270. }
  271. template <typename T, typename S>
  272. T ease<T, S>::in_elastic(const T& x, const T& y, S a)
  273. {
  274. if (a == S(0))
  275. {
  276. return x;
  277. }
  278. else if (a == S(1))
  279. {
  280. return y;
  281. }
  282. return linear(x, y, -std::pow(S(1024), a - S(1)) * std::sin(S(20.944) * (a - S(1.075))));
  283. }
  284. template <typename T, typename S>
  285. T ease<T, S>::out_elastic(const T& x, const T& y, S a)
  286. {
  287. if (a == S(0))
  288. {
  289. return x;
  290. }
  291. else if (a == S(1))
  292. {
  293. return y;
  294. }
  295. return linear(x, y, std::pow(S(2), S(-10) * a) * std::sin(S(20.944) * (a - S(0.075))) + S(1));
  296. }
  297. template <typename T, typename S>
  298. T ease<T, S>::in_out_elastic(const T& x, const T& y, S a)
  299. {
  300. if (a == S(0))
  301. {
  302. return x;
  303. }
  304. else if (a == S(1))
  305. {
  306. return y;
  307. }
  308. if (a < S(0.5))
  309. {
  310. return linear(x, y, std::pow(S(2), S(20) * a - S(11)) * std::sin(S(15.5334) - S(27.5293) * a));
  311. }
  312. else
  313. {
  314. return linear(y, x, std::pow(2, S(9) - S(20) * a) * std::sin(S(15.5334) - S(27.5293) * a));
  315. }
  316. }
  317. template <typename T, typename S>
  318. T ease<T, S>::in_bounce(const T& x, const T& y, S a)
  319. {
  320. return linear(x, y, S(1) - ease<S, S>::out_bounce(S(0), S(1), S(1) - a));
  321. }
  322. template <typename T, typename S>
  323. T ease<T, S>::out_bounce(const T& x, const T& y, S a)
  324. {
  325. const S n = S(7.5625);
  326. const S d = S(2.75);
  327. if (a < S(1) / d)
  328. {
  329. a = n * a * a;
  330. }
  331. else if (a < S(2) / d)
  332. {
  333. a -= S(1.5) / d;
  334. a = n * a * a + S(0.75);
  335. }
  336. else if (a < S(2.5) / d)
  337. {
  338. a -= S(2.25) / d;
  339. a = n * a * a + S(0.9375);
  340. }
  341. else
  342. {
  343. a -= S(2.625) / d;
  344. a = n * a * a + S(0.984375);
  345. }
  346. return linear(x, y, a);
  347. }
  348. template <typename T, typename S>
  349. T ease<T, S>::in_out_bounce(const T& x, const T& y, S a)
  350. {
  351. if (a < S(0.5))
  352. {
  353. return linear(x, y, (S(1) - ease<S, S>::out_bounce(S(0), S(1), S(1) - S(2) * a)) * S(0.5));
  354. }
  355. else
  356. {
  357. return linear(x, y, (S(1) + ease<S, S>::out_bounce(S(0), S(1), S(2) * a - S(1))) * S(0.5));
  358. }
  359. }
  360. #endif // ANTKEEPER_EASE_HPP