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/LineTessellatorTests.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 Foundation89import ORIXAtlas90import simd91import Testing9293private func area(_ m: LineMesh, halfWidth: Float) -> Float {94 var sum: Float = 095 for t in stride(from: 0, to: m.indices.count, by: 3) {96 let a = m.vertices[Int(m.indices[t])].resolved(halfWidth: halfWidth), b = m.vertices[Int(m.indices[t + 1])].resolved(halfWidth: halfWidth), c = m.vertices[Int(m.indices[t + 2])].resolved(halfWidth: halfWidth)97 sum += abs((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) / 298 }99 return sum100}101102private func distanceToPolyline(_ p: SIMD2<Float>, _ line: [SIMD2<Float>], closed: Bool = false) -> Float {103 var best = Float.infinity104 let count = closed ? line.count : line.count - 1105 for i in 0..<count {106 let a = line[i], b = line[(i + 1) % line.count]107 let ab = b - a108 let t = max(0, min(1, simd_dot(p - a, ab) / max(1e-12, simd_dot(ab, ab))))109 best = min(best, simd_distance(p, a + ab * t))110 }111 return best112}113114private func stroke(_ pts: [SIMD2<Float>], closed: Bool = false, join: LineJoin, cap: LineCap, miterLimit: Float = 2) -> LineMesh {115 var m = LineMesh()116 LineTessellator.tessellate(pts, closed: closed, join: join, cap: cap, miterLimit: miterLimit, into: &m)117 return m118}119120@Suite struct LineTessellatorTests {121 @Test func straightSegmentIsOneQuad() {122 let m = stroke([SIMD2(0, 0), SIMD2(100, 0)], join: .miter, cap: .butt)123 #expect(m.vertices.count == 4 && m.indices.count == 6)124 #expect(abs(area(m, halfWidth: 3) - 600) < 1e-3)125 #expect(m.vertices.allSatisfy { abs(simd_length($0.extrusion) - 1) < 1e-6 && $0.extrusion.x == 0 })126 // The width is not in the geometry: the same mesh at another width.127 #expect(abs(area(m, halfWidth: 10) - 2000) < 1e-3)128 }129130 @Test func capsAddExactlyTheirShape() {131 let line = [SIMD2<Float>(0, 0), SIMD2(100, 0)]132 let h: Float = 4133 let square = stroke(line, join: .miter, cap: .square)134 #expect(abs(area(square, halfWidth: h) - (100 + 2 * h) * 2 * h) < 1e-2)135 let round = stroke(line, join: .miter, cap: .round)136 let extra = area(round, halfWidth: h) - 100 * 2 * h137 // Two half discs approximated by 8 chords each: slightly less than a full disc, within 3 %.138 #expect(extra < .pi * h * h && extra > .pi * h * h * 0.97)139 // Every rim vertex of a round cap is exactly at the half width from the line.140 for v in round.vertices { #expect(distanceToPolyline(v.resolved(halfWidth: h), line) <= h + 1e-3) }141 // Round caps reach beyond the ends, butt caps do not.142 #expect(round.vertices.map { $0.resolved(halfWidth: h).x }.min()! < -h + 1e-3)143 #expect(stroke(line, join: .miter, cap: .butt).vertices.map { $0.resolved(halfWidth: h).x }.min()! == 0)144 }145146 @Test func miterJoinAtARightAngle() {147 let line = [SIMD2<Float>(0, 0), SIMD2(100, 0), SIMD2(100, 100)]148 let m = stroke(line, join: .miter, cap: .butt)149 // Two shared vertices at the corner: 6 vertices, 2 quads.150 #expect(m.vertices.count == 6 && m.indices.count == 12)151 let lengths = m.vertices.map { simd_length($0.extrusion) }152 #expect(abs(lengths.max()! - Float(2).squareRoot()) < 1e-5)153 // Area of an L of width 2h with a sharp corner: 2 × 100 × 2h (the corner square is counted once by the miters).154 #expect(abs(area(m, halfWidth: 5) - 2000) < 1e-2)155 }156157 @Test func miterLimitFallsBackToABevel() {158 // A 170° turn: the miter would be 11.5 half widths long.159 let tip = SIMD2<Float>(100, 0)160 let back = SIMD2<Float>(100 - 100 * cos(10 * .pi / 180), 100 * sin(10 * .pi / 180))161 let line = [SIMD2<Float>(0, 0), tip, back]162 let limited = stroke(line, join: .miter, cap: .butt, miterLimit: 2)163 #expect(limited.vertices.allSatisfy { simd_length($0.extrusion) <= 1 + 1e-5 })164 #expect(limited.vertices.count == 9 && limited.indices.count == 15)165 for v in limited.vertices { #expect(distanceToPolyline(v.resolved(halfWidth: 6), line) <= 6 + 1e-3) }166 let unlimited = stroke(line, join: .miter, cap: .butt, miterLimit: 20)167 #expect(abs(unlimited.vertices.map { simd_length($0.extrusion) }.max()! - 1 / sin(5 * .pi / 180)) < 1e-2)168 // The limit is respected for any turn angle.169 for degrees in stride(from: 5.0, through: 175, by: 5) {170 let a = Float(degrees * .pi / 180)171 let m = stroke([SIMD2(0, 0), SIMD2(50, 0), SIMD2(50 + 50 * cos(a), 50 * sin(a))], join: .miter, cap: .butt, miterLimit: 2)172 #expect(m.vertices.allSatisfy { simd_length($0.extrusion) <= 2 + 1e-4 })173 }174 }175176 @Test func bevelAndRoundJoinsStayWithinTheHalfWidth() {177 let line = [SIMD2<Float>(0, 0), SIMD2(100, 0), SIMD2(100, 100), SIMD2(0, 60), SIMD2(40, 200)]178 let h: Float = 8179 for join in [LineJoin.bevel, .round] {180 let m = stroke(line, join: join, cap: .round)181 #expect(m.indices.allSatisfy { Int($0) < m.vertices.count })182 for v in m.vertices { #expect(distanceToPolyline(v.resolved(halfWidth: h), line) <= h * 1.001 + 1e-3) }183 }184 // A round join covers more than a bevel and less than a miter.185 let corner = [SIMD2<Float>(0, 0), SIMD2(100, 0), SIMD2(100, 100)]186 let bevel = area(stroke(corner, join: .bevel, cap: .butt), halfWidth: h)187 let round = area(stroke(corner, join: .round, cap: .butt), halfWidth: h)188 #expect(round > bevel)189 // Outer wedge of a 90° turn: bevel h²/2, round πh²/4 (approximated from inside).190 #expect(abs((round - bevel) - (.pi / 4 - 0.5) * h * h) < 0.03 * h * h)191 }192193 @Test func gentleRoundJoinsAreDrawnAsMiters() {194 // 10° turns: a fan would be invisible, two shared vertices are enough.195 var pts: [SIMD2<Float>] = []196 for i in 0..<10 { let a = Float(i) * 10 * .pi / 180; pts.append(SIMD2(100 * cos(a), 100 * sin(a))) }197 let m = stroke(pts, join: .round, cap: .butt)198 #expect(m.vertices.count == 2 * pts.count)199 #expect(m.vertices.allSatisfy { simd_length($0.extrusion) <= LineTessellator.roundAsMiterThreshold + 1e-4 })200 }201202 @Test func closedLinesHaveNoCaps() {203 let squareRing = [SIMD2<Float>(0, 0), SIMD2(100, 0), SIMD2(100, 100), SIMD2(0, 100)]204 let m = stroke(squareRing, closed: true, join: .miter, cap: .round)205 #expect(m.vertices.count == 8 && m.indices.count == 4 * 6)206 // A frame: outer square 100 + 2h, inner 100 - 2h.207 let h: Float = 5208 #expect(abs(area(m, halfWidth: h) - (110 * 110 - 90 * 90)) < 1e-1)209 // The same ring given with its closing point.210 let again = stroke(squareRing + [squareRing[0]], closed: true, join: .miter, cap: .round)211 #expect(again.vertices.count == 8)212 }213214 @Test func degenerateInputProducesNothing() {215 #expect(stroke([], join: .round, cap: .round).indices.isEmpty)216 #expect(stroke([SIMD2(1, 1)], join: .round, cap: .round).indices.isEmpty)217 #expect(stroke([SIMD2(1, 1), SIMD2(1, 1), SIMD2(1, 1)], join: .round, cap: .round).indices.isEmpty)218 #expect(stroke([SIMD2(.nan, 1), SIMD2(.infinity, 4)], join: .round, cap: .round).indices.isEmpty)219 // Non-finite points are dropped, the rest is drawn.220 let m = stroke([SIMD2(0, 0), SIMD2(.nan, 5), SIMD2(10, 0)], join: .round, cap: .butt)221 #expect(m.vertices.count == 4)222 // A U-turn (direction exactly reversed) does not produce NaN.223 for join in [LineJoin.miter, .bevel, .round] {224 let u = stroke([SIMD2(0, 0), SIMD2(10, 0), SIMD2(0, 0)], join: join, cap: .round)225 #expect(u.vertices.allSatisfy { $0.extrusion.x.isFinite && $0.extrusion.y.isFinite && $0.position.x.isFinite })226 #expect(u.indices.allSatisfy { Int($0) < u.vertices.count })227 }228 }229230 @Test func noDegenerateTrianglesOnOrdinaryInput() {231 var g = SeededGenerator(seed: 5)232 for _ in 0..<200 {233 var pts: [SIMD2<Float>] = [SIMD2(0, 0)]234 for _ in 0..<Int.random(in: 1...30, using: &g) {235 pts.append(pts.last! + SIMD2(Float.random(in: 20...60, using: &g), Float.random(in: -40...40, using: &g)))236 }237 for (join, cap) in [(LineJoin.miter, LineCap.butt), (.round, .round), (.bevel, .square)] {238 let m = stroke(pts, join: join, cap: cap)239 #expect(m.indices.count % 3 == 0 && m.indices.allSatisfy { Int($0) < m.vertices.count })240 var degenerate = 0241 for t in stride(from: 0, to: m.indices.count, by: 3) {242 let a = m.vertices[Int(m.indices[t])].resolved(halfWidth: 3), b = m.vertices[Int(m.indices[t + 1])].resolved(halfWidth: 3), c = m.vertices[Int(m.indices[t + 2])].resolved(halfWidth: 3)243 if abs((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) < 1e-6 { degenerate += 1 }244 }245 #expect(degenerate == 0)246 // The stroke covers at least the length × width (joins and caps only add).247 var length: Float = 0248 for i in 1..<pts.count { length += simd_distance(pts[i - 1], pts[i]) }249 #expect(area(m, halfWidth: 3) >= length * 6 * 0.999)250 }251 }252 }253}254