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