wrap_Math.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /**
  2. * Copyright (c) 2006-2021 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "wrap_Math.h"
  21. #include "wrap_RandomGenerator.h"
  22. #include "wrap_BezierCurve.h"
  23. #include "wrap_Transform.h"
  24. #include "MathModule.h"
  25. #include "BezierCurve.h"
  26. #include "Transform.h"
  27. #include <cmath>
  28. #include <iostream>
  29. #include <algorithm>
  30. // Put the Lua code directly into a raw string literal.
  31. static const char math_lua[] =
  32. #include "wrap_Math.lua"
  33. ;
  34. namespace love
  35. {
  36. namespace math
  37. {
  38. #define instance() (Module::getInstance<Math>(Module::M_MATH))
  39. int w__getRandomGenerator(lua_State *L)
  40. {
  41. RandomGenerator *t = instance()->getRandomGenerator();
  42. luax_pushtype(L, t);
  43. return 1;
  44. }
  45. int w_newRandomGenerator(lua_State *L)
  46. {
  47. RandomGenerator::Seed s;
  48. if (lua_gettop(L) > 0)
  49. s = luax_checkrandomseed(L, 1);
  50. RandomGenerator *t = instance()->newRandomGenerator();
  51. if (lua_gettop(L) > 0)
  52. {
  53. bool should_error = false;
  54. try
  55. {
  56. t->setSeed(s);
  57. }
  58. catch (love::Exception &e)
  59. {
  60. t->release();
  61. should_error = true;
  62. lua_pushstring(L, e.what());
  63. }
  64. if (should_error)
  65. return luaL_error(L, "%s", lua_tostring(L, -1));
  66. }
  67. luax_pushtype(L, t);
  68. t->release();
  69. return 1;
  70. }
  71. int w_newBezierCurve(lua_State *L)
  72. {
  73. std::vector<Vector2> points;
  74. if (lua_istable(L, 1))
  75. {
  76. int top = (int) luax_objlen(L, 1);
  77. points.reserve(top / 2);
  78. for (int i = 1; i <= top; i += 2)
  79. {
  80. lua_rawgeti(L, 1, i);
  81. lua_rawgeti(L, 1, i+1);
  82. Vector2 v;
  83. v.x = (float) luaL_checknumber(L, -2);
  84. v.y = (float) luaL_checknumber(L, -1);
  85. points.push_back(v);
  86. lua_pop(L, 2);
  87. }
  88. }
  89. else
  90. {
  91. int top = (int) lua_gettop(L);
  92. points.reserve(top / 2);
  93. for (int i = 1; i <= top; i += 2)
  94. {
  95. Vector2 v;
  96. v.x = (float) luaL_checknumber(L, i);
  97. v.y = (float) luaL_checknumber(L, i+1);
  98. points.push_back(v);
  99. }
  100. }
  101. BezierCurve *curve = instance()->newBezierCurve(points);
  102. luax_pushtype(L, curve);
  103. curve->release();
  104. return 1;
  105. }
  106. int w_newTransform(lua_State *L)
  107. {
  108. Transform *t = nullptr;
  109. if (lua_isnoneornil(L, 1))
  110. t = instance()->newTransform();
  111. else
  112. {
  113. float x = (float) luaL_checknumber(L, 1);
  114. float y = (float) luaL_checknumber(L, 2);
  115. float a = (float) luaL_optnumber(L, 3, 0.0);
  116. float sx = (float) luaL_optnumber(L, 4, 1.0);
  117. float sy = (float) luaL_optnumber(L, 5, sx);
  118. float ox = (float) luaL_optnumber(L, 6, 0.0);
  119. float oy = (float) luaL_optnumber(L, 7, 0.0);
  120. float kx = (float) luaL_optnumber(L, 8, 0.0);
  121. float ky = (float) luaL_optnumber(L, 9, 0.0);
  122. t = instance()->newTransform(x, y, a, sx, sy, ox, oy, kx, ky);
  123. }
  124. luax_pushtype(L, t);
  125. t->release();
  126. return 1;
  127. }
  128. int w_triangulate(lua_State *L)
  129. {
  130. std::vector<love::Vector2> vertices;
  131. if (lua_istable(L, 1))
  132. {
  133. int top = (int) luax_objlen(L, 1);
  134. vertices.reserve(top / 2);
  135. for (int i = 1; i <= top; i += 2)
  136. {
  137. lua_rawgeti(L, 1, i);
  138. lua_rawgeti(L, 1, i+1);
  139. Vector2 v;
  140. v.x = (float) luaL_checknumber(L, -2);
  141. v.y = (float) luaL_checknumber(L, -1);
  142. vertices.push_back(v);
  143. lua_pop(L, 2);
  144. }
  145. }
  146. else
  147. {
  148. int top = (int) lua_gettop(L);
  149. vertices.reserve(top / 2);
  150. for (int i = 1; i <= top; i += 2)
  151. {
  152. Vector2 v;
  153. v.x = (float) luaL_checknumber(L, i);
  154. v.y = (float) luaL_checknumber(L, i+1);
  155. vertices.push_back(v);
  156. }
  157. }
  158. if (vertices.size() < 3)
  159. return luaL_error(L, "Need at least 3 vertices to triangulate");
  160. std::vector<Triangle> triangles;
  161. luax_catchexcept(L, [&]() {
  162. if (vertices.size() == 3)
  163. triangles.push_back(Triangle(vertices[0], vertices[1], vertices[2]));
  164. else
  165. triangles = triangulate(vertices);
  166. });
  167. lua_createtable(L, (int) triangles.size(), 0);
  168. for (int i = 0; i < (int) triangles.size(); ++i)
  169. {
  170. const Triangle &tri = triangles[i];
  171. lua_createtable(L, 6, 0);
  172. lua_pushnumber(L, tri.a.x);
  173. lua_rawseti(L, -2, 1);
  174. lua_pushnumber(L, tri.a.y);
  175. lua_rawseti(L, -2, 2);
  176. lua_pushnumber(L, tri.b.x);
  177. lua_rawseti(L, -2, 3);
  178. lua_pushnumber(L, tri.b.y);
  179. lua_rawseti(L, -2, 4);
  180. lua_pushnumber(L, tri.c.x);
  181. lua_rawseti(L, -2, 5);
  182. lua_pushnumber(L, tri.c.y);
  183. lua_rawseti(L, -2, 6);
  184. lua_rawseti(L, -2, i+1);
  185. }
  186. return 1;
  187. }
  188. int w_isConvex(lua_State *L)
  189. {
  190. std::vector<love::Vector2> vertices;
  191. if (lua_istable(L, 1))
  192. {
  193. int top = (int) luax_objlen(L, 1);
  194. vertices.reserve(top / 2);
  195. for (int i = 1; i <= top; i += 2)
  196. {
  197. lua_rawgeti(L, 1, i);
  198. lua_rawgeti(L, 1, i+1);
  199. love::Vector2 v;
  200. v.x = (float) luaL_checknumber(L, -2);
  201. v.y = (float) luaL_checknumber(L, -1);
  202. vertices.push_back(v);
  203. lua_pop(L, 2);
  204. }
  205. }
  206. else
  207. {
  208. int top = lua_gettop(L);
  209. vertices.reserve(top / 2);
  210. for (int i = 1; i <= top; i += 2)
  211. {
  212. love::Vector2 v;
  213. v.x = (float) luaL_checknumber(L, i);
  214. v.y = (float) luaL_checknumber(L, i+1);
  215. vertices.push_back(v);
  216. }
  217. }
  218. luax_pushboolean(L, isConvex(vertices));
  219. return 1;
  220. }
  221. static int getGammaArgs(lua_State *L, float color[4])
  222. {
  223. int numcomponents = 0;
  224. if (lua_istable(L, 1))
  225. {
  226. int n = (int) luax_objlen(L, 1);
  227. for (int i = 1; i <= n && i <= 4; i++)
  228. {
  229. lua_rawgeti(L, 1, i);
  230. color[i - 1] = (float) luax_checknumberclamped01(L, -1);
  231. numcomponents++;
  232. }
  233. lua_pop(L, numcomponents);
  234. }
  235. else
  236. {
  237. int n = lua_gettop(L);
  238. for (int i = 1; i <= n && i <= 4; i++)
  239. {
  240. color[i - 1] = (float) luax_checknumberclamped01(L, i);
  241. numcomponents++;
  242. }
  243. }
  244. if (numcomponents == 0)
  245. luaL_checknumber(L, 1);
  246. return numcomponents;
  247. }
  248. int w_gammaToLinear(lua_State *L)
  249. {
  250. float color[4];
  251. int numcomponents = getGammaArgs(L, color);
  252. for (int i = 0; i < numcomponents; i++)
  253. {
  254. // Alpha should always be linear.
  255. if (i < 3)
  256. color[i] = gammaToLinear(color[i]);
  257. lua_pushnumber(L, color[i]);
  258. }
  259. return numcomponents;
  260. }
  261. int w_linearToGamma(lua_State *L)
  262. {
  263. float color[4];
  264. int numcomponents = getGammaArgs(L, color);
  265. for (int i = 0; i < numcomponents; i++)
  266. {
  267. // Alpha should always be linear.
  268. if (i < 3)
  269. color[i] = linearToGamma(color[i]);
  270. lua_pushnumber(L, color[i]);
  271. }
  272. return numcomponents;
  273. }
  274. int w_noise(lua_State *L)
  275. {
  276. int nargs = std::min(std::max(lua_gettop(L), 1), 4);
  277. double args[4];
  278. for (int i = 0; i < nargs; i++)
  279. args[i] = luaL_checknumber(L, i + 1);
  280. float val = 0.0f;
  281. switch (nargs)
  282. {
  283. case 1:
  284. val = noise1(args[0]);
  285. break;
  286. case 2:
  287. val = noise2(args[0], args[1]);
  288. break;
  289. case 3:
  290. val = noise3(args[0], args[1], args[2]);
  291. break;
  292. case 4:
  293. val = noise4(args[0], args[1], args[2], args[3]);
  294. break;
  295. }
  296. lua_pushnumber(L, (lua_Number) val);
  297. return 1;
  298. }
  299. // C functions in a struct, necessary for the FFI versions of math functions.
  300. struct FFI_Math
  301. {
  302. float (*noise1)(double x);
  303. float (*noise2)(double x, double y);
  304. float (*noise3)(double x, double y, double z);
  305. float (*noise4)(double x, double y, double z, double w);
  306. float (*gammaToLinear)(float c);
  307. float (*linearToGamma)(float c);
  308. };
  309. static FFI_Math ffifuncs =
  310. {
  311. noise1,
  312. noise2,
  313. noise3,
  314. noise4,
  315. gammaToLinear,
  316. linearToGamma,
  317. };
  318. // List of functions to wrap.
  319. static const luaL_Reg functions[] =
  320. {
  321. // love.math.random etc. are defined in wrap_Math.lua.
  322. { "_getRandomGenerator", w__getRandomGenerator },
  323. { "newRandomGenerator", w_newRandomGenerator },
  324. { "newBezierCurve", w_newBezierCurve },
  325. { "newTransform", w_newTransform },
  326. { "triangulate", w_triangulate },
  327. { "isConvex", w_isConvex },
  328. { "gammaToLinear", w_gammaToLinear },
  329. { "linearToGamma", w_linearToGamma },
  330. { "noise", w_noise },
  331. { 0, 0 }
  332. };
  333. static const lua_CFunction types[] =
  334. {
  335. luaopen_randomgenerator,
  336. luaopen_beziercurve,
  337. luaopen_transform,
  338. 0
  339. };
  340. extern "C" int luaopen_love_math(lua_State *L)
  341. {
  342. Math *instance = instance();
  343. if (instance == nullptr)
  344. {
  345. luax_catchexcept(L, [&](){ instance = new Math(); });
  346. }
  347. else
  348. instance->retain();
  349. WrappedModule w;
  350. w.module = instance;
  351. w.name = "math";
  352. w.type = &Module::type;
  353. w.functions = functions;
  354. w.types = types;
  355. int n = luax_register_module(L, w);
  356. // Execute wrap_Math.lua, sending the math table and ffifuncs pointer as args.
  357. luaL_loadbuffer(L, math_lua, sizeof(math_lua), "=[love \"wrap_Math.lua\"]");
  358. lua_pushvalue(L, -2);
  359. luax_pushpointerasstring(L, &ffifuncs);
  360. lua_call(L, 2, 0);
  361. return n;
  362. }
  363. } // math
  364. } // love