SPB Git

Repository Public BSD-3-Clause

TileCoverTests.swift 12.8 KB 243 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/TileCoverTests.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 Testing9394@Suite struct TileCoverTests {95    let montreal = Coordinate(latitude: 45.5017, longitude: -73.5673)96    let viewport = CGSize(width: 393, height: 852)9798    @Test func tileZoomIsTheIntegerPartInsideTheArchive() {99        #expect(TileCover.tileZoom(forZoom: 12.99, minZoom: 0, maxZoom: 15) == 12)100        #expect(TileCover.tileZoom(forZoom: 18.2, minZoom: 0, maxZoom: 15) == 15)101        #expect(TileCover.tileZoom(forZoom: 1.5, minZoom: 4, maxZoom: 15) == 4)102        #expect(TileCover.tileZoom(forZoom: .nan, minZoom: 0, maxZoom: 6) == 0)103        #expect(TileCover.tileZoom(forZoom: -3, minZoom: 0, maxZoom: 6) == 0)104    }105106    @Test func zoomZeroIsOneTilePerWorldCopy() {107        let narrow = AtlasCamera(center: Coordinate(latitude: 0, longitude: 0), zoom: 0).projection(viewport: CGSize(width: 400, height: 400))108        #expect(TileCover.tiles(for: narrow, tileZoom: 0) == [CoveredTile(id: TileID(z: 0, x: 0, y: 0), wrap: 0)])109        let wide = AtlasCamera(center: Coordinate(latitude: 0, longitude: 0), zoom: 0).projection(viewport: CGSize(width: 1200, height: 400))110        let tiles = TileCover.tiles(for: wide, tileZoom: 0)111        #expect(tiles.first == CoveredTile(id: TileID(z: 0, x: 0, y: 0), wrap: 0))112        #expect(Set(tiles) == [CoveredTile(id: TileID(z: 0, x: 0, y: 0), wrap: -1), CoveredTile(id: TileID(z: 0, x: 0, y: 0), wrap: 0), CoveredTile(id: TileID(z: 0, x: 0, y: 0), wrap: 1)])113    }114115    @Test func antimeridianYieldsBothEdgesOfTheWorld() {116        let p = AtlasCamera(center: Coordinate(latitude: 0, longitude: 179.99), zoom: 3).projection(viewport: CGSize(width: 600, height: 400))117        let tiles = TileCover.tiles(for: p, tileZoom: 3)118        #expect(tiles.contains(CoveredTile(id: TileID(z: 3, x: 7, y: 3), wrap: 0)))119        #expect(tiles.contains(CoveredTile(id: TileID(z: 3, x: 0, y: 3), wrap: 1)))120        #expect(tiles.allSatisfy { $0.id.isValid })121        #expect(!tiles.contains { $0.wrap == 0 && $0.id.x == 0 })122        // From the other side the primary copy is on the right and the copy -1 on the left.123        let q = AtlasCamera(center: Coordinate(latitude: 0, longitude: -179.99), zoom: 3).projection(viewport: CGSize(width: 600, height: 400))124        let other = TileCover.tiles(for: q, tileZoom: 3)125        #expect(other.contains(CoveredTile(id: TileID(z: 3, x: 0, y: 3), wrap: 0)) && other.contains(CoveredTile(id: TileID(z: 3, x: 7, y: 3), wrap: -1)))126    }127128    @Test func polesClampTheRows() {129        for lat in [89.0, -89.0] {130            let p = AtlasCamera(center: Coordinate(latitude: lat, longitude: 10), zoom: 2).projection(viewport: viewport)131            let tiles = TileCover.tiles(for: p, tileZoom: 2)132            #expect(!tiles.isEmpty)133            #expect(tiles.allSatisfy { $0.id.isValid && $0.id.z == 2 })134            #expect(tiles.contains { $0.id.y == (lat > 0 ? 0 : 3) })135            #expect(!tiles.contains { $0.id.y == (lat > 0 ? 3 : 0) })136        }137    }138139    @Test func flatViewOverMontreal() {140        let p = AtlasCamera(center: montreal, zoom: 14).projection(viewport: viewport)141        let tiles = TileCover.tiles(for: p, tileZoom: 14)142        #expect(tiles.first?.id == WebMercator.tile(for: montreal, zoom: 14))143        // 393 × 852 points over 512-point tiles: at most 2 columns × 3 rows.144        #expect(tiles.count >= 2 && tiles.count <= 6)145        #expect(Set(tiles).count == tiles.count)146        #expect(tiles.allSatisfy { $0.wrap == 0 })147    }148149    @Test func everyVisibleGroundPointIsCovered() throws {150        for (zoom, bearing, pitch) in [(14.0, 0.0, 0.0), (13.6, 37, 0), (15.2, 120, 45), (12.9, 300, 60), (3.3, 10, 20)] {151            let p = AtlasCamera(center: montreal, zoom: zoom, bearing: bearing, pitch: pitch).projection(viewport: viewport)152            let z = UInt8(zoom.rounded(.down))153            let tiles = Set(TileCover.tiles(for: p, tileZoom: z, limit: 10_000).map(\.id))154            for x in stride(from: 0.0, through: 393, by: 39.3) {155                for y in stride(from: 0.0, through: 852, by: 42.6) {156                    guard let c = p.coordinate(at: CGPoint(x: x, y: y)) else { continue }157                    #expect(tiles.contains(WebMercator.tile(for: c, zoom: z)), "zoom \(zoom) bearing \(bearing) pitch \(pitch) point \(x),\(y)")158                }159            }160        }161    }162163    @Test func pitchAddsFarTilesAndTheLimitKeepsTheNearest() {164        let flat = TileCover.tiles(for: AtlasCamera(center: montreal, zoom: 15).projection(viewport: viewport), tileZoom: 15, limit: 1000)165        let p = AtlasCamera(center: montreal, zoom: 15, pitch: 60).projection(viewport: viewport)166        let tilted = TileCover.tiles(for: p, tileZoom: 15, limit: 1000)167        #expect(tilted.count > flat.count)168        let centre = WebMercator.tile(for: montreal, zoom: 15)169        // Looking north: the extra tiles are to the north (smaller y), none far to the south.170        #expect(tilted.map(\.id.y).min()! < flat.map(\.id.y).min()!)171        #expect(tilted.map(\.id.y).max()! <= centre.y + 1)172        // Centre-out order and a cap that drops the farthest.173        func d(_ t: CoveredTile) -> Double {174            let n = 32768.0, c = WebMercator.project(montreal)175            let dx = Double(t.id.x) + 0.5 - c.x * n, dy = Double(t.id.y) + 0.5 - c.y * n176            return dx * dx + dy * dy177        }178        #expect(zip(tilted, tilted.dropFirst()).allSatisfy { d($0) <= d($1) })179        let capped = TileCover.tiles(for: p, tileZoom: 15, limit: 5)180        #expect(capped == Array(tilted.prefix(5)))181        #expect(TileCover.tiles(for: p, tileZoom: 15, limit: 0).isEmpty)182    }183184    @Test func shallowestTilesFarSmallerThanTheViewStayBounded() {185        // An archive that starts at zoom 12 seen from zoom 2: the scan is windowed and capped, not millions of tiles.186        let p = AtlasCamera(center: montreal, zoom: 2).projection(viewport: viewport)187        let tiles = TileCover.tiles(for: p, tileZoom: 12, limit: 64)188        #expect(tiles.count == 64)189        #expect(tiles.first?.id == WebMercator.tile(for: montreal, zoom: 12))190    }191192    // MARK: Fallback193194    @Test func ancestorsNearestFirst() {195        let t = TileID(z: 5, x: 9, y: 11)196        #expect(TileFallback.ancestors(of: t, minZoom: 2) == [TileID(z: 4, x: 4, y: 5), TileID(z: 3, x: 2, y: 2), TileID(z: 2, x: 1, y: 1)])197        #expect(TileFallback.ancestors(of: TileID(z: 0, x: 0, y: 0), minZoom: 0).isEmpty)198        #expect(TileClip.of(t, in: TileID(z: 3, x: 2, y: 2)) == TileClip(minX: 0.25, minY: 0.75, maxX: 0.5, maxY: 1))199        #expect(TileClip.of(t, in: t) == .full)200        #expect(TileClip.of(t, in: TileID(z: 3, x: 0, y: 0)) == nil)201    }202203    @Test func readyTilesAreDrawnAsThemselves() {204        let cover = [CoveredTile(id: TileID(z: 3, x: 1, y: 2), wrap: 0), CoveredTile(id: TileID(z: 3, x: 7, y: 2), wrap: -1)]205        let items = TileFallback.resolve(cover: cover, minZoom: 0, maxZoom: 6) { _ in true }206        #expect(items.count == 2 && items.allSatisfy { !$0.isFallback && $0.clip == .full })207        #expect(items[1].wrap == -1)208    }209210    @Test func missingTilesFallBackToTheNearestReadyAncestorClipped() {211        let a = TileID(z: 4, x: 4, y: 6), b = TileID(z: 4, x: 5, y: 6)212        let ready: Set<TileID> = [TileID(z: 2, x: 1, y: 1), TileID(z: 0, x: 0, y: 0)]213        let items = TileFallback.resolve(cover: [CoveredTile(id: a), CoveredTile(id: b)], minZoom: 0, maxZoom: 6) { ready.contains($0) }214        #expect(items.count == 2)215        #expect(items.allSatisfy { $0.source == TileID(z: 2, x: 1, y: 1) && $0.isFallback })216        #expect(items[0].clip == TileClip(minX: 0, minY: 0.5, maxX: 0.25, maxY: 0.75))217        #expect(items[1].clip == TileClip(minX: 0.25, minY: 0.5, maxX: 0.5, maxY: 0.75))218        // Nothing ready at all: nothing to draw, and ancestors below the archive's first zoom are not used.219        #expect(TileFallback.resolve(cover: [CoveredTile(id: a)], minZoom: 0, maxZoom: 6) { _ in false }.isEmpty)220        #expect(TileFallback.resolve(cover: [CoveredTile(id: a)], minZoom: 3, maxZoom: 6) { ready.contains($0) }.isEmpty)221    }222223    @Test func readyChildrenReplaceTheirQuadrantAndTheAncestorFillsTheRest() {224        let t = TileID(z: 3, x: 2, y: 2)225        let children = t.children226        let parent = TileID(z: 2, x: 1, y: 1)227        let ready: Set<TileID> = [children[0], children[3], parent]228        let items = TileFallback.resolve(cover: [CoveredTile(id: t, wrap: 2)], minZoom: 0, maxZoom: 6) { ready.contains($0) }229        #expect(items.count == 4 && items.allSatisfy { $0.target == t && $0.wrap == 2 })230        #expect(items.filter { $0.source == parent }.count == 2)231        #expect(items.first { $0.source == children[0] }?.clip == .full)232        // The four draws tile the target without overlap: areas in units of the target tile add up to one.233        func area(_ i: TileDrawItem) -> Double {234            let side = 1 / Double(1 << (Int(i.source.z) - Int(t.z) + 4)) * 16235            return (i.clip.maxX - i.clip.minX) * (i.clip.maxY - i.clip.minY) * side * side236        }237        #expect(abs(items.map(area).reduce(0, +) - 1) < 1e-12)238        // At the archive's deepest zoom there are no children to look for.239        let deepest = TileFallback.resolve(cover: [CoveredTile(id: t)], minZoom: 0, maxZoom: 3) { ready.contains($0) }240        #expect(deepest.count == 1 && deepest[0].source == parent)241    }242}243
Files main
Menu

Theme