linalg.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. DEF_VECTOR_INT_OPS(2)
  284. DEF_VECTOR_INT_OPS(3)
  285. static bool vec2i__hash__(int argc, py_Ref argv) {
  286. PY_CHECK_ARGC(1);
  287. c11_vec2i v = py_tovec2i(argv);
  288. uint64_t hash = ((uint64_t)v.x << 32) | (uint64_t)v.y;
  289. py_newint(py_retval(), (py_i64)hash);
  290. return true;
  291. }
  292. static bool vec3i__hash__(int argc, py_Ref argv) {
  293. PY_CHECK_ARGC(1);
  294. c11_vec3i v = py_tovec3i(argv);
  295. uint64_t x_part = (uint64_t)(v.x & 0xFFFFFF);
  296. uint64_t y_part = (uint64_t)(v.y & 0xFFFFFF);
  297. uint64_t z_part = (uint64_t)(v.z & 0xFFFF);
  298. uint64_t hash = (x_part << 40) | (y_part << 16) | z_part;
  299. py_newint(py_retval(), (py_i64)hash);
  300. return true;
  301. }
  302. static bool vec2__repr__(int argc, py_Ref argv) {
  303. PY_CHECK_ARGC(1);
  304. char buf[64];
  305. int size = snprintf(buf, 64, "vec2(%.4f, %.4f)", argv[0]._vec2.x, argv[0]._vec2.y);
  306. py_newstrv(py_retval(), (c11_sv){buf, size});
  307. return true;
  308. }
  309. static bool vec2_rotate(int argc, py_Ref argv) {
  310. PY_CHECK_ARGC(2);
  311. py_f64 radians;
  312. if(!py_castfloat(&argv[1], &radians)) return false;
  313. float cr = cosf(radians);
  314. float sr = sinf(radians);
  315. c11_vec2 res;
  316. res.x = argv[0]._vec2.x * cr - argv[0]._vec2.y * sr;
  317. res.y = argv[0]._vec2.x * sr + argv[0]._vec2.y * cr;
  318. py_newvec2(py_retval(), res);
  319. return true;
  320. }
  321. static bool vec2_angle_STATIC(int argc, py_Ref argv) {
  322. PY_CHECK_ARGC(2);
  323. PY_CHECK_ARG_TYPE(0, tp_vec2);
  324. PY_CHECK_ARG_TYPE(1, tp_vec2);
  325. float val = atan2f(argv[1]._vec2.y, argv[1]._vec2.x) - atan2f(argv[0]._vec2.y, argv[0]._vec2.x);
  326. const float PI = 3.1415926535897932384f;
  327. if(val > PI) val -= 2 * PI;
  328. if(val < -PI) val += 2 * PI;
  329. py_newfloat(py_retval(), val);
  330. return true;
  331. }
  332. static bool vec2_smoothdamp_STATIC(int argc, py_Ref argv) {
  333. PY_CHECK_ARGC(6);
  334. PY_CHECK_ARG_TYPE(0, tp_vec2); // current: vec2
  335. PY_CHECK_ARG_TYPE(1, tp_vec2); // target: vec2
  336. PY_CHECK_ARG_TYPE(2, tp_vec2); // current_velocity: vec2
  337. float smoothTime;
  338. if(!py_castfloat32(&argv[3], &smoothTime)) return false;
  339. float maxSpeed;
  340. if(!py_castfloat32(&argv[4], &maxSpeed)) return false;
  341. float deltaTime;
  342. if(!py_castfloat32(&argv[5], &deltaTime)) return false;
  343. c11_vec2 current = argv[0]._vec2;
  344. c11_vec2 target = argv[1]._vec2;
  345. c11_vec2 currentVelocity = argv[2]._vec2;
  346. // https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector2.cs#L289
  347. // Based on Game Programming Gems 4 Chapter 1.10
  348. smoothTime = c11__max(0.0001F, smoothTime);
  349. float omega = 2.0F / smoothTime;
  350. float x = omega * deltaTime;
  351. float exp = 1.0F / (1.0F + x + 0.48F * x * x + 0.235F * x * x * x);
  352. float change_x = current.x - target.x;
  353. float change_y = current.y - target.y;
  354. c11_vec2 originalTo = target;
  355. // Clamp maximum speed
  356. float maxChange = maxSpeed * smoothTime;
  357. float maxChangeSq = maxChange * maxChange;
  358. float sqDist = change_x * change_x + change_y * change_y;
  359. if(sqDist > maxChangeSq) {
  360. float mag = sqrtf(sqDist);
  361. change_x = change_x / mag * maxChange;
  362. change_y = change_y / mag * maxChange;
  363. }
  364. target.x = current.x - change_x;
  365. target.y = current.y - change_y;
  366. float temp_x = (currentVelocity.x + omega * change_x) * deltaTime;
  367. float temp_y = (currentVelocity.y + omega * change_y) * deltaTime;
  368. currentVelocity.x = (currentVelocity.x - omega * temp_x) * exp;
  369. currentVelocity.y = (currentVelocity.y - omega * temp_y) * exp;
  370. float output_x = target.x + (change_x + temp_x) * exp;
  371. float output_y = target.y + (change_y + temp_y) * exp;
  372. // Prevent overshooting
  373. float origMinusCurrent_x = originalTo.x - current.x;
  374. float origMinusCurrent_y = originalTo.y - current.y;
  375. float outMinusOrig_x = output_x - originalTo.x;
  376. float outMinusOrig_y = output_y - originalTo.y;
  377. if(origMinusCurrent_x * outMinusOrig_x + origMinusCurrent_y * outMinusOrig_y > 0) {
  378. output_x = originalTo.x;
  379. output_y = originalTo.y;
  380. currentVelocity.x = (output_x - originalTo.x) / deltaTime;
  381. currentVelocity.y = (output_y - originalTo.y) / deltaTime;
  382. }
  383. py_Ref ret = py_retval();
  384. py_newtuple(ret, 2);
  385. py_newvec2(py_tuple_getitem(ret, 0),
  386. (c11_vec2){
  387. {output_x, output_y}
  388. });
  389. py_newvec2(py_tuple_getitem(ret, 1), currentVelocity);
  390. return true;
  391. }
  392. DEFINE_VEC_FIELD(vec2, float, py_f64, x)
  393. DEFINE_VEC_FIELD(vec2, float, py_f64, y)
  394. static bool vec2__with_z(int argc, py_Ref argv) {
  395. PY_CHECK_ARGC(2);
  396. float z;
  397. if(!py_castfloat32(&argv[1], &z)) return false;
  398. c11_vec3 v = {
  399. {argv->_vec2.x, argv->_vec2.y, z}
  400. };
  401. py_newvec3(py_retval(), v);
  402. return true;
  403. }
  404. /* mat3x3 */
  405. static bool mat3x3__new__(int argc, py_Ref argv) {
  406. PY_CHECK_ARGC(10);
  407. c11_mat3x3* m = py_newmat3x3(py_retval());
  408. for(int i = 0; i < 9; i++) {
  409. py_f64 val;
  410. if(!py_castfloat(&argv[i + 1], &val)) return false;
  411. m->data[i] = val;
  412. }
  413. return true;
  414. }
  415. static bool mat3x3__repr__(int argc, py_Ref argv) {
  416. PY_CHECK_ARGC(1);
  417. c11_mat3x3* m = py_tomat3x3(argv);
  418. char buf[256];
  419. const char* fmt =
  420. "mat3x3(%.4f, %.4f, %.4f,\n %.4f, %.4f, %.4f,\n "
  421. " %.4f, %.4f, %.4f)";
  422. int size = snprintf(buf,
  423. 256,
  424. fmt,
  425. m->data[0],
  426. m->data[1],
  427. m->data[2],
  428. m->data[3],
  429. m->data[4],
  430. m->data[5],
  431. m->data[6],
  432. m->data[7],
  433. m->data[8]);
  434. py_newstrv(py_retval(), (c11_sv){buf, size});
  435. return true;
  436. }
  437. static bool mat3x3__getitem__(int argc, py_Ref argv) {
  438. PY_CHECK_ARGC(2);
  439. PY_CHECK_ARG_TYPE(1, tp_tuple);
  440. c11_mat3x3* ud = py_tomat3x3(argv);
  441. if(py_tuple_len(&argv[1]) != 2) return IndexError("expected a tuple of length 2");
  442. py_Ref i = py_tuple_getitem(&argv[1], 0);
  443. py_Ref j = py_tuple_getitem(&argv[1], 1);
  444. if(!py_checktype(i, tp_int) || !py_checktype(j, tp_int)) return false;
  445. if(i->_i64 < 0 || i->_i64 >= 3 || j->_i64 < 0 || j->_i64 >= 3) {
  446. return IndexError("index out of range");
  447. }
  448. py_newfloat(py_retval(), ud->m[i->_i64][j->_i64]);
  449. return true;
  450. }
  451. static bool mat3x3__setitem__(int argc, py_Ref argv) {
  452. PY_CHECK_ARGC(3);
  453. PY_CHECK_ARG_TYPE(1, tp_tuple);
  454. c11_mat3x3* ud = py_tomat3x3(argv);
  455. if(py_tuple_len(&argv[1]) != 2) return IndexError("expected a tuple of length 2");
  456. py_Ref i = py_tuple_getitem(&argv[1], 0);
  457. py_Ref j = py_tuple_getitem(&argv[1], 1);
  458. if(!py_checktype(i, tp_int) || !py_checktype(j, tp_int)) return false;
  459. py_f64 val;
  460. if(!py_castfloat(&argv[2], &val)) return false;
  461. if(i->_i64 < 0 || i->_i64 >= 3 || j->_i64 < 0 || j->_i64 >= 3) {
  462. return IndexError("index out of range");
  463. }
  464. ud->m[i->_i64][j->_i64] = val;
  465. py_newnone(py_retval());
  466. return true;
  467. }
  468. static bool mat3x3__eq__(int argc, py_Ref argv) {
  469. PY_CHECK_ARGC(2);
  470. if(argv[1].type != tp_mat3x3) {
  471. py_newnotimplemented(py_retval());
  472. return true;
  473. }
  474. c11_mat3x3* lhs = py_tomat3x3(argv);
  475. c11_mat3x3* rhs = py_tomat3x3(&argv[1]);
  476. for(int i = 0; i < 9; i++) {
  477. if(!isclose(lhs->data[i], rhs->data[i])) {
  478. py_newbool(py_retval(), false);
  479. return true;
  480. }
  481. }
  482. py_newbool(py_retval(), true);
  483. return true;
  484. }
  485. DEFINE_BOOL_NE(mat3x3, mat3x3__eq__)
  486. static void matmul(const c11_mat3x3* lhs, const c11_mat3x3* rhs, c11_mat3x3* out) {
  487. out->_11 = lhs->_11 * rhs->_11 + lhs->_12 * rhs->_21 + lhs->_13 * rhs->_31;
  488. out->_12 = lhs->_11 * rhs->_12 + lhs->_12 * rhs->_22 + lhs->_13 * rhs->_32;
  489. out->_13 = lhs->_11 * rhs->_13 + lhs->_12 * rhs->_23 + lhs->_13 * rhs->_33;
  490. out->_21 = lhs->_21 * rhs->_11 + lhs->_22 * rhs->_21 + lhs->_23 * rhs->_31;
  491. out->_22 = lhs->_21 * rhs->_12 + lhs->_22 * rhs->_22 + lhs->_23 * rhs->_32;
  492. out->_23 = lhs->_21 * rhs->_13 + lhs->_22 * rhs->_23 + lhs->_23 * rhs->_33;
  493. out->_31 = lhs->_31 * rhs->_11 + lhs->_32 * rhs->_21 + lhs->_33 * rhs->_31;
  494. out->_32 = lhs->_31 * rhs->_12 + lhs->_32 * rhs->_22 + lhs->_33 * rhs->_32;
  495. out->_33 = lhs->_31 * rhs->_13 + lhs->_32 * rhs->_23 + lhs->_33 * rhs->_33;
  496. }
  497. static float determinant(const c11_mat3x3* m) {
  498. return m->_11 * (m->_22 * m->_33 - m->_23 * m->_32) -
  499. m->_12 * (m->_21 * m->_33 - m->_23 * m->_31) +
  500. m->_13 * (m->_21 * m->_32 - m->_22 * m->_31);
  501. }
  502. static bool inverse(const c11_mat3x3* m, c11_mat3x3* out) {
  503. float det = determinant(m);
  504. if(isclose(det, 0)) return false;
  505. float invdet = 1.0f / det;
  506. out->_11 = (m->_22 * m->_33 - m->_23 * m->_32) * invdet;
  507. out->_12 = (m->_13 * m->_32 - m->_12 * m->_33) * invdet;
  508. out->_13 = (m->_12 * m->_23 - m->_13 * m->_22) * invdet;
  509. out->_21 = (m->_23 * m->_31 - m->_21 * m->_33) * invdet;
  510. out->_22 = (m->_11 * m->_33 - m->_13 * m->_31) * invdet;
  511. out->_23 = (m->_13 * m->_21 - m->_11 * m->_23) * invdet;
  512. out->_31 = (m->_21 * m->_32 - m->_22 * m->_31) * invdet;
  513. out->_32 = (m->_12 * m->_31 - m->_11 * m->_32) * invdet;
  514. out->_33 = (m->_11 * m->_22 - m->_12 * m->_21) * invdet;
  515. return true;
  516. }
  517. static void trs(c11_vec2 t, float r, c11_vec2 s, c11_mat3x3* out) {
  518. float cr = cosf(r);
  519. float sr = sinf(r);
  520. // clang-format off
  521. *out = (c11_mat3x3){
  522. ._11 = s.x * cr, ._12 = -s.y * sr, ._13 = t.x,
  523. ._21 = s.x * sr, ._22 = s.y * cr, ._23 = t.y,
  524. ._31 = 0, ._32 = 0, ._33 = 1,
  525. };
  526. // clang-format on
  527. }
  528. static bool mat3x3__matmul__(int argc, py_Ref argv) {
  529. PY_CHECK_ARGC(2);
  530. c11_mat3x3* lhs = py_tomat3x3(argv);
  531. if(argv[1].type == tp_mat3x3) {
  532. c11_mat3x3* rhs = py_tomat3x3(&argv[1]);
  533. c11_mat3x3* out = py_newmat3x3(py_retval());
  534. matmul(lhs, rhs, out);
  535. } else if(argv[1].type == tp_vec3) {
  536. c11_vec3 rhs = py_tovec3(&argv[1]);
  537. c11_vec3 res;
  538. res.x = lhs->_11 * rhs.x + lhs->_12 * rhs.y + lhs->_13 * rhs.z;
  539. res.y = lhs->_21 * rhs.x + lhs->_22 * rhs.y + lhs->_23 * rhs.z;
  540. res.z = lhs->_31 * rhs.x + lhs->_32 * rhs.y + lhs->_33 * rhs.z;
  541. py_newvec3(py_retval(), res);
  542. } else {
  543. py_newnotimplemented(py_retval());
  544. }
  545. return true;
  546. }
  547. static bool mat3x3__invert__(int argc, py_Ref argv) {
  548. PY_CHECK_ARGC(1);
  549. c11_mat3x3* ud = py_tomat3x3(argv);
  550. c11_mat3x3* out = py_newmat3x3(py_retval());
  551. if(inverse(ud, out)) return true;
  552. return ZeroDivisionError("matrix is not invertible");
  553. }
  554. static bool mat3x3_matmul(int argc, py_Ref argv) {
  555. PY_CHECK_ARGC(3);
  556. PY_CHECK_ARG_TYPE(0, tp_mat3x3);
  557. PY_CHECK_ARG_TYPE(1, tp_mat3x3);
  558. PY_CHECK_ARG_TYPE(2, tp_mat3x3);
  559. c11_mat3x3* lhs = py_tomat3x3(&argv[0]);
  560. c11_mat3x3* rhs = py_tomat3x3(&argv[1]);
  561. c11_mat3x3* out = py_tomat3x3(&argv[2]);
  562. matmul(lhs, rhs, out);
  563. py_newnone(py_retval());
  564. return true;
  565. }
  566. static bool mat3x3_determinant(int argc, py_Ref argv) {
  567. PY_CHECK_ARGC(1);
  568. c11_mat3x3* ud = py_tomat3x3(argv);
  569. py_newfloat(py_retval(), determinant(ud));
  570. return true;
  571. }
  572. static bool mat3x3_copy(int argc, py_Ref argv) {
  573. PY_CHECK_ARGC(1);
  574. c11_mat3x3* ud = py_tomat3x3(argv);
  575. c11_mat3x3* out = py_newmat3x3(py_retval());
  576. *out = *ud;
  577. return true;
  578. }
  579. static bool mat3x3_inverse(int argc, py_Ref argv) {
  580. PY_CHECK_ARGC(1);
  581. c11_mat3x3* ud = py_tomat3x3(argv);
  582. c11_mat3x3* out = py_newmat3x3(py_retval());
  583. if(inverse(ud, out)) return true;
  584. return ZeroDivisionError("matrix is not invertible");
  585. }
  586. static bool mat3x3_copy_(int argc, py_Ref argv) {
  587. PY_CHECK_ARGC(2);
  588. PY_CHECK_ARG_TYPE(1, tp_mat3x3);
  589. c11_mat3x3* self = py_tomat3x3(argv);
  590. c11_mat3x3* other = py_tomat3x3(&argv[1]);
  591. *self = *other;
  592. py_newnone(py_retval());
  593. return true;
  594. }
  595. static bool mat3x3_inverse_(int argc, py_Ref argv) {
  596. PY_CHECK_ARGC(1);
  597. c11_mat3x3* ud = py_tomat3x3(argv);
  598. c11_mat3x3 res;
  599. if(inverse(ud, &res)) {
  600. *ud = res;
  601. py_newnone(py_retval());
  602. return true;
  603. }
  604. return ZeroDivisionError("matrix is not invertible");
  605. }
  606. static bool mat3x3_zeros_STATIC(int argc, py_Ref argv) {
  607. PY_CHECK_ARGC(0);
  608. c11_mat3x3* out = py_newmat3x3(py_retval());
  609. memset(out, 0, sizeof(c11_mat3x3));
  610. return true;
  611. }
  612. static bool mat3x3_identity_STATIC(int argc, py_Ref argv) {
  613. PY_CHECK_ARGC(0);
  614. c11_mat3x3* out = py_newmat3x3(py_retval());
  615. // clang-format off
  616. *out = (c11_mat3x3){
  617. ._11 = 1, ._12 = 0, ._13 = 0,
  618. ._21 = 0, ._22 = 1, ._23 = 0,
  619. ._31 = 0, ._32 = 0, ._33 = 1,
  620. };
  621. // clang-format on
  622. return true;
  623. }
  624. static bool mat3x3_trs_STATIC(int argc, py_Ref argv) {
  625. PY_CHECK_ARGC(3);
  626. py_f64 r;
  627. if(!py_checktype(&argv[0], tp_vec2)) return false;
  628. if(!py_castfloat(&argv[1], &r)) return false;
  629. if(!py_checktype(&argv[2], tp_vec2)) return false;
  630. c11_vec2 t = py_tovec2(&argv[0]);
  631. c11_vec2 s = py_tovec2(&argv[2]);
  632. c11_mat3x3* out = py_newmat3x3(py_retval());
  633. trs(t, r, s, out);
  634. return true;
  635. }
  636. static bool mat3x3_copy_trs_(int argc, py_Ref argv) {
  637. PY_CHECK_ARGC(4);
  638. c11_mat3x3* ud = py_tomat3x3(&argv[0]);
  639. py_f64 r;
  640. if(!py_checktype(&argv[1], tp_vec2)) return false;
  641. if(!py_castfloat(&argv[2], &r)) return false;
  642. if(!py_checktype(&argv[3], tp_vec2)) return false;
  643. c11_vec2 t = py_tovec2(&argv[1]);
  644. c11_vec2 s = py_tovec2(&argv[3]);
  645. trs(t, r, s, ud);
  646. py_newnone(py_retval());
  647. return true;
  648. }
  649. static bool mat3x3_t(int argc, py_Ref argv) {
  650. PY_CHECK_ARGC(1);
  651. c11_mat3x3* ud = py_tomat3x3(argv);
  652. c11_vec2 res;
  653. res.x = ud->_13;
  654. res.y = ud->_23;
  655. py_newvec2(py_retval(), res);
  656. return true;
  657. }
  658. static bool mat3x3_r(int argc, py_Ref argv) {
  659. PY_CHECK_ARGC(1);
  660. c11_mat3x3* ud = py_tomat3x3(argv);
  661. float r = atan2f(ud->_21, ud->_11);
  662. py_newfloat(py_retval(), r);
  663. return true;
  664. }
  665. static bool mat3x3_s(int argc, py_Ref argv) {
  666. PY_CHECK_ARGC(1);
  667. c11_mat3x3* ud = py_tomat3x3(argv);
  668. c11_vec2 res;
  669. res.x = sqrtf(ud->_11 * ud->_11 + ud->_21 * ud->_21);
  670. res.y = sqrtf(ud->_12 * ud->_12 + ud->_22 * ud->_22);
  671. py_newvec2(py_retval(), res);
  672. return true;
  673. }
  674. static bool mat3x3_transform_point(int argc, py_Ref argv) {
  675. PY_CHECK_ARGC(2);
  676. PY_CHECK_ARG_TYPE(1, tp_vec2);
  677. c11_mat3x3* ud = py_tomat3x3(&argv[0]);
  678. c11_vec2 p = py_tovec2(&argv[1]);
  679. c11_vec2 res;
  680. res.x = ud->_11 * p.x + ud->_12 * p.y + ud->_13;
  681. res.y = ud->_21 * p.x + ud->_22 * p.y + ud->_23;
  682. py_newvec2(py_retval(), res);
  683. return true;
  684. }
  685. static bool mat3x3_transform_vector(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;
  692. res.y = ud->_21 * p.x + ud->_22 * p.y;
  693. py_newvec2(py_retval(), res);
  694. return true;
  695. }
  696. /* vec2i */
  697. DEFINE_VEC_FIELD(vec2i, int, py_i64, x)
  698. DEFINE_VEC_FIELD(vec2i, int, py_i64, y)
  699. static bool vec2i__repr__(int argc, py_Ref argv) {
  700. PY_CHECK_ARGC(1);
  701. c11_vec2i data = py_tovec2i(argv);
  702. char buf[64];
  703. int size = snprintf(buf, 64, "vec2i(%d, %d)", data.x, data.y);
  704. py_newstrv(py_retval(), (c11_sv){buf, size});
  705. return true;
  706. }
  707. /* vec3i */
  708. static bool vec3i__repr__(int argc, py_Ref argv) {
  709. PY_CHECK_ARGC(1);
  710. c11_vec3i data = py_tovec3i(argv);
  711. char buf[64];
  712. int size = snprintf(buf, 64, "vec3i(%d, %d, %d)", data.x, data.y, data.z);
  713. py_newstrv(py_retval(), (c11_sv){buf, size});
  714. return true;
  715. }
  716. DEFINE_VEC_FIELD(vec3i, int, py_i64, x)
  717. DEFINE_VEC_FIELD(vec3i, int, py_i64, y)
  718. DEFINE_VEC_FIELD(vec3i, int, py_i64, z)
  719. /* vec3 */
  720. static bool vec3__repr__(int argc, py_Ref argv) {
  721. PY_CHECK_ARGC(1);
  722. c11_vec3 data = py_tovec3(argv);
  723. char buf[64];
  724. int size = snprintf(buf, 64, "vec3(%.4f, %.4f, %.4f)", data.x, data.y, data.z);
  725. py_newstrv(py_retval(), (c11_sv){buf, size});
  726. return true;
  727. }
  728. DEFINE_VEC_FIELD(vec3, float, py_f64, x)
  729. DEFINE_VEC_FIELD(vec3, float, py_f64, y)
  730. DEFINE_VEC_FIELD(vec3, float, py_f64, z)
  731. static bool vec3__xy(int argc, py_Ref argv) {
  732. PY_CHECK_ARGC(1);
  733. c11_vec3 data = py_tovec3(argv);
  734. c11_vec2 res = {
  735. {data.x, data.y}
  736. };
  737. py_newvec2(py_retval(), res);
  738. return true;
  739. }
  740. static bool vec3__with_xy(int argc, py_Ref argv) {
  741. PY_CHECK_ARGC(2);
  742. PY_CHECK_ARG_TYPE(1, tp_vec2);
  743. c11_vec2 xy = py_tovec2(&argv[1]);
  744. c11_vec3 res = {
  745. {xy.x, xy.y, py_tovec3(argv).z}
  746. };
  747. py_newvec3(py_retval(), res);
  748. return true;
  749. }
  750. void pk__add_module_linalg() {
  751. py_Ref mod = py_newmodule("linalg");
  752. py_Type vec2 = pk_newtype("vec2", tp_object, mod, NULL, false, true);
  753. py_Type vec3 = pk_newtype("vec3", tp_object, mod, NULL, false, true);
  754. py_Type vec2i = pk_newtype("vec2i", tp_object, mod, NULL, false, true);
  755. py_Type vec3i = pk_newtype("vec3i", tp_object, mod, NULL, false, true);
  756. py_Type mat3x3 = pk_newtype("mat3x3", tp_object, mod, NULL, false, true);
  757. py_setdict(mod, py_name("vec2"), py_tpobject(vec2));
  758. py_setdict(mod, py_name("vec3"), py_tpobject(vec3));
  759. py_setdict(mod, py_name("vec2i"), py_tpobject(vec2i));
  760. py_setdict(mod, py_name("vec3i"), py_tpobject(vec3i));
  761. py_setdict(mod, py_name("mat3x3"), py_tpobject(mat3x3));
  762. assert(vec2 == tp_vec2);
  763. assert(vec3 == tp_vec3);
  764. assert(vec2i == tp_vec2i);
  765. assert(vec3i == tp_vec3i);
  766. assert(mat3x3 == tp_mat3x3);
  767. /* vec2 */
  768. py_bindmagic(vec2, __new__, vec2__new__);
  769. py_bindmagic(vec2, __add__, vec2__add__);
  770. py_bindmagic(vec2, __sub__, vec2__sub__);
  771. py_bindmagic(vec2, __mul__, vec2__mul__);
  772. py_bindmagic(vec2, __truediv__, vec2__truediv__);
  773. py_bindmagic(vec2, __repr__, vec2__repr__);
  774. py_bindmagic(vec2, __eq__, vec2__eq__);
  775. py_bindmagic(vec2, __ne__, vec2__ne__);
  776. py_bindmethod(vec2, "dot", vec2_dot);
  777. py_bindmethod(vec2, "length", vec2_length);
  778. py_bindmethod(vec2, "length_squared", vec2_length_squared);
  779. py_bindmethod(vec2, "normalize", vec2_normalize);
  780. py_bindmethod(vec2, "rotate", vec2_rotate);
  781. // clang-format off
  782. py_newvec2(_const(vec2, "ZERO"), (c11_vec2){{0, 0}});
  783. py_newvec2(_const(vec2, "ONE"), (c11_vec2){{1, 1}});
  784. py_newvec2(_const(vec2, "LEFT"), (c11_vec2){{-1, 0}});
  785. py_newvec2(_const(vec2, "RIGHT"), (c11_vec2){{1, 0}});
  786. py_newvec2(_const(vec2, "UP"), (c11_vec2){{0, -1}});
  787. py_newvec2(_const(vec2, "DOWN"), (c11_vec2){{0, 1}});
  788. // clang-format on
  789. py_bindstaticmethod(vec2, "angle", vec2_angle_STATIC);
  790. py_bindstaticmethod(vec2, "smooth_damp", vec2_smoothdamp_STATIC);
  791. py_bindproperty(vec2, "x", vec2__x, NULL);
  792. py_bindproperty(vec2, "y", vec2__y, NULL);
  793. py_bindmethod(vec2, "with_x", vec2__with_x);
  794. py_bindmethod(vec2, "with_y", vec2__with_y);
  795. py_bindmethod(vec2, "with_z", vec2__with_z);
  796. /* mat3x3 */
  797. py_bindmagic(mat3x3, __new__, mat3x3__new__);
  798. py_bindmagic(mat3x3, __repr__, mat3x3__repr__);
  799. py_bindmagic(mat3x3, __getitem__, mat3x3__getitem__);
  800. py_bindmagic(mat3x3, __setitem__, mat3x3__setitem__);
  801. py_bindmagic(mat3x3, __matmul__, mat3x3__matmul__);
  802. py_bindmagic(mat3x3, __invert__, mat3x3__invert__);
  803. py_bindmagic(mat3x3, __eq__, mat3x3__eq__);
  804. py_bindmagic(mat3x3, __ne__, mat3x3__ne__);
  805. py_bindmethod(mat3x3, "matmul", mat3x3_matmul);
  806. py_bindmethod(mat3x3, "determinant", mat3x3_determinant);
  807. py_bindmethod(mat3x3, "copy", mat3x3_copy);
  808. py_bindmethod(mat3x3, "inverse", mat3x3_inverse);
  809. py_bindmethod(mat3x3, "copy_", mat3x3_copy_);
  810. py_bindmethod(mat3x3, "inverse_", mat3x3_inverse_);
  811. py_bindstaticmethod(mat3x3, "zeros", mat3x3_zeros_STATIC);
  812. py_bindstaticmethod(mat3x3, "identity", mat3x3_identity_STATIC);
  813. py_bindstaticmethod(mat3x3, "trs", mat3x3_trs_STATIC);
  814. py_bindmethod(mat3x3, "copy_trs_", mat3x3_copy_trs_);
  815. py_bindmethod(mat3x3, "t", mat3x3_t);
  816. py_bindmethod(mat3x3, "r", mat3x3_r);
  817. py_bindmethod(mat3x3, "s", mat3x3_s);
  818. py_bindmethod(mat3x3, "transform_point", mat3x3_transform_point);
  819. py_bindmethod(mat3x3, "transform_vector", mat3x3_transform_vector);
  820. /* vec2i */
  821. py_bindmagic(vec2i, __new__, vec2i__new__);
  822. py_bindmagic(vec2i, __repr__, vec2i__repr__);
  823. py_bindmagic(vec2i, __add__, vec2i__add__);
  824. py_bindmagic(vec2i, __sub__, vec2i__sub__);
  825. py_bindmagic(vec2i, __mul__, vec2i__mul__);
  826. py_bindmagic(vec2i, __eq__, vec2i__eq__);
  827. py_bindmagic(vec2i, __ne__, vec2i__ne__);
  828. py_bindmagic(vec2i, __hash__, vec2i__hash__);
  829. py_bindproperty(vec2i, "x", vec2i__x, NULL);
  830. py_bindproperty(vec2i, "y", vec2i__y, NULL);
  831. py_bindmethod(vec2i, "with_x", vec2i__with_x);
  832. py_bindmethod(vec2i, "with_y", vec2i__with_y);
  833. py_bindmethod(vec2i, "dot", vec2i_dot);
  834. // clang-format off
  835. py_newvec2i(_const(vec2i, "ZERO"), (c11_vec2i){{0, 0}});
  836. py_newvec2i(_const(vec2i, "ONE"), (c11_vec2i){{1, 1}});
  837. py_newvec2i(_const(vec2i, "LEFT"), (c11_vec2i){{-1, 0}});
  838. py_newvec2i(_const(vec2i, "RIGHT"), (c11_vec2i){{1, 0}});
  839. py_newvec2i(_const(vec2i, "UP"), (c11_vec2i){{0, -1}});
  840. py_newvec2i(_const(vec2i, "DOWN"), (c11_vec2i){{0, 1}});
  841. // clang-format on
  842. /* vec3i */
  843. py_bindmagic(vec3i, __new__, vec3i__new__);
  844. py_bindmagic(vec3i, __repr__, vec3i__repr__);
  845. py_bindmagic(vec3i, __add__, vec3i__add__);
  846. py_bindmagic(vec3i, __sub__, vec3i__sub__);
  847. py_bindmagic(vec3i, __mul__, vec3i__mul__);
  848. py_bindmagic(vec3i, __eq__, vec3i__eq__);
  849. py_bindmagic(vec3i, __ne__, vec3i__ne__);
  850. py_bindmagic(vec3i, __hash__, vec3i__hash__);
  851. py_bindproperty(vec3i, "x", vec3i__x, NULL);
  852. py_bindproperty(vec3i, "y", vec3i__y, NULL);
  853. py_bindproperty(vec3i, "z", vec3i__z, NULL);
  854. py_bindmethod(vec3i, "with_x", vec3i__with_x);
  855. py_bindmethod(vec3i, "with_y", vec3i__with_y);
  856. py_bindmethod(vec3i, "with_z", vec3i__with_z);
  857. py_bindmethod(vec3i, "dot", vec3i_dot);
  858. py_newvec3i(_const(vec3i, "ZERO"),
  859. (c11_vec3i){
  860. {0, 0, 0}
  861. });
  862. py_newvec3i(_const(vec3i, "ONE"),
  863. (c11_vec3i){
  864. {1, 1, 1}
  865. });
  866. /* vec3 */
  867. py_bindmagic(vec3, __new__, vec3__new__);
  868. py_bindmagic(vec3, __add__, vec3__add__);
  869. py_bindmagic(vec3, __sub__, vec3__sub__);
  870. py_bindmagic(vec3, __mul__, vec3__mul__);
  871. py_bindmagic(vec3, __truediv__, vec3__truediv__);
  872. py_bindmagic(vec3, __repr__, vec3__repr__);
  873. py_bindmagic(vec3, __eq__, vec3__eq__);
  874. py_bindmagic(vec3, __ne__, vec3__ne__);
  875. py_bindmethod(vec3, "dot", vec3_dot);
  876. py_bindmethod(vec3, "length", vec3_length);
  877. py_bindmethod(vec3, "length_squared", vec3_length_squared);
  878. py_bindmethod(vec3, "normalize", vec3_normalize);
  879. py_bindproperty(vec3, "x", vec3__x, NULL);
  880. py_bindproperty(vec3, "y", vec3__y, NULL);
  881. py_bindproperty(vec3, "z", vec3__z, NULL);
  882. py_bindproperty(vec3, "xy", vec3__xy, NULL);
  883. py_bindmethod(vec3, "with_x", vec3__with_x);
  884. py_bindmethod(vec3, "with_y", vec3__with_y);
  885. py_bindmethod(vec3, "with_z", vec3__with_z);
  886. py_bindmethod(vec3, "with_xy", vec3__with_xy);
  887. py_newvec3(_const(vec3, "ZERO"),
  888. (c11_vec3){
  889. {0, 0, 0}
  890. });
  891. py_newvec3(_const(vec3, "ONE"),
  892. (c11_vec3){
  893. {1, 1, 1}
  894. });
  895. }