linalg.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #include "pocketpy/linalg.h"
  2. namespace pkpy{
  3. #define BIND_VEC_ADDR(D) \
  4. vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){ \
  5. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  6. return VAR_T(VoidP, &self.x); \
  7. });
  8. #define BIND_VEC_VEC_OP(D, name, op) \
  9. vm->bind_method<1>(type, #name, [](VM* vm, ArgsView args){ \
  10. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  11. PyVec##D& other = CAST(PyVec##D&, args[1]); \
  12. return VAR(self op other); \
  13. });
  14. #define BIND_VEC_FLOAT_OP(D, name, op) \
  15. vm->bind_method<1>(type, #name, [](VM* vm, ArgsView args){ \
  16. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  17. f64 other = CAST(f64, args[1]); \
  18. return VAR(self op other); \
  19. });
  20. #define BIND_VEC_FUNCTION_0(D, name) \
  21. vm->bind_method<0>(type, #name, [](VM* vm, ArgsView args){ \
  22. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  23. return VAR(self.name()); \
  24. });
  25. #define BIND_VEC_FUNCTION_1(D, name) \
  26. vm->bind_method<0>(type, #name, [](VM* vm, ArgsView args){ \
  27. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  28. PyVec##D& other = CAST(PyVec##D&, args[1]); \
  29. return VAR(self.name(other)); \
  30. });
  31. #define BIND_VEC_FIELD(D, name) \
  32. vm->bind_property(type, #name, \
  33. [](VM* vm, ArgsView args){ \
  34. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  35. return VAR(self.name); \
  36. }, [](VM* vm, ArgsView args){ \
  37. PyVec##D& self = _CAST(PyVec##D&, args[0]); \
  38. self.name = CAST(f64, args[1]); \
  39. return vm->None; \
  40. });
  41. void PyVec2::_register(VM* vm, PyObject* mod, PyObject* type){
  42. vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
  43. float x = CAST_F(args[1]);
  44. float y = CAST_F(args[2]);
  45. return VAR(Vec2(x, y));
  46. });
  47. vm->bind_method<0>(type, "__getnewargs__", [](VM* vm, ArgsView args){
  48. PyVec2& self = _CAST(PyVec2&, args[0]);
  49. return VAR(Tuple({ VAR(self.x), VAR(self.y) }));
  50. });
  51. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  52. PyVec2& self = _CAST(PyVec2&, obj);
  53. std::stringstream ss;
  54. ss << "vec2(" << self.x << ", " << self.y << ")";
  55. return VAR(ss.str());
  56. });
  57. vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){
  58. PyVec2& self = _CAST(PyVec2&, args[0]);
  59. return VAR_T(PyVec2, self);
  60. });
  61. vm->bind_method<1>(type, "rotate", [](VM* vm, ArgsView args){
  62. Vec2 self = _CAST(PyVec2&, args[0]);
  63. float radian = CAST(f64, args[1]);
  64. float cr = cosf(radian);
  65. float sr = sinf(radian);
  66. Mat3x3 rotate(cr, -sr, 0.0f,
  67. sr, cr, 0.0f,
  68. 0.0f, 0.0f, 1.0f);
  69. self = rotate.transform_vector(self);
  70. return VAR(self);
  71. });
  72. BIND_VEC_ADDR(2)
  73. BIND_VEC_VEC_OP(2, __add__, +)
  74. BIND_VEC_VEC_OP(2, __sub__, -)
  75. BIND_VEC_FLOAT_OP(2, __mul__, *)
  76. BIND_VEC_FLOAT_OP(2, __rmul__, *)
  77. BIND_VEC_FLOAT_OP(2, __truediv__, /)
  78. BIND_VEC_VEC_OP(2, __eq__, ==)
  79. BIND_VEC_FIELD(2, x)
  80. BIND_VEC_FIELD(2, y)
  81. BIND_VEC_FUNCTION_1(2, dot)
  82. BIND_VEC_FUNCTION_1(2, cross)
  83. BIND_VEC_FUNCTION_0(2, length)
  84. BIND_VEC_FUNCTION_0(2, length_squared)
  85. BIND_VEC_FUNCTION_0(2, normalize)
  86. }
  87. void PyVec3::_register(VM* vm, PyObject* mod, PyObject* type){
  88. vm->bind_constructor<4>(type, [](VM* vm, ArgsView args){
  89. float x = CAST_F(args[1]);
  90. float y = CAST_F(args[2]);
  91. float z = CAST_F(args[3]);
  92. return VAR(Vec3(x, y, z));
  93. });
  94. vm->bind_method<0>(type, "__getnewargs__", [](VM* vm, ArgsView args){
  95. PyVec3& self = _CAST(PyVec3&, args[0]);
  96. return VAR(Tuple({ VAR(self.x), VAR(self.y), VAR(self.z) }));
  97. });
  98. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  99. PyVec3& self = _CAST(PyVec3&, obj);
  100. std::stringstream ss;
  101. ss << "vec3(" << self.x << ", " << self.y << ", " << self.z << ")";
  102. return VAR(ss.str());
  103. });
  104. vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){
  105. PyVec3& self = _CAST(PyVec3&, args[0]);
  106. return VAR_T(PyVec3, self);
  107. });
  108. BIND_VEC_ADDR(3)
  109. BIND_VEC_VEC_OP(3, __add__, +)
  110. BIND_VEC_VEC_OP(3, __sub__, -)
  111. BIND_VEC_FLOAT_OP(3, __mul__, *)
  112. BIND_VEC_FLOAT_OP(3, __rmul__, *)
  113. BIND_VEC_FLOAT_OP(3, __truediv__, /)
  114. BIND_VEC_VEC_OP(3, __eq__, ==)
  115. BIND_VEC_FIELD(3, x)
  116. BIND_VEC_FIELD(3, y)
  117. BIND_VEC_FIELD(3, z)
  118. BIND_VEC_FUNCTION_1(3, dot)
  119. BIND_VEC_FUNCTION_1(3, cross)
  120. BIND_VEC_FUNCTION_0(3, length)
  121. BIND_VEC_FUNCTION_0(3, length_squared)
  122. BIND_VEC_FUNCTION_0(3, normalize)
  123. }
  124. void PyVec4::_register(VM* vm, PyObject* mod, PyObject* type){
  125. vm->bind_constructor<1+4>(type, [](VM* vm, ArgsView args){
  126. float x = CAST_F(args[1]);
  127. float y = CAST_F(args[2]);
  128. float z = CAST_F(args[3]);
  129. float w = CAST_F(args[4]);
  130. return VAR(Vec4(x, y, z, w));
  131. });
  132. vm->bind_method<0>(type, "__getnewargs__", [](VM* vm, ArgsView args){
  133. PyVec4& self = _CAST(PyVec4&, args[0]);
  134. return VAR(Tuple({ VAR(self.x), VAR(self.y), VAR(self.z), VAR(self.w) }));
  135. });
  136. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  137. PyVec4& self = _CAST(PyVec4&, obj);
  138. std::stringstream ss;
  139. ss << "vec4(" << self.x << ", " << self.y << ", " << self.z << ", " << self.w << ")";
  140. return VAR(ss.str());
  141. });
  142. vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){
  143. PyVec4& self = _CAST(PyVec4&, args[0]);
  144. return VAR_T(PyVec4, self);
  145. });
  146. BIND_VEC_ADDR(4)
  147. BIND_VEC_VEC_OP(4, __add__, +)
  148. BIND_VEC_VEC_OP(4, __sub__, -)
  149. BIND_VEC_FLOAT_OP(4, __mul__, *)
  150. BIND_VEC_FLOAT_OP(4, __rmul__, *)
  151. BIND_VEC_FLOAT_OP(4, __truediv__, /)
  152. BIND_VEC_VEC_OP(4, __eq__, ==)
  153. BIND_VEC_FIELD(4, x)
  154. BIND_VEC_FIELD(4, y)
  155. BIND_VEC_FIELD(4, z)
  156. BIND_VEC_FIELD(4, w)
  157. BIND_VEC_FUNCTION_1(4, dot)
  158. BIND_VEC_FUNCTION_0(4, length)
  159. BIND_VEC_FUNCTION_0(4, length_squared)
  160. BIND_VEC_FUNCTION_0(4, normalize)
  161. }
  162. #undef BIND_VEC_ADDR
  163. #undef BIND_VEC_VEC_OP
  164. #undef BIND_VEC_FLOAT_OP
  165. #undef BIND_VEC_FIELD
  166. #undef BIND_VEC_FUNCTION_0
  167. #undef BIND_VEC_FUNCTION_1
  168. void PyMat3x3::_register(VM* vm, PyObject* mod, PyObject* type){
  169. vm->bind_constructor<-1>(type, [](VM* vm, ArgsView args){
  170. if(args.size() == 1+0) return VAR_T(PyMat3x3, Mat3x3::zeros());
  171. if(args.size() == 1+9){
  172. Mat3x3 mat;
  173. for(int i=0; i<9; i++) mat.v[i] = CAST_F(args[1+i]);
  174. return VAR_T(PyMat3x3, mat);
  175. }
  176. if(args.size() == 1+1){
  177. List& a = CAST(List&, args[1]);
  178. if(a.size() != 3) vm->ValueError("Mat3x3.__new__ takes 3x3 list");
  179. Mat3x3 mat;
  180. for(int i=0; i<3; i++){
  181. List& b = CAST(List&, a[i]);
  182. if(b.size() != 3) vm->ValueError("Mat3x3.__new__ takes 3x3 list");
  183. for(int j=0; j<3; j++){
  184. mat.m[i][j] = CAST_F(b[j]);
  185. }
  186. }
  187. return VAR_T(PyMat3x3, mat);
  188. }
  189. vm->TypeError("Mat3x3.__new__ takes 0 or 1 arguments");
  190. return vm->None;
  191. });
  192. vm->bind_method<0>(type, "__getnewargs__", [](VM* vm, ArgsView args){
  193. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  194. Tuple t(9);
  195. for(int i=0; i<9; i++) t[i] = VAR(self.v[i]);
  196. return VAR(std::move(t));
  197. });
  198. #define METHOD_PROXY_NONE(name) \
  199. vm->bind_method<0>(type, #name, [](VM* vm, ArgsView args){ \
  200. PyMat3x3& self = _CAST(PyMat3x3&, args[0]); \
  201. self.name(); \
  202. return vm->None; \
  203. });
  204. METHOD_PROXY_NONE(set_zeros)
  205. METHOD_PROXY_NONE(set_ones)
  206. METHOD_PROXY_NONE(set_identity)
  207. #undef METHOD_PROXY_NONE
  208. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  209. PyMat3x3& self = _CAST(PyMat3x3&, obj);
  210. std::stringstream ss;
  211. ss << std::fixed << std::setprecision(4);
  212. ss << "mat3x3([[" << self._11 << ", " << self._12 << ", " << self._13 << "],\n";
  213. ss << " [" << self._21 << ", " << self._22 << ", " << self._23 << "],\n";
  214. ss << " [" << self._31 << ", " << self._32 << ", " << self._33 << "]])";
  215. return VAR(ss.str());
  216. });
  217. vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){
  218. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  219. return VAR_T(PyMat3x3, self);
  220. });
  221. vm->bind__getitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj, PyObject* index){
  222. PyMat3x3& self = _CAST(PyMat3x3&, obj);
  223. Tuple& t = CAST(Tuple&, index);
  224. if(t.size() != 2){
  225. vm->TypeError("Mat3x3.__getitem__ takes a tuple of 2 integers");
  226. return vm->None;
  227. }
  228. i64 i = CAST(i64, t[0]);
  229. i64 j = CAST(i64, t[1]);
  230. if(i < 0 || i >= 3 || j < 0 || j >= 3){
  231. vm->IndexError("index out of range");
  232. return vm->None;
  233. }
  234. return VAR(self.m[i][j]);
  235. });
  236. vm->bind__setitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj, PyObject* index, PyObject* value){
  237. PyMat3x3& self = _CAST(PyMat3x3&, obj);
  238. Tuple& t = CAST(Tuple&, index);
  239. if(t.size() != 2){
  240. vm->TypeError("Mat3x3.__setitem__ takes a tuple of 2 integers");
  241. return;
  242. }
  243. i64 i = CAST(i64, t[0]);
  244. i64 j = CAST(i64, t[1]);
  245. if(i < 0 || i >= 3 || j < 0 || j >= 3){
  246. vm->IndexError("index out of range");
  247. return;
  248. }
  249. self.m[i][j] = CAST_F(value);
  250. });
  251. #define PROPERTY_FIELD(field) \
  252. vm->bind_property(type, #field ": float", \
  253. [](VM* vm, ArgsView args){ \
  254. PyMat3x3& self = _CAST(PyMat3x3&, args[0]); \
  255. return VAR(self.field); \
  256. }, [](VM* vm, ArgsView args){ \
  257. PyMat3x3& self = _CAST(PyMat3x3&, args[0]); \
  258. self.field = CAST(f64, args[1]); \
  259. return vm->None; \
  260. });
  261. PROPERTY_FIELD(_11)
  262. PROPERTY_FIELD(_12)
  263. PROPERTY_FIELD(_13)
  264. PROPERTY_FIELD(_21)
  265. PROPERTY_FIELD(_22)
  266. PROPERTY_FIELD(_23)
  267. PROPERTY_FIELD(_31)
  268. PROPERTY_FIELD(_32)
  269. PROPERTY_FIELD(_33)
  270. #undef PROPERTY_FIELD
  271. vm->bind__add__(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__sub__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  277. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  278. PyMat3x3& other = CAST(PyMat3x3&, _1);
  279. return VAR_T(PyMat3x3, self - other);
  280. });
  281. vm->bind__mul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  282. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  283. f64 other = CAST_F(_1);
  284. return VAR_T(PyMat3x3, self * other);
  285. });
  286. vm->bind_method<1>(type, "__rmul__", [](VM* vm, ArgsView args){
  287. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  288. f64 other = CAST_F(args[1]);
  289. return VAR_T(PyMat3x3, self * other);
  290. });
  291. vm->bind__truediv__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  292. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  293. f64 other = CAST_F(_1);
  294. return VAR_T(PyMat3x3, self / other);
  295. });
  296. vm->bind__matmul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  297. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  298. if(is_non_tagged_type(_1, PyMat3x3::_type(vm))){
  299. PyMat3x3& other = _CAST(PyMat3x3&, _1);
  300. return VAR_T(PyMat3x3, self.matmul(other));
  301. }
  302. if(is_non_tagged_type(_1, PyVec3::_type(vm))){
  303. PyVec3& other = _CAST(PyVec3&, _1);
  304. return VAR_T(PyVec3, self.matmul(other));
  305. }
  306. return vm->NotImplemented;
  307. });
  308. vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){
  309. PyMat3x3& self = _CAST(PyMat3x3&, _0);
  310. if(is_non_tagged_type(_1, PyMat3x3::_type(vm))){
  311. PyMat3x3& other = _CAST(PyMat3x3&, _1);
  312. return VAR(self == other);
  313. }
  314. return vm->NotImplemented;
  315. });
  316. vm->bind_method<0>(type, "determinant", [](VM* vm, ArgsView args){
  317. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  318. return VAR(self.determinant());
  319. });
  320. vm->bind_method<0>(type, "transpose", [](VM* vm, ArgsView args){
  321. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  322. return VAR_T(PyMat3x3, self.transpose());
  323. });
  324. vm->bind__invert__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  325. PyMat3x3& self = _CAST(PyMat3x3&, obj);
  326. Mat3x3 ret;
  327. bool ok = self.inverse(ret);
  328. if(!ok) vm->ValueError("matrix is not invertible");
  329. return VAR_T(PyMat3x3, ret);
  330. });
  331. vm->bind_func<0>(type, "zeros", [](VM* vm, ArgsView args){
  332. PK_UNUSED(args);
  333. return VAR_T(PyMat3x3, Mat3x3::zeros());
  334. });
  335. vm->bind_func<0>(type, "ones", [](VM* vm, ArgsView args){
  336. PK_UNUSED(args);
  337. return VAR_T(PyMat3x3, Mat3x3::ones());
  338. });
  339. vm->bind_func<0>(type, "identity", [](VM* vm, ArgsView args){
  340. PK_UNUSED(args);
  341. return VAR_T(PyMat3x3, Mat3x3::identity());
  342. });
  343. /*************** affine transformations ***************/
  344. vm->bind_func<3>(type, "trs", [](VM* vm, ArgsView args){
  345. PyVec2& t = CAST(PyVec2&, args[0]);
  346. f64 r = CAST_F(args[1]);
  347. PyVec2& s = CAST(PyVec2&, args[2]);
  348. return VAR_T(PyMat3x3, Mat3x3::trs(t, r, s));
  349. });
  350. vm->bind_method<0>(type, "is_affine", [](VM* vm, ArgsView args){
  351. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  352. return VAR(self.is_affine());
  353. });
  354. vm->bind_method<0>(type, "translation", [](VM* vm, ArgsView args){
  355. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  356. return VAR_T(PyVec2, self.translation());
  357. });
  358. vm->bind_method<0>(type, "rotation", [](VM* vm, ArgsView args){
  359. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  360. return VAR(self.rotation());
  361. });
  362. vm->bind_method<0>(type, "scale", [](VM* vm, ArgsView args){
  363. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  364. return VAR_T(PyVec2, self.scale());
  365. });
  366. vm->bind_method<1>(type, "transform_point", [](VM* vm, ArgsView args){
  367. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  368. PyVec2& v = CAST(PyVec2&, args[1]);
  369. return VAR_T(PyVec2, self.transform_point(v));
  370. });
  371. vm->bind_method<1>(type, "transform_vector", [](VM* vm, ArgsView args){
  372. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  373. PyVec2& v = CAST(PyVec2&, args[1]);
  374. return VAR_T(PyVec2, self.transform_vector(v));
  375. });
  376. }
  377. } // namespace pkpy