rename dt to x
[liba.git] / lua / src / trajpoly5.c
blob0c17c43bb6937e20a919efe5c23fbbd2d370c355
1 /***
2 quintic polynomial trajectory
3 @module liba.trajpoly5
4 */
6 #include "trajpoly5.h"
7 #include "a/trajpoly5.h"
9 static int liba_trajpoly5_gen_(lua_State *L, a_trajpoly5 *ctx, int arg, int top)
11 a_float p0 = 0, v0 = 0, a0 = 0;
12 a_float p1 = 0, v1 = 0, a1 = 0;
13 a_float ts = 0;
14 switch (top)
16 default:
17 case 7:
18 a1 = (a_float)luaL_checknumber(L, arg + 7);
19 A_FALLTHROUGH;
20 case 6:
21 a0 = (a_float)luaL_checknumber(L, arg + 6);
22 A_FALLTHROUGH;
23 case 5:
24 v1 = (a_float)luaL_checknumber(L, arg + 5);
25 A_FALLTHROUGH;
26 case 4:
27 v0 = (a_float)luaL_checknumber(L, arg + 4);
28 A_FALLTHROUGH;
29 case 3:
30 p1 = (a_float)luaL_checknumber(L, arg + 3);
31 A_FALLTHROUGH;
32 case 2:
33 p0 = (a_float)luaL_checknumber(L, arg + 2);
34 A_FALLTHROUGH;
35 case 1:
36 ts = (a_float)luaL_checknumber(L, arg + 1);
37 A_FALLTHROUGH;
38 case 0:
39 a_trajpoly5_gen(ctx, ts, p0, p1, v0, v1, a0, a1);
41 return 1;
44 /***
45 constructor for quintic polynomial trajectory
46 @tparam number ts difference between final time and initial time
47 @tparam number p0 initial position
48 @tparam number p1 final position
49 @tparam[opt] number v0 initial velocity
50 @tparam[opt] number v1 final velocity
51 @tparam[opt] number a0 initial acceleration
52 @tparam[opt] number a1 final acceleration
53 @treturn a.trajpoly5 quintic polynomial trajectory userdata
54 @function new
56 int liba_trajpoly5_new(lua_State *L)
58 int const top = lua_gettop(L);
59 if (top > 2)
61 a_trajpoly5 *const ctx = lua_newclass(L, a_trajpoly5);
62 lua_registry_get(L, liba_trajpoly5_new);
63 lua_setmetatable(L, -2);
64 return liba_trajpoly5_gen_(L, ctx, 0, top);
66 return 0;
69 /***
70 generate for quintic polynomial trajectory
71 @tparam a.trajpoly5 ctx quintic polynomial trajectory userdata
72 @tparam number ts difference between final time and initial time
73 @tparam number p0 initial position
74 @tparam number p1 final position
75 @tparam[opt] number v0 initial velocity
76 @tparam[opt] number v1 final velocity
77 @tparam[opt] number a0 initial acceleration
78 @tparam[opt] number a1 final acceleration
79 @treturn a.trajpoly5 quintic polynomial trajectory userdata
80 @function gen
82 int liba_trajpoly5_gen(lua_State *L)
84 int const top = lua_gettop(L);
85 if (top > 3)
87 luaL_checktype(L, 1, LUA_TUSERDATA);
88 a_trajpoly5 *const ctx = (a_trajpoly5 *)lua_touserdata(L, 1);
89 lua_pushvalue(L, 1);
90 return liba_trajpoly5_gen_(L, ctx, 1, top - 1);
92 return 0;
95 /***
96 calculate position for quintic polynomial trajectory
97 @tparam a.trajpoly5 ctx quintic polynomial trajectory userdata
98 @tparam number x difference between current time and initial time
99 @treturn number position output
100 @function pos
102 int liba_trajpoly5_pos(lua_State *L)
104 a_trajpoly5 const *const ctx = (a_trajpoly5 const *)lua_touserdata(L, 1);
105 if (ctx)
107 a_float const x = (a_float)luaL_checknumber(L, 2);
108 lua_pushnumber(L, (lua_Number)a_trajpoly5_pos(ctx, x));
109 return 1;
111 return 0;
114 /***
115 calculate velocity for quintic polynomial trajectory
116 @tparam a.trajpoly5 ctx quintic polynomial trajectory userdata
117 @tparam number x difference between current time and initial time
118 @treturn number velocity output
119 @function vel
121 int liba_trajpoly5_vel(lua_State *L)
123 a_trajpoly5 const *const ctx = (a_trajpoly5 const *)lua_touserdata(L, 1);
124 if (ctx)
126 a_float const x = (a_float)luaL_checknumber(L, 2);
127 lua_pushnumber(L, (lua_Number)a_trajpoly5_vel(ctx, x));
128 return 1;
130 return 0;
133 /***
134 calculate acceleration for quintic polynomial trajectory
135 @tparam a.trajpoly5 ctx quintic polynomial trajectory userdata
136 @tparam number x difference between current time and initial time
137 @treturn number acceleration output
138 @function acc
140 int liba_trajpoly5_acc(lua_State *L)
142 a_trajpoly5 const *const ctx = (a_trajpoly5 const *)lua_touserdata(L, 1);
143 if (ctx)
145 a_float const x = (a_float)luaL_checknumber(L, 2);
146 lua_pushnumber(L, (lua_Number)a_trajpoly5_acc(ctx, x));
147 return 1;
149 return 0;
152 static int liba_trajpoly5_set(lua_State *L)
154 switch (a_hash_bkdr(lua_tostring(L, 2), 0))
156 case 0xE8859EEB: // __name
157 case 0xA65758B2: // __index
158 case 0xAEB551C6: // __newindex
159 break;
160 default:
161 lua_getmetatable(L, 1);
162 lua_replace(L, 1);
163 lua_rawset(L, 1);
165 return 0;
168 static int liba_trajpoly5_get(lua_State *L)
170 a_trajpoly5 const *const ctx = (a_trajpoly5 const *)lua_touserdata(L, 1);
171 switch (a_hash_bkdr(lua_tostring(L, 2), 0))
173 case 0x00000070: // p
174 lua_array_num_new(L, ctx->p, A_LEN(ctx->p));
175 break;
176 case 0x00000076: // v
177 lua_array_num_new(L, ctx->v, A_LEN(ctx->v));
178 break;
179 case 0x00000061: // a
180 lua_array_num_new(L, ctx->a, A_LEN(ctx->a));
181 break;
182 case 0xA65758B2: // __index
183 lua_registry_get(L, liba_trajpoly5_new);
184 lua_pushstring(L, "p");
185 lua_array_num_new(L, ctx->p, A_LEN(ctx->p));
186 lua_rawset(L, -3);
187 lua_pushstring(L, "v");
188 lua_array_num_new(L, ctx->v, A_LEN(ctx->v));
189 lua_rawset(L, -3);
190 lua_pushstring(L, "a");
191 lua_array_num_new(L, ctx->a, A_LEN(ctx->a));
192 lua_rawset(L, -3);
193 break;
194 default:
195 lua_getmetatable(L, 1);
196 lua_replace(L, 1);
197 lua_rawget(L, 1);
199 return 1;
202 static int liba_trajpoly5_(lua_State *L)
204 lua_pushcfunction(L, liba_trajpoly5_new);
205 lua_replace(L, 1);
206 lua_call(L, lua_gettop(L) - 1, 1);
207 return 1;
210 int luaopen_liba_trajpoly5(lua_State *L)
212 static lua_fun const funcs[] = {
213 {"new", liba_trajpoly5_new},
214 {"gen", liba_trajpoly5_gen},
215 {"pos", liba_trajpoly5_pos},
216 {"vel", liba_trajpoly5_vel},
217 {"acc", liba_trajpoly5_acc},
219 lua_createtable(L, 0, A_LEN(funcs));
220 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
221 lua_createtable(L, 0, 1);
222 lua_fun_set(L, -1, "__call", liba_trajpoly5_);
223 lua_setmetatable(L, -2);
225 static lua_fun const metas[] = {
226 {"__newindex", liba_trajpoly5_set},
227 {"__index", liba_trajpoly5_get},
229 lua_createtable(L, 0, A_LEN(metas) + A_LEN(funcs) + 1);
230 lua_fun_reg(L, -1, metas, A_LEN(metas));
231 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
232 lua_str_set(L, -1, "__name", "a.trajpoly5");
233 lua_registry_set(L, liba_trajpoly5_new);
235 return 1;