11from warnings import warn
22
33import numpy as np
4- import transforms3d
54import math
65from ._utilities import shear_angle_to_shear_factor
76
@@ -16,9 +15,55 @@ class AffineTransform3D:
1615
1716 """
1817
18+ @staticmethod
19+ def _identity_matrix ():
20+ return np .eye (4 , dtype = float )
21+
22+ @staticmethod
23+ def _scale_matrix (sx = 1.0 , sy = 1.0 , sz = 1.0 ):
24+ return np .asarray ([
25+ [sx , 0 , 0 , 0 ],
26+ [0 , sy , 0 , 0 ],
27+ [0 , 0 , sz , 0 ],
28+ [0 , 0 , 0 , 1 ],
29+ ], dtype = float )
30+
31+ @staticmethod
32+ def _rotation_matrix_x (angle_in_rad ):
33+ c = np .cos (angle_in_rad )
34+ s = np .sin (angle_in_rad )
35+ return np .asarray ([
36+ [1 , 0 , 0 , 0 ],
37+ [0 , c , - s , 0 ],
38+ [0 , s , c , 0 ],
39+ [0 , 0 , 0 , 1 ],
40+ ], dtype = float )
41+
42+ @staticmethod
43+ def _rotation_matrix_y (angle_in_rad ):
44+ c = np .cos (angle_in_rad )
45+ s = np .sin (angle_in_rad )
46+ return np .asarray ([
47+ [c , 0 , s , 0 ],
48+ [0 , 1 , 0 , 0 ],
49+ [- s , 0 , c , 0 ],
50+ [0 , 0 , 0 , 1 ],
51+ ], dtype = float )
52+
53+ @staticmethod
54+ def _rotation_matrix_z (angle_in_rad ):
55+ c = np .cos (angle_in_rad )
56+ s = np .sin (angle_in_rad )
57+ return np .asarray ([
58+ [c , - s , 0 , 0 ],
59+ [s , c , 0 , 0 ],
60+ [0 , 0 , 1 , 0 ],
61+ [0 , 0 , 0 , 1 ],
62+ ], dtype = float )
63+
1964 def __init__ (self , transform = None , image = None ):
2065 if transform is None :
21- self ._matrix = transforms3d . zooms . zfdir2aff ( 1 )
66+ self ._matrix = self . _identity_matrix ( )
2267 if isinstance (transform , AffineTransform3D ):
2368 self ._matrix = np .copy (transform ._matrix )
2469 if isinstance (transform , str ):
@@ -56,11 +101,11 @@ def scale(self, scale_x: float = None, scale_y: float = None, scale_z: float = N
56101 warn ('scale_z must not be 0' )
57102 scale_z = 1
58103 if scale_x is not None :
59- self ._concatenate (transforms3d . zooms . zfdir2aff ( scale_x , direction = ( 1 , 0 , 0 ), origin = ( 0 , 0 , 0 ) ))
104+ self ._concatenate (self . _scale_matrix ( sx = scale_x ))
60105 if scale_y is not None :
61- self ._concatenate (transforms3d . zooms . zfdir2aff ( scale_y , direction = ( 0 , 1 , 0 ), origin = ( 0 , 0 , 0 ) ))
106+ self ._concatenate (self . _scale_matrix ( sy = scale_y ))
62107 if scale_z is not None :
63- self ._concatenate (transforms3d . zooms . zfdir2aff ( scale_z , direction = ( 0 , 0 , 1 ), origin = ( 0 , 0 , 0 ) ))
108+ self ._concatenate (self . _scale_matrix ( sz = scale_z ))
64109
65110 return self
66111
@@ -83,11 +128,11 @@ def rotate(self, axis : int = 2, angle_in_degrees : float = 0):
83128 angle_in_rad = angle_in_degrees * np .pi / 180.0
84129
85130 if axis == 0 :
86- self ._concatenate (self ._3x3_to_4x4 ( transforms3d . euler . euler2mat ( angle_in_rad , 0 , 0 ) ))
131+ self ._concatenate (self ._rotation_matrix_x ( angle_in_rad ))
87132 if axis == 1 :
88- self ._concatenate (self ._3x3_to_4x4 ( transforms3d . euler . euler2mat ( 0 , angle_in_rad , 0 ) ))
133+ self ._concatenate (self ._rotation_matrix_y ( angle_in_rad ))
89134 if axis == 2 :
90- self ._concatenate (self ._3x3_to_4x4 ( transforms3d . euler . euler2mat ( 0 , 0 , angle_in_rad ) ))
135+ self ._concatenate (self ._rotation_matrix_z ( angle_in_rad ))
91136
92137 return self
93138
0 commit comments