wrap_Math.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * Copyright (c) 2006-2013 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_randomseed(lua_State *L)
  32. {
  33. uint64 seed = luax_checkrandomseed(L, 1);
  34. Math::instance.randomseed(seed);
  35. return 0;
  36. }
  37. int w_random(lua_State *L)
  38. {
  39. return luax_getrandom(L, 1, Math::instance.random());
  40. }
  41. int w_randomnormal(lua_State *L)
  42. {
  43. double mean = 0.0, stddev = 1.0;
  44. if (lua_gettop(L) > 1)
  45. {
  46. mean = luaL_checknumber(L, 1);
  47. stddev = luaL_checknumber(L, 2);
  48. }
  49. else
  50. {
  51. stddev = luaL_optnumber(L, 1, 1.);
  52. }
  53. double r = Math::instance.randomnormal(stddev);
  54. lua_pushnumber(L, r + mean);
  55. return 1;
  56. }
  57. int w_newRandomGenerator(lua_State *L)
  58. {
  59. RandomGenerator *t = Math::instance.newRandomGenerator();
  60. if (lua_gettop(L) > 0)
  61. {
  62. uint64 seed = luax_checkrandomseed(L, 1);
  63. t->randomseed(seed);
  64. }
  65. luax_newtype(L, "RandomGenerator", MATH_RANDOM_GENERATOR_T, (void *) t);
  66. return 1;
  67. }
  68. int w_newBezierCurve(lua_State *L)
  69. {
  70. std::vector<Vector> points;
  71. if (lua_istable(L, 1))
  72. {
  73. size_t top = lua_objlen(L, 1);
  74. points.reserve(top / 2);
  75. for (size_t i = 1; i <= top; i += 2)
  76. {
  77. lua_rawgeti(L, 1, i);
  78. lua_rawgeti(L, 1, i+1);
  79. Vector v;
  80. v.x = luaL_checknumber(L, -2);
  81. v.y = luaL_checknumber(L, -1);
  82. points.push_back(v);
  83. lua_pop(L, 2);
  84. }
  85. }
  86. else
  87. {
  88. size_t top = lua_gettop(L);
  89. points.reserve(top / 2);
  90. for (size_t i = 1; i <= top; i += 2)
  91. {
  92. Vector v;
  93. v.x = luaL_checknumber(L, i);
  94. v.y = luaL_checknumber(L, i+1);
  95. points.push_back(v);
  96. }
  97. }
  98. BezierCurve *curve = Math::instance.newBezierCurve(points);
  99. luax_newtype(L, "BezierCurve", MATH_BEZIER_CURVE_T, (void *)curve);
  100. return 1;
  101. }
  102. int w_triangulate(lua_State *L)
  103. {
  104. std::vector<vertex> vertices;
  105. if (lua_istable(L, 1))
  106. {
  107. size_t top = lua_objlen(L, 1);
  108. vertices.reserve(top / 2);
  109. for (size_t i = 1; i <= top; i += 2)
  110. {
  111. lua_rawgeti(L, 1, i);
  112. lua_rawgeti(L, 1, i+1);
  113. vertex v;
  114. v.x = luaL_checknumber(L, -2);
  115. v.y = luaL_checknumber(L, -1);
  116. vertices.push_back(v);
  117. lua_pop(L, 2);
  118. }
  119. }
  120. else
  121. {
  122. size_t top = lua_gettop(L);
  123. vertices.reserve(top / 2);
  124. for (size_t i = 1; i <= top; i += 2)
  125. {
  126. vertex v;
  127. v.x = luaL_checknumber(L, i);
  128. v.y = luaL_checknumber(L, i+1);
  129. vertices.push_back(v);
  130. }
  131. }
  132. if (vertices.size() < 3)
  133. return luaL_error(L, "Need at least 3 vertices to triangulate");
  134. std::vector<Triangle> triangles;
  135. try
  136. {
  137. if (vertices.size() == 3)
  138. triangles.push_back(Triangle(vertices[0], vertices[1], vertices[2]));
  139. else
  140. triangles = Math::instance.triangulate(vertices);
  141. }
  142. catch (love::Exception &e)
  143. {
  144. return luaL_error(L, e.what());
  145. }
  146. lua_createtable(L, triangles.size(), 0);
  147. for (size_t i = 0; i < triangles.size(); ++i)
  148. {
  149. Triangle &tri = triangles[i];
  150. lua_createtable(L, 6, 0);
  151. lua_pushnumber(L, tri.a.x);
  152. lua_rawseti(L, -2, 1);
  153. lua_pushnumber(L, tri.a.y);
  154. lua_rawseti(L, -2, 2);
  155. lua_pushnumber(L, tri.b.x);
  156. lua_rawseti(L, -2, 3);
  157. lua_pushnumber(L, tri.b.y);
  158. lua_rawseti(L, -2, 4);
  159. lua_pushnumber(L, tri.c.x);
  160. lua_rawseti(L, -2, 5);
  161. lua_pushnumber(L, tri.c.y);
  162. lua_rawseti(L, -2, 6);
  163. lua_rawseti(L, -2, i+1);
  164. }
  165. return 1;
  166. }
  167. int w_isConvex(lua_State *L)
  168. {
  169. std::vector<vertex> vertices;
  170. if (lua_istable(L, 1))
  171. {
  172. size_t top = lua_objlen(L, 1);
  173. vertices.reserve(top / 2);
  174. for (size_t i = 1; i <= top; i += 2)
  175. {
  176. lua_rawgeti(L, 1, i);
  177. lua_rawgeti(L, 1, i+1);
  178. vertex v;
  179. v.x = luaL_checknumber(L, -2);
  180. v.y = luaL_checknumber(L, -1);
  181. vertices.push_back(v);
  182. lua_pop(L, 2);
  183. }
  184. }
  185. else
  186. {
  187. size_t top = lua_gettop(L);
  188. vertices.reserve(top / 2);
  189. for (size_t i = 1; i <= top; i += 2)
  190. {
  191. vertex v;
  192. v.x = luaL_checknumber(L, i);
  193. v.y = luaL_checknumber(L, i+1);
  194. vertices.push_back(v);
  195. }
  196. }
  197. lua_pushboolean(L, Math::instance.isConvex(vertices));
  198. return 1;
  199. }
  200. int w_noise(lua_State *L)
  201. {
  202. float w, x, y, z;
  203. float val;
  204. switch (lua_gettop(L))
  205. {
  206. case 1:
  207. x = luaL_checknumber(L, 1);
  208. val = Math::instance.noise(x);
  209. break;
  210. case 2:
  211. x = luaL_checknumber(L, 1);
  212. y = luaL_checknumber(L, 2);
  213. val = Math::instance.noise(x, y);
  214. break;
  215. case 3:
  216. x = luaL_checknumber(L, 1);
  217. y = luaL_checknumber(L, 2);
  218. z = luaL_checknumber(L, 3);
  219. val = Math::instance.noise(x, y, z);
  220. break;
  221. case 4:
  222. default:
  223. x = luaL_checknumber(L, 1);
  224. y = luaL_checknumber(L, 2);
  225. z = luaL_checknumber(L, 3);
  226. w = luaL_checknumber(L, 4);
  227. val = Math::instance.noise(x, y, z, w);
  228. break;
  229. }
  230. lua_pushnumber(L, (lua_Number) val);
  231. return 1;
  232. }
  233. // List of functions to wrap.
  234. static const luaL_Reg functions[] =
  235. {
  236. { "randomseed", w_randomseed },
  237. { "random", w_random },
  238. { "randomnormal", w_randomnormal },
  239. { "newRandomGenerator", w_newRandomGenerator },
  240. { "newBezierCurve", w_newBezierCurve },
  241. { "triangulate", w_triangulate },
  242. { "isConvex", w_isConvex },
  243. { "noise", w_noise },
  244. { 0, 0 }
  245. };
  246. static const lua_CFunction types[] =
  247. {
  248. luaopen_randomgenerator,
  249. luaopen_beziercurve,
  250. 0
  251. };
  252. extern "C" int luaopen_love_math(lua_State *L)
  253. {
  254. Math::instance.retain();
  255. WrappedModule w;
  256. w.module = &Math::instance;
  257. w.name = "math";
  258. w.flags = MODULE_T;
  259. w.functions = functions;
  260. w.types = types;
  261. int n = luax_register_module(L, w);
  262. return n;
  263. }
  264. } // math
  265. } // love