Commit b4b17d78 authored by Matt Clarkson's avatar Matt Clarkson

Merge branch '7-validate-rotation-matrix' into 'master'

Resolve "Add orthogonal test to validate_matrix.py::validate_rotation_matrix"

Closes #7

See merge request WEISS/SoftwareRepositories/SNAPPY/scikit-surgerycore!2
parents de13e976 334ab403
Pipeline #1380 passed with stages
in 16 minutes and 58 seconds
......@@ -66,6 +66,9 @@ def validate_rotation_matrix(matrix):
2. Is 2D
3. Has 3 rows
4. Has 3 columns
5. Is orthogonal, i.e., transpose(matrix) * matrix = identity matrix.
6. Is its determinant positive (+1) (c.f., it is a reflection matrix
(improper rotation) if the determinant is negative (-1))
:param matrix: rotation matrix
:raises: TypeError, ValueError if not
......@@ -79,6 +82,23 @@ def validate_rotation_matrix(matrix):
raise ValueError("Rotation matrix should have 3 rows.")
if matrix.shape[1] != 3:
raise ValueError("Rotation matrix should have 3 columns.")
# Check the orthogonality: transpose(matrix) * matrix = identity matrix.
mat = np.matmul(np.transpose(matrix), matrix)
tolerance = 1e-6
identity = np.eye(3)
residual = np.absolute(mat) - identity
if np.flatnonzero(np.where(
(residual < -tolerance) | (residual > tolerance))).shape[0] > 0:
raise ValueError("Rotation matrix should be orthogonal.")
# Check if the determinant is positive
# (assuming that the absolute value of the determinant is (close to) 1,
# which is followed by the orthogonality).
if np.linalg.det(matrix) < 0.0:
raise ValueError(
"Rotation matrix should have a positive determinant (+1).")
return True
......
......@@ -27,10 +27,10 @@ def test_using_pytest_cookienewwithgitinit_withTK():
six.print_("Got another error (not TCL), that's not OK.")
assert False
except ModuleNotFoundError:
six.print_("Got module not found on tkinter, please check your python installation")
#except ModuleNotFoundError:
#six.print_("Got module not found on tkinter, please check your python installation")
#we're not trying to test whether we have tkinter so this is ok
assert True
#assert True
except ImportError:
six.print_("Got import error on tkinter, please check your python installation")
#we're not trying to test whether we have tkinter so this is ok
......
......@@ -105,6 +105,18 @@ def test_rotation_matrix_invalid_because_too_few_columns():
vm.validate_rotation_matrix(np.ones((3, 2)))
def test_rotation_matrix_invalid_because_not_orthogonal():
with pytest.raises(ValueError):
vm.validate_rotation_matrix(
np.array([[3.0, -4.0, 1.0], [5.0, 3.0, -7.0], [-9.0, 2.0, 6.0]]))
def test_rotation_matrix_invalid_because_determinant_not_positive():
with pytest.raises(ValueError):
vm.validate_rotation_matrix(
np.array([[-1.0, 0.0, 0.0], [0.0, -1.0, 0.0], [0.0, 0.0, -1.0]]))
def test_rotation_matrix_valid():
rotation_matrix = np.eye(3)
assert vm.validate_rotation_matrix(rotation_matrix)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment