| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #pragma once
- #include "bindings.h"
- namespace pkpy{
- inline bool isclose(float a, float b){ return std::fabs(a - b) <= NumberTraits<4>::kEpsilon; }
- struct Vec2{
- float x, y;
- Vec2() : x(0.0f), y(0.0f) {}
- Vec2(float x, float y) : x(x), y(y) {}
- Vec2(const Vec2& v) = default;
- Vec2 operator+(const Vec2& v) const { return Vec2(x + v.x, y + v.y); }
- Vec2& operator+=(const Vec2& v) { x += v.x; y += v.y; return *this; }
- Vec2 operator-(const Vec2& v) const { return Vec2(x - v.x, y - v.y); }
- Vec2& operator-=(const Vec2& v) { x -= v.x; y -= v.y; return *this; }
- Vec2 operator*(float s) const { return Vec2(x * s, y * s); }
- Vec2& operator*=(float s) { x *= s; y *= s; return *this; }
- Vec2 operator/(float s) const { return Vec2(x / s, y / s); }
- Vec2& operator/=(float s) { x /= s; y /= s; return *this; }
- Vec2 operator-() const { return Vec2(-x, -y); }
- bool operator==(const Vec2& v) const { return isclose(x, v.x) && isclose(y, v.y); }
- bool operator!=(const Vec2& v) const { return !isclose(x, v.x) || !isclose(y, v.y); }
- float dot(const Vec2& v) const { return x * v.x + y * v.y; }
- float cross(const Vec2& v) const { return x * v.y - y * v.x; }
- float length() const { return sqrtf(x * x + y * y); }
- float length_squared() const { return x * x + y * y; }
- Vec2 normalize() const { float l = length(); return Vec2(x / l, y / l); }
- NoReturn assign(const Vec2& v) { x = v.x; y = v.y; return {}; }
- };
- struct Vec3{
- 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) {}
- Vec3(const Vec3& v) = default;
- Vec3 operator+(const Vec3& v) const { return Vec3(x + v.x, y + v.y, z + v.z); }
- Vec3& operator+=(const Vec3& v) { x += v.x; y += v.y; z += v.z; return *this; }
- Vec3 operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); }
- Vec3& operator-=(const Vec3& v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
- Vec3 operator*(float s) const { return Vec3(x * s, y * s, z * s); }
- Vec3& operator*=(float s) { x *= s; y *= s; z *= s; return *this; }
- Vec3 operator/(float s) const { return Vec3(x / s, y / s, z / s); }
- Vec3& operator/=(float s) { x /= s; y /= s; z /= s; return *this; }
- Vec3 operator-() const { return Vec3(-x, -y, -z); }
- bool operator==(const Vec3& v) const { return isclose(x, v.x) && isclose(y, v.y) && isclose(z, v.z); }
- bool operator!=(const Vec3& v) const { return !isclose(x, v.x) || !isclose(y, v.y) || !isclose(z, v.z); }
- float dot(const Vec3& v) const { return x * v.x + y * v.y + z * v.z; }
- Vec3 cross(const Vec3& v) const { return Vec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); }
- float length() const { return sqrtf(x * x + y * y + z * z); }
- float length_squared() const { return x * x + y * y + z * z; }
- Vec3 normalize() const { float l = length(); return Vec3(x / l, y / l, z / l); }
- NoReturn assign(const Vec3& v) { x = v.x; y = v.y; z = v.z; return {}; }
- };
- struct Vec4{
- 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) {}
- Vec4(const Vec4& v) = default;
- Vec4 operator+(const Vec4& v) const { return Vec4(x + v.x, y + v.y, z + v.z, w + v.w); }
- Vec4& operator+=(const Vec4& v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; }
- Vec4 operator-(const Vec4& v) const { return Vec4(x - v.x, y - v.y, z - v.z, w - v.w); }
- Vec4& operator-=(const Vec4& v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; }
- Vec4 operator*(float s) const { return Vec4(x * s, y * s, z * s, w * s); }
- Vec4& operator*=(float s) { x *= s; y *= s; z *= s; w *= s; return *this; }
- Vec4 operator/(float s) const { return Vec4(x / s, y / s, z / s, w / s); }
- Vec4& operator/=(float s) { x /= s; y /= s; z /= s; w /= s; return *this; }
- Vec4 operator-() const { return Vec4(-x, -y, -z, -w); }
- bool operator==(const Vec4& v) const { return isclose(x, v.x) && isclose(y, v.y) && isclose(z, v.z) && isclose(w, v.w); }
- bool operator!=(const Vec4& v) const { return !isclose(x, v.x) || !isclose(y, v.y) || !isclose(z, v.z) || !isclose(w, v.w); }
- float dot(const Vec4& v) const { return x * v.x + y * v.y + z * v.z + w * v.w; }
- float length() const { return sqrtf(x * x + y * y + z * z + w * w); }
- float length_squared() const { return x * x + y * y + z * z + w * w; }
- Vec4 normalize() const { float l = length(); return Vec4(x / l, y / l, z / l, w / l); }
- NoReturn assign(const Vec4& v) { x = v.x; y = v.y; z = v.z; w = v.w; return {}; }
- };
- struct Mat3x3{
- union {
- struct {
- float _11, _12, _13;
- float _21, _22, _23;
- float _31, _32, _33;
- };
- float m[3][3];
- float v[9];
- };
- Mat3x3();
- Mat3x3(float, float, float, float, float, float, float, float, float);
- Mat3x3(const Mat3x3& other) = default;
- void set_zeros();
- void set_ones();
- void set_identity();
- static Mat3x3 zeros();
- static Mat3x3 ones();
- static Mat3x3 identity();
- Mat3x3 operator+(const Mat3x3& other) const;
- Mat3x3 operator-(const Mat3x3& other) const;
- Mat3x3 operator*(float scalar) const;
- Mat3x3 operator/(float scalar) const;
- bool operator==(const Mat3x3& other) const;
- bool operator!=(const Mat3x3& other) const;
-
- Mat3x3 matmul(const Mat3x3& other) const;
- Vec3 matmul(const Vec3& other) const;
- float determinant() const;
- Mat3x3 transpose() const;
- bool inverse(Mat3x3& out) const;
- /*************** affine transformations ***************/
- static Mat3x3 trs(Vec2 t, float radian, Vec2 s);
- bool is_affine() const;
- Vec2 _t() const;
- float _r() const;
- Vec2 _s() const;
- };
- struct PyVec2: Vec2 {
- PY_CLASS(PyVec2, linalg, vec2)
- PyVec2() : Vec2() {}
- PyVec2(const Vec2& v) : Vec2(v) {}
- PyVec2(const PyVec2& v) = default;
- 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) {}
- PyVec3(const PyVec3& v) = default;
- 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){}
- PyVec4(const PyVec4& v) = default;
- 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){}
- PyMat3x3(const PyMat3x3& other) = default;
- 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<Vec2>(VM* vm, PyObject* obj) { return CAST(PyVec2&, obj); }
- template<> inline Vec3 py_cast<Vec3>(VM* vm, PyObject* obj) { return CAST(PyVec3&, obj); }
- template<> inline Vec4 py_cast<Vec4>(VM* vm, PyObject* obj) { return CAST(PyVec4&, obj); }
- template<> inline Mat3x3 py_cast<Mat3x3>(VM* vm, PyObject* obj) { return CAST(PyMat3x3&, obj); }
- template<> inline Vec2 _py_cast<Vec2>(VM* vm, PyObject* obj) { return _CAST(PyVec2&, obj); }
- template<> inline Vec3 _py_cast<Vec3>(VM* vm, PyObject* obj) { return _CAST(PyVec3&, obj); }
- template<> inline Vec4 _py_cast<Vec4>(VM* vm, PyObject* obj) { return _CAST(PyVec4&, obj); }
- template<> inline Mat3x3 _py_cast<Mat3x3>(VM* vm, PyObject* obj) { return _CAST(PyMat3x3&, obj); }
- void add_module_linalg(VM* vm);
- static_assert(sizeof(Py_<PyMat3x3>) <= 64);
- static_assert(std::is_trivially_copyable<PyVec2>::value);
- static_assert(std::is_trivially_copyable<PyVec3>::value);
- static_assert(std::is_trivially_copyable<PyVec4>::value);
- static_assert(std::is_trivially_copyable<PyMat3x3>::value);
- } // namespace pkpy
|