wrap_Math.cpp 5.3 KB

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