linalg.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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(type, "angle(__from: vec2, __to: vec2) -> float", [](VM* vm, ArgsView args){
  48. PyVec2 __from = CAST(PyVec2, args[0]);
  49. PyVec2 __to = CAST(PyVec2, args[1]);
  50. float val = atan2f(__to.y, __to.x) - atan2f(__from.y, __from.x);
  51. const float PI = 3.1415926535897932384f;
  52. if(val > PI) val -= 2*PI;
  53. if(val < -PI) val += 2*PI;
  54. return VAR(val);
  55. });
  56. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  57. PyVec2& self = _CAST(PyVec2&, obj);
  58. std::stringstream ss;
  59. ss << std::fixed << std::setprecision(3);
  60. ss << "vec2(" << self.x << ", " << self.y << ")";
  61. return VAR(ss.str());
  62. });
  63. vm->bind_method<1>(type, "rotate", [](VM* vm, ArgsView args){
  64. Vec2 self = _CAST(PyVec2&, args[0]);
  65. float radian = CAST(f64, args[1]);
  66. float cr = cosf(radian);
  67. float sr = sinf(radian);
  68. Mat3x3 rotate(cr, -sr, 0.0f,
  69. sr, cr, 0.0f,
  70. 0.0f, 0.0f, 1.0f);
  71. self = rotate.transform_vector(self);
  72. return VAR(self);
  73. });
  74. vm->bind_method<1>(type, "rotate_", [](VM* vm, ArgsView args){
  75. Vec2& self = _CAST(PyVec2&, args[0]);
  76. float radian = CAST(f64, args[1]);
  77. float cr = cosf(radian);
  78. float sr = sinf(radian);
  79. Mat3x3 rotate(cr, -sr, 0.0f,
  80. sr, cr, 0.0f,
  81. 0.0f, 0.0f, 1.0f);
  82. self = rotate.transform_vector(self);
  83. return vm->None;
  84. });
  85. BIND_VEC_VEC_OP(2, __add__, +)
  86. BIND_VEC_VEC_OP(2, __sub__, -)
  87. BIND_VEC_FLOAT_OP(2, __mul__, *)
  88. BIND_VEC_FLOAT_OP(2, __rmul__, *)
  89. BIND_VEC_FLOAT_OP(2, __truediv__, /)
  90. BIND_VEC_FIELD(2, x)
  91. BIND_VEC_FIELD(2, y)
  92. BIND_VEC_FUNCTION_1(2, dot)
  93. BIND_VEC_FUNCTION_1(2, cross)
  94. BIND_VEC_FUNCTION_0(2, length)
  95. BIND_VEC_FUNCTION_0(2, length_squared)
  96. BIND_VEC_FUNCTION_0(2, normalize)
  97. }
  98. void PyVec3::_register(VM* vm, PyObject* mod, PyObject* type){
  99. PY_STRUCT_LIKE(PyVec3)
  100. vm->bind_constructor<4>(type, [](VM* vm, ArgsView args){
  101. float x = CAST_F(args[1]);
  102. float y = CAST_F(args[2]);
  103. float z = CAST_F(args[3]);
  104. return VAR(Vec3(x, y, z));
  105. });
  106. vm->bind_method<0>(type, "__getnewargs__", [](VM* vm, ArgsView args){
  107. PyVec3& self = _CAST(PyVec3&, args[0]);
  108. return VAR(Tuple({ VAR(self.x), VAR(self.y), VAR(self.z) }));
  109. });
  110. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  111. PyVec3& self = _CAST(PyVec3&, obj);
  112. std::stringstream ss;
  113. ss << std::fixed << std::setprecision(3);
  114. ss << "vec3(" << self.x << ", " << self.y << ", " << self.z << ")";
  115. return VAR(ss.str());
  116. });
  117. BIND_VEC_VEC_OP(3, __add__, +)
  118. BIND_VEC_VEC_OP(3, __sub__, -)
  119. BIND_VEC_FLOAT_OP(3, __mul__, *)
  120. BIND_VEC_FLOAT_OP(3, __rmul__, *)
  121. BIND_VEC_FLOAT_OP(3, __truediv__, /)
  122. BIND_VEC_FIELD(3, x)
  123. BIND_VEC_FIELD(3, y)
  124. BIND_VEC_FIELD(3, z)
  125. BIND_VEC_FUNCTION_1(3, dot)
  126. BIND_VEC_FUNCTION_1(3, cross)
  127. BIND_VEC_FUNCTION_0(3, length)
  128. BIND_VEC_FUNCTION_0(3, length_squared)
  129. BIND_VEC_FUNCTION_0(3, normalize)
  130. }
  131. void PyVec4::_register(VM* vm, PyObject* mod, PyObject* type){
  132. PY_STRUCT_LIKE(PyVec4)
  133. vm->bind_constructor<1+4>(type, [](VM* vm, ArgsView args){
  134. float x = CAST_F(args[1]);
  135. float y = CAST_F(args[2]);
  136. float z = CAST_F(args[3]);
  137. float w = CAST_F(args[4]);
  138. return VAR(Vec4(x, y, z, w));
  139. });
  140. vm->bind_method<0>(type, "__getnewargs__", [](VM* vm, ArgsView args){
  141. PyVec4& self = _CAST(PyVec4&, args[0]);
  142. return VAR(Tuple({ VAR(self.x), VAR(self.y), VAR(self.z), VAR(self.w) }));
  143. });
  144. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  145. PyVec4& self = _CAST(PyVec4&, obj);
  146. std::stringstream ss;
  147. ss << std::fixed << std::setprecision(3);
  148. ss << "vec4(" << self.x << ", " << self.y << ", " << self.z << ", " << self.w << ")";
  149. return VAR(ss.str());
  150. });
  151. BIND_VEC_VEC_OP(4, __add__, +)
  152. BIND_VEC_VEC_OP(4, __sub__, -)
  153. BIND_VEC_FLOAT_OP(4, __mul__, *)
  154. BIND_VEC_FLOAT_OP(4, __rmul__, *)
  155. BIND_VEC_FLOAT_OP(4, __truediv__, /)
  156. BIND_VEC_FIELD(4, x)
  157. BIND_VEC_FIELD(4, y)
  158. BIND_VEC_FIELD(4, z)
  159. BIND_VEC_FIELD(4, w)
  160. BIND_VEC_FUNCTION_1(4, dot)
  161. BIND_VEC_FUNCTION_0(4, length)
  162. BIND_VEC_FUNCTION_0(4, length_squared)
  163. BIND_VEC_FUNCTION_0(4, normalize)
  164. }
  165. #undef BIND_VEC_ADDR
  166. #undef BIND_VEC_VEC_OP
  167. #undef BIND_VEC_FLOAT_OP
  168. #undef BIND_VEC_FIELD
  169. #undef BIND_VEC_FUNCTION_0
  170. #undef BIND_VEC_FUNCTION_1
  171. void PyMat3x3::_register(VM* vm, PyObject* mod, PyObject* type){
  172. PY_STRUCT_LIKE(PyMat3x3)
  173. vm->bind_constructor<-1>(type, [](VM* vm, ArgsView args){
  174. if(args.size() == 1+0) return VAR_T(PyMat3x3, Mat3x3::zeros());
  175. if(args.size() == 1+9){
  176. Mat3x3 mat;
  177. for(int i=0; i<9; i++) mat.v[i] = CAST_F(args[1+i]);
  178. return VAR_T(PyMat3x3, mat);
  179. }
  180. if(args.size() == 1+1){
  181. List& a = CAST(List&, args[1]);
  182. if(a.size() != 3) vm->ValueError("Mat3x3.__new__ takes 3x3 list");
  183. Mat3x3 mat;
  184. for(int i=0; i<3; i++){
  185. List& b = CAST(List&, a[i]);
  186. if(b.size() != 3) vm->ValueError("Mat3x3.__new__ takes 3x3 list");
  187. for(int j=0; j<3; j++){
  188. mat.m[i][j] = CAST_F(b[j]);
  189. }
  190. }
  191. return VAR_T(PyMat3x3, mat);
  192. }
  193. vm->TypeError(fmt("Mat3x3.__new__ takes 0 or 1 or 9 arguments, got ", args.size()-1));
  194. return vm->None;
  195. });
  196. vm->bind_method<0>(type, "__getnewargs__", [](VM* vm, ArgsView args){
  197. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  198. Tuple t(9);
  199. for(int i=0; i<9; i++) t[i] = VAR(self.v[i]);
  200. return VAR(std::move(t));
  201. });
  202. #define METHOD_PROXY_NONE(name) \
  203. vm->bind_method<0>(type, #name, [](VM* vm, ArgsView args){ \
  204. PyMat3x3& self = _CAST(PyMat3x3&, args[0]); \
  205. self.name(); \
  206. return vm->None; \
  207. });
  208. METHOD_PROXY_NONE(set_zeros)
  209. METHOD_PROXY_NONE(set_ones)
  210. METHOD_PROXY_NONE(set_identity)
  211. #undef METHOD_PROXY_NONE
  212. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  213. PyMat3x3& self = _CAST(PyMat3x3&, obj);
  214. std::stringstream ss;
  215. ss << std::fixed << std::setprecision(3);
  216. ss << "mat3x3([[" << self._11 << ", " << self._12 << ", " << self._13 << "],\n";
  217. ss << " [" << self._21 << ", " << self._22 << ", " << self._23 << "],\n";
  218. ss << " [" << self._31 << ", " << self._32 << ", " << self._33 << "]])";
  219. return VAR(ss.str());
  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_method<0>(type, "determinant", [](VM* vm, ArgsView args){
  309. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  310. return VAR(self.determinant());
  311. });
  312. vm->bind_method<0>(type, "transpose", [](VM* vm, ArgsView args){
  313. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  314. return VAR_T(PyMat3x3, self.transpose());
  315. });
  316. vm->bind__invert__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  317. PyMat3x3& self = _CAST(PyMat3x3&, obj);
  318. Mat3x3 ret;
  319. bool ok = self.inverse(ret);
  320. if(!ok) vm->ValueError("matrix is not invertible");
  321. return VAR_T(PyMat3x3, ret);
  322. });
  323. vm->bind_func<0>(type, "zeros", [](VM* vm, ArgsView args){
  324. PK_UNUSED(args);
  325. return VAR_T(PyMat3x3, Mat3x3::zeros());
  326. });
  327. vm->bind_func<0>(type, "ones", [](VM* vm, ArgsView args){
  328. PK_UNUSED(args);
  329. return VAR_T(PyMat3x3, Mat3x3::ones());
  330. });
  331. vm->bind_func<0>(type, "identity", [](VM* vm, ArgsView args){
  332. PK_UNUSED(args);
  333. return VAR_T(PyMat3x3, Mat3x3::identity());
  334. });
  335. /*************** affine transformations ***************/
  336. vm->bind_func<3>(type, "trs", [](VM* vm, ArgsView args){
  337. PyVec2& t = CAST(PyVec2&, args[0]);
  338. f64 r = CAST_F(args[1]);
  339. PyVec2& s = CAST(PyVec2&, args[2]);
  340. return VAR_T(PyMat3x3, Mat3x3::trs(t, r, s));
  341. });
  342. vm->bind_method<0>(type, "is_affine", [](VM* vm, ArgsView args){
  343. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  344. return VAR(self.is_affine());
  345. });
  346. vm->bind_method<0>(type, "_t", [](VM* vm, ArgsView args){
  347. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  348. return VAR_T(PyVec2, self._t());
  349. });
  350. vm->bind_method<0>(type, "_r", [](VM* vm, ArgsView args){
  351. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  352. return VAR(self._r());
  353. });
  354. vm->bind_method<0>(type, "_s", [](VM* vm, ArgsView args){
  355. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  356. return VAR_T(PyVec2, self._s());
  357. });
  358. vm->bind_method<1>(type, "transform_point", [](VM* vm, ArgsView args){
  359. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  360. PyVec2& v = CAST(PyVec2&, args[1]);
  361. return VAR_T(PyVec2, self.transform_point(v));
  362. });
  363. vm->bind_method<1>(type, "transform_vector", [](VM* vm, ArgsView args){
  364. PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
  365. PyVec2& v = CAST(PyVec2&, args[1]);
  366. return VAR_T(PyVec2, self.transform_vector(v));
  367. });
  368. }
  369. void add_module_linalg(VM* vm){
  370. PyObject* linalg = vm->new_module("linalg");
  371. PyVec2::register_class(vm, linalg);
  372. PyVec3::register_class(vm, linalg);
  373. PyVec4::register_class(vm, linalg);
  374. PyMat3x3::register_class(vm, linalg);
  375. PyObject* float_p = vm->_modules["c"]->attr("float_p");
  376. linalg->attr().set("vec2_p", float_p);
  377. linalg->attr().set("vec3_p", float_p);
  378. linalg->attr().set("vec4_p", float_p);
  379. linalg->attr().set("mat3x3_p", float_p);
  380. }
  381. } // namespace pkpy