# Test 01
# Example 3
import numpy as np
class Matrix:
"""
Definition: This class generates Rotation and Translation matrices,
that can be used to multiply any matrix and obtain the translation or rotation.
It uses `numpy` to generate the matrices:
np.float32: creates the array with 16 float32 elements
np.reshape: np.reshape rearrange the array into a 4X4 matrix
Returns: It returns Rotation and translation matrices.
Obs: **kwargs (keyword arguments) are used to facilitate the identification of the parameters, so initiate the
object
like: Matrix(x_angle='45', x_dist='100', z_angle='60', z_dist='100'), if an argument is not provided,
the default 0 will be put to the argument.
"""
def __init__(self, **kwargs):
"""
Initializes the Object.
"""
self._x_angle = kwargs['x_angle'] if 'x_angle' in kwargs else '0'
self._x_dist = kwargs['x_dist'] if 'x_dist' in kwargs else '0'
self._y_angle = kwargs['y_angle'] if 'y_angle' in kwargs else '0'
self._y_dist = kwargs['y_dist'] if 'y_dist' in kwargs else '0'
self._z_angle = kwargs['z_angle'] if 'z_angle' in kwargs else '0'
self._z_dist = kwargs['z_dist'] if 'z_dist' in kwargs else '0'
self._m_degrees = kwargs['m_degrees'] if 'm_degrees' in kwargs else 'True'
def trans_x(self, a=0):
"""
Definition: Translates the matrix a given amount `a` on the *X* axis by Defining a 4x4 identity
matrix with `a` as the (1,4) element.
:type a: float
:param a: Distance translated on the X-axis
Returns: The Translation Matrix on the *X* axis by a distance *a*
"""
if a:
self._x_dist = a
trans_x = np.float32([1, 0, 0, self._x_dist,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1])
trans_x = np.reshape(trans_x, (4, 4))
return trans_x
def trans_y(self, b=0):
"""
Definition: Translate the matrix a given amount `d` on the *Z* axis. by Defining a matrix T 4x4 identity
matrix with *b* (3,4) element position.
:type b: float
:param b: Distance translated on the Z-axis
Returns: The Translation Matrix on the *Z* axis by a distance *b*
"""
if b:
self._y_dist = b
trans_y = np.float32([1, 0, 0, 0,
0, 1, 0, self._y_dist,
0, 0, 1, 0,
0, 0, 0, 1])
trans_y = np.reshape(trans_y, (4, 4))
return trans_y
def trans_z(self, c=0):
"""
Definition: Translate the matrix a given amount `d` on the *Z* axis. by Defining a matrix T 4x4 identity
matrix with *c* (3,4) element position.
:type c: float
:param c: Distance translated on the Z-axis
Returns: The Translation Matrix on the *Z* axis by a distance *c*
"""
if c:
self._z_dist = c
trans_z = np.float32([1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, self._z_dist,
0, 0, 0, 1])
trans_z = np.reshape(trans_z, (4, 4))
return trans_z
def rot_x(self, gamma=0, degrees=True):
"""
Definition: Receives an alpha angle and returns the rotation matrix for the given angle at the *X* axis.
If the angle is given in radian degrees should be False.
:type gamma: float
:param gamma: Rotation Angle around the X axis
:type degrees: bool
:param degrees: Indicates if the provided angle is in degrees, if yes It will be converted to radians
Returns: The Rotational Matrix at the X axis by an *gamma* angle
"""
if gamma:
self._x_angle = gamma
if degrees:
self._m_degrees = degrees
self._x_angle = np.deg2rad(gamma)
rot_x = np.float32([1, 0, 0, 0,
0, float("{:.3f}".format(np.cos(self._x_angle))), float("{:.3f}".format(-np.sin(self._x_angle))), 0,
0, float("{:.3f}".format(np.sin(self._x_angle))), float("{:.3f}".format(np.cos(self._x_angle))), 0,
0, 0, 0, 1])
rot_x = np.reshape(rot_x, (4, 4))
return rot_x
def rot_y(self, beta=0, degrees=True):
"""
Definition: Receives an theta angle and returns the rotation matrix for the given angle at the *Z* axis.
If the angle is given in radian degrees should be False.
:type beta: float
:param beta: Rotation Angle around the Z axis
:type degrees: bool
:param degrees: Indicates if the provided angle is in degrees, if yes It will be converted to radians
Returns: The Rotational Matrix at the Z axis by an *beta* angle
"""
if beta:
self._y_angle = beta
if degrees:
self._m_degrees = degrees
self._y_angle = np.deg2rad(beta)
rot_y = np.float32([float("{:.3f}".format(np.cos(self._y_angle))), 0, 0, float("{:.3f}".format(np.sin(self._y_angle))),
0, 0, 0, 0,
float("{:.3f}".format(-np.sin(self._y_angle))), 0, 1, float("{:.3f}".format(np.cos(self._y_angle))),
0, 0, 0, 1])
rot_y = np.reshape(rot_y, (4, 4))
return rot_y
def rot_z(self, alpha=0, degrees=True):
"""
Definition: Receives an theta angle and returns the rotation matrix for the given angle at the *Z* axis.
If the angle is given in radian degrees should be False.
:type alpha: float
:param alpha: Rotation Angle around the Z axis
:type degrees: bool
:param degrees: Indicates if the provided angle is in degrees, if yes It will be converted to radians
Returns: The Rotational Matrix at the Z axis by an *alpha* angle
"""
if alpha:
self._z_angle = alpha
if degrees:
self._m_degrees = degrees
self._z_angle = np.deg2rad(alpha)
rot_z = np.float32([float("{:.3f}".format(np.cos(self._z_angle))), float("{:.3f}".format(-np.sin(self._z_angle))), 0, 0,
float("{:.3f}".format(np.sin(self._z_angle))), float("{:.3f}".format(np.cos(self._z_angle))), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1])
rot_z = np.reshape(rot_z, (4, 4))
return rot_z
def main():
"""
Example 3
"""
print('Example 3:')
a1 = Matrix() # Rotation in x by 90
a2 = Matrix() # Translation in X by 0.75
a3 = Matrix() # Rotation in Z by 30
a4 = Matrix() # Rotation in Z by -30
a5 = Matrix() # Translation in X by 0.5
a6 = Matrix() # Both transforms
a7 = Matrix() # g
print()
print('Rotation in X by 90:')
print(a1.rot_x(45))
print()
print('Translation in X by 0.75:')
print(a2.trans_x(0.75))
print()
print('Rotation in Z by 30')
print(a3.rot_z(30))
# print()
# print('Rotation in X by 90 x Translation in X by 0.75:')
# print(np.matmul(a1.rot_x(90), a2.trans_x(0.75)))
print()
print('First Individual Transform:')
print('Rotation in X by 90 x Translation in X by 0.75 x Rotation in Z by 30:')
print(np.matmul((np.matmul(a1.rot_x(90), a2.trans_x(0.75))), a3.rot_z(30)))
print()
print('Rotation in Z by 30:')
print(a4.rot_z(-30))
print()
print('Translation in X by 0.5:')
print(a5.trans_x(0.55))
print()
print('Second Individual Transform:')
print('Rotation in Z by 30 x Translation in X by 0.5')
print(np.matmul(a4.rot_z(-30), a5.trans_x(0.55)))
print()
print('Product of both transforms:')
a6 = np.matmul(np.matmul((np.matmul(a1.rot_x(90), a2.trans_x(0.75))), a3.rot_z(30)), np.matmul(a4.rot_z(-30), a5.trans_x(0.55)))
print(a6)
print()
print('G:')
a7 = np.float32([0.1, 0.1, 0, 1])
a7 = np.reshape(a7, (4, 1))
print(a7)
print()
print('Final Countdown:')
print(np.matmul(a6, a7))
# print('Rotation in X by 90 and rotation in Z by 30')
# print(np.matmul(a0.rot_x(90), a0.rot_z(30)))
return
if __name__ == '__main__':
main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
Output:
Example 3:
Rotation in X by 90:
[[ 1. 0. 0. 0. ]
[ 0. 0.707 -0.707 0. ]
[ 0. 0.707 0.707 0. ]
[ 0. 0. 0. 1. ]]
Translation in X by 0.75:
[[1. 0. 0. 0.75]
[0. 1. 0. 0. ]
[0. 0. 1. 0. ]
[0. 0. 0. 1. ]]
Rotation in Z by 30
[[ 0.866 -0.5 0. 0. ]
[ 0.5 0.866 0. 0. ]
[ 0. 0. 1. 0. ]
[ 0. 0. 0. 1. ]]
First Individual Transform:
Rotation in X by 90 x Translation in X by 0.75 x Rotation in Z by 30:
[[ 0.866 -0.5 0. 0.75 ]
[ 0. 0. -1. 0. ]
[ 0.5 0.866 0. 0. ]
[ 0. 0. 0. 1. ]]
Rotation in Z by 30:
[[ 0.866 0.5 0. 0. ]
[-0.5 0.866 0. 0. ]
[ 0. 0. 1. 0. ]
[ 0. 0. 0. 1. ]]
Translation in X by 0.5:
[[1. 0. 0. 0.55]
[0. 1. 0. 0. ]
[0. 0. 1. 0. ]
[0. 0. 0. 1. ]]
Second Individual Transform:
Rotation in Z by 30 x Translation in X by 0.5
[[ 0.866 0.5 0. 0.4763]
[-0.5 0.866 0. -0.275 ]
[ 0. 0. 1. 0. ]
[ 0. 0. 0. 1. ]]
Product of both transforms:
[[ 0.999956 0. 0. 1.2999759]
[ 0. 0. -1. 0. ]
[ 0. 0.999956 0. 0. ]
[ 0. 0. 0. 1. ]]
G:
[[0.1]
[0.1]
[0. ]
[1. ]]
Final Answer:
[[1.3999715 ]
[0. ]
[0.09999561]
[1. ]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66