diff options
| -rw-r--r-- | build.zig | 5 | ||||
| -rw-r--r-- | src/vector/morph.zig | 60 | ||||
| -rw-r--r-- | test/scene_morph.script | 46 | ||||
| -rw-r--r-- | test/scene_morph.zig | 25 |
4 files changed, 131 insertions, 5 deletions
@@ -271,6 +271,11 @@ pub fn build(b: *Build) void { compiled_script.addPrefixedDirectoryArg("frames=", compiled.app.addExport(compiled_exe, .{ .fps = 30, .size = .{ 640, 360 }, .frames = 200 }).frames); test_step.dependOn(&compiled_script.step); test_anim.dependOn(&compiled_script.step); + // Holes through morphs, on the GPU and the CPU renderer (vector/morph.zig). + const morph = sb.addScene(.{ .root_source_file = b.path("test/scene_morph.zig") }); + const morph_script = morph.app.addScript(morph.app.addExecutable(), b.path("test/scene_morph.script"), .{}); + test_step.dependOn(&morph_script.step); + test_anim.dependOn(&morph_script.step); // The talk example: a Deck in a sketch's own State, stepped, sent back, // jumped by slides, clicked on its slider, reloaded mid-wait with its diff --git a/src/vector/morph.zig b/src/vector/morph.zig index f285c7e..a05037d 100644 --- a/src/vector/morph.zig +++ b/src/vector/morph.zig @@ -2,7 +2,9 @@ //! in order; each pair's subpaths, then each subpath's cubics, are made equal //! in number by splitting the longest curves; then every control point and //! color is interpolated. A shape without a partner collapses to (or grows -//! from) the other drawing's center. +//! from) the other drawing's center. Outer contours pair with outer ones +//! and holes with holes, so a glyph's counter stays a counter; a hole +//! without a partner shrinks to nothing (or grows) inside its shape. const std = @import("std"); const geom = @import("math"); const Drawing = @import("Drawing.zig"); @@ -22,10 +24,14 @@ pub fn between(arena: std.mem.Allocator, a: Drawing, ma: geom.Affine, b: Drawing // The missing side is the other side, shrunk to a point. const pa = try placed(arena, a, sa, ma, sb, b, mb, cb); const pb = try placed(arena, b, sb, mb, sa, a, ma, ca); - const count = @max(pa.len, pb.len); - for (0..count) |k| { - const from = try pad(arena, pa, k, if (pa.len > 0) lastPoint(pa) else ca); - const to = try pad(arena, pb, k, if (pb.len > 0) lastPoint(pb) else cb); + // Outer contours pair up, then holes; a missing shape's subpaths + // are its partner's, in order. + const ha = if (sa != null and sb != null) try holesLast(arena, pa) else 0; + const hb = if (sa != null and sb != null) try holesLast(arena, pb) else 0; + const outers = @max(pa.len - ha, pb.len - hb); + for (0..outers + @max(ha, hb)) |k| { + const from = try side(arena, pa, ha, pb, hb, k, outers, ca); + const to = try side(arena, pb, hb, pa, ha, k, outers, cb); const aligned = try align2(arena, from.cubics, to.cubics); for (aligned[0], aligned[1]) |x, y| { var c: Cubic = undefined; @@ -78,6 +84,50 @@ fn pad(arena: std.mem.Allocator, paths: []const Path, k: usize, point: Vec) !Pat return .{ .cubics = one, .closed = false }; } +/// Puts the holes of `paths` (wound against the largest, as a font's +/// counters are) after its outer contours, each in order; returns how many. +fn holesLast(arena: std.mem.Allocator, paths: []Path) !usize { + const areas = try arena.alloc(f32, paths.len); + var main: f32 = 0; + for (areas, paths) |*area, p| { + area.* = geom.cross(p.cubics[p.cubics.len - 1][3], p.cubics[0][0]); + for (p.cubics) |c| area.* += geom.cross(c[0], c[1]) + geom.cross(c[1], c[2]) + geom.cross(c[2], c[3]); + if (@abs(area.*) > @abs(main)) main = area.*; + } + const all = try arena.dupe(Path, paths); + var n: usize = 0; + var holes: usize = 0; + for ([_]bool{ false, true }) |hole| for (all, areas) |p, area| if ((area * main < 0) == hole) { + paths[n] = p; + n += 1; + holes += @intFromBool(hole); + }; + return holes; +} + +/// One side's path for pair `k`: its outer contours, then its holes. One it +/// lacks is a point: an outer contour's where its last one ends (or at its +/// drawing's `middle`); a hole's where the other side's hole has its middle, +/// relative to the outer contours' bounds on each side, so that a hole with +/// no partner shrinks to nothing (or grows) inside its shape, moving with it. +fn side(arena: std.mem.Allocator, own: []const Path, holes: usize, other: []const Path, other_holes: usize, k: usize, outers: usize, middle: Vec) !Path { + const shell = own[0 .. own.len - holes]; + if (k < outers) return pad(arena, shell, k, if (shell.len > 0) lastPoint(shell) else middle); + const j = k - outers; + if (j < holes) return own[own.len - holes + j]; + const there = extent(other[0 .. other.len - other_holes]); + const here = extent(shell); + const hole = extent(other[other.len - other_holes + j ..][0..1]); + const at = ((hole.min + hole.max) / geom.splat(2) - there.min) / (there.max - there.min); + return pad(arena, &.{}, 0, here.min + at * (here.max - here.min)); +} + +fn extent(paths: []const Path) geom.Bounds { + var b: geom.Bounds = .{}; + for (paths) |p| for (p.cubics) |c| for (c) |q| b.add(q); + return b; +} + fn lastPoint(paths: []const Path) Vec { const last = paths[paths.len - 1].cubics; return last[last.len - 1][3]; diff --git a/test/scene_morph.script b/test/scene_morph.script new file mode 100644 index 0000000..0da4670 --- /dev/null +++ b/test/scene_morph.script @@ -0,0 +1,46 @@ +# Holes through morphs (test/scene_morph.zig), a quarter, half and three +# quarters of the way, on the GPU and then on the CPU: the middle of each +# hole is the background, the ring around it drawn (the 0 into the 5 while +# its hole is half its size or more); at the end the disc fills it. +console set deck.playing false +console set deck.t 1 +screenshot .zig-cache/anim/morph-1.png +expect-pixel .zig-cache/anim/morph-1.png 300 360 255 255 255 255 1 +expect-pixel .zig-cache/anim/morph-1.png 240 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-1.png 640 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-1.png 1040 360 28 28 31 255 1 +console set deck.t 2 +screenshot .zig-cache/anim/morph-2.png +expect-pixel .zig-cache/anim/morph-2.png 300 360 255 255 255 255 1 +expect-pixel .zig-cache/anim/morph-2.png 240 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-2.png 640 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-2.png 1040 360 28 28 31 255 1 +console set deck.t 3 +screenshot .zig-cache/anim/morph-3.png +expect-pixel .zig-cache/anim/morph-3.png 300 360 255 255 255 255 1 +expect-pixel .zig-cache/anim/morph-3.png 240 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-3.png 1040 360 28 28 31 255 1 +key b +expect backend cpu +console set deck.t 1 +screenshot .zig-cache/anim/morph-cpu-1.png +expect-pixel .zig-cache/anim/morph-cpu-1.png 300 360 255 255 255 255 1 +expect-pixel .zig-cache/anim/morph-cpu-1.png 240 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-cpu-1.png 640 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-cpu-1.png 1040 360 28 28 31 255 1 +console set deck.t 2 +screenshot .zig-cache/anim/morph-cpu-2.png +expect-pixel .zig-cache/anim/morph-cpu-2.png 300 360 255 255 255 255 1 +expect-pixel .zig-cache/anim/morph-cpu-2.png 240 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-cpu-2.png 640 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-cpu-2.png 1040 360 28 28 31 255 1 +console set deck.t 3 +screenshot .zig-cache/anim/morph-cpu-3.png +expect-pixel .zig-cache/anim/morph-cpu-3.png 300 360 255 255 255 255 1 +expect-pixel .zig-cache/anim/morph-cpu-3.png 240 360 28 28 31 255 1 +expect-pixel .zig-cache/anim/morph-cpu-3.png 1040 360 28 28 31 255 1 +console set deck.t 4 +screenshot .zig-cache/anim/morph-cpu-4.png +expect-pixel .zig-cache/anim/morph-cpu-4.png 240 360 255 255 255 255 1 +expect-pixel .zig-cache/anim/morph-cpu-4.png 640 360 255 255 255 255 1 +expect-pixel .zig-cache/anim/morph-cpu-4.png 1040 360 28 28 31 255 1 diff --git a/test/scene_morph.zig b/test/scene_morph.zig new file mode 100644 index 0000000..fbde51d --- /dev/null +++ b/test/scene_morph.zig @@ -0,0 +1,25 @@ +//! Holes through a morph (vector/morph.zig), for test/scene_morph.script: +//! a ring into a disc, a 0 into a 5, and a 0 whose path has its hole first +//! into a ring, side by side, together over 4 s at a steady pace. +const anim = @import("anim"); + +pub const scene = anim.Scene(construct); + +const disc = "M 40 0 C 40 22 22 40 0 40 C -22 40 -40 22 -40 0 C -40 -22 -22 -40 0 -40 C 22 -40 40 -22 40 0 Z "; +const ring_hole = "M 20 0 C 20 -11 11 -20 0 -20 C -11 -20 -20 -11 -20 0 C -20 11 -11 20 0 20 C 11 20 20 11 20 0 Z "; +const zero = "M 0 -45 C 25 -45 30 -20 30 0 C 30 20 25 45 0 45 C -25 45 -30 20 -30 0 C -30 -20 -25 -45 0 -45 Z "; +const zero_hole = "M 0 -25 C -12 -25 -14 -12 -14 0 C -14 12 -12 25 0 25 C 12 25 14 12 14 0 C 14 -12 12 -25 0 -25 Z "; +const five = "M -25 -45 H 25 V -33 H -13 V -8 H 5 L 20 -3 L 28 10 V 25 L 20 40 L 5 45 H -25 V 33 H 5 L 14 25 V 10 L 5 4 H -25 Z "; + +const head = "<svg viewBox=\"-50 -50 100 100\" width=\"100\" height=\"100\"><path fill=\"white\" d=\""; +const tail = "\"/></svg>"; + +fn construct(s: *anim.Builder) void { + const pairs = .{ .{ disc ++ ring_hole, disc, -400 }, .{ zero ++ zero_hole, five, 0 }, .{ zero_hole ++ zero, disc ++ ring_hole, 400 } }; + var morphs: [pairs.len]anim.Animation = undefined; + inline for (pairs, &morphs) |p, *m| { + const from = s.svg(head ++ p[0] ++ tail, .{ .x = p[2], .scale = 2, .visible = true }); + m.* = from.transformInto(s.svg(head ++ p[1] ++ tail, .{ .x = p[2], .scale = 2 })); + } + s.play(morphs, .{ .seconds = 4, .ease = anim.ease.linear }); +} |
