Packages/FoldKit/Tests/NotationTests/DiagramTests.swift
208 lines · 10907 bytes
25 symbols in this file
valleyCreaseOfThisStepIsDashedmountainCreaseOfThisStepIsDashDotvalleyAndMountainAreAsSeenNotAsAssignedanEdgeUnderASeamIsHiddenboundaryEdgesAreSolidaCreaseMadeEarlierAndFlatAgainIsLightcreasesNotYetMetAreNotDrawnAtAlledgesOccludedByLayersAboveThemAreOmittedanEdgePartlyOccludedIsClippedNotDroppedanEdgeBehindAFaceOfAnotherClusterIsHiddenanEdgeInFrontOfAFaceOfAnotherClusterStaysVisiblehalfWithWallRaisedaPlainMultiAxisStepGetsOneArrowPerAxisaTechniqueStepGetsItsOwnArrowsNotOnePerAxisfoldAndUnfoldStepGetsTheReturnHeadanUnfoldIsDrawnOnTheExistingLinesWithTheUnfoldArrowanAxisAlreadyFoldedThatFoldsFurtherGetsAFoldArrowNotAnUnfoldArrowanUnfoldArrowHasAHollowHeadturnOverStepCarriesTheTurnOverSymbolrotateStepCarriesItsDegreesaRepeatedStepCarriesTheRepeatMarkaRepeatedPushArrowCarriesTheRepeatStrokeeveryStepMatchesItsGoldenImagetheCranesReverseFoldsDrawTheirCreasesBesideThePushArrowanEdgeUnderAFlatSeamInTheTopLayerIsHidden
1import Testing
2import FoldFormat
3import Folding
4import Models
5import Notation
6import FoldKitTestSupport
7
8@Test func valleyCreaseOfThisStepIsDashed() throws {
9 #expect(try diagram("half.fold", step: 1).edges.filter { $0.style == .dashed }.map(\.edge) == [6])
10}
11
12@Test func mountainCreaseOfThisStepIsDashDot() throws {
13 #expect(try diagram("mountain.fold", step: 1).edges.filter { $0.style == .dashDot }.map(\.edge) == [6])
14}
15
16@Test func valleyAndMountainAreAsSeenNotAsAssigned() throws {
17 // quarters step 3 folds both layers; the top layer is flipped, so its edge e9 is
18 // assigned M, but the fold is a valley as seen and nothing is dash-dot
19 let d = try diagram("quarters.fold", step: 3)
20 #expect(d.edges.contains { $0.style == .dashed })
21 #expect(!d.edges.contains { $0.style == .dashDot })
22 #expect(d.arrows.contains { if case .valleyFold = $0 { true } else { false } })
23}
24
25@Test func anEdgeUnderASeamIsHidden() throws {
26 // quarters step 3 draws frame 2: e8 lies under the seam between f3 and f2
27 #expect(try !diagram("quarters.fold", step: 3).edges.contains { $0.edge == 8 })
28 #expect(try diagram("quarters.fold", step: 3).edges.contains { $0.style == .dashed })
29}
30
31@Test func boundaryEdgesAreSolid() throws {
32 #expect(try diagram("half.fold", step: 1).edges.filter { $0.style == .solid }.count == 6)
33}
34
35@Test func aCreaseMadeEarlierAndFlatAgainIsLight() throws {
36 // quarters step 2 draws frame 1, after the fold-and-unfold on x = 1
37 #expect(Set(try diagram("quarters.fold", step: 2).edges.filter { $0.style == .light }.map(\.edge)) == [8, 9])
38}
39
40@Test func creasesNotYetMetAreNotDrawnAtAll() throws {
41 // frame 0 carries the y = 1 creases too; step 1's diagram must not show them
42 #expect(try diagram("quarters.fold", step: 1).edges.allSatisfy { ![10, 11].contains($0.edge) })
43 #expect(try diagram("quarters.fold", step: 1).edges.contains { $0.style == .dashed })
44}
45
46@Test func edgesOccludedByLayersAboveThemAreOmitted() throws {
47 // occlusion step 3 draws frame 2: f0's raw edge from (2,2) to (2,0) is under the strip
48 #expect(try !diagram("occlusion.fold", step: 3).edges.contains { $0.edge == 0 })
49 #expect(try diagram("occlusion.fold", step: 3).edges.contains { $0.style == .solid })
50}
51
52@Test func anEdgePartlyOccludedIsClippedNotDropped() throws {
53 // f0's raw edge from (0,2) to (2,2) is covered for x in [1, 2]
54 let segments = try visibleSegmentsOf("occlusion.fold", edge: 7, inFrame: 2)
55 #expect(segments.count == 1)
56 #expect(abs(segmentLength(segments[0]) - 1) < 1e-9)
57}
58
59@Test func anEdgeBehindAFaceOfAnotherClusterIsHidden() throws {
60 // half.fold's right half stood up at x = 1; seen from the +x side, the wall hides the far
61 // raw edge x = 0, and seen from the −x side it does not
62 let (frame, file) = try halfWithWallRaised()
63 let order = try LayerOrder(faceOrders: [], normals: faceNormals(frame, file))
64 let behind = visibleSegments(of: 5, frame: frame, file: file, order: order,
65 viewpoint: Viewpoint(eye: SIMD3(1, 0, 0.5), up: SIMD3(0, 0, 1)))
66 #expect(behind.isEmpty)
67 let inFront = visibleSegments(of: 5, frame: frame, file: file, order: order,
68 viewpoint: Viewpoint(eye: SIMD3(-1, 0, 0.5), up: SIMD3(0, 0, 1)))
69 #expect(inFront.count == 1)
70 #expect(abs(segmentLength(inFront[0]) - 2) < 1e-9)
71}
72
73@Test func anEdgeInFrontOfAFaceOfAnotherClusterStaysVisible() throws {
74 // the wall's top edge, seen from the +x side, is nearer the eye than the floor behind it
75 let (frame, file) = try halfWithWallRaised()
76 let order = try LayerOrder(faceOrders: [], normals: faceNormals(frame, file))
77 let top = visibleSegments(of: 2, frame: frame, file: file, order: order,
78 viewpoint: Viewpoint(eye: SIMD3(1, 0, 0.5), up: SIMD3(0, 0, 1)))
79 #expect(top.count == 1)
80 #expect(abs(segmentLength(top[0]) - 2) < 1e-9)
81}
82
83/// half.fold's frame 0 with f1 turned 90° about x = 1, up to z = 1.
84private func halfWithWallRaised() throws -> (FoldFrame, FoldFile) {
85 let file = try FoldFile(contentsOf: fixture("half.fold"))
86 var frame = file.frames[0]
87 frame.verticesCoords[2] = SIMD3(1, 0, 1)
88 frame.verticesCoords[3] = SIMD3(1, 2, 1)
89 return (frame, file)
90}
91
92@Test func aPlainMultiAxisStepGetsOneArrowPerAxis() throws {
93 let arrows = try diagram("letter.fold", step: 1).arrows
94 #expect(arrows.filter { if case .valleyFold = $0 { true } else { false } }.count == 1)
95 #expect(arrows.filter { if case .mountainFold = $0 { true } else { false } }.count == 1)
96}
97
98@Test func aTechniqueStepGetsItsOwnArrowsNotOnePerAxis() throws {
99 let arrows = try diagram("letter-technique.fold", step: 1).arrows
100 #expect(arrows.filter { if case .technique(.insideReverse, _, _) = $0 { true } else { false } }.count == 1)
101 #expect(!arrows.contains { if case .valleyFold = $0 { true } else if case .mountainFold = $0 { true } else { false } })
102}
103
104@Test func foldAndUnfoldStepGetsTheReturnHead() throws {
105 #expect(try diagram("quarters.fold", step: 1).arrows
106 .contains { if case .foldAndUnfold = $0 { true } else { false } })
107}
108
109@Test func anUnfoldIsDrawnOnTheExistingLinesWithTheUnfoldArrow() throws {
110 // box step 7 unfolds the top and bottom corners, folded at 180 in frame 6: their creases are
111 // drawn as they stand, not as new folds, and each axis gets the unfold arrow
112 let box = try ModelLibrary.model(named: "box")
113 let d = buildDiagram(file: box, step: try readStep(7, in: box))
114 #expect(!d.edges.contains { $0.style == .dashed || $0.style == .dashDot })
115 #expect(d.edges.filter { [25, 77].contains($0.edge) }.allSatisfy { $0.style == .solid })
116 #expect(Set(d.edges.map(\.edge)).isSuperset(of: [25, 77]))
117 #expect(d.arrows.count == 2)
118 #expect(d.arrows.allSatisfy { if case .unfold = $0 { true } else { false } })
119}
120
121@Test func anAxisAlreadyFoldedThatFoldsFurtherGetsAFoldArrowNotAnUnfoldArrow() throws {
122 // half.fold with f1 already stood up at 90° about e6, then folded on to 180: e6 stays the
123 // existing line, and the arrow is the valley fold as seen from the oblique eye
124 var (frame, file) = try halfWithWallRaised()
125 frame.edgesFoldAngle[6] = 90
126 file.frames[0] = frame
127 let flat = try readStep(1, in: FoldFile(contentsOf: fixture("half.fold"))).axes[0]
128 let axis = FoldAxis(edge: flat.edge, origin: flat.origin, direction: flat.direction,
129 movingFaces: flat.movingFaces, startAngle: 90, endAngle: 180)
130 let step = FoldStep(index: 1, kind: .fold, instruction: "Fold the wall down.", axes: [axis],
131 viewpoint: Viewpoint(eye: SIMD3(-1, 0, 1), up: SIMD3(0, 1, 0)))
132 let d = buildDiagram(file: file, step: step)
133 #expect(d.edges.filter { $0.edge == 6 }.allSatisfy { $0.style == .solid })
134 #expect(d.arrows.count == 1)
135 #expect(d.arrows.allSatisfy { if case .valleyFold = $0 { true } else { false } })
136}
137
138@Test func anUnfoldArrowHasAHollowHead() {
139 let square = [SIMD2<Double>(0, 0), SIMD2(1, 0), SIMD2(1, 1), SIMD2(0, 1)]
140 let edges = square.indices.map { DiagramEdge(edge: $0, from: square[$0], to: square[($0 + 1) % 4], style: .solid) }
141 let (from, over, to) = (SIMD2<Double>(0.2, 0.5), SIMD2<Double>(0.5, 0.8), SIMD2<Double>(0.8, 0.5))
142 let valley = inkedPixels(render(Diagram(edges: edges, arrows: [.valleyFold(from: from, over: over, to: to)])))
143 let unfold = inkedPixels(render(Diagram(edges: edges, arrows: [.unfold(from: from, over: over, to: to)])))
144 let bare = inkedPixels(render(Diagram(edges: edges, arrows: [])))
145 #expect(unfold > bare)
146 #expect(unfold < valley)
147}
148
149@Test func turnOverStepCarriesTheTurnOverSymbol() throws {
150 #expect(try diagram("half.fold", step: 2).arrows.contains(.turnOver(.vertical)))
151}
152
153@Test func rotateStepCarriesItsDegrees() throws {
154 #expect(try diagram("half.fold", step: 3).arrows.contains(.rotate(degrees: 90)))
155}
156
157@Test func aRepeatedStepCarriesTheRepeatMark() throws {
158 #expect(try diagram("letter-sequential.fold", step: 2).arrows.contains(.repeatMark(of: 1)))
159}
160
161@Test func aRepeatedPushArrowCarriesTheRepeatStroke() {
162 let square = [SIMD2<Double>(0, 0), SIMD2(1, 0), SIMD2(1, 1), SIMD2(0, 1)]
163 let edges = square.indices.map { DiagramEdge(edge: $0, from: square[$0], to: square[($0 + 1) % 4], style: .solid) }
164 let push = DiagramArrow.technique(.insideReverse, at: SIMD2(0.5, 0.5), toward: SIMD2(1, 0.5))
165 let once = render(Diagram(edges: edges, arrows: [push]))
166 let repeated = render(Diagram(edges: edges, arrows: [push, .repeatMark(of: 1)]))
167 #expect(inkedPixels(repeated) > inkedPixels(once))
168}
169
170@Test func everyStepMatchesItsGoldenImage() throws {
171 for name in ["half", "mountain", "letter", "letter-sequential", "letter-staggered", "letter-technique", "quarters",
172 "occlusion"] {
173 let file = try FoldFile(contentsOf: fixture("\(name).fold"))
174 for step in try readSteps(file) {
175 try expectSnapshot(render(buildDiagram(file: file, step: step)), named: "\(name)-\(step.index)")
176 }
177 }
178 for name in ["cup", "box", "sampan", "crane"] {
179 let model = try ModelLibrary.model(named: name)
180 for step in try readSteps(model) {
181 try expectSnapshot(render(buildDiagram(file: model, step: step)), named: "\(name)-\(step.index)")
182 }
183 }
184}
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}