2
0

wrap_BezierCurve.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "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. deriv->release();
  43. return 1;
  44. }
  45. int w_BezierCurve_getControlPoint(lua_State *L)
  46. {
  47. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  48. int idx = luaL_checkint(L, 2);
  49. if (idx > 0) // 1-indexing
  50. idx--;
  51. luax_catchexcept(L, [&]() {
  52. Vector v = curve->getControlPoint(idx);
  53. lua_pushnumber(L, v.x);
  54. lua_pushnumber(L, v.y);
  55. });
  56. return 2;
  57. }
  58. int w_BezierCurve_setControlPoint(lua_State *L)
  59. {
  60. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  61. int idx = luaL_checkint(L, 2);
  62. float vx = (float) luaL_checknumber(L, 3);
  63. float vy = (float) luaL_checknumber(L, 4);
  64. if (idx > 0) // 1-indexing
  65. idx--;
  66. luax_catchexcept(L, [&](){ curve->setControlPoint(idx, Vector(vx,vy)); });
  67. return 0;
  68. }
  69. int w_BezierCurve_insertControlPoint(lua_State *L)
  70. {
  71. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  72. float vx = (float) luaL_checknumber(L, 2);
  73. float vy = (float) luaL_checknumber(L, 3);
  74. int idx = luaL_optint(L, 4, -1);
  75. if (idx > 0) // 1-indexing
  76. idx--;
  77. luax_catchexcept(L, [&](){ curve->insertControlPoint(Vector(vx,vy), idx); });
  78. return 0;
  79. }
  80. int w_BezierCurve_getControlPointCount(lua_State *L)
  81. {
  82. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  83. lua_pushinteger(L, curve->getControlPointCount());
  84. return 1;
  85. }
  86. int w_BezierCurve_translate(lua_State *L)
  87. {
  88. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  89. float dx = (float) luaL_checknumber(L, 2);
  90. float dy = (float) luaL_checknumber(L, 3);
  91. curve->translate(Vector(dx,dy));
  92. return 0;
  93. }
  94. int w_BezierCurve_rotate(lua_State *L)
  95. {
  96. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  97. double phi = luaL_checknumber(L, 2);
  98. float ox = (float) luaL_optnumber(L, 3, 0);
  99. float oy = (float) luaL_optnumber(L, 4, 0);
  100. curve->rotate(phi, Vector(ox,oy));
  101. return 0;
  102. }
  103. int w_BezierCurve_scale(lua_State *L)
  104. {
  105. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  106. double s = luaL_checknumber(L, 2);
  107. float ox = (float) luaL_optnumber(L, 3, 0);
  108. float oy = (float) luaL_optnumber(L, 4, 0);
  109. curve->scale(s, Vector(ox,oy));
  110. return 0;
  111. }
  112. int w_BezierCurve_evaluate(lua_State *L)
  113. {
  114. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  115. double t = luaL_checknumber(L, 2);
  116. luax_catchexcept(L, [&]() {
  117. Vector v = curve->evaluate(t);
  118. lua_pushnumber(L, v.x);
  119. lua_pushnumber(L, v.y);
  120. });
  121. return 2;
  122. }
  123. int w_BezierCurve_render(lua_State *L)
  124. {
  125. BezierCurve *curve = luax_checkbeziercurve(L, 1);
  126. int accuracy = luaL_optint(L, 2, 5);
  127. std::vector<Vector> points;
  128. luax_catchexcept(L, [&](){ points = curve->render(accuracy); });
  129. lua_createtable(L, (int) points.size() * 2, 0);
  130. for (int i = 0; i < (int) points.size(); ++i)
  131. {
  132. lua_pushnumber(L, points[i].x);
  133. lua_rawseti(L, -2, 2*i+1);
  134. lua_pushnumber(L, points[i].y);
  135. lua_rawseti(L, -2, 2*i+2);
  136. }
  137. return 1;
  138. }
  139. static const luaL_Reg functions[] =
  140. {
  141. {"getDegree", w_BezierCurve_getDegree},
  142. {"getDerivative", w_BezierCurve_getDerivative},
  143. {"getControlPoint", w_BezierCurve_getControlPoint},
  144. {"setControlPoint", w_BezierCurve_setControlPoint},
  145. {"insertControlPoint", w_BezierCurve_insertControlPoint},
  146. {"getControlPointCount", w_BezierCurve_getControlPointCount},
  147. {"translate", w_BezierCurve_translate},
  148. {"rotate", w_BezierCurve_rotate},
  149. {"scale", w_BezierCurve_scale},
  150. {"evaluate", w_BezierCurve_evaluate},
  151. {"render", w_BezierCurve_render},
  152. { 0, 0 }
  153. };
  154. extern "C" int luaopen_beziercurve(lua_State *L)
  155. {
  156. return luax_register_type(L, "BezierCurve", functions);
  157. }
  158. } // math
  159. } // love