| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
from Vector import Vector |
| 4 |
from Metadata import export, constmethod, class_declarations, no_args |
| 5 |
from PyObjectBase import PyObjectBase |
| 6 |
from enum import IntEnum |
| 7 |
from typing import overload, Union, Tuple, Sequence |
| 8 |
|
| 9 |
class ScaleType(IntEnum): |
| 10 |
Other = -1 |
| 11 |
NoScaling = 0 |
| 12 |
NonUniformRight = 1 |
| 13 |
NonUniformLeft = 2 |
| 14 |
Uniform = 3 |
| 15 |
|
| 16 |
@export( |
| 17 |
TwinPointer="Matrix4D", |
| 18 |
Constructor=True, |
| 19 |
Delete=True, |
| 20 |
NumberProtocol=True, |
| 21 |
RichCompare=True, |
| 22 |
) |
| 23 |
@class_declarations( |
| 24 |
"""public: |
| 25 |
MatrixPy(const Matrix4D & mat, PyTypeObject *T = &Type) |
| 26 |
:PyObjectBase(new Matrix4D(mat),T){} |
| 27 |
Matrix4D value() const |
| 28 |
{ return *(getMatrixPtr()); } |
| 29 |
""" |
| 30 |
) |
| 31 |
class Matrix(PyObjectBase): |
| 32 |
""" |
| 33 |
Base.Matrix class. |
| 34 |
|
| 35 |
A 4x4 Matrix. |
| 36 |
In particular, this matrix can represent an affine transformation, that is, |
| 37 |
given a 3D vector `x`, apply the transformation y = M*x + b, where the matrix |
| 38 |
`M` is a linear map and the vector `b` is a translation. |
| 39 |
`y` can be obtained using a linear transformation represented by the 4x4 matrix |
| 40 |
`A` conformed by the augmented 3x4 matrix (M|b), augmented by row with |
| 41 |
(0,0,0,1), therefore: (y, 1) = A*(x, 1). |
| 42 |
|
| 43 |
The following constructors are supported: |
| 44 |
|
| 45 |
Matrix() |
| 46 |
Empty constructor. |
| 47 |
|
| 48 |
Matrix(matrix) |
| 49 |
Copy constructor. |
| 50 |
matrix : Base.Matrix. |
| 51 |
|
| 52 |
Matrix(*coef) |
| 53 |
Define from 16 coefficients of the 4x4 matrix. |
| 54 |
coef : sequence of float |
| 55 |
The sequence can have up to 16 elements which complete the matrix by rows. |
| 56 |
|
| 57 |
Matrix(vector1, vector2, vector3, vector4) |
| 58 |
Define from four 3D vectors which represent the columns of the 3x4 submatrix, |
| 59 |
useful to represent an affine transformation. The fourth row is made up by |
| 60 |
(0,0,0,1). |
| 61 |
vector1 : Base.Vector |
| 62 |
vector2 : Base.Vector |
| 63 |
vector3 : Base.Vector |
| 64 |
vector4 : Base.Vector |
| 65 |
Default to (0,0,0). Optional. |
| 66 |
|
| 67 |
Author: Juergen Riegel ([email protected]) |
| 68 |
Licence: LGPL |
| 69 |
""" |
| 70 |
|
| 71 |
A11: float = 0.0 |
| 72 |
"""The (1,1) matrix element.""" |
| 73 |
A12: float = 0.0 |
| 74 |
"""The (1,2) matrix element.""" |
| 75 |
A13: float = 0.0 |
| 76 |
"""The (1,3) matrix element.""" |
| 77 |
A14: float = 0.0 |
| 78 |
"""The (1,4) matrix element.""" |
| 79 |
A21: float = 0.0 |
| 80 |
"""The (2,1) matrix element.""" |
| 81 |
A22: float = 0.0 |
| 82 |
"""The (2,2) matrix element.""" |
| 83 |
A23: float = 0.0 |
| 84 |
"""The (2,3) matrix element.""" |
| 85 |
A24: float = 0.0 |
| 86 |
"""The (2,4) matrix element.""" |
| 87 |
A31: float = 0.0 |
| 88 |
"""The (3,1) matrix element.""" |
| 89 |
A32: float = 0.0 |
| 90 |
"""The (3,2) matrix element.""" |
| 91 |
A33: float = 0.0 |
| 92 |
"""The (3,3) matrix element.""" |
| 93 |
A34: float = 0.0 |
| 94 |
"""The (3,4) matrix element.""" |
| 95 |
A41: float = 0.0 |
| 96 |
"""The (4,1) matrix element.""" |
| 97 |
A42: float = 0.0 |
| 98 |
"""The (4,2) matrix element.""" |
| 99 |
A43: float = 0.0 |
| 100 |
"""The (4,3) matrix element.""" |
| 101 |
A44: float = 0.0 |
| 102 |
"""The (4,4) matrix element.""" |
| 103 |
|
| 104 |
A: Sequence[float] = [] |
| 105 |
"""The matrix elements.""" |
| 106 |
|
| 107 |
@overload |
| 108 |
def move(self, vector: Vector) -> None: ... |
| 109 |
@overload |
| 110 |
def move(self, x: float, y: float, z: float) -> None: ... |
| 111 |
def move(self, *args) -> None: |
| 112 |
""" |
| 113 |
move(vector) -> None |
| 114 |
move(x, y, z) -> None |
| 115 |
|
| 116 |
Move the matrix along a vector, equivalent to left multiply the matrix |
| 117 |
by a pure translation transformation. |
| 118 |
|
| 119 |
vector : Base.Vector, tuple |
| 120 |
x : float |
| 121 |
`x` translation. |
| 122 |
y : float |
| 123 |
`y` translation. |
| 124 |
z : float |
| 125 |
`z` translation. |
| 126 |
""" |
| 127 |
... |
| 128 |
|
| 129 |
@overload |
| 130 |
def scale(self, vector: Vector) -> None: ... |
| 131 |
@overload |
| 132 |
def scale(self, x: float, y: float, z: float) -> None: ... |
| 133 |
@overload |
| 134 |
def scale(self, factor: float) -> None: ... |
| 135 |
def scale(self, *args) -> None: |
| 136 |
""" |
| 137 |
scale(vector) -> None |
| 138 |
scale(x, y, z) -> None |
| 139 |
scale(factor) -> None |
| 140 |
|
| 141 |
Scale the first three rows of the matrix. |
| 142 |
|
| 143 |
vector : Base.Vector |
| 144 |
x : float |
| 145 |
First row factor scale. |
| 146 |
y : float |
| 147 |
Second row factor scale. |
| 148 |
z : float |
| 149 |
Third row factor scale. |
| 150 |
factor : float |
| 151 |
global factor scale. |
| 152 |
""" |
| 153 |
... |
| 154 |
|
| 155 |
@constmethod |
| 156 |
def hasScale(self, tol: float = 0) -> ScaleType: |
| 157 |
""" |
| 158 |
hasScale(tol=0) -> ScaleType |
| 159 |
|
| 160 |
Return an enum value of ScaleType. Possible values are: |
| 161 |
Uniform, NonUniformLeft, NonUniformRight, NoScaling or Other |
| 162 |
if it's not a scale matrix. |
| 163 |
|
| 164 |
tol : float |
| 165 |
""" |
| 166 |
... |
| 167 |
|
| 168 |
@constmethod |
| 169 |
def decompose(self) -> Tuple["Matrix", "Matrix", "Matrix", "Matrix"]: |
| 170 |
""" |
| 171 |
decompose() -> Base.Matrix, Base.Matrix, Base.Matrix, Base.Matrix |
| 172 |
|
| 173 |
Return a tuple of matrices representing shear, scale, rotation and move. |
| 174 |
So that matrix = move * rotation * scale * shear. |
| 175 |
""" |
| 176 |
... |
| 177 |
|
| 178 |
@no_args |
| 179 |
def nullify(self) -> None: |
| 180 |
""" |
| 181 |
nullify() -> None |
| 182 |
|
| 183 |
Make this the null matrix. |
| 184 |
""" |
| 185 |
... |
| 186 |
|
| 187 |
@no_args |
| 188 |
@constmethod |
| 189 |
def isNull(self) -> bool: |
| 190 |
""" |
| 191 |
isNull() -> bool |
| 192 |
|
| 193 |
Check if this is the null matrix. |
| 194 |
""" |
| 195 |
... |
| 196 |
|
| 197 |
@no_args |
| 198 |
def unity(self) -> None: |
| 199 |
""" |
| 200 |
unity() -> None |
| 201 |
|
| 202 |
Make this matrix to unity (4D identity matrix). |
| 203 |
""" |
| 204 |
... |
| 205 |
|
| 206 |
@constmethod |
| 207 |
def isUnity(self, tol: float = 0.0) -> bool: |
| 208 |
""" |
| 209 |
isUnity([tol=0.0]) -> bool |
| 210 |
|
| 211 |
Check if this is the unit matrix (4D identity matrix). |
| 212 |
""" |
| 213 |
... |
| 214 |
|
| 215 |
def transform(self, vector: Vector, matrix2: "Matrix") -> None: |
| 216 |
""" |
| 217 |
transform(vector, matrix2) -> None |
| 218 |
|
| 219 |
Transform the matrix around a given point. |
| 220 |
Equivalent to left multiply the matrix by T*M*T_inv, where M is `matrix2`, T the |
| 221 |
translation generated by `vector` and T_inv the inverse translation. |
| 222 |
For example, if `matrix2` is a rotation, the result is the transformation generated |
| 223 |
by the current matrix followed by a rotation around the point represented by `vector`. |
| 224 |
|
| 225 |
vector : Base.Vector |
| 226 |
matrix2 : Base.Matrix |
| 227 |
""" |
| 228 |
... |
| 229 |
|
| 230 |
@constmethod |
| 231 |
def col(self, index: int) -> Vector: |
| 232 |
""" |
| 233 |
col(index) -> Base.Vector |
| 234 |
|
| 235 |
Return the vector of a column, that is, the vector generated by the three |
| 236 |
first elements of the specified column. |
| 237 |
|
| 238 |
index : int |
| 239 |
Required column index. |
| 240 |
""" |
| 241 |
... |
| 242 |
|
| 243 |
def setCol(self, index: int, vector: Vector) -> None: |
| 244 |
""" |
| 245 |
setCol(index, vector) -> None |
| 246 |
|
| 247 |
Set the vector of a column, that is, the three first elements of the specified |
| 248 |
column by index. |
| 249 |
|
| 250 |
index : int |
| 251 |
Required column index. |
| 252 |
vector : Base.Vector |
| 253 |
""" |
| 254 |
... |
| 255 |
|
| 256 |
@constmethod |
| 257 |
def row(self, index: int) -> Vector: |
| 258 |
""" |
| 259 |
row(index) -> Base.Vector |
| 260 |
|
| 261 |
Return the vector of a row, that is, the vector generated by the three |
| 262 |
first elements of the specified row. |
| 263 |
|
| 264 |
index : int |
| 265 |
Required row index. |
| 266 |
""" |
| 267 |
... |
| 268 |
|
| 269 |
def setRow(self, index: int, vector: Vector) -> None: |
| 270 |
""" |
| 271 |
setRow(index, vector) -> None |
| 272 |
|
| 273 |
Set the vector of a row, that is, the three first elements of the specified |
| 274 |
row by index. |
| 275 |
|
| 276 |
index : int |
| 277 |
Required row index. |
| 278 |
vector : Base.Vector |
| 279 |
""" |
| 280 |
... |
| 281 |
|
| 282 |
@no_args |
| 283 |
@constmethod |
| 284 |
def diagonal(self) -> Vector: |
| 285 |
""" |
| 286 |
diagonal() -> Base.Vector |
| 287 |
|
| 288 |
Return the diagonal of the 3x3 leading principal submatrix as vector. |
| 289 |
""" |
| 290 |
... |
| 291 |
|
| 292 |
def setDiagonal(self, vector: Vector) -> None: |
| 293 |
""" |
| 294 |
setDiagonal(vector) -> None |
| 295 |
|
| 296 |
Set the diagonal of the 3x3 leading principal submatrix. |
| 297 |
|
| 298 |
vector : Base.Vector |
| 299 |
""" |
| 300 |
... |
| 301 |
|
| 302 |
def rotateX(self, angle: float) -> None: |
| 303 |
""" |
| 304 |
rotateX(angle) -> None |
| 305 |
|
| 306 |
Rotate around X axis. |
| 307 |
|
| 308 |
angle : float |
| 309 |
Angle in radians. |
| 310 |
""" |
| 311 |
... |
| 312 |
|
| 313 |
def rotateY(self, angle: float) -> None: |
| 314 |
""" |
| 315 |
rotateY(angle) -> None |
| 316 |
|
| 317 |
Rotate around Y axis. |
| 318 |
|
| 319 |
angle : float |
| 320 |
Angle in radians. |
| 321 |
""" |
| 322 |
... |
| 323 |
|
| 324 |
def rotateZ(self, angle: float) -> None: |
| 325 |
""" |
| 326 |
rotateZ(angle) -> None |
| 327 |
|
| 328 |
Rotate around Z axis. |
| 329 |
|
| 330 |
angle : float |
| 331 |
Angle in radians. |
| 332 |
""" |
| 333 |
... |
| 334 |
|
| 335 |
@overload |
| 336 |
def multiply(self, matrix: "Matrix") -> "Matrix": ... |
| 337 |
@overload |
| 338 |
def multiply(self, vector: Vector) -> Vector: ... |
| 339 |
@constmethod |
| 340 |
def multiply(self, obj: Union["Matrix", Vector]) -> Union["Matrix", Vector]: |
| 341 |
""" |
| 342 |
multiply(matrix) -> Base.Matrix |
| 343 |
multiply(vector) -> Base.Vector |
| 344 |
|
| 345 |
Right multiply the matrix by the given object. |
| 346 |
If the argument is a vector, this is augmented to the 4D vector (`vector`, 1). |
| 347 |
|
| 348 |
matrix : Base.Matrix |
| 349 |
vector : Base.Vector |
| 350 |
""" |
| 351 |
... |
| 352 |
|
| 353 |
@constmethod |
| 354 |
def multVec(self, vector: Vector) -> Vector: |
| 355 |
""" |
| 356 |
multVec(vector) -> Base.Vector |
| 357 |
|
| 358 |
Compute the transformed vector using the matrix. |
| 359 |
|
| 360 |
vector : Base.Vector |
| 361 |
""" |
| 362 |
... |
| 363 |
|
| 364 |
@no_args |
| 365 |
def invert(self) -> None: |
| 366 |
""" |
| 367 |
invert() -> None |
| 368 |
|
| 369 |
Compute the inverse matrix in-place, if possible. |
| 370 |
""" |
| 371 |
... |
| 372 |
|
| 373 |
@no_args |
| 374 |
@constmethod |
| 375 |
def inverse(self) -> "Matrix": |
| 376 |
""" |
| 377 |
inverse() -> Base.Matrix |
| 378 |
|
| 379 |
Compute the inverse matrix, if possible. |
| 380 |
""" |
| 381 |
... |
| 382 |
|
| 383 |
@no_args |
| 384 |
def transpose(self) -> None: |
| 385 |
""" |
| 386 |
transpose() -> None |
| 387 |
|
| 388 |
Transpose the matrix in-place. |
| 389 |
""" |
| 390 |
... |
| 391 |
|
| 392 |
@no_args |
| 393 |
@constmethod |
| 394 |
def transposed(self) -> "Matrix": |
| 395 |
""" |
| 396 |
transposed() -> Base.Matrix |
| 397 |
|
| 398 |
Returns a transposed copy of this matrix. |
| 399 |
""" |
| 400 |
... |
| 401 |
|
| 402 |
@no_args |
| 403 |
@constmethod |
| 404 |
def determinant(self) -> float: |
| 405 |
""" |
| 406 |
determinant() -> float |
| 407 |
|
| 408 |
Compute the determinant of the matrix. |
| 409 |
""" |
| 410 |
... |
| 411 |
|
| 412 |
@constmethod |
| 413 |
def isOrthogonal(self, tol: float = 1e-6) -> float: |
| 414 |
""" |
| 415 |
isOrthogonal(tol=1e-6) -> float |
| 416 |
|
| 417 |
Checks if the matrix is orthogonal, i.e. M * M^T = k*I and returns |
| 418 |
the multiple of the identity matrix. If it's not orthogonal 0 is returned. |
| 419 |
|
| 420 |
tol : float |
| 421 |
Tolerance used to check orthogonality. |
| 422 |
""" |
| 423 |
... |
| 424 |
|
| 425 |
@constmethod |
| 426 |
def submatrix(self, dim: int) -> "Matrix": |
| 427 |
""" |
| 428 |
submatrix(dim) -> Base.Matrix |
| 429 |
|
| 430 |
Get the leading principal submatrix of the given dimension. |
| 431 |
The (4 - `dim`) remaining dimensions are completed with the |
| 432 |
corresponding identity matrix. |
| 433 |
|
| 434 |
dim : int |
| 435 |
Dimension parameter must be in the range [1,4]. |
| 436 |
""" |
| 437 |
... |
| 438 |
|
| 439 |
@no_args |
| 440 |
@constmethod |
| 441 |
def analyze(self) -> str: |
| 442 |
""" |
| 443 |
analyze() -> str |
| 444 |
|
| 445 |
Analyzes the type of transformation. |
| 446 |
""" |
| 447 |
... |