|
| 1 | +from typing import MutableSequence, Sequence, Union |
1 | 2 | from .Vector3 import Vector3
|
2 | 3 |
|
3 | 4 |
|
4 | 5 | class Matrix4x4:
|
5 |
| - M: list |
| 6 | + M: MutableSequence[float] |
6 | 7 |
|
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] |
13 | 14 |
|
14 | 15 | def __getitem__(self, index):
|
15 | 16 | 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.") |
17 | 23 | return self.M[index]
|
18 | 24 |
|
19 | 25 | def __setitem__(self, index, value):
|
20 | 26 | 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.") |
23 | 33 | self.M[index] = value
|
24 | 34 |
|
25 | 35 | def __eq__(self, other):
|
26 | 36 | if not isinstance(other, Matrix4x4):
|
27 | 37 | return False
|
28 |
| - print() |
| 38 | + return all(abs(a - b) < 1e-6 for a, b in zip(self.M, other.M)) |
29 | 39 |
|
30 | 40 | def __mul__(lhs, rhs):
|
31 | 41 | res = Matrix4x4([0] * 16)
|
@@ -134,7 +144,10 @@ def __mul__(lhs, rhs):
|
134 | 144 | @staticmethod
|
135 | 145 | def Scale(vector: Vector3):
|
136 | 146 | 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] |
138 | 151 | )
|
139 | 152 |
|
140 | 153 | @property
|
|
0 commit comments