a b/T1-TSE/utils.py
1
# ==============================================================================
2
# Copyright (C) 2023 Haresh Rengaraj Rajamohan, Tianyu Wang, Kevin Leung, 
3
# Gregory Chang, Kyunghyun Cho, Richard Kijowski & Cem M. Deniz 
4
#
5
# This file is part of OAI-MRI-TKR
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as published
9
# by the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU Affero General Public License for more details.
16
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
# ==============================================================================
20
import random
21
import numpy as np
22
import math
23
24
def angle_axis_to_rotation_matrix(angle,axis):
25
26
    '''
27
    :param angle: angle of rotation
28
    :param axis: Axis 0f rotation
29
    :return: Rotation Matrix
30
    '''
31
32
    A = np.outer(axis,axis)
33
    B = np.zeros((3,3),dtype=float)
34
    B[0,1] = -1*axis[-1]
35
    B[0,2] = axis[-2]
36
    B[1,2] = axis[0]
37
    B = B - B.transpose()
38
    R = math.cos(angle)+np.eye(3)+ math.sin(angle)*B + (1-math.cos(angle))*A
39
    return R
40
41
42
def generating_random_rotation_matrix():
43
    '''
44
    :return: random rotation matrix, axis of rotation, angle of rotation
45
    random rotation matrix is selected in such a way that the axis of rotation is uinformally distributed on a unit
46
    sphere and the angle is uniformally distributed.
47
    '''
48
49
    guassian_vector = [random.gauss(mu=0, sigma=1), random.gauss(mu=0, sigma=1), random.gauss(mu=0, sigma=1)]
50
    axis_of_rotation = list(np.array(guassian_vector) / np.linalg.norm(x=np.array(guassian_vector)))
51
    angle_of_rotation = 2 * math.pi * random.uniform(0, 1)
52
    rotation_matrix = angle_axis_to_rotation_matrix(angle=angle_of_rotation, axis=axis_of_rotation)
53
    return rotation_matrix, axis_of_rotation, angle_of_rotation