Packages/FoldKit/Tests/FoldingTests/ReorientMappingTests.swift
40 lines · 1735 bytes
6 symbols in this file
1import FoldFormat
2import FoldKitTestSupport
3import Folding
4import Testing
5
6/// `half.fold`'s 2 × 2 sheet from 4 units above its centre, with a 90° field of view in an
7/// 800 × 600 viewport: 150 points wide.
8private func sheetFromAbove() throws -> ReorientMapping {
9 let file = try FoldFile(contentsOf: fixture("half.fold"))
10 let camera = Camera(eye: SIMD3(1, 1, 4), target: SIMD3(1, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 2)
11 return ReorientMapping(positions: file.frames[0].verticesCoords, camera: camera, viewportSize: SIMD2(800, 600))
12}
13
14@Test func theWidthIsTheModelsProjectedWidth() throws {
15 #expect(abs(try sheetFromAbove().width - 150) < 1e-9)
16}
17
18@Test func aRightwardDragAcrossTheWidthCompletesTheStep() throws {
19 let m = try sheetFromAbove()
20 #expect(abs(m.parameter(translation: 75, current: 0) - 0.5) < 1e-12)
21 #expect(m.parameter(translation: 150, current: 0) == 1)
22}
23
24@Test func theReorientParameterIsClampedToUnitRange() throws {
25 let m = try sheetFromAbove()
26 #expect(m.parameter(translation: 1000, current: 0.5) == 1)
27 #expect(m.parameter(translation: -1000, current: 0.5) == 0)
28}
29
30@Test func aLeftwardDragTurnsTheModelBack() throws {
31 #expect(abs(try sheetFromAbove().parameter(translation: -37.5, current: 0.5) - 0.25) < 1e-12)
32}
33
34@Test func aModelSeenEdgeOnStillTakesADeliberateDrag() throws {
35 let file = try FoldFile(contentsOf: fixture("half.fold"))
36 let camera = Camera(eye: SIMD3(1, 5, 0), target: SIMD3(1, 1, 0), up: SIMD3(1, 0, 0), fieldOfView: .pi / 2)
37 let m = ReorientMapping(positions: file.frames[0].verticesCoords, camera: camera, viewportSize: SIMD2(800, 600))
38 #expect(m.width == 44)
39 #expect(m.parameter(translation: 0, current: 0) == 0)
40}