Commit 72fe3dbb26
Verified · cmc
Layout: unified · split
Packages/FoldKit/Sources/Notation/DiagramBuilder.swift +8 −3
| @@ -21,6 +21,8 @@ public enum DiagramArrow: Equatable, Sendable { | |||
| 21 | case valleyFold(from: SIMD2<Double>, over: SIMD2<Double>, to: SIMD2<Double>) | 21 | case valleyFold(from: SIMD2<Double>, over: SIMD2<Double>, to: SIMD2<Double>) |
| 22 | case mountainFold(from: SIMD2<Double>, over: SIMD2<Double>, to: SIMD2<Double>) | 22 | case mountainFold(from: SIMD2<Double>, over: SIMD2<Double>, to: SIMD2<Double>) |
| 23 | case foldAndUnfold(from: SIMD2<Double>, over: SIMD2<Double>, to: SIMD2<Double>, mountain: Bool) | 23 | case foldAndUnfold(from: SIMD2<Double>, over: SIMD2<Double>, to: SIMD2<Double>, mountain: Bool) |
| 24 | /// A crease folded before the step opened out again. | ||
| 25 | case unfold(from: SIMD2<Double>, over: SIMD2<Double>, to: SIMD2<Double>) | ||
| 24 | /// The technique's symbol at the midpoint of the step's first crease, pointing toward where | 26 | /// The technique's symbol at the midpoint of the step's first crease, pointing toward where |
| 25 | /// that axis's moving set ends. | 27 | /// that axis's moving set ends. |
| 26 | case technique(StepTechnique, at: SIMD2<Double>, toward: SIMD2<Double>) | 28 | case technique(StepTechnique, at: SIMD2<Double>, toward: SIMD2<Double>) |
| @@ -65,8 +67,9 @@ private let arcBulge = 0.3 | |||
| 65 | /// | 67 | /// |
| 66 | /// Edges whose `edges_krz:firstCreasedAtStep` is later than the step, and subdivision edges, are | 68 | /// Edges whose `edges_krz:firstCreasedAtStep` is later than the step, and subdivision edges, are |
| 67 | /// not drawn. An edge the step creases (its two faces move differently) is dashed for a valley as | 69 | /// not drawn. An edge the step creases (its two faces move differently) is dashed for a valley as |
| 68 | /// seen and dash-dot for a mountain, whatever its assignment; other `B` edges and folded creases | 70 | /// seen and dash-dot for a mountain, whatever its assignment, unless it is folded in the drawn |
| 69 | /// are solid; earlier creases flat again are light. Each edge is clipped by `visibleSegments`, and | 71 | /// frame; `B` edges and folded creases are solid; earlier creases flat again are light. An axis |
| 72 | /// folded in the drawn frame gets the unfold arrow. Each edge is clipped by `visibleSegments`, and | ||
| 70 | /// a piece coincident with one already drawn is drawn once, step creases before solid before light. | 73 | /// a piece coincident with one already drawn is drawn once, step creases before solid before light. |
| 71 | /// | 74 | /// |
| 72 | /// - Precondition: `validate(file)` is empty. | 75 | /// - Precondition: `validate(file)` is empty. |
| @@ -94,7 +97,7 @@ public func buildDiagram(file: FoldFile, step: FoldStep) -> Diagram { | |||
| 94 | candidates.append((e, .solid, 1)) | 97 | candidates.append((e, .solid, 1)) |
| 95 | } else if created == nil { | 98 | } else if created == nil { |
| 96 | continue | 99 | continue |
| 97 | } else if let axis = creasing[e] { | 100 | } else if let axis = creasing[e], abs(frame.edgesFoldAngle[e]) <= 1e-9 { |
| 98 | candidates.append((e, valley[axis] ? .dashed : .dashDot, 0)) | 101 | candidates.append((e, valley[axis] ? .dashed : .dashDot, 0)) |
| 99 | } else if abs(frame.edgesFoldAngle[e]) > 1e-9 { | 102 | } else if abs(frame.edgesFoldAngle[e]) > 1e-9 { |
| 100 | candidates.append((e, .solid, 1)) | 103 | candidates.append((e, .solid, 1)) |
| @@ -198,6 +201,8 @@ private func arrows(_ file: FoldFile, _ step: FoldStep, valley: [Bool], project: | |||
| 198 | let over = apex(from, to) | 201 | let over = apex(from, to) |
| 199 | if step.kind == .foldAndUnfold { | 202 | if step.kind == .foldAndUnfold { |
| 200 | arrows.append(.foldAndUnfold(from: from, over: over, to: to, mountain: !valley[i])) | 203 | arrows.append(.foldAndUnfold(from: from, over: over, to: to, mountain: !valley[i])) |
| 204 | } else if abs(axis.startAngle) > 1e-9 { | ||
| 205 | arrows.append(.unfold(from: from, over: over, to: to)) | ||
| 201 | } else if valley[i] { | 206 | } else if valley[i] { |
| 202 | arrows.append(.valleyFold(from: from, over: over, to: to)) | 207 | arrows.append(.valleyFold(from: from, over: over, to: to)) |
| 203 | } else { | 208 | } else { |
Packages/FoldKit/Sources/Notation/DiagramDrawing.swift +5 −1
| @@ -22,7 +22,8 @@ public func drawDiagram(_ diagram: Diagram, style: DiagramStyle, in context: CGC | |||
| 22 | var points = diagram.edges.flatMap { [$0.from, $0.to] } | 22 | var points = diagram.edges.flatMap { [$0.from, $0.to] } |
| 23 | for arrow in diagram.arrows { | 23 | for arrow in diagram.arrows { |
| 24 | switch arrow { | 24 | switch arrow { |
| 25 | case let .valleyFold(from, over, to), let .mountainFold(from, over, to), let .foldAndUnfold(from, over, to, _): | 25 | case let .valleyFold(from, over, to), let .mountainFold(from, over, to), let .foldAndUnfold(from, over, to, _), |
| 26 | let .unfold(from, over, to): | ||
| 26 | points += [from, over, to] | 27 | points += [from, over, to] |
| 27 | case let .technique(_, at, toward): | 28 | case let .technique(_, at, toward): |
| 28 | points += [at, toward] | 29 | points += [at, toward] |
| @@ -90,6 +91,9 @@ public func drawDiagram(_ diagram: Diagram, style: DiagramStyle, in context: CGC | |||
| 90 | drawTurnOver(context, style, centre: symbolCentre, vertical: axis == .vertical) | 91 | drawTurnOver(context, style, centre: symbolCentre, vertical: axis == .vertical) |
| 91 | case let .rotate(degrees): | 92 | case let .rotate(degrees): |
| 92 | drawRotate(context, style, centre: symbolCentre, degrees: degrees) | 93 | drawRotate(context, style, centre: symbolCentre, degrees: degrees) |
| 94 | case let .unfold(from, over, to): | ||
| 95 | drawFoldArrow(context, style, pixel(from), pixel(over), pixel(to), head: .hollowFull, returnHead: false, | ||
| 96 | tick: repeated) | ||
| 93 | case .repeatMark: | 97 | case .repeatMark: |
| 94 | break | 98 | break |
| 95 | } | 99 | } |
Packages/FoldKit/Tests/NotationTests/DiagramTests.swift +23
| @@ -106,6 +106,29 @@ private func halfWithWallRaised() throws -> (FoldFrame, FoldFile) { | |||
| 106 | .contains { if case .foldAndUnfold = $0 { true } else { false } }) | 106 | .contains { if case .foldAndUnfold = $0 { true } else { false } }) |
| 107 | } | 107 | } |
| 108 | 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 anUnfoldArrowHasAHollowHead() { | ||
| 122 | let square = [SIMD2<Double>(0, 0), SIMD2(1, 0), SIMD2(1, 1), SIMD2(0, 1)] | ||
| 123 | let edges = square.indices.map { DiagramEdge(edge: $0, from: square[$0], to: square[($0 + 1) % 4], style: .solid) } | ||
| 124 | let (from, over, to) = (SIMD2<Double>(0.2, 0.5), SIMD2<Double>(0.5, 0.8), SIMD2<Double>(0.8, 0.5)) | ||
| 125 | let valley = inkedPixels(render(Diagram(edges: edges, arrows: [.valleyFold(from: from, over: over, to: to)]))) | ||
| 126 | let unfold = inkedPixels(render(Diagram(edges: edges, arrows: [.unfold(from: from, over: over, to: to)]))) | ||
| 127 | let bare = inkedPixels(render(Diagram(edges: edges, arrows: []))) | ||
| 128 | #expect(unfold > bare) | ||
| 129 | #expect(unfold < valley) | ||
| 130 | } | ||
| 131 | |||
| 109 | @Test func turnOverStepCarriesTheTurnOverSymbol() throws { | 132 | @Test func turnOverStepCarriesTheTurnOverSymbol() throws { |
| 110 | #expect(try diagram("half.fold", step: 2).arrows.contains(.turnOver(.vertical))) | 133 | #expect(try diagram("half.fold", step: 2).arrows.contains(.turnOver(.vertical))) |
| 111 | } | 134 | } |
Packages/FoldKit/Tests/NotationTests/Snapshots/NOTATION.md +6
| @@ -15,6 +15,7 @@ points. | |||
| 15 | | Valley-fold arrow | curved arrow, filled full head | OrigamiUSA (filled head) | | 15 | | Valley-fold arrow | curved arrow, filled full head | OrigamiUSA (filled head) | |
| 16 | | Mountain-fold arrow | curved arrow, hollow half head, barb on the outside of the arc | Randlett and OrigamiUSA agree | | 16 | | Mountain-fold arrow | curved arrow, hollow half head, barb on the outside of the arc | Randlett and OrigamiUSA agree | |
| 17 | | Fold and unfold | curved arrow, fold head at the destination, hollow full head at the start pointing back | OrigamiUSA (double-headed form; the zig-zag form is not used) | | 17 | | Fold and unfold | curved arrow, fold head at the destination, hollow full head at the start pointing back | OrigamiUSA (double-headed form; the zig-zag form is not used) | |
| 18 | | Unfold | curved arrow, hollow full head at the destination, no head at the start | Randlett and OrigamiUSA agree (the unfold arrow) | | ||
| 18 | | Inside and outside reverse fold, sink, squash | hollow push arrow with a notched tail, tip on the crease | OrigamiUSA (push) | | 19 | | Inside and outside reverse fold, sink, squash | hollow push arrow with a notched tail, tip on the crease | OrigamiUSA (push) | |
| 19 | | Petal fold | valley-fold arrow lifting the flap | OrigamiUSA | | 20 | | Petal fold | valley-fold arrow lifting the flap | OrigamiUSA | |
| 20 | | Turn over | loop-the-loop arrow: left to right for a vertical axis, bottom to top for a horizontal one | Randlett and OrigamiUSA agree | | 21 | | Turn over | loop-the-loop arrow: left to right for a vertical axis, bottom to top for a horizontal one | Randlett and OrigamiUSA agree | |
| @@ -23,6 +24,11 @@ points. | |||
| 23 | 24 | ||
| 24 | Valley and mountain are as seen from the step's viewpoint, not FOLD's assignment. | 25 | Valley and mountain are as seen from the step's viewpoint, not FOLD's assignment. |
| 25 | 26 | ||
| 27 | A crease the step moves that is already folded in the frame drawn is the existing line, solid | ||
| 28 | where visible, never dashed or dash-dot: the reader is holding that fold. An axis folded in the | ||
| 29 | frame drawn, such as box step 7's corners, gets the unfold arrow instead of a valley or mountain | ||
| 30 | arrow. | ||
| 31 | |||
| 26 | Arrow geometry: a fold arrow runs from the area-weighted centroid of the axis's moving set before | 32 | Arrow geometry: a fold arrow runs from the area-weighted centroid of the axis's moving set before |
| 27 | the step to the same point after it (at the peak for a fold and unfold), its apex 0.3 of the chord | 33 | the step to the same point after it (at the peak for a fold and unfold), its apex 0.3 of the chord |
| 28 | from the chord's midpoint, toward screen-up or, for a vertical chord, screen-right. Arrows of a | 34 | from the chord's midpoint, toward screen-up or, for a vertical chord, screen-right. Arrows of a |
Packages/FoldKit/Tests/NotationTests/Snapshots/box-11.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/box-7.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/box-9.png
Binary file not shown.