123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /**
- * Copyright (c) 2006-2013 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software. If you use this software
- * in a product, an acknowledgment in the product documentation would be
- * appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
- #include "common/Exception.h"
- #include "wrap_BezierCurve.h"
- #include <cmath>
- namespace love
- {
- namespace math
- {
- BezierCurve *luax_checkbeziercurve(lua_State *L, int idx)
- {
- return luax_checktype<BezierCurve>(L, idx, "BezierCurve", MATH_BEZIER_CURVE_T);
- }
- int w_BezierCurve_getDegree(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- lua_pushnumber(L, curve->getDegree());
- return 1;
- }
- int w_BezierCurve_getDerivative(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- BezierCurve *deriv = new BezierCurve(curve->getDerivative());
- luax_pushtype(L, "BezierCurve", MATH_BEZIER_CURVE_T, deriv);
- return 1;
- }
- int w_BezierCurve_getControlPoint(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- int idx = luaL_checkinteger(L, 2);
- if (idx > 0) // 1-indexing
- idx--;
- try
- {
- Vector v = curve->getControlPoint(idx);
- lua_pushnumber(L, v.x);
- lua_pushnumber(L, v.y);
- }
- catch (Exception &e)
- {
- return luaL_error(L, e.what());
- }
- return 2;
- }
- int w_BezierCurve_setControlPoint(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- int idx = luaL_checkinteger(L, 2);
- double vx = luaL_checknumber(L, 3);
- double vy = luaL_checknumber(L, 4);
- if (idx > 0) // 1-indexing
- idx--;
- try
- {
- curve->setControlPoint(idx, Vector(vx,vy));
- }
- catch (Exception &e)
- {
- return luaL_error(L, e.what());
- }
- return 0;
- }
- int w_BezierCurve_insertControlPoint(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- double vx = luaL_checknumber(L, 2);
- double vy = luaL_checknumber(L, 3);
- int idx = luaL_optinteger(L, 4, -1);
- if (idx > 0) // 1-indexing
- idx--;
- try
- {
- curve->insertControlPoint(Vector(vx,vy), idx);
- }
- catch (Exception &e)
- {
- return luaL_error(L, e.what());
- }
- return 0;
- }
- int w_BezierCurve_translate(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- double dx = luaL_checknumber(L, 2);
- double dy = luaL_checknumber(L, 3);
- curve->translate(Vector(dx,dy));
- return 0;
- }
- int w_BezierCurve_rotate(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- double phi = luaL_checknumber(L, 2);
- double ox = luaL_optnumber(L, 3, 0);
- double oy = luaL_optnumber(L, 4, 0);
- curve->rotate(phi, Vector(ox,oy));
- return 0;
- }
- int w_BezierCurve_scale(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- double s = luaL_checknumber(L, 2);
- double ox = luaL_optnumber(L, 3, 0);
- double oy = luaL_optnumber(L, 4, 0);
- curve->scale(s, Vector(ox,oy));
- return 0;
- }
- int w_BezierCurve_eval(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- double t = luaL_checknumber(L, 2);
- try
- {
- Vector v = curve->eval(t);
- lua_pushnumber(L, v.x);
- lua_pushnumber(L, v.y);
- }
- catch (Exception &e)
- {
- return luaL_error(L, e.what());
- }
- return 2;
- }
- int w_BezierCurve_render(lua_State *L)
- {
- BezierCurve *curve = luax_checkbeziercurve(L, 1);
- int accuracy = luaL_optinteger(L, 2, 5);
- std::vector<Vector> points;
- try
- {
- points = curve->render(accuracy);
- }
- catch (Exception &e)
- {
- return luaL_error(L, e.what());
- }
- lua_createtable(L, points.size()*2, 0);
- for (size_t i = 0; i < points.size(); ++i)
- {
- lua_pushnumber(L, points[i].x);
- lua_rawseti(L, -2, 2*i+1);
- lua_pushnumber(L, points[i].y);
- lua_rawseti(L, -2, 2*i+2);
- }
- return 1;
- }
- static const luaL_Reg functions[] =
- {
- {"getDegree", w_BezierCurve_getDegree},
- {"getDerivative", w_BezierCurve_getDerivative},
- {"getControlPoint", w_BezierCurve_getControlPoint},
- {"setControlPoint", w_BezierCurve_setControlPoint},
- {"insertControlPoint", w_BezierCurve_insertControlPoint},
- {"translate", w_BezierCurve_translate},
- {"rotate", w_BezierCurve_rotate},
- {"scale", w_BezierCurve_scale},
- {"eval", w_BezierCurve_eval},
- {"render", w_BezierCurve_render},
- { 0, 0 }
- };
- extern "C" int luaopen_beziercurve(lua_State *L)
- {
- return luax_register_type(L, "BezierCurve", functions);
- }
- } // math
- } // love
|