Repository Public BSD-3-Clause
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Maps9// File : Packages/ORIXAtlas/Tests/ORIXAtlasTests/SyntheticRendererTests.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 ORIXMapStyle93import ORIXTileStore94import Testing9596/// Pixel-exact checks on tiles written by the tests: one zoom 12 tile of land crossed by a vertical motorway, and a97/// horizontal minor road.98@Suite(.enabled(if: AtlasRenderer.isMetalAvailable)) struct SyntheticRendererTests {99 static let tile = TileID(z: 12, x: 1210, y: 1465)100 static var centre: Coordinate { WebMercator.unproject(x: (Double(tile.x) + 0.5) / 4096, y: (Double(tile.y) + 0.5) / 4096) }101102 static func landWithRoads() -> [UInt8] {103 MVT.layer("earth", [MVT.Feature(type: .polygon, parts: [[(-64, -64), (4160, -64), (4160, 4160), (-64, 4160)]], attributes: [("kind", "earth")])])104 + MVT.layer("roads", [105 MVT.Feature(type: .line, parts: [[(2048, -64), (2048, 4160)]], attributes: [("kind", "highway")]),106 MVT.Feature(type: .line, parts: [[(-64, 1024), (4160, 1024)]], attributes: [("kind", "minor_road"), ("kind_detail", "residential")]),107 ])108 }109110 func run(_ bitmap: AtlasBitmap, row: Int, of colour: HexColor) -> Int {111 var n = 0112 for x in 0..<bitmap.width { let c = bitmap.color(x: x, y: row)!; if HexColor(red: c.red, green: c.green, blue: c.blue) == colour { n += 1 } }113 return n114 }115116 @Test func lineWidthInPointsFollowsTheStyleAtEveryZoom() async throws {117 let archive = try SyntheticArchive.make([(Self.tile, Self.landWithRoads())])118 let day = BuiltInThemes.day119 let renderer = try await AtlasRenderer(store: TileStore(archive: archive), theme: day)120 let size = CGSize(width: 240, height: 120)121 let scale = 4.0122 for zoom in [12.0, 12.37, 12.99, 13, 14.5, 16, 17.2] {123 for pitch in [0.0, 50] {124 let bitmap = try await renderer.renderBitmap(camera: AtlasCamera(center: Self.centre, zoom: zoom, pitch: pitch), size: size, scale: scale)125 let row = bitmap.height / 2126 let road = Double(run(bitmap, row: row, of: day[.highway])) / scale127 let casing = Double(run(bitmap, row: row, of: day[.highwayCasing])) / scale128 let expected = AtlasStyle.roadWidth(.highway, theme: day, zoom: zoom)129 let expectedCasing = AtlasStyle.casingWidth(.highway, theme: day, zoom: zoom)130 // Fully covered pixels only are counted, so up to one pixel is lost on each side (and one more on each casing band);131 // a pixel counts as covered when its four samples are, which can round a width up by a fraction of a pixel.132 #expect(road <= expected + 0.5 / scale && road >= expected - 2 / scale - 0.01, "zoom \(zoom) pitch \(pitch): \(road) for \(expected)")133 #expect(casing <= expectedCasing - expected + 1 / scale && casing >= expectedCasing - expected - 4 / scale - 0.01, "zoom \(zoom) pitch \(pitch): casing \(casing)")134 // Symmetric about the centre line.135 let c = bitmap.color(x: bitmap.width / 2, y: row)!136 #expect(HexColor(red: c.red, green: c.green, blue: c.blue) == day[.highway])137 // Away from the road: land.138 let l = bitmap.color(x: 8, y: row)!139 #expect(HexColor(red: l.red, green: l.green, blue: l.blue) == day[.land])140 }141 }142 }143144 @Test func hairlinesAreOnePixelWideAndFaint() async throws {145 // The minor road is 0.4 point wide at zoom 11: at scale 1 it is drawn one pixel wide at 40 % opacity instead of vanishing.146 let archive = try SyntheticArchive.make([(Self.tile, Self.landWithRoads())])147 var theme = BuiltInThemes.day148 theme.roadCasings = false149 theme.colors[.minorRoad] = HexColor("#000000")!150 let renderer = try await AtlasRenderer(store: TileStore(archive: archive), theme: theme)151 let size = CGSize(width: 100, height: 100)152 let road = WebMercator.unproject(x: (Double(Self.tile.x) + 0.5) / 4096, y: (Double(Self.tile.y) + 0.25) / 4096)153 let p = AtlasCamera(center: road, zoom: 12).projection(viewport: size)154 #expect(abs(AtlasStyle.roadWidth(.minor, theme: theme, zoom: 12) - 0.776) < 0.01)155 let bitmap = try await renderer.renderBitmap(camera: p.camera, size: size, scale: 1)156 // Column 10: sum of the darkening over the rows equals width × 255 (coverage is conserved), spread over at most 2 rows.157 let land = theme[.land]158 var darkening = 0.0, rows = 0159 for y in 0..<100 { let c = bitmap.color(x: 10, y: y)!; let d = Double(land.red) - Double(c.red); if d > 1 { rows += 1; darkening += d / Double(land.red) } }160 #expect(rows >= 1 && rows <= 2)161 #expect(abs(darkening - 0.776) < 0.08)162 }163164 @Test func geometryInTheTileBufferIsClippedToTheTile() async throws {165 // Two adjacent tiles: the left one is land, the right one has no land at all. The land polygon of the left tile166 // extends 64 units into its buffer and must not spill over the right tile.167 let left = Self.tile, right = TileID(z: 12, x: Self.tile.x + 1, y: Self.tile.y)168 let archive = try SyntheticArchive.make([(left, Self.landWithRoads()), (right, MVT.layer("places", []))])169 let renderer = try await AtlasRenderer(store: TileStore(archive: archive), theme: BuiltInThemes.day)170 let border = WebMercator.unproject(x: Double(right.x) / 4096, y: (Double(left.y) + 0.5) / 4096)171 let size = CGSize(width: 200, height: 100)172 for bearing in [0.0, 90] {173 let bitmap = try await renderer.renderBitmap(camera: AtlasCamera(center: border, zoom: 12, bearing: bearing), size: size, scale: 2)174 let day = BuiltInThemes.day175 func at(_ x: Int, _ y: Int) -> HexColor { let c = bitmap.color(x: x, y: y)!; return HexColor(red: c.red, green: c.green, blue: c.blue) }176 if bearing == 0 {177 // The border is the vertical line x = 200 px: land strictly left, background strictly right.178 #expect(at(198, 100) == day[.land] && at(201, 100) == day[.water] && at(215, 100) == day[.water])179 } else {180 // East is up: the right tile is the top half.181 #expect(at(200, 101) == day[.land] && at(200, 98) == day[.water])182 }183 #expect(await renderer.lastFrame.uncoveredTiles == 0)184 }185 }186187 @Test func corruptTileIsCountedOnceAndReplacedByItsParent() async throws {188 let parent = try #require(Self.tile.parent)189 let garbage: [UInt8] = [0x1a, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x01]190 #expect(throws: (any Error).self) { _ = try VectorTile(data: Data(garbage)) }191 let siblings = parent.children192 var tiles: [(TileID, [UInt8])] = [(parent, Self.landWithRoads())]193 for s in siblings { tiles.append((s, s == Self.tile ? garbage : MVT.layer("places", []))) }194 let renderer = try await AtlasRenderer(store: TileStore(archive: try SyntheticArchive.make(tiles)), theme: BuiltInThemes.day)195 let size = CGSize(width: 100, height: 100)196 // Centre of the corrupt tile: drawn from the parent (land). Centre of a valid empty sibling: background.197 func centre(_ t: TileID) -> Coordinate { WebMercator.unproject(x: (Double(t.x) + 0.3) / 4096, y: (Double(t.y) + 0.3) / 4096) }198 let a = try await renderer.renderBitmap(camera: AtlasCamera(center: centre(Self.tile), zoom: 12.2), size: size, scale: 1)199 var c = a.color(x: 50, y: 50)!200 #expect(HexColor(red: c.red, green: c.green, blue: c.blue) == BuiltInThemes.day[.land])201 var stats = await renderer.lastFrame202 #expect(stats.fallbackItems == 1 && stats.uncoveredTiles == 0)203 #expect(await renderer.corruptTileCount == 1)204 let sibling = try #require(siblings.first { $0 != Self.tile })205 let b = try await renderer.renderBitmap(camera: AtlasCamera(center: centre(sibling), zoom: 12.2), size: size, scale: 1)206 c = b.color(x: 50, y: 50)!207 #expect(HexColor(red: c.red, green: c.green, blue: c.blue) == BuiltInThemes.day[.water])208 stats = await renderer.lastFrame209 #expect(await renderer.corruptTileCount == 1)210 // After a purge the tile is tried, and counted, again.211 await renderer.purge()212 _ = try await renderer.renderBitmap(camera: AtlasCamera(center: centre(Self.tile), zoom: 12.2), size: size, scale: 1)213 #expect(await renderer.corruptTileCount == 2)214 }215216 @Test func shallowerCameraDrawsTheDeepestTilesItHas() async throws {217 // Archive starting at zoom 12 seen from zoom 10.5: the zoom 12 tile is shrunk, not missing.218 let renderer = try await AtlasRenderer(store: TileStore(archive: try SyntheticArchive.make([(Self.tile, Self.landWithRoads())])), theme: BuiltInThemes.day)219 let bitmap = try await renderer.renderBitmap(camera: AtlasCamera(center: Self.centre, zoom: 10.5), size: CGSize(width: 300, height: 300), scale: 1)220 let c = bitmap.color(x: 150 + 20, y: 150 + 20)!221 #expect(HexColor(red: c.red, green: c.green, blue: c.blue) == BuiltInThemes.day[.land])222 // The tile is 512 / 2^1.5 = 181 points wide: 100 points from the centre is outside it.223 let o = bitmap.color(x: 150 + 100, y: 150)!224 #expect(HexColor(red: o.red, green: o.green, blue: o.blue) == BuiltInThemes.day[.water])225 }226}227