linalg.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #include "bindings.h"
  3. namespace pkpy{
  4. inline bool isclose(float a, float b){ return std::fabs(a - b) < 1e-4; }
  5. struct Vec2{
  6. static void _register(VM* vm, PyObject* mod, PyObject* type);
  7. float x, y;
  8. Vec2() : x(0.0f), y(0.0f) {}
  9. Vec2(float x, float y) : x(x), y(y) {}
  10. Vec2 operator+(const Vec2& v) const { return Vec2(x + v.x, y + v.y); }
  11. Vec2 operator-(const Vec2& v) const { return Vec2(x - v.x, y - v.y); }
  12. Vec2 operator*(float s) const { return Vec2(x * s, y * s); }
  13. Vec2 operator*(const Vec2& v) const { return Vec2(x * v.x, y * v.y); }
  14. Vec2 operator/(float s) const { return Vec2(x / s, y / s); }
  15. Vec2 operator-() const { return Vec2(-x, -y); }
  16. bool operator==(const Vec2& v) const { return isclose(x, v.x) && isclose(y, v.y); }
  17. bool operator!=(const Vec2& v) const { return !isclose(x, v.x) || !isclose(y, v.y); }
  18. float dot(const Vec2& v) const { return x * v.x + y * v.y; }
  19. float cross(const Vec2& v) const { return x * v.y - y * v.x; }
  20. float length() const { return sqrtf(x * x + y * y); }
  21. float length_squared() const { return x * x + y * y; }
  22. Vec2 normalize() const { float l = length(); return Vec2(x / l, y / l); }
  23. Vec2 rotate(float radian) const { float cr = cosf(radian), sr = sinf(radian); return Vec2(x * cr - y * sr, x * sr + y * cr); }
  24. NoReturn normalize_() { float l = length(); x /= l; y /= l; return {}; }
  25. NoReturn copy_(const Vec2& v) { x = v.x; y = v.y; return {}; }
  26. };
  27. struct Vec3{
  28. static void _register(VM* vm, PyObject* mod, PyObject* type);
  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 operator+(const Vec3& v) const { return Vec3(x + v.x, y + v.y, z + v.z); }
  33. Vec3 operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); }
  34. Vec3 operator*(float s) const { return Vec3(x * s, y * s, z * s); }
  35. Vec3 operator*(const Vec3& v) const { return Vec3(x * v.x, y * v.y, z * v.z); }
  36. Vec3 operator/(float s) const { return Vec3(x / s, y / s, z / s); }
  37. Vec3 operator-() const { return Vec3(-x, -y, -z); }
  38. bool operator==(const Vec3& v) const { return isclose(x, v.x) && isclose(y, v.y) && isclose(z, v.z); }
  39. bool operator!=(const Vec3& v) const { return !isclose(x, v.x) || !isclose(y, v.y) || !isclose(z, v.z); }
  40. float dot(const Vec3& v) const { return x * v.x + y * v.y + z * v.z; }
  41. 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); }
  42. float length() const { return sqrtf(x * x + y * y + z * z); }
  43. float length_squared() const { return x * x + y * y + z * z; }
  44. Vec3 normalize() const { float l = length(); return Vec3(x / l, y / l, z / l); }
  45. NoReturn normalize_() { float l = length(); x /= l; y /= l; z /= l; return {}; }
  46. NoReturn copy_(const Vec3& v) { x = v.x; y = v.y; z = v.z; return {}; }
  47. };
  48. struct Vec4{
  49. static void _register(VM* vm, PyObject* mod, PyObject* type);
  50. float x, y, z, w;
  51. Vec4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {}
  52. Vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
  53. Vec4 operator+(const Vec4& v) const { return Vec4(x + v.x, y + v.y, z + v.z, w + v.w); }
  54. Vec4 operator-(const Vec4& v) const { return Vec4(x - v.x, y - v.y, z - v.z, w - v.w); }
  55. Vec4 operator*(float s) const { return Vec4(x * s, y * s, z * s, w * s); }
  56. Vec4 operator*(const Vec4& v) const { return Vec4(x * v.x, y * v.y, z * v.z, w * v.w); }
  57. Vec4 operator/(float s) const { return Vec4(x / s, y / s, z / s, w / s); }
  58. Vec4 operator-() const { return Vec4(-x, -y, -z, -w); }
  59. bool operator==(const Vec4& v) const { return isclose(x, v.x) && isclose(y, v.y) && isclose(z, v.z) && isclose(w, v.w); }
  60. bool operator!=(const Vec4& v) const { return !isclose(x, v.x) || !isclose(y, v.y) || !isclose(z, v.z) || !isclose(w, v.w); }
  61. float dot(const Vec4& v) const { return x * v.x + y * v.y + z * v.z + w * v.w; }
  62. float length() const { return sqrtf(x * x + y * y + z * z + w * w); }
  63. float length_squared() const { return x * x + y * y + z * z + w * w; }
  64. Vec4 normalize() const { float l = length(); return Vec4(x / l, y / l, z / l, w / l); }
  65. NoReturn normalize_() { float l = length(); x /= l; y /= l; z /= l; w /= l; return {}; }
  66. NoReturn copy_(const Vec4& v) { x = v.x; y = v.y; z = v.z; w = v.w; return {}; }
  67. };
  68. struct Mat3x3{
  69. static void _register(VM* vm, PyObject* mod, PyObject* type);
  70. union {
  71. struct {
  72. float _11, _12, _13;
  73. float _21, _22, _23;
  74. float _31, _32, _33;
  75. };
  76. float m[3][3];
  77. float v[9];
  78. };
  79. Mat3x3();
  80. Mat3x3(float, float, float, float, float, float, float, float, float);
  81. static Mat3x3 zeros();
  82. static Mat3x3 ones();
  83. static Mat3x3 identity();
  84. Mat3x3 operator+(const Mat3x3& other) const;
  85. Mat3x3 operator-(const Mat3x3& other) const;
  86. Mat3x3 operator*(float scalar) const;
  87. Mat3x3 operator/(float scalar) const;
  88. bool operator==(const Mat3x3& other) const;
  89. bool operator!=(const Mat3x3& other) const;
  90. Mat3x3 matmul(const Mat3x3& other) const;
  91. Vec3 matmul(const Vec3& other) const;
  92. float determinant() const;
  93. Mat3x3 transpose() const;
  94. bool inverse(Mat3x3& out) const;
  95. /*************** affine transformations ***************/
  96. static Mat3x3 trs(Vec2 t, float radian, Vec2 s);
  97. bool is_affine() const;
  98. Vec2 _t() const;
  99. float _r() const;
  100. Vec2 _s() const;
  101. };
  102. void add_module_linalg(VM* vm);
  103. static_assert(sizeof(Py_<Mat3x3>) <= 64);
  104. static_assert(std::is_trivially_copyable<Vec2>::value);
  105. static_assert(std::is_trivially_copyable<Vec3>::value);
  106. static_assert(std::is_trivially_copyable<Vec4>::value);
  107. static_assert(std::is_trivially_copyable<Mat3x3>::value);
  108. } // namespace pkpy