summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 02:22:43 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit5553b933f7c243b2706eb2a7989d8e33d2f6042b (patch)
tree963ad335aa5eb3983e1b2e2de115095029de829f /src
parente2daafff2ed8cc20438d05722575db69fa9ba823 (diff)
downloadsketchbook-5553b933f7c243b2706eb2a7989d8e33d2f6042b.tar.gz
sketchbook-5553b933f7c243b2706eb2a7989d8e33d2f6042b.zip
vector: morph pairs outer contours with outer ones and holes with holes
A glyph with a counter blobbed through a morph: subpaths paired by their index alone, so a hole with no partner (a 0 into a 5) collapsed onto the end of the other glyph's outline, travelling out of its own shape and leaving the counter filled, and one listed before its outline (or a shape gaining a hole) was paired with the other side's outline, its winding turned inside out. In manual's second slide the 0 became a black blob for a third of its morph into the 5. Now each pair of shapes sorts its subpaths, outer contours then holes (a hole winds against the shape's largest subpath, as a font's counters do), and pairs within each group. A hole with no partner shrinks to a point (or grows from one) where its middle stands relative to the outer contours' bounds on the other side, so it stays inside its shape as that moves. A shape without a partner, or one with no hole, morphs as before, bit for bit. Test: test/scene_morph, a ring into a disc, a 0 into a 5 and a 0 whose path lists its hole first into a ring, at a quarter, half and three quarters on the GPU and on the CPU: each hole's middle is the background (the 0 into the 5 while its hole is half its size or more), and the disc and the 5 fill it at the end. The old pairing fails it at half way on all three. What changed: the compiled test scene's equation morph (export frames 32-59, compiled-morph.png: no stray dot from a's hole, b and a keep their counters) and manual's three morphs of holed glyphs (exported frames 28.1-29.3 s, C0 into 11 000 000; 55.7-56.9 s, the 0 into the 5; 58.8-60.4 s, ADD into SUB with the row). Everything else is byte-identical: the talk's exports, every scene screenshot (manual's are taken at rest), and zig build diff's corpus, which draws no morph. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src')
-rw-r--r--src/vector/morph.zig60
1 files changed, 55 insertions, 5 deletions
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];