Packages/FoldKit/Tests/FoldingTests/CameraOrbitTests.swift

main
fold/Packages/FoldKit/Tests/FoldingTests/CameraOrbitTests.swift history · blame · raw

85 lines · 3142 bytes

12 symbols in this file
 1import Testing
 2import simd
 3import Folding
 4
 5private let front = Camera(eye: SIMD3(0, 0, 5), target: .zero, up: SIMD3(0, 1, 0), fieldOfView: .pi / 6)
 6
 7/// The point's position in clip space, divided by w.
 8private func screen(_ p: SIMD3<Double>, _ camera: Camera) -> SIMD2<Double> {
 9    let clip = camera.viewProjection(aspect: 1) * SIMD4(p, 1)
10    return SIMD2(clip.x, clip.y) / clip.w
11}
12
13private func isPerpendicularUnitUp(_ camera: Camera) -> Bool {
14    let up = camera.up, f = camera.viewDirection
15    return up.x.isFinite && up.y.isFinite && up.z.isFinite
16        && abs(length(up) - 1) < 1e-12 && abs(dot(up, f)) < 1e-12
17}
18
19@Test func orbitingByPositiveXCarriesTheNearSideRight() {
20    let near = SIMD3(0.0, 0, 1)
21    #expect(screen(near, front.orbited(by: SIMD2(0.1, 0))).x > 0.01)
22}
23
24@Test func orbitingByPositiveYCarriesTheNearSideDown() {
25    let near = SIMD3(0.0, 0, 1)
26    #expect(screen(near, front.orbited(by: SIMD2(0, 0.1))).y < -0.01)
27}
28
29@Test func aQuarterTurnAboutUpMovesTheEyeToTheLeftOfTheScene() {
30    let c = front.orbited(by: SIMD2(.pi / 2, 0))
31    #expect(length(c.eye - SIMD3(-5, 0, 0)) < 1e-12)
32    #expect(length(c.up - SIMD3(0, 1, 0)) < 1e-12)
33}
34
35@Test func orbitingKeepsTheTargetDistanceAndFieldOfView() {
36    let c = front.orbited(by: SIMD2(0.7, -1.3))
37    #expect(c.target == front.target)
38    #expect(abs(length(c.eye - c.target) - 5) < 1e-12)
39    #expect(c.fieldOfView == front.fieldOfView)
40}
41
42@Test func upStaysPerpendicularToTheViewDirectionOverThePoles() {
43    var c = front
44    for _ in 0..<200 {
45        c = c.orbited(by: SIMD2(0, 0.1))
46        #expect(isPerpendicularUnitUp(c))
47        let m = c.viewProjection(aspect: 1)
48        #expect((0..<4).allSatisfy { i in (0..<4).allSatisfy { j in m[i][j].isFinite } })
49    }
50    #expect(abs(length(c.eye - c.target) - 5) < 1e-9)
51}
52
53@Test func orbitingExactlyOntoThePoleKeepsAUsableUp() {
54    let c = front.orbited(by: SIMD2(0, .pi / 2))
55    #expect(length(c.eye - SIMD3(0, 5, 0)) < 1e-12)
56    #expect(isPerpendicularUnitUp(c))
57    #expect(length(c.up - SIMD3(0, 0, -1)) < 1e-12)
58}
59
60@Test func manyDiagonalOrbitsDoNotDrift() {
61    var c = front
62    for k in 0..<1000 {
63        c = c.orbited(by: SIMD2(0.031 * Double(k % 7 - 3), 0.017 * Double(k % 5 - 2)))
64    }
65    #expect(isPerpendicularUnitUp(c))
66    #expect(abs(length(c.eye - c.target) - 5) < 1e-9)
67}
68
69@Test func orbitingMakesAnObliqueUpPerpendicular() {
70    let oblique = Camera(eye: SIMD3(0, 0, 5), target: .zero, up: SIMD3(0, 1, 1), fieldOfView: .pi / 6)
71    #expect(isPerpendicularUnitUp(oblique.orbited(by: .zero)))
72    #expect(isPerpendicularUnitUp(oblique.orbited(by: SIMD2(0.2, 0.3))))
73}
74
75@Test func zoomingByTwoHalvesTheTangentOfTheHalfAngle() {
76    let c = front.zoomed(by: 2, within: 0.1...2)
77    #expect(abs(tan(c.fieldOfView / 2) - tan(front.fieldOfView / 2) / 2) < 1e-12)
78    #expect(c.eye == front.eye && c.up == front.up)
79    #expect(front.zoomed(by: 1, within: 0.1...2).fieldOfView == front.fieldOfView)
80}
81
82@Test func zoomingIsClampedToTheRange() {
83    #expect(front.zoomed(by: 100, within: 0.2...1).fieldOfView == 0.2)
84    #expect(front.zoomed(by: 0.01, within: 0.2...1).fieldOfView == 1)
85}