From 0da7f8d2941cfa15d19e4f58d6602e8e9d6216cd Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 26 Apr 2024 16:54:23 +0800 Subject: [PATCH] rename `invert` to `inverse` --- docs/modules/linalg.md | 7 +++++-- include/typings/linalg.pyi | 4 ++-- src/collections.cpp | 4 ++-- src/linalg.cpp | 35 +++++++++++++++++++++++++---------- tests/80_linalg.py | 9 +++++++-- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/docs/modules/linalg.md b/docs/modules/linalg.md index fc8c7a4c..82fc0ee1 100644 --- a/docs/modules/linalg.md +++ b/docs/modules/linalg.md @@ -20,6 +20,7 @@ class vec2(_StructLike['vec2']): def __init__(self, x: float, y: float) -> None: ... def __add__(self, other: vec2) -> vec2: ... def __sub__(self, other: vec2) -> vec2: ... + def __getitem__(self, index: int) -> float: ... @overload def __mul__(self, other: float) -> vec2: ... @@ -61,6 +62,7 @@ class vec3(_StructLike['vec3']): def __init__(self, x: float, y: float, z: float) -> None: ... def __add__(self, other: vec3) -> vec3: ... def __sub__(self, other: vec3) -> vec3: ... + def __getitem__(self, index: int) -> float: ... @overload def __mul__(self, other: float) -> vec3: ... @@ -87,6 +89,7 @@ class vec4(_StructLike['vec4']): def __init__(self, x: float, y: float, z: float, w: float) -> None: ... def __add__(self, other: vec4) -> vec4: ... def __sub__(self, other: vec4) -> vec4: ... + def __getitem__(self, index: int) -> float: ... @overload def __mul__(self, other: float) -> vec4: ... @@ -122,7 +125,7 @@ class mat3x3(_StructLike['mat3x3']): def __init__(self, a: list[float]): ... def determinant(self) -> float: ... - def invert(self) -> mat3x3: ... + def inverse(self) -> mat3x3: ... def transpose(self) -> mat3x3: ... def __getitem__(self, index: tuple[int, int]) -> float: ... @@ -142,7 +145,7 @@ class mat3x3(_StructLike['mat3x3']): def matmul(self, other: mat3x3, out: mat3x3 = None) -> mat3x3 | None: ... def copy_(self, other: mat3x3) -> None: ... - def invert_(self) -> None: ... + def inverse_(self) -> None: ... def transpose_(self) -> None: ... @staticmethod diff --git a/include/typings/linalg.pyi b/include/typings/linalg.pyi index dc7f20c2..cb1ebbb6 100644 --- a/include/typings/linalg.pyi +++ b/include/typings/linalg.pyi @@ -113,7 +113,7 @@ class mat3x3(_StructLike['mat3x3']): def __init__(self, a: list[float]): ... def determinant(self) -> float: ... - def invert(self) -> mat3x3: ... + def inverse(self) -> mat3x3: ... def transpose(self) -> mat3x3: ... def __getitem__(self, index: tuple[int, int]) -> float: ... @@ -133,7 +133,7 @@ class mat3x3(_StructLike['mat3x3']): def matmul(self, other: mat3x3, out: mat3x3 = None) -> mat3x3 | None: ... def copy_(self, other: mat3x3) -> None: ... - def invert_(self) -> None: ... + def inverse_(self) -> None: ... def transpose_(self) -> None: ... @staticmethod diff --git a/src/collections.cpp b/src/collections.cpp index 8376fb8a..c53b7620 100644 --- a/src/collections.cpp +++ b/src/collections.cpp @@ -386,7 +386,7 @@ namespace pkpy PyDeque::PyDeque(VM *vm, PyObject *iterable, PyObject *maxlen) { - if (!vm->py_eq(maxlen, vm->None)) // fix the maxlen first + if (maxlen != vm->None) // fix the maxlen first { int tmp = CAST(int, maxlen); if (tmp < 0) @@ -402,7 +402,7 @@ namespace pkpy this->bounded = false; this->maxlen = -1; } - if (!vm->py_eq(iterable, vm->None)) + if (iterable != vm->None) { this->dequeItems.clear(); // clear the deque auto _lock = vm->heap.gc_scope_lock(); // locking the heap diff --git a/src/linalg.cpp b/src/linalg.cpp index 4ca2bbeb..bc9d4233 100644 --- a/src/linalg.cpp +++ b/src/linalg.cpp @@ -405,24 +405,21 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s vm->bind__invert__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ Mat3x3& self = _CAST(Mat3x3&, obj); Mat3x3 ret; - bool ok = self.inverse(ret); - if(!ok) vm->ValueError("matrix is not invertible"); + if(!self.inverse(ret)) vm->ValueError("matrix is not invertible"); return VAR_T(Mat3x3, ret); }); - vm->bind_method<0>(type, "invert", [](VM* vm, ArgsView args){ + vm->bind_method<0>(type, "inverse", [](VM* vm, ArgsView args){ Mat3x3& self = _CAST(Mat3x3&, args[0]); Mat3x3 ret; - bool ok = self.inverse(ret); - if(!ok) vm->ValueError("matrix is not invertible"); + if(!self.inverse(ret)) vm->ValueError("matrix is not invertible"); return VAR_T(Mat3x3, ret); }); - vm->bind_method<0>(type, "invert_", [](VM* vm, ArgsView args){ + vm->bind_method<0>(type, "inverse_", [](VM* vm, ArgsView args){ Mat3x3& self = _CAST(Mat3x3&, args[0]); Mat3x3 ret; - bool ok = self.inverse(ret); - if(!ok) vm->ValueError("matrix is not invertible"); + if(!self.inverse(ret)) vm->ValueError("matrix is not invertible"); self = ret; return vm->None; }); @@ -510,14 +507,32 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s vm->bind_method<1>(type, "transform_point", [](VM* vm, ArgsView args){ const Mat3x3& self = _CAST(Mat3x3&, args[0]); Vec2 v = CAST(Vec2, args[1]); - Vec2 res = Vec2(self._11 * v.x + self._12 * v.y + self._13, self._21 * v.x + self._22 * v.y + self._23); + Vec2 res(self._11 * v.x + self._12 * v.y + self._13, self._21 * v.x + self._22 * v.y + self._23); + return VAR_T(Vec2, res); + }); + + vm->bind_method<1>(type, "inverse_transform_point", [](VM* vm, ArgsView args){ + const Mat3x3& self = _CAST(Mat3x3&, args[0]); + Vec2 v = CAST(Vec2, args[1]); + Mat3x3 inv; + if(!self.inverse(inv)) vm->ValueError("matrix is not invertible"); + Vec2 res(inv._11 * v.x + inv._12 * v.y + inv._13, inv._21 * v.x + inv._22 * v.y + inv._23); return VAR_T(Vec2, res); }); vm->bind_method<1>(type, "transform_vector", [](VM* vm, ArgsView args){ const Mat3x3& self = _CAST(Mat3x3&, args[0]); Vec2 v = CAST(Vec2, args[1]); - Vec2 res = Vec2(self._11 * v.x + self._12 * v.y, self._21 * v.x + self._22 * v.y); + Vec2 res(self._11 * v.x + self._12 * v.y, self._21 * v.x + self._22 * v.y); + return VAR_T(Vec2, res); + }); + + vm->bind_method<1>(type, "inverse_transform_vector", [](VM* vm, ArgsView args){ + const Mat3x3& self = _CAST(Mat3x3&, args[0]); + Vec2 v = CAST(Vec2, args[1]); + Mat3x3 inv; + if(!self.inverse(inv)) vm->ValueError("matrix is not invertible"); + Vec2 res(inv._11 * v.x + inv._12 * v.y, inv._21 * v.x + inv._22 * v.y); return VAR_T(Vec2, res); }); } diff --git a/tests/80_linalg.py b/tests/80_linalg.py index a3d69577..bd887451 100644 --- a/tests/80_linalg.py +++ b/tests/80_linalg.py @@ -349,8 +349,8 @@ assert test_mat_copy == test_mat.transpose() assert test_mat_copy.transpose() == test_mat_copy.transpose().transpose().transpose() # test inverse -assert ~static_test_mat_float == static_test_mat_float_inv == static_test_mat_float.invert() -assert static_test_mat_float.invert_() is None +assert ~static_test_mat_float == static_test_mat_float_inv == static_test_mat_float.inverse() +assert static_test_mat_float.inverse_() is None assert static_test_mat_float == static_test_mat_float_inv try: @@ -438,6 +438,11 @@ test_mat_copy = test_mat.copy() test_vec2_copy = test_vec2.copy() temp_vec2 = test_mat_copy.transform_vector(test_vec2_copy) +# test inverse_transform_point +assert test_mat_copy.inverse_transform_point(test_vec2_copy) == test_mat_copy.inverse().transform_point(test_vec2_copy) +# test inverse_transform_vector +assert test_mat_copy.inverse_transform_vector(test_vec2_copy) == test_mat_copy.inverse().transform_vector(test_vec2_copy) + import c a = vec2(1, 2) b = a.to_struct()