From 10f886bb9b6e155e222fcd69a852d0bf20ab4d95 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 5 May 2024 00:18:57 +0800 Subject: [PATCH] add constants --- include/typings/linalg.pyi | 9 +++++++++ src/linalg.cpp | 15 ++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/include/typings/linalg.pyi b/include/typings/linalg.pyi index 71af3a89..d529e870 100644 --- a/include/typings/linalg.pyi +++ b/include/typings/linalg.pyi @@ -5,6 +5,9 @@ class vec2(_StructLike['vec2']): x: float y: float + ZERO: 'vec2' = ... + ONE: 'vec2' = ... + def __init__(self, x: float, y: float) -> None: ... def __add__(self, other: vec2) -> vec2: ... def __sub__(self, other: vec2) -> vec2: ... @@ -47,6 +50,9 @@ class vec3(_StructLike['vec3']): y: float z: float + ZERO: 'vec3' = ... + ONE: 'vec3' = ... + def __init__(self, x: float, y: float, z: float) -> None: ... def __add__(self, other: vec3) -> vec3: ... def __sub__(self, other: vec3) -> vec3: ... @@ -74,6 +80,9 @@ class vec4(_StructLike['vec4']): z: float w: float + ZERO: 'vec4' = ... + ONE: '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: ... diff --git a/src/linalg.cpp b/src/linalg.cpp index f3d57dc4..34ce0703 100644 --- a/src/linalg.cpp +++ b/src/linalg.cpp @@ -117,6 +117,9 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s void Vec2::_register(VM* vm, PyObject* mod, PyObject* type){ PY_STRUCT_LIKE(Vec2) + type->attr().set("ZERO", vm->new_user_object(0, 0)); + type->attr().set("ONE", vm->new_user_object(1, 1)); + vm->bind_func(type, __new__, 3, [](VM* vm, ArgsView args){ float x = CAST_F(args[1]); float y = CAST_F(args[2]); @@ -187,6 +190,9 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s void Vec3::_register(VM* vm, PyObject* mod, PyObject* type){ PY_STRUCT_LIKE(Vec3) + type->attr().set("ZERO", vm->new_user_object(0, 0, 0)); + type->attr().set("ONE", vm->new_user_object(1, 1, 1)); + vm->bind_func(type, __new__, 4, [](VM* vm, ArgsView args){ float x = CAST_F(args[1]); float y = CAST_F(args[2]); @@ -222,6 +228,9 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s void Vec4::_register(VM* vm, PyObject* mod, PyObject* type){ PY_STRUCT_LIKE(Vec4) + type->attr().set("ZERO", vm->new_user_object(0, 0, 0, 0)); + type->attr().set("ONE", vm->new_user_object(1, 1, 1, 1)); + vm->bind_func(type, __new__, 5, [](VM* vm, ArgsView args){ float x = CAST_F(args[1]); float y = CAST_F(args[2]); @@ -431,17 +440,17 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s }); // @staticmethod - vm->bind(type, "zeros()", [](VM* vm, ArgsView args){ + vm->bind_func(type, "zeros", 0, [](VM* vm, ArgsView args){ return vm->new_user_object(Mat3x3::zeros()); }, {}, BindType::STATICMETHOD); // @staticmethod - vm->bind(type, "ones()", [](VM* vm, ArgsView args){ + vm->bind_func(type, "ones", 0, [](VM* vm, ArgsView args){ return vm->new_user_object(Mat3x3::ones()); }, {}, BindType::STATICMETHOD); // @staticmethod - vm->bind(type, "identity()", [](VM* vm, ArgsView args){ + vm->bind_func(type, "identity", 0, [](VM* vm, ArgsView args){ return vm->new_user_object(Mat3x3::identity()); }, {}, BindType::STATICMETHOD);