Skip to content

Commit

Permalink
Merge pull request #87 from cocoatype/81-extract-observations-to-module
Browse files Browse the repository at this point in the history
Extract Observations to module
  • Loading branch information
Arclite authored May 9, 2024
2 parents fbb0b46 + d42aae3 commit 0b7cac6
Show file tree
Hide file tree
Showing 45 changed files with 166 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Created by Geoff Pado on 5/8/24.
// Copyright © 2024 Cocoatype, LLC. All rights reserved.

import CoreGraphics

extension CGPoint {
static func flippedPoint(from point: CGPoint, scaledTo size: CGSize) -> CGPoint {
var scaledPoint = point

#if canImport(UIKit)
scaledPoint.y = (1.0 - scaledPoint.y)
#endif

scaledPoint.x *= size.width
scaledPoint.y *= size.height

return scaledPoint
}
}

extension CGSize {
static func * (size: CGSize, multiplier: CGFloat) -> CGSize {
return CGSize(width: size.width * multiplier, height: size.height * multiplier)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Created by Geoff Pado on 5/29/20.
// Copyright © 2020 Cocoatype, LLC. All rights reserved.

public extension String {
extension String {
var words: [(String, Range<String.Index>)] {
var words = [(String, Range<String.Index>)]()
enumerateSubstrings(in: startIndex..<endIndex, options: [.byWords]) { word, wordRange, _, _ in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// Created by Geoff Pado on 5/1/19.
// Copyright © 2019 Cocoatype, LLC. All rights reserved.

#if canImport(AppKit)
import AppKit
#elseif canImport(UIKit)
import UIKit
#endif
import CoreGraphics
import Foundation

struct CharacterObservation: Hashable {
let bounds: Shape
let textObservationUUID: UUID
public struct CharacterObservation: Hashable {
public let bounds: Shape
public let textObservationUUID: UUID
}

extension CGPoint: Hashable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
import Vision

public struct RecognizedTextObservation: TextObservation, RedactableObservation {
init?(_ recognizedText: RecognizedText, imageSize: CGSize) {
public init?(_ recognizedText: RecognizedText, imageSize: CGSize) {
self.recognizedText = recognizedText
self.imageSize = imageSize

Expand Down Expand Up @@ -42,7 +42,7 @@ public struct RecognizedTextObservation: TextObservation, RedactableObservation
}
}

var allWordObservations: [WordObservation] {
public var allWordObservations: [WordObservation] {
wordObservations { _, _ in true }
}

Expand All @@ -55,7 +55,7 @@ public struct RecognizedTextObservation: TextObservation, RedactableObservation
private let recognizedText: RecognizedText
private let imageSize: CGSize

let characterObservations: [CharacterObservation]
public let characterObservations: [CharacterObservation]
}

extension Shape {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

import Foundation

protocol RedactableObservation {
public protocol RedactableObservation {
var characterObservations: [CharacterObservation] { get }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public protocol TextObservation: Equatable {
var bounds: Shape { get }
}

extension TextObservation {
public extension TextObservation {
var path: CGPath { bounds.path }
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
// Created by Geoff Pado on 5/17/22.
// Copyright © 2022 Cocoatype, LLC. All rights reserved.

import Vision

#if canImport(AppKit)
import AppKit
#elseif canImport(UIKit)
#endif

#if canImport(UIKit)
import UIKit
#endif

import Vision

public struct TextRectangleObservation: TextObservation, RedactableObservation {
#if canImport(UIKit)
init(_ textObservation: VNTextObservation, in image: UIImage) {
public init(_ textObservation: VNTextObservation, in image: UIImage) {
let imageSize = image.size * image.scale
self.init(textObservation, scaledTo: imageSize)
}
#elseif canImport(AppKit)
init(_ textObservation: VNTextObservation, in image: NSImage) {
#endif

#if canImport(AppKit) && !targetEnvironment(macCatalyst)
public init(_ textObservation: VNTextObservation, in image: NSImage) {
self.init(textObservation, scaledTo: image.size)
}
#endif
Expand All @@ -32,5 +36,5 @@ public struct TextRectangleObservation: TextObservation, RedactableObservation {
}

public let bounds: Shape
let characterObservations: [CharacterObservation]
public let characterObservations: [CharacterObservation]
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Created by Geoff Pado on 5/17/22.
// Copyright © 2022 Cocoatype, LLC. All rights reserved.

#if canImport(AppKit)
import AppKit
#elseif canImport(UIKit)
import UIKit
#endif
import CoreGraphics
import Foundation

public struct WordObservation: TextObservation {
init?(recognizedText: RecognizedText, string: String, range: Range<String.Index>, imageSize: CGSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

import Vision

struct RecognizedText: Equatable {
public struct RecognizedText: Equatable {
let recognizedText: VisionText
let uuid: UUID

var string: String { recognizedText.string }

init(recognizedText: VisionText, uuid: UUID) {
public init(recognizedText: VisionText, uuid: UUID) {
self.recognizedText = recognizedText
self.uuid = uuid
}

static func == (lhs: RecognizedText, rhs: RecognizedText) -> Bool {
public static func == (lhs: RecognizedText, rhs: RecognizedText) -> Bool {
lhs.uuid == rhs.uuid
}
}

protocol VisionText {
public protocol VisionText {
func boundingBox(for range: Range<String.Index>) throws -> VNRectangleObservation?
var string: String { get }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import Foundation
import CoreGraphics

public struct Shape: Hashable {
let bottomLeft: CGPoint
let bottomRight: CGPoint
let topLeft: CGPoint
let topRight: CGPoint
public let bottomLeft: CGPoint
public let bottomRight: CGPoint
public let topLeft: CGPoint
public let topRight: CGPoint

internal init(bottomLeft: CGPoint, bottomRight: CGPoint, topLeft: CGPoint, topRight: CGPoint) {
public init(bottomLeft: CGPoint, bottomRight: CGPoint, topLeft: CGPoint, topRight: CGPoint) {
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
self.topLeft = topLeft
Expand All @@ -26,9 +26,9 @@ public struct Shape: Hashable {
)
}

var boundingBox: CGRect { path.boundingBox }
public var boundingBox: CGRect { path.boundingBox }

var path: CGPath {
public var path: CGPath {
let path = CGMutablePath()
path.move(to: topLeft)
path.addLine(to: bottomLeft)
Expand All @@ -52,22 +52,22 @@ public struct Shape: Hashable {
)
}

var angle: Double {
public var angle: Double {
guard (centerRight.x - centerLeft.x) != 0 else { return .pi / 2 }
// leftyLoosey by @KaenAitch on 2/3/22
// the slope of the primary axis of the shape
let leftyLoosey = Double((centerRight.y - centerLeft.y) / (centerRight.x - centerLeft.x))
return atan(leftyLoosey)
}

var center: CGPoint {
public var center: CGPoint {
CGPoint(
x: (topLeft.x + bottomLeft.x + bottomRight.x + topRight.x) / 4.0,
y: (topLeft.y + bottomLeft.y + bottomRight.y + topRight.y) / 4.0
)
}

func union(_ other: Shape) -> Shape {
public func union(_ other: Shape) -> Shape {
let transform = CGAffineTransformMakeRotation(angle)

let ourRotatedCenterLeft = centerLeft.applying(transform)
Expand Down Expand Up @@ -95,11 +95,11 @@ public struct Shape: Hashable {
}

static let zero = Shape(bottomLeft: .zero, bottomRight: .zero, topLeft: .zero, topRight: .zero)
var isNotZero: Bool {
public var isNotZero: Bool {
self != Self.zero
}

var isNotEmpty: Bool {
public var isNotEmpty: Bool {
// https://math.stackexchange.com/a/1259133
let shoelace =
bottomLeft.x * bottomRight.y +
Expand Down
30 changes: 30 additions & 0 deletions Modules/Capabilities/Observations/Tests/ShapeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Created by Geoff Pado on 10/19/23.
// Copyright © 2023 Cocoatype, LLC. All rights reserved.

import XCTest

@testable import Observations

final class ShapeTests: XCTestCase {
func testIsNotEmptyReturnsFalseForEmptyShape() {
let emptyShape = Shape(
bottomLeft: CGPoint(x: 5, y: 5),
bottomRight: CGPoint(x: 5, y: 5),
topLeft: CGPoint(x: 5, y: 5),
topRight: CGPoint(x: 5, y: 5)
)

XCTAssertFalse(emptyShape.isNotEmpty)
}

func testIsNotEmptyReturnsTrueForNonEmptyShape() {
let shape = Shape(
bottomLeft: CGPoint(x: 0, y: 5),
bottomRight: CGPoint(x: 5, y: 5),
topLeft: CGPoint(x: 0, y: 0),
topRight: CGPoint(x: 5, y: 0)
)

XCTAssertTrue(shape.isNotEmpty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Editing
import Intents
import os.log
import Observations
import UIKit
import UniformTypeIdentifiers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright © 2022 Cocoatype, LLC. All rights reserved.

import Intents
import Observations
import UniformTypeIdentifiers
import Vision
import XCTest
Expand Down
1 change: 1 addition & 0 deletions Modules/Legacy/Editing/Sources/BrushStampFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright © 2020 Cocoatype, LLC. All rights reserved.

import ErrorHandling
import Observations
import UIKit

public class BrushStampFactory: NSObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright © 2022 Cocoatype, LLC. All rights reserved.

@_implementationOnly import ClippingBezier
import Observations
import UIKit

class PhotoEditingObservationDebugView: PhotoEditingRedactionView {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Created by Geoff Pado on 4/27/19.
// Copyright © 2019 Cocoatype, LLC. All rights reserved.

import Observations
import UIKit

class PhotoEditingObservationVisualizationView: PhotoEditingRedactionView {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Created by Geoff Pado on 4/27/19.
// Copyright © 2019 Cocoatype, LLC. All rights reserved.

import Observations
import UIKit

class PhotoEditingScrollView: UIScrollView {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Created by Geoff Pado on 5/27/19.
// Copyright © 2019 Cocoatype, LLC. All rights reserved.

import Observations
import UIKit

public class PhotoEditingView: UIView, UIScrollViewDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import AutoRedactionsUI
import Defaults
import Observations
import Photos
import UIKit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright © 2019 Cocoatype, LLC. All rights reserved.

@_implementationOnly import ClippingBezier
import Observations
import UIKit

class PhotoEditingWorkspaceView: UIControl, UIGestureRecognizerDelegate {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Created by Geoff Pado on 1/2/20.
// Copyright © 2020 Cocoatype, LLC. All rights reserved.

import Observations
import UIKit

class RedactionPathLayer: CALayer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// Created by Geoff Pado on 4/10/19.
// Copyright © 2019 Cocoatype, LLC. All rights reserved.

#if canImport(AppKit)
import AppKit
#elseif canImport(UIKit)
import UIKit
#endif
import CoreGraphics

public extension CGSize {
static func * (size: CGSize, multiplier: CGFloat) -> CGSize {
Expand All @@ -26,19 +22,6 @@ public extension CGPoint {
return CGPoint(x: point.x + size.width, y: point.y + size.height)
}

static func flippedPoint(from point: CGPoint, scaledTo size: CGSize) -> CGPoint {
var scaledPoint = point

#if canImport(UIKit)
scaledPoint.y = (1.0 - scaledPoint.y)
#endif

scaledPoint.x *= size.width
scaledPoint.y *= size.height

return scaledPoint
}

func isEqual(to otherPoint: CGPoint, accuracy: Double) -> Bool {
return (abs(x - otherPoint.x) < accuracy) && (abs(y - otherPoint.y) < accuracy)
}
Expand Down
Loading

0 comments on commit 0b7cac6

Please sign in to comment.