linalg.c 47 KB

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