release 0.1.13
[liba.git] / lua / src / trajpoly3.c
blob101681d21fd9584cca4ce923cf38670b3ec09314
1 /***
2 cubic polynomial trajectory
3 @module liba.trajpoly3
4 */
6 #include "trajpoly3.h"
7 #include "a/trajpoly3.h"
9 static int liba_trajpoly3_gen_(lua_State *L, a_trajpoly3 *ctx, int arg, int top)
11 a_float p0 = 0, v0 = 0;
12 a_float p1 = 0, v1 = 0;
13 a_float ts = 0;
14 switch (top)
16 default:
17 case 5:
18 v1 = (a_float)luaL_checknumber(L, arg + 5);
19 A_FALLTHROUGH;
20 case 4:
21 v0 = (a_float)luaL_checknumber(L, arg + 4);
22 A_FALLTHROUGH;
23 case 3:
24 p1 = (a_float)luaL_checknumber(L, arg + 3);
25 A_FALLTHROUGH;
26 case 2:
27 p0 = (a_float)luaL_checknumber(L, arg + 2);
28 A_FALLTHROUGH;
29 case 1:
30 ts = (a_float)luaL_checknumber(L, arg + 1);
31 A_FALLTHROUGH;
32 case 0:
33 a_trajpoly3_gen(ctx, ts, p0, p1, v0, v1);
35 return 1;
38 /***
39 constructor for cubic polynomial trajectory
40 @tparam number ts difference between final time and initial time
41 @tparam number p0 initial position
42 @tparam number p1 final position
43 @tparam[opt] number v0 initial velocity
44 @tparam[opt] number v1 final velocity
45 @treturn a.trajpoly3 cubic polynomial trajectory userdata
46 @function new
48 int liba_trajpoly3_new(lua_State *L)
50 int const top = lua_gettop(L);
51 if (top > 2)
53 a_trajpoly3 *const ctx = lua_newclass(L, a_trajpoly3);
54 lua_registry_get(L, liba_trajpoly3_new);
55 lua_setmetatable(L, -2);
56 return liba_trajpoly3_gen_(L, ctx, 0, top);
58 return 0;
61 /***
62 generate for cubic polynomial trajectory
63 @tparam a.trajpoly3 ctx cubic polynomial trajectory userdata
64 @tparam number ts difference between final time and initial time
65 @tparam number p0 initial position
66 @tparam number p1 final position
67 @tparam[opt] number v0 initial velocity
68 @tparam[opt] number v1 final velocity
69 @treturn a.trajpoly3 cubic polynomial trajectory userdata
70 @function gen
72 int liba_trajpoly3_gen(lua_State *L)
74 int const top = lua_gettop(L);
75 if (top > 3)
77 luaL_checktype(L, 1, LUA_TUSERDATA);
78 a_trajpoly3 *const ctx = (a_trajpoly3 *)lua_touserdata(L, 1);
79 lua_pushvalue(L, 1);
80 return liba_trajpoly3_gen_(L, ctx, 1, top - 1);
82 return 0;
85 /***
86 calculate position for cubic polynomial trajectory
87 @tparam a.trajpoly3 ctx cubic polynomial trajectory userdata
88 @tparam number x difference between current time and initial time
89 @treturn number position output
90 @function pos
92 int liba_trajpoly3_pos(lua_State *L)
94 a_trajpoly3 const *const ctx = (a_trajpoly3 const *)lua_touserdata(L, 1);
95 if (ctx)
97 a_float const x = (a_float)luaL_checknumber(L, 2);
98 lua_pushnumber(L, (lua_Number)a_trajpoly3_pos(ctx, x));
99 return 1;
101 return 0;
104 /***
105 calculate velocity for cubic polynomial trajectory
106 @tparam a.trajpoly3 ctx cubic polynomial trajectory userdata
107 @tparam number x difference between current time and initial time
108 @treturn number velocity output
109 @function vel
111 int liba_trajpoly3_vel(lua_State *L)
113 a_trajpoly3 const *const ctx = (a_trajpoly3 const *)lua_touserdata(L, 1);
114 if (ctx)
116 a_float const x = (a_float)luaL_checknumber(L, 2);
117 lua_pushnumber(L, (lua_Number)a_trajpoly3_vel(ctx, x));
118 return 1;
120 return 0;
123 /***
124 calculate acceleration for cubic polynomial trajectory
125 @tparam a.trajpoly3 ctx cubic polynomial trajectory userdata
126 @tparam number x difference between current time and initial time
127 @treturn number acceleration output
128 @function acc
130 int liba_trajpoly3_acc(lua_State *L)
132 a_trajpoly3 const *const ctx = (a_trajpoly3 const *)lua_touserdata(L, 1);
133 if (ctx)
135 a_float const x = (a_float)luaL_checknumber(L, 2);
136 lua_pushnumber(L, (lua_Number)a_trajpoly3_acc(ctx, x));
137 return 1;
139 return 0;
142 static int liba_trajpoly3_set(lua_State *L)
144 switch (a_hash_bkdr(lua_tostring(L, 2), 0))
146 case 0xE8859EEB: // __name
147 case 0xA65758B2: // __index
148 case 0xAEB551C6: // __newindex
149 break;
150 default:
151 lua_getmetatable(L, 1);
152 lua_replace(L, 1);
153 lua_rawset(L, 1);
155 return 0;
158 static int liba_trajpoly3_get(lua_State *L)
160 a_trajpoly3 const *const ctx = (a_trajpoly3 const *)lua_touserdata(L, 1);
161 switch (a_hash_bkdr(lua_tostring(L, 2), 0))
163 case 0x00000070: // p
164 lua_array_num_new(L, ctx->p, A_LEN(ctx->p));
165 break;
166 case 0x00000076: // v
167 lua_array_num_new(L, ctx->v, A_LEN(ctx->v));
168 break;
169 case 0x00000061: // a
170 lua_array_num_new(L, ctx->a, A_LEN(ctx->a));
171 break;
172 case 0xA65758B2: // __index
173 lua_registry_get(L, liba_trajpoly3_new);
174 lua_pushstring(L, "p");
175 lua_array_num_new(L, ctx->p, A_LEN(ctx->p));
176 lua_rawset(L, -3);
177 lua_pushstring(L, "v");
178 lua_array_num_new(L, ctx->v, A_LEN(ctx->v));
179 lua_rawset(L, -3);
180 lua_pushstring(L, "a");
181 lua_array_num_new(L, ctx->a, A_LEN(ctx->a));
182 lua_rawset(L, -3);
183 break;
184 default:
185 lua_getmetatable(L, 1);
186 lua_replace(L, 1);
187 lua_rawget(L, 1);
189 return 1;
192 static int liba_trajpoly3_(lua_State *L)
194 lua_pushcfunction(L, liba_trajpoly3_new);
195 lua_replace(L, 1);
196 lua_call(L, lua_gettop(L) - 1, 1);
197 return 1;
200 int luaopen_liba_trajpoly3(lua_State *L)
202 static lua_fun const funcs[] = {
203 {"new", liba_trajpoly3_new},
204 {"gen", liba_trajpoly3_gen},
205 {"pos", liba_trajpoly3_pos},
206 {"vel", liba_trajpoly3_vel},
207 {"acc", liba_trajpoly3_acc},
209 lua_createtable(L, 0, A_LEN(funcs));
210 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
211 lua_createtable(L, 0, 1);
212 lua_fun_set(L, -1, "__call", liba_trajpoly3_);
213 lua_setmetatable(L, -2);
215 static lua_fun const metas[] = {
216 {"__newindex", liba_trajpoly3_set},
217 {"__index", liba_trajpoly3_get},
219 lua_createtable(L, 0, A_LEN(metas) + A_LEN(funcs) + 1);
220 lua_fun_reg(L, -1, metas, A_LEN(metas));
221 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
222 lua_str_set(L, -1, "__name", "a.trajpoly3");
223 lua_registry_set(L, liba_trajpoly3_new);
225 return 1;