guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
easing.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/motion/easing.hpp"
24
25#include <algorithm>
26#include <cmath>
27
28namespace guillaume::motion
29{
30 namespace
31 {
39 float bezierComponent(float u, float p1, float p2)
40 {
41 const float oneMinusU = 1.0f - u;
42 return 3.0f * oneMinusU * oneMinusU * u * p1
43 + 3.0f * oneMinusU * u * u * p2 + u * u * u;
44 }
45 } // namespace
46
47 float cubicBezier(float x, float x1, float y1, float x2, float y2)
48 {
49 const float target = std::clamp(x, 0.0f, 1.0f);
50
51 // Find the parameter u whose x coordinate matches the target, using a
52 // bisection that is stable and precise enough for animation.
53 float low = 0.0f;
54 float high = 1.0f;
55 float u = target;
56
57 for (int i = 0; i < 32; ++i) {
58 u = (low + high) * 0.5f;
59 const float currentX = bezierComponent(u, x1, x2);
60 if (currentX < target) {
61 low = u;
62 } else {
63 high = u;
64 }
65 }
66
67 return bezierComponent(u, y1, y2);
68 }
69
70 float applyEasing(Easing easing, float t)
71 {
72 const float clamped = std::clamp(t, 0.0f, 1.0f);
73
74 switch (easing) {
75 case Easing::Linear:
76 return clamped;
77 case Easing::Decelerate:
78 return cubicBezier(clamped, 0.0f, 0.0f, 0.0f, 1.0f);
79 case Easing::Accelerate:
80 return cubicBezier(clamped, 0.3f, 0.0f, 1.0f, 1.0f);
81 case Easing::Emphasized:
82 case Easing::EmphasizedDecelerate:
83 return cubicBezier(clamped, 0.05f, 0.7f, 0.1f, 1.0f);
84 case Easing::EmphasizedAccelerate:
85 return cubicBezier(clamped, 0.3f, 0.0f, 0.8f, 0.15f);
86 case Easing::Standard:
87 default:
88 return cubicBezier(clamped, 0.2f, 0.0f, 0.0f, 1.0f);
89 }
90 }
91
92} // namespace guillaume::motion