Ver Fonte

allow `vec2(vec2i)`

blueloveTH há 1 ano atrás
pai
commit
05e1de4101
3 ficheiros alterados com 21 adições e 3 exclusões
  1. 6 0
      include/typings/linalg.pyi
  2. 10 3
      src/modules/linalg.c
  3. 5 0
      tests/80_linalg.py

+ 6 - 0
include/typings/linalg.pyi

@@ -48,7 +48,10 @@ class vec2(_vecF['vec2']):
     def with_y(self, y: float) -> vec2: ...
     def with_y(self, y: float) -> vec2: ...
     def with_z(self, z: float) -> vec3: ...
     def with_z(self, z: float) -> vec3: ...
 
 
+    @overload
     def __init__(self, x: float, y: float) -> None: ...
     def __init__(self, x: float, y: float) -> None: ...
+    @overload
+    def __init__(self, xy: vec2i) -> None: ...
 
 
     def rotate(self, radians: float) -> vec2: ...
     def rotate(self, radians: float) -> vec2: ...
 
 
@@ -159,7 +162,10 @@ class vec3(_vecF['vec3']):
     def with_z(self, z: float) -> vec3: ...
     def with_z(self, z: float) -> vec3: ...
     def with_xy(self, xy: vec2) -> vec3: ...
     def with_xy(self, xy: vec2) -> vec3: ...
 
 
+    @overload
     def __init__(self, x: float, y: float, z: float) -> None: ...
     def __init__(self, x: float, y: float, z: float) -> None: ...
+    @overload
+    def __init__(self, xyz: vec3i) -> None: ...
 
 
 
 
 
 

+ 10 - 3
src/modules/linalg.c

@@ -110,10 +110,17 @@ static py_Ref _const(py_Type type, const char* name) {
 
 
 #define DEF_VECTOR_OPS(D)                                                                          \
 #define DEF_VECTOR_OPS(D)                                                                          \
     static bool vec##D##__new__(int argc, py_Ref argv) {                                           \
     static bool vec##D##__new__(int argc, py_Ref argv) {                                           \
-        PY_CHECK_ARGC(D + 1);                                                                      \
         c11_vec##D res;                                                                            \
         c11_vec##D res;                                                                            \
-        for(int i = 0; i < D; i++) {                                                               \
-            if(!py_castfloat32(&argv[i + 1], &res.data[i])) return false;                          \
+        if(argc == 2) {                                                                            \
+            PY_CHECK_ARG_TYPE(1, tp_vec##D##i);                                                    \
+            c11_vec##D##i v = py_tovec##D##i(&argv[1]);                                            \
+            for(int i = 0; i < D; i++)                                                             \
+                res.data[i] = v.data[i];                                                           \
+        } else {                                                                                   \
+            PY_CHECK_ARGC(D + 1);                                                                  \
+            for(int i = 0; i < D; i++) {                                                           \
+                if(!py_castfloat32(&argv[i + 1], &res.data[i])) return false;                      \
+            }                                                                                      \
         }                                                                                          \
         }                                                                                          \
         py_newvec##D(py_retval(), res);                                                            \
         py_newvec##D(py_retval(), res);                                                            \
         return true;                                                                               \
         return true;                                                                               \

+ 5 - 0
tests/80_linalg.py

@@ -400,3 +400,8 @@ a[vec2i(1, 2)] = 1
 assert a[vec2i(1, 2)] == 1
 assert a[vec2i(1, 2)] == 1
 a[vec3i(1, 2, 3)] = 2
 a[vec3i(1, 2, 3)] = 2
 assert a[vec3i(1, 2, 3)] == 2
 assert a[vec3i(1, 2, 3)] == 2
+
+assert vec2(vec2i.LEFT) == vec2(-1, 0)
+assert vec2(vec2i.RIGHT) == vec2(1, 0)
+assert vec3(vec3i.ONE) == vec3(1, 1, 1)
+assert vec3(vec3i.ZERO) == vec3(0, 0, 0)