Commit 681afb14 authored by Guofang Xiao's avatar Guofang Xiao Committed by Matt Clarkson

Issue #11: Adding Guofang's 3x3 standard rotation matrices.

parent de13e976
Pipeline #1428 passed with stages
in 14 minutes and 5 seconds
......@@ -10,3 +10,7 @@
# PyInstaller
build/
dist/
# Virtual Environment
venv/
"""
Construct 3x3 rotation matrices for rotating around the x, y, z axes.
"""
import numpy as np
def construct_rx_matrix(angle, is_in_radians=True):
"""
Construct a rotation matrix for rotation around the x axis.
:param angle: the angle to rotate
:param is_in_radians: if angle is in radians or not, default being True
:returns: rot_x -- the 3x3 rotation matrix
"""
if not is_in_radians:
angle = np.pi * angle / 180
cos_x = np.cos(angle)
sin_x = np.sin(angle)
rot_x = np.array([[1, 0, 0], [0, cos_x, -sin_x], [0, sin_x, cos_x]])
return rot_x
def construct_ry_matrix(angle, is_in_radians=True):
"""
Construct a rotation matrix for rotation around the y axis.
:param angle: the angle to rotate
:param is_in_radians: if angle is in radians or not, default being True
:returns: rot_y -- the 3x3 rotation matrix
"""
if not is_in_radians:
angle = np.pi * angle / 180
cos_y = np.cos(angle)
sin_y = np.sin(angle)
rot_y = np.array([[cos_y, 0, sin_y], [0, 1, 0], [-sin_y, 0, cos_y]])
return rot_y
def construct_rz_matrix(angle, is_in_radians=True):
"""
Construct a rotation matrix for rotation around the z axis.
:param angle: the angle to rotate
:param is_in_radians: if angle is in radians or not, default being True
:returns: rot_z -- the 3x3 rotation matrix
"""
if not is_in_radians:
angle = np.pi * angle / 180
cos_z = np.cos(angle)
sin_z = np.sin(angle)
rot_z = np.array([[cos_z, -sin_z, 0], [sin_z, cos_z, 0], [0, 0, 1]])
return rot_z
"""
Tests for matrix.py
"""
import pytest
import numpy as np
import sksurgerycore.transforms.matrix as mat
import sksurgerycore.utilities.validate_matrix as vm
def check_construct_rx_matrix(angle, is_in_radians, point):
""""
Test if the rotation matrix for rotating around the x axis is correct.
:param angle: the angle to rotate
:param is_in_radians: if angle is in radians or not
:param point: the point to be rotated
:returns: new_point -- the point after rotation
"""
rot_x = mat.construct_rx_matrix(angle, is_in_radians)
vm.validate_rotation_matrix(rot_x)
new_point = np.matmul(rot_x, point)
assert new_point[0] == point[0]
assert np.abs(np.linalg.norm(new_point) - np.linalg.norm(point)) <= 0.0001
return new_point
def check_construct_ry_matrix(angle, is_in_radians, point):
""""
Test if the rotation matrix for rotating around the y axis is correct.
:param angle: the angle to rotate
:param is_in_radians: if angle is in radians or not
:param point: the point to be rotated
:returns: new_point -- the point after rotation
"""
rot_y = mat.construct_ry_matrix(angle, is_in_radians)
vm.validate_rotation_matrix(rot_y)
new_point = np.matmul(rot_y, point)
assert new_point[1] == point[1]
assert np.abs(np.linalg.norm(new_point) - np.linalg.norm(point)) <= 0.0001
return new_point
def check_construct_rz_matrix(angle, is_in_radians, point):
""""
Test if the rotation matrix for rotating around the z axis is correct.
:param angle: the angle to rotate
:param is_in_radians: if angle is in radians or not
:param point: the point to be rotated
:returns: new_point -- the point after rotation
"""
rot_z = mat.construct_rz_matrix(angle, is_in_radians)
vm.validate_rotation_matrix(rot_z)
new_point = np.matmul(rot_z, point)
assert new_point[2] == point[2]
assert np.abs(np.linalg.norm(new_point) - np.linalg.norm(point)) <= 0.0001
return new_point
def test_construct_rx_matrix():
tiny = 0.0001
new_point = check_construct_rx_matrix(90, 0, np.array([0, 1, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2] - 1) <= tiny
new_point = check_construct_rx_matrix(np.pi/2, 1, np.array([0, 1, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2] - 1) <= tiny
new_point = check_construct_rx_matrix(-90, 0, np.array([0, 1, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2] + 1) <= tiny
new_point = check_construct_rx_matrix(-np.pi/2, 1, np.array([0, 1, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2] + 1) <= tiny
new_point = check_construct_rx_matrix(180, 0, np.array([0, 1, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1] + 1) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rx_matrix(np.pi, 1, np.array([0, 1, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1] + 1) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rx_matrix(-180, 0, np.array([0, 1, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1] + 1) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rx_matrix(-np.pi, 1, np.array([0, 1, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1] + 1) <= tiny
assert np.abs(new_point[2]) <= tiny
def test_construct_ry_matrix():
tiny = 0.0001
new_point = check_construct_ry_matrix(90, 0, np.array([1, 0, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2] + 1) <= tiny
new_point = check_construct_ry_matrix(np.pi / 2, 1, np.array([1, 0, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2] + 1) <= tiny
new_point = check_construct_ry_matrix(-90, 0, np.array([1, 0, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2] - 1) <= tiny
new_point = check_construct_ry_matrix(-np.pi / 2, 1, np.array([1, 0, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2] - 1) <= tiny
new_point = check_construct_ry_matrix(180, 0, np.array([1, 0, 0]).T)
assert np.abs(new_point[0] + 1) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_ry_matrix(np.pi, 1, np.array([1, 0, 0]).T)
assert np.abs(new_point[0] + 1) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_ry_matrix(-180, 0, np.array([1, 0, 0]).T)
assert np.abs(new_point[0] + 1) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_ry_matrix(-np.pi, 1, np.array([1, 0, 0]).T)
assert np.abs(new_point[0] + 1) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2]) <= tiny
def test_construct_rz_matrix():
tiny = 0.0001
new_point = check_construct_rz_matrix(90, 0, np.array([1, 0, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1] - 1) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rz_matrix(np.pi / 2, 1, np.array([1, 0, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1] - 1) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rz_matrix(-90, 0, np.array([1, 0, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1] + 1) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rz_matrix(-np.pi / 2, 1, np.array([1, 0, 0]).T)
assert np.abs(new_point[0]) <= tiny
assert np.abs(new_point[1] + 1) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rz_matrix(180, 0, np.array([1, 0, 0]).T)
assert np.abs(new_point[0] + 1) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rz_matrix(np.pi, 1, np.array([1, 0, 0]).T)
assert np.abs(new_point[0] + 1) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rz_matrix(-180, 0, np.array([1, 0, 0]).T)
assert np.abs(new_point[0] + 1) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2]) <= tiny
new_point = check_construct_rz_matrix(-np.pi, 1, np.array([1, 0, 0]).T)
assert np.abs(new_point[0] + 1) <= tiny
assert np.abs(new_point[1]) <= tiny
assert np.abs(new_point[2]) <= tiny
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