wrap_Math.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /**
  2. * Copyright (c) 2006-2015 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_CompressedData.h"
  24. #include "MathModule.h"
  25. #include "BezierCurve.h"
  26. #include <cmath>
  27. #include <iostream>
  28. #include <algorithm>
  29. // Put the Lua code directly into a raw string literal.
  30. static const char math_lua[] =
  31. #include "wrap_Math.lua"
  32. ;
  33. namespace love
  34. {
  35. namespace math
  36. {
  37. int w__random(lua_State *L)
  38. {
  39. lua_pushnumber(L, Math::instance.random());
  40. return 1;
  41. }
  42. int w_randomNormal(lua_State *L)
  43. {
  44. double stddev = luaL_optnumber(L, 1, 1.0);
  45. double mean = luaL_optnumber(L, 2, 0.0);
  46. double r = Math::instance.randomNormal(stddev);
  47. lua_pushnumber(L, r + mean);
  48. return 1;
  49. }
  50. int w_setRandomSeed(lua_State *L)
  51. {
  52. luax_catchexcept(L, [&](){ Math::instance.setRandomSeed(luax_checkrandomseed(L, 1)); });
  53. return 0;
  54. }
  55. int w_getRandomSeed(lua_State *L)
  56. {
  57. RandomGenerator::Seed s = Math::instance.getRandomSeed();
  58. lua_pushnumber(L, (lua_Number) s.b32.low);
  59. lua_pushnumber(L, (lua_Number) s.b32.high);
  60. return 2;
  61. }
  62. int w_setRandomState(lua_State *L)
  63. {
  64. luax_catchexcept(L, [&](){ Math::instance.setRandomState(luax_checkstring(L, 1)); });
  65. return 0;
  66. }
  67. int w_getRandomState(lua_State *L)
  68. {
  69. luax_pushstring(L, Math::instance.getRandomState());
  70. return 1;
  71. }
  72. int w_newRandomGenerator(lua_State *L)
  73. {
  74. RandomGenerator::Seed s;
  75. if (lua_gettop(L) > 0)
  76. s = luax_checkrandomseed(L, 1);
  77. RandomGenerator *t = Math::instance.newRandomGenerator();
  78. if (lua_gettop(L) > 0)
  79. {
  80. bool should_error = false;
  81. try
  82. {
  83. t->setSeed(s);
  84. }
  85. catch (love::Exception &e)
  86. {
  87. t->release();
  88. should_error = true;
  89. lua_pushstring(L, e.what());
  90. }
  91. if (should_error)
  92. return luaL_error(L, "%s", lua_tostring(L, -1));
  93. }
  94. luax_pushtype(L, MATH_RANDOM_GENERATOR_ID, t);
  95. t->release();
  96. return 1;
  97. }
  98. int w_newBezierCurve(lua_State *L)
  99. {
  100. std::vector<Vector> points;
  101. if (lua_istable(L, 1))
  102. {
  103. int top = (int) luax_objlen(L, 1);
  104. points.reserve(top / 2);
  105. for (int i = 1; i <= top; i += 2)
  106. {
  107. lua_rawgeti(L, 1, i);
  108. lua_rawgeti(L, 1, i+1);
  109. Vector v;
  110. v.x = (float) luaL_checknumber(L, -2);
  111. v.y = (float) luaL_checknumber(L, -1);
  112. points.push_back(v);
  113. lua_pop(L, 2);
  114. }
  115. }
  116. else
  117. {
  118. int top = (int) lua_gettop(L);
  119. points.reserve(top / 2);
  120. for (int i = 1; i <= top; i += 2)
  121. {
  122. Vector v;
  123. v.x = (float) luaL_checknumber(L, i);
  124. v.y = (float) luaL_checknumber(L, i+1);
  125. points.push_back(v);
  126. }
  127. }
  128. BezierCurve *curve = Math::instance.newBezierCurve(points);
  129. luax_pushtype(L, MATH_BEZIER_CURVE_ID, curve);
  130. curve->release();
  131. return 1;
  132. }
  133. int w_triangulate(lua_State *L)
  134. {
  135. std::vector<Vertex> vertices;
  136. if (lua_istable(L, 1))
  137. {
  138. int top = (int) luax_objlen(L, 1);
  139. vertices.reserve(top / 2);
  140. for (int i = 1; i <= top; i += 2)
  141. {
  142. lua_rawgeti(L, 1, i);
  143. lua_rawgeti(L, 1, i+1);
  144. Vertex v;
  145. v.x = (float) luaL_checknumber(L, -2);
  146. v.y = (float) luaL_checknumber(L, -1);
  147. vertices.push_back(v);
  148. lua_pop(L, 2);
  149. }
  150. }
  151. else
  152. {
  153. int top = (int) lua_gettop(L);
  154. vertices.reserve(top / 2);
  155. for (int i = 1; i <= top; i += 2)
  156. {
  157. Vertex v;
  158. v.x = (float) luaL_checknumber(L, i);
  159. v.y = (float) luaL_checknumber(L, i+1);
  160. vertices.push_back(v);
  161. }
  162. }
  163. if (vertices.size() < 3)
  164. return luaL_error(L, "Need at least 3 vertices to triangulate");
  165. std::vector<Triangle> triangles;
  166. luax_catchexcept(L, [&]() {
  167. if (vertices.size() == 3)
  168. triangles.push_back(Triangle(vertices[0], vertices[1], vertices[2]));
  169. else
  170. triangles = Math::instance.triangulate(vertices);
  171. });
  172. lua_createtable(L, (int) triangles.size(), 0);
  173. for (int i = 0; i < (int) triangles.size(); ++i)
  174. {
  175. const Triangle &tri = triangles[i];
  176. lua_createtable(L, 6, 0);
  177. lua_pushnumber(L, tri.a.x);
  178. lua_rawseti(L, -2, 1);
  179. lua_pushnumber(L, tri.a.y);
  180. lua_rawseti(L, -2, 2);
  181. lua_pushnumber(L, tri.b.x);
  182. lua_rawseti(L, -2, 3);
  183. lua_pushnumber(L, tri.b.y);
  184. lua_rawseti(L, -2, 4);
  185. lua_pushnumber(L, tri.c.x);
  186. lua_rawseti(L, -2, 5);
  187. lua_pushnumber(L, tri.c.y);
  188. lua_rawseti(L, -2, 6);
  189. lua_rawseti(L, -2, i+1);
  190. }
  191. return 1;
  192. }
  193. int w_isConvex(lua_State *L)
  194. {
  195. std::vector<Vertex> vertices;
  196. if (lua_istable(L, 1))
  197. {
  198. int top = (int) luax_objlen(L, 1);
  199. vertices.reserve(top / 2);
  200. for (int i = 1; i <= top; i += 2)
  201. {
  202. lua_rawgeti(L, 1, i);
  203. lua_rawgeti(L, 1, i+1);
  204. Vertex v;
  205. v.x = (float) luaL_checknumber(L, -2);
  206. v.y = (float) luaL_checknumber(L, -1);
  207. vertices.push_back(v);
  208. lua_pop(L, 2);
  209. }
  210. }
  211. else
  212. {
  213. int top = lua_gettop(L);
  214. vertices.reserve(top / 2);
  215. for (int i = 1; i <= top; i += 2)
  216. {
  217. Vertex v;
  218. v.x = (float) luaL_checknumber(L, i);
  219. v.y = (float) luaL_checknumber(L, i+1);
  220. vertices.push_back(v);
  221. }
  222. }
  223. luax_pushboolean(L, Math::instance.isConvex(vertices));
  224. return 1;
  225. }
  226. static int getGammaArgs(lua_State *L, float color[4])
  227. {
  228. int numcomponents = 0;
  229. if (lua_istable(L, 1))
  230. {
  231. int n = (int) luax_objlen(L, 1);
  232. for (int i = 1; i <= n && i <= 4; i++)
  233. {
  234. lua_rawgeti(L, 1, i);
  235. color[i - 1] = (float) luaL_checknumber(L, -1) / 255.0f;
  236. numcomponents++;
  237. }
  238. lua_pop(L, numcomponents);
  239. }
  240. else
  241. {
  242. int n = lua_gettop(L);
  243. for (int i = 1; i <= n && i <= 4; i++)
  244. {
  245. color[i - 1] = (float) luaL_checknumber(L, i) / 255.0f;
  246. numcomponents++;
  247. }
  248. }
  249. if (numcomponents == 0)
  250. luaL_checknumber(L, 1);
  251. return numcomponents;
  252. }
  253. int w_gammaToLinear(lua_State *L)
  254. {
  255. float color[4];
  256. int numcomponents = getGammaArgs(L, color);
  257. for (int i = 0; i < numcomponents; i++)
  258. {
  259. // Alpha should always be linear.
  260. if (i < 3)
  261. color[i] = Math::instance.gammaToLinear(color[i]);
  262. lua_pushnumber(L, color[i] * 255);
  263. }
  264. return numcomponents;
  265. }
  266. int w_linearToGamma(lua_State *L)
  267. {
  268. float color[4];
  269. int numcomponents = getGammaArgs(L, color);
  270. for (int i = 0; i < numcomponents; i++)
  271. {
  272. // Alpha should always be linear.
  273. if (i < 3)
  274. color[i] = Math::instance.linearToGamma(color[i]);
  275. lua_pushnumber(L, color[i] * 255);
  276. }
  277. return numcomponents;
  278. }
  279. int w_noise(lua_State *L)
  280. {
  281. int nargs = std::min(std::max(lua_gettop(L), 1), 4);
  282. float args[4];
  283. for (int i = 0; i < nargs; i++)
  284. args[i] = (float) luaL_checknumber(L, i + 1);
  285. float val = 0.0f;
  286. switch (nargs)
  287. {
  288. case 1:
  289. val = Math::instance.noise(args[0]);
  290. break;
  291. case 2:
  292. val = Math::instance.noise(args[0], args[1]);
  293. break;
  294. case 3:
  295. val = Math::instance.noise(args[0], args[1], args[2]);
  296. break;
  297. case 4:
  298. val = Math::instance.noise(args[0], args[1], args[2], args[3]);
  299. break;
  300. }
  301. lua_pushnumber(L, (lua_Number) val);
  302. return 1;
  303. }
  304. int w_compress(lua_State *L)
  305. {
  306. const char *fstr = lua_isnoneornil(L, 2) ? nullptr : luaL_checkstring(L, 2);
  307. Compressor::Format format = Compressor::FORMAT_LZ4;
  308. if (fstr && !Compressor::getConstant(fstr, format))
  309. return luaL_error(L, "Invalid compressed data format: %s", fstr);
  310. int level = (int) luaL_optnumber(L, 3, -1);
  311. CompressedData *cdata = nullptr;
  312. if (lua_isstring(L, 1))
  313. {
  314. size_t rawsize = 0;
  315. const char *rawbytes = luaL_checklstring(L, 1, &rawsize);
  316. luax_catchexcept(L, [&](){ cdata = Math::instance.compress(format, rawbytes, rawsize, level); });
  317. }
  318. else
  319. {
  320. Data *rawdata = luax_checktype<Data>(L, 1, DATA_ID);
  321. luax_catchexcept(L, [&](){ cdata = Math::instance.compress(format, rawdata, level); });
  322. }
  323. luax_pushtype(L, MATH_COMPRESSED_DATA_ID, cdata);
  324. return 1;
  325. }
  326. int w_decompress(lua_State *L)
  327. {
  328. char *rawbytes = nullptr;
  329. size_t rawsize = 0;
  330. if (luax_istype(L, 1, MATH_COMPRESSED_DATA_ID))
  331. {
  332. CompressedData *data = luax_checkcompresseddata(L, 1);
  333. rawsize = data->getDecompressedSize();
  334. luax_catchexcept(L, [&](){ rawbytes = Math::instance.decompress(data, rawsize); });
  335. }
  336. else
  337. {
  338. Compressor::Format format = Compressor::FORMAT_LZ4;
  339. const char *fstr = luaL_checkstring(L, 2);
  340. if (!Compressor::getConstant(fstr, format))
  341. return luaL_error(L, "Invalid compressed data format: %s", fstr);
  342. size_t compressedsize = 0;
  343. const char *cbytes = nullptr;
  344. if (luax_istype(L, 1, DATA_ID))
  345. {
  346. Data *data = luax_checktype<Data>(L, 1, DATA_ID);
  347. cbytes = (const char *) data->getData();
  348. compressedsize = data->getSize();
  349. }
  350. else
  351. cbytes = luaL_checklstring(L, 1, &compressedsize);
  352. luax_catchexcept(L, [&](){ rawbytes = Math::instance.decompress(format, cbytes, compressedsize, rawsize); });
  353. }
  354. lua_pushlstring(L, rawbytes, rawsize);
  355. delete[] rawbytes;
  356. return 1;
  357. }
  358. // C functions in a struct, necessary for the FFI versions of math functions.
  359. struct FFI_Math
  360. {
  361. double (*random)(void);
  362. float (*noise1)(float x);
  363. float (*noise2)(float x, float y);
  364. float (*noise3)(float x, float y, float z);
  365. float (*noise4)(float x, float y, float z, float w);
  366. float (*gammaToLinear)(float c);
  367. float (*linearToGamma)(float c);
  368. };
  369. static FFI_Math ffifuncs =
  370. {
  371. [](void) -> double // random
  372. {
  373. return Math::instance.random();
  374. },
  375. [](float x) -> float // noise1
  376. {
  377. return Math::instance.noise(x);
  378. },
  379. [](float x, float y) -> float // noise2
  380. {
  381. return Math::instance.noise(x, y);
  382. },
  383. [](float x, float y, float z) -> float // noise3
  384. {
  385. return Math::instance.noise(x, y, z);
  386. },
  387. [](float x, float y, float z, float w) -> float // noise4
  388. {
  389. return Math::instance.noise(x, y, z, w);
  390. },
  391. [](float c) -> float // gammaToLinear
  392. {
  393. return Math::instance.gammaToLinear(c);
  394. },
  395. [](float c) -> float // linearToGamma
  396. {
  397. return Math::instance.linearToGamma(c);
  398. }
  399. };
  400. // List of functions to wrap.
  401. static const luaL_Reg functions[] =
  402. {
  403. { "_random", w__random }, // love.math.random is inside wrap_Math.lua.
  404. { "randomNormal", w_randomNormal },
  405. { "setRandomSeed", w_setRandomSeed },
  406. { "getRandomSeed", w_getRandomSeed },
  407. { "setRandomState", w_setRandomState },
  408. { "getRandomState", w_getRandomState },
  409. { "newRandomGenerator", w_newRandomGenerator },
  410. { "newBezierCurve", w_newBezierCurve },
  411. { "triangulate", w_triangulate },
  412. { "isConvex", w_isConvex },
  413. { "gammaToLinear", w_gammaToLinear },
  414. { "linearToGamma", w_linearToGamma },
  415. { "noise", w_noise },
  416. { "compress", w_compress },
  417. { "decompress", w_decompress },
  418. { 0, 0 }
  419. };
  420. static const lua_CFunction types[] =
  421. {
  422. luaopen_randomgenerator,
  423. luaopen_beziercurve,
  424. luaopen_compresseddata,
  425. 0
  426. };
  427. extern "C" int luaopen_love_math(lua_State *L)
  428. {
  429. Math::instance.retain();
  430. WrappedModule w;
  431. w.module = &Math::instance;
  432. w.name = "math";
  433. w.type = MODULE_ID;
  434. w.functions = functions;
  435. w.types = types;
  436. int n = luax_register_module(L, w);
  437. // Execute wrap_Event.lua, sending the math table and ffifuncs pointer as args.
  438. luaL_loadbuffer(L, math_lua, sizeof(math_lua), "wrap_Math.lua");
  439. lua_pushvalue(L, -2);
  440. lua_pushlightuserdata(L, &ffifuncs);
  441. lua_call(L, 2, 0);
  442. return n;
  443. }
  444. } // math
  445. } // love