Last active
September 29, 2020 01:35
-
-
Save Fraktality/9fac81522f0e4b5d9507449fff813291 to your computer and use it in GitHub Desktop.
Verifies that a CFrame is a valid rotation matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local validateRotation do | |
local dot = Vector3.new().Dot | |
local cross = Vector3.new().Cross | |
local abs = math.abs | |
local EPS = 1e-4 | |
local function assertEq(value, expected) | |
assert(abs(value - expected) < EPS) | |
end | |
function validateRotation(cf) | |
cf = cf - cf.p | |
local rx = cf.rightVector | |
local ry = cf.upVector | |
local rz = -cf.lookVector | |
-- verify columns are unit length | |
assertEq(rx.magnitude, 1) | |
assertEq(ry.magnitude, 1) | |
assertEq(rz.magnitude, 1) | |
-- verify orthonormal basis | |
assert(dot(rz, cross(rx, ry)) > 1 - EPS) | |
assert(dot(rx, cross(ry, rz)) > 1 - EPS) | |
assert(dot(ry, cross(rz, rx)) > 1 - EPS) | |
-- verify r*r^T == I | |
local _, _, _, | |
qxx, qyx, qzx, | |
qxy, qyy, qzy, | |
qxz, qyz, qzz = (cf*cf:inverse()):components() | |
assertEq(qxx, 1) | |
assertEq(qxy, 0) | |
assertEq(qxz, 0) | |
assertEq(qyx, 0) | |
assertEq(qyy, 1) | |
assertEq(qyz, 0) | |
assertEq(qzx, 0) | |
assertEq(qzy, 0) | |
assertEq(qzz, 1) | |
print("Tests passed") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment