linalg.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. PY_CLASS(Vec2, linalg, vec2)
  7. Vec2* _() { return this; }
  8. static void _register(VM* vm, PyObject* mod, PyObject* type);
  9. float x, y;
  10. Vec2() : x(0.0f), y(0.0f) {}
  11. Vec2(float x, float y) : x(x), y(y) {}
  12. Vec2 operator+(const Vec2& v) const { return Vec2(x + v.x, y + v.y); }
  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 Vec2& v) const { return Vec2(x * v.x, y * v.y); }
  16. Vec2 operator/(float s) const { return Vec2(x / s, y / s); }
  17. Vec2 operator-() const { return Vec2(-x, -y); }
  18. bool operator==(const Vec2& v) const { return isclose(x, v.x) && isclose(y, v.y); }
  19. bool operator!=(const Vec2& v) const { return !isclose(x, v.x) || !isclose(y, v.y); }
  20. float dot(const Vec2& v) const { return x * v.x + y * v.y; }
  21. float cross(const Vec2& v) const { return x * v.y - y * v.x; }
  22. float length() const { return sqrtf(x * x + y * y); }
  23. float length_squared() const { return x * x + y * y; }
  24. Vec2 normalize() const { float l = length(); return Vec2(x / l, y / l); }
  25. Vec2 rotate(float radian) const { float cr = cosf(radian), sr = sinf(radian); return Vec2(x * cr - y * sr, x * sr + y * cr); }
  26. NoReturn normalize_() { float l = length(); x /= l; y /= l; return {}; }
  27. NoReturn copy_(const Vec2& v) { x = v.x; y = v.y; return {}; }
  28. };
  29. struct Vec3{
  30. PY_CLASS(Vec3, linalg, vec3)
  31. Vec3* _() { return this; }
  32. static void _register(VM* vm, PyObject* mod, PyObject* type);
  33. float x, y, z;
  34. Vec3() : x(0.0f), y(0.0f), z(0.0f) {}
  35. Vec3(float x, float y, float z) : x(x), y(y), z(z) {}
  36. Vec3 operator+(const Vec3& v) const { return Vec3(x + v.x, y + v.y, z + v.z); }
  37. Vec3 operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); }
  38. Vec3 operator*(float s) const { return Vec3(x * s, y * s, z * s); }
  39. Vec3 operator*(const Vec3& v) const { return Vec3(x * v.x, y * v.y, z * v.z); }
  40. Vec3 operator/(float s) const { return Vec3(x / s, y / s, z / s); }
  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 normalize_() { float l = length(); x /= l; y /= l; z /= l; return {}; }
  50. NoReturn copy_(const Vec3& v) { x = v.x; y = v.y; z = v.z; return {}; }
  51. };
  52. struct Vec4{
  53. PY_CLASS(Vec4, linalg, vec4)
  54. Vec4* _() { return this; }
  55. static void _register(VM* vm, PyObject* mod, PyObject* type);
  56. float x, y, z, w;
  57. Vec4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {}
  58. Vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
  59. Vec4 operator+(const Vec4& v) const { return Vec4(x + v.x, y + v.y, z + v.z, w + v.w); }
  60. Vec4 operator-(const Vec4& v) const { return Vec4(x - v.x, y - v.y, z - v.z, w - v.w); }
  61. Vec4 operator*(float s) const { return Vec4(x * s, y * s, z * s, w * s); }
  62. Vec4 operator*(const Vec4& v) const { return Vec4(x * v.x, y * v.y, z * v.z, w * v.w); }
  63. Vec4 operator/(float s) const { return Vec4(x / s, y / s, z / s, w / s); }
  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 normalize_() { float l = length(); x /= l; y /= l; z /= l; w /= l; return {}; }
  72. NoReturn copy_(const Vec4& v) { x = v.x; y = v.y; z = v.z; w = v.w; return {}; }
  73. };
  74. struct Mat3x3{
  75. PY_CLASS(Mat3x3, linalg, mat3x3)
  76. Mat3x3* _(){ return this; }
  77. static void _register(VM* vm, PyObject* mod, PyObject* type);
  78. union {
  79. struct {
  80. float _11, _12, _13;
  81. float _21, _22, _23;
  82. float _31, _32, _33;
  83. };
  84. float m[3][3];
  85. float v[9];
  86. };
  87. Mat3x3();
  88. Mat3x3(float, float, float, float, float, float, float, float, float);
  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. void add_module_linalg(VM* vm);
  111. static_assert(sizeof(Py_<Mat3x3>) <= 64);
  112. static_assert(std::is_trivially_copyable<Vec2>::value);
  113. static_assert(std::is_trivially_copyable<Vec3>::value);
  114. static_assert(std::is_trivially_copyable<Vec4>::value);
  115. static_assert(std::is_trivially_copyable<Mat3x3>::value);
  116. } // namespace pkpy