Skip to content

Commit

Permalink
v1.4.2: Fix __imatmul__ operator's incorrect result.
Browse files Browse the repository at this point in the history
  • Loading branch information
shBLOCK committed Oct 7, 2023
1 parent a446cdf commit 233392e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
17 changes: 10 additions & 7 deletions codegen/templates/transform_2d.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,15 @@ cdef class Transform2D:
return t

def __imatmul__(self, Transform2D other) -> Transform2D:
self.xx = other.tdotx(self.xx, self.xy)
self.xy = other.tdoty(self.xx, self.xy)
self.yx = other.tdotx(self.yx, self.yy)
self.yy = other.tdoty(self.yx, self.yy)
self.ox = other.mulx(self.ox, self.oy)
self.oy = other.muly(self.ox, self.oy)
cdef py_float xx = other.tdotx(self.xx, self.xy)
cdef py_float xy = other.tdoty(self.xx, self.xy)
cdef py_float yx = other.tdotx(self.yx, self.yy)
cdef py_float yy = other.tdoty(self.yx, self.yy)
cdef py_float ox = other.mulx(self.ox, self.oy)
cdef py_float oy = other.muly(self.ox, self.oy)
self.xx, self.xy = xx, xy
self.yx, self.yy = yx, yy
self.ox, self.oy = ox, oy
return self

cdef inline py_float _determinant(self) noexcept:
Expand Down Expand Up @@ -343,7 +346,7 @@ cdef class Transform2D:
return t

def rotate_ip(self, py_float rotation, /) -> Transform2D:
self.__imul__(Transform2D.rotation(rotation))
self.__imatmul__(Transform2D.rotation(rotation))
return self

def rotated(self, py_float rotation, /) -> Transform2D:
Expand Down
30 changes: 17 additions & 13 deletions codegen/templates/transform_3d.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,22 @@ cdef class Transform3D:
return t

def __imatmul__(self, Transform3D other) -> Transform3D:
self.xx = other.tdotx(self.xx, self.xy, self.xz)
self.xy = other.tdoty(self.xx, self.xy, self.xz)
self.xz = other.tdotz(self.xx, self.xy, self.xz)
self.yx = other.tdotx(self.yx, self.yy, self.yz)
self.yy = other.tdoty(self.yx, self.yy, self.yz)
self.yz = other.tdotz(self.yx, self.yy, self.yz)
self.zx = other.tdotx(self.zx, self.zy, self.zz)
self.zy = other.tdoty(self.zx, self.zy, self.zz)
self.zz = other.tdotz(self.zx, self.zy, self.zz)
self.ox = other.mulx(self.ox, self.oy, self.oz)
self.oy = other.muly(self.ox, self.oy, self.oz)
self.oz = other.mulz(self.ox, self.oy, self.oz)
cdef py_float xx = other.tdotx(self.xx, self.xy, self.xz)
cdef py_float xy = other.tdoty(self.xx, self.xy, self.xz)
cdef py_float xz = other.tdotz(self.xx, self.xy, self.xz)
cdef py_float yx = other.tdotx(self.yx, self.yy, self.yz)
cdef py_float yy = other.tdoty(self.yx, self.yy, self.yz)
cdef py_float yz = other.tdotz(self.yx, self.yy, self.yz)
cdef py_float zx = other.tdotx(self.zx, self.zy, self.zz)
cdef py_float zy = other.tdoty(self.zx, self.zy, self.zz)
cdef py_float zz = other.tdotz(self.zx, self.zy, self.zz)
cdef py_float ox = other.mulx(self.ox, self.oy, self.oz)
cdef py_float oy = other.muly(self.ox, self.oy, self.oz)
cdef py_float oz = other.mulz(self.ox, self.oy, self.oz)
self.xx, self.xy, self.xz = xx, xy, xz
self.yx, self.yy, self.yz = yx, yy, yz
self.zx, self.zy, self.zz = zx, zy, zz
self.ox, self.oy, self.oz = ox, oy, oz
return self

cdef inline py_float _determinant(self) noexcept:
Expand Down Expand Up @@ -424,7 +428,7 @@ cdef class Transform3D:
return t

def rotate_ip(self, Vec3 axis, py_float angle, /) -> Transform3D:
self.__imul__(Transform3D.rotating(axis, angle))
self.__imatmul__(Transform3D.rotating(axis, angle))
return self

def rotated(self, Vec3 axis, py_float angle, /) -> Transform3D:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ requires = ["setuptools", "cython"]

[project]
name = "gdmath"
version = "1.4.1"
version = "1.4.2"
description = "GdMath: a fast math library for game development"
readme = "README.md"
requires-python = ">= 3.9"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_transform_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ def test_matmul():
assert (t1 @ t2).is_close(ans)
assert t1(t2).is_close(ans)

def test_imatmul():
t1 = Transform2D(1, 2, 3, 4, 5, 6)
t2 = Transform2D(3, 1, 2, 6, 5, 4)
t3 = Transform2D(t1)
t3 @= t2
assert t2 @ t1 == t3

def test_determinant():
assert isclose(Transform2D(1.6, 2.5, 3.4, 4.3, 5.2, 6.1).determinant, -1.62)

Expand Down
7 changes: 7 additions & 0 deletions tests/test_transform_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def test_matmul():
assert (t1 @ t2).is_close(ans)
assert t1(t2).is_close(ans)

def test_imatmul():
t1 = Transform3D(*range(1, 13))
t2 = Transform3D(10, 8, 4, 6, 12, 7, 5, 3, 2, 1, 11, 9)
t3 = Transform3D(t1)
t3 @= t2
assert t2 @ t1 == t3

def test_determinant():
assert isclose(Transform3D(1.12, 2.11, 0, 0, 5.8, 6.7, 7.6, 8.5, 0, 1, 2, 3).determinant, 43.6572)

Expand Down

0 comments on commit 233392e

Please sign in to comment.