-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
293 lines (204 loc) · 9.9 KB
/
matrix.py
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
BSD 3-Clause License:
Copyright (c) 2023, Eric Vignola
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import numpy as np
from ._transforms import (
_eulerToMatrix,
_matrixIdentity,
_matrixInverse,
_matrixMultiply,
_matrixNormalize,
_matrixPointMultiply,
_matrixToEuler,
_matrixToQuaternion,
_quaternionSlerp,
_quaternionToMatrix,
_vectorLerp,
)
from ._utils import _matchDepth, _setDimension
# axes as mapped by Maya's rotate order indices
XYZ = 0
YZX = 1
ZXY = 2
XZY = 3
YXZ = 4
ZYX = 5
# XYZ axes indices
X = 0
Y = 1
Z = 2
#----------------------------------------------- MATRIX MATH -----------------------------------------------#
def identity(count):
""" Creates 4x4 identity matrices
"""
return _matrixIdentity(count)
def to_euler(matrix, axes=XYZ):
""" Converts an euler angle to a 4x4 transform matrix
>>> m = random(2) # make 2 random matrices
>>> print (to_euler(m[0],0)) # from 1 matrix makes 1 euler angle with xyz rotate order
>>> print (to_euler(m,0)) # from 2 matrices makes 2 euler angles with xyz rotate order
>>> print (to_euler(m,[2,0])) # from 2 matrices makes 2 euler angles with zxy and xyz rotate orders
>>> print (to_euler(m[0],range(6))) # from 1 matrix makes 6 euler angles with all possible 6 rotate orders
"""
matrix = _setDimension(matrix,3, reshape_matrix=True)
axes = _setDimension(axes,1,dtype=np.int32)
matrix, axes = _matchDepth(matrix, axes)
return _matrixToEuler(matrix,axes)
def to_quaternion(matrix):
""" Converts 4x4 matrix to transform matrices
>>> m = random(2) # make 2 random matrices
>>> print (repr(to_quaternion(m[0]))) # from 1 matrix make 1 quaternion
>>> print (repr(to_quaternion(m))) # from 2 matrices make 2 quaternions
"""
matrix = _setDimension(matrix, 3, reshape_matrix=True)
return _matrixToQuaternion(matrix)
def normalize(matrix):
""" Normalizes the rotation component of transform matrices
"""
matrix = _setDimension(matrix, 3, reshape_matrix=True)
return _matrixNormalize(matrix)
def inverse(matrix):
""" Inverse a list of transform matrices
"""
matrix = _setDimension(matrix, 3, reshape_matrix=True)
return _matrixInverse(matrix)
def point(point, matrix):
""" Point * Matrix multiplication
"""
point = _setDimension(point, 2)
matrix = _setDimension(matrix, 3, reshape_matrix=True)
point, matrix = _matchDepth(point, matrix)
return _matrixPointMultiply(point[:, :3], matrix)
def multiply(matrix0, matrix1):
""" Matrix * Matrix multiplication
"""
matrix0 = _setDimension(matrix0, 3, reshape_matrix=True)
matrix1 = _setDimension(matrix1, 3, reshape_matrix=True)
matrix0, matrix1 = _matchDepth(matrix0, matrix1)
return _matrixMultiply(matrix0,matrix1)
def slerp(matrix0, matrix1, weight=0.5):
"""
slerp(matrix0, matrix1, weight=0.5)
Performs a spherical interpolation between two lists of transform matrices.
Translation component will be ignored.
Parameters
----------
matrix0 : *[4x4 float]* or *[[4x4 float],...]* or *[16 float]* or *[[16 float],...]
a single, or list of matrices which correspond to weight=0
matrix1 : *[float, float, float, float]* or *[[float, float, float, float],...]*
a single, or list of matrices which correspond to weight=1
weight : *float* or *[float,...]*
weight values to interpolate between quat0 and quat1. default = 0.5
Returns
-------
matrices : np.array(n,4,4)
a list of interpolated 4x4 transform matrices
See Also
--------
euler.slerp : Performs a spherical interpolation between two lists of euler angles.
quaternion.lerp : Performs a spherical interpolation between two lists of quaternions.
vector.slerp : Performs a spherical interpolation between two lists of vectors.
Examples
--------
>>> matrix0 = random(100) # init quat0
>>> matrix1 = random(100) # init quat1
>>> print (repr(slerp(matrix0 ,matrix1)) # get the halfway point between the two lists
>>> print (repr(slerp(matrix0, matrix1[0])) # get the halfway point between the all items of matrix0 and the first item of matrix1
"""
matrix0 = _setDimension(matrix0,3,reshape_matrix=True)
matrix1 = _setDimension(matrix1,3,reshape_matrix=True)
weight = _setDimension(weight,1)
matrix0, matrix1, weight = _matchDepth(matrix0, matrix1, weight)
q0 = _matrixToQuaternion(matrix0)
q1 = _matrixToQuaternion(matrix1)
q = _quaternionSlerp(q0, q1, weight)
return _quaternionToMatrix(q)
def interpolate(matrix0, matrix1, weight=0.5):
"""
interpolate(matrix0, matrix1, weight=0.5)
Performs interpolates scale, rotation and position between two lists of transform matrices.
Parameters
----------
matrix0 : *[4x4 float]* or *[[4x4 float],...]* or *[16 float]* or *[[16 float],...]
a single, or list of matrices which correspond to weight=0
matrix1 : *[float, float, float, float]* or *[[float, float, float, float],...]*
a single, or list of matrices which correspond to weight=1
weight : *float* or *[float,...]*
weight values to interpolate between quat0 and quat1. default = 0.5
Returns
-------
matrices : np.array(n,4,4)
a list of interpolated 4x4 transform matrices
Examples
--------
>>> matrix0 = random(100) # init matrix0
>>> matrix1 = random(100) # init matrix1
>>> print (repr(interpolate(matrix0, matrix1)) # get the halfway point between the two lists
>>> print (repr(interpolate(matrix0, matrix1[0])) # get the halfway point between the all items of matrix0 and the first item of matrix1
>>> print (repr(interpolate(matrix0[0], matrix1[0], weight=[.25,.5,.75])) # get the 1/4, 1/2 an 3/4 points between matrix0[0] and matrix1[0]
"""
# Set expected dimensions
matrix0 = _setDimension(matrix0,3,reshape_matrix=True)
matrix1 = _setDimension(matrix1,3,reshape_matrix=True)
weight = _setDimension(weight,1)
matrix0, matrix1, weight = _matchDepth(matrix0, matrix1, weight)
# Grab scale components
scale0 = np.einsum('...i,...i', matrix0[:,:3,:3], matrix0[:,:3,:3]) ** 0.5
scale1 = np.einsum('...i,...i', matrix1[:,:3,:3], matrix1[:,:3,:3]) ** 0.5
# Normalize matrices and interpolate rotation
matrix0[:,0,:3] /= scale0[:,0][:,None]
matrix0[:,1,:3] /= scale0[:,1][:,None]
matrix0[:,2,:3] /= scale0[:,2][:,None]
matrix1[:,0,:3] /= scale1[:,0][:,None]
matrix1[:,1,:3] /= scale1[:,1][:,None]
matrix1[:,2,:3] /= scale1[:,2][:,None]
q0 = _matrixToQuaternion(matrix0)
q1 = _matrixToQuaternion(matrix1)
matrix = _quaternionToMatrix(_quaternionSlerp(q0, q1, weight))
# Interpolate scale
scale = _vectorLerp(scale0,scale1,weight)
# Scale interpolated matrices
matrix[:,0,:3] *= scale[:,0][:,None]
matrix[:,1,:3] *= scale[:,1][:,None]
matrix[:,2,:3] *= scale[:,2][:,None]
# Interpolate position
matrix[:,3,:3] = _vectorLerp(matrix0[:,3,:3], matrix1[:,3,:3], weight)
return matrix
def local(matrix, parent_matrix):
""" Returns the local matrix to a parent matrix
"""
matrix = _setDimension(matrix, 3, reshape_matrix=True)
parent_matrix = _setDimension(parent_matrix, 3, reshape_matrix=True)
matrix, parent_matrix = _matchDepth(matrix, parent_matrix)
return _matrixMultiply(matrix, _matrixInverse(parent_matrix))
def random(n, seed=None, random_position=False):
""" Computes a list of random 4x4 rotation matrices
"""
euler = np.random.seed(seed)
euler = np.radians(360 - np.random.random((n,3))*720)
M = _eulerToMatrix(euler, np.zeros(euler.shape[0], dtype=np.int32))
if random_position:
M[:,3,:3] = 1 - np.random.random((n, 3)) * 2
return M