Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
mozman committed Jan 13, 2021
1 parent 833fed8 commit f14be14
Showing 1 changed file with 77 additions and 73 deletions.
150 changes: 77 additions & 73 deletions src/ezdxf/math/_matrix44.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Home-page: http://code.google.com/p/gameobjects/
# Author: Will McGugan
# Download-URL: http://code.google.com/p/gameobjects/downloads/list
# Copyright (c) 2010-2020 Manfred Moitzi
# Copyright (c) 2010-2021 Manfred Moitzi
# License: MIT License
from typing import Sequence, Iterable, List, Tuple, TYPE_CHECKING
import math
Expand All @@ -17,26 +17,31 @@
__all__ = ['Matrix44']


# removed array.array because array is optimized for space not speed, and space optimization is not needed
# removed array.array because array is optimized for space not speed, and space
# optimization is not needed

def floats(items: Iterable) -> List[float]:
return [float(v) for v in items]


class Matrix44:
"""
This is a pure Python implementation for 4x4 transformation matrices, to avoid dependency to big numerical packages
like :mod:`numpy`, before binary wheels, installation of these packages wasn't always easy on Windows.
This is a pure Python implementation for 4x4 transformation matrices, to
avoid dependency to big numerical packages like :mod:`numpy`, before binary
wheels, installation of these packages wasn't always easy on Windows.
The utility functions for constructing transformations and transforming vectors and points assumes that vectors
are stored as row vectors, meaning when multiplied, transformations are applied left to right
(e.g. vAB transforms v by A then by B).
The utility functions for constructing transformations and transforming
vectors and points assumes that vectors are stored as row vectors, meaning
when multiplied, transformations are applied left to right (e.g. vAB
transforms v by A then by B).
Matrix44 initialization:
- ``Matrix44()`` returns the identity matrix.
- ``Matrix44(values)`` values is an iterable with the 16 components of the matrix.
- ``Matrix44(row1, row2, row3, row4)`` four rows, each row with four values.
- ``Matrix44(values)`` values is an iterable with the 16 components of
the matrix.
- ``Matrix44(row1, row2, row3, row4)`` four rows, each row with four
values.
"""
_identity = (
Expand Down Expand Up @@ -64,8 +69,8 @@ def __init__(self, *args):
elif nargs == 4:
self._matrix = floats(chain(*args))
else:
raise ValueError(
"Invalid count of arguments (4 row vectors or one list with 16 values).")
raise ValueError("Invalid count of arguments (4 row vectors or one "
"list with 16 values).")
if len(self._matrix) != 16:
raise ValueError("Invalid matrix count")

Expand All @@ -81,7 +86,8 @@ def format_row(row):
", ".join(format_row(row) for row in self.rows())

def get_2d_transformation(self) -> Tuple[float, ...]:
""" Returns a the 2D transformation as a row-major matrix in a linear array (tuple).
""" Returns a the 2D transformation as a row-major matrix in a linear
array (tuple).
A more correct transformation could be implemented like so:
https://stackoverflow.com/questions/10629737/convert-3d-4x4-rotation-matrix-into-2d
Expand Down Expand Up @@ -179,14 +185,17 @@ def uz(self) -> Vec3:

@property
def is_cartesian(self) -> bool:
""" Returns ``True`` if target coordinate system is a right handed orthogonal coordinate system. """
""" Returns ``True`` if target coordinate system is a right handed
orthogonal coordinate system.
"""
return self.uy.cross(self.uz).normalize().isclose(self.ux.normalize())

@property
def is_orthogonal(self) -> bool:
""" Returns ``True`` if target coordinate system has orthogonal axis.
Does not check for left- or right handed orientation, any orientation of the axis valid.
Does not check for left- or right handed orientation, any orientation
of the axis valid.
"""
ux = self.ux.normalize()
Expand All @@ -198,8 +207,8 @@ def is_orthogonal(self) -> bool:

@classmethod
def scale(cls, sx: float, sy: float = None, sz: float = None) -> 'Matrix44':
"""
Returns a scaling transformation matrix. If `sy` is ``None``, `sy` = `sx`, and if `sz` is ``None`` `sz` = `sx`.
""" Returns a scaling transformation matrix. If `sy` is ``None``,
`sy` = `sx`, and if `sz` is ``None`` `sz` = `sx`.
"""
if sy is None:
Expand All @@ -217,7 +226,8 @@ def scale(cls, sx: float, sy: float = None, sz: float = None) -> 'Matrix44':

@classmethod
def translate(cls, dx: float, dy: float, dz: float) -> 'Matrix44':
""" Returns a translation matrix for translation vector (dx, dy, dz). """
""" Returns a translation matrix for translation vector (dx, dy, dz).
"""
return cls([
1., 0., 0., 0.,
0., 1., 0., 0.,
Expand All @@ -227,8 +237,7 @@ def translate(cls, dx: float, dy: float, dz: float) -> 'Matrix44':

@classmethod
def x_rotate(cls, angle: float) -> 'Matrix44':
"""
Returns a rotation matrix about the x-axis.
""" Returns a rotation matrix about the x-axis.
Args:
angle: rotation angle in radians
Expand All @@ -245,8 +254,7 @@ def x_rotate(cls, angle: float) -> 'Matrix44':

@classmethod
def y_rotate(cls, angle: float) -> 'Matrix44':
"""
Returns a rotation matrix about the y-axis.
""" Returns a rotation matrix about the y-axis.
Args:
angle: rotation angle in radians
Expand All @@ -263,8 +271,7 @@ def y_rotate(cls, angle: float) -> 'Matrix44':

@classmethod
def z_rotate(cls, angle: float) -> 'Matrix44':
"""
Returns a rotation matrix about the z-axis.
""" Returns a rotation matrix about the z-axis.
Args:
angle: rotation angle in radians
Expand All @@ -281,8 +288,7 @@ def z_rotate(cls, angle: float) -> 'Matrix44':

@classmethod
def axis_rotate(cls, axis: 'Vertex', angle: float) -> 'Matrix44':
"""
Returns a rotation matrix about an arbitrary `axis`.
""" Returns a rotation matrix about an arbitrary `axis`.
Args:
axis: rotation axis as ``(x, y, z)`` tuple or :class:`Vec3` object
Expand Down Expand Up @@ -332,8 +338,7 @@ def xyz_rotate(cls, angle_x: float, angle_y: float,
def perspective_projection(cls, left: float, right: float, top: float,
bottom: float, near: float,
far: float) -> 'Matrix44':
"""
Returns a matrix for a 2D projection.
""" Returns a matrix for a 2D projection.
Args:
left: Coordinate of left of screen
Expand All @@ -355,8 +360,7 @@ def perspective_projection(cls, left: float, right: float, top: float,
@classmethod
def perspective_projection_fov(cls, fov: float, aspect: float, near: float,
far: float) -> 'Matrix44':
"""
Returns a matrix for a 2D projection.
""" Returns a matrix for a 2D projection.
Args:
fov: The field of view (in radians)
Expand All @@ -382,8 +386,7 @@ def chain(*matrices: 'Matrix44') -> 'Matrix44':

@staticmethod
def ucs(ux=X_AXIS, uy=Y_AXIS, uz=Z_AXIS, origin=NULLVEC) -> 'Matrix44':
"""
Returns a matrix for coordinate transformation from WCS to UCS.
""" Returns a matrix for coordinate transformation from WCS to UCS.
For transformation from UCS to WCS, transpose the returned matrix.
Args:
Expand Down Expand Up @@ -425,15 +428,19 @@ def __iter__(self) -> Iterable[float]:
return iter(self._matrix)

def __mul__(self, other: 'Matrix44') -> 'Matrix44':
""" Returns a new matrix as result of the matrix multiplication with another matrix. """
""" Returns a new matrix as result of the matrix multiplication with
another matrix.
"""
res_matrix = self.copy()
res_matrix.__imul__(other)
return res_matrix

# __matmul__ = __mul__ does not work!

def __matmul__(self, other: 'Matrix44') -> 'Matrix44':
""" Returns a new matrix as result of the matrix multiplication with another matrix. """
""" Returns a new matrix as result of the matrix multiplication with
another matrix.
"""
res_matrix = self.copy()
res_matrix.__imul__(other)
return res_matrix
Expand Down Expand Up @@ -505,8 +512,8 @@ def transform_vertices(self, vectors: Iterable['Vertex']) -> Iterable[Vec3]:

def transform_directions(self, vectors: Iterable['Vertex'],
normalize=False) -> Iterable[Vec3]:
"""
Returns an iterable of transformed direction vectors without translation.
""" Returns an iterable of transformed direction vectors without
translation.
"""
m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, *_ = self._matrix
Expand All @@ -520,8 +527,7 @@ def transform_directions(self, vectors: Iterable['Vertex'],
yield v.normalize() if normalize else v

def ucs_vertex_from_wcs(self, wcs: Vec3) -> Vec3:
"""
Returns an UCS vector from WCS vertex.
""" Returns an UCS vector from WCS vertex.
Works only if matrix is used as cartesian UCS without scaling.
Expand All @@ -531,8 +537,7 @@ def ucs_vertex_from_wcs(self, wcs: Vec3) -> Vec3:
return self.ucs_direction_from_wcs(wcs - self.origin)

def ucs_direction_from_wcs(self, wcs: Vec3) -> Vec3:
"""
Returns UCS direction vector from WCS direction.
""" Returns UCS direction vector from WCS direction.
Works only if matrix is used as cartesian UCS without scaling.
Expand Down Expand Up @@ -583,8 +588,7 @@ def determinant(self) -> float:
m03 * m12 * m20 * m31 + m03 * m12 * m21 * m30

def inverse(self) -> None:
"""
Calculates the inverse of the matrix.
""" Calculates the inverse of the matrix.
Raises:
ZeroDivisionError: if matrix has no inverse.
Expand All @@ -598,51 +602,51 @@ def inverse(self) -> None:
m30, m31, m32, m33 = self._matrix
self._matrix = [
(
m12 * m23 * m31 - m13 * m22 * m31 + m13 * m21 * m32 - m11 * m23 * m32 - m12 * m21 * m33 +
m11 * m22 * m33) * f,
m12 * m23 * m31 - m13 * m22 * m31 + m13 * m21 * m32 -
m11 * m23 * m32 - m12 * m21 * m33 + m11 * m22 * m33) * f,
(
m03 * m22 * m31 - m02 * m23 * m31 - m03 * m21 * m32 + m01 * m23 * m32 + m02 * m21 * m33 -
m01 * m22 * m33) * f,
m03 * m22 * m31 - m02 * m23 * m31 - m03 * m21 * m32 +
m01 * m23 * m32 + m02 * m21 * m33 - m01 * m22 * m33) * f,
(
m02 * m13 * m31 - m03 * m12 * m31 + m03 * m11 * m32 - m01 * m13 * m32 - m02 * m11 * m33 +
m01 * m12 * m33) * f,
m02 * m13 * m31 - m03 * m12 * m31 + m03 * m11 * m32 -
m01 * m13 * m32 - m02 * m11 * m33 + m01 * m12 * m33) * f,
(
m03 * m12 * m21 - m02 * m13 * m21 - m03 * m11 * m22 + m01 * m13 * m22 + m02 * m11 * m23 -
m01 * m12 * m23) * f,
m03 * m12 * m21 - m02 * m13 * m21 - m03 * m11 * m22 +
m01 * m13 * m22 + m02 * m11 * m23 - m01 * m12 * m23) * f,
(
m13 * m22 * m30 - m12 * m23 * m30 - m13 * m20 * m32 + m10 * m23 * m32 + m12 * m20 * m33 -
m10 * m22 * m33) * f,
m13 * m22 * m30 - m12 * m23 * m30 - m13 * m20 * m32 +
m10 * m23 * m32 + m12 * m20 * m33 - m10 * m22 * m33) * f,
(
m02 * m23 * m30 - m03 * m22 * m30 + m03 * m20 * m32 - m00 * m23 * m32 - m02 * m20 * m33 +
m00 * m22 * m33) * f,
m02 * m23 * m30 - m03 * m22 * m30 + m03 * m20 * m32 -
m00 * m23 * m32 - m02 * m20 * m33 + m00 * m22 * m33) * f,
(
m03 * m12 * m30 - m02 * m13 * m30 - m03 * m10 * m32 + m00 * m13 * m32 + m02 * m10 * m33 -
m00 * m12 * m33) * f,
m03 * m12 * m30 - m02 * m13 * m30 - m03 * m10 * m32 +
m00 * m13 * m32 + m02 * m10 * m33 - m00 * m12 * m33) * f,
(
m02 * m13 * m20 - m03 * m12 * m20 + m03 * m10 * m22 - m00 * m13 * m22 - m02 * m10 * m23 +
m00 * m12 * m23) * f,
m02 * m13 * m20 - m03 * m12 * m20 + m03 * m10 * m22 -
m00 * m13 * m22 - m02 * m10 * m23 + m00 * m12 * m23) * f,
(
m11 * m23 * m30 - m13 * m21 * m30 + m13 * m20 * m31 - m10 * m23 * m31 - m11 * m20 * m33 +
m10 * m21 * m33) * f,
m11 * m23 * m30 - m13 * m21 * m30 + m13 * m20 * m31 -
m10 * m23 * m31 - m11 * m20 * m33 + m10 * m21 * m33) * f,
(
m03 * m21 * m30 - m01 * m23 * m30 - m03 * m20 * m31 + m00 * m23 * m31 + m01 * m20 * m33 -
m00 * m21 * m33) * f,
m03 * m21 * m30 - m01 * m23 * m30 - m03 * m20 * m31 +
m00 * m23 * m31 + m01 * m20 * m33 - m00 * m21 * m33) * f,
(
m01 * m13 * m30 - m03 * m11 * m30 + m03 * m10 * m31 - m00 * m13 * m31 - m01 * m10 * m33 +
m00 * m11 * m33) * f,
m01 * m13 * m30 - m03 * m11 * m30 + m03 * m10 * m31 -
m00 * m13 * m31 - m01 * m10 * m33 + m00 * m11 * m33) * f,
(
m03 * m11 * m20 - m01 * m13 * m20 - m03 * m10 * m21 + m00 * m13 * m21 + m01 * m10 * m23 -
m00 * m11 * m23) * f,
m03 * m11 * m20 - m01 * m13 * m20 - m03 * m10 * m21 +
m00 * m13 * m21 + m01 * m10 * m23 - m00 * m11 * m23) * f,
(
m12 * m21 * m30 - m11 * m22 * m30 - m12 * m20 * m31 + m10 * m22 * m31 + m11 * m20 * m32 -
m10 * m21 * m32) * f,
m12 * m21 * m30 - m11 * m22 * m30 - m12 * m20 * m31 +
m10 * m22 * m31 + m11 * m20 * m32 - m10 * m21 * m32) * f,
(
m01 * m22 * m30 - m02 * m21 * m30 + m02 * m20 * m31 - m00 * m22 * m31 - m01 * m20 * m32 +
m00 * m21 * m32) * f,
m01 * m22 * m30 - m02 * m21 * m30 + m02 * m20 * m31 -
m00 * m22 * m31 - m01 * m20 * m32 + m00 * m21 * m32) * f,
(
m02 * m11 * m30 - m01 * m12 * m30 - m02 * m10 * m31 + m00 * m12 * m31 + m01 * m10 * m32 -
m00 * m11 * m32) * f,
m02 * m11 * m30 - m01 * m12 * m30 - m02 * m10 * m31 +
m00 * m12 * m31 + m01 * m10 * m32 - m00 * m11 * m32) * f,
(
m01 * m12 * m20 - m02 * m11 * m20 + m02 * m10 * m21 - m00 * m12 * m21 - m01 * m10 * m22 +
m00 * m11 * m22) * f,
m01 * m12 * m20 - m02 * m11 * m20 + m02 * m10 * m21 -
m00 * m12 * m21 - m01 * m10 * m22 + m00 * m11 * m22) * f,
]

0 comments on commit f14be14

Please sign in to comment.