Packages/MoonriseKit/Tests/SkyRendererTests/PointingTests.swift
42 lines · 1494 bytes
1import CoreGraphics
2import Testing
3import SkyEngine
4@testable import SkyRenderer
5
6// Rows of the rotation matrix are the device axes in reference coordinates
7// (x north, y west, z up); the third row is the device z axis, toward the user.
8
9@Test func uprightFacingNorth() {
10 // Screen faces south, so device z = south = (-1, 0, 0).
11 let h = Pointing.direction(m31: -1, m32: 0, m33: 0)
12 #expect(abs(h.altitude) < 1e-9)
13 #expect(abs(h.azimuth) < 1e-9)
14}
15
16@Test func uprightFacingEast() {
17 // Screen faces west: device z = (0, 1, 0).
18 let h = Pointing.direction(m31: 0, m32: 1, m33: 0)
19 #expect(abs(h.azimuth - 90) < 1e-9)
20}
21
22@Test func flatFaceUpPointsDown() {
23 let h = Pointing.direction(m31: 0, m32: 0, m33: 1)
24 #expect(abs(h.altitude + 90) < 1e-9)
25}
26
27@Test func tiltedUpToward45DegreesSouth() {
28 // Camera toward south and 45° up: −z = (−s, 0, s), so z = (s, 0, −s).
29 let s = 0.5.squareRoot()
30 let h = Pointing.direction(m31: s, m32: 0, m33: -s)
31 #expect(abs(h.altitude - 45) < 1e-9)
32 #expect(abs(h.azimuth - 180) < 1e-9)
33}
34
35@Test func polarProjectionPutsPoleAtCentreAndObserverBelow() {
36 let p = PolarProjection(north: true, centerLongitude: -87.6, size: CGSize(width: 200, height: 200))
37 let pole = p.point(lon: 0, lat: 90)!
38 #expect(abs(pole.x - 100) < 1e-9 && abs(pole.y - 100) < 1e-9)
39 let you = p.point(lon: -87.6, lat: 41.9)!
40 #expect(abs(you.x - 100) < 1e-9 && you.y > 180)
41 #expect(p.point(lon: 0, lat: 20) == nil)
42}