Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/Features/Scanner/SimulatedCamera.swift8// Module : SPVault9// Repository : https://git.spboucher.ai/sp-id-card-vault10// Project Website : Not specified11// 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 : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041#if DEBUG || targetEnvironment(simulator)42import CoreGraphics43import CoreImage44import Foundation4546/// Stand-in for the camera where there is none (the iOS Simulator) and for47/// tests. It "photographs" a flat card artwork with a real pinhole-camera model48/// — the card is rotated in 3D, projected, and laid on a table — so detection49/// and perspective correction face the geometry of a true photograph.50/// Compiled out of Release device builds.51enum SimulatedCamera {52 struct Shot: @unchecked Sendable {53 var image: CGImage54 /// Exact outline of the card in the shot (normalised, origin top-left).55 var quad: CardQuad56 }5758 /// - Parameters:59 /// - yaw: rotation of the card about its vertical axis, degrees.60 /// - pitch: rotation about its horizontal axis, degrees.61 /// - roll: in-plane rotation, degrees.62 /// - fill: card width as a fraction of the picture width when seen flat.63 static func photograph(_ card: CGImage, yaw: Double = 13, pitch: Double = 9, roll: Double = -3, fill: Double = 0.7,64 canvas: CGSize = CGSize(width: 3024, height: 4032)) -> Shot {65 let focal = Double(max(canvas.width, canvas.height)) * 0.78 // ≈ 26 mm equivalent66 let distance = focal / (fill * Double(canvas.width)) // card width = 1 unit67 let halfW = 0.5, halfH = 0.5 * Double(card.height) / Double(card.width)68 let a = yaw * .pi / 180, b = pitch * .pi / 180, c = roll * .pi / 18069 func project(_ x0: Double, _ y0: Double) -> CGPoint {70 let x = x0 * cos(c) - y0 * sin(c), y = x0 * sin(c) + y0 * cos(c)71 let x1 = x * cos(a), z1 = -x * sin(a)72 let y2 = y * cos(b) - z1 * sin(b), z2 = y * sin(b) + z1 * cos(b)73 let z = distance + z274 // Core Image coordinates: origin bottom-left, y up.75 return CGPoint(x: Double(canvas.width) / 2 + focal * x1 / z, y: Double(canvas.height) / 2 + focal * y2 / z)76 }77 let tl = project(-halfW, halfH), tr = project(halfW, halfH), br = project(halfW, -halfH), bl = project(-halfW, -halfH)78 let warped = CIImage(cgImage: card).applyingFilter("CIPerspectiveTransform", parameters: [79 "inputTopLeft": CIVector(cgPoint: tl), "inputTopRight": CIVector(cgPoint: tr),80 "inputBottomRight": CIVector(cgPoint: br), "inputBottomLeft": CIVector(cgPoint: bl)])81 let frame = CGRect(origin: .zero, size: canvas)82 // A walnut table top: two tones and a soft pool of light.83 let table = CIFilter(name: "CILinearGradient", parameters: [84 "inputPoint0": CIVector(x: 0, y: 0), "inputPoint1": CIVector(x: canvas.width, y: canvas.height),85 "inputColor0": CIColor(red: 0.20, green: 0.15, blue: 0.11), "inputColor1": CIColor(red: 0.33, green: 0.26, blue: 0.20)])!.outputImage!86 .cropped(to: frame)87 let light = CIFilter(name: "CIRadialGradient", parameters: [88 "inputCenter": CIVector(x: canvas.width * 0.4, y: canvas.height * 0.62), "inputRadius0": 0, "inputRadius1": canvas.height * 0.6,89 "inputColor0": CIColor(red: 1, green: 0.95, blue: 0.85, alpha: 0.10), "inputColor1": CIColor(red: 1, green: 1, blue: 1, alpha: 0)])!.outputImage!90 .cropped(to: frame)91 let composed = warped.composited(over: light.composited(over: table))92 let image = ImagePipeline.context.createCGImage(composed, from: frame, format: .RGBA8, colorSpace: ImagePipeline.sRGB)!93 func normalised(_ p: CGPoint) -> CGPoint { CGPoint(x: p.x / canvas.width, y: 1 - p.y / canvas.height) }94 return Shot(image: image, quad: CardQuad(topLeft: normalised(tl), topRight: normalised(tr),95 bottomRight: normalised(br), bottomLeft: normalised(bl)))96 }97}98#endif99