add vec3.with_xy

This commit is contained in:
blueloveTH 2024-09-16 02:15:05 +08:00
parent 7a7f91f1cf
commit 4aab227b53
3 changed files with 16 additions and 1 deletions

View File

@ -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: ...

View File

@ -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){

View File

@ -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)