forked from cuddlyogre/ExportLDraw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrices.py
More file actions
184 lines (139 loc) · 4.36 KB
/
matrices.py
File metadata and controls
184 lines (139 loc) · 4.36 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
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
import math
import mathutils
# import numpy as np
using_np = 'np' in globals()
def Vector(array):
if using_np:
return np.array(array)
else:
return mathutils.Vector(array)
def Vector4(array):
if using_np:
return np.array(array + (1.0,))
else:
return mathutils.Vector(array + (1.0,))
def Matrix(array):
if using_np:
return np.array(array)
else:
return mathutils.Matrix(array)
def set_matrix_world(obj, matrix_world):
if using_np:
obj.matrix_world[0] = matrix_world[0]
obj.matrix_world[1] = matrix_world[1]
obj.matrix_world[2] = matrix_world[2]
obj.matrix_world[3] = matrix_world[3]
else:
obj.matrix_world = matrix_world
# https://www.kite.com/python/answers/how-to-normalize-an-array-in-numpy-in-python
def normalize(vector):
if using_np:
norm = np.linalg.norm(vector)
normal_array = vector / norm
return normal_array
else:
return vector.normalized()
def determinant(matrix):
if using_np:
pass
else:
return matrix.determinant()
def is_degenerate(matrix):
if using_np:
pass
else:
return determinant(matrix) == 0
def is_reversed(matrix):
if using_np:
pass
else:
return determinant(matrix) < 0
# https://stackoverflow.com/questions/9171158/how-do-you-get-the-magnitude-of-a-vector-in-numpy
def length(vector):
if using_np:
return math.sqrt(vector[0] ** 2 + vector[1] ** 2 + vector[2] ** 2)
return np.sqrt(vector.dot(vector))
return np.linalg.norm(vector)
else:
return vector.length
def dot(a, b):
if using_np:
return np.dot(a, b)
else:
return a.dot(b)
def cross(a, b):
if using_np:
return np.cross(a, b)
else:
return a.cross(b)
identity = mathutils.Matrix.Identity(4).freeze()
# identity = Matrix((
# (1.0, 0.0, 0.0, 0.0),
# (0.0, 1.0, 0.0, 0.0),
# (0.0, 0.0, 1.0, 0.0),
# (0.0, 0.0, 0.0, 1.0)
# )).freeze()
rotation = mathutils.Matrix.Rotation(math.radians(-90), 4, 'X').freeze()
# rotation = Matrix((
# (1.0, 0.0, 0.0, 0.0),
# (0.0, -4.371138828673793e-08, 1.0, 0.0),
# (0.0, -1.0, -4.371138828673793e-08, 0.0),
# (0.0, 0.0, 0.0, 1.0)
# ))
reverse_rotation = mathutils.Matrix.Rotation(math.radians(90), 4, 'X').freeze()
# reverse_rotation = Matrix((
# (1.0, 0.0, 0.0, 0.0),
# (0.0, 7.549790126404332e-08, -1.0, 0.0),
# (0.0, 1.0, 7.549790126404332e-08, 0.0),
# (0.0, 0.0, 0.0, 1.0)
# ))
reflection = Matrix((
(1.0, 0.0, 0.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, -1.0, 0.0),
(0.0, 0.0, 0.0, 1.0)
)).freeze()
# mathutils.Matrix.Scale(scale, 4)
def scaled_matrix(scale=1.0):
return Matrix((
(scale, 0.0, 0.0, 0.0),
(0.0, scale, 0.0, 0.0),
(0.0, 0.0, scale, 0.0),
(0.0, 0.0, 0.0, 1.0)
)).freeze()
def mt4(matrix):
if using_np:
return np.array((
matrix[0],
matrix[1],
matrix[2],
matrix[3],
))
else:
return matrix
# https://bluebubblebee.medium.com/lets-talk-about-the-dot-product-in-videogames-67d833872820
# The dot product is also the cosine of the angle that both vector form, divided by their length
# dotProduct(v0, v1) === cos(angle_between_v0_and_v1) / (length(v1) * length(v2))
def dot_product(v0, v1):
return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2]
# https://stackoverflow.com/a/48266808
def unit_vector(vector):
""" Returns the unit vector of the vector."""
return vector / np.linalg.norm(vector)
def angle_between(v1, v2):
"""Finds angle between two vectors"""
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
def x_rotation(vector, theta):
"""Rotates 3-D vector around x-axis"""
R = np.array([[1, 0, 0], [0, np.cos(theta), -np.sin(theta)], [0, np.sin(theta), np.cos(theta)]])
return np.dot(R, vector)
def y_rotation(vector, theta):
"""Rotates 3-D vector around y-axis"""
R = np.array([[np.cos(theta), 0, np.sin(theta)], [0, 1, 0], [-np.sin(theta), 0, np.cos(theta)]])
return np.dot(R, vector)
def z_rotation(vector, theta):
"""Rotates 3-D vector around z-axis"""
R = np.array([[np.cos(theta), -np.sin(theta), 0], [np.sin(theta), np.cos(theta), 0], [0, 0, 1]])
return np.dot(R, vector)