SPB Git

Repository Public

ShapeRecognizerTests.swift 21.3 KB 364 lines swift

1// SP Notes iOS2// File: Tests/ShapeRecognizerTests.swift3// Description: Smart shapes on synthetic strokes (seeded noise): each of the five shapes at several sizes and rotations is recognised4//              with the right geometry; scribbles, letters, open curves and spirals are left alone; the clean shape replaces the5//              stroke on a PencilKit canvas as one undo step.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import PencilKit10import Testing11import UIKit12@testable import SPNotes1314/// SplitMix64: the same strokes on every run.15struct SeededGenerator: RandomNumberGenerator {16    var state: UInt6417    init(seed: UInt64) { state = seed }18    mutating func next() -> UInt64 {19        state &+= 0x9E37_79B9_7F4A_7C1520        var z = state21        z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B922        z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB23        return z ^ (z >> 31)24    }25}2627/// Synthetic hand-drawn strokes.28struct StrokeMaker {29    var rng: SeededGenerator30    /// Amplitude of the hand's wobble, as a fraction of the size of the shape.31    var noise: CGFloat = 0.0123233    init(seed: UInt64) { rng = SeededGenerator(seed: seed) }3435    /// A polyline through `vertices` with a point every ~3 pt, a slow wobble (proportional to the size) and a tremor of up to36    /// 0.25 × the wobble on each point.37    mutating func hand(_ vertices: [CGPoint], size: CGFloat) -> [CGPoint] {38        var out: [CGPoint] = []39        let phaseX = CGFloat.random(in: 0...(2 * .pi), using: &rng), phaseY = CGFloat.random(in: 0...(2 * .pi), using: &rng)40        var travelled: CGFloat = 041        for i in 1..<vertices.count {42            let a = vertices[i - 1], b = vertices[i], length = hypot(b.x - a.x, b.y - a.y)43            let pieces = max(1, Int(length / 3))44            for k in (i == 1 ? 0 : 1)...pieces {45                let t = CGFloat(k) / CGFloat(pieces), s = travelled + t * length46                let wobble = size * noise47                let dx = wobble * sin(s / size * 5 + phaseX) + CGFloat.random(in: -0.25...0.25, using: &rng) * wobble48                let dy = wobble * cos(s / size * 4 + phaseY) + CGFloat.random(in: -0.25...0.25, using: &rng) * wobble49                out.append(CGPoint(x: a.x + (b.x - a.x) * t + dx, y: a.y + (b.y - a.y) * t + dy))50            }51            travelled += length52        }53        return out54    }5556    mutating func curve(size: CGFloat, count: Int = 160, _ f: (CGFloat) -> CGPoint) -> [CGPoint] {57        let phase = CGFloat.random(in: 0...(2 * .pi), using: &rng)58        return (0..<count).map { i in59            let t = CGFloat(i) / CGFloat(count - 1), p = f(t), wobble = size * noise60            return CGPoint(x: p.x + wobble * sin(t * 11 + phase) + CGFloat.random(in: -0.25...0.25, using: &rng) * wobble,61                           y: p.y + wobble * cos(t * 9 + phase) + CGFloat.random(in: -0.25...0.25, using: &rng) * wobble)62        }63    }64}6566func rotated(_ p: CGPoint, by angle: CGFloat, around c: CGPoint) -> CGPoint {67    let dx = p.x - c.x, dy = p.y - c.y68    return CGPoint(x: c.x + dx * cos(angle) - dy * sin(angle), y: c.y + dx * sin(angle) + dy * cos(angle))69}7071private func degrees(_ value: CGFloat) -> CGFloat { value * .pi / 180 }72private func gap(_ a: CGPoint, _ b: CGPoint) -> CGFloat { hypot(a.x - b.x, a.y - b.y) }73/// Every expected point has a found point within `tolerance`.74private func matches(_ expected: [CGPoint], _ found: [CGPoint], tolerance: CGFloat) -> Bool {75    expected.allSatisfy { e in found.contains { gap($0, e) <= tolerance } }76}7778@Suite("Shape — reconnaissance des formes") struct ShapeRecognizerTests {79    static let sizes: [CGFloat] = [60, 150, 420]80    static let rotations: [CGFloat] = [0, 12, 33, 61, 90, 137, 201, 298].map(degrees)81    static let center = CGPoint(x: 500, y: 400)8283    @Test func linesAreRecognisedAtEverySizeAndAngle() {84        var count = 085        for (i, size) in Self.sizes.enumerated() {86            for (j, angle) in Self.rotations.enumerated() {87                var maker = StrokeMaker(seed: UInt64(100 + i * 10 + j))88                let a = rotated(CGPoint(x: Self.center.x - size / 2, y: Self.center.y), by: angle, around: Self.center)89                let b = rotated(CGPoint(x: Self.center.x + size / 2, y: Self.center.y), by: angle, around: Self.center)90                let match = ShapeRecognizer.recognize(maker.hand([a, b], size: size))91                guard case .line(let from, let to)? = match?.shape else { Issue.record("line \(size) at \(angle): \(String(describing: match))"); continue }92                #expect(gap(from, a) <= size * 0.04 && gap(to, b) <= size * 0.04)93                count += 194            }95        }96        #expect(count == 24)97        // a nearly horizontal line is made exactly horizontal98        var maker = StrokeMaker(seed: 7)99        guard case .line(let from, let to)? = ShapeRecognizer.recognize(maker.hand([CGPoint(x: 100, y: 200), CGPoint(x: 400, y: 206)], size: 300))?.shape else { Issue.record("horizontal"); return }100        #expect(from.y == to.y)101    }102103    @Test func arrowsAreRecognisedWithTheirDirection() {104        var count = 0105        for (i, size) in Self.sizes.enumerated() {106            for (j, angle) in Self.rotations.enumerated() {107                for closedHead in [false, true] {108                    var maker = StrokeMaker(seed: UInt64(300 + i * 20 + j * 2 + (closedHead ? 1 : 0)))109                    let tail = CGPoint(x: Self.center.x - size / 2, y: Self.center.y), tip = CGPoint(x: Self.center.x + size / 2, y: Self.center.y)110                    let barb = size * 0.2111                    let up = CGPoint(x: tip.x - barb * cos(degrees(32)), y: tip.y - barb * sin(degrees(32))), down = CGPoint(x: up.x, y: tip.y + barb * sin(degrees(32)))112                    let path = (closedHead ? [tail, tip, up, down, tip] : [tail, tip, up, tip, down]).map { rotated($0, by: angle, around: Self.center) }113                    let match = ShapeRecognizer.recognize(maker.hand(path, size: size))114                    guard case .arrow(let from, let to)? = match?.shape else { Issue.record("arrow \(size) at \(angle) closed=\(closedHead): \(String(describing: match))"); continue }115                    #expect(gap(from, path[0]) <= size * 0.06 && gap(to, path[1]) <= size * 0.06)116                    count += 1117                }118            }119        }120        #expect(count == 48)121    }122123    @Test func rectanglesAreRecognisedWithSizeAndRotation() {124        var count = 0125        for (i, size) in Self.sizes.enumerated() {126            for (j, angle) in Self.rotations.enumerated() {127                for (k, ratio) in [CGFloat(1), 0.55].enumerated() {128                    var maker = StrokeMaker(seed: UInt64(500 + i * 20 + j * 2 + k))129                    let w = size, h = size * ratio130                    let corners = [CGPoint(x: -w / 2, y: -h / 2), CGPoint(x: w / 2, y: -h / 2), CGPoint(x: w / 2, y: h / 2), CGPoint(x: -w / 2, y: h / 2)]131                        .map { rotated(CGPoint(x: Self.center.x + $0.x, y: Self.center.y + $0.y), by: angle, around: Self.center) }132                    // start in the middle of a side (j even) or at a corner, with a small gap at the closure133                    let start = CGPoint(x: (corners[0].x + corners[1].x) / 2, y: (corners[0].y + corners[1].y) / 2)134                    let path = j % 2 == 0 ? [start, corners[1], corners[2], corners[3], corners[0], CGPoint(x: start.x - (start.x - corners[0].x) * 0.08, y: start.y - (start.y - corners[0].y) * 0.08)]135                                          : [corners[0], corners[1], corners[2], corners[3], corners[0]]136                    let match = ShapeRecognizer.recognize(maker.hand(path, size: size))137                    guard case .rectangle(let c, let s, let r)? = match?.shape else { Issue.record("rectangle \(size) at \(angle) ratio \(ratio): \(String(describing: match))"); continue }138                    let diagonal = hypot(w, h)139                    #expect(gap(c, Self.center) <= diagonal * 0.04)140                    #expect(matches(corners, RecognizedShape.corners(center: c, size: s, rotation: r), tolerance: diagonal * 0.07))141                    #expect(r > -.pi / 4 - 0.001 && r <= .pi / 4 + 0.001)142                    count += 1143                }144            }145        }146        #expect(count == 48)147        // a rectangle drawn nearly upright is made upright148        var maker = StrokeMaker(seed: 9)149        let tilted = [CGPoint(x: 100, y: 100), CGPoint(x: 300, y: 104), CGPoint(x: 298, y: 224), CGPoint(x: 98, y: 220), CGPoint(x: 100, y: 100)]150        guard case .rectangle(_, _, let rotation)? = ShapeRecognizer.recognize(maker.hand(tilted, size: 200))?.shape else { Issue.record("upright"); return }151        #expect(rotation == 0)152    }153154    @Test func trianglesAreRecognisedWithTheirCorners() {155        var count = 0156        for (i, size) in Self.sizes.enumerated() {157            for (j, angle) in Self.rotations.enumerated() {158                for (k, shape) in [[CGPoint(x: 0, y: -0.5), CGPoint(x: 0.5, y: 0.4), CGPoint(x: -0.5, y: 0.4)], [CGPoint(x: -0.5, y: -0.3), CGPoint(x: 0.5, y: 0.35), CGPoint(x: -0.4, y: 0.35)]].enumerated() {159                    var maker = StrokeMaker(seed: UInt64(700 + i * 20 + j * 2 + k))160                    let corners = shape.map { rotated(CGPoint(x: Self.center.x + $0.x * size, y: Self.center.y + $0.y * size), by: angle, around: Self.center) }161                    let match = ShapeRecognizer.recognize(maker.hand(corners + [corners[0]], size: size))162                    guard case .triangle(let a, let b, let c)? = match?.shape else { Issue.record("triangle \(size) at \(angle) #\(k): \(String(describing: match))"); continue }163                    #expect(matches(corners, [a, b, c], tolerance: size * 0.07))164                    count += 1165                }166            }167        }168        #expect(count == 48)169    }170171    @Test func ellipsesAndCirclesAreRecognisedWithCentreRadiiAndAxis() {172        var count = 0173        for (i, size) in Self.sizes.enumerated() {174            for (j, angle) in Self.rotations.enumerated() {175                for (k, ratio) in [CGFloat(1), 0.5].enumerated() {176                    var maker = StrokeMaker(seed: UInt64(900 + i * 20 + j * 2 + k))177                    let a = size / 2, b = size / 2 * ratio178                    // a little more than one turn (j odd): the pen goes past its starting point179                    let turn: CGFloat = j % 2 == 1 ? 2 * .pi * 1.04 : 2 * .pi * 0.985180                    let points = maker.curve(size: size) { t in181                        rotated(CGPoint(x: Self.center.x + a * cos(t * turn + 1), y: Self.center.y + b * sin(t * turn + 1)), by: angle, around: Self.center)182                    }183                    let match = ShapeRecognizer.recognize(points)184                    guard case .ellipse(let c, let radii, let rotation)? = match?.shape else { Issue.record("ellipse \(size) at \(angle) ratio \(ratio): \(String(describing: match))"); continue }185                    #expect(gap(c, Self.center) <= size * 0.04)186                    if ratio == 1 {187                        #expect(radii.width == radii.height && rotation == 0)188                        #expect(abs(radii.width - a) <= a * 0.08)189                    } else {190                        #expect(abs(max(radii.width, radii.height) - a) <= a * 0.08 && abs(min(radii.width, radii.height) - b) <= a * 0.08)191                        let major = radii.width >= radii.height ? rotation : rotation + .pi / 2192                        #expect(abs(ShapeRecognizer.normalizedHalfTurn(major - angle)) <= degrees(8))193                    }194                    count += 1195                }196            }197        }198        #expect(count == 48)199    }200201    // MARK: what must be left alone202203    static func impostors() -> [(String, [CGPoint])] {204        var maker = StrokeMaker(seed: 4242)205        func letter(_ name: String, _ unit: [CGPoint], size: CGFloat = 120) -> (String, [CGPoint]) {206            (name, maker.hand(unit.map { CGPoint(x: 300 + $0.x * size, y: 300 + $0.y * size) }, size: size))207        }208        func p(_ x: CGFloat, _ y: CGFloat) -> CGPoint { CGPoint(x: x, y: y) }209        var list: [(String, [CGPoint])] = [210            letter("L", [p(0, 0), p(0, 1), p(0.6, 1)]),211            letter("V", [p(0, 0), p(0.4, 1), p(0.8, 0)]),212            letter("N", [p(0, 1), p(0, 0), p(0.7, 1), p(0.7, 0)]),213            letter("M", [p(0, 1), p(0, 0), p(0.5, 0.7), p(1, 0), p(1, 1)]),214            letter("W", [p(0, 0), p(0.25, 1), p(0.5, 0.3), p(0.75, 1), p(1, 0)]),215            letter("Z", [p(0, 0), p(0.8, 0), p(0, 1), p(0.8, 1)]),216            letter("7", [p(0, 0), p(0.7, 0), p(0.2, 1)]),217            letter("4", [p(0.6, 1), p(0.6, 0), p(0, 0.7), p(0.9, 0.7)]),218            letter("zigzag", (0..<9).map { p(CGFloat($0) * 0.2, $0 % 2 == 0 ? 0 : 0.5) }),219            letter("hook", [p(0, 0), p(1, 0), p(1, 0.5), p(0.3, 0.5)]),220            letter("small tick", [p(0, 0), p(0.5, 0.5)], size: 20),221        ]222        list.append(("C", maker.curve(size: 120) { t in CGPoint(x: 300 + 60 * cos(0.8 + t * 4.7), y: 300 + 60 * sin(0.8 + t * 4.7)) }))223        list.append(("U", maker.curve(size: 120) { t in CGPoint(x: 300 + 50 * cos(.pi - t * .pi), y: 300 + 90 * sin(t * .pi)) }))224        list.append(("S", maker.curve(size: 120) { t in CGPoint(x: 300 + 45 * sin(t * 2 * .pi), y: 240 + 120 * t) }))225        list.append(("half circle", maker.curve(size: 160) { t in CGPoint(x: 300 + 80 * cos(t * .pi), y: 300 + 80 * sin(t * .pi)) }))226        list.append(("quarter arc", maker.curve(size: 200) { t in CGPoint(x: 300 + 200 * cos(t * .pi / 2), y: 300 + 200 * sin(t * .pi / 2)) }))227        list.append(("wave", maker.curve(size: 300) { t in CGPoint(x: 100 + 300 * t, y: 300 + 30 * sin(t * 6 * .pi)) }))228        list.append(("spiral 2.5 turns", maker.curve(size: 200, count: 300) { t in CGPoint(x: 300 + (15 + 85 * t) * cos(t * 5 * .pi), y: 300 + (15 + 85 * t) * sin(t * 5 * .pi)) }))229        list.append(("spiral 1.5 turns", maker.curve(size: 200, count: 240) { t in CGPoint(x: 300 + (20 + 80 * t) * cos(t * 3 * .pi), y: 300 + (20 + 80 * t) * sin(t * 3 * .pi)) }))230        list.append(("eight", maker.curve(size: 160, count: 240) { t in CGPoint(x: 300 + 50 * sin(t * 4 * .pi), y: 300 + 80 * sin(t * 2 * .pi)) }))231        list.append(("loop e", maker.curve(size: 120, count: 200) { t in CGPoint(x: 300 + 120 * t + 40 * cos(t * 2 * .pi + .pi), y: 300 + 40 * sin(t * 2 * .pi + .pi)) }))232        list.append(("heart", maker.curve(size: 160, count: 240) { t in233            let a = t * 2 * .pi234            return CGPoint(x: 300 + 5 * 16 * pow(sin(a), 3), y: 300 - 5 * (13 * cos(a) - 5 * cos(2 * a) - 2 * cos(3 * a) - cos(4 * a)))235        }))236        list.append(("star", maker.hand((0...10).map { i in237            let r: CGFloat = i % 2 == 0 ? 90 : 38, a = CGFloat(i) * .pi / 5 - .pi / 2238            return CGPoint(x: 300 + r * cos(a), y: 300 + r * sin(a))239        }, size: 180)))240        // scribbles: random walks with momentum241        for seed in 0..<6 {242            var rng = SeededGenerator(seed: UInt64(9000 + seed))243            var point = CGPoint(x: 300, y: 300), heading = CGFloat.random(in: 0...(2 * .pi), using: &rng), points: [CGPoint] = []244            for _ in 0..<260 {245                heading += CGFloat.random(in: -0.9...0.9, using: &rng)246                point = CGPoint(x: point.x + 4 * cos(heading), y: point.y + 4 * sin(heading))247                points.append(point)248            }249            list.append(("scribble \(seed)", points))250        }251        // hatching back and forth252        list.append(("hatching", maker.hand((0..<14).map { CGPoint(x: 200 + CGFloat($0) * 12, y: $0 % 2 == 0 ? 200 : 320) }, size: 160)))253        return list254    }255256    @Test func scribblesLettersOpenCurvesAndSpiralsAreNotShapes() {257        for (name, points) in Self.impostors() {258            let match = ShapeRecognizer.recognize(points)259            #expect(match == nil, "\(name) was read as \(String(describing: match))")260        }261        #expect(ShapeRecognizer.recognize([]) == nil)262        #expect(ShapeRecognizer.recognize([CGPoint(x: 1, y: 1)]) == nil)263        #expect(ShapeRecognizer.recognize([CGPoint(x: CGFloat.nan, y: 1), CGPoint(x: 5, y: CGFloat.infinity)]) == nil)264    }265266    @Test func aHigherThresholdRefusesSloppyShapes() {267        var maker = StrokeMaker(seed: 77)268        maker.noise = 0.03269        let sloppy = maker.hand([CGPoint(x: 100, y: 100), CGPoint(x: 400, y: 100)], size: 300)270        var strict = ShapeRecognizer.Options.standard271        strict.minimumConfidence = 0.95272        #expect(ShapeRecognizer.recognize(sloppy, options: strict) == nil)273    }274}275276// MARK: - on a canvas277278@MainActor279enum CanvasHost {280    /// A PencilKit canvas in a window, so it has an undo manager.281    static func make() throws -> (UIWindow, PKCanvasView) {282        let scene = try #require(UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first)283        let window = UIWindow(windowScene: scene)284        window.frame = CGRect(x: 0, y: 0, width: 800, height: 600)285        let controller = UIViewController()286        window.rootViewController = controller287        window.isHidden = false288        let canvas = PKCanvasView(frame: controller.view.bounds)289        controller.view.addSubview(canvas)290        return (window, canvas)291    }292}293294@MainActor295@Suite("Shape — remplacement sur le canevas") struct ShapeSnappingTests {296    func handStroke(_ points: [CGPoint], width: CGFloat = 5) -> PKStroke {297        let controls = points.enumerated().map { PKStrokePoint(location: $1, timeOffset: Double($0) * 0.01, size: CGSize(width: width, height: width), opacity: 1, force: 1, azimuth: 0, altitude: .pi / 2) }298        return PKStroke(ink: PKInk(.pen, color: .blue), path: PKStrokePath(controlPoints: controls, creationDate: Date()))299    }300301    @Test func theCleanShapeKeepsInkAndWidthAndPassesThroughItsCorners() throws {302        var maker = StrokeMaker(seed: 5)303        let corners = [CGPoint(x: 100, y: 100), CGPoint(x: 340, y: 100), CGPoint(x: 340, y: 260), CGPoint(x: 100, y: 260)]304        let drawing = PKDrawing(strokes: [handStroke([CGPoint(x: 10, y: 10), CGPoint(x: 30, y: 400)]), handStroke(maker.hand(corners + [corners[0]], size: 240))])305        let snapped = try #require(ShapeSnapping.snappingLastStroke(of: drawing))306        #expect(snapped.match.shape.kind == .rectangle)307        #expect(snapped.drawing.strokes.count == 2)308        let shape = try #require(snapped.drawing.strokes.last)309        #expect(shape.ink.inkType == .pen)310        #expect(InkConversion.inkColor(shape.ink.color).hex == "#0000ffff")311        let curve = shape.path.interpolatedPoints(by: .parametricStep(0.25)).map(\.location)312        #expect(curve.allSatisfy { _ in true })313        #expect(abs(shape.path.interpolatedPoint(at: 5).size.width - 5) < 0.01)314        // sharp corners: the curve passes within 3 pt of each corner of the drawn rectangle, and every point lies on a side315        for corner in corners { #expect(curve.contains { hypot($0.x - corner.x, $0.y - corner.y) < 6 }) }316        guard case .rectangle(let c, let s, let r) = snapped.match.shape else { return }317        let exact = RecognizedShape.corners(center: c, size: s, rotation: r)318        for corner in exact { #expect(curve.contains { hypot($0.x - corner.x, $0.y - corner.y) < 0.01 }) }319        for p in curve { #expect((0..<4).map { ShapeRecognizer.segmentDistance(p, exact[$0], exact[($0 + 1) % 4]) }.min() ?? 9 < 0.35) }320        // a scribble is left as drawn321        let scribble = PKDrawing(strokes: [handStroke(ShapeRecognizerTests.impostors().first { $0.0 == "scribble 1" }?.1 ?? [])])322        #expect(ShapeSnapping.snappingLastStroke(of: scribble) == nil)323        // an arrow is two strokes (shaft and head)324        let arrow = PKDrawing(strokes: [handStroke(maker.hand([CGPoint(x: 100, y: 300), CGPoint(x: 400, y: 300), CGPoint(x: 350, y: 270), CGPoint(x: 400, y: 300), CGPoint(x: 350, y: 330)], size: 300))])325        #expect(ShapeSnapping.snappingLastStroke(of: arrow)?.drawing.strokes.count == 2)326    }327328    @Test func snappingOnTheCanvasIsOneUndoStep() throws {329        let (window, canvas) = try CanvasHost.make()330        defer { window.isHidden = true }331        let undo = try #require(canvas.undoManager)332        undo.groupsByEvent = false333        var maker = StrokeMaker(seed: 11)334        let hand = handStroke(maker.curve(size: 200) { t in CGPoint(x: 300 + 100 * cos(t * 2 * .pi), y: 250 + 100 * sin(t * 2 * .pi)) })335        canvas.drawing = PKDrawing(strokes: [handStroke([CGPoint(x: 20, y: 20), CGPoint(x: 60, y: 90), CGPoint(x: 20, y: 150)]), hand])336        let before = canvas.drawing337338        undo.beginUndoGrouping()339        let match = DrawingCanvasController.snapLastStroke(on: canvas)340        undo.endUndoGrouping()341        #expect(match?.shape.kind == .ellipse)342        #expect(canvas.drawing.strokes.count == 2)343        #expect(canvas.drawing != before)344        guard case .ellipse(let c, let radii, _)? = match?.shape else { return }345        #expect(abs(c.x - 300) < 5 && abs(c.y - 250) < 5 && abs(radii.width - 100) < 6)346        let snapped = canvas.drawing347348        // one undo brings the hand-drawn stroke back, exactly349        #expect(undo.canUndo)350        undo.undo()351        #expect(canvas.drawing == before)352        #expect(canvas.drawing.strokes.last?.path.count == hand.path.count)353        // …and redo puts the shape again354        #expect(undo.canRedo)355        undo.redo()356        #expect(canvas.drawing == snapped)357        // nothing to snap: nothing registered358        canvas.drawing = PKDrawing(strokes: [handStroke(ShapeRecognizerTests.impostors().first { $0.0 == "S" }?.1 ?? [])])359        undo.removeAllActions()360        #expect(DrawingCanvasController.snapLastStroke(on: canvas) == nil)361        #expect(!undo.canUndo)362    }363}364
Files main
Menu

Theme