linalg.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. #include "pocketpy/linalg.h"
  2. namespace pkpy{
  3. #define BIND_VEC_VEC_OP(D, name, op) \
  4. vm->bind_method<1>(type, #name, [](VM* vm, ArgsView args){ \
  5. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  6. PyVec##D& other = CAST(PyVec##D&, args[1]); \
  7. return VAR(self op other); \
  8. });
  9. #define BIND_VEC_FLOAT_OP(D, name, op) \
  10. vm->bind_method<1>(type, #name, [](VM* vm, ArgsView args){ \
  11. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  12. f64 other = CAST(f64, args[1]); \
  13. return VAR(self op other); \
  14. });
  15. #define BIND_VEC_FUNCTION_0(D, name) \
  16. vm->bind_method<0>(type, #name, [](VM* vm, ArgsView args){ \
  17. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  18. return VAR(self.name()); \
  19. });
  20. #define BIND_VEC_FUNCTION_1(D, name) \
  21. vm->bind_method<1>(type, #name, [](VM* vm, ArgsView args){ \
  22. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  23. PyVec##D& other = CAST(PyVec##D&, args[1]); \
  24. return VAR(self.name(other)); \
  25. });
  26. // https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector2.cs#L289
  27. static Vec2 SmoothDamp(Vec2 current, Vec2 target, PyVec2& currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
  28. {
  29. // Based on Game Programming Gems 4 Chapter 1.10
  30. smoothTime = std::max(0.0001F, smoothTime);
  31. float omega = 2.0F / smoothTime;
  32. float x = omega * deltaTime;
  33. float exp = 1.0F / (1.0F + x + 0.48F * x * x + 0.235F * x * x * x);
  34. float change_x = current.x - target.x;
  35. float change_y = current.y - target.y;
  36. Vec2 originalTo = target;
  37. // Clamp maximum speed
  38. float maxChange = maxSpeed * smoothTime;
  39. float maxChangeSq = maxChange * maxChange;
  40. float sqDist = change_x * change_x + change_y * change_y;
  41. if (sqDist > maxChangeSq)
  42. {
  43. float mag = std::sqrt(sqDist);
  44. change_x = change_x / mag * maxChange;
  45. change_y = change_y / mag * maxChange;
  46. }
  47. target.x = current.x - change_x;
  48. target.y = current.y - change_y;
  49. float temp_x = (currentVelocity.x + omega * change_x) * deltaTime;
  50. float temp_y = (currentVelocity.y + omega * change_y) * deltaTime;
  51. currentVelocity.x = (currentVelocity.x - omega * temp_x) * exp;
  52. currentVelocity.y = (currentVelocity.y - omega * temp_y) * exp;
  53. float output_x = target.x + (change_x + temp_x) * exp;
  54. float output_y = target.y + (change_y + temp_y) * exp;
  55. // Prevent overshooting
  56. float origMinusCurrent_x = originalTo.x - current.x;
  57. float origMinusCurrent_y = originalTo.y - current.y;
  58. float outMinusOrig_x = output_x - originalTo.x;
  59. float outMinusOrig_y = output_y - originalTo.y;
  60. if (origMinusCurrent_x * outMinusOrig_x + origMinusCurrent_y * outMinusOrig_y > 0)
  61. {
  62. output_x = originalTo.x;
  63. output_y = originalTo.y;
  64. currentVelocity.x = (output_x - originalTo.x) / deltaTime;
  65. currentVelocity.y = (output_y - originalTo.y) / deltaTime;
  66. }
  67. return Vec2(output_x, output_y);
  68. }
  69. void PyVec2::_register(VM* vm, PyObject* mod, PyObject* type){
  70. PY_STRUCT_LIKE(PyVec2)
  71. vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
  72. float x = CAST_F(args[1]);
  73. float y = CAST_F(args[2]);
  74. return vm->heap.gcnew<PyVec2>(PK_OBJ_GET(Type, args[0]), Vec2(x, y));
  75. });
  76. // @staticmethod
  77. vm->bind(type, "smooth_damp(current: vec2, target: vec2, current_velocity_: vec2, smooth_time: float, max_speed: float, delta_time: float) -> vec2", [](VM* vm, ArgsView args){
  78. Vec2 current = CAST(Vec2, args[0]);
  79. Vec2 target = CAST(Vec2, args[1]);
  80. PyVec2& current_velocity_ = CAST(PyVec2&, args[2]);
  81. float smooth_time = CAST_F(args[3]);
  82. float max_speed = CAST_F(args[4]);
  83. float delta_time = CAST_F(args[5]);
  84. Vec2 ret = SmoothDamp(current, target, current_velocity_, smooth_time, max_speed, delta_time);
  85. return VAR(ret);
  86. }, {}, BindType::STATICMETHOD);
  87. // @staticmethod
  88. vm->bind(type, "angle(__from: vec2, __to: vec2) -> float", [](VM* vm, ArgsView args){
  89. PyVec2 __from = CAST(PyVec2, args[0]);
  90. PyVec2 __to = CAST(PyVec2, args[1]);
  91. float val = atan2f(__to.y, __to.x) - atan2f(__from.y, __from.x);
  92. const float PI = 3.1415926535897932384f;
  93. if(val > PI) val -= 2*PI;
  94. if(val < -PI) val += 2*PI;
  95. return VAR(val);
  96. }, {}, BindType::STATICMETHOD);
  97. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  98. Vec2 self = _CAST(PyVec2&, obj);
  99. SStream ss;
  100. ss.setprecision(3);
  101. ss << "vec2(" << self.x << ", " << self.y << ")";
  102. return VAR(ss.str());
  103. });
  104. vm->bind_method<1>(type, "rotate", [](VM* vm, ArgsView args){
  105. Vec2 self = _CAST(PyVec2&, args[0]);
  106. float radian = CAST(f64, args[1]);
  107. return VAR_T(PyVec2, self.rotate(radian));
  108. });
  109. vm->bind_method<1>(type, "rotate_", [](VM* vm, ArgsView args){
  110. PyVec2& self = _CAST(PyVec2&, args[0]);
  111. float radian = CAST(f64, args[1]);
  112. self = self.rotate(radian);
  113. return vm->None;
  114. });
  115. PY_FIELD(PyVec2, "x", _, x)
  116. PY_FIELD(PyVec2, "y", _, y)
  117. BIND_VEC_VEC_OP(2, __add__, +)
  118. BIND_VEC_VEC_OP(2, __sub__, -)
  119. BIND_VEC_FLOAT_OP(2, __mul__, *)
  120. BIND_VEC_FLOAT_OP(2, __rmul__, *)
  121. BIND_VEC_FLOAT_OP(2, __truediv__, /)
  122. BIND_VEC_FUNCTION_1(2, dot)
  123. BIND_VEC_FUNCTION_1(2, cross)
  124. BIND_VEC_FUNCTION_1(2, copy_)
  125. BIND_VEC_FUNCTION_0(2, length)
  126. BIND_VEC_FUNCTION_0(2, length_squared)
  127. BIND_VEC_FUNCTION_0(2, normalize)
  128. BIND_VEC_FUNCTION_0(2, normalize_)
  129. }
  130. void PyVec3::_register(VM* vm, PyObject* mod, PyObject* type){
  131. PY_STRUCT_LIKE(PyVec3)
  132. vm->bind_constructor<4>(type, [](VM* vm, ArgsView args){
  133. float x = CAST_F(args[1]);
  134. float y = CAST_F(args[2]);
  135. float z = CAST_F(args[3]);
  136. return vm->heap.gcnew<PyVec3>(PK_OBJ_GET(Type, args[0]), Vec3(x, y, z));
  137. });
  138. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  139. Vec3 self = _CAST(PyVec3&, obj);
  140. SStream ss;
  141. ss.setprecision(3);
  142. ss << "vec3(" << self.x << ", " << self.y << ", " << self.z << ")";
  143. return VAR(ss.str());
  144. });
  145. PY_FIELD(PyVec3, "x", _, x)
  146. PY_FIELD(PyVec3, "y", _, y)
  147. PY_FIELD(PyVec3, "z", _, z)
  148. BIND_VEC_VEC_OP(3, __add__, +)
  149. BIND_VEC_VEC_OP(3, __sub__, -)
  150. BIND_VEC_FLOAT_OP(3, __mul__, *)
  151. BIND_VEC_FLOAT_OP(3, __rmul__, *)
  152. BIND_VEC_FLOAT_OP(3, __truediv__, /)
  153. BIND_VEC_FUNCTION_1(3, dot)
  154. BIND_VEC_FUNCTION_1(3, cross)
  155. BIND_VEC_FUNCTION_1(3, copy_)
  156. BIND_VEC_FUNCTION_0(3, length)
  157. BIND_VEC_FUNCTION_0(3, length_squared)
  158. BIND_VEC_FUNCTION_0(3, normalize)
  159. BIND_VEC_FUNCTION_0(3, normalize_)
  160. }
  161. void PyVec4::_register(VM* vm, PyObject* mod, PyObject* type){
  162. PY_STRUCT_LIKE(PyVec4)
  163. vm->bind_constructor<1+4>(type, [](VM* vm, ArgsView args){
  164. float x = CAST_F(args[1]);
  165. float y = CAST_F(args[2]);
  166. float z = CAST_F(args[3]);
  167. float w = CAST_F(args[4]);
  168. return vm->heap.gcnew<PyVec4>(PK_OBJ_GET(Type, args[0]), Vec4(x, y, z, w));
  169. });
  170. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  171. Vec4 self = _CAST(PyVec4&, obj);
  172. SStream ss;
  173. ss.setprecision(3);
  174. ss << "vec4(" << self.x << ", " << self.y << ", " << self.z << ", " << self.w << ")";
  175. return VAR(ss.str());
  176. });
  177. PY_FIELD(PyVec4, "x", _, x)
  178. PY_FIELD(PyVec4, "y", _, y)
  179. PY_FIELD(PyVec4, "z", _, z)
  180. PY_FIELD(PyVec4, "w", _, w)
  181. BIND_VEC_VEC_OP(4, __add__, +)
  182. BIND_VEC_VEC_OP(4, __sub__, -)
  183. BIND_VEC_FLOAT_OP(4, __mul__, *)
  184. BIND_VEC_FLOAT_OP(4, __rmul__, *)
  185. BIND_VEC_FLOAT_OP(4, __truediv__, /)
  186. BIND_VEC_FUNCTION_1(4, dot)
  187. BIND_VEC_FUNCTION_1(4, copy_)
  188. BIND_VEC_FUNCTION_0(4, length)
  189. BIND_VEC_FUNCTION_0(4, length_squared)
  190. BIND_VEC_FUNCTION_0(4, normalize)
  191. BIND_VEC_FUNCTION_0(4, normalize_)
  192. }
  193. #undef BIND_VEC_VEC_OP
  194. #undef BIND_VEC_FLOAT_OP
  195. #undef BIND_VEC_FUNCTION_0
  196. #undef BIND_VEC_FUNCTION_1
  197. void PyMat3x3::_register(VM* vm, PyObject* mod, PyObject* type){
  198. PY_STRUCT_LIKE(PyMat3x3)
  199. vm->bind_constructor<-1>(type, [](VM* vm, ArgsView args){
  200. if(args.size() == 1+0) return vm->heap.gcnew<PyMat3x3>(PK_OBJ_GET(Type, args[0]), Mat3x3::zeros());
  201. if(args.size() == 1+1){
  202. const List& list = CAST(List&, args[1]);
  203. if(list.size() != 9) vm->TypeError("Mat3x3.__new__ takes a list of 9 floats");
  204. Mat3x3 mat;
  205. for(int i=0; i<9; i++) mat.v[i] = CAST_F(list[i]);
  206. return vm->heap.gcnew<PyMat3x3>(PK_OBJ_GET(Type, args[0]), mat);
  207. }
  208. if(args.size() == 1+9){
  209. Mat3x3 mat;
  210. for(int i=0; i<9; i++) mat.v[i] = CAST_F(args[1+i]);
  211. return vm->heap.gcnew<PyMat3x3>(PK_OBJ_GET(Type, args[0]), mat);
  212. }
  213. vm->TypeError(_S("Mat3x3.__new__ takes 0 or 1 or 9 arguments, got ", args.size()-1));
  214. return vm->None;
  215. });
  216. vm->bind_method<1>(type, "copy_", [](VM* vm, ArgsView args){
  217. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  218. const PyMat3x3& other = CAST(PyMat3x3&, args[1]);
  219. self = other;
  220. return vm->None;
  221. });
  222. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  223. const PyMat3x3& self = _CAST(PyMat3x3&, obj);
  224. SStream ss;
  225. ss.setprecision(3);
  226. ss << "mat3x3([" << self._11 << ", " << self._12 << ", " << self._13 << ",\n";
  227. ss << " " << self._21 << ", " << self._22 << ", " << self._23 << ",\n";
  228. ss << " " << self._31 << ", " << self._32 << ", " << self._33 << "])";
  229. return VAR(ss.str());
  230. });
  231. vm->bind__getitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj, PyObject* index){
  232. PyMat3x3& self = _CAST(PyMat3x3&, obj);
  233. Tuple& t = CAST(Tuple&, index);
  234. if(t.size() != 2){
  235. vm->TypeError("Mat3x3.__getitem__ takes a tuple of 2 integers");
  236. }
  237. i64 i = CAST(i64, t[0]);
  238. i64 j = CAST(i64, t[1]);
  239. if(i < 0 || i >= 3 || j < 0 || j >= 3){
  240. vm->IndexError("index out of range");
  241. }
  242. return VAR(self.m[i][j]);
  243. });
  244. vm->bind__setitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj, PyObject* index, PyObject* value){
  245. PyMat3x3& self = _CAST(PyMat3x3&, obj);
  246. const Tuple& t = CAST(Tuple&, index);
  247. if(t.size() != 2){
  248. vm->TypeError("Mat3x3.__setitem__ takes a tuple of 2 integers");
  249. }
  250. i64 i = CAST(i64, t[0]);
  251. i64 j = CAST(i64, t[1]);
  252. if(i < 0 || i >= 3 || j < 0 || j >= 3){
  253. vm->IndexError("index out of range");
  254. }
  255. self.m[i][j] = CAST_F(value);
  256. });
  257. PY_FIELD(PyMat3x3, "_11", _, _11)
  258. PY_FIELD(PyMat3x3, "_12", _, _12)
  259. PY_FIELD(PyMat3x3, "_13", _, _13)
  260. PY_FIELD(PyMat3x3, "_21", _, _21)
  261. PY_FIELD(PyMat3x3, "_22", _, _22)
  262. PY_FIELD(PyMat3x3, "_23", _, _23)
  263. PY_FIELD(PyMat3x3, "_31", _, _31)
  264. PY_FIELD(PyMat3x3, "_32", _, _32)
  265. PY_FIELD(PyMat3x3, "_33", _, _33)
  266. vm->bind__add__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  267. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  268. PyMat3x3& other = CAST(PyMat3x3&, _1);
  269. return VAR_T(PyMat3x3, self + other);
  270. });
  271. vm->bind__sub__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  272. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  273. PyMat3x3& other = CAST(PyMat3x3&, _1);
  274. return VAR_T(PyMat3x3, self - other);
  275. });
  276. vm->bind__mul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  277. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  278. f64 other = CAST_F(_1);
  279. return VAR_T(PyMat3x3, self * other);
  280. });
  281. vm->bind_method<1>(type, "__rmul__", [](VM* vm, ArgsView args){
  282. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  283. f64 other = CAST_F(args[1]);
  284. return VAR_T(PyMat3x3, self * other);
  285. });
  286. vm->bind__truediv__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  287. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  288. f64 other = CAST_F(_1);
  289. return VAR_T(PyMat3x3, self / other);
  290. });
  291. vm->bind__matmul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  292. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  293. if(is_non_tagged_type(_1, PyMat3x3::_type(vm))){
  294. const PyMat3x3& other = _CAST(PyMat3x3&, _1);
  295. return VAR_T(PyMat3x3, self.matmul(other));
  296. }
  297. if(is_non_tagged_type(_1, PyVec3::_type(vm))){
  298. const PyVec3& other = _CAST(PyVec3&, _1);
  299. return VAR_T(PyVec3, self.matmul(other));
  300. }
  301. return vm->NotImplemented;
  302. });
  303. vm->bind(type, "matmul(self, other: mat3x3, out: mat3x3 = None)", [](VM* vm, ArgsView args){
  304. const PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  305. const PyMat3x3& other = CAST(PyMat3x3&, args[1]);
  306. if(args[2] == vm->None){
  307. return VAR_T(PyMat3x3, self.matmul(other));
  308. }else{
  309. PyMat3x3& out = CAST(PyMat3x3&, args[2]);
  310. out = self.matmul(other);
  311. return vm->None;
  312. }
  313. });
  314. vm->bind_method<0>(type, "determinant", [](VM* vm, ArgsView args){
  315. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  316. return VAR(self.determinant());
  317. });
  318. vm->bind_method<0>(type, "transpose", [](VM* vm, ArgsView args){
  319. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  320. return VAR_T(PyMat3x3, self.transpose());
  321. });
  322. vm->bind__invert__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  323. PyMat3x3& self = _CAST(PyMat3x3&, obj);
  324. Mat3x3 ret;
  325. bool ok = self.inverse(ret);
  326. if(!ok) vm->ValueError("matrix is not invertible");
  327. return VAR_T(PyMat3x3, ret);
  328. });
  329. vm->bind_method<0>(type, "invert", [](VM* vm, ArgsView args){
  330. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  331. Mat3x3 ret;
  332. bool ok = self.inverse(ret);
  333. if(!ok) vm->ValueError("matrix is not invertible");
  334. return VAR_T(PyMat3x3, ret);
  335. });
  336. vm->bind_method<0>(type, "invert_", [](VM* vm, ArgsView args){
  337. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  338. Mat3x3 ret;
  339. bool ok = self.inverse(ret);
  340. if(!ok) vm->ValueError("matrix is not invertible");
  341. self = ret;
  342. return vm->None;
  343. });
  344. vm->bind_method<0>(type, "transpose_", [](VM* vm, ArgsView args){
  345. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  346. self = self.transpose();
  347. return vm->None;
  348. });
  349. // @staticmethod
  350. vm->bind(type, "zeros()", [](VM* vm, ArgsView args){
  351. PK_UNUSED(args);
  352. return VAR_T(PyMat3x3, Mat3x3::zeros());
  353. }, {}, BindType::STATICMETHOD);
  354. // @staticmethod
  355. vm->bind(type, "ones()", [](VM* vm, ArgsView args){
  356. PK_UNUSED(args);
  357. return VAR_T(PyMat3x3, Mat3x3::ones());
  358. }, {}, BindType::STATICMETHOD);
  359. // @staticmethod
  360. vm->bind(type, "identity()", [](VM* vm, ArgsView args){
  361. PK_UNUSED(args);
  362. return VAR_T(PyMat3x3, Mat3x3::identity());
  363. }, {}, BindType::STATICMETHOD);
  364. /*************** affine transformations ***************/
  365. // @staticmethod
  366. vm->bind(type, "trs(t: vec2, r: float, s: vec2)", [](VM* vm, ArgsView args){
  367. Vec2 t = CAST(Vec2, args[0]);
  368. f64 r = CAST_F(args[1]);
  369. Vec2 s = CAST(Vec2, args[2]);
  370. return VAR_T(PyMat3x3, Mat3x3::trs(t, r, s));
  371. }, {}, BindType::STATICMETHOD);
  372. vm->bind(type, "copy_trs_(self, t: vec2, r: float, s: vec2)", [](VM* vm, ArgsView args){
  373. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  374. Vec2 t = CAST(Vec2, args[1]);
  375. f64 r = CAST_F(args[2]);
  376. Vec2 s = CAST(Vec2, args[3]);
  377. self = Mat3x3::trs(t, r, s);
  378. return vm->None;
  379. });
  380. vm->bind_method<0>(type, "is_affine", [](VM* vm, ArgsView args){
  381. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  382. return VAR(self.is_affine());
  383. });
  384. vm->bind_method<0>(type, "_t", [](VM* vm, ArgsView args){
  385. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  386. return VAR_T(PyVec2, self._t());
  387. });
  388. vm->bind_method<0>(type, "_r", [](VM* vm, ArgsView args){
  389. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  390. return VAR(self._r());
  391. });
  392. vm->bind_method<0>(type, "_s", [](VM* vm, ArgsView args){
  393. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  394. return VAR_T(PyVec2, self._s());
  395. });
  396. vm->bind_method<1>(type, "transform_point", [](VM* vm, ArgsView args){
  397. const PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  398. Vec2 v = CAST(Vec2, args[1]);
  399. Vec2 res = Vec2(self._11 * v.x + self._12 * v.y + self._13, self._21 * v.x + self._22 * v.y + self._23);
  400. return VAR_T(PyVec2, res);
  401. });
  402. vm->bind_method<1>(type, "transform_vector", [](VM* vm, ArgsView args){
  403. const PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  404. Vec2 v = CAST(Vec2, args[1]);
  405. Vec2 res = Vec2(self._11 * v.x + self._12 * v.y, self._21 * v.x + self._22 * v.y);
  406. return VAR_T(PyVec2, res);
  407. });
  408. }
  409. void add_module_linalg(VM* vm){
  410. PyObject* linalg = vm->new_module("linalg");
  411. PyVec2::register_class(vm, linalg);
  412. PyVec3::register_class(vm, linalg);
  413. PyVec4::register_class(vm, linalg);
  414. PyMat3x3::register_class(vm, linalg);
  415. PyObject* float_p = vm->_modules["c"]->attr("float_p");
  416. linalg->attr().set("vec2_p", float_p);
  417. linalg->attr().set("vec3_p", float_p);
  418. linalg->attr().set("vec4_p", float_p);
  419. linalg->attr().set("mat3x3_p", float_p);
  420. }
  421. /////////////// mat3x3 ///////////////
  422. Mat3x3::Mat3x3() {}
  423. Mat3x3::Mat3x3(float _11, float _12, float _13,
  424. float _21, float _22, float _23,
  425. float _31, float _32, float _33)
  426. : _11(_11), _12(_12), _13(_13)
  427. , _21(_21), _22(_22), _23(_23)
  428. , _31(_31), _32(_32), _33(_33) {}
  429. Mat3x3 Mat3x3::zeros(){
  430. return Mat3x3(0, 0, 0, 0, 0, 0, 0, 0, 0);
  431. }
  432. Mat3x3 Mat3x3::ones(){
  433. return Mat3x3(1, 1, 1, 1, 1, 1, 1, 1, 1);
  434. }
  435. Mat3x3 Mat3x3::identity(){
  436. return Mat3x3(1, 0, 0, 0, 1, 0, 0, 0, 1);
  437. }
  438. Mat3x3 Mat3x3::operator+(const Mat3x3& other) const{
  439. Mat3x3 ret;
  440. for (int i=0; i<9; ++i) ret.v[i] = v[i] + other.v[i];
  441. return ret;
  442. }
  443. Mat3x3 Mat3x3::operator-(const Mat3x3& other) const{
  444. Mat3x3 ret;
  445. for (int i=0; i<9; ++i) ret.v[i] = v[i] - other.v[i];
  446. return ret;
  447. }
  448. Mat3x3 Mat3x3::operator*(float scalar) const{
  449. Mat3x3 ret;
  450. for (int i=0; i<9; ++i) ret.v[i] = v[i] * scalar;
  451. return ret;
  452. }
  453. Mat3x3 Mat3x3::operator/(float scalar) const{
  454. Mat3x3 ret;
  455. for (int i=0; i<9; ++i) ret.v[i] = v[i] / scalar;
  456. return ret;
  457. }
  458. bool Mat3x3::operator==(const Mat3x3& other) const{
  459. for (int i=0; i<9; ++i){
  460. if (!isclose(v[i], other.v[i])) return false;
  461. }
  462. return true;
  463. }
  464. bool Mat3x3::operator!=(const Mat3x3& other) const{
  465. for (int i=0; i<9; ++i){
  466. if (!isclose(v[i], other.v[i])) return true;
  467. }
  468. return false;
  469. }
  470. Mat3x3 Mat3x3::matmul(const Mat3x3& other) const{
  471. Mat3x3 out;
  472. out._11 = _11 * other._11 + _12 * other._21 + _13 * other._31;
  473. out._12 = _11 * other._12 + _12 * other._22 + _13 * other._32;
  474. out._13 = _11 * other._13 + _12 * other._23 + _13 * other._33;
  475. out._21 = _21 * other._11 + _22 * other._21 + _23 * other._31;
  476. out._22 = _21 * other._12 + _22 * other._22 + _23 * other._32;
  477. out._23 = _21 * other._13 + _22 * other._23 + _23 * other._33;
  478. out._31 = _31 * other._11 + _32 * other._21 + _33 * other._31;
  479. out._32 = _31 * other._12 + _32 * other._22 + _33 * other._32;
  480. out._33 = _31 * other._13 + _32 * other._23 + _33 * other._33;
  481. return out;
  482. }
  483. Vec3 Mat3x3::matmul(const Vec3& other) const{
  484. Vec3 out;
  485. out.x = _11 * other.x + _12 * other.y + _13 * other.z;
  486. out.y = _21 * other.x + _22 * other.y + _23 * other.z;
  487. out.z = _31 * other.x + _32 * other.y + _33 * other.z;
  488. return out;
  489. }
  490. float Mat3x3::determinant() const{
  491. return _11 * _22 * _33 + _12 * _23 * _31 + _13 * _21 * _32
  492. - _11 * _23 * _32 - _12 * _21 * _33 - _13 * _22 * _31;
  493. }
  494. Mat3x3 Mat3x3::transpose() const{
  495. Mat3x3 ret;
  496. ret._11 = _11; ret._12 = _21; ret._13 = _31;
  497. ret._21 = _12; ret._22 = _22; ret._23 = _32;
  498. ret._31 = _13; ret._32 = _23; ret._33 = _33;
  499. return ret;
  500. }
  501. bool Mat3x3::inverse(Mat3x3& out) const{
  502. float det = determinant();
  503. if (isclose(det, 0)) return false;
  504. float inv_det = 1.0f / det;
  505. out._11 = (_22 * _33 - _23 * _32) * inv_det;
  506. out._12 = (_13 * _32 - _12 * _33) * inv_det;
  507. out._13 = (_12 * _23 - _13 * _22) * inv_det;
  508. out._21 = (_23 * _31 - _21 * _33) * inv_det;
  509. out._22 = (_11 * _33 - _13 * _31) * inv_det;
  510. out._23 = (_13 * _21 - _11 * _23) * inv_det;
  511. out._31 = (_21 * _32 - _22 * _31) * inv_det;
  512. out._32 = (_12 * _31 - _11 * _32) * inv_det;
  513. out._33 = (_11 * _22 - _12 * _21) * inv_det;
  514. return true;
  515. }
  516. Mat3x3 Mat3x3::trs(Vec2 t, float radian, Vec2 s){
  517. float cr = cosf(radian);
  518. float sr = sinf(radian);
  519. return Mat3x3(s.x * cr, -s.y * sr, t.x,
  520. s.x * sr, s.y * cr, t.y,
  521. 0.0f, 0.0f, 1.0f);
  522. }
  523. bool Mat3x3::is_affine() const{
  524. float det = _11 * _22 - _12 * _21;
  525. if(isclose(det, 0)) return false;
  526. return _31 == 0.0f && _32 == 0.0f && _33 == 1.0f;
  527. }
  528. Vec2 Mat3x3::_t() const { return Vec2(_13, _23); }
  529. float Mat3x3::_r() const { return atan2f(_21, _11); }
  530. Vec2 Mat3x3::_s() const {
  531. return Vec2(
  532. sqrtf(_11 * _11 + _21 * _21),
  533. sqrtf(_12 * _12 + _22 * _22)
  534. );
  535. }
  536. } // namespace pkpy