linalg.cpp 16 KB

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