Repository Public BSD-3-Clause
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Maps9// File : Apps/ORIXMaps/Sources/Explore/TripCoverage.swift10// Module : Explore11// 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 Foundation89import ORIXGeoCore90import ORIXMapPackage9192/// How much of a trip the maps on the device cover, and which published maps would cover the rest.93struct TripCoverage: Equatable {94 /// Share of the sampled trip that lies inside an installed region, 0…1.95 var coveredShare: Double96 /// Installed regions the trip crosses.97 var installed: [MapRegion]98 /// Catalog regions, not installed, that contain a part of the trip no installed region covers.99 var available: [CatalogRegion]100101 var isComplete: Bool { coveredShare >= 0.999 }102103 /// Distance between two samples for a trip of `total` meters: 500 m, more on very long trips (at most about 2 000 samples).104 static func sampleStep(forLength total: Double) -> Double { max(500, total / 2_000) }105106 /// Samples the line and tests each sample against the regions' bounds. The development region ("dev") is ignored.107 static func compute(line points: [Coordinate], regions allRegions: [MapRegion], catalog: [CatalogRegion], installedIDs: Set<String>) -> TripCoverage {108 let total = Geodesy.length(of: points)109 let step = sampleStep(forLength: total)110 var samples: [Coordinate] = []111 var d = 0.0112 while d <= total { if let p = Geodesy.point(along: points, at: d) { samples.append(p.point) }; d += step }113 if let last = points.last { samples.append(last) }114 let regions = allRegions.filter { $0.id != "dev" }115 let covered = samples.filter { s in regions.contains { $0.bounds.contains(s) } }.count116 let used = regions.filter { r in samples.contains { r.bounds.contains($0) } }117 let uncovered = samples.filter { s in !regions.contains { $0.bounds.contains(s) } }118 let offered = catalog.filter { c in !installedIDs.contains(c.id) && uncovered.contains { c.bbox.contains($0) } }119 return TripCoverage(coveredShare: samples.isEmpty ? 0 : Double(covered) / Double(samples.count), installed: used, available: offered)120 }121}122