rename invert to inverse

This commit is contained in:
blueloveTH 2024-04-26 16:54:23 +08:00
parent 99195ac490
commit 0da7f8d294
5 changed files with 41 additions and 18 deletions

View File

@ -20,6 +20,7 @@ class vec2(_StructLike['vec2']):
def __init__(self, x: float, y: float) -> None: ... def __init__(self, x: float, y: float) -> None: ...
def __add__(self, other: vec2) -> vec2: ... def __add__(self, other: vec2) -> vec2: ...
def __sub__(self, other: vec2) -> vec2: ... def __sub__(self, other: vec2) -> vec2: ...
def __getitem__(self, index: int) -> float: ...
@overload @overload
def __mul__(self, other: float) -> vec2: ... 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 __init__(self, x: float, y: float, z: float) -> None: ...
def __add__(self, other: vec3) -> vec3: ... def __add__(self, other: vec3) -> vec3: ...
def __sub__(self, other: vec3) -> vec3: ... def __sub__(self, other: vec3) -> vec3: ...
def __getitem__(self, index: int) -> float: ...
@overload @overload
def __mul__(self, other: float) -> vec3: ... 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 __init__(self, x: float, y: float, z: float, w: float) -> None: ...
def __add__(self, other: vec4) -> vec4: ... def __add__(self, other: vec4) -> vec4: ...
def __sub__(self, other: vec4) -> vec4: ... def __sub__(self, other: vec4) -> vec4: ...
def __getitem__(self, index: int) -> float: ...
@overload @overload
def __mul__(self, other: float) -> vec4: ... def __mul__(self, other: float) -> vec4: ...
@ -122,7 +125,7 @@ class mat3x3(_StructLike['mat3x3']):
def __init__(self, a: list[float]): ... def __init__(self, a: list[float]): ...
def determinant(self) -> float: ... def determinant(self) -> float: ...
def invert(self) -> mat3x3: ... def inverse(self) -> mat3x3: ...
def transpose(self) -> mat3x3: ... def transpose(self) -> mat3x3: ...
def __getitem__(self, index: tuple[int, int]) -> float: ... 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 matmul(self, other: mat3x3, out: mat3x3 = None) -> mat3x3 | None: ...
def copy_(self, other: mat3x3) -> None: ... def copy_(self, other: mat3x3) -> None: ...
def invert_(self) -> None: ... def inverse_(self) -> None: ...
def transpose_(self) -> None: ... def transpose_(self) -> None: ...
@staticmethod @staticmethod

View File

@ -113,7 +113,7 @@ class mat3x3(_StructLike['mat3x3']):
def __init__(self, a: list[float]): ... def __init__(self, a: list[float]): ...
def determinant(self) -> float: ... def determinant(self) -> float: ...
def invert(self) -> mat3x3: ... def inverse(self) -> mat3x3: ...
def transpose(self) -> mat3x3: ... def transpose(self) -> mat3x3: ...
def __getitem__(self, index: tuple[int, int]) -> float: ... 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 matmul(self, other: mat3x3, out: mat3x3 = None) -> mat3x3 | None: ...
def copy_(self, other: mat3x3) -> None: ... def copy_(self, other: mat3x3) -> None: ...
def invert_(self) -> None: ... def inverse_(self) -> None: ...
def transpose_(self) -> None: ... def transpose_(self) -> None: ...
@staticmethod @staticmethod

View File

@ -386,7 +386,7 @@ namespace pkpy
PyDeque::PyDeque(VM *vm, PyObject *iterable, PyObject *maxlen) 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); int tmp = CAST(int, maxlen);
if (tmp < 0) if (tmp < 0)
@ -402,7 +402,7 @@ namespace pkpy
this->bounded = false; this->bounded = false;
this->maxlen = -1; this->maxlen = -1;
} }
if (!vm->py_eq(iterable, vm->None)) if (iterable != vm->None)
{ {
this->dequeItems.clear(); // clear the deque this->dequeItems.clear(); // clear the deque
auto _lock = vm->heap.gc_scope_lock(); // locking the heap auto _lock = vm->heap.gc_scope_lock(); // locking the heap

View File

@ -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){ vm->bind__invert__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
Mat3x3& self = _CAST(Mat3x3&, obj); Mat3x3& self = _CAST(Mat3x3&, obj);
Mat3x3 ret; Mat3x3 ret;
bool ok = self.inverse(ret); if(!self.inverse(ret)) vm->ValueError("matrix is not invertible");
if(!ok) vm->ValueError("matrix is not invertible");
return VAR_T(Mat3x3, ret); 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& self = _CAST(Mat3x3&, args[0]);
Mat3x3 ret; Mat3x3 ret;
bool ok = self.inverse(ret); if(!self.inverse(ret)) vm->ValueError("matrix is not invertible");
if(!ok) vm->ValueError("matrix is not invertible");
return VAR_T(Mat3x3, ret); 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& self = _CAST(Mat3x3&, args[0]);
Mat3x3 ret; Mat3x3 ret;
bool ok = self.inverse(ret); if(!self.inverse(ret)) vm->ValueError("matrix is not invertible");
if(!ok) vm->ValueError("matrix is not invertible");
self = ret; self = ret;
return vm->None; 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){ vm->bind_method<1>(type, "transform_point", [](VM* vm, ArgsView args){
const Mat3x3& self = _CAST(Mat3x3&, args[0]); const Mat3x3& self = _CAST(Mat3x3&, args[0]);
Vec2 v = CAST(Vec2, args[1]); 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); return VAR_T(Vec2, res);
}); });
vm->bind_method<1>(type, "transform_vector", [](VM* vm, ArgsView args){ vm->bind_method<1>(type, "transform_vector", [](VM* vm, ArgsView args){
const Mat3x3& self = _CAST(Mat3x3&, args[0]); const Mat3x3& self = _CAST(Mat3x3&, args[0]);
Vec2 v = CAST(Vec2, args[1]); 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); return VAR_T(Vec2, res);
}); });
} }

View File

@ -349,8 +349,8 @@ assert test_mat_copy == test_mat.transpose()
assert test_mat_copy.transpose() == test_mat_copy.transpose().transpose().transpose() assert test_mat_copy.transpose() == test_mat_copy.transpose().transpose().transpose()
# test inverse # test inverse
assert ~static_test_mat_float == static_test_mat_float_inv == static_test_mat_float.invert() assert ~static_test_mat_float == static_test_mat_float_inv == static_test_mat_float.inverse()
assert static_test_mat_float.invert_() is None assert static_test_mat_float.inverse_() is None
assert static_test_mat_float == static_test_mat_float_inv assert static_test_mat_float == static_test_mat_float_inv
try: try:
@ -438,6 +438,11 @@ test_mat_copy = test_mat.copy()
test_vec2_copy = test_vec2.copy() test_vec2_copy = test_vec2.copy()
temp_vec2 = test_mat_copy.transform_vector(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 import c
a = vec2(1, 2) a = vec2(1, 2)
b = a.to_struct() b = a.to_struct()