diff --git a/include/pocketpy/linalg.h b/include/pocketpy/linalg.h index a77b4bbe..e05ca423 100644 --- a/include/pocketpy/linalg.h +++ b/include/pocketpy/linalg.h @@ -7,6 +7,11 @@ namespace pkpy{ inline bool isclose(float a, float b){ return std::fabs(a - b) <= NumberTraits<4>::kEpsilon; } struct Vec2{ + PY_CLASS(Vec2, linalg, vec2) + + Vec2* _() { return this; } + static void _register(VM* vm, PyObject* mod, PyObject* type); + float x, y; Vec2() : x(0.0f), y(0.0f) {} Vec2(float x, float y) : x(x), y(y) {} @@ -30,6 +35,11 @@ struct Vec2{ }; struct Vec3{ + PY_CLASS(Vec3, linalg, vec3) + + Vec3* _() { return this; } + static void _register(VM* vm, PyObject* mod, PyObject* type); + float x, y, z; Vec3() : x(0.0f), y(0.0f), z(0.0f) {} Vec3(float x, float y, float z) : x(x), y(y), z(z) {} @@ -52,6 +62,11 @@ struct Vec3{ }; struct Vec4{ + PY_CLASS(Vec4, linalg, vec4) + + Vec4* _() { return this; } + static void _register(VM* vm, PyObject* mod, PyObject* type); + float x, y, z, w; Vec4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {} Vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {} @@ -72,7 +87,12 @@ struct Vec4{ NoReturn copy_(const Vec4& v) { x = v.x; y = v.y; z = v.z; w = v.w; return {}; } }; -struct Mat3x3{ +struct Mat3x3{ + PY_CLASS(Mat3x3, linalg, mat3x3) + + Mat3x3* _(){ return this; } + static void _register(VM* vm, PyObject* mod, PyObject* type); + union { struct { float _11, _12, _13; @@ -113,74 +133,18 @@ struct Mat3x3{ Vec2 _s() const; }; -struct PyVec2: Vec2 { - PY_CLASS(PyVec2, linalg, vec2) - PyVec2() : Vec2() {} - PyVec2(const Vec2& v) : Vec2(v) {} - Vec2* _() { return this; } - - static void _register(VM* vm, PyObject* mod, PyObject* type); -}; - -struct PyVec3: Vec3 { - PY_CLASS(PyVec3, linalg, vec3) - - PyVec3() : Vec3() {} - PyVec3(const Vec3& v) : Vec3(v) {} - Vec3* _() { return this; } - - static void _register(VM* vm, PyObject* mod, PyObject* type); -}; - -struct PyVec4: Vec4{ - PY_CLASS(PyVec4, linalg, vec4) - - PyVec4(): Vec4(){} - PyVec4(const Vec4& v): Vec4(v){} - Vec4* _(){ return this; } - - static void _register(VM* vm, PyObject* mod, PyObject* type); -}; - -struct PyMat3x3: Mat3x3{ - PY_CLASS(PyMat3x3, linalg, mat3x3) - - PyMat3x3(): Mat3x3(){} - PyMat3x3(const Mat3x3& other): Mat3x3(other){} - Mat3x3* _(){ return this; } - - static void _register(VM* vm, PyObject* mod, PyObject* type); -}; - -inline PyObject* py_var(VM* vm, Vec2 obj){ return VAR_T(PyVec2, obj); } -inline PyObject* py_var(VM* vm, const PyVec2& obj){ return VAR_T(PyVec2, obj);} - -inline PyObject* py_var(VM* vm, Vec3 obj){ return VAR_T(PyVec3, obj); } -inline PyObject* py_var(VM* vm, const PyVec3& obj){ return VAR_T(PyVec3, obj);} - -inline PyObject* py_var(VM* vm, Vec4 obj){ return VAR_T(PyVec4, obj); } -inline PyObject* py_var(VM* vm, const PyVec4& obj){ return VAR_T(PyVec4, obj);} - -inline PyObject* py_var(VM* vm, const Mat3x3& obj){ return VAR_T(PyMat3x3, obj); } -inline PyObject* py_var(VM* vm, const PyMat3x3& obj){ return VAR_T(PyMat3x3, obj); } - -template<> inline Vec2 py_cast(VM* vm, PyObject* obj) { return CAST(PyVec2&, obj); } -template<> inline Vec3 py_cast(VM* vm, PyObject* obj) { return CAST(PyVec3&, obj); } -template<> inline Vec4 py_cast(VM* vm, PyObject* obj) { return CAST(PyVec4&, obj); } -template<> inline Mat3x3 py_cast(VM* vm, PyObject* obj) { return CAST(PyMat3x3&, obj); } - -template<> inline Vec2 _py_cast(VM* vm, PyObject* obj) { return _CAST(PyVec2&, obj); } -template<> inline Vec3 _py_cast(VM* vm, PyObject* obj) { return _CAST(PyVec3&, obj); } -template<> inline Vec4 _py_cast(VM* vm, PyObject* obj) { return _CAST(PyVec4&, obj); } -template<> inline Mat3x3 _py_cast(VM* vm, PyObject* obj) { return _CAST(PyMat3x3&, obj); } +inline PyObject* py_var(VM* vm, Vec2 obj){ return VAR_T(Vec2, obj); } +inline PyObject* py_var(VM* vm, Vec3 obj){ return VAR_T(Vec3, obj); } +inline PyObject* py_var(VM* vm, Vec4 obj){ return VAR_T(Vec4, obj); } +inline PyObject* py_var(VM* vm, const Mat3x3& obj){ return VAR_T(Mat3x3, obj); } void add_module_linalg(VM* vm); -static_assert(sizeof(Py_) <= 64); -static_assert(std::is_trivially_copyable::value); -static_assert(std::is_trivially_copyable::value); -static_assert(std::is_trivially_copyable::value); -static_assert(std::is_trivially_copyable::value); +static_assert(sizeof(Py_) <= 64); +static_assert(std::is_trivially_copyable::value); +static_assert(std::is_trivially_copyable::value); +static_assert(std::is_trivially_copyable::value); +static_assert(std::is_trivially_copyable::value); } // namespace pkpy \ No newline at end of file diff --git a/src/linalg.cpp b/src/linalg.cpp index 0b96f3ec..e2ffd4c5 100644 --- a/src/linalg.cpp +++ b/src/linalg.cpp @@ -4,54 +4,54 @@ namespace pkpy{ #define BIND_VEC_VEC_OP(D, name, op) \ vm->bind##name(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ \ - PyVec##D& self = _CAST(PyVec##D&, _0); \ - PyVec##D& other = CAST(PyVec##D&, _1); \ + Vec##D& self = _CAST(Vec##D&, _0); \ + Vec##D& other = CAST(Vec##D&, _1); \ return VAR(self op other); \ }); #define BIND_VEC_FLOAT_OP(D, name, op) \ vm->bind##name(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ \ - PyVec##D& self = _CAST(PyVec##D&, _0); \ + Vec##D& self = _CAST(Vec##D&, _0); \ f64 other = CAST(f64, _1); \ return VAR(self op other); \ }); #define BIND_VEC_FUNCTION_0(D, name) \ vm->bind_method<0>(type, #name, [](VM* vm, ArgsView args){ \ - PyVec##D& self = _CAST(PyVec##D&, args[0]); \ + Vec##D& self = _CAST(Vec##D&, args[0]); \ return VAR(self.name()); \ }); #define BIND_VEC_FUNCTION_1(D, name) \ vm->bind_method<1>(type, #name, [](VM* vm, ArgsView args){ \ - PyVec##D& self = _CAST(PyVec##D&, args[0]); \ - PyVec##D& other = CAST(PyVec##D&, args[1]); \ + Vec##D& self = _CAST(Vec##D&, args[0]); \ + Vec##D& other = CAST(Vec##D&, args[1]); \ return VAR(self.name(other)); \ }); #define BIND_VEC_MUL_OP(D) \ vm->bind__mul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ \ - PyVec##D& self = _CAST(PyVec##D&, _0); \ - if(is_non_tagged_type(_1, PyVec##D::_type(vm))){ \ - PyVec##D& other = _CAST(PyVec##D&, _1); \ + Vec##D& self = _CAST(Vec##D&, _0); \ + if(is_non_tagged_type(_1, Vec##D::_type(vm))){ \ + Vec##D& other = _CAST(Vec##D&, _1); \ return VAR(self * other); \ } \ f64 other = CAST(f64, _1); \ return VAR(self * other); \ }); \ vm->bind_method<1>(type, "__rmul__", [](VM* vm, ArgsView args){ \ - PyVec##D& self = _CAST(PyVec##D&, args[0]); \ + Vec##D& self = _CAST(Vec##D&, args[0]); \ f64 other = CAST(f64, args[1]); \ return VAR(self * other); \ }); \ vm->bind__truediv__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ \ - PyVec##D& self = _CAST(PyVec##D&, _0); \ + Vec##D& self = _CAST(Vec##D&, _0); \ f64 other = CAST(f64, _1); \ return VAR(self / other); \ }); // https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector2.cs#L289 -static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float smoothTime, float maxSpeed, float deltaTime) +static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float smoothTime, float maxSpeed, float deltaTime) { // Based on Game Programming Gems 4 Chapter 1.10 smoothTime = std::max(0.0001F, smoothTime); @@ -105,20 +105,20 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float return Vec2(output_x, output_y); } - void PyVec2::_register(VM* vm, PyObject* mod, PyObject* type){ - PY_STRUCT_LIKE(PyVec2) + void Vec2::_register(VM* vm, PyObject* mod, PyObject* type){ + PY_STRUCT_LIKE(Vec2) vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){ float x = CAST_F(args[1]); float y = CAST_F(args[2]); - return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), Vec2(x, y)); + return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), Vec2(x, y)); }); // @staticmethod vm->bind(type, "smooth_damp(current: vec2, target: vec2, current_velocity_: vec2, smooth_time: float, max_speed: float, delta_time: float) -> vec2", [](VM* vm, ArgsView args){ Vec2 current = CAST(Vec2, args[0]); Vec2 target = CAST(Vec2, args[1]); - PyVec2& current_velocity_ = CAST(PyVec2&, args[2]); + Vec2& current_velocity_ = CAST(Vec2&, args[2]); float smooth_time = CAST_F(args[3]); float max_speed = CAST_F(args[4]); float delta_time = CAST_F(args[5]); @@ -128,8 +128,8 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float // @staticmethod vm->bind(type, "angle(__from: vec2, __to: vec2) -> float", [](VM* vm, ArgsView args){ - PyVec2 __from = CAST(PyVec2, args[0]); - PyVec2 __to = CAST(PyVec2, args[1]); + Vec2 __from = CAST(Vec2, args[0]); + Vec2 __to = CAST(Vec2, args[1]); float val = atan2f(__to.y, __to.x) - atan2f(__from.y, __from.x); const float PI = 3.1415926535897932384f; if(val > PI) val -= 2*PI; @@ -138,7 +138,7 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float }, {}, BindType::STATICMETHOD); vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ - Vec2 self = _CAST(PyVec2&, obj); + Vec2 self = _CAST(Vec2&, obj); SStream ss; ss.setprecision(3); ss << "vec2(" << self.x << ", " << self.y << ")"; @@ -146,20 +146,20 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float }); vm->bind_method<1>(type, "rotate", [](VM* vm, ArgsView args){ - Vec2 self = _CAST(PyVec2&, args[0]); + Vec2 self = _CAST(Vec2&, args[0]); float radian = CAST(f64, args[1]); - return VAR_T(PyVec2, self.rotate(radian)); + return VAR_T(Vec2, self.rotate(radian)); }); vm->bind_method<1>(type, "rotate_", [](VM* vm, ArgsView args){ - PyVec2& self = _CAST(PyVec2&, args[0]); + Vec2& self = _CAST(Vec2&, args[0]); float radian = CAST(f64, args[1]); self = self.rotate(radian); return vm->None; }); - PY_FIELD(PyVec2, "x", _, x) - PY_FIELD(PyVec2, "y", _, y) + PY_FIELD(Vec2, "x", _, x) + PY_FIELD(Vec2, "y", _, y) BIND_VEC_VEC_OP(2, __add__, +) BIND_VEC_VEC_OP(2, __sub__, -) @@ -174,27 +174,27 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float BIND_VEC_FUNCTION_0(2, normalize_) } - void PyVec3::_register(VM* vm, PyObject* mod, PyObject* type){ - PY_STRUCT_LIKE(PyVec3) + void Vec3::_register(VM* vm, PyObject* mod, PyObject* type){ + PY_STRUCT_LIKE(Vec3) vm->bind_constructor<4>(type, [](VM* vm, ArgsView args){ float x = CAST_F(args[1]); float y = CAST_F(args[2]); float z = CAST_F(args[3]); - return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), Vec3(x, y, z)); + return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), Vec3(x, y, z)); }); vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ - Vec3 self = _CAST(PyVec3&, obj); + Vec3 self = _CAST(Vec3&, obj); SStream ss; ss.setprecision(3); ss << "vec3(" << self.x << ", " << self.y << ", " << self.z << ")"; return VAR(ss.str()); }); - PY_FIELD(PyVec3, "x", _, x) - PY_FIELD(PyVec3, "y", _, y) - PY_FIELD(PyVec3, "z", _, z) + PY_FIELD(Vec3, "x", _, x) + PY_FIELD(Vec3, "y", _, y) + PY_FIELD(Vec3, "z", _, z) BIND_VEC_VEC_OP(3, __add__, +) BIND_VEC_VEC_OP(3, __sub__, -) @@ -208,29 +208,29 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float BIND_VEC_FUNCTION_0(3, normalize_) } - void PyVec4::_register(VM* vm, PyObject* mod, PyObject* type){ - PY_STRUCT_LIKE(PyVec4) + void Vec4::_register(VM* vm, PyObject* mod, PyObject* type){ + PY_STRUCT_LIKE(Vec4) vm->bind_constructor<1+4>(type, [](VM* vm, ArgsView args){ float x = CAST_F(args[1]); float y = CAST_F(args[2]); float z = CAST_F(args[3]); float w = CAST_F(args[4]); - return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), Vec4(x, y, z, w)); + return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), Vec4(x, y, z, w)); }); vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ - Vec4 self = _CAST(PyVec4&, obj); + Vec4 self = _CAST(Vec4&, obj); SStream ss; ss.setprecision(3); ss << "vec4(" << self.x << ", " << self.y << ", " << self.z << ", " << self.w << ")"; return VAR(ss.str()); }); - PY_FIELD(PyVec4, "x", _, x) - PY_FIELD(PyVec4, "y", _, y) - PY_FIELD(PyVec4, "z", _, z) - PY_FIELD(PyVec4, "w", _, w) + PY_FIELD(Vec4, "x", _, x) + PY_FIELD(Vec4, "y", _, y) + PY_FIELD(Vec4, "z", _, z) + PY_FIELD(Vec4, "w", _, w) BIND_VEC_VEC_OP(4, __add__, +) BIND_VEC_VEC_OP(4, __sub__, -) @@ -248,36 +248,36 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float #undef BIND_VEC_FUNCTION_0 #undef BIND_VEC_FUNCTION_1 - void PyMat3x3::_register(VM* vm, PyObject* mod, PyObject* type){ - PY_STRUCT_LIKE(PyMat3x3) + void Mat3x3::_register(VM* vm, PyObject* mod, PyObject* type){ + PY_STRUCT_LIKE(Mat3x3) vm->bind_constructor<-1>(type, [](VM* vm, ArgsView args){ - if(args.size() == 1+0) return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), Mat3x3::zeros()); + if(args.size() == 1+0) return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), Mat3x3::zeros()); if(args.size() == 1+1){ const List& list = CAST(List&, args[1]); if(list.size() != 9) vm->TypeError("Mat3x3.__new__ takes a list of 9 floats"); Mat3x3 mat; for(int i=0; i<9; i++) mat.v[i] = CAST_F(list[i]); - return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), mat); + return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), mat); } if(args.size() == 1+9){ Mat3x3 mat; for(int i=0; i<9; i++) mat.v[i] = CAST_F(args[1+i]); - return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), mat); + return vm->heap.gcnew(PK_OBJ_GET(Type, args[0]), mat); } vm->TypeError(_S("Mat3x3.__new__ takes 0 or 1 or 9 arguments, got ", args.size()-1)); return vm->None; }); vm->bind_method<1>(type, "copy_", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - const PyMat3x3& other = CAST(PyMat3x3&, args[1]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); + const Mat3x3& other = CAST(Mat3x3&, args[1]); self = other; return vm->None; }); vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ - const PyMat3x3& self = _CAST(PyMat3x3&, obj); + const Mat3x3& self = _CAST(Mat3x3&, obj); SStream ss; ss.setprecision(3); ss << "mat3x3([" << self._11 << ", " << self._12 << ", " << self._13 << ",\n"; @@ -287,7 +287,7 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float }); vm->bind__getitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj, PyObject* index){ - PyMat3x3& self = _CAST(PyMat3x3&, obj); + Mat3x3& self = _CAST(Mat3x3&, obj); Tuple& t = CAST(Tuple&, index); if(t.size() != 2){ vm->TypeError("Mat3x3.__getitem__ takes a tuple of 2 integers"); @@ -301,7 +301,7 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float }); vm->bind__setitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj, PyObject* index, PyObject* value){ - PyMat3x3& self = _CAST(PyMat3x3&, obj); + Mat3x3& self = _CAST(Mat3x3&, obj); const Tuple& t = CAST(Tuple&, index); if(t.size() != 2){ vm->TypeError("Mat3x3.__setitem__ takes a tuple of 2 integers"); @@ -314,99 +314,99 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float self.m[i][j] = CAST_F(value); }); - PY_FIELD(PyMat3x3, "_11", _, _11) - PY_FIELD(PyMat3x3, "_12", _, _12) - PY_FIELD(PyMat3x3, "_13", _, _13) - PY_FIELD(PyMat3x3, "_21", _, _21) - PY_FIELD(PyMat3x3, "_22", _, _22) - PY_FIELD(PyMat3x3, "_23", _, _23) - PY_FIELD(PyMat3x3, "_31", _, _31) - PY_FIELD(PyMat3x3, "_32", _, _32) - PY_FIELD(PyMat3x3, "_33", _, _33) + PY_FIELD(Mat3x3, "_11", _, _11) + PY_FIELD(Mat3x3, "_12", _, _12) + PY_FIELD(Mat3x3, "_13", _, _13) + PY_FIELD(Mat3x3, "_21", _, _21) + PY_FIELD(Mat3x3, "_22", _, _22) + PY_FIELD(Mat3x3, "_23", _, _23) + PY_FIELD(Mat3x3, "_31", _, _31) + PY_FIELD(Mat3x3, "_32", _, _32) + PY_FIELD(Mat3x3, "_33", _, _33) vm->bind__add__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ - PyMat3x3& self = _CAST(PyMat3x3&, _0); - PyMat3x3& other = CAST(PyMat3x3&, _1); - return VAR_T(PyMat3x3, self + other); + Mat3x3& self = _CAST(Mat3x3&, _0); + Mat3x3& other = CAST(Mat3x3&, _1); + return VAR_T(Mat3x3, self + other); }); vm->bind__sub__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ - PyMat3x3& self = _CAST(PyMat3x3&, _0); - PyMat3x3& other = CAST(PyMat3x3&, _1); - return VAR_T(PyMat3x3, self - other); + Mat3x3& self = _CAST(Mat3x3&, _0); + Mat3x3& other = CAST(Mat3x3&, _1); + return VAR_T(Mat3x3, self - other); }); vm->bind__mul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ - PyMat3x3& self = _CAST(PyMat3x3&, _0); + Mat3x3& self = _CAST(Mat3x3&, _0); f64 other = CAST_F(_1); - return VAR_T(PyMat3x3, self * other); + return VAR_T(Mat3x3, self * other); }); vm->bind_method<1>(type, "__rmul__", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); f64 other = CAST_F(args[1]); - return VAR_T(PyMat3x3, self * other); + return VAR_T(Mat3x3, self * other); }); vm->bind__truediv__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ - PyMat3x3& self = _CAST(PyMat3x3&, _0); + Mat3x3& self = _CAST(Mat3x3&, _0); f64 other = CAST_F(_1); - return VAR_T(PyMat3x3, self / other); + return VAR_T(Mat3x3, self / other); }); vm->bind__matmul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ - PyMat3x3& self = _CAST(PyMat3x3&, _0); - if(is_non_tagged_type(_1, PyMat3x3::_type(vm))){ - const PyMat3x3& other = _CAST(PyMat3x3&, _1); - return VAR_T(PyMat3x3, self.matmul(other)); + Mat3x3& self = _CAST(Mat3x3&, _0); + if(is_non_tagged_type(_1, Mat3x3::_type(vm))){ + const Mat3x3& other = _CAST(Mat3x3&, _1); + return VAR_T(Mat3x3, self.matmul(other)); } - if(is_non_tagged_type(_1, PyVec3::_type(vm))){ - const PyVec3& other = _CAST(PyVec3&, _1); - return VAR_T(PyVec3, self.matmul(other)); + if(is_non_tagged_type(_1, Vec3::_type(vm))){ + const Vec3& other = _CAST(Vec3&, _1); + return VAR_T(Vec3, self.matmul(other)); } return vm->NotImplemented; }); vm->bind(type, "matmul(self, other: mat3x3, out: mat3x3 = None)", [](VM* vm, ArgsView args){ - const PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - const PyMat3x3& other = CAST(PyMat3x3&, args[1]); + const Mat3x3& self = _CAST(Mat3x3&, args[0]); + const Mat3x3& other = CAST(Mat3x3&, args[1]); if(args[2] == vm->None){ - return VAR_T(PyMat3x3, self.matmul(other)); + return VAR_T(Mat3x3, self.matmul(other)); }else{ - PyMat3x3& out = CAST(PyMat3x3&, args[2]); + Mat3x3& out = CAST(Mat3x3&, args[2]); out = self.matmul(other); return vm->None; } }); vm->bind_method<0>(type, "determinant", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); return VAR(self.determinant()); }); vm->bind_method<0>(type, "transpose", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - return VAR_T(PyMat3x3, self.transpose()); + Mat3x3& self = _CAST(Mat3x3&, args[0]); + return VAR_T(Mat3x3, self.transpose()); }); vm->bind__invert__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ - PyMat3x3& self = _CAST(PyMat3x3&, obj); + Mat3x3& self = _CAST(Mat3x3&, obj); Mat3x3 ret; bool ok = self.inverse(ret); if(!ok) vm->ValueError("matrix is not invertible"); - return VAR_T(PyMat3x3, ret); + return VAR_T(Mat3x3, ret); }); vm->bind_method<0>(type, "invert", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); Mat3x3 ret; bool ok = self.inverse(ret); if(!ok) vm->ValueError("matrix is not invertible"); - return VAR_T(PyMat3x3, ret); + return VAR_T(Mat3x3, ret); }); vm->bind_method<0>(type, "invert_", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); Mat3x3 ret; bool ok = self.inverse(ret); if(!ok) vm->ValueError("matrix is not invertible"); @@ -415,24 +415,24 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float }); vm->bind_method<0>(type, "transpose_", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); self = self.transpose(); return vm->None; }); // @staticmethod vm->bind(type, "zeros()", [](VM* vm, ArgsView args){ - return VAR_T(PyMat3x3, Mat3x3::zeros()); + return VAR_T(Mat3x3, Mat3x3::zeros()); }, {}, BindType::STATICMETHOD); // @staticmethod vm->bind(type, "ones()", [](VM* vm, ArgsView args){ - return VAR_T(PyMat3x3, Mat3x3::ones()); + return VAR_T(Mat3x3, Mat3x3::ones()); }, {}, BindType::STATICMETHOD); // @staticmethod vm->bind(type, "identity()", [](VM* vm, ArgsView args){ - return VAR_T(PyMat3x3, Mat3x3::identity()); + return VAR_T(Mat3x3, Mat3x3::identity()); }, {}, BindType::STATICMETHOD); /*************** affine transformations ***************/ @@ -441,11 +441,11 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float Vec2 t = CAST(Vec2, args[0]); f64 r = CAST_F(args[1]); Vec2 s = CAST(Vec2, args[2]); - return VAR_T(PyMat3x3, Mat3x3::trs(t, r, s)); + return VAR_T(Mat3x3, Mat3x3::trs(t, r, s)); }, {}, BindType::STATICMETHOD); vm->bind(type, "copy_trs_(self, t: vec2, r: float, s: vec2)", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); Vec2 t = CAST(Vec2, args[1]); f64 r = CAST_F(args[2]); Vec2 s = CAST(Vec2, args[3]); @@ -454,68 +454,68 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float }); vm->bind(type, "copy_t_(self, t: vec2)", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); Vec2 t = CAST(Vec2, args[1]); self = Mat3x3::trs(t, self._r(), self._s()); return vm->None; }); vm->bind(type, "copy_r_(self, r: float)", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); f64 r = CAST_F(args[1]); self = Mat3x3::trs(self._t(), r, self._s()); return vm->None; }); vm->bind(type, "copy_s_(self, s: vec2)", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); Vec2 s = CAST(Vec2, args[1]); self = Mat3x3::trs(self._t(), self._r(), s); return vm->None; }); vm->bind_method<0>(type, "is_affine", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); return VAR(self.is_affine()); }); vm->bind_method<0>(type, "_t", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - return VAR_T(PyVec2, self._t()); + Mat3x3& self = _CAST(Mat3x3&, args[0]); + return VAR_T(Vec2, self._t()); }); vm->bind_method<0>(type, "_r", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + Mat3x3& self = _CAST(Mat3x3&, args[0]); return VAR(self._r()); }); vm->bind_method<0>(type, "_s", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - return VAR_T(PyVec2, self._s()); + Mat3x3& self = _CAST(Mat3x3&, args[0]); + return VAR_T(Vec2, self._s()); }); vm->bind_method<1>(type, "transform_point", [](VM* vm, ArgsView args){ - const PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + 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); - return VAR_T(PyVec2, res); + return VAR_T(Vec2, res); }); vm->bind_method<1>(type, "transform_vector", [](VM* vm, ArgsView args){ - const PyMat3x3& self = _CAST(PyMat3x3&, args[0]); + 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); - return VAR_T(PyVec2, res); + return VAR_T(Vec2, res); }); } void add_module_linalg(VM* vm){ PyObject* linalg = vm->new_module("linalg"); - PyVec2::register_class(vm, linalg); - PyVec3::register_class(vm, linalg); - PyVec4::register_class(vm, linalg); - PyMat3x3::register_class(vm, linalg); + Vec2::register_class(vm, linalg); + Vec3::register_class(vm, linalg); + Vec4::register_class(vm, linalg); + Mat3x3::register_class(vm, linalg); PyObject* float_p = vm->_modules["c"]->attr("float_p"); linalg->attr().set("vec2_p", float_p);