linalg.cpp 29 KB

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