Commit 70453b66a3

70453b66a3e873febe8f8caa7a7dfc33708f2736

parent: 8d06aa03b7

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-20 03:01 UTC

Render mesh: keep corners on straight sides in their face's triangles

Ref #4

Layout: unified · split

Packages/FoldKit/Sources/FoldCompiler/PopCompiler.swift +6
@@ -26,6 +26,12 @@ private let angleSamples = 256
2626/// their way. The close axes drive the named spine edge back to ±180 and solve the rest, as for a
2727/// rigid step, and must arrive at `after`'s coordinates.
2828///
29/// The starting guess can lead the solver to another branch of the vertex's motion. Closing the
30/// loops does not catch that: every branch is closed. The check that the close reaches `after`
31/// catches a branch that ends elsewhere; one that ends on the same coordinates with the tip
32/// outside is caught by the validator's layer-order rules on frame n, and in general by the rule
33/// on the side a moving face departs toward and arrives from.
34///
2935/// - Throws: `unresolvedCrease` when the spine names no edge or is not folded flat,
3036/// `noRigidMotion` when the flap cannot open on its spine, `popped` does not close or the close
3137/// does not reach `after`, and `popTooStrained` when the pop's strain exceeds 10%.
Packages/FoldKit/Sources/Folding/RenderMesh.swift +38 −11
@@ -44,23 +44,29 @@ public struct RenderMesh: Sendable {
4444 let origin = flat[face[corners[0]]]
4545 let u = flat[face[corners[1]]] - origin, v = flat[face[corners[2]]] - origin
4646 let det = u.x * v.y - u.y * v.x
47 func point(_ p: SIMD2<Double>) -> Int {
48 let id = points.index(of: p)
49 if anchors[id] == nil {
50 let w = points.points[id] - origin
51 anchors[id] = Anchor(corners: SIMD3(face[corners[0]], face[corners[1]], face[corners[2]]),
52 a: (w.x * v.y - w.y * v.x) / det, b: (u.x * w.y - u.y * w.x) / det)
53 }
54 return id
55 }
4756 for cell in cells {
4857 var ids: [Int] = []
4958 for p in cell {
50 let id = points.index(of: p)
59 let id = point(p)
5160 if ids.last != id && ids.first != id { ids.append(id) }
52 if anchors[id] == nil {
53 let w = points.points[id] - origin
54 anchors[id] = Anchor(corners: SIMD3(face[corners[0]], face[corners[1]], face[corners[2]]),
55 a: (w.x * v.y - w.y * v.x) / det, b: (u.x * w.y - u.y * w.x) / det)
56 }
5761 }
5862 guard ids.count >= 3 else { continue }
59 for k in 1..<(ids.count - 1) {
60 let (p, q, r) = (points.points[ids[0]], points.points[ids[k]], points.points[ids[k + 1]])
61 let area = ((q - p).x * (r - p).y - (q - p).y * (r - p).x) / 2
62 guard area > tolerance * size else { continue }
63 triangles.append((ids[0], ids[k], ids[k + 1]))
63 var fanned = fan(ids, points.points, minimumArea: tolerance * size)
64 if fanned == nil {
65 let centre = ids.reduce(SIMD2<Double>.zero) { $0 + points.points[$1] } / Double(ids.count)
66 fanned = fan(ids, around: point(centre))
67 }
68 for triangle in fanned! {
69 triangles.append(triangle)
6470 faceOf.append(f)
6571 }
6672 }
@@ -132,6 +138,27 @@ private func split(_ polygon: [SIMD2<Double>], at value: Double, axis: Int, tole
132138 return [below, above]
133139}
134140
141/// A convex cell, its corners counterclockwise, fanned from the first corner whose fan has no
142/// triangle of `minimumArea` or less; nil when every corner's fan does. A corner on a straight
143/// side would otherwise be left out of every triangle.
144private func fan(_ ids: [Int], _ points: [SIMD2<Double>], minimumArea: Double) -> [(Int, Int, Int)]? {
145 for apex in ids.indices {
146 let ring = (0..<ids.count).map { ids[(apex + $0) % ids.count] }
147 let triangles = (1..<(ring.count - 1)).map { (ring[0], ring[$0], ring[$0 + 1]) }
148 let flat = triangles.contains { a, b, c in
149 let (p, q, r) = (points[a], points[b], points[c])
150 return ((q - p).x * (r - p).y - (q - p).y * (r - p).x) / 2 <= minimumArea
151 }
152 if !flat { return triangles }
153 }
154 return nil
155}
156
157/// A convex cell fanned around an added point `centre` inside it.
158private func fan(_ ids: [Int], around centre: Int) -> [(Int, Int, Int)] {
159 ids.indices.map { (centre, ids[$0], ids[($0 + 1) % ids.count]) }
160}
161
135162/// The indices of the three corners spanning the largest triangle, the first such in index order.
136163func widestCorners(_ corners: [SIMD2<Double>]) -> [Int] {
137164 var best = [0, 1, 2], largest = -1.0
Packages/FoldKit/Tests/FoldingTests/RenderMeshTests.swift added +67
@@ -0,0 +1,67 @@
1import Foundation
2import Testing
3import simd
4import FoldFormat
5import Folding
6
7/// A flat 2 × 1 sheet with `points` as its vertices and `faces` as its faces; edges are not read
8/// by the mesh and are left empty.
9private func sheet(_ points: [SIMD2<Double>], _ faces: [[Int]]) -> FoldFile {
10 FoldFile(fileSpec: 1.2, fileAuthor: "Traditional", edgesVertices: [], facesVertices: faces,
11 edgesFirstCreasedAtStep: [],
12 frames: [FoldFrame(verticesCoords: points.map { SIMD3($0.x, $0.y, 0) }, edgesAssignment: [],
13 edgesFoldAngle: [], faceOrders: [])])
14}
15
16/// A crease along y = 0.5 split at M = (1, 0.5), and a crease from M to the top edge. M is a
17/// 180° corner of the bottom face, listed so that a fan from the face's first corner meets it
18/// collinearly.
19private func tJunction() -> FoldFile {
20 sheet([SIMD2(0, 0), SIMD2(2, 0), SIMD2(2, 0.5), SIMD2(2, 1), SIMD2(1, 1), SIMD2(0, 1), SIMD2(0, 0.5),
21 SIMD2(1, 0.5)],
22 [[2, 7, 6, 0, 1], [6, 7, 4, 5], [7, 2, 3, 4]])
23}
24
25/// Whether every mesh vertex in or on a face's outline is a corner of one of that face's triangles.
26private func everyFaceUsesItsVertices(_ file: FoldFile, _ mesh: RenderMesh) -> Bool {
27 let flat = file.frames[0].verticesCoords.map { SIMD2($0.x, $0.y) }
28 return file.facesVertices.indices.allSatisfy { f in
29 let outline = file.facesVertices[f].map { flat[$0] }
30 var used = Set<Int>()
31 for (t, (a, b, c)) in mesh.triangles.enumerated() where mesh.faceOf[t] == f { used.formUnion([a, b, c]) }
32 return mesh.vertices.indices.allSatisfy { v in
33 let p = mesh.vertices[v]
34 let inside = outline.indices.allSatisfy { i in
35 let a = outline[i], side = outline[(i + 1) % outline.count] - a, offset = p - a
36 return side.x * offset.y - side.y * offset.x >= -1e-12
37 }
38 return !inside || used.contains(v)
39 }
40 }
41}
42
43@Test(arguments: [10.0, 0.3, 2.0 / 32])
44func aStraightCornerIsInItsFacesTriangles(_ spacing: Double) {
45 let file = tJunction()
46 #expect(everyFaceUsesItsVertices(file, RenderMesh(file: file, spacing: spacing)))
47}
48
49@Test func aNonRigidStepOverAStraightCornerBuilds() {
50 // a crease along y = 0.5 split at M, straight in both faces that hold it, and a crease at
51 // x = 1.5; the right top face is held, the rest move, so M is a free vertex of the pop
52 let m = SIMD2(1.03, 0.5)
53 let file = sheet([SIMD2(0, 0), SIMD2(1.5, 0), SIMD2(2, 0), SIMD2(2, 0.5), SIMD2(2, 1), SIMD2(1.5, 1),
54 SIMD2(0, 1), SIMD2(0, 0.5), m, SIMD2(1.5, 0.5)],
55 [[9, 8, 7, 0, 1], [7, 8, 9, 5, 6], [1, 2, 3, 9], [9, 3, 4, 5]])
56 let start = file.frames[0].verticesCoords
57 let step = FoldStep(index: 1, kind: .fold, instruction: "Pop.",
58 axes: [FoldAxis(edge: 0, origin: .zero, direction: SIMD3(1, 0, 0), movingFaces: [0, 1, 2],
59 startAngle: 0, endAngle: 0)],
60 nonRigid: true,
61 pop: Pop(spine: [], openAngle: 30, phases: (1.0 / 3)...(2.0 / 3), openAxes: [],
62 popped: start, closeAxes: []),
63 viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0)))
64 let i = FoldInterpolator(file: file, step: step)
65 #expect(everyFaceUsesItsVertices(file, i.mesh))
66 #expect(zip(i.positions(at: 0.5), start).allSatisfy { simd_distance($0, $1) < 1e-9 })
67}