Skip to content

Commit d9823c8

Browse files
isHarryhK0lb3
authored andcommitted
refactor(math): Matrix4x4
1 parent 36907ac commit d9823c8

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

UnityPy/math/Matrix4x4.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1+
from typing import MutableSequence, Sequence, Union
12
from .Vector3 import Vector3
23

34

45
class Matrix4x4:
5-
M: list
6+
M: MutableSequence[float]
67

7-
def __init__(self, values):
8-
if len(values) != 16:
9-
raise ValueError(
10-
"There must be sixteen and only sixteen input values for Matrix."
11-
)
12-
self.M = values
8+
def __init__(self, values: Sequence[Union[int, float]]):
9+
if not isinstance(values, Sequence) or len(values) != 16:
10+
raise ValueError("Values must be a sequence with 16 elements.")
11+
if not all(isinstance(v, (int, float)) for v in values):
12+
raise TypeError("All values must be numeric.")
13+
self.M = [float(v) for v in values]
1314

1415
def __getitem__(self, index):
1516
if isinstance(index, tuple):
16-
index = index[0] + index[1] * 4
17+
row, col = index
18+
if not (0 <= row < 4 and 0 <= col < 4):
19+
raise IndexError("Row and column indices must in range [0, 3].")
20+
index = row + col * 4
21+
if not (0 <= index < 16):
22+
raise IndexError("Index out of range for Matrix4x4.")
1723
return self.M[index]
1824

1925
def __setitem__(self, index, value):
2026
if isinstance(index, tuple):
21-
# row, column
22-
index = index[0] + index[1] * 4
27+
row, col = index
28+
if not (0 <= row < 4 and 0 <= col < 4):
29+
raise IndexError("Row and column indices must in range [0, 3].")
30+
index = row + col * 4
31+
if not (0 <= index < 16):
32+
raise IndexError("Index out of range for Matrix4x4.")
2333
self.M[index] = value
2434

2535
def __eq__(self, other):
2636
if not isinstance(other, Matrix4x4):
2737
return False
28-
print()
38+
return all(abs(a - b) < 1e-6 for a, b in zip(self.M, other.M))
2939

3040
def __mul__(lhs, rhs):
3141
res = Matrix4x4([0] * 16)
@@ -134,7 +144,10 @@ def __mul__(lhs, rhs):
134144
@staticmethod
135145
def Scale(vector: Vector3):
136146
return Matrix4x4(
137-
[vector.X, 0, 0, 0, 0, vector.Y, 0, 0, 0, 0, vector.Z, 0, 0, 0, 0, 1]
147+
[vector.X, 0, 0, 0,
148+
0, vector.Y, 0, 0,
149+
0, 0, vector.Z, 0,
150+
0, 0, 0, 1]
138151
)
139152

140153
@property

0 commit comments

Comments
 (0)