verify basisVectorsToPointsMap on random points

This commit is contained in:
Paul Zabelin
2018-03-04 22:05:37 -08:00
parent f2b7ca41ae
commit b3fdf4ba93
2 changed files with 40 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
import Quick
import Nimble
import simd
import GameKit
@testable import PerspectiveTransform
class BasisSpec: QuickSpec {
@@ -24,16 +25,27 @@ class BasisSpec: QuickSpec {
}
context("basisVectorsToPointsMap") {
let basisVectors = [Vector3Type(0, 0, 0),
Vector3Type(0, 1, 0),
Vector3Type(0, 0, 1),
Vector3Type(1, 1, 1)]
context("any 4 points") {
var points: [CGPoint]!
var subject: Matrix3x3Type!
it("should map base vectors to points") {
let basis = Perspective(start).basisVectorsToPointsMap
for (index, vector) in basisVectors.enumerated() {
let tranformed = basis * vector
expect(tranformed.dehomogenized) == start.corners[index]
let basisVectors = [Vector3Type(1, 0, 0),
Vector3Type(0, 1, 0),
Vector3Type(0, 0, 1),
Vector3Type(1, 1, 1)]
let source = GKRandomSource.sharedRandom()
beforeEach {
points = source.nextFourPoints()
subject = Perspective(points).basisVectorsToPointsMap
}
it("should map base vectors to points") {
for (index, vector) in basisVectors.enumerated() {
let tranformed = subject * vector
expect(tranformed.dehomogenized) points[index]
}
}
}

View File

@@ -8,7 +8,7 @@
import simd
import GameKit
import Nimble
@testable import Nimble
extension CATransform3D {
func flattened() -> [CGFloat] {
@@ -41,10 +41,28 @@ public func beCloseTo(_ expectedValue: CATransform3D, within delta: CGFloat = CG
}
}
public func beCloseTo(_ expectedValue: CGPoint, within delta: CGFloat = CGFloat(DefaultDelta)) -> Predicate<CGPoint> {
let errorMessage = "be close to <\(stringify(expectedValue))> (each within \(stringify(delta)))"
return Predicate.simple(errorMessage) { actualExpression in
if let actual = try actualExpression.evaluate() {
if actual == expectedValue {
return .matches
} else {
let eachCoordinate = beCloseTo([expectedValue.x, expectedValue.y].map{Double($0)})
return try! eachCoordinate.satisfies(actualExpression.cast{[$0!.x,$0!.y].map{Double($0)}}).status
}
}
return .doesNotMatch
}
}
public func (lhs: Expectation<CATransform3D>, rhs: CATransform3D) {
lhs.to(beCloseTo(rhs))
}
public func (lhs: Expectation<CGPoint>, rhs: CGPoint) {
lhs.to(beCloseTo(rhs))
}
extension CATransform3D: Equatable {
public static func ==(lhs: CATransform3D, rhs: CATransform3D) -> Bool {
return CATransform3DEqualToTransform(lhs, rhs)