Commit dcab232542

dcab232542ca8857b5dcb11c300d348b531bb98c

parent: 62b6d31dbc

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-23 01:24 UTC

Hide edges under a flat seam in an upper layer

An upper face overlying one of an edge's faces continues across a flat
edge along that edge into the same layer of paper, so its neighbour
there covers the seam's other side. The crane's preliminary base no
longer shows the diagonal beneath its front layer, and edges of lower
layers no longer show through bend lines or flat folds elsewhere in the
crane and the box. A test checks that the crane's neck crease is drawn
beside its push arrow (R82).

Ref #7

Layout: unified · split

Packages/FoldKit/Sources/Notation/EdgeOcclusion.swift +33 −1
@@ -9,7 +9,8 @@ import simd
99/// `order.clusters` read looking along −`eye`. A face in neither of their clusters is above the
1010/// edge where it lies nearer the eye; a face seen edge-on covers nothing. An edge on the outer
1111/// boundary of the union stays visible; one under the seam between two upper faces, covered on
12/// both sides, does not.
12/// both sides, does not. Where an upper face overlying one of the edge's faces meets a neighbour
13/// across a flat edge along the edge, the neighbour covers its side of that seam.
1314public func visibleSegments(of edge: Int, frame: FoldFrame, file: FoldFile, order: LayerOrder,
1415 viewpoint: Viewpoint) -> [(SIMD2<Double>, SIMD2<Double>)] {
1516 Occlusion(frame: frame, file: file, order: order, viewpoint: viewpoint).visibleSegments(of: edge)
@@ -42,6 +43,8 @@ struct Occlusion {
4243 let above: [[Int]]
4344 /// Each face's cluster.
4445 let cluster: [Int]
46 /// Each face's neighbours across an interior edge flat in the frame, with that edge.
47 let flatNeighbours: [[(face: Int, edge: Int)]]
4548 let positions: [SIMD3<Double>]
4649 let facesVertices: [[Int]]
4750 let normals: [SIMD3<Double>]
@@ -71,6 +74,12 @@ struct Occlusion {
7174 }
7275 self.above = above
7376 self.cluster = cluster
77 var flatNeighbours = Array(repeating: [(face: Int, edge: Int)](), count: file.facesVertices.count)
78 for (e, faces) in edgeFaces.enumerated() where faces.count == 2 && abs(frame.edgesFoldAngle[e]) <= 1e-9 {
79 flatNeighbours[faces[0]].append((faces[1], e))
80 flatNeighbours[faces[1]].append((faces[0], e))
81 }
82 self.flatNeighbours = flatNeighbours
7483 positions = frame.verticesCoords
7584 facesVertices = file.facesVertices
7685 normals = faceNormals(positions: frame.verticesCoords, faces: file.facesVertices)
@@ -89,6 +98,17 @@ struct Occlusion {
8998 guard length(b - a) > tolerance else { return [] }
9099 let occluders = Set(edgeFaces[edge].flatMap { above[$0] })
91100 var covers = occluders.sorted().compactMap { cover(a, b, by: outlines[$0]) }
101 // an occluder lying over one of the edge's faces continues across a flat edge along the
102 // edge into the same layer of paper: under that seam the neighbour covers its side too
103 let overlying = Set(edgeFaces[edge].flatMap { h in
104 above[h].filter { convexPolygonsOverlap(outlines[$0], outlines[h], tolerance: tolerance) }
105 })
106 let seamed = Set(overlying.flatMap { f in
107 flatNeighbours[f].filter { alongEdge($0.edge, edge) }.map(\.face)
108 }).subtracting(occluders).subtracting(edgeFaces[edge])
109 covers += seamed.sorted().compactMap { f in
110 cover(a, b, by: outlines[f]).flatMap { $0.side == .both ? nil : $0 }
111 }
92112 let own = Set(edgeFaces[edge].map { cluster[$0] })
93113 for f in outlines.indices where !own.contains(cluster[f]) {
94114 guard let c = cover(a, b, by: outlines[f]), let nearer = nearer(f, than: edge) else { continue }
@@ -115,6 +135,18 @@ struct Occlusion {
115135 .map { (a + $0.lowerBound * (b - a), a + $0.upperBound * (b - a)) }
116136 }
117137
138 /// Whether edge `e` lies on the line of edge `other` in the frame and shares a stretch of it.
139 private func alongEdge(_ e: Int, _ other: Int) -> Bool {
140 let a = positions[edgesVertices[other][0]], b = positions[edgesVertices[other][1]]
141 let (p, q) = (positions[edgesVertices[e][0]], positions[edgesVertices[e][1]])
142 let d = b - a, length2 = length_squared(d)
143 guard length2 > 0 else { return false }
144 let off = [p, q].map { length(cross(d, $0 - a)) / length2.squareRoot() }
145 guard off.allSatisfy({ $0 <= tolerance }) else { return false }
146 let s = dot(p - a, d) / length2, t = dot(q - a, d) / length2
147 return min(max(s, t), 1) - max(min(s, t), 0) > tolerance / length2.squareRoot()
148 }
149
118150 enum Side { case left, right, both }
119151
120152 /// The parameters of `edge` at which face `f`'s plane lies nearer the eye along the view, or
Packages/FoldKit/Tests/NotationTests/DiagramTests.swift +24
@@ -182,3 +182,27 @@ private func halfWithWallRaised() throws -> (FoldFrame, FoldFile) {
182182 }
183183 }
184184}
185
186@Test func theCranesReverseFoldsDrawTheirCreasesBesideThePushArrow() throws {
187 // R82: the neck's crease on the top layer is drawn as a step crease, and the push arrow, whose
188 // tip is on it, does not hide it
189 let crane = try ModelLibrary.model(named: "crane")
190 let full = buildDiagram(file: crane, step: try readStep(13, in: crane))
191 let creases = full.edges.filter { $0.style == .dashed || $0.style == .dashDot }
192 let others = full.edges.filter { $0.style != .dashed && $0.style != .dashDot }
193 let push = full.arrows.filter { if case .technique = $0 { true } else { false } }
194 #expect(!creases.isEmpty && push.count == 1)
195 let creaseInk = inkedPixels(render(Diagram(edges: full.edges, arrows: [])))
196 - inkedPixels(render(Diagram(edges: others, arrows: [])))
197 let creaseInkWithArrow = inkedPixels(render(Diagram(edges: full.edges, arrows: push)))
198 - inkedPixels(render(Diagram(edges: others, arrows: push)))
199 #expect(Double(creaseInkWithArrow) >= 0.9 * Double(creaseInk))
200}
201
202@Test func anEdgeUnderAFlatSeamInTheTopLayerIsHidden() throws {
203 // the crane's preliminary base: the front layer is one flat piece of paper split along the
204 // centre line by bend lines, and the layers' diagonal beneath it does not show through
205 let crane = try ModelLibrary.model(named: "crane")
206 let edges = buildDiagram(file: crane, step: try readStep(5, in: crane)).edges
207 #expect(!edges.contains { abs($0.from.x) < 1e-6 && abs($0.to.x) < 1e-6 && min($0.from.y, $0.to.y) < 0.7 })
208}
Packages/FoldKit/Tests/NotationTests/Snapshots/box-10.png

Binary file not shown.

Packages/FoldKit/Tests/NotationTests/Snapshots/box-9.png

Binary file not shown.

Packages/FoldKit/Tests/NotationTests/Snapshots/crane-10.png

Binary file not shown.

Packages/FoldKit/Tests/NotationTests/Snapshots/crane-12.png

Binary file not shown.

Packages/FoldKit/Tests/NotationTests/Snapshots/crane-5.png

Binary file not shown.

Packages/FoldKit/Tests/NotationTests/Snapshots/crane-6.png

Binary file not shown.

Packages/FoldKit/Tests/NotationTests/Snapshots/crane-7.png

Binary file not shown.

Packages/FoldKit/Tests/NotationTests/Snapshots/crane-8.png

Binary file not shown.

Packages/FoldKit/Tests/NotationTests/Snapshots/crane-9.png

Binary file not shown.