Skip to content

Commit

Permalink
Resolved merge conflicts?
Browse files Browse the repository at this point in the history
  • Loading branch information
julianschiavo committed Sep 21, 2018
1 parent 9ec3ddd commit 99149e1
Show file tree
Hide file tree
Showing 22 changed files with 1,312 additions and 912 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[submodule "Submodules/WeTransfer-iOS-CI"]
path = Submodules/WeTransfer-iOS-CI
url=https://github.com/WeTransfer/WeTransfer-iOS-CI.git
[submodule "Submodules/ios-snapshot-test-case"]
path = Submodules/ios-snapshot-test-case
url = https://github.com/uber/ios-snapshot-test-case/
branch = master
1 change: 1 addition & 0 deletions Submodules/ios-snapshot-test-case
Submodule ios-snapshot-test-case added at de1c71
1,839 changes: 1,012 additions & 827 deletions WeScan.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions WeScan.xcodeproj/xcshareddata/xcschemes/WeScan.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@
ReferencedContainer = "container:WeScan.xcodeproj">
</BuildableReference>
</MacroExpansion>
<EnvironmentVariables>
<EnvironmentVariable
key = "IMAGE_DIFF_DIR"
value = "$(SOURCE_ROOT)/$(PROJECT_NAME)Tests/FailureDiffs"
isEnabled = "YES">
</EnvironmentVariable>
<EnvironmentVariable
key = "FB_REFERENCE_IMAGE_DIR"
value = "$(SOURCE_ROOT)/$(PROJECT_NAME)Tests/ReferenceImages"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
Expand Down
2 changes: 1 addition & 1 deletion WeScan/Common/CaptureSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// A class containing global variables and settings for this capture session
class CaptureSession {
final class CaptureSession {

static let current = CaptureSession()

Expand Down
2 changes: 1 addition & 1 deletion WeScan/Common/Quadrilateral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import AVFoundation

/// A data structure representing a quadrilateral and its position. This class exists to bypass the fact that CIRectangleFeature is read-only.
public class Quadrilateral: Transformable {
public final class Quadrilateral: Transformable {

/// A point that specifies the top left corner of the quadrilateral.
var topLeft: CGPoint
Expand Down
15 changes: 7 additions & 8 deletions WeScan/Common/VisionRectangleDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,24 @@ struct VisionRectangleDetector {
static func rectangle(forImage image: CIImage, completion: @escaping ((Quadrilateral?) -> ())) {

let imageRequestHandler = VNImageRequestHandler(ciImage: image, options: [:])
var biggestRectangle: Quadrilateral? = nil


// Create the rectangle request, and, if found, return the biggest rectangle (else return nothing).
let rectangleDetectionRequest: VNDetectRectanglesRequest = {
let rectDetectRequest = VNDetectRectanglesRequest(completionHandler: { (request, error) in
if error == nil {

guard let results = request.results as? [VNRectangleObservation] else { return }

// TODO: Fix all of this

let quads: [Quadrilateral] = []
let quads: [Quadrilateral] = results.map({ observation in
return Quadrilateral(topLeft: observation.topLeft, topRight: observation.topRight, bottomRight: observation.bottomRight, bottomLeft: observation.bottomLeft)
})

guard let biggest = results.count > 1 ? quads.biggest() : quads.first else { return }

let transform = CGAffineTransform.identity
.scaledBy(x: image.extent.size.width, y: image.extent.size.height)

biggestRectangle = Quadrilateral(topLeft: biggest.topLeft.applying(transform), topRight: biggest.topRight.applying(transform), bottomRight: biggest.bottomRight.applying(transform), bottomLeft: biggest.bottomLeft.applying(transform))
completion(biggestRectangle)

completion(biggest.applying(transform))

} else { completion(nil) }
})
Expand Down
16 changes: 8 additions & 8 deletions WeScan/Scan/RectangleFeaturesFunnel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,20 @@ final class RectangleFeaturesFunnel {
private func updateRectangleMatches() {
resetMatchingScores()

for currentRect in rectangles {
for rect in rectangles {
if currentRect.matches(rect.rectangleFeature, withThreshold: matchingThreshold) {
currentRect.matchingScore += 1
rect.matchingScore += 1
}
}
for (i, currentRect) in rectangles.enumerated() {
for (j, rect) in rectangles.enumerated() {
if j > i && currentRect.matches(rect.rectangleFeature, withThreshold: matchingThreshold) {
currentRect.matchingScore += 1
rect.matchingScore += 1
}
}
}
}

/// Resets the matching score of all of the rectangles in the queue to 0
private func resetMatchingScores() {
rectangles = rectangles.map { (rectangle) -> RectangleMatch in
rectangle.matchingScore = 0
rectangle.matchingScore = 1
return rectangle
}
}
Expand Down
44 changes: 44 additions & 0 deletions WeScanTests/AVCaptureVideoOrientationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// AVCaptureVideoOrientationTests.swift
// WeScanTests
//
// Created by James Campbell on 8/8/18.
// Copyright © 2018 WeTransfer. All rights reserved.
//

import XCTest
import AVFoundation

@testable import WeScan

class AVCaptureVideoOrientationTests: XCTestCase {

func testPortaitsMapToPortrait() {
XCTAssertEqual(AVCaptureVideoOrientation(deviceOrientation: .portrait), .portrait)
}

func testPortaitsUpsideDownMapToPortraitUpsideDown() {
XCTAssertEqual(AVCaptureVideoOrientation(deviceOrientation: .portraitUpsideDown), .portraitUpsideDown)
}

func testLandscapeLeftMapToLandscapeLeft() {
XCTAssertEqual(AVCaptureVideoOrientation(deviceOrientation: .landscapeLeft), .landscapeLeft)
}

func testLandscapeRightMapToLandscapeRight() {
XCTAssertEqual(AVCaptureVideoOrientation(deviceOrientation: .landscapeRight), .landscapeRight)
}

func testFaceUpMapToPortrait() {
XCTAssertEqual(AVCaptureVideoOrientation(deviceOrientation: .faceUp), .portrait)
}

func testFaceDownMapToPortraitUpsideDown() {
XCTAssertEqual(AVCaptureVideoOrientation(deviceOrientation: .faceDown), .portraitUpsideDown)
}

func testDefaultToPortrait() {
XCTAssertEqual(AVCaptureVideoOrientation(deviceOrientation: .unknown), .portrait)
}

}
49 changes: 49 additions & 0 deletions WeScanTests/CGAffineTransformTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// CGAffineTransformTests.swift
// WeScanTests
//
// Created by James Campbell on 8/8/18.
// Copyright © 2018 WeTransfer. All rights reserved.
//

import XCTest
@testable import WeScan

class CGAffineTransformTests: XCTestCase {

func testScalesUpCorrectly() {

let fromSize = CGSize(width: 1, height: 1)
let toSize = CGSize(width: 2, height: 2)

let scale = CGAffineTransform.scaleTransform(forSize: fromSize, aspectFillInSize: toSize)

XCTAssertEqual(scale.a, 2)
XCTAssertEqual(scale.d, 2)
}

func testScalesDownCorrectly() {

let fromSize = CGSize(width: 2, height: 2)
let toSize = CGSize(width: 1, height: 1)

let scale = CGAffineTransform.scaleTransform(forSize: fromSize, aspectFillInSize: toSize)

XCTAssertEqual(scale.a, 0.5)
XCTAssertEqual(scale.d, 0.5)
}

func testTranslatesCorrectly() {

let fromRect = CGRect(x: 0, y: 0, width: 10, height: 10)
let toRect = CGRect(x: 5, y: 5, width: 10, height: 10)

let translate = CGAffineTransform.translateTransform(fromCenterOfRect: fromRect, toCenterOfRect: toRect)

XCTAssertEqual(translate.a, 1.0)
XCTAssertEqual(translate.d, 1.0)
XCTAssertEqual(translate.tx, 5.0)
XCTAssertEqual(translate.ty, 5.0)
}

}
45 changes: 42 additions & 3 deletions WeScanTests/CIRectangleDetectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,51 @@
// Copyright © 2018 WeTransfer. All rights reserved.
//

import FBSnapshotTestCase
import XCTest
@testable import WeScan

class CIRectangleDetectorTests: XCTestCase {
class CIRectangleDetectorTests: FBSnapshotTestCase {

override func setUp() {
super.setUp()

recordMode = false
}

func testDetectorUsesValidType() {
XCTAssertNotNil(CIRectangleDetector.rectangleDetector)
func testCorrectlyDetectsAndReturnsQuadilateral() {

let targetSize = CGSize(width: 50, height: 50)

let containerLeyer = CALayer()
containerLeyer.backgroundColor = UIColor.white.cgColor
containerLeyer.frame = CGRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height)
containerLeyer.masksToBounds = true

let targetLayer = CALayer()
targetLayer.backgroundColor = UIColor.black.cgColor
targetLayer.frame = containerLeyer.frame.insetBy(dx: 5, dy: 5)

containerLeyer.addSublayer(targetLayer)

UIGraphicsBeginImageContextWithOptions(targetSize, true, 1.0)

containerLeyer.render(in: UIGraphicsGetCurrentContext()!)

let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

let ciImage = CIImage(cgImage: image.cgImage!)
let quad = CIRectangleDetector.rectangle(forImage: ciImage)!

let resultView = UIView(frame: containerLeyer.frame)
resultView.layer.addSublayer(containerLeyer)

let quadView = QuadrilateralView(frame: resultView.bounds)
quadView.drawQuadrilateral(quad: quad, animated: false)
quadView.backgroundColor = UIColor.red
resultView.addSubview(quadView)

FBSnapshotVerifyView(resultView)
}
}
33 changes: 18 additions & 15 deletions WeScanTests/RectangleFeaturesFunnelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ final class RectangleFeaturesFunnelTests: XCTestCase {

for i in 0 ..< rectangleFeatures.count {
funnel.add(rectangleFeatures[i], currentlyDisplayedRectangle: nil) { (_, _) in
expectation.fulfill()


expectation.fulfill()
}
}

Expand Down Expand Up @@ -65,7 +67,7 @@ final class RectangleFeaturesFunnelTests: XCTestCase {
wait(for: [expectation], timeout: 3.0)
}

/// Ensures that feeding the funnel with rectangles similar than the currently displayed one doesn't trigger the completion block.
/// Ensures that feeding the funnel with rectangles similar to the currently displayed one doesn't trigger the completion block.
func testAddPreviouslyDisplayedRect() {
let rectangleFeatures = ImageFeatureTestHelpers.getRectangleFeatures(from: .rect1, withCount: funnel.maxNumberOfRectangles * 2)

Expand All @@ -88,23 +90,23 @@ final class RectangleFeaturesFunnelTests: XCTestCase {
let count = 100
let type1RectangleFeatures = ImageFeatureTestHelpers.getRectangleFeatures(from: .rect1, withCount: count)
let type2RectangleFeatures = ImageFeatureTestHelpers.getRectangleFeatures(from: .rect2, withCount: count)
var currentlyDisplayedRect: CIRectangleFeature?
var currentlyDisplayedRect: Quadrilateral?

let expectation = XCTestExpectation(description: "Funnel add callback")
expectation.isInverted = true

// TODO: Fix this

// for i in 0 ..< count {
// let rectangleFeature = i % 2 == 0 ? type1RectangleFeatures[i] : type2RectangleFeatures[i]
// funnel.add(rectangleFeature, currentlyDisplayedRectangle: currentlyDisplayedRect, completion: { (rectFeature) in
//
// currentlyDisplayedRect = rectFeature
// if i >= funnel.maxNumberOfRectangles {
// expectation.fulfill()
// }
// })
// }
for i in 0 ..< count {
let rectangleFeature = i % 2 == 0 ? type1RectangleFeatures[i] : type2RectangleFeatures[i]

funnel.add(rectangleFeature, currentlyDisplayedRectangle: currentlyDisplayedRect, completion: { (result, rectFeature) in

currentlyDisplayedRect = rectFeature
if i >= funnel.maxNumberOfRectangles && result == .showOnly {
expectation.fulfill()
}
})
}

wait(for: [expectation], timeout: 3.0)
}
Expand All @@ -126,6 +128,7 @@ final class RectangleFeaturesFunnelTests: XCTestCase {
XCTAssert(rectangle.isWithin(1.0, ofRectangleFeature: rectangleFeaturesType1[0]))
expectationType1.fulfill()
}

wait(for: [expectationType1], timeout: 3.0)
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 99149e1

Please sign in to comment.