linalg.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. #include "pocketpy/pocketpy.h"
  2. #include "pocketpy/common/sstream.h"
  3. #include "pocketpy/common/utils.h"
  4. #include "pocketpy/interpreter/vm.h"
  5. #include "pocketpy/objects/object.h"
  6. #include <math.h>
  7. static bool isclose(float a, float b) { return fabs(a - b) < 1e-4; }
  8. #define DEFINE_VEC_FIELD(name, T, Tc, field) \
  9. static bool name##__##field(int argc, py_Ref argv) { \
  10. PY_CHECK_ARGC(1); \
  11. py_new##T(py_retval(), py_to##name(argv).field); \
  12. return true; \
  13. } \
  14. static bool name##__with_##field(int argc, py_Ref argv) { \
  15. PY_CHECK_ARGC(2); \
  16. Tc val; \
  17. if(!py_cast##T(&argv[1], &val)) return false; \
  18. c11_##name v = py_to##name(argv); \
  19. v.field = val; \
  20. py_new##name(py_retval(), v); \
  21. return true; \
  22. }
  23. #define DEFINE_BOOL_NE(name, f_eq) \
  24. static bool name##__ne__(int argc, py_Ref argv) { \
  25. f_eq(argc, argv); \
  26. py_Ref ret = py_retval(); \
  27. if(ret->type == tp_NotImplementedType) return true; \
  28. ret->_bool = !ret->_bool; \
  29. return true; \
  30. }
  31. void py_newvec2(py_OutRef out, c11_vec2 v) {
  32. out->type = tp_vec2;
  33. out->is_ptr = false;
  34. out->_vec2 = v;
  35. }
  36. c11_vec2 py_tovec2(py_Ref self) {
  37. assert(self->type == tp_vec2);
  38. return self->_vec2;
  39. }
  40. void py_newvec2i(py_OutRef out, c11_vec2i v) {
  41. out->type = tp_vec2i;
  42. out->is_ptr = false;
  43. out->_vec2i = v;
  44. }
  45. c11_vec2i py_tovec2i(py_Ref self) {
  46. assert(self->type == tp_vec2i);
  47. return self->_vec2i;
  48. }
  49. void py_newvec3(py_OutRef out, c11_vec3 v) {
  50. out->type = tp_vec3;
  51. out->is_ptr = false;
  52. c11_vec3* data = (c11_vec3*)(&out->extra);
  53. *data = v;
  54. }
  55. c11_vec3 py_tovec3(py_Ref self) {
  56. assert(self->type == tp_vec3);
  57. return *(c11_vec3*)(&self->extra);
  58. }
  59. void py_newvec3i(py_OutRef out, c11_vec3i v) {
  60. out->type = tp_vec3i;
  61. out->is_ptr = false;
  62. c11_vec3i* data = (c11_vec3i*)(&out->extra);
  63. *data = v;
  64. }
  65. c11_vec3i py_tovec3i(py_Ref self) {
  66. assert(self->type == tp_vec3i);
  67. return *(c11_vec3i*)(&self->extra);
  68. }
  69. c11_mat3x3* py_newmat3x3(py_OutRef out) {
  70. return py_newobject(out, tp_mat3x3, 0, sizeof(c11_mat3x3));
  71. }
  72. c11_mat3x3* py_tomat3x3(py_Ref self) {
  73. assert(self->type == tp_mat3x3);
  74. return py_touserdata(self);
  75. }
  76. static py_Ref _const(py_Type type, const char* name) {
  77. return py_emplacedict(py_tpobject(type), py_name(name));
  78. }
  79. #define DEF_VECTOR_ELEMENT_WISE(D, T, name, op) \
  80. static bool T##name(int argc, py_Ref argv) { \
  81. PY_CHECK_ARGC(2); \
  82. if(argv[1].type != tp_##T) { \
  83. py_newnotimplemented(py_retval()); \
  84. return true; \
  85. } \
  86. c11_##T a = py_to##T(&argv[0]); \
  87. c11_##T b = py_to##T(&argv[1]); \
  88. c11_##T res; \
  89. for(int i = 0; i < D; i++) \
  90. res.data[i] = a.data[i] op b.data[i]; \
  91. py_new##T(py_retval(), res); \
  92. return true; \
  93. }
  94. #define DEF_VECTOR_OPS(D) \
  95. static bool vec##D##__new__(int argc, py_Ref argv) { \
  96. c11_vec##D res; \
  97. if(argc == 2) { \
  98. PY_CHECK_ARG_TYPE(1, tp_vec##D##i); \
  99. c11_vec##D##i v = py_tovec##D##i(&argv[1]); \
  100. for(int i = 0; i < D; i++) \
  101. res.data[i] = v.data[i]; \
  102. } else { \
  103. PY_CHECK_ARGC(D + 1); \
  104. for(int i = 0; i < D; i++) { \
  105. if(!py_castfloat32(&argv[i + 1], &res.data[i])) return false; \
  106. } \
  107. } \
  108. py_newvec##D(py_retval(), res); \
  109. return true; \
  110. } \
  111. DEF_VECTOR_ELEMENT_WISE(D, vec##D, __add__, +) \
  112. DEF_VECTOR_ELEMENT_WISE(D, vec##D, __sub__, -) \
  113. static bool vec##D##__mul__(int argc, py_Ref argv) { \
  114. PY_CHECK_ARGC(2); \
  115. c11_vec##D res; \
  116. switch(argv[1].type) { \
  117. case tp_vec##D: { \
  118. c11_vec##D a = py_tovec##D(&argv[0]); \
  119. c11_vec##D b = py_tovec##D(&argv[1]); \
  120. for(int i = 0; i < D; i++) \
  121. res.data[i] = a.data[i] * b.data[i]; \
  122. py_newvec##D(py_retval(), res); \
  123. return true; \
  124. } \
  125. case tp_int: { \
  126. c11_vec##D a = py_tovec##D(&argv[0]); \
  127. py_i64 b = argv[1]._i64; \
  128. for(int i = 0; i < D; i++) \
  129. res.data[i] = a.data[i] * b; \
  130. py_newvec##D(py_retval(), res); \
  131. return true; \
  132. } \
  133. case tp_float: { \
  134. c11_vec##D a = py_tovec##D(&argv[0]); \
  135. py_f64 b = argv[1]._f64; \
  136. for(int i = 0; i < D; i++) \
  137. res.data[i] = a.data[i] * b; \
  138. py_newvec##D(py_retval(), res); \
  139. return true; \
  140. } \
  141. default: py_newnotimplemented(py_retval()); return true; \
  142. } \
  143. } \
  144. static bool vec##D##__truediv__(int argc, py_Ref argv) { \
  145. PY_CHECK_ARGC(2); \
  146. float divisor; \
  147. if(!py_castfloat32(&argv[1], &divisor)) { \
  148. py_clearexc(NULL); \
  149. py_newnotimplemented(py_retval()); \
  150. return true; \
  151. } \
  152. c11_vec##D res; \
  153. c11_vec##D a = py_tovec##D(&argv[0]); \
  154. for(int i = 0; i < D; i++) \
  155. res.data[i] = a.data[i] / divisor; \
  156. py_newvec##D(py_retval(), res); \
  157. return true; \
  158. } \
  159. static bool vec##D##__eq__(int argc, py_Ref argv) { \
  160. PY_CHECK_ARGC(2); \
  161. if(argv[1].type != tp_vec##D) { \
  162. py_newnotimplemented(py_retval()); \
  163. return true; \
  164. } \
  165. c11_vec##D lhs = py_tovec##D(&argv[0]); \
  166. c11_vec##D rhs = py_tovec##D(&argv[1]); \
  167. bool ok = true; \
  168. for(int i = 0; i < D; i++) { \
  169. if(!isclose(lhs.data[i], rhs.data[i])) ok = false; \
  170. } \
  171. py_newbool(py_retval(), ok); \
  172. return true; \
  173. } \
  174. DEFINE_BOOL_NE(vec##D, vec##D##__eq__) \
  175. static bool vec##D##_length(int argc, py_Ref argv) { \
  176. PY_CHECK_ARGC(1); \
  177. c11_vec##D v = py_tovec##D(argv); \
  178. float sum = 0; \
  179. for(int i = 0; i < D; i++) \
  180. sum += v.data[i] * v.data[i]; \
  181. py_newfloat(py_retval(), sqrtf(sum)); \
  182. return true; \
  183. } \
  184. static bool vec##D##_length_squared(int argc, py_Ref argv) { \
  185. PY_CHECK_ARGC(1); \
  186. c11_vec##D v = py_tovec##D(argv); \
  187. float sum = 0; \
  188. for(int i = 0; i < D; i++) \
  189. sum += v.data[i] * v.data[i]; \
  190. py_newfloat(py_retval(), sum); \
  191. return true; \
  192. } \
  193. static bool vec##D##_dot(int argc, py_Ref argv) { \
  194. PY_CHECK_ARGC(2); \
  195. PY_CHECK_ARG_TYPE(1, tp_vec##D); \
  196. c11_vec##D a = py_tovec##D(&argv[0]); \
  197. c11_vec##D b = py_tovec##D(&argv[1]); \
  198. float sum = 0; \
  199. for(int i = 0; i < D; i++) \
  200. sum += a.data[i] * b.data[i]; \
  201. py_newfloat(py_retval(), sum); \
  202. return true; \
  203. } \
  204. static bool vec##D##_normalize(int argc, py_Ref argv) { \
  205. PY_CHECK_ARGC(1); \
  206. c11_vec##D self = py_tovec##D(argv); \
  207. float len = 0; \
  208. for(int i = 0; i < D; i++) \
  209. len += self.data[i] * self.data[i]; \
  210. if(isclose(len, 0)) return ZeroDivisionError("cannot normalize zero vector"); \
  211. len = sqrtf(len); \
  212. c11_vec##D res; \
  213. for(int i = 0; i < D; i++) \
  214. res.data[i] = self.data[i] / len; \
  215. py_newvec##D(py_retval(), res); \
  216. return true; \
  217. }
  218. DEF_VECTOR_OPS(2)
  219. DEF_VECTOR_OPS(3)
  220. #define DEF_VECTOR_INT_OPS(D) \
  221. static bool vec##D##i__new__(int argc, py_Ref argv) { \
  222. PY_CHECK_ARGC(D + 1); \
  223. c11_vec##D##i res; \
  224. for(int i = 0; i < D; i++) { \
  225. if(!py_checkint(&argv[i + 1])) return false; \
  226. res.data[i] = py_toint(&argv[i + 1]); \
  227. } \
  228. py_newvec##D##i(py_retval(), res); \
  229. return true; \
  230. } \
  231. DEF_VECTOR_ELEMENT_WISE(D, vec##D##i, __add__, +) \
  232. DEF_VECTOR_ELEMENT_WISE(D, vec##D##i, __sub__, -) \
  233. static bool vec##D##i__mul__(int argc, py_Ref argv) { \
  234. PY_CHECK_ARGC(2); \
  235. c11_vec##D##i res; \
  236. switch(argv[1].type) { \
  237. case tp_vec##D##i: { \
  238. c11_vec##D##i a = py_tovec##D##i(&argv[0]); \
  239. c11_vec##D##i b = py_tovec##D##i(&argv[1]); \
  240. for(int i = 0; i < D; i++) \
  241. res.data[i] = a.data[i] * b.data[i]; \
  242. py_newvec##D##i(py_retval(), res); \
  243. return true; \
  244. } \
  245. case tp_int: { \
  246. c11_vec##D##i a = py_tovec##D##i(&argv[0]); \
  247. py_i64 b = argv[1]._i64; \
  248. for(int i = 0; i < D; i++) \
  249. res.data[i] = a.data[i] * b; \
  250. py_newvec##D##i(py_retval(), res); \
  251. return true; \
  252. } \
  253. default: py_newnotimplemented(py_retval()); return true; \
  254. } \
  255. } \
  256. static bool vec##D##i__eq__(int argc, py_Ref argv) { \
  257. PY_CHECK_ARGC(2); \
  258. if(argv[1].type != tp_vec##D##i) { \
  259. py_newnotimplemented(py_retval()); \
  260. return true; \
  261. } \
  262. c11_vec##D##i lhs = py_tovec##D##i(&argv[0]); \
  263. c11_vec##D##i rhs = py_tovec##D##i(&argv[1]); \
  264. bool ok = true; \
  265. for(int i = 0; i < D; i++) { \
  266. if(lhs.data[i] != rhs.data[i]) ok = false; \
  267. } \
  268. py_newbool(py_retval(), ok); \
  269. return true; \
  270. } \
  271. DEFINE_BOOL_NE(vec##D##i, vec##D##i__eq__) \
  272. static bool vec##D##i##_dot(int argc, py_Ref argv) { \
  273. PY_CHECK_ARGC(2); \
  274. PY_CHECK_ARG_TYPE(1, tp_vec##D##i); \
  275. c11_vec##D##i a = py_tovec##D##i(&argv[0]); \
  276. c11_vec##D##i b = py_tovec##D##i(&argv[1]); \
  277. py_i64 sum = 0; \
  278. for(int i = 0; i < D; i++) \
  279. sum += a.data[i] * b.data[i]; \
  280. py_newint(py_retval(), sum); \
  281. return true; \
  282. } \
  283. static bool vec##D##i##__floordiv__(int argc, py_Ref argv) { \
  284. PY_CHECK_ARGC(2); \
  285. PY_CHECK_ARG_TYPE(1, tp_int); \
  286. c11_vec##D##i a = py_tovec##D##i(&argv[0]); \
  287. py_i64 b = py_toint(&argv[1]); \
  288. for(int i = 0; i < D; i++) \
  289. a.data[i] /= b; \
  290. py_newvec##D##i(py_retval(), a); \
  291. return true; \
  292. }
  293. DEF_VECTOR_INT_OPS(2)
  294. DEF_VECTOR_INT_OPS(3)
  295. static bool vec2i__hash__(int argc, py_Ref argv) {
  296. PY_CHECK_ARGC(1);
  297. c11_vec2i v = py_tovec2i(argv);
  298. uint64_t x_part = (uint32_t)v.x & 0xFFFFFFFF;
  299. uint64_t y_part = (uint32_t)v.y & 0xFFFFFFFF;
  300. uint64_t hash = (x_part << 32) | y_part;
  301. py_newint(py_retval(), (py_i64)hash);
  302. return true;
  303. }
  304. static bool vec3i__hash__(int argc, py_Ref argv) {
  305. PY_CHECK_ARGC(1);
  306. c11_vec3i v = py_tovec3i(argv);
  307. uint64_t x_part = (uint32_t)v.x & 0xFFFFFF;
  308. uint64_t y_part = (uint32_t)v.y & 0xFFFFFF;
  309. uint64_t z_part = (uint32_t)v.z & 0xFFFF;
  310. uint64_t hash = (x_part << 40) | (y_part << 16) | z_part;
  311. py_newint(py_retval(), (py_i64)hash);
  312. return true;
  313. }
  314. static bool vec2__repr__(int argc, py_Ref argv) {
  315. PY_CHECK_ARGC(1);
  316. char buf[64];
  317. int size = snprintf(buf, 64, "vec2(%.4f, %.4f)", argv[0]._vec2.x, argv[0]._vec2.y);
  318. py_newstrv(py_retval(), (c11_sv){buf, size});
  319. return true;
  320. }
  321. static bool vec2_rotate(int argc, py_Ref argv) {
  322. PY_CHECK_ARGC(2);
  323. py_f64 radians;
  324. if(!py_castfloat(&argv[1], &radians)) return false;
  325. float cr = cosf(radians);
  326. float sr = sinf(radians);
  327. c11_vec2 res;
  328. res.x = argv[0]._vec2.x * cr - argv[0]._vec2.y * sr;
  329. res.y = argv[0]._vec2.x * sr + argv[0]._vec2.y * cr;
  330. py_newvec2(py_retval(), res);
  331. return true;
  332. }
  333. static bool vec2_angle_STATIC(int argc, py_Ref argv) {
  334. PY_CHECK_ARGC(2);
  335. PY_CHECK_ARG_TYPE(0, tp_vec2);
  336. PY_CHECK_ARG_TYPE(1, tp_vec2);
  337. float val = atan2f(argv[1]._vec2.y, argv[1]._vec2.x) - atan2f(argv[0]._vec2.y, argv[0]._vec2.x);
  338. if(val > PK_M_PI) val -= 2 * (float)PK_M_PI;
  339. if(val < -PK_M_PI) val += 2 * (float)PK_M_PI;
  340. py_newfloat(py_retval(), val);
  341. return true;
  342. }
  343. static bool vec2_smoothdamp_STATIC(int argc, py_Ref argv) {
  344. PY_CHECK_ARGC(6);
  345. PY_CHECK_ARG_TYPE(0, tp_vec2); // current: vec2
  346. PY_CHECK_ARG_TYPE(1, tp_vec2); // target: vec2
  347. PY_CHECK_ARG_TYPE(2, tp_vec2); // current_velocity: vec2
  348. float smoothTime;
  349. if(!py_castfloat32(&argv[3], &smoothTime)) return false;
  350. float maxSpeed;
  351. if(!py_castfloat32(&argv[4], &maxSpeed)) return false;
  352. float deltaTime;
  353. if(!py_castfloat32(&argv[5], &deltaTime)) return false;
  354. c11_vec2 current = argv[0]._vec2;
  355. c11_vec2 target = argv[1]._vec2;
  356. c11_vec2 currentVelocity = argv[2]._vec2;
  357. // https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector2.cs#L289
  358. // Based on Game Programming Gems 4 Chapter 1.10
  359. smoothTime = c11__max(0.0001F, smoothTime);
  360. float omega = 2.0F / smoothTime;
  361. float x = omega * deltaTime;
  362. float exp = 1.0F / (1.0F + x + 0.48F * x * x + 0.235F * x * x * x);
  363. float change_x = current.x - target.x;
  364. float change_y = current.y - target.y;
  365. c11_vec2 originalTo = target;
  366. // Clamp maximum speed
  367. float maxChange = maxSpeed * smoothTime;
  368. float maxChangeSq = maxChange * maxChange;
  369. float sqDist = change_x * change_x + change_y * change_y;
  370. if(sqDist > maxChangeSq) {
  371. float mag = sqrtf(sqDist);
  372. change_x = change_x / mag * maxChange;
  373. change_y = change_y / mag * maxChange;
  374. }
  375. target.x = current.x - change_x;
  376. target.y = current.y - change_y;
  377. float temp_x = (currentVelocity.x + omega * change_x) * deltaTime;
  378. float temp_y = (currentVelocity.y + omega * change_y) * deltaTime;
  379. currentVelocity.x = (currentVelocity.x - omega * temp_x) * exp;
  380. currentVelocity.y = (currentVelocity.y - omega * temp_y) * exp;
  381. float output_x = target.x + (change_x + temp_x) * exp;
  382. float output_y = target.y + (change_y + temp_y) * exp;
  383. // Prevent overshooting
  384. float origMinusCurrent_x = originalTo.x - current.x;
  385. float origMinusCurrent_y = originalTo.y - current.y;
  386. float outMinusOrig_x = output_x - originalTo.x;
  387. float outMinusOrig_y = output_y - originalTo.y;
  388. if(origMinusCurrent_x * outMinusOrig_x + origMinusCurrent_y * outMinusOrig_y > 0) {
  389. output_x = originalTo.x;
  390. output_y = originalTo.y;
  391. currentVelocity.x = (output_x - originalTo.x) / deltaTime;
  392. currentVelocity.y = (output_y - originalTo.y) / deltaTime;
  393. }
  394. py_Ref ret = py_retval();
  395. py_Ref p = py_newtuple(ret, 2);
  396. py_newvec2(&p[0],
  397. (c11_vec2){
  398. {output_x, output_y}
  399. });
  400. py_newvec2(&p[1], currentVelocity);
  401. return true;
  402. }
  403. DEFINE_VEC_FIELD(vec2, float, py_f64, x)
  404. DEFINE_VEC_FIELD(vec2, float, py_f64, y)
  405. static bool vec2__with_z(int argc, py_Ref argv) {
  406. PY_CHECK_ARGC(2);
  407. float z;
  408. if(!py_castfloat32(&argv[1], &z)) return false;
  409. c11_vec3 v = {
  410. {argv->_vec2.x, argv->_vec2.y, z}
  411. };
  412. py_newvec3(py_retval(), v);
  413. return true;
  414. }
  415. /* mat3x3 */
  416. static bool mat3x3__new__(int argc, py_Ref argv) {
  417. PY_CHECK_ARGC(10);
  418. c11_mat3x3* m = py_newmat3x3(py_retval());
  419. for(int i = 0; i < 9; i++) {
  420. py_f64 val;
  421. if(!py_castfloat(&argv[i + 1], &val)) return false;
  422. m->data[i] = val;
  423. }
  424. return true;
  425. }
  426. static bool mat3x3__repr__(int argc, py_Ref argv) {
  427. PY_CHECK_ARGC(1);
  428. c11_mat3x3* m = py_tomat3x3(argv);
  429. char buf[256];
  430. const char* fmt =
  431. "mat3x3(%.4f, %.4f, %.4f,\n %.4f, %.4f, %.4f,\n "
  432. " %.4f, %.4f, %.4f)";
  433. int size = snprintf(buf,
  434. 256,
  435. fmt,
  436. m->data[0],
  437. m->data[1],
  438. m->data[2],
  439. m->data[3],
  440. m->data[4],
  441. m->data[5],
  442. m->data[6],
  443. m->data[7],
  444. m->data[8]);
  445. py_newstrv(py_retval(), (c11_sv){buf, size});
  446. return true;
  447. }
  448. static bool mat3x3__getitem__(int argc, py_Ref argv) {
  449. PY_CHECK_ARGC(2);
  450. PY_CHECK_ARG_TYPE(1, tp_tuple);
  451. c11_mat3x3* ud = py_tomat3x3(argv);
  452. if(py_tuple_len(&argv[1]) != 2) return IndexError("expected a tuple of length 2");
  453. py_Ref i = py_tuple_getitem(&argv[1], 0);
  454. py_Ref j = py_tuple_getitem(&argv[1], 1);
  455. if(!py_checktype(i, tp_int) || !py_checktype(j, tp_int)) return false;
  456. if(i->_i64 < 0 || i->_i64 >= 3 || j->_i64 < 0 || j->_i64 >= 3) {
  457. return IndexError("index out of range");
  458. }
  459. py_newfloat(py_retval(), ud->m[i->_i64][j->_i64]);
  460. return true;
  461. }
  462. static bool mat3x3__setitem__(int argc, py_Ref argv) {
  463. PY_CHECK_ARGC(3);
  464. PY_CHECK_ARG_TYPE(1, tp_tuple);
  465. c11_mat3x3* ud = py_tomat3x3(argv);
  466. if(py_tuple_len(&argv[1]) != 2) return IndexError("expected a tuple of length 2");
  467. py_Ref i = py_tuple_getitem(&argv[1], 0);
  468. py_Ref j = py_tuple_getitem(&argv[1], 1);
  469. if(!py_checktype(i, tp_int) || !py_checktype(j, tp_int)) return false;
  470. py_f64 val;
  471. if(!py_castfloat(&argv[2], &val)) return false;
  472. if(i->_i64 < 0 || i->_i64 >= 3 || j->_i64 < 0 || j->_i64 >= 3) {
  473. return IndexError("index out of range");
  474. }
  475. ud->m[i->_i64][j->_i64] = val;
  476. py_newnone(py_retval());
  477. return true;
  478. }
  479. static bool mat3x3__eq__(int argc, py_Ref argv) {
  480. PY_CHECK_ARGC(2);
  481. if(argv[1].type != tp_mat3x3) {
  482. py_newnotimplemented(py_retval());
  483. return true;
  484. }
  485. c11_mat3x3* lhs = py_tomat3x3(argv);
  486. c11_mat3x3* rhs = py_tomat3x3(&argv[1]);
  487. for(int i = 0; i < 9; i++) {
  488. if(!isclose(lhs->data[i], rhs->data[i])) {
  489. py_newbool(py_retval(), false);
  490. return true;
  491. }
  492. }
  493. py_newbool(py_retval(), true);
  494. return true;
  495. }
  496. DEFINE_BOOL_NE(mat3x3, mat3x3__eq__)
  497. static void matmul(const c11_mat3x3* lhs, const c11_mat3x3* rhs, c11_mat3x3* out) {
  498. out->_11 = lhs->_11 * rhs->_11 + lhs->_12 * rhs->_21 + lhs->_13 * rhs->_31;
  499. out->_12 = lhs->_11 * rhs->_12 + lhs->_12 * rhs->_22 + lhs->_13 * rhs->_32;
  500. out->_13 = lhs->_11 * rhs->_13 + lhs->_12 * rhs->_23 + lhs->_13 * rhs->_33;
  501. out->_21 = lhs->_21 * rhs->_11 + lhs->_22 * rhs->_21 + lhs->_23 * rhs->_31;
  502. out->_22 = lhs->_21 * rhs->_12 + lhs->_22 * rhs->_22 + lhs->_23 * rhs->_32;
  503. out->_23 = lhs->_21 * rhs->_13 + lhs->_22 * rhs->_23 + lhs->_23 * rhs->_33;
  504. out->_31 = lhs->_31 * rhs->_11 + lhs->_32 * rhs->_21 + lhs->_33 * rhs->_31;
  505. out->_32 = lhs->_31 * rhs->_12 + lhs->_32 * rhs->_22 + lhs->_33 * rhs->_32;
  506. out->_33 = lhs->_31 * rhs->_13 + lhs->_32 * rhs->_23 + lhs->_33 * rhs->_33;
  507. }
  508. static float determinant(const c11_mat3x3* m) {
  509. return m->_11 * (m->_22 * m->_33 - m->_23 * m->_32) -
  510. m->_12 * (m->_21 * m->_33 - m->_23 * m->_31) +
  511. m->_13 * (m->_21 * m->_32 - m->_22 * m->_31);
  512. }
  513. static bool inverse(const c11_mat3x3* m, c11_mat3x3* out) {
  514. float det = determinant(m);
  515. if(isclose(det, 0)) return false;
  516. float invdet = 1.0f / det;
  517. out->_11 = (m->_22 * m->_33 - m->_23 * m->_32) * invdet;
  518. out->_12 = (m->_13 * m->_32 - m->_12 * m->_33) * invdet;
  519. out->_13 = (m->_12 * m->_23 - m->_13 * m->_22) * invdet;
  520. out->_21 = (m->_23 * m->_31 - m->_21 * m->_33) * invdet;
  521. out->_22 = (m->_11 * m->_33 - m->_13 * m->_31) * invdet;
  522. out->_23 = (m->_13 * m->_21 - m->_11 * m->_23) * invdet;
  523. out->_31 = (m->_21 * m->_32 - m->_22 * m->_31) * invdet;
  524. out->_32 = (m->_12 * m->_31 - m->_11 * m->_32) * invdet;
  525. out->_33 = (m->_11 * m->_22 - m->_12 * m->_21) * invdet;
  526. return true;
  527. }
  528. static void trs(c11_vec2 t, float r, c11_vec2 s, c11_mat3x3* out) {
  529. float cr = cosf(r);
  530. float sr = sinf(r);
  531. // clang-format off
  532. *out = (c11_mat3x3){
  533. ._11 = s.x * cr, ._12 = -s.y * sr, ._13 = t.x,
  534. ._21 = s.x * sr, ._22 = s.y * cr, ._23 = t.y,
  535. ._31 = 0, ._32 = 0, ._33 = 1,
  536. };
  537. // clang-format on
  538. }
  539. static bool mat3x3__matmul__(int argc, py_Ref argv) {
  540. PY_CHECK_ARGC(2);
  541. c11_mat3x3* lhs = py_tomat3x3(argv);
  542. if(argv[1].type == tp_mat3x3) {
  543. c11_mat3x3* rhs = py_tomat3x3(&argv[1]);
  544. c11_mat3x3* out = py_newmat3x3(py_retval());
  545. matmul(lhs, rhs, out);
  546. } else if(argv[1].type == tp_vec3) {
  547. c11_vec3 rhs = py_tovec3(&argv[1]);
  548. c11_vec3 res;
  549. res.x = lhs->_11 * rhs.x + lhs->_12 * rhs.y + lhs->_13 * rhs.z;
  550. res.y = lhs->_21 * rhs.x + lhs->_22 * rhs.y + lhs->_23 * rhs.z;
  551. res.z = lhs->_31 * rhs.x + lhs->_32 * rhs.y + lhs->_33 * rhs.z;
  552. py_newvec3(py_retval(), res);
  553. } else {
  554. py_newnotimplemented(py_retval());
  555. }
  556. return true;
  557. }
  558. static bool mat3x3__invert__(int argc, py_Ref argv) {
  559. PY_CHECK_ARGC(1);
  560. c11_mat3x3* ud = py_tomat3x3(argv);
  561. c11_mat3x3* out = py_newmat3x3(py_retval());
  562. if(inverse(ud, out)) return true;
  563. return ZeroDivisionError("matrix is not invertible");
  564. }
  565. static bool mat3x3_matmul(int argc, py_Ref argv) {
  566. PY_CHECK_ARGC(3);
  567. PY_CHECK_ARG_TYPE(0, tp_mat3x3);
  568. PY_CHECK_ARG_TYPE(1, tp_mat3x3);
  569. PY_CHECK_ARG_TYPE(2, tp_mat3x3);
  570. c11_mat3x3* lhs = py_tomat3x3(&argv[0]);
  571. c11_mat3x3* rhs = py_tomat3x3(&argv[1]);
  572. c11_mat3x3* out = py_tomat3x3(&argv[2]);
  573. matmul(lhs, rhs, out);
  574. py_newnone(py_retval());
  575. return true;
  576. }
  577. static bool mat3x3_determinant(int argc, py_Ref argv) {
  578. PY_CHECK_ARGC(1);
  579. c11_mat3x3* ud = py_tomat3x3(argv);
  580. py_newfloat(py_retval(), determinant(ud));
  581. return true;
  582. }
  583. static bool mat3x3_copy(int argc, py_Ref argv) {
  584. PY_CHECK_ARGC(1);
  585. c11_mat3x3* ud = py_tomat3x3(argv);
  586. c11_mat3x3* out = py_newmat3x3(py_retval());
  587. *out = *ud;
  588. return true;
  589. }
  590. static bool mat3x3_inverse(int argc, py_Ref argv) {
  591. PY_CHECK_ARGC(1);
  592. c11_mat3x3* ud = py_tomat3x3(argv);
  593. c11_mat3x3* out = py_newmat3x3(py_retval());
  594. if(inverse(ud, out)) return true;
  595. return ZeroDivisionError("matrix is not invertible");
  596. }
  597. static bool mat3x3_copy_(int argc, py_Ref argv) {
  598. PY_CHECK_ARGC(2);
  599. PY_CHECK_ARG_TYPE(1, tp_mat3x3);
  600. c11_mat3x3* self = py_tomat3x3(argv);
  601. c11_mat3x3* other = py_tomat3x3(&argv[1]);
  602. *self = *other;
  603. py_newnone(py_retval());
  604. return true;
  605. }
  606. static bool mat3x3_inverse_(int argc, py_Ref argv) {
  607. PY_CHECK_ARGC(1);
  608. c11_mat3x3* ud = py_tomat3x3(argv);
  609. c11_mat3x3 res;
  610. if(inverse(ud, &res)) {
  611. *ud = res;
  612. py_newnone(py_retval());
  613. return true;
  614. }
  615. return ZeroDivisionError("matrix is not invertible");
  616. }
  617. static bool mat3x3_zeros_STATIC(int argc, py_Ref argv) {
  618. PY_CHECK_ARGC(0);
  619. c11_mat3x3* out = py_newmat3x3(py_retval());
  620. memset(out, 0, sizeof(c11_mat3x3));
  621. return true;
  622. }
  623. static bool mat3x3_identity_STATIC(int argc, py_Ref argv) {
  624. PY_CHECK_ARGC(0);
  625. c11_mat3x3* out = py_newmat3x3(py_retval());
  626. // clang-format off
  627. *out = (c11_mat3x3){
  628. ._11 = 1, ._12 = 0, ._13 = 0,
  629. ._21 = 0, ._22 = 1, ._23 = 0,
  630. ._31 = 0, ._32 = 0, ._33 = 1,
  631. };
  632. // clang-format on
  633. return true;
  634. }
  635. static bool mat3x3_trs_STATIC(int argc, py_Ref argv) {
  636. PY_CHECK_ARGC(3);
  637. py_f64 r;
  638. if(!py_checktype(&argv[0], tp_vec2)) return false;
  639. if(!py_castfloat(&argv[1], &r)) return false;
  640. if(!py_checktype(&argv[2], tp_vec2)) return false;
  641. c11_vec2 t = py_tovec2(&argv[0]);
  642. c11_vec2 s = py_tovec2(&argv[2]);
  643. c11_mat3x3* out = py_newmat3x3(py_retval());
  644. trs(t, r, s, out);
  645. return true;
  646. }
  647. static bool mat3x3_copy_trs_(int argc, py_Ref argv) {
  648. PY_CHECK_ARGC(4);
  649. c11_mat3x3* ud = py_tomat3x3(&argv[0]);
  650. py_f64 r;
  651. if(!py_checktype(&argv[1], tp_vec2)) return false;
  652. if(!py_castfloat(&argv[2], &r)) return false;
  653. if(!py_checktype(&argv[3], tp_vec2)) return false;
  654. c11_vec2 t = py_tovec2(&argv[1]);
  655. c11_vec2 s = py_tovec2(&argv[3]);
  656. trs(t, r, s, ud);
  657. py_newnone(py_retval());
  658. return true;
  659. }
  660. static bool mat3x3_t(int argc, py_Ref argv) {
  661. PY_CHECK_ARGC(1);
  662. c11_mat3x3* ud = py_tomat3x3(argv);
  663. c11_vec2 res;
  664. res.x = ud->_13;
  665. res.y = ud->_23;
  666. py_newvec2(py_retval(), res);
  667. return true;
  668. }
  669. static bool mat3x3_r(int argc, py_Ref argv) {
  670. PY_CHECK_ARGC(1);
  671. c11_mat3x3* ud = py_tomat3x3(argv);
  672. float r = atan2f(ud->_21, ud->_11);
  673. py_newfloat(py_retval(), r);
  674. return true;
  675. }
  676. static bool mat3x3_s(int argc, py_Ref argv) {
  677. PY_CHECK_ARGC(1);
  678. c11_mat3x3* ud = py_tomat3x3(argv);
  679. c11_vec2 res;
  680. res.x = sqrtf(ud->_11 * ud->_11 + ud->_21 * ud->_21);
  681. res.y = sqrtf(ud->_12 * ud->_12 + ud->_22 * ud->_22);
  682. py_newvec2(py_retval(), res);
  683. return true;
  684. }
  685. static bool mat3x3_transform_point(int argc, py_Ref argv) {
  686. PY_CHECK_ARGC(2);
  687. PY_CHECK_ARG_TYPE(1, tp_vec2);
  688. c11_mat3x3* ud = py_tomat3x3(&argv[0]);
  689. c11_vec2 p = py_tovec2(&argv[1]);
  690. c11_vec2 res;
  691. res.x = ud->_11 * p.x + ud->_12 * p.y + ud->_13;
  692. res.y = ud->_21 * p.x + ud->_22 * p.y + ud->_23;
  693. py_newvec2(py_retval(), res);
  694. return true;
  695. }
  696. static bool mat3x3_transform_vector(int argc, py_Ref argv) {
  697. PY_CHECK_ARGC(2);
  698. PY_CHECK_ARG_TYPE(1, tp_vec2);
  699. c11_mat3x3* ud = py_tomat3x3(&argv[0]);
  700. c11_vec2 p = py_tovec2(&argv[1]);
  701. c11_vec2 res;
  702. res.x = ud->_11 * p.x + ud->_12 * p.y;
  703. res.y = ud->_21 * p.x + ud->_22 * p.y;
  704. py_newvec2(py_retval(), res);
  705. return true;
  706. }
  707. /* vec2i */
  708. DEFINE_VEC_FIELD(vec2i, int, py_i64, x)
  709. DEFINE_VEC_FIELD(vec2i, int, py_i64, y)
  710. static bool vec2i__repr__(int argc, py_Ref argv) {
  711. PY_CHECK_ARGC(1);
  712. c11_vec2i data = py_tovec2i(argv);
  713. char buf[64];
  714. int size = snprintf(buf, 64, "vec2i(%d, %d)", data.x, data.y);
  715. py_newstrv(py_retval(), (c11_sv){buf, size});
  716. return true;
  717. }
  718. /* vec3i */
  719. static bool vec3i__repr__(int argc, py_Ref argv) {
  720. PY_CHECK_ARGC(1);
  721. c11_vec3i data = py_tovec3i(argv);
  722. char buf[64];
  723. int size = snprintf(buf, 64, "vec3i(%d, %d, %d)", data.x, data.y, data.z);
  724. py_newstrv(py_retval(), (c11_sv){buf, size});
  725. return true;
  726. }
  727. DEFINE_VEC_FIELD(vec3i, int, py_i64, x)
  728. DEFINE_VEC_FIELD(vec3i, int, py_i64, y)
  729. DEFINE_VEC_FIELD(vec3i, int, py_i64, z)
  730. /* vec3 */
  731. static bool vec3__repr__(int argc, py_Ref argv) {
  732. PY_CHECK_ARGC(1);
  733. c11_vec3 data = py_tovec3(argv);
  734. char buf[64];
  735. int size = snprintf(buf, 64, "vec3(%.4f, %.4f, %.4f)", data.x, data.y, data.z);
  736. py_newstrv(py_retval(), (c11_sv){buf, size});
  737. return true;
  738. }
  739. DEFINE_VEC_FIELD(vec3, float, py_f64, x)
  740. DEFINE_VEC_FIELD(vec3, float, py_f64, y)
  741. DEFINE_VEC_FIELD(vec3, float, py_f64, z)
  742. static bool vec3__xy(int argc, py_Ref argv) {
  743. PY_CHECK_ARGC(1);
  744. c11_vec3 data = py_tovec3(argv);
  745. c11_vec2 res = {
  746. {data.x, data.y}
  747. };
  748. py_newvec2(py_retval(), res);
  749. return true;
  750. }
  751. static bool vec3__with_xy(int argc, py_Ref argv) {
  752. PY_CHECK_ARGC(2);
  753. PY_CHECK_ARG_TYPE(1, tp_vec2);
  754. c11_vec2 xy = py_tovec2(&argv[1]);
  755. c11_vec3 res = {
  756. {xy.x, xy.y, py_tovec3(argv).z}
  757. };
  758. py_newvec3(py_retval(), res);
  759. return true;
  760. }
  761. void pk__add_module_linalg() {
  762. py_Ref mod = py_newmodule("linalg");
  763. py_Type vec2 = pk_newtype("vec2", tp_object, mod, NULL, false, true);
  764. py_Type vec3 = pk_newtype("vec3", tp_object, mod, NULL, false, true);
  765. py_Type vec2i = pk_newtype("vec2i", tp_object, mod, NULL, false, true);
  766. py_Type vec3i = pk_newtype("vec3i", tp_object, mod, NULL, false, true);
  767. py_Type mat3x3 = pk_newtype("mat3x3", tp_object, mod, NULL, false, true);
  768. py_setdict(mod, py_name("vec2"), py_tpobject(vec2));
  769. py_setdict(mod, py_name("vec3"), py_tpobject(vec3));
  770. py_setdict(mod, py_name("vec2i"), py_tpobject(vec2i));
  771. py_setdict(mod, py_name("vec3i"), py_tpobject(vec3i));
  772. py_setdict(mod, py_name("mat3x3"), py_tpobject(mat3x3));
  773. assert(vec2 == tp_vec2);
  774. assert(vec3 == tp_vec3);
  775. assert(vec2i == tp_vec2i);
  776. assert(vec3i == tp_vec3i);
  777. assert(mat3x3 == tp_mat3x3);
  778. /* vec2 */
  779. py_bindmagic(vec2, __new__, vec2__new__);
  780. py_bindmagic(vec2, __add__, vec2__add__);
  781. py_bindmagic(vec2, __sub__, vec2__sub__);
  782. py_bindmagic(vec2, __mul__, vec2__mul__);
  783. py_bindmagic(vec2, __truediv__, vec2__truediv__);
  784. py_bindmagic(vec2, __repr__, vec2__repr__);
  785. py_bindmagic(vec2, __eq__, vec2__eq__);
  786. py_bindmagic(vec2, __ne__, vec2__ne__);
  787. py_bindmethod(vec2, "dot", vec2_dot);
  788. py_bindmethod(vec2, "length", vec2_length);
  789. py_bindmethod(vec2, "length_squared", vec2_length_squared);
  790. py_bindmethod(vec2, "normalize", vec2_normalize);
  791. py_bindmethod(vec2, "rotate", vec2_rotate);
  792. // clang-format off
  793. py_newvec2(_const(vec2, "ZERO"), (c11_vec2){{0, 0}});
  794. py_newvec2(_const(vec2, "ONE"), (c11_vec2){{1, 1}});
  795. py_newvec2(_const(vec2, "LEFT"), (c11_vec2){{-1, 0}});
  796. py_newvec2(_const(vec2, "RIGHT"), (c11_vec2){{1, 0}});
  797. py_newvec2(_const(vec2, "UP"), (c11_vec2){{0, -1}});
  798. py_newvec2(_const(vec2, "DOWN"), (c11_vec2){{0, 1}});
  799. // clang-format on
  800. py_bindstaticmethod(vec2, "angle", vec2_angle_STATIC);
  801. py_bindstaticmethod(vec2, "smooth_damp", vec2_smoothdamp_STATIC);
  802. py_bindproperty(vec2, "x", vec2__x, NULL);
  803. py_bindproperty(vec2, "y", vec2__y, NULL);
  804. py_bindmethod(vec2, "with_x", vec2__with_x);
  805. py_bindmethod(vec2, "with_y", vec2__with_y);
  806. py_bindmethod(vec2, "with_z", vec2__with_z);
  807. /* mat3x3 */
  808. py_bindmagic(mat3x3, __new__, mat3x3__new__);
  809. py_bindmagic(mat3x3, __repr__, mat3x3__repr__);
  810. py_bindmagic(mat3x3, __getitem__, mat3x3__getitem__);
  811. py_bindmagic(mat3x3, __setitem__, mat3x3__setitem__);
  812. py_bindmagic(mat3x3, __matmul__, mat3x3__matmul__);
  813. py_bindmagic(mat3x3, __invert__, mat3x3__invert__);
  814. py_bindmagic(mat3x3, __eq__, mat3x3__eq__);
  815. py_bindmagic(mat3x3, __ne__, mat3x3__ne__);
  816. py_bindmethod(mat3x3, "matmul", mat3x3_matmul);
  817. py_bindmethod(mat3x3, "determinant", mat3x3_determinant);
  818. py_bindmethod(mat3x3, "copy", mat3x3_copy);
  819. py_bindmethod(mat3x3, "inverse", mat3x3_inverse);
  820. py_bindmethod(mat3x3, "copy_", mat3x3_copy_);
  821. py_bindmethod(mat3x3, "inverse_", mat3x3_inverse_);
  822. py_bindstaticmethod(mat3x3, "zeros", mat3x3_zeros_STATIC);
  823. py_bindstaticmethod(mat3x3, "identity", mat3x3_identity_STATIC);
  824. py_bindstaticmethod(mat3x3, "trs", mat3x3_trs_STATIC);
  825. py_bindmethod(mat3x3, "copy_trs_", mat3x3_copy_trs_);
  826. py_bindmethod(mat3x3, "t", mat3x3_t);
  827. py_bindmethod(mat3x3, "r", mat3x3_r);
  828. py_bindmethod(mat3x3, "s", mat3x3_s);
  829. py_bindmethod(mat3x3, "transform_point", mat3x3_transform_point);
  830. py_bindmethod(mat3x3, "transform_vector", mat3x3_transform_vector);
  831. /* vec2i */
  832. py_bindmagic(vec2i, __new__, vec2i__new__);
  833. py_bindmagic(vec2i, __repr__, vec2i__repr__);
  834. py_bindmagic(vec2i, __add__, vec2i__add__);
  835. py_bindmagic(vec2i, __sub__, vec2i__sub__);
  836. py_bindmagic(vec2i, __mul__, vec2i__mul__);
  837. py_bindmagic(vec2i, __floordiv__, vec2i__floordiv__);
  838. py_bindmagic(vec2i, __eq__, vec2i__eq__);
  839. py_bindmagic(vec2i, __ne__, vec2i__ne__);
  840. py_bindmagic(vec2i, __hash__, vec2i__hash__);
  841. py_bindproperty(vec2i, "x", vec2i__x, NULL);
  842. py_bindproperty(vec2i, "y", vec2i__y, NULL);
  843. py_bindmethod(vec2i, "with_x", vec2i__with_x);
  844. py_bindmethod(vec2i, "with_y", vec2i__with_y);
  845. py_bindmethod(vec2i, "dot", vec2i_dot);
  846. // clang-format off
  847. py_newvec2i(_const(vec2i, "ZERO"), (c11_vec2i){{0, 0}});
  848. py_newvec2i(_const(vec2i, "ONE"), (c11_vec2i){{1, 1}});
  849. py_newvec2i(_const(vec2i, "LEFT"), (c11_vec2i){{-1, 0}});
  850. py_newvec2i(_const(vec2i, "RIGHT"), (c11_vec2i){{1, 0}});
  851. py_newvec2i(_const(vec2i, "UP"), (c11_vec2i){{0, -1}});
  852. py_newvec2i(_const(vec2i, "DOWN"), (c11_vec2i){{0, 1}});
  853. // clang-format on
  854. /* vec3i */
  855. py_bindmagic(vec3i, __new__, vec3i__new__);
  856. py_bindmagic(vec3i, __repr__, vec3i__repr__);
  857. py_bindmagic(vec3i, __add__, vec3i__add__);
  858. py_bindmagic(vec3i, __sub__, vec3i__sub__);
  859. py_bindmagic(vec3i, __mul__, vec3i__mul__);
  860. py_bindmagic(vec3i, __floordiv__, vec3i__floordiv__);
  861. py_bindmagic(vec3i, __eq__, vec3i__eq__);
  862. py_bindmagic(vec3i, __ne__, vec3i__ne__);
  863. py_bindmagic(vec3i, __hash__, vec3i__hash__);
  864. py_bindproperty(vec3i, "x", vec3i__x, NULL);
  865. py_bindproperty(vec3i, "y", vec3i__y, NULL);
  866. py_bindproperty(vec3i, "z", vec3i__z, NULL);
  867. py_bindmethod(vec3i, "with_x", vec3i__with_x);
  868. py_bindmethod(vec3i, "with_y", vec3i__with_y);
  869. py_bindmethod(vec3i, "with_z", vec3i__with_z);
  870. py_bindmethod(vec3i, "dot", vec3i_dot);
  871. py_newvec3i(_const(vec3i, "ZERO"),
  872. (c11_vec3i){
  873. {0, 0, 0}
  874. });
  875. py_newvec3i(_const(vec3i, "ONE"),
  876. (c11_vec3i){
  877. {1, 1, 1}
  878. });
  879. /* vec3 */
  880. py_bindmagic(vec3, __new__, vec3__new__);
  881. py_bindmagic(vec3, __add__, vec3__add__);
  882. py_bindmagic(vec3, __sub__, vec3__sub__);
  883. py_bindmagic(vec3, __mul__, vec3__mul__);
  884. py_bindmagic(vec3, __truediv__, vec3__truediv__);
  885. py_bindmagic(vec3, __repr__, vec3__repr__);
  886. py_bindmagic(vec3, __eq__, vec3__eq__);
  887. py_bindmagic(vec3, __ne__, vec3__ne__);
  888. py_bindmethod(vec3, "dot", vec3_dot);
  889. py_bindmethod(vec3, "length", vec3_length);
  890. py_bindmethod(vec3, "length_squared", vec3_length_squared);
  891. py_bindmethod(vec3, "normalize", vec3_normalize);
  892. py_bindproperty(vec3, "x", vec3__x, NULL);
  893. py_bindproperty(vec3, "y", vec3__y, NULL);
  894. py_bindproperty(vec3, "z", vec3__z, NULL);
  895. py_bindproperty(vec3, "xy", vec3__xy, NULL);
  896. py_bindmethod(vec3, "with_x", vec3__with_x);
  897. py_bindmethod(vec3, "with_y", vec3__with_y);
  898. py_bindmethod(vec3, "with_z", vec3__with_z);
  899. py_bindmethod(vec3, "with_xy", vec3__with_xy);
  900. py_newvec3(_const(vec3, "ZERO"),
  901. (c11_vec3){
  902. {0, 0, 0}
  903. });
  904. py_newvec3(_const(vec3, "ONE"),
  905. (c11_vec3){
  906. {1, 1, 1}
  907. });
  908. }
  909. #undef DEFINE_VEC_FIELD
  910. #undef DEFINE_BOOL_NE
  911. #undef DEF_VECTOR_ELEMENT_WISE
  912. #undef DEF_VECTOR_OPS
  913. #undef DEF_VECTOR_INT_OPS