-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixFactory.py
More file actions
160 lines (99 loc) · 3.9 KB
/
MatrixFactory.py
File metadata and controls
160 lines (99 loc) · 3.9 KB
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
'''
* MatrixFactory.py
*
* Created on: 30.12.2018
* Author: Andrew Jason Bishop
*
* General description:
* xxx
'''
import numpy as np
import pprint as pp
class MatrixFactory:
def __init__(self, _dimension):
self.identity = np.identity( _dimension, dtype=float )
def createNewIdentityMatrix(self):
return np.matrix( self.identity )
def createTranslationMatrix_expected(self, _translation):
rowElementCount = _translation.shape[0]
translatAsColumnVector = _translation.reshape(( rowElementCount ,1))
newMatrix = self.createNewIdentityMatrix()
# paste translat factors to rightmost matrix column from top row down:
newMatrix[ :rowElementCount, -1] = translatAsColumnVector
return newMatrix
def createTranslationMatrix(self, _translation):
rowElementCount = _translation.shape[0]
translatAsColumnVector = _translation.reshape(( rowElementCount ,1))
newMatrix = self.createNewIdentityMatrix()
# paste translat factors to rightmost matrix column from top row down:
newMatrix[-1,0:rowElementCount] = _translation
return newMatrix
def createScaleMatrix(self, _scale):
rowElementCount = _scale.shape[0]
newMatrix = self.createNewIdentityMatrix()
for i in range(0, rowElementCount):
newMatrix[i,i] = _scale[i]
return newMatrix
def createExtrinsicRotationMatrix(self, _rotAngles):
rotMatX = self.createRotMatrix_X( _rotAngles[0] )
rotMatY = self.createRotMatrix_Y( _rotAngles[1] )
rotMatZ = self.createRotMatrix_Z( _rotAngles[2] )
rotMatrix = rotMatZ * rotMatY * rotMatX
return rotMatrix
def createRotMatrix_X(self, _rotAngle):
newMatrix = self.createNewIdentityMatrix()
s, c = self.getSinCos( _rotAngle )
newMatrix[1,1], newMatrix[1,2] = c, -s
newMatrix[2,1], newMatrix[2,2] = s, c
return newMatrix
def createRotMatrix_Y(self, _rotAngle):
newMatrix = self.createNewIdentityMatrix()
s, c = self.getSinCos( _rotAngle )
newMatrix[0,0], newMatrix[0,2] = c, s
newMatrix[2,0], newMatrix[2,2] = -s, c
return newMatrix
def createRotMatrix_Z(self, _rotAngle):
newMatrix = self.createNewIdentityMatrix()
s, c = self.getSinCos( _rotAngle )
newMatrix[0,0], newMatrix[0,1] = c, -s
newMatrix[1,0], newMatrix[1,1] = s, c
return newMatrix
def getSinCos(self, _rotAngle):
theta = np.radians(_rotAngle)
return np.sin(theta), np.cos(theta)
def rowVectorToColumnVector(self, _rowVector):
rowElementCount = _rowVector.shape[0]
asColumnVector = _rowVector.reshape((rowElementCount , 1))
return asColumnVector
if __name__ == '__main__':
def verbose(_title, _array):
print(_title)
pp.pprint(_array)
print("")
print("")
float_formatter = lambda x: "%+.3f" % x
np.set_printoptions(formatter={'float_kind':float_formatter})
#rM = myMF.createExtrinsicRotationMatrix( (0,90,0) )
myMF = MatrixFactory(4)
r2 = np.sqrt(2)
v = np.array([0.,0.,r2,1.])
a = np.array([45.,45.,45.])
v = myMF.rowVectorToColumnVector( v )
rMx = myMF.createRotMatrix_X(45.)
rMy = myMF.createRotMatrix_Y(45.)
rMz = myMF.createRotMatrix_Z(45.)
verbose("column vertex:", v)
rMx_v = rMx*v
verbose("x rotated vertex:", rMx_v)
rMxy_v = rMy*rMx_v
verbose("xy rotated vertex:", rMxy_v)
rMxyz_v = rMz*rMxy_v
verbose("xyz rotated vertex:", rMxyz_v)
rall = rMz*rMy*rMx*v
verbose("combi matrix rotated vertex:", rall)
#rMex = myMF.createExtrinsicRotationMatrix( (0.,0.,45.) )
rMex = myMF.createExtrinsicRotationMatrix( a )
verbose("extrinsic rotation matrix:", rMex)
rMex_v = rMex*v
verbose("extrinsic rotated vertex:", rMex_v)
''' END '''