some cleanup

This commit is contained in:
blueloveTH 2024-01-19 13:16:36 +08:00
parent af58396b04
commit 77d727a7c2
3 changed files with 24 additions and 82 deletions

View File

@ -141,26 +141,6 @@ struct Mat3x3{
return ret; return ret;
} }
Mat3x3& operator+=(const Mat3x3& other){
for (int i=0; i<9; ++i) v[i] += other.v[i];
return *this;
}
Mat3x3& operator-=(const Mat3x3& other){
for (int i=0; i<9; ++i) v[i] -= other.v[i];
return *this;
}
Mat3x3& operator*=(float scalar){
for (int i=0; i<9; ++i) v[i] *= scalar;
return *this;
}
Mat3x3& operator/=(float scalar){
for (int i=0; i<9; ++i) v[i] /= scalar;
return *this;
}
void matmul(const Mat3x3& other, Mat3x3& out) const{ void matmul(const Mat3x3& other, Mat3x3& out) const{
out._11 = _11 * other._11 + _12 * other._21 + _13 * other._31; out._11 = _11 * other._11 + _12 * other._21 + _13 * other._31;
out._12 = _11 * other._12 + _12 * other._22 + _13 * other._32; out._12 = _11 * other._12 + _12 * other._22 + _13 * other._32;
@ -255,20 +235,6 @@ struct Mat3x3{
} }
}; };
struct PyVec2;
struct PyVec3;
struct PyVec4;
struct PyMat3x3;
PyObject* py_var(VM*, Vec2);
PyObject* py_var(VM*, const PyVec2&);
PyObject* py_var(VM*, Vec3);
PyObject* py_var(VM*, const PyVec3&);
PyObject* py_var(VM*, Vec4);
PyObject* py_var(VM*, const PyVec4&);
PyObject* py_var(VM*, const Mat3x3&);
PyObject* py_var(VM*, const PyMat3x3&);
struct PyVec2: Vec2 { struct PyVec2: Vec2 {
PY_CLASS(PyVec2, linalg, vec2) PY_CLASS(PyVec2, linalg, vec2)

View File

@ -4,7 +4,7 @@ namespace pkpy
{ {
struct PyDequeIter // Iterator for the deque type struct PyDequeIter // Iterator for the deque type
{ {
PY_CLASS(PyDequeIter, builtins, _deque_iterator) PY_CLASS(PyDequeIter, collections, _deque_iterator)
PyObject *ref; PyObject *ref;
bool is_reversed; bool is_reversed;
std::deque<PyObject *>::iterator begin, end, current; std::deque<PyObject *>::iterator begin, end, current;
@ -578,7 +578,7 @@ namespace pkpy
{ {
PyObject *mod = vm->new_module("collections"); PyObject *mod = vm->new_module("collections");
PyDeque::register_class(vm, mod); PyDeque::register_class(vm, mod);
PyDequeIter::register_class(vm, vm->builtins); PyDequeIter::register_class(vm, mod);
CodeObject_ code = vm->compile(kPythonLibs["collections"], "collections.py", EXEC_MODE); CodeObject_ code = vm->compile(kPythonLibs["collections"], "collections.py", EXEC_MODE);
vm->_exec(code, mod); vm->_exec(code, mod);
} }

View File

@ -29,18 +29,6 @@ namespace pkpy{
return VAR(self.name(other)); \ return VAR(self.name(other)); \
}); });
#define BIND_VEC_FIELD(D, name) \
vm->bind_property(type, #name, \
[](VM* vm, ArgsView args){ \
PyVec##D& self = _CAST(PyVec##D&, args[0]); \
return VAR(self.name); \
}, [](VM* vm, ArgsView args){ \
PyVec##D& self = _CAST(PyVec##D&, args[0]); \
self.name = CAST(f64, args[1]); \
return vm->None; \
});
// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector2.cs#L289 // 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, PyVec2& currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
{ {
@ -148,13 +136,14 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float
return VAR(self); return VAR(self);
}); });
PY_FIELD(PyVec2, "x", _, x)
PY_FIELD(PyVec2, "y", _, y)
BIND_VEC_VEC_OP(2, __add__, +) BIND_VEC_VEC_OP(2, __add__, +)
BIND_VEC_VEC_OP(2, __sub__, -) BIND_VEC_VEC_OP(2, __sub__, -)
BIND_VEC_FLOAT_OP(2, __mul__, *) BIND_VEC_FLOAT_OP(2, __mul__, *)
BIND_VEC_FLOAT_OP(2, __rmul__, *) BIND_VEC_FLOAT_OP(2, __rmul__, *)
BIND_VEC_FLOAT_OP(2, __truediv__, /) BIND_VEC_FLOAT_OP(2, __truediv__, /)
BIND_VEC_FIELD(2, x)
BIND_VEC_FIELD(2, y)
BIND_VEC_FUNCTION_1(2, dot) BIND_VEC_FUNCTION_1(2, dot)
BIND_VEC_FUNCTION_1(2, cross) BIND_VEC_FUNCTION_1(2, cross)
BIND_VEC_FUNCTION_1(2, assign) BIND_VEC_FUNCTION_1(2, assign)
@ -181,14 +170,15 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float
return VAR(ss.str()); return VAR(ss.str());
}); });
PY_FIELD(PyVec3, "x", _, x)
PY_FIELD(PyVec3, "y", _, y)
PY_FIELD(PyVec3, "z", _, z)
BIND_VEC_VEC_OP(3, __add__, +) BIND_VEC_VEC_OP(3, __add__, +)
BIND_VEC_VEC_OP(3, __sub__, -) BIND_VEC_VEC_OP(3, __sub__, -)
BIND_VEC_FLOAT_OP(3, __mul__, *) BIND_VEC_FLOAT_OP(3, __mul__, *)
BIND_VEC_FLOAT_OP(3, __rmul__, *) BIND_VEC_FLOAT_OP(3, __rmul__, *)
BIND_VEC_FLOAT_OP(3, __truediv__, /) BIND_VEC_FLOAT_OP(3, __truediv__, /)
BIND_VEC_FIELD(3, x)
BIND_VEC_FIELD(3, y)
BIND_VEC_FIELD(3, z)
BIND_VEC_FUNCTION_1(3, dot) BIND_VEC_FUNCTION_1(3, dot)
BIND_VEC_FUNCTION_1(3, cross) BIND_VEC_FUNCTION_1(3, cross)
BIND_VEC_FUNCTION_1(3, assign) BIND_VEC_FUNCTION_1(3, assign)
@ -216,15 +206,16 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float
return VAR(ss.str()); return VAR(ss.str());
}); });
PY_FIELD(PyVec4, "x", _, x)
PY_FIELD(PyVec4, "y", _, y)
PY_FIELD(PyVec4, "z", _, z)
PY_FIELD(PyVec4, "w", _, w)
BIND_VEC_VEC_OP(4, __add__, +) BIND_VEC_VEC_OP(4, __add__, +)
BIND_VEC_VEC_OP(4, __sub__, -) BIND_VEC_VEC_OP(4, __sub__, -)
BIND_VEC_FLOAT_OP(4, __mul__, *) BIND_VEC_FLOAT_OP(4, __mul__, *)
BIND_VEC_FLOAT_OP(4, __rmul__, *) BIND_VEC_FLOAT_OP(4, __rmul__, *)
BIND_VEC_FLOAT_OP(4, __truediv__, /) BIND_VEC_FLOAT_OP(4, __truediv__, /)
BIND_VEC_FIELD(4, x)
BIND_VEC_FIELD(4, y)
BIND_VEC_FIELD(4, z)
BIND_VEC_FIELD(4, w)
BIND_VEC_FUNCTION_1(4, dot) BIND_VEC_FUNCTION_1(4, dot)
BIND_VEC_FUNCTION_1(4, assign) BIND_VEC_FUNCTION_1(4, assign)
BIND_VEC_FUNCTION_0(4, length) BIND_VEC_FUNCTION_0(4, length)
@ -232,10 +223,8 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float
BIND_VEC_FUNCTION_0(4, normalize) BIND_VEC_FUNCTION_0(4, normalize)
} }
#undef BIND_VEC_ADDR
#undef BIND_VEC_VEC_OP #undef BIND_VEC_VEC_OP
#undef BIND_VEC_FLOAT_OP #undef BIND_VEC_FLOAT_OP
#undef BIND_VEC_FIELD
#undef BIND_VEC_FUNCTION_0 #undef BIND_VEC_FUNCTION_0
#undef BIND_VEC_FUNCTION_1 #undef BIND_VEC_FUNCTION_1
@ -299,7 +288,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){ vm->bind__setitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj, PyObject* index, PyObject* value){
PyMat3x3& self = _CAST(PyMat3x3&, obj); PyMat3x3& self = _CAST(PyMat3x3&, obj);
Tuple& t = CAST(Tuple&, index); const Tuple& t = CAST(Tuple&, index);
if(t.size() != 2){ if(t.size() != 2){
vm->TypeError("Mat3x3.__setitem__ takes a tuple of 2 integers"); vm->TypeError("Mat3x3.__setitem__ takes a tuple of 2 integers");
return; return;
@ -313,28 +302,15 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float
self.m[i][j] = CAST_F(value); self.m[i][j] = CAST_F(value);
}); });
#define PROPERTY_FIELD(field) \ PY_FIELD(PyMat3x3, "_11", _, _11)
vm->bind_property(type, #field ": float", \ PY_FIELD(PyMat3x3, "_12", _, _12)
[](VM* vm, ArgsView args){ \ PY_FIELD(PyMat3x3, "_13", _, _13)
PyMat3x3& self = _CAST(PyMat3x3&, args[0]); \ PY_FIELD(PyMat3x3, "_21", _, _21)
return VAR(self.field); \ PY_FIELD(PyMat3x3, "_22", _, _22)
}, [](VM* vm, ArgsView args){ \ PY_FIELD(PyMat3x3, "_23", _, _23)
PyMat3x3& self = _CAST(PyMat3x3&, args[0]); \ PY_FIELD(PyMat3x3, "_31", _, _31)
self.field = CAST(f64, args[1]); \ PY_FIELD(PyMat3x3, "_32", _, _32)
return vm->None; \ PY_FIELD(PyMat3x3, "_33", _, _33)
});
PROPERTY_FIELD(_11)
PROPERTY_FIELD(_12)
PROPERTY_FIELD(_13)
PROPERTY_FIELD(_21)
PROPERTY_FIELD(_22)
PROPERTY_FIELD(_23)
PROPERTY_FIELD(_31)
PROPERTY_FIELD(_32)
PROPERTY_FIELD(_33)
#undef PROPERTY_FIELD
vm->bind__add__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ vm->bind__add__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
PyMat3x3& self = _CAST(PyMat3x3&, _0); PyMat3x3& self = _CAST(PyMat3x3&, _0);