Packages/FoldKit/Tests/FoldCompilerTests/CollinearAxisTests.swift

00ea029d0652da12d0f4237769e7020b72cfa7f3
fold/Packages/FoldKit/Tests/FoldCompilerTests/CollinearAxisTests.swift history · blame · raw

90 lines · 5152 bytes

7 symbols in this file
 1import Testing
 2@testable import FoldCompiler
 3import FoldFormat
 4import Folding
 5import Foundation
 6
 7@Test func aSecondFoldAndUnfoldAxisWithCollinearEdgesTurnsBackAtItsPeak() throws {
 8    let file = try compile(strips(.foldAndUnfold), pattern: stripsPattern())
 9    let step = try readStep(1, in: file)
10    for axis in step.axes {
11        for k in 0...256 {
12            let angle = axis.angle(at: Double(k) / 256, kind: step.kind)
13            #expect(angle >= -1e-9 && angle <= 180 + 1e-9, "edge \(axis.edge) t \(Double(k) / 256)")
14        }
15        #expect(abs(axis.angle(at: 0.5, kind: step.kind) - 180) < 1e-9)
16        #expect(abs(axis.angle(at: 1, kind: step.kind)) < 1e-9)
17    }
18}
19
20@Test func anAxisWithCollinearEdgesTakesItsOwnWindow() throws {
21    let file = try compile(strips(.fold, windows: [[0, 0.5], [0.5, 1]]), pattern: stripsPattern())
22    #expect(try readStep(1, in: file).axes.map(\.window) == [0...0.5, 0.5...1])
23}
24
25@Test func anAxisAngleOutsideItsTravelIsRejected() throws {
26    // the second strip's angle carried on past its peak to a full turn
27    let file = try compile(strips(.foldAndUnfold), pattern: stripsPattern())
28    let step = try readStep(1, in: file)
29    let a = step.axes[1]
30    let runaway = FoldAxis(edge: a.edge, origin: a.origin, direction: a.direction, movingFaces: a.movingFaces,
31                           startAngle: a.startAngle, endAngle: a.endAngle, peakAngle: a.peakAngle,
32                           parent: a.parent, window: a.window, curve: (0...256).map { 360 * Double($0) / 256 })
33    let broken = FoldStep(index: 1, kind: step.kind, instruction: step.instruction, axes: [step.axes[0], runaway],
34                          viewpoint: step.viewpoint)
35    #expect(throws: CompileError.angleOutsideTravel(step: 1, edge: a.edge)) { try checkTravel(1, broken) }
36}
37
38@Test func aSolvedCurveMayBendPastItsEndAnglesWithinAHalfTurn() throws {
39    // a coupled crease flat at both ends that bends mid-step, and a hold-then-rise curve whose
40    // interpolation undershoots its start
41    let file = try compile(strips(.fold, windows: nil), pattern: stripsPattern())
42    let a = try readStep(1, in: file).axes[1]
43    let bend = (0...256).map { 120 * sin(.pi * Double($0) / 256) }
44    let rise = (0...256).map { $0 < 200 ? 0 : 90 * Double($0 - 199) / 57 }
45    for (end, curve) in [(0.0, bend), (90.0, rise)] {
46        let axis = FoldAxis(edge: a.edge, origin: a.origin, direction: a.direction, movingFaces: a.movingFaces,
47                            startAngle: 0, endAngle: end, window: a.window, curve: curve)
48        #expect(axis.angle(at: 0.775, kind: .fold) < 0 || end == 0)
49        try checkTravel(1, FoldStep(index: 1, kind: .fold, instruction: "", axes: [axis],
50                                    viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))))
51    }
52}
53
54@Test func compilingRejectsAnAngleBeyondAHalfTurn() throws {
55    let script = FoldScript(creasePattern: "", title: "Strips", steps: [
56        ScriptStep(kind: .foldAndUnfold, instruction: "Fold both ends in and unfold.",
57                   creases: [.edgeIndex(10), .edgeIndex(12)], movingSide: [SIMD2(0.5, 0.5), SIMD2(3.5, 0.5)],
58                   peakAngles: [180, 200]),
59    ])
60    #expect(throws: CompileError.angleOutsideTravel(step: 1, edge: 12)) {
61        try compile(script, pattern: stripsPattern())
62    }
63}
64
65/// A 4 × 2 sheet with creases x = 1 and x = 3, each split by y = 1 into two collinear edges.
66private func stripsPattern() -> FoldFile {
67    let points: [SIMD3<Double>] = [[0, 0, 0], [1, 0, 0], [3, 0, 0], [4, 0, 0], [4, 1, 0], [4, 2, 0],
68                                   [3, 2, 0], [1, 2, 0], [0, 2, 0], [0, 1, 0], [1, 1, 0], [3, 1, 0]]
69    let edges = [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 0],
70                 [1, 10], [10, 7], [2, 11], [11, 6], [9, 10], [10, 11], [11, 4]]
71    let assignment: [EdgeAssignment] = Array(repeating: .boundary, count: 10)
72        + [.valley, .valley, .valley, .valley, .flat, .flat, .flat]
73    return FoldFile(fileSpec: 1.2, fileTitle: "Strips", fileAuthor: "Traditional", fileClasses: ["creasePattern"],
74                    edgesVertices: edges,
75                    facesVertices: [[0, 1, 10, 9], [9, 10, 7, 8], [1, 2, 11, 10], [10, 11, 6, 7], [2, 3, 4, 11],
76                                    [11, 4, 5, 6]],
77                    edgesFirstCreasedAtStep: edges.map { _ in nil },
78                    frames: [FoldFrame(verticesCoords: points, edgesAssignment: assignment,
79                                       edgesFoldAngle: edges.map { _ in 0 }, faceOrders: [])])
80}
81
82/// One step folding both end strips in over the middle, about the lower edges of x = 1 and x = 3.
83private func strips(_ kind: StepKind, windows: [[Double]]? = nil) -> FoldScript {
84    FoldScript(creasePattern: "", title: "Strips", steps: [
85        ScriptStep(kind: kind, instruction: "Fold both ends in.", creases: [.edgeIndex(10), .edgeIndex(12)],
86                   movingSide: [SIMD2(0.5, 0.5), SIMD2(3.5, 0.5)],
87                   angles: kind == .fold ? [180, 180] : nil, peakAngles: kind == .foldAndUnfold ? [180, 180] : nil,
88                   windows: windows),
89    ])
90}