implement adjugate via cross product of vectors

This commit is contained in:
Paul Zabelin
2016-02-21 14:41:53 -08:00
parent f367d5a648
commit 62e11bda7d
2 changed files with 8 additions and 20 deletions

View File

@@ -7,10 +7,11 @@ class HomogeneousInverseSpec: QuickSpec {
override func spec() {
describe("homogeneousInverse") {
context("multiply adj by vector") {
let m = Matrix3x3Type([-122, -152, 18544, 122, 0, 0, 0, 152, 0])
let expected = Matrix3x3Type([[0.0, 0.0, 1.0], [152.0, -0.0, 1.0], [-0.0, 122.0, 1.0]])
it("should match expected") {
let m = Matrix3x3Type([-122, -152, 18544, 122, 0, 0, 0, 152, 0])
let expected = Matrix3x3Type([[0.0, 0.0, 1.0], [152.0, -0.0, 1.0], [-0.0, 122.0, 1.0]])
expect(m.homogeneousInverse()).to(beCloseTo(expected))
expect(m.homogeneousInverse().zNormalized()).to(beCloseTo(expected))
}
}
}

View File

@@ -53,22 +53,9 @@ extension Matrix3x3Type {
}
private func adjugate()-> Matrix3x3Type {
return Matrix3x3Type(rows:[
Vector3Type(
ScalarType(self[1,1]*self[2,2]-self[2,1]*self[1,2]),
ScalarType(self[2,0]*self[1,2]-self[1,0]*self[2,2]),
ScalarType(self[1,0]*self[2,1]-self[2,0]*self[1,1])
),
Vector3Type(
ScalarType(self[2,1]*self[0,2]-self[0,1]*self[2,2]),
ScalarType(self[0,0]*self[2,2]-self[2,0]*self[0,2]),
ScalarType(self[2,0]*self[0,1]-self[0,0]*self[2,1])
),
Vector3Type(
ScalarType(self[0,1]*self[1,2]-self[1,1]*self[0,2]),
ScalarType(self[1,0]*self[0,2]-self[0,0]*self[1,2]),
ScalarType(self[0,0]*self[1,1]-self[1,0]*self[0,1])
)
]).zNormalized()
let c1 = self[0]
let c2 = self[1]
let c3 = self[2]
return Matrix3x3Type([cross(c2, c3), cross(c3, c1), cross(c1, c2)]).transpose.zNormalized()
}
}