Skip to content

Commit cf33dec

Browse files
tcl3AtkinsSJ
authored andcommitted
LibWeb: Extract SLERP algorithm into its own method
No functional changes.
1 parent ab574de commit cf33dec

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Libraries/LibWeb/CSS/Interpolation.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ static RefPtr<CSSStyleValue const> interpolate_translate(DOM::Element& element,
162162
move(new_values));
163163
}
164164

165+
// https://drafts.csswg.org/css-transforms-2/#interpolation-of-decomposed-3d-matrix-values
166+
static FloatVector4 slerp(FloatVector4 const& from, FloatVector4 const& to, float delta)
167+
{
168+
auto product = from.dot(to);
169+
170+
product = clamp(product, -1.0f, 1.0f);
171+
if (fabsf(product) >= 1.0f)
172+
return from;
173+
174+
auto theta = acosf(product);
175+
auto w = sinf(delta * theta) / sqrtf(1 - (product * product));
176+
177+
return from * (cosf(delta * theta) - (product * w)) + to * w;
178+
}
179+
165180
ValueComparingRefPtr<CSSStyleValue const> interpolate_property(DOM::Element& element, PropertyID property_id, CSSStyleValue const& a_from, CSSStyleValue const& a_to, float delta, AllowDiscrete allow_discrete)
166181
{
167182
auto from = with_keyword_values_resolved(element, property_id, a_from);
@@ -555,21 +570,7 @@ RefPtr<CSSStyleValue const> interpolate_transform(DOM::Element& element, CSSStyl
555570

556571
// https://drafts.csswg.org/css-transforms-2/#interpolation-of-decomposed-3d-matrix-values
557572
static constexpr auto interpolate = [](DecomposedValues& from, DecomposedValues& to, float delta) -> DecomposedValues {
558-
auto product = clamp(from.rotation.dot(to.rotation), -1.0f, 1.0f);
559-
FloatVector4 interpolated_rotation;
560-
if (fabsf(product) == 1.0f) {
561-
interpolated_rotation = from.rotation;
562-
} else {
563-
auto theta = acos(product);
564-
auto w = sin(delta * theta) / sqrtf(1.0f - product * product);
565-
566-
for (int i = 0; i < 4; i++) {
567-
from.rotation[i] *= cos(delta * theta) - product * w;
568-
to.rotation[i] *= w;
569-
interpolated_rotation[i] = from.rotation[i] + to.rotation[i];
570-
}
571-
}
572-
573+
auto interpolated_rotation = slerp(from.rotation, to.rotation, delta);
573574
return {
574575
interpolate_raw(from.translation, to.translation, delta),
575576
interpolate_raw(from.scale, to.scale, delta),

0 commit comments

Comments
 (0)