SPB Git

Repository Public

Geometry.swift 5.7 KB 145 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SPIDER — Universal Web Intelligence Engine7// File             : services/ocr-worker/Sources/spider-ocr/Geometry.swift8// Module           : Services9// Repository       : https://git.spboucher.ai/spider10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0413//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          : ISC33//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// SPIDER OCR worker — coordinate spaces.42//43// Three spaces are used:44//   raw    : the pixel grid as stored in the file (what ImageIO decodes).45//   base   : the raw grid after the EXIF orientation is applied (what a browser displays). All46//            coordinates reported by the worker are in this space, in pixels, origin top-left.47//   vision : base rotated clockwise by the detected rotation (0/90/180/270); the space in which48//            Vision sees upright text.49// Every transform is an isometry of the unit square (top-left origin), one of the 8 EXIF codes.50import CoreGraphics51import Foundation5253struct UnitMap: Equatable {54    let a: Double, b: Double, c: Double, d: Double, tx: Double, ty: Double5556    func apply(_ p: CGPoint) -> CGPoint {57        CGPoint(x: a * Double(p.x) + b * Double(p.y) + tx, y: c * Double(p.x) + d * Double(p.y) + ty)58    }5960    /// The matrix is orthogonal, so its inverse is its transpose.61    var inverse: UnitMap {62        UnitMap(a: a, b: c, c: b, d: d, tx: -(a * tx + c * ty), ty: -(b * tx + d * ty))63    }6465    /// `self` first, then `next`.66    func then(_ next: UnitMap) -> UnitMap {67        UnitMap(a: next.a * a + next.b * c, b: next.a * b + next.b * d,68                c: next.c * a + next.d * c, d: next.c * b + next.d * d,69                tx: next.a * tx + next.b * ty + next.tx, ty: next.c * tx + next.d * ty + next.ty)70    }7172    var swapsAxes: Bool { a == 0 }7374    /// raw -> displayed, for an EXIF orientation code (1...8).75    static func exif(_ code: Int) -> UnitMap {76        switch code {77        case 2: return UnitMap(a: -1, b: 0, c: 0, d: 1, tx: 1, ty: 0)78        case 3: return UnitMap(a: -1, b: 0, c: 0, d: -1, tx: 1, ty: 1)79        case 4: return UnitMap(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: 1)80        case 5: return UnitMap(a: 0, b: 1, c: 1, d: 0, tx: 0, ty: 0)81        case 6: return UnitMap(a: 0, b: -1, c: 1, d: 0, tx: 1, ty: 0)82        case 7: return UnitMap(a: 0, b: -1, c: -1, d: 0, tx: 1, ty: 1)83        case 8: return UnitMap(a: 0, b: 1, c: -1, d: 0, tx: 0, ty: 1)84        default: return UnitMap(a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0)85        }86    }8788    /// Clockwise rotation by 0, 90, 180 or 270 degrees.89    static func rotation(_ degrees: Int) -> UnitMap {90        switch ((degrees % 360) + 360) % 360 {91        case 90: return exif(6)92        case 180: return exif(3)93        case 270: return exif(8)94        default: return exif(1)95        }96    }9798    /// The EXIF code equivalent to this map.99    var exifCode: Int {100        for code in 1...8 where UnitMap.exif(code) == self { return code }101        return 1102    }103}104105func boundingRect(_ points: [CGPoint]) -> CGRect {106    guard let first = points.first else { return .zero }107    var minX = first.x, maxX = first.x, minY = first.y, maxY = first.y108    for p in points.dropFirst() {109        minX = min(minX, p.x); maxX = max(maxX, p.x)110        minY = min(minY, p.y); maxY = max(maxY, p.y)111    }112    return CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY)113}114115func corners(_ r: CGRect) -> [CGPoint] {116    [CGPoint(x: r.minX, y: r.minY), CGPoint(x: r.maxX, y: r.minY),117     CGPoint(x: r.maxX, y: r.maxY), CGPoint(x: r.minX, y: r.maxY)]118}119120func iou(_ a: CGRect, _ b: CGRect) -> Double {121    let inter = a.intersection(b)122    if inter.isNull || inter.isEmpty { return 0 }123    let i = Double(inter.width * inter.height)124    let u = Double(a.width * a.height + b.width * b.height) - i125    return u > 0 ? i / u : 0126}127128/// Text similarity in [0, 1]: 1 - Levenshtein / max length, on case-folded text without whitespace.129func textSimilarity(_ lhs: String, _ rhs: String) -> Double {130    let x = Array(lhs.lowercased().filter { !$0.isWhitespace })131    let y = Array(rhs.lowercased().filter { !$0.isWhitespace })132    if x.isEmpty && y.isEmpty { return 1 }133    if x.isEmpty || y.isEmpty { return 0 }134    var prev = Array(0...y.count)135    var cur = [Int](repeating: 0, count: y.count + 1)136    for i in 1...x.count {137        cur[0] = i138        for j in 1...y.count {139            cur[j] = min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (x[i - 1] == y[j - 1] ? 0 : 1))140        }141        swap(&prev, &cur)142    }143    return 1 - Double(prev[y.count]) / Double(max(x.count, y.count))144}145
Files main
Menu

Theme