1
0

linalg.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #pragma once
  2. #include "bindings.h"
  3. namespace pkpy{
  4. inline bool isclose(float a, float b){ return fabsf(a - b) < 1e-4f; }
  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. };
  27. struct Vec3{
  28. float x, y, z;
  29. Vec3() : x(0.0f), y(0.0f), z(0.0f) {}
  30. Vec3(float x, float y, float z) : x(x), y(y), z(z) {}
  31. Vec3(const Vec3& v) = default;
  32. Vec3 operator+(const Vec3& v) const { return Vec3(x + v.x, y + v.y, z + v.z); }
  33. Vec3& operator+=(const Vec3& v) { x += v.x; y += v.y; z += v.z; return *this; }
  34. Vec3 operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); }
  35. Vec3& operator-=(const Vec3& v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
  36. Vec3 operator*(float s) const { return Vec3(x * s, y * s, z * s); }
  37. Vec3& operator*=(float s) { x *= s; y *= s; z *= s; return *this; }
  38. Vec3 operator/(float s) const { return Vec3(x / s, y / s, z / s); }
  39. Vec3& operator/=(float s) { x /= s; y /= s; z /= s; return *this; }
  40. Vec3 operator-() const { return Vec3(-x, -y, -z); }
  41. bool operator==(const Vec3& v) const { return isclose(x, v.x) && isclose(y, v.y) && isclose(z, v.z); }
  42. bool operator!=(const Vec3& v) const { return !isclose(x, v.x) || !isclose(y, v.y) || !isclose(z, v.z); }
  43. float dot(const Vec3& v) const { return x * v.x + y * v.y + z * v.z; }
  44. 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); }
  45. float length() const { return sqrtf(x * x + y * y + z * z); }
  46. float length_squared() const { return x * x + y * y + z * z; }
  47. Vec3 normalize() const { float l = length(); return Vec3(x / l, y / l, z / l); }
  48. };
  49. struct Vec4{
  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(const Vec4& v) = default;
  54. Vec4 operator+(const Vec4& v) const { return Vec4(x + v.x, y + v.y, z + v.z, w + v.w); }
  55. Vec4& operator+=(const Vec4& v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; }
  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*(float s) const { return Vec4(x * s, y * s, z * s, w * s); }
  59. Vec4& operator*=(float s) { x *= s; y *= s; z *= s; w *= s; 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-() const { return Vec4(-x, -y, -z, -w); }
  63. bool operator==(const Vec4& v) const { return isclose(x, v.x) && isclose(y, v.y) && isclose(z, v.z) && isclose(w, v.w); }
  64. bool operator!=(const Vec4& v) const { return !isclose(x, v.x) || !isclose(y, v.y) || !isclose(z, v.z) || !isclose(w, v.w); }
  65. float dot(const Vec4& v) const { return x * v.x + y * v.y + z * v.z + w * v.w; }
  66. float length() const { return sqrtf(x * x + y * y + z * z + w * w); }
  67. float length_squared() const { return x * x + y * y + z * z + w * w; }
  68. Vec4 normalize() const { float l = length(); return Vec4(x / l, y / l, z / l, w / l); }
  69. };
  70. struct Mat3x3{
  71. union {
  72. struct {
  73. float _11, _12, _13;
  74. float _21, _22, _23;
  75. float _31, _32, _33;
  76. };
  77. float m[3][3];
  78. float v[9];
  79. };
  80. Mat3x3() {}
  81. Mat3x3(float _11, float _12, float _13,
  82. float _21, float _22, float _23,
  83. float _31, float _32, float _33)
  84. : _11(_11), _12(_12), _13(_13)
  85. , _21(_21), _22(_22), _23(_23)
  86. , _31(_31), _32(_32), _33(_33) {}
  87. Mat3x3(const Mat3x3& other) = default;
  88. void set_zeros(){ for (int i=0; i<9; ++i) v[i] = 0.0f; }
  89. void set_ones(){ for (int i=0; i<9; ++i) v[i] = 1.0f; }
  90. void set_identity(){ set_zeros(); _11 = _22 = _33 = 1.0f; }
  91. static Mat3x3 zeros(){
  92. return Mat3x3(0, 0, 0, 0, 0, 0, 0, 0, 0);
  93. }
  94. static Mat3x3 ones(){
  95. return Mat3x3(1, 1, 1, 1, 1, 1, 1, 1, 1);
  96. }
  97. static Mat3x3 identity(){
  98. return Mat3x3(1, 0, 0, 0, 1, 0, 0, 0, 1);
  99. }
  100. Mat3x3 operator+(const Mat3x3& other) const{
  101. Mat3x3 ret;
  102. for (int i=0; i<9; ++i) ret.v[i] = v[i] + other.v[i];
  103. return ret;
  104. }
  105. Mat3x3 operator-(const Mat3x3& other) const{
  106. Mat3x3 ret;
  107. for (int i=0; i<9; ++i) ret.v[i] = v[i] - other.v[i];
  108. return ret;
  109. }
  110. Mat3x3 operator*(float scalar) const{
  111. Mat3x3 ret;
  112. for (int i=0; i<9; ++i) ret.v[i] = v[i] * scalar;
  113. return ret;
  114. }
  115. Mat3x3 operator/(float scalar) const{
  116. Mat3x3 ret;
  117. for (int i=0; i<9; ++i) ret.v[i] = v[i] / scalar;
  118. return ret;
  119. }
  120. Mat3x3& operator+=(const Mat3x3& other){
  121. for (int i=0; i<9; ++i) v[i] += other.v[i];
  122. return *this;
  123. }
  124. Mat3x3& operator-=(const Mat3x3& other){
  125. for (int i=0; i<9; ++i) v[i] -= other.v[i];
  126. return *this;
  127. }
  128. Mat3x3& operator*=(float scalar){
  129. for (int i=0; i<9; ++i) v[i] *= scalar;
  130. return *this;
  131. }
  132. Mat3x3& operator/=(float scalar){
  133. for (int i=0; i<9; ++i) v[i] /= scalar;
  134. return *this;
  135. }
  136. Mat3x3 matmul(const Mat3x3& other) const{
  137. Mat3x3 ret;
  138. ret._11 = _11 * other._11 + _12 * other._21 + _13 * other._31;
  139. ret._12 = _11 * other._12 + _12 * other._22 + _13 * other._32;
  140. ret._13 = _11 * other._13 + _12 * other._23 + _13 * other._33;
  141. ret._21 = _21 * other._11 + _22 * other._21 + _23 * other._31;
  142. ret._22 = _21 * other._12 + _22 * other._22 + _23 * other._32;
  143. ret._23 = _21 * other._13 + _22 * other._23 + _23 * other._33;
  144. ret._31 = _31 * other._11 + _32 * other._21 + _33 * other._31;
  145. ret._32 = _31 * other._12 + _32 * other._22 + _33 * other._32;
  146. ret._33 = _31 * other._13 + _32 * other._23 + _33 * other._33;
  147. return ret;
  148. }
  149. Vec3 matmul(const Vec3& other) const{
  150. Vec3 ret;
  151. ret.x = _11 * other.x + _12 * other.y + _13 * other.z;
  152. ret.y = _21 * other.x + _22 * other.y + _23 * other.z;
  153. ret.z = _31 * other.x + _32 * other.y + _33 * other.z;
  154. return ret;
  155. }
  156. bool operator==(const Mat3x3& other) const{
  157. for (int i=0; i<9; ++i){
  158. if (!isclose(v[i], other.v[i])) return false;
  159. }
  160. return true;
  161. }
  162. bool operator!=(const Mat3x3& other) const{
  163. for (int i=0; i<9; ++i){
  164. if (!isclose(v[i], other.v[i])) return true;
  165. }
  166. return false;
  167. }
  168. float determinant() const{
  169. return _11 * _22 * _33 + _12 * _23 * _31 + _13 * _21 * _32
  170. - _11 * _23 * _32 - _12 * _21 * _33 - _13 * _22 * _31;
  171. }
  172. Mat3x3 transpose() const{
  173. Mat3x3 ret;
  174. ret._11 = _11; ret._12 = _21; ret._13 = _31;
  175. ret._21 = _12; ret._22 = _22; ret._23 = _32;
  176. ret._31 = _13; ret._32 = _23; ret._33 = _33;
  177. return ret;
  178. }
  179. bool inverse(Mat3x3& ret) const{
  180. float det = determinant();
  181. if (isclose(det, 0)) return false;
  182. float inv_det = 1.0f / det;
  183. ret._11 = (_22 * _33 - _23 * _32) * inv_det;
  184. ret._12 = (_13 * _32 - _12 * _33) * inv_det;
  185. ret._13 = (_12 * _23 - _13 * _22) * inv_det;
  186. ret._21 = (_23 * _31 - _21 * _33) * inv_det;
  187. ret._22 = (_11 * _33 - _13 * _31) * inv_det;
  188. ret._23 = (_13 * _21 - _11 * _23) * inv_det;
  189. ret._31 = (_21 * _32 - _22 * _31) * inv_det;
  190. ret._32 = (_12 * _31 - _11 * _32) * inv_det;
  191. ret._33 = (_11 * _22 - _12 * _21) * inv_det;
  192. return true;
  193. }
  194. /*************** affine transformations ***************/
  195. static Mat3x3 trs(Vec2 t, float radian, Vec2 s){
  196. float cr = cosf(radian);
  197. float sr = sinf(radian);
  198. return Mat3x3(s.x * cr, -s.y * sr, t.x,
  199. s.x * sr, s.y * cr, t.y,
  200. 0.0f, 0.0f, 1.0f);
  201. }
  202. bool is_affine() const{
  203. float det = _11 * _22 - _12 * _21;
  204. if(isclose(det, 0)) return false;
  205. return _31 == 0.0f && _32 == 0.0f && _33 == 1.0f;
  206. }
  207. Vec2 _t() const { return Vec2(_13, _23); }
  208. float _r() const { return atan2f(_21, _11); }
  209. Vec2 _s() const {
  210. return Vec2(
  211. sqrtf(_11 * _11 + _21 * _21),
  212. sqrtf(_12 * _12 + _22 * _22)
  213. );
  214. }
  215. Vec2 transform_point(Vec2 vec) const {
  216. return Vec2(_11 * vec.x + _12 * vec.y + _13, _21 * vec.x + _22 * vec.y + _23);
  217. }
  218. Vec2 transform_vector(Vec2 vec) const {
  219. return Vec2(_11 * vec.x + _12 * vec.y, _21 * vec.x + _22 * vec.y);
  220. }
  221. };
  222. struct PyVec2;
  223. struct PyVec3;
  224. struct PyVec4;
  225. struct PyMat3x3;
  226. PyObject* py_var(VM*, Vec2);
  227. PyObject* py_var(VM*, const PyVec2&);
  228. PyObject* py_var(VM*, Vec3);
  229. PyObject* py_var(VM*, const PyVec3&);
  230. PyObject* py_var(VM*, Vec4);
  231. PyObject* py_var(VM*, const PyVec4&);
  232. PyObject* py_var(VM*, const Mat3x3&);
  233. PyObject* py_var(VM*, const PyMat3x3&);
  234. struct PyVec2: Vec2 {
  235. PY_CLASS(PyVec2, linalg, vec2)
  236. PyVec2() : Vec2() {}
  237. PyVec2(const Vec2& v) : Vec2(v) {}
  238. PyVec2(const PyVec2& v) = default;
  239. static void _register(VM* vm, PyObject* mod, PyObject* type);
  240. };
  241. struct PyVec3: Vec3 {
  242. PY_CLASS(PyVec3, linalg, vec3)
  243. PyVec3() : Vec3() {}
  244. PyVec3(const Vec3& v) : Vec3(v) {}
  245. PyVec3(const PyVec3& v) = default;
  246. static void _register(VM* vm, PyObject* mod, PyObject* type);
  247. };
  248. struct PyVec4: Vec4{
  249. PY_CLASS(PyVec4, linalg, vec4)
  250. PyVec4(): Vec4(){}
  251. PyVec4(const Vec4& v): Vec4(v){}
  252. PyVec4(const PyVec4& v) = default;
  253. static void _register(VM* vm, PyObject* mod, PyObject* type);
  254. };
  255. struct PyMat3x3: Mat3x3{
  256. PY_CLASS(PyMat3x3, linalg, mat3x3)
  257. PyMat3x3(): Mat3x3(){}
  258. PyMat3x3(const Mat3x3& other): Mat3x3(other){}
  259. PyMat3x3(const PyMat3x3& other) = default;
  260. static void _register(VM* vm, PyObject* mod, PyObject* type);
  261. };
  262. inline PyObject* py_var(VM* vm, Vec2 obj){ return VAR_T(PyVec2, obj); }
  263. inline PyObject* py_var(VM* vm, const PyVec2& obj){ return VAR_T(PyVec2, obj);}
  264. inline PyObject* py_var(VM* vm, Vec3 obj){ return VAR_T(PyVec3, obj); }
  265. inline PyObject* py_var(VM* vm, const PyVec3& obj){ return VAR_T(PyVec3, obj);}
  266. inline PyObject* py_var(VM* vm, Vec4 obj){ return VAR_T(PyVec4, obj); }
  267. inline PyObject* py_var(VM* vm, const PyVec4& obj){ return VAR_T(PyVec4, obj);}
  268. inline PyObject* py_var(VM* vm, const Mat3x3& obj){ return VAR_T(PyMat3x3, obj); }
  269. inline PyObject* py_var(VM* vm, const PyMat3x3& obj){ return VAR_T(PyMat3x3, obj); }
  270. template<> inline Vec2 py_cast<Vec2>(VM* vm, PyObject* obj) { return CAST(PyVec2&, obj); }
  271. template<> inline Vec3 py_cast<Vec3>(VM* vm, PyObject* obj) { return CAST(PyVec3&, obj); }
  272. template<> inline Vec4 py_cast<Vec4>(VM* vm, PyObject* obj) { return CAST(PyVec4&, obj); }
  273. template<> inline Mat3x3 py_cast<Mat3x3>(VM* vm, PyObject* obj) { return CAST(PyMat3x3&, obj); }
  274. template<> inline Vec2 _py_cast<Vec2>(VM* vm, PyObject* obj) { return _CAST(PyVec2&, obj); }
  275. template<> inline Vec3 _py_cast<Vec3>(VM* vm, PyObject* obj) { return _CAST(PyVec3&, obj); }
  276. template<> inline Vec4 _py_cast<Vec4>(VM* vm, PyObject* obj) { return _CAST(PyVec4&, obj); }
  277. template<> inline Mat3x3 _py_cast<Mat3x3>(VM* vm, PyObject* obj) { return _CAST(PyMat3x3&, obj); }
  278. inline void add_module_linalg(VM* vm){
  279. PyObject* linalg = vm->new_module("linalg");
  280. PyVec2::register_class(vm, linalg);
  281. PyVec3::register_class(vm, linalg);
  282. PyVec4::register_class(vm, linalg);
  283. PyMat3x3::register_class(vm, linalg);
  284. }
  285. static_assert(sizeof(Py_<PyMat3x3>) <= 64);
  286. static_assert(std::is_trivially_copyable<PyVec2>::value);
  287. static_assert(std::is_trivially_copyable<PyVec3>::value);
  288. static_assert(std::is_trivially_copyable<PyVec4>::value);
  289. static_assert(std::is_trivially_copyable<PyMat3x3>::value);
  290. } // namespace pkpy