wrap_Math.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * Copyright (c) 2006-2014 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 "MathModule.h"
  24. #include "BezierCurve.h"
  25. #include <cmath>
  26. #include <iostream>
  27. namespace love
  28. {
  29. namespace math
  30. {
  31. int w_random(lua_State *L)
  32. {
  33. return luax_getrandom(L, 1, Math::instance.random());
  34. }
  35. int w_randomNormal(lua_State *L)
  36. {
  37. double stddev = luaL_optnumber(L, 1, 1.0);
  38. double mean = luaL_optnumber(L, 2, 0.0);
  39. double r = Math::instance.randomNormal(stddev);
  40. lua_pushnumber(L, r + mean);
  41. return 1;
  42. }
  43. int w_setRandomSeed(lua_State *L)
  44. {
  45. EXCEPT_GUARD(Math::instance.setRandomSeed(luax_checkrandomseed(L, 1));)
  46. return 0;
  47. }
  48. int w_getRandomSeed(lua_State *L)
  49. {
  50. RandomGenerator::Seed s = Math::instance.getRandomSeed();
  51. lua_pushnumber(L, (lua_Number) s.b32.low);
  52. lua_pushnumber(L, (lua_Number) s.b32.high);
  53. return 2;
  54. }
  55. int w_setRandomState(lua_State *L)
  56. {
  57. EXCEPT_GUARD(Math::instance.setRandomState(luax_checkstring(L, 1));)
  58. return 0;
  59. }
  60. int w_getRandomState(lua_State *L)
  61. {
  62. luax_pushstring(L, Math::instance.getRandomState());
  63. return 1;
  64. }
  65. int w_newRandomGenerator(lua_State *L)
  66. {
  67. RandomGenerator::Seed s;
  68. if (lua_gettop(L) > 0)
  69. s = luax_checkrandomseed(L, 1);
  70. RandomGenerator *t = Math::instance.newRandomGenerator();
  71. if (lua_gettop(L) > 0)
  72. {
  73. bool should_error = false;
  74. try
  75. {
  76. t->setSeed(s);
  77. }
  78. catch (love::Exception &e)
  79. {
  80. t->release();
  81. should_error = true;
  82. lua_pushstring(L, e.what());
  83. }
  84. if (should_error)
  85. return luaL_error(L, "%s", lua_tostring(L, -1));
  86. }
  87. luax_pushtype(L, "RandomGenerator", MATH_RANDOM_GENERATOR_T, t);
  88. return 1;
  89. }
  90. int w_newBezierCurve(lua_State *L)
  91. {
  92. std::vector<Vector> points;
  93. if (lua_istable(L, 1))
  94. {
  95. size_t top = lua_objlen(L, 1);
  96. points.reserve(top / 2);
  97. for (size_t i = 1; i <= top; i += 2)
  98. {
  99. lua_rawgeti(L, 1, i);
  100. lua_rawgeti(L, 1, i+1);
  101. Vector v;
  102. v.x = (float) luaL_checknumber(L, -2);
  103. v.y = (float) luaL_checknumber(L, -1);
  104. points.push_back(v);
  105. lua_pop(L, 2);
  106. }
  107. }
  108. else
  109. {
  110. size_t top = lua_gettop(L);
  111. points.reserve(top / 2);
  112. for (size_t i = 1; i <= top; i += 2)
  113. {
  114. Vector v;
  115. v.x = (float) luaL_checknumber(L, i);
  116. v.y = (float) luaL_checknumber(L, i+1);
  117. points.push_back(v);
  118. }
  119. }
  120. BezierCurve *curve = Math::instance.newBezierCurve(points);
  121. luax_pushtype(L, "BezierCurve", MATH_BEZIER_CURVE_T, curve);
  122. return 1;
  123. }
  124. int w_triangulate(lua_State *L)
  125. {
  126. std::vector<Vertex> vertices;
  127. if (lua_istable(L, 1))
  128. {
  129. size_t top = lua_objlen(L, 1);
  130. vertices.reserve(top / 2);
  131. for (size_t i = 1; i <= top; i += 2)
  132. {
  133. lua_rawgeti(L, 1, i);
  134. lua_rawgeti(L, 1, i+1);
  135. Vertex v;
  136. v.x = (float) luaL_checknumber(L, -2);
  137. v.y = (float) luaL_checknumber(L, -1);
  138. vertices.push_back(v);
  139. lua_pop(L, 2);
  140. }
  141. }
  142. else
  143. {
  144. size_t top = lua_gettop(L);
  145. vertices.reserve(top / 2);
  146. for (size_t i = 1; i <= top; i += 2)
  147. {
  148. Vertex v;
  149. v.x = (float) luaL_checknumber(L, i);
  150. v.y = (float) luaL_checknumber(L, i+1);
  151. vertices.push_back(v);
  152. }
  153. }
  154. if (vertices.size() < 3)
  155. return luaL_error(L, "Need at least 3 vertices to triangulate");
  156. std::vector<Triangle> triangles;
  157. EXCEPT_GUARD(
  158. if (vertices.size() == 3)
  159. triangles.push_back(Triangle(vertices[0], vertices[1], vertices[2]));
  160. else
  161. triangles = Math::instance.triangulate(vertices);
  162. )
  163. lua_createtable(L, triangles.size(), 0);
  164. for (size_t i = 0; i < triangles.size(); ++i)
  165. {
  166. const Triangle &tri = triangles[i];
  167. lua_createtable(L, 6, 0);
  168. lua_pushnumber(L, tri.a.x);
  169. lua_rawseti(L, -2, 1);
  170. lua_pushnumber(L, tri.a.y);
  171. lua_rawseti(L, -2, 2);
  172. lua_pushnumber(L, tri.b.x);
  173. lua_rawseti(L, -2, 3);
  174. lua_pushnumber(L, tri.b.y);
  175. lua_rawseti(L, -2, 4);
  176. lua_pushnumber(L, tri.c.x);
  177. lua_rawseti(L, -2, 5);
  178. lua_pushnumber(L, tri.c.y);
  179. lua_rawseti(L, -2, 6);
  180. lua_rawseti(L, -2, i+1);
  181. }
  182. return 1;
  183. }
  184. int w_isConvex(lua_State *L)
  185. {
  186. std::vector<Vertex> vertices;
  187. if (lua_istable(L, 1))
  188. {
  189. size_t top = lua_objlen(L, 1);
  190. vertices.reserve(top / 2);
  191. for (size_t i = 1; i <= top; i += 2)
  192. {
  193. lua_rawgeti(L, 1, i);
  194. lua_rawgeti(L, 1, i+1);
  195. Vertex v;
  196. v.x = (float) luaL_checknumber(L, -2);
  197. v.y = (float) luaL_checknumber(L, -1);
  198. vertices.push_back(v);
  199. lua_pop(L, 2);
  200. }
  201. }
  202. else
  203. {
  204. int top = lua_gettop(L);
  205. vertices.reserve(top / 2);
  206. for (int i = 1; i <= top; i += 2)
  207. {
  208. Vertex v;
  209. v.x = (float) luaL_checknumber(L, i);
  210. v.y = (float) luaL_checknumber(L, i+1);
  211. vertices.push_back(v);
  212. }
  213. }
  214. lua_pushboolean(L, Math::instance.isConvex(vertices));
  215. return 1;
  216. }
  217. static int getGammaArgs(lua_State *L, float color[4])
  218. {
  219. int numcomponents = 0;
  220. if (lua_istable(L, 1))
  221. {
  222. int n = lua_objlen(L, 1);
  223. for (int i = 1; i <= n && i <= 4; i++)
  224. {
  225. lua_rawgeti(L, 1, i);
  226. color[i - 1] = (float) luaL_checknumber(L, -1) / 255.0;
  227. numcomponents++;
  228. }
  229. lua_pop(L, numcomponents);
  230. }
  231. else
  232. {
  233. int n = lua_gettop(L);
  234. for (int i = 1; i <= n && i <= 4; i++)
  235. {
  236. color[i - 1] = (float) luaL_checknumber(L, i) / 255.0;
  237. numcomponents++;
  238. }
  239. }
  240. if (numcomponents == 0)
  241. luaL_checknumber(L, 1);
  242. return numcomponents;
  243. }
  244. int w_gammaToLinear(lua_State *L)
  245. {
  246. float color[4];
  247. int numcomponents = getGammaArgs(L, color);
  248. for (int i = 0; i < numcomponents; i++)
  249. {
  250. // Alpha should always be linear.
  251. if (i < 3)
  252. color[i] = Math::instance.gammaToLinear(color[i]);
  253. lua_pushnumber(L, color[i] * 255);
  254. }
  255. return numcomponents;
  256. }
  257. int w_linearToGamma(lua_State *L)
  258. {
  259. float color[4];
  260. int numcomponents = getGammaArgs(L, color);
  261. for (int i = 0; i < numcomponents; i++)
  262. {
  263. // Alpha should always be linear.
  264. if (i < 3)
  265. color[i] = Math::instance.linearToGamma(color[i]);
  266. lua_pushnumber(L, color[i] * 255);
  267. }
  268. return numcomponents;
  269. }
  270. int w_noise(lua_State *L)
  271. {
  272. float w, x, y, z;
  273. float val;
  274. switch (lua_gettop(L))
  275. {
  276. case 1:
  277. x = (float) luaL_checknumber(L, 1);
  278. val = Math::instance.noise(x);
  279. break;
  280. case 2:
  281. x = (float) luaL_checknumber(L, 1);
  282. y = (float) luaL_checknumber(L, 2);
  283. val = Math::instance.noise(x, y);
  284. break;
  285. case 3:
  286. x = (float) luaL_checknumber(L, 1);
  287. y = (float) luaL_checknumber(L, 2);
  288. z = (float) luaL_checknumber(L, 3);
  289. val = Math::instance.noise(x, y, z);
  290. break;
  291. case 4:
  292. default:
  293. x = (float) luaL_checknumber(L, 1);
  294. y = (float) luaL_checknumber(L, 2);
  295. z = (float) luaL_checknumber(L, 3);
  296. w = (float) luaL_checknumber(L, 4);
  297. val = Math::instance.noise(x, y, z, w);
  298. break;
  299. }
  300. lua_pushnumber(L, (lua_Number) val);
  301. return 1;
  302. }
  303. // List of functions to wrap.
  304. static const luaL_Reg functions[] =
  305. {
  306. { "random", w_random },
  307. { "randomNormal", w_randomNormal },
  308. { "setRandomSeed", w_setRandomSeed },
  309. { "getRandomSeed", w_getRandomSeed },
  310. { "setRandomState", w_setRandomState },
  311. { "getRandomState", w_getRandomState },
  312. { "newRandomGenerator", w_newRandomGenerator },
  313. { "newBezierCurve", w_newBezierCurve },
  314. { "triangulate", w_triangulate },
  315. { "isConvex", w_isConvex },
  316. { "gammaToLinear", w_gammaToLinear },
  317. { "linearToGamma", w_linearToGamma },
  318. { "noise", w_noise },
  319. { 0, 0 }
  320. };
  321. static const lua_CFunction types[] =
  322. {
  323. luaopen_randomgenerator,
  324. luaopen_beziercurve,
  325. 0
  326. };
  327. extern "C" int luaopen_love_math(lua_State *L)
  328. {
  329. Math::instance.retain();
  330. WrappedModule w;
  331. w.module = &Math::instance;
  332. w.name = "math";
  333. w.flags = MODULE_T;
  334. w.functions = functions;
  335. w.types = types;
  336. int n = luax_register_module(L, w);
  337. return n;
  338. }
  339. } // math
  340. } // love