From 73a618eb4bce8f6e489a66978714c2f29f13a2af Mon Sep 17 00:00:00 2001 From: Paul Zabelin Date: Wed, 10 Apr 2019 13:24:05 -0700 Subject: [PATCH] extract corners() and reuse method for CGRect --- Example/OpenCV-OSX-Tests/OpenCVPerformanceTest.swift | 10 +++++----- Example/OpenCV-OSX-Tests/OpenCV_Spec.swift | 9 +++++---- .../Tests/CompareTransformSpecConfiguration.swift | 12 ++++++------ Example/Tests/NimbleSpecHelper.swift | 11 +++++++++++ Example/Tests/PerformanceTest.swift | 10 +++++----- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/Example/OpenCV-OSX-Tests/OpenCVPerformanceTest.swift b/Example/OpenCV-OSX-Tests/OpenCVPerformanceTest.swift index 856e96b..6303842 100644 --- a/Example/OpenCV-OSX-Tests/OpenCVPerformanceTest.swift +++ b/Example/OpenCV-OSX-Tests/OpenCVPerformanceTest.swift @@ -18,11 +18,11 @@ class OpenCVPerformanceTest: XCTestCase { lowerLeft: CGPoint(x: 459.781253, y: 251.836131)) let start: Quadrilateral = { var one = Quadrilateral() - let overlayFrame = CGRect(x: 0, y: 0, width: 1, height: 1) - one.upperLeft = CGPoint(x: overlayFrame.minX, y: overlayFrame.minY) - one.upperRight = CGPoint(x: overlayFrame.maxX, y: overlayFrame.minY) - one.lowerLeft = CGPoint(x: overlayFrame.minX, y: overlayFrame.maxY) - one.lowerRight = CGPoint(x: overlayFrame.maxX, y: overlayFrame.maxY) + let corners = CGRect(x: 0, y: 0, width: 1, height: 1).corners() + one.upperLeft = corners[0] + one.upperRight = corners[1] + one.lowerLeft = corners[2] + one.lowerRight = corners[3] return one }() diff --git a/Example/OpenCV-OSX-Tests/OpenCV_Spec.swift b/Example/OpenCV-OSX-Tests/OpenCV_Spec.swift index 8fa2bcb..e3a8bac 100644 --- a/Example/OpenCV-OSX-Tests/OpenCV_Spec.swift +++ b/Example/OpenCV-OSX-Tests/OpenCV_Spec.swift @@ -7,10 +7,11 @@ struct OpenCVAdapter: TransformMatrixCalculator { var method: OpenCVTransformer func transform(frame: CGRect, points: [CGPoint]) -> CATransform3D { - let start = Quadrilateral(upperLeft: CGPoint(x: frame.minX, y: frame.minY), - upperRight: CGPoint(x: frame.maxX, y: frame.minY), - lowerRight: CGPoint(x: frame.maxX, y: frame.maxY), - lowerLeft: CGPoint(x: frame.minX, y: frame.maxY)) + let corners = frame.corners() + let start = Quadrilateral(upperLeft: corners[0], + upperRight: corners[1], + lowerRight: corners[3], + lowerLeft: corners[2]) let destination = Quadrilateral(upperLeft: points[0], upperRight: points[1], lowerRight: points[3], diff --git a/Example/Tests/CompareTransformSpecConfiguration.swift b/Example/Tests/CompareTransformSpecConfiguration.swift index 22d5b4f..6ffb27a 100644 --- a/Example/Tests/CompareTransformSpecConfiguration.swift +++ b/Example/Tests/CompareTransformSpecConfiguration.swift @@ -27,8 +27,7 @@ class CompareTransformSpecConfiguration: QuickConfiguration { } it("should be identity for same start and destination") { - let points = [CGPoint(x: 0, y: 0), CGPoint(x: 20, y: 0), CGPoint(x: 0, y: 10), CGPoint(x: 20, y: 10)] - let toItself = transformer.transform(frame: frame, points: points) + let toItself = transformer.transform(frame: frame, points: frame.corners()) expect(toItself) ≈ CATransform3DIdentity } @@ -53,10 +52,11 @@ struct AlgebraMethod: TransformMatrixCalculator { destination.bottomRight = points[3] let start = QuadrilateralCalc() - start.topLeft = CGPoint(x: frame.minX, y: frame.minY) - start.topRight = CGPoint(x: frame.maxX, y: frame.minY) - start.bottomLeft = CGPoint(x: frame.minX, y: frame.maxY) - start.bottomRight = CGPoint(x: frame.maxX, y: frame.maxY) + let corners = frame.corners() + start.topLeft = corners[0] + start.topRight = corners[1] + start.bottomLeft = corners[2] + start.bottomRight = corners[3] return start.rectToQuad(rect: start.box(), quad: destination) } diff --git a/Example/Tests/NimbleSpecHelper.swift b/Example/Tests/NimbleSpecHelper.swift index 82e91f3..c3e910c 100644 --- a/Example/Tests/NimbleSpecHelper.swift +++ b/Example/Tests/NimbleSpecHelper.swift @@ -90,3 +90,14 @@ extension GKRandomSource { return Array(0...3).map {_ in nextPoint()} } } + +extension CGRect { + func corners() -> [CGPoint] { + return [ + CGPoint(x: minX, y: minY), // topLeft + CGPoint(x: maxX, y: minY), // topRight + CGPoint(x: minX, y: maxY), // bottomLeft + CGPoint(x: maxX, y: maxY) // bottomRight + ] + } +} diff --git a/Example/Tests/PerformanceTest.swift b/Example/Tests/PerformanceTest.swift index 1b908d4..7dae95c 100644 --- a/Example/Tests/PerformanceTest.swift +++ b/Example/Tests/PerformanceTest.swift @@ -41,11 +41,11 @@ class PerformanceTest: XCTestCase { destination.bottomRight = points[3] let start = QuadrilateralCalc() - let overlayFrame = CGRect(x: 0, y: 0, width: 1, height: 1) - start.topLeft = CGPoint(x: overlayFrame.minX, y: overlayFrame.minY) - start.topRight = CGPoint(x: overlayFrame.maxX, y: overlayFrame.minY) - start.bottomLeft = CGPoint(x: overlayFrame.minX, y: overlayFrame.maxY) - start.topLeft = CGPoint(x: overlayFrame.maxX, y: overlayFrame.maxY) + let corners = CGRect(x: 0, y: 0, width: 1, height: 1).corners() + start.topLeft = corners[0] + start.topRight = corners[1] + start.bottomLeft = corners[2] + start.topLeft = corners[3] measure { repeatTimes.times {