linalg.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #pragma once
  2. #include "bindings.h"
  3. namespace pkpy{
  4. inline bool isclose(float a, float b){ return std::fabs(a - b) <= NumberTraits<4>::kEpsilon; }
  5. struct Vec2{
  6. float x, y;
  7. Vec2() : x(0.0f), y(0.0f) {}
  8. Vec2(float x, float y) : x(x), y(y) {}
  9. Vec2(const Vec2& v) = default;
  10. Vec2 operator+(const Vec2& v) const { return Vec2(x + v.x, y + v.y); }
  11. Vec2& operator+=(const Vec2& v) { x += v.x; y += v.y; return *this; }
  12. Vec2 operator-(const Vec2& v) const { return Vec2(x - v.x, y - v.y); }
  13. Vec2& operator-=(const Vec2& v) { x -= v.x; y -= v.y; return *this; }
  14. Vec2 operator*(float s) const { return Vec2(x * s, y * s); }
  15. Vec2& operator*=(float s) { x *= s; y *= s; return *this; }
  16. Vec2 operator/(float s) const { return Vec2(x / s, y / s); }
  17. Vec2& operator/=(float s) { x /= s; y /= s; return *this; }
  18. Vec2 operator-() const { return Vec2(-x, -y); }
  19. bool operator==(const Vec2& v) const { return isclose(x, v.x) && isclose(y, v.y); }
  20. bool operator!=(const Vec2& v) const { return !isclose(x, v.x) || !isclose(y, v.y); }
  21. float dot(const Vec2& v) const { return x * v.x + y * v.y; }
  22. float cross(const Vec2& v) const { return x * v.y - y * v.x; }
  23. float length() const { return sqrtf(x * x + y * y); }
  24. float length_squared() const { return x * x + y * y; }
  25. Vec2 normalize() const { float l = length(); return Vec2(x / l, y / l); }
  26. NoReturn assign(const Vec2& v) { x = v.x; y = v.y; return {}; }
  27. };
  28. struct Vec3{
  29. float x, y, z;
  30. Vec3() : x(0.0f), y(0.0f), z(0.0f) {}
  31. Vec3(float x, float y, float z) : x(x), y(y), z(z) {}
  32. Vec3(const Vec3& v) = default;
  33. Vec3 operator+(const Vec3& v) const { return Vec3(x + v.x, y + v.y, z + v.z); }
  34. Vec3& operator+=(const Vec3& v) { x += v.x; y += v.y; z += v.z; return *this; }
  35. Vec3 operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); }
  36. Vec3& operator-=(const Vec3& v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
  37. Vec3 operator*(float s) const { return Vec3(x * s, y * s, z * s); }
  38. Vec3& operator*=(float s) { x *= s; y *= s; z *= s; return *this; }
  39. Vec3 operator/(float s) const { return Vec3(x / s, y / s, z / s); }
  40. Vec3& operator/=(float s) { x /= s; y /= s; z /= s; return *this; }
  41. Vec3 operator-() const { return Vec3(-x, -y, -z); }
  42. bool operator==(const Vec3& v) const { return isclose(x, v.x) && isclose(y, v.y) && isclose(z, v.z); }
  43. bool operator!=(const Vec3& v) const { return !isclose(x, v.x) || !isclose(y, v.y) || !isclose(z, v.z); }
  44. float dot(const Vec3& v) const { return x * v.x + y * v.y + z * v.z; }
  45. 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); }
  46. float length() const { return sqrtf(x * x + y * y + z * z); }
  47. float length_squared() const { return x * x + y * y + z * z; }
  48. Vec3 normalize() const { float l = length(); return Vec3(x / l, y / l, z / l); }
  49. NoReturn assign(const Vec3& v) { x = v.x; y = v.y; z = v.z; return {}; }
  50. };
  51. struct Vec4{
  52. float x, y, z, w;
  53. Vec4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {}
  54. Vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
  55. Vec4(const Vec4& v) = default;
  56. Vec4 operator+(const Vec4& v) const { return Vec4(x + v.x, y + v.y, z + v.z, w + v.w); }
  57. Vec4& operator+=(const Vec4& v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; }
  58. Vec4 operator-(const Vec4& v) const { return Vec4(x - v.x, y - v.y, z - v.z, w - v.w); }
  59. Vec4& operator-=(const Vec4& v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; }
  60. Vec4 operator*(float s) const { return Vec4(x * s, y * s, z * s, w * s); }
  61. Vec4& operator*=(float s) { x *= s; y *= s; z *= s; w *= s; return *this; }
  62. Vec4 operator/(float s) const { return Vec4(x / s, y / s, z / s, w / s); }
  63. Vec4& operator/=(float s) { x /= s; y /= s; z /= s; w /= s; return *this; }
  64. Vec4 operator-() const { return Vec4(-x, -y, -z, -w); }
  65. bool operator==(const Vec4& v) const { return isclose(x, v.x) && isclose(y, v.y) && isclose(z, v.z) && isclose(w, v.w); }
  66. bool operator!=(const Vec4& v) const { return !isclose(x, v.x) || !isclose(y, v.y) || !isclose(z, v.z) || !isclose(w, v.w); }
  67. float dot(const Vec4& v) const { return x * v.x + y * v.y + z * v.z + w * v.w; }
  68. float length() const { return sqrtf(x * x + y * y + z * z + w * w); }
  69. float length_squared() const { return x * x + y * y + z * z + w * w; }
  70. Vec4 normalize() const { float l = length(); return Vec4(x / l, y / l, z / l, w / l); }
  71. NoReturn assign(const Vec4& v) { x = v.x; y = v.y; z = v.z; w = v.w; return {}; }
  72. };
  73. struct Mat3x3{
  74. union {
  75. struct {
  76. float _11, _12, _13;
  77. float _21, _22, _23;
  78. float _31, _32, _33;
  79. };
  80. float m[3][3];
  81. float v[9];
  82. };
  83. Mat3x3();
  84. Mat3x3(float, float, float, float, float, float, float, float, float);
  85. Mat3x3(const Mat3x3& other) = default;
  86. void set_zeros();
  87. void set_ones();
  88. void set_identity();
  89. static Mat3x3 zeros();
  90. static Mat3x3 ones();
  91. static Mat3x3 identity();
  92. Mat3x3 operator+(const Mat3x3& other) const;
  93. Mat3x3 operator-(const Mat3x3& other) const;
  94. Mat3x3 operator*(float scalar) const;
  95. Mat3x3 operator/(float scalar) const;
  96. bool operator==(const Mat3x3& other) const;
  97. bool operator!=(const Mat3x3& other) const;
  98. Mat3x3 matmul(const Mat3x3& other) const;
  99. Vec3 matmul(const Vec3& other) const;
  100. float determinant() const;
  101. Mat3x3 transpose() const;
  102. bool inverse(Mat3x3& out) const;
  103. /*************** affine transformations ***************/
  104. static Mat3x3 trs(Vec2 t, float radian, Vec2 s);
  105. bool is_affine() const;
  106. Vec2 _t() const;
  107. float _r() const;
  108. Vec2 _s() const;
  109. };
  110. struct PyVec2: Vec2 {
  111. PY_CLASS(PyVec2, linalg, vec2)
  112. PyVec2() : Vec2() {}
  113. PyVec2(const Vec2& v) : Vec2(v) {}
  114. PyVec2(const PyVec2& v) = default;
  115. Vec2* _() { return this; }
  116. static void _register(VM* vm, PyObject* mod, PyObject* type);
  117. };
  118. struct PyVec3: Vec3 {
  119. PY_CLASS(PyVec3, linalg, vec3)
  120. PyVec3() : Vec3() {}
  121. PyVec3(const Vec3& v) : Vec3(v) {}
  122. PyVec3(const PyVec3& v) = default;
  123. Vec3* _() { return this; }
  124. static void _register(VM* vm, PyObject* mod, PyObject* type);
  125. };
  126. struct PyVec4: Vec4{
  127. PY_CLASS(PyVec4, linalg, vec4)
  128. PyVec4(): Vec4(){}
  129. PyVec4(const Vec4& v): Vec4(v){}
  130. PyVec4(const PyVec4& v) = default;
  131. Vec4* _(){ return this; }
  132. static void _register(VM* vm, PyObject* mod, PyObject* type);
  133. };
  134. struct PyMat3x3: Mat3x3{
  135. PY_CLASS(PyMat3x3, linalg, mat3x3)
  136. PyMat3x3(): Mat3x3(){}
  137. PyMat3x3(const Mat3x3& other): Mat3x3(other){}
  138. PyMat3x3(const PyMat3x3& other) = default;
  139. Mat3x3* _(){ return this; }
  140. static void _register(VM* vm, PyObject* mod, PyObject* type);
  141. };
  142. inline PyObject* py_var(VM* vm, Vec2 obj){ return VAR_T(PyVec2, obj); }
  143. inline PyObject* py_var(VM* vm, const PyVec2& obj){ return VAR_T(PyVec2, obj);}
  144. inline PyObject* py_var(VM* vm, Vec3 obj){ return VAR_T(PyVec3, obj); }
  145. inline PyObject* py_var(VM* vm, const PyVec3& obj){ return VAR_T(PyVec3, obj);}
  146. inline PyObject* py_var(VM* vm, Vec4 obj){ return VAR_T(PyVec4, obj); }
  147. inline PyObject* py_var(VM* vm, const PyVec4& obj){ return VAR_T(PyVec4, obj);}
  148. inline PyObject* py_var(VM* vm, const Mat3x3& obj){ return VAR_T(PyMat3x3, obj); }
  149. inline PyObject* py_var(VM* vm, const PyMat3x3& obj){ return VAR_T(PyMat3x3, obj); }
  150. template<> inline Vec2 py_cast<Vec2>(VM* vm, PyObject* obj) { return CAST(PyVec2&, obj); }
  151. template<> inline Vec3 py_cast<Vec3>(VM* vm, PyObject* obj) { return CAST(PyVec3&, obj); }
  152. template<> inline Vec4 py_cast<Vec4>(VM* vm, PyObject* obj) { return CAST(PyVec4&, obj); }
  153. template<> inline Mat3x3 py_cast<Mat3x3>(VM* vm, PyObject* obj) { return CAST(PyMat3x3&, obj); }
  154. template<> inline Vec2 _py_cast<Vec2>(VM* vm, PyObject* obj) { return _CAST(PyVec2&, obj); }
  155. template<> inline Vec3 _py_cast<Vec3>(VM* vm, PyObject* obj) { return _CAST(PyVec3&, obj); }
  156. template<> inline Vec4 _py_cast<Vec4>(VM* vm, PyObject* obj) { return _CAST(PyVec4&, obj); }
  157. template<> inline Mat3x3 _py_cast<Mat3x3>(VM* vm, PyObject* obj) { return _CAST(PyMat3x3&, obj); }
  158. void add_module_linalg(VM* vm);
  159. static_assert(sizeof(Py_<PyMat3x3>) <= 64);
  160. static_assert(std::is_trivially_copyable<PyVec2>::value);
  161. static_assert(std::is_trivially_copyable<PyVec3>::value);
  162. static_assert(std::is_trivially_copyable<PyVec4>::value);
  163. static_assert(std::is_trivially_copyable<PyMat3x3>::value);
  164. } // namespace pkpy