Răsfoiți Sursa

add `vec3.with_xy`

blueloveTH 1 an în urmă
părinte
comite
4aab227b53
3 a modificat fișierele cu 16 adăugiri și 1 ștergeri
  1. 1 0
      include/typings/linalg.pyi
  2. 12 0
      src/modules/linalg.c
  3. 3 1
      tests/80_linalg.py

+ 1 - 0
include/typings/linalg.pyi

@@ -131,6 +131,7 @@ class vec3(_vecD['vec3']):
     def with_x(self, x: float) -> vec3: ...
     def with_y(self, y: float) -> vec3: ...
     def with_z(self, z: float) -> vec3: ...
+    def with_xy(self, xy: vec2) -> vec3: ...
 
     def __init__(self, x: float, y: float, z: float) -> None: ...
 

+ 12 - 0
src/modules/linalg.c

@@ -777,6 +777,17 @@ static bool vec3__xy(int argc, py_Ref argv) {
     return true;
 }
 
+static bool vec3__with_xy(int argc, py_Ref argv) {
+    PY_CHECK_ARGC(2);
+    PY_CHECK_ARG_TYPE(1, tp_vec2);
+    c11_vec2 xy = py_tovec2(&argv[1]);
+    c11_vec3 res = {
+        {xy.x, xy.y, py_tovec3(argv).z}
+    };
+    py_newvec3(py_retval(), res);
+    return true;
+}
+
 void pk__add_module_linalg() {
     py_Ref mod = py_newmodule("linalg");
 
@@ -898,6 +909,7 @@ void pk__add_module_linalg() {
     py_bindmethod(vec3, "with_x", vec3__with_x);
     py_bindmethod(vec3, "with_y", vec3__with_y);
     py_bindmethod(vec3, "with_z", vec3__with_z);
+    py_bindmethod(vec3, "with_xy", vec3__with_xy);
 
     py_newvec3(py_emplacedict(py_tpobject(vec3), py_name("ZERO")),
                (c11_vec3){

+ 3 - 1
tests/80_linalg.py

@@ -368,4 +368,6 @@ assert vec3(1, 2, 3).xy == vec2(1, 2)
 # test vec3.ONE
 assert vec3.ONE == vec3(1, 1, 1)
 # test vec3.ZERO
-assert vec3.ZERO == vec3(0, 0, 0)
+assert vec3.ZERO == vec3(0, 0, 0)
+# test vec3.with_xy
+assert vec3(1, 2, 3).with_xy(vec2(4, 5)) == vec3(4, 5, 3)