SPB Git

Repository Public BSD-3-Clause

CameraTests.swift 12.7 KB 244 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Maps9// File             : Packages/ORIXAtlas/Tests/ORIXAtlasTests/CameraTests.swift10// Module           : ORIXAtlasTests11// Repository       : https://git.spboucher.ai/orix-maps12// Created          : 2026-11-2513// Last Modified    : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import CoreGraphics89import Foundation90import ORIXAtlas91import ORIXGeoCore92import simd93import Testing9495@Suite struct CameraTests {96    let montreal = Coordinate(latitude: 45.5017, longitude: -73.5673)97    let viewport = CGSize(width: 393, height: 852)9899    @Test func centreMapsToTheMiddleOfTheView() throws {100        for (bearing, pitch) in [(0.0, 0.0), (77, 0), (0, 60), (213, 45)] {101            let p = AtlasCamera(center: montreal, zoom: 14.3, bearing: bearing, pitch: pitch).projection(viewport: viewport)102            let s = try #require(p.point(for: montreal))103            #expect(abs(s.x - 196.5) < 1e-6 && abs(s.y - 426) < 1e-6)104            let c = try #require(p.coordinate(at: CGPoint(x: 196.5, y: 426)))105            #expect(abs(c.latitude - montreal.latitude) < 1e-9 && abs(c.longitude - montreal.longitude) < 1e-9)106        }107    }108109    @Test func screenAndCoordinateRoundTrip() throws {110        var worst = 0.0111        for zoom in [1.0, 6.5, 12, 17.25, 20] {112            for bearing in [0.0, 45, 200, 359] {113                for pitch in [0.0, 30, 60] {114                    let p = AtlasCamera(center: montreal, zoom: zoom, bearing: bearing, pitch: pitch).projection(viewport: viewport)115                    for x in stride(from: 0.0, through: 393, by: 131) {116                        for y in stride(from: 0.0, through: 852, by: 142) {117                            let s = CGPoint(x: x, y: y)118                            guard let c = p.coordinate(at: s) else {119                                // Only acceptable beyond a pole, at world-scale zooms.120                                #expect(zoom < 3)121                                continue122                            }123                            let back = try #require(p.point(for: c))124                            // At world scale the nearest copy of the world may be another one: compare modulo the world width.125                            var dx = abs(Double(back.x) - x)126                            if zoom < 3, pitch == 0, bearing == 0 { dx = min(dx, abs(dx - p.worldSize)) }127                            if zoom >= 3 { worst = max(worst, dx, abs(Double(back.y) - y)) }128                        }129                    }130                }131            }132        }133        // Points; the residual is double rounding through the inverse matrix.134        #expect(worst < 1e-5)135    }136137    @Test func scaleIsOnePointPerWorldPointAtTheCentre() throws {138        let cam = AtlasCamera(center: montreal, zoom: 12)139        let p = cam.projection(viewport: viewport)140        #expect(p.worldSize == 512 * 4096)141        let c = try #require(p.coordinate(at: CGPoint(x: 296.5, y: 426)))142        #expect(abs((c.longitude - montreal.longitude) - 100.0 / p.worldSize * 360) < 1e-10)143        #expect(abs(c.latitude - montreal.latitude) < 1e-10)144        #expect(abs(cam.metersPerPoint - WebMercator.metersPerPoint(latitude: montreal.latitude, zoom: 12)) < 1e-12)145        // 100 points east is 100 × metres-per-point on the ground.146        let metres = Geodesy.distance(montreal, c)147        #expect(abs(metres - 100 * cam.metersPerPoint) / metres < 0.005)148    }149150    @Test func bearingPutsThatDirectionAtTheTop() throws {151        let east = Coordinate(latitude: montreal.latitude, longitude: montreal.longitude + 0.01)152        let north = Coordinate(latitude: montreal.latitude + 0.01, longitude: montreal.longitude)153        let p0 = AtlasCamera(center: montreal, zoom: 13).projection(viewport: viewport)154        #expect(try #require(p0.point(for: east)).x > 196.5 + 10)155        #expect(try #require(p0.point(for: north)).y < 426 - 10)156        // Bearing 90: east is up, north is to the left.157        let p90 = AtlasCamera(center: montreal, zoom: 13, bearing: 90).projection(viewport: viewport)158        let e = try #require(p90.point(for: east)), n = try #require(p90.point(for: north))159        #expect(abs(e.x - 196.5) < 1e-6 && e.y < 426 - 10)160        #expect(abs(n.y - 426) < 1e-6 && n.x < 196.5 - 10)161    }162163    @Test func pitchForeshortensAndWidensTheFarGround() throws {164        let north = Coordinate(latitude: montreal.latitude + 0.004, longitude: montreal.longitude)165        let flat = AtlasCamera(center: montreal, zoom: 14).projection(viewport: viewport)166        let tilted = AtlasCamera(center: montreal, zoom: 14, pitch: 60).projection(viewport: viewport)167        let a = try #require(flat.point(for: north)), b = try #require(tilted.point(for: north))168        #expect(a.y < 426 && b.y < 426)169        #expect(426 - b.y < 426 - a.y)170        let q = try #require(tilted.groundQuad)171        let top = simd_distance(q[0], q[1]), bottom = simd_distance(q[3], q[2])172        #expect(top > bottom * 2)173        // Flat view: the ground quad is the viewport rectangle in world points.174        let f = try #require(flat.groundQuad)175        #expect(abs(simd_distance(f[0], f[1]) - 393) < 1e-6 && abs(simd_distance(f[1], f[2]) - 852) < 1e-6)176    }177178    @Test func worldWrapKeepsTheNearestCopy() throws {179        let p = AtlasCamera(center: Coordinate(latitude: 0, longitude: 179.9), zoom: 6).projection(viewport: viewport)180        let across = try #require(p.point(for: Coordinate(latitude: 0, longitude: -179.9)))181        // 0.2° east of the centre, not 359.8° west of it.182        #expect(abs(Double(across.x) - (196.5 + 0.2 / 360 * p.worldSize)) < 1e-6)183        let c = try #require(p.coordinate(at: across))184        #expect(abs(c.longitude - -179.9) < 1e-9)185        // Any screen point gives a longitude inside [-180, 180).186        for x in stride(from: -2000.0, through: 2000, by: 250) {187            let lon = try #require(p.coordinate(at: CGPoint(x: x, y: 426))).longitude188            #expect(lon >= -180 && lon < 180)189        }190    }191192    @Test func nothingBeyondThePoles() {193        let p = AtlasCamera(center: Coordinate(latitude: 0, longitude: 0), zoom: 0).projection(viewport: viewport)194        // The world is 512 points tall and the view 852: the top and bottom strips show no map.195        #expect(p.coordinate(at: CGPoint(x: 196.5, y: 10)) == nil)196        #expect(p.coordinate(at: CGPoint(x: 196.5, y: 842)) == nil)197        #expect(p.coordinate(at: CGPoint(x: 196.5, y: 426 - 250)) != nil)198    }199200    @Test func normalizationMatchesMapCameraRules() {201        let c = AtlasCamera(center: Coordinate(latitude: 95, longitude: 190), zoom: .nan, bearing: -90, pitch: 85).normalized202        #expect(c.center.latitude == WebMercator.maxLatitude)203        #expect(abs(c.center.longitude - -170) < 1e-12)204        #expect(c.zoom == 2 && c.bearing == 270 && c.pitch == 60)205        #expect(AtlasCamera(center: Coordinate(latitude: .infinity, longitude: .nan), zoom: 99, bearing: .nan, pitch: -5).normalized206            == AtlasCamera(center: Coordinate(latitude: 0, longitude: 0), zoom: 20, bearing: 0, pitch: 0))207        // A degenerate viewport does not produce NaN.208        let p = AtlasCamera(center: Coordinate(latitude: 0, longitude: 0), zoom: 3).projection(viewport: .zero)209        #expect(p.point(for: Coordinate(latitude: 0, longitude: 0)) != nil)210        #expect(p.point(for: Coordinate(latitude: .nan, longitude: 0)) == nil)211        #expect(p.coordinate(at: CGPoint(x: Double.nan, y: 0)) == nil)212    }213214    @Test func tileMatrixAgreesWithTheDoubleProjectionAtStreetZoom() throws {215        // A deepest tile (z15) overzoomed at zoom 20, tilted and rotated: float matrix against double conversions.216        let cam = AtlasCamera(center: montreal, zoom: 20, bearing: 33, pitch: 55)217        let p = cam.projection(viewport: viewport)218        let tile = WebMercator.tile(for: montreal, zoom: 15)219        let m = p.tileMatrix(for: tile)220        var worst = 0.0221        for (ux, uy) in [(0.0, 0.0), (4096, 0), (4096, 4096), (0, 4096), (2048, 2048), (1234, 3210)] {222            let clip = m * SIMD4<Float>(Float(ux), Float(uy), 0, 1)223            let sx = (Double(clip.x / clip.w) * 0.5 + 0.5) * 393, sy = (0.5 - Double(clip.y / clip.w) * 0.5) * 852224            let n = 32768.0225            let coordinate = WebMercator.unproject(x: (Double(tile.x) + ux / 4096) / n, y: (Double(tile.y) + uy / 4096) / n)226            guard let expected = p.point(for: coordinate) else { continue }227            // Compare only what is near the view: far corners of the tile are thousands of points away at zoom 20.228            guard abs(expected.x) < 5000, abs(expected.y) < 5000 else { continue }229            worst = max(worst, abs(sx - Double(expected.x)), abs(sy - Double(expected.y)))230        }231        #expect(worst < 0.05)232        #expect(abs(p.tileUnitsPerPoint(tileZoom: 15) - 4096 / (512 * 32)) < 1e-12)233    }234235    @Test func visibleBoundsContainTheCentreAndHandleTheAntimeridian() throws {236        let b = try #require(AtlasCamera(center: montreal, zoom: 11, bearing: 40, pitch: 30).projection(viewport: viewport).visibleBounds)237        #expect(b.contains(montreal) && !b.crossesAntimeridian)238        let d = try #require(AtlasCamera(center: Coordinate(latitude: 10, longitude: 180), zoom: 5).projection(viewport: viewport).visibleBounds)239        #expect(d.crossesAntimeridian && d.contains(Coordinate(latitude: 10, longitude: -179)) && d.contains(Coordinate(latitude: 10, longitude: 179)))240        let w = try #require(AtlasCamera(center: Coordinate(latitude: 0, longitude: 0), zoom: 0).projection(viewport: CGSize(width: 600, height: 300)).visibleBounds)241        #expect(w.minLon == -180 && w.maxLon == 180)242    }243}244
Files main
Menu

Theme