import FoldFormat import FoldKitTestSupport import Folding import simd import Testing @Test func theFacetTracksTheFinger() throws { let m = try mapping("half.fold", step: 1) // viewed from above let target = m.path[32] // t = 0.25 let t = m.parameter(grabbedAt: m.path[0], translation: target - m.path[0], current: 0) #expect(abs(t - 0.25) < 0.01) } @Test func dragPerpendicularToThePathDoesNotAdvanceTheFold() throws { let m = try mapping("half.fold", step: 1) let along = simd_normalize(m.path[1] - m.path[0]) let perp = SIMD2(-along.y, along.x) #expect(m.parameter(grabbedAt: m.path[0], translation: perp * 50, current: 0) < 0.01) } @Test func parameterIsClampedToUnitRange() throws { let m = try mapping("half.fold", step: 1) let along = simd_normalize(m.path[128] - m.path[0]) #expect(m.parameter(grabbedAt: m.path[0], translation: along * 100_000, current: 0) == 1) #expect(m.parameter(grabbedAt: m.path[0], translation: along * -100_000, current: 0) == 0) } @Test func headOnFoldIsReportedDegenerate() throws { #expect(try mappingForFoldTowardTheCamera().isDegenerate) } @Test func degenerateMappingFallsBackToVerticalDrag() throws { let m = try mappingForFoldTowardTheCamera() #expect(m.parameter(grabbedAt: .zero, translation: SIMD2(0, 50), current: 0) > 0.1) } @Test func theMappingIsNeverInfinitelySensitive() throws { let m = try mappingForFoldTowardTheCamera() #expect(m.parameter(grabbedAt: .zero, translation: SIMD2(0, 1), current: 0) < 0.2) } @Test func aNonRigidStepIsDraggedPhaseByPhase() throws { // following the path's own samples in order reaches t = 1 without ever going back let m = try reverseMapping(openAngle: 30) var t = 0.0 for p in m.path.dropFirst() { let next = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) #expect(next >= t - 1e-9) t = next } #expect(t > 0.99) } @Test func foldAndUnfoldGoesOutThenBack() throws { let m = try mapping("quarters.fold", step: 1) let atPeak = m.parameter(grabbedAt: m.path[0], translation: m.path[64] - m.path[0], current: 0) #expect(abs(atPeak - 0.5) < 0.01) let back = m.parameter(grabbedAt: m.path[0], translation: .zero, current: atPeak) #expect(back > 0.99) } @Test func aPathThatRetracesItselfOnScreenIsFollowedWithoutJumpingToItsEnd() throws { // seen along (1, 0, 1), the flap's centroid passes the screen point of its end at 90° let file = try FoldFile(contentsOf: fixture("half.fold")) let m = FoldParameterMapping(file: file, step: try readStep(1, in: file), camera: Camera(eye: SIMD3(1, 1, 0) + normalize(SIMD3(1, 0, 1)) * 4, target: SIMD3(1, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4), viewportSize: SIMD2(800, 600)) var t = 0.0 for (k, p) in m.path.enumerated().dropFirst() { t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) #expect(abs(t - Double(k) / 128) < 0.01, "sample \(k)") } } @Test func aStaggeredStepIsFollowedAcrossItsWindows() throws { // two axes on windows 0...0.5 and 0.5...1; following the path's samples reaches each in turn. // 0.02 allows the last sample before a slow window end, within the end tolerance, to count as it let m = try mapping("letter-staggered.fold", step: 1) var t = 0.0 for (k, p) in m.path.enumerated().dropFirst() { t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) #expect(abs(t - Double(k) / 128) < 0.02, "sample \(k)") } } @Test func aLateWindowIsReachedByTheVerticalDragAndThenFollowed() throws { // nothing moves before 0.5, so that half of the path is the vertical fallback let file = try FoldFile(contentsOf: fixture("half.fold")) let full = try readStep(1, in: file) let a = full.axes[0] let step = FoldStep(index: 1, kind: .fold, instruction: full.instruction, axes: [FoldAxis(edge: a.edge, origin: a.origin, direction: a.direction, movingFaces: a.movingFaces, startAngle: 0, endAngle: 180, window: 0.5...1)], viewpoint: full.viewpoint) let m = FoldParameterMapping(file: file, step: step, camera: Camera(eye: SIMD3(1, 1, 4), target: SIMD3(1, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4), viewportSize: SIMD2(800, 600)) #expect(!m.isDegenerate) let early = m.parameter(grabbedAt: m.path[0], translation: SIMD2(0, 40), current: 0) #expect(abs(early - 0.25) < 0.01) var t = 0.0 for (k, p) in m.path.enumerated().dropFirst() { t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) #expect(abs(t - Double(k) / 128) < 0.02, "sample \(k)") } } @Test func aFoldAndUnfoldPeaksAtTheMiddleOfItsWindow() throws { let file = try FoldFile(contentsOf: fixture("quarters.fold")) let full = try readStep(1, in: file) let a = full.axes[0] let step = FoldStep(index: 1, kind: .foldAndUnfold, instruction: full.instruction, axes: [FoldAxis(edge: a.edge, origin: a.origin, direction: a.direction, movingFaces: a.movingFaces, startAngle: a.startAngle, endAngle: a.endAngle, peakAngle: a.peakAngle, window: 0...0.6)], viewpoint: full.viewpoint) let m = FoldParameterMapping(file: file, step: step, camera: Camera(eye: SIMD3(1, 1, 4), target: SIMD3(1, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4), viewportSize: SIMD2(800, 600)) let peak = m.point(at: 0.3) let atPeak = m.parameter(grabbedAt: m.path[0], translation: peak - m.path[0], current: 0) #expect(abs(atPeak - 0.3) < 0.01) let back = m.parameter(grabbedAt: m.path[0], translation: .zero, current: atPeak) #expect(abs(back - 0.6) < 0.01) } /// The reverse fixture opened to `openAngle` from flat. private func reverseFixture(openAngle: Double) throws -> FoldFile { try compiled("reverse", openAngle: openAngle, step: 2) } /// The mapping for the reverse fixture's non-rigid step, seen from above. Opened to 30° from flat /// the pop moves the tip far enough to be followed, and its path starts back along the open's. private func reverseMapping(openAngle: Double, viewportSize: SIMD2 = SIMD2(800, 600)) throws -> FoldParameterMapping { let file = try reverseFixture(openAngle: openAngle) return FoldParameterMapping(file: file, step: try readSteps(file)[1], camera: Camera(eye: SIMD3(2, 1, 8), target: SIMD3(2, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4), viewportSize: viewportSize) } @Test func aFallbackStretchFitsInHalfTheView() throws { // R81: at 20° from flat the pop is too small to follow; dragged straight down it spans at most // half the view's height, so it ends inside a short view let phases = try readSteps(reverseFixture(openAngle: 20))[1].pop!.phases for height in [600.0, 200.0] { let m = try reverseMapping(openAngle: 20, viewportSize: SIMD2(800, height)) let drop = m.point(at: phases.upperBound) - m.point(at: phases.lowerBound) #expect(abs(drop.x) < 1e-9) #expect(abs(drop.y - min(160, height / 2) * (phases.upperBound - phases.lowerBound)) < 1e-9, "height \(height)") } } @Test func aNonRigidStepCanBeDraggedBackThroughItsPhases() throws { // the pop's path starts back along the open's, so dragging back passes that turnaround too let m = try reverseMapping(openAngle: 30) var t = 0.0 for p in m.path.dropFirst() { t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) } #expect(t == 1) for p in m.path.reversed().dropFirst() { let next = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) #expect(next <= t + 1e-9) t = next } #expect(t < 0.01) } @Test func aFoldAndUnfoldStillUnfoldsAfterItsPeak() throws { // past the peak the return half retraces the outbound half, so moving toward the start unfolds let m = try mapping("quarters.fold", step: 1) var t = 0.0 for p in m.path[1...80] { t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) } #expect(abs(t - 0.625) < 0.01) for p in m.path[0..<48].reversed() { let next = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) #expect(next >= t - 1e-9) t = next } #expect(t == 1) } @Test func aStaggeredStepCanBeDraggedBackPastItsWindowBoundary() throws { // seen from above, the second window's path retraces the first's, so samples 63 and 65 are one // screen point and only the drag's position says which window it is in let m = try mapping("letter-staggered.fold", step: 1) var position = m.position(at: 0) for k in Array(1...96) + Array((0..<96).reversed()) { position = m.position(grabbedAt: m.path[0], translation: m.path[k] - m.path[0], current: position) #expect(abs(position.t - Double(k) / 128) < 0.01, "sample \(k)") } } @Test func aDragOnARetracedStretchStaysInItsPhase() throws { // the pop's samples 44...54 lie within half a point of the open's path; a finger there stays // in the phase the drag is in until it reaches the turnaround let m = try reverseMapping(openAngle: 30) let phases = try readSteps(reverseFixture(openAngle: 30))[1].pop!.phases var opening = m.position(at: 0) for k in 1...36 { opening = m.position(grabbedAt: m.path[0], translation: m.path[k] - m.path[0], current: opening) } var popping = opening for k in 37...54 { popping = m.position(grabbedAt: m.path[0], translation: m.path[k] - m.path[0], current: popping) } #expect(popping.t > phases.lowerBound) for k in 44...54 { let p = m.path[k] - m.path[0] #expect(m.position(grabbedAt: m.path[0], translation: p, current: opening).t < phases.lowerBound, "sample \(k)") #expect(m.position(grabbedAt: m.path[0], translation: p, current: popping).t > phases.lowerBound, "sample \(k)") } } @Test func aPopTooSmallToFollowIsDraggedThroughItsTurnarounds() throws { // at the default 20° from flat the pop moves the tip too little to follow, so its path is the // straight line down, retracing the open's seen from above; a finger following the path // passes through each turnaround, where the drag changes phase, forward and back let m = try reverseMapping(openAngle: 20) let phases = try readSteps(reverseFixture(openAngle: 20))[1].pop!.phases let ts = ((1...128).map { Double($0) / 128 } + [phases.lowerBound, phases.upperBound]).sorted() var position = m.position(at: 0) for t in ts { let next = m.position(grabbedAt: m.path[0], translation: m.point(at: t) - m.path[0], current: position) #expect(next.t >= position.t - 1e-9) position = next } #expect(position.t == 1) for t in ts.reversed().dropFirst() + [0] { let next = m.position(grabbedAt: m.path[0], translation: m.point(at: t) - m.path[0], current: position) #expect(next.t <= position.t + 1e-9) position = next } #expect(position.t < 0.01) }