瀏覽代碼

rename `invert` to `inverse`

blueloveTH 1 年之前
父節點
當前提交
0da7f8d294
共有 5 個文件被更改,包括 41 次插入18 次删除
  1. 5 2
      docs/modules/linalg.md
  2. 2 2
      include/typings/linalg.pyi
  3. 2 2
      src/collections.cpp
  4. 25 10
      src/linalg.cpp
  5. 7 2
      tests/80_linalg.py

+ 5 - 2
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

+ 2 - 2
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

+ 2 - 2
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

+ 25 - 10
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);
         });
     }

+ 7 - 2
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()