wrap_BezierCurve.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "common/Exception.h"
  21. #include "wrap_BezierCurve.h"
  22. #include <cmath>
  23. namespace love
  24. {
  25. namespace math
  26. {
  27. BezierCurve *luax_checkbeziercurve(lua_State *L, int idx)
  28. {
  29. return luax_checktype<BezierCurve>(L, idx, "BezierCurve", MATH_BEZIER_CURVE_T);
  30. }
  31. int w_BezierCurve_getDegree(lua_State *L)
  32. {
  33. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  34. lua_pushnumber(L, curve->getDegree());
  35. return 1;
  36. }
  37. int w_BezierCurve_getDerivative(lua_State *L)
  38. {
  39. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  40. BezierCurve *deriv = new BezierCurve(curve->getDerivative());
  41. luax_pushtype(L, "BezierCurve", MATH_BEZIER_CURVE_T, deriv);
  42. return 1;
  43. }
  44. int w_BezierCurve_getControlPoint(lua_State *L)
  45. {
  46. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  47. int idx = luaL_checkinteger(L, 2);
  48. if (idx > 0) // 1-indexing
  49. idx--;
  50. try
  51. {
  52. Vector v = curve->getControlPoint(idx);
  53. lua_pushnumber(L, v.x);
  54. lua_pushnumber(L, v.y);
  55. }
  56. catch (Exception &e)
  57. {
  58. return luaL_error(L, e.what());
  59. }
  60. return 2;
  61. }
  62. int w_BezierCurve_setControlPoint(lua_State *L)
  63. {
  64. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  65. int idx = luaL_checkinteger(L, 2);
  66. double vx = luaL_checknumber(L, 3);
  67. double vy = luaL_checknumber(L, 4);
  68. if (idx > 0) // 1-indexing
  69. idx--;
  70. try
  71. {
  72. curve->setControlPoint(idx, Vector(vx,vy));
  73. }
  74. catch (Exception &e)
  75. {
  76. return luaL_error(L, e.what());
  77. }
  78. return 0;
  79. }
  80. int w_BezierCurve_insertControlPoint(lua_State *L)
  81. {
  82. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  83. double vx = luaL_checknumber(L, 2);
  84. double vy = luaL_checknumber(L, 3);
  85. int idx = luaL_optinteger(L, 4, -1);
  86. if (idx > 0) // 1-indexing
  87. idx--;
  88. try
  89. {
  90. curve->insertControlPoint(Vector(vx,vy), idx);
  91. }
  92. catch (Exception &e)
  93. {
  94. return luaL_error(L, e.what());
  95. }
  96. return 0;
  97. }
  98. int w_BezierCurve_translate(lua_State *L)
  99. {
  100. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  101. double dx = luaL_checknumber(L, 2);
  102. double dy = luaL_checknumber(L, 3);
  103. curve->translate(Vector(dx,dy));
  104. return 0;
  105. }
  106. int w_BezierCurve_rotate(lua_State *L)
  107. {
  108. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  109. double phi = luaL_checknumber(L, 2);
  110. double ox = luaL_optnumber(L, 3, 0);
  111. double oy = luaL_optnumber(L, 4, 0);
  112. curve->rotate(phi, Vector(ox,oy));
  113. return 0;
  114. }
  115. int w_BezierCurve_scale(lua_State *L)
  116. {
  117. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  118. double s = luaL_checknumber(L, 2);
  119. double ox = luaL_optnumber(L, 3, 0);
  120. double oy = luaL_optnumber(L, 4, 0);
  121. curve->scale(s, Vector(ox,oy));
  122. return 0;
  123. }
  124. int w_BezierCurve_eval(lua_State *L)
  125. {
  126. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  127. double t = luaL_checknumber(L, 2);
  128. try
  129. {
  130. Vector v = curve->eval(t);
  131. lua_pushnumber(L, v.x);
  132. lua_pushnumber(L, v.y);
  133. }
  134. catch (Exception &e)
  135. {
  136. return luaL_error(L, e.what());
  137. }
  138. return 2;
  139. }
  140. int w_BezierCurve_render(lua_State *L)
  141. {
  142. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  143. int accuracy = luaL_optinteger(L, 2, 5);
  144. std::vector<Vector> points;
  145. try
  146. {
  147. points = curve->render(accuracy);
  148. }
  149. catch (Exception &e)
  150. {
  151. return luaL_error(L, e.what());
  152. }
  153. lua_createtable(L, points.size()*2, 0);
  154. for (size_t i = 0; i < points.size(); ++i)
  155. {
  156. lua_pushnumber(L, points[i].x);
  157. lua_rawseti(L, -2, 2*i+1);
  158. lua_pushnumber(L, points[i].y);
  159. lua_rawseti(L, -2, 2*i+2);
  160. }
  161. return 1;
  162. }
  163. static const luaL_Reg functions[] =
  164. {
  165. {"getDegree", w_BezierCurve_getDegree},
  166. {"getDerivative", w_BezierCurve_getDerivative},
  167. {"getControlPoint", w_BezierCurve_getControlPoint},
  168. {"setControlPoint", w_BezierCurve_setControlPoint},
  169. {"insertControlPoint", w_BezierCurve_insertControlPoint},
  170. {"translate", w_BezierCurve_translate},
  171. {"rotate", w_BezierCurve_rotate},
  172. {"scale", w_BezierCurve_scale},
  173. {"eval", w_BezierCurve_eval},
  174. {"render", w_BezierCurve_render},
  175. { 0, 0 }
  176. };
  177. extern "C" int luaopen_beziercurve(lua_State *L)
  178. {
  179. return luax_register_type(L, "BezierCurve", functions);
  180. }
  181. } // math
  182. } // love