Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassMaps/Maps/GlassMapEngine.swift8// Module : SPGlassMaps9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import Observation43import SPGlassCore4445/// What the map controls need from a map, whatever draws it.46///47/// GlassKit's map components never import a map framework: they display injected state and call injected48/// closures. A map engine (MapKit through ``GlassMapKitEngine``, or a proprietary renderer) conforms to this49/// protocol so that the controls can be wired in one line.50///51/// ```swift52/// @MainActor @Observable53/// final class TileEngine: GlassMapEngine {54/// private(set) var heading: Double? = 055/// private(set) var zoomLevel = 12.056/// var canZoomIn: Bool { zoomLevel < 20 }57/// var canZoomOut: Bool { zoomLevel > 2 }58/// func zoomIn() { zoomLevel = min(zoomLevel + 1, 20) }59/// func zoomOut() { zoomLevel = max(zoomLevel - 1, 2) }60/// func resetNorth() { heading = 0 }61/// func centerOnUser() { /* ask the renderer to follow the position the host already has */ }62/// }63/// ```64///65/// Conforming types are `@Observable` classes isolated to the main actor.66@MainActor67public protocol GlassMapEngine: AnyObject, Observable {68 /// Direction the top of the map points to, in degrees clockwise from north; `nil` when it is unknown.69 var heading: Double? { get }70 /// False at the closest zoom.71 var canZoomIn: Bool { get }72 /// False at the farthest zoom.73 var canZoomOut: Bool { get }7475 func zoomIn()76 func zoomOut()77 /// Rotates the map so that north is up.78 func resetNorth()79 /// Centres the map on the user's position. Engines never request the location permission here: the80 /// host application does, and reports the outcome through ``GlassLocationState``.81 func centerOnUser()82}8384/// Zoom arithmetic for engines that express zoom as a camera distance in metres.85public enum GlassMapZoom {86 public enum Direction: Sendable {87 case `in`, out88 }8990 /// The camera distance after one zoom step, kept inside `range`.91 /// - Parameter factor: ratio of one step; values at or below 1 are treated as 2.92 public static func distance(after direction: Direction, from distance: Double, factor: Double = 2, in range: ClosedRange<Double>) -> Double {93 let step = factor.isFinite && factor > 1 ? factor : 294 let current = distance.isFinite && distance > 0 ? distance : range.upperBound95 let next = direction == .in ? current / step : current * step96 return min(max(next, range.lowerBound), range.upperBound)97 }9899 /// Whether a further zoom in would change the distance.100 public static func canZoomIn(distance: Double, in range: ClosedRange<Double>) -> Bool {101 distance.isFinite && distance > range.lowerBound102 }103104 /// Whether a further zoom out would change the distance.105 public static func canZoomOut(distance: Double, in range: ClosedRange<Double>) -> Bool {106 distance.isFinite && distance < range.upperBound107 }108}109110/// Heading arithmetic shared by the compass and the engines.111public enum GlassHeading {112 /// Brings any angle into `0..<360`. NaN and infinite values become 0.113 public static func normalized(_ degrees: Double) -> Double {114 guard degrees.isFinite else { return 0 }115 let remainder = degrees.truncatingRemainder(dividingBy: 360)116 let positive = remainder < 0 ? remainder + 360 : remainder117 return positive >= 360 ? 0 : positive118 }119120 /// The signed rotation from one heading to another along the shortest arc, in `-180..<180`.121 public static func shortestDelta(from: Double, to: Double) -> Double {122 let difference = normalized(to) - normalized(from)123 if difference >= 180 { return difference - 360 }124 if difference < -180 { return difference + 360 }125 return difference126 }127128 /// The needle's next rotation angle: the current (unbounded) angle moved along the shortest arc so that129 /// the needle points north for `heading`. Keeping the angle unbounded avoids a full turn between 359° and 0°.130 public static func needleRotation(current: Double, heading: Double) -> Double {131 let base = current.isFinite ? current : 0132 return base + shortestDelta(from: base, to: -heading)133 }134135 /// The heading as whole degrees, 0…359.136 public static func displayDegrees(_ heading: Double) -> Int {137 Int(normalized(heading).rounded()) % 360138 }139140 /// What assistive technologies read: "Heading 270 degrees" or "Heading unavailable".141 public static func accessibilityValue(_ heading: Double?) -> String {142 guard let heading, heading.isFinite else { return GlassL10n.string(.compassUnavailable) }143 return GlassL10n.string(.compassHeading, displayDegrees(heading))144 }145}146