//! Interpolation over plain data. `lerp` works field by field on any struct, //! array, vector or number: numbers blend, and everything else (bools, enums, //! unions) switches to `b` once `t` reaches 1. Pair it with an easing: //! //! camera = tween.lerp(from, to, tween.ease.inOutCubic(t)); const std = @import("std"); pub fn lerp(a: anytype, b: @TypeOf(a), t: f32) @TypeOf(a) { const T = @TypeOf(a); return switch (@typeInfo(T)) { .float => a + (b - a) * @as(T, @floatCast(t)), .comptime_float => a + (b - a) * t, .int => @intFromFloat(@round(@as(f64, @floatFromInt(a)) + (@as(f64, @floatFromInt(b)) - @as(f64, @floatFromInt(a))) * t)), .vector => |v| if (@typeInfo(v.child) == .float) a + (b - a) * @as(T, @splat(@floatCast(t))) else blk: { var out: T = undefined; inline for (0..v.len) |i| out[i] = lerp(a[i], b[i], t); break :blk out; }, .array => blk: { var out: T = undefined; for (&out, a, b) |*o, x, y| o.* = lerp(x, y, t); break :blk out; }, .@"struct" => |s| blk: { var out: T = undefined; inline for (s.field_names) |name| @field(out, name) = lerp(@field(a, name), @field(b, name), t); break :blk out; }, else => if (t >= 1) b else a, }; } /// Like `lerp`, but only the fields named in `names` (a tuple of enum /// literals, `.{ .x, .zoom }`) move; every other field stays as in `a`. pub fn lerpFields(a: anytype, b: @TypeOf(a), t: f32, comptime names: anytype) @TypeOf(a) { var out = a; inline for (names) |name| { const field = @tagName(name); @field(out, field) = lerp(@field(a, field), @field(b, field), t); } return out; } /// Moves the fields that `target` names toward its values: the literal is /// the list, so `toward(sprite, .{ .x = 100, .alpha = 0 }, t)` animates just /// those two, converting each value to the field's own type. pub fn toward(a: anytype, target: anytype, t: f32) @TypeOf(a) { const T = @TypeOf(a); var out = a; inline for (@typeInfo(@TypeOf(target)).@"struct".field_names) |name| { if (!@hasField(T, name)) @compileError(@typeName(T) ++ " has no field '" ++ name ++ "' to animate"); const F = @FieldType(T, name); const goal: F = switch (@typeInfo(F)) { .@"struct" => toward(@field(a, name), @field(target, name), 1), else => @field(target, name), }; @field(out, name) = lerp(@field(a, name), goal, t); } return out; } /// Where `t` sits between `start` and `end`, clamped to 0..1. pub fn progress(t: f32, start: f32, end: f32) f32 { if (end <= start) return if (t >= end) 1 else 0; return std.math.clamp((t - start) / (end - start), 0, 1); } /// Easing curves, 0 to 1 over 0 to 1. Plain functions, so any `fn (f32) f32` /// can stand in for one. pub const ease = struct { pub fn linear(t: f32) f32 { return t; } pub fn inQuad(t: f32) f32 { return t * t; } pub fn outQuad(t: f32) f32 { return 1 - (1 - t) * (1 - t); } pub fn inOutQuad(t: f32) f32 { return if (t < 0.5) 2 * t * t else 1 - std.math.pow(f32, -2 * t + 2, 2) / 2; } pub fn inCubic(t: f32) f32 { return t * t * t; } pub fn outCubic(t: f32) f32 { return 1 - std.math.pow(f32, 1 - t, 3); } pub fn inOutCubic(t: f32) f32 { return if (t < 0.5) 4 * t * t * t else 1 - std.math.pow(f32, -2 * t + 2, 3) / 2; } pub fn inOutSine(t: f32) f32 { return -(@cos(std.math.pi * t) - 1) / 2; } pub fn outBack(t: f32) f32 { const c1 = 1.70158; const c3 = c1 + 1; return 1 + c3 * std.math.pow(f32, t - 1, 3) + c1 * std.math.pow(f32, t - 1, 2); } pub fn outElastic(t: f32) f32 { if (t <= 0 or t >= 1) return std.math.clamp(t, 0, 1); return std.math.pow(f32, 2, -10 * t) * @sin((t * 10 - 0.75) * (2 * std.math.pi / 3.0)) + 1; } /// manim's default: smooth start and stop. pub fn smooth(t: f32) f32 { const s = std.math.clamp(t, 0, 1); return s * s * s * (s * (s * 6 - 15) + 10); } /// Out and back again, for pulses. pub fn thereAndBack(t: f32) f32 { return smooth(if (t < 0.5) 2 * t else 2 * (1 - t)); } }; test "comptime field lists pick what animates" { const Sprite = struct { x: f32 = 0, y: f32 = 0, alpha: f32 = 1, frame: u8 = 3, pos: struct { u: f32 = 0 } = .{} }; const a: Sprite = .{}; const b: Sprite = .{ .x = 10, .y = 10, .alpha = 0 }; try std.testing.expectEqual(Sprite{ .x = 5, .y = 0, .alpha = 0.5 }, lerpFields(a, b, 0.5, .{ .x, .alpha })); try std.testing.expectEqual(Sprite{ .x = 50, .alpha = 0.5, .frame = 3, .pos = .{ .u = 2 } }, toward(a, .{ .x = 100, .alpha = 0, .pos = .{ .u = 4 } }, 0.5)); } test "lerp walks plain data field by field" { const Camera = struct { x: f32, y: f32, zoom: f32 = 1, mode: enum { free, locked } = .free }; const mid = lerp(Camera{ .x = 0, .y = 10 }, Camera{ .x = 10, .y = 20, .zoom = 3, .mode = .locked }, 0.5); try std.testing.expectEqual(Camera{ .x = 5, .y = 15, .zoom = 2, .mode = .free }, mid); try std.testing.expectEqual(@Vector(2, f32){ 1, 2 }, lerp(@Vector(2, f32){ 0, 0 }, .{ 2, 4 }, 0.5)); try std.testing.expectEqual([2]i32{ 5, -5 }, lerp([2]i32{ 0, 0 }, .{ 10, -10 }, 0.5)); try std.testing.expectEqual(@as(f32, 1), ease.smooth(1)); try std.testing.expectEqual(@as(f32, 0.5), progress(1.5, 1, 2)); }