create regress_simple for JavaScript
[liba.git] / lua / src / trajpoly7.c
blob83cb4ac7c9d9ecf4d3c4740408e39ecb19bee245
1 /***
2 hepta polynomial trajectory
3 @module liba.trajpoly7
4 */
6 #include "trajpoly7.h"
7 #include "a/trajpoly7.h"
9 static int liba_trajpoly7_gen_(lua_State *L, a_trajpoly7 *ctx, int arg, int top)
11 a_float p0 = 0, v0 = 0, a0 = 0, j0 = 0;
12 a_float p1 = 0, v1 = 0, a1 = 0, j1 = 0;
13 a_float ts = 0;
14 switch (top)
16 default:
17 case 9:
18 j1 = (a_float)luaL_checknumber(L, arg + 9);
19 A_FALLTHROUGH;
20 case 8:
21 j0 = (a_float)luaL_checknumber(L, arg + 8);
22 A_FALLTHROUGH;
23 case 7:
24 a1 = (a_float)luaL_checknumber(L, arg + 7);
25 A_FALLTHROUGH;
26 case 6:
27 a0 = (a_float)luaL_checknumber(L, arg + 6);
28 A_FALLTHROUGH;
29 case 5:
30 v1 = (a_float)luaL_checknumber(L, arg + 5);
31 A_FALLTHROUGH;
32 case 4:
33 v0 = (a_float)luaL_checknumber(L, arg + 4);
34 A_FALLTHROUGH;
35 case 3:
36 p1 = (a_float)luaL_checknumber(L, arg + 3);
37 A_FALLTHROUGH;
38 case 2:
39 p0 = (a_float)luaL_checknumber(L, arg + 2);
40 A_FALLTHROUGH;
41 case 1:
42 ts = (a_float)luaL_checknumber(L, arg + 1);
43 A_FALLTHROUGH;
44 case 0:
45 a_trajpoly7_gen(ctx, ts, p0, p1, v0, v1, a0, a1, j0, j1);
47 return 1;
50 /***
51 constructor for hepta polynomial trajectory
52 @tparam number ts difference between final time and initial time
53 @tparam number p0 initial position
54 @tparam number p1 final position
55 @tparam[opt] number v0 initial velocity
56 @tparam[opt] number v1 final velocity
57 @tparam[opt] number a0 initial acceleration
58 @tparam[opt] number a1 final acceleration
59 @tparam[opt] number j0 initial jerk
60 @tparam[opt] number j1 final jerk
61 @treturn a.trajpoly7 hepta polynomial trajectory userdata
62 @function new
64 int liba_trajpoly7_new(lua_State *L)
66 int const top = lua_gettop(L);
67 if (top > 2)
69 a_trajpoly7 *const ctx = lua_newclass(L, a_trajpoly7);
70 lua_registry_get(L, liba_trajpoly7_new);
71 lua_setmetatable(L, -2);
72 return liba_trajpoly7_gen_(L, ctx, 0, top);
74 return 0;
77 /***
78 generate for hepta polynomial trajectory
79 @tparam a.trajpoly7 ctx hepta polynomial trajectory userdata
80 @tparam number ts difference between final time and initial time
81 @tparam number p0 initial position
82 @tparam number p1 final position
83 @tparam[opt] number v0 initial velocity
84 @tparam[opt] number v1 final velocity
85 @tparam[opt] number a0 initial acceleration
86 @tparam[opt] number a1 final acceleration
87 @tparam[opt] number j0 initial jerk
88 @tparam[opt] number j1 final jerk
89 @treturn a.trajpoly7 hepta polynomial trajectory userdata
90 @function gen
92 int liba_trajpoly7_gen(lua_State *L)
94 int const top = lua_gettop(L);
95 if (top > 3)
97 luaL_checktype(L, 1, LUA_TUSERDATA);
98 a_trajpoly7 *const ctx = (a_trajpoly7 *)lua_touserdata(L, 1);
99 lua_pushvalue(L, 1);
100 return liba_trajpoly7_gen_(L, ctx, 1, top - 1);
102 return 0;
105 /***
106 calculate position for hepta polynomial trajectory
107 @tparam a.trajpoly7 ctx hepta polynomial trajectory userdata
108 @tparam number x difference between current time and initial time
109 @treturn number position output
110 @function pos
112 int liba_trajpoly7_pos(lua_State *L)
114 a_trajpoly7 const *const ctx = (a_trajpoly7 const *)lua_touserdata(L, 1);
115 if (ctx)
117 a_float const x = (a_float)luaL_checknumber(L, 2);
118 lua_pushnumber(L, (lua_Number)a_trajpoly7_pos(ctx, x));
119 return 1;
121 return 0;
124 /***
125 calculate velocity for hepta polynomial trajectory
126 @tparam a.trajpoly7 ctx hepta polynomial trajectory userdata
127 @tparam number x difference between current time and initial time
128 @treturn number velocity output
129 @function vel
131 int liba_trajpoly7_vel(lua_State *L)
133 a_trajpoly7 const *const ctx = (a_trajpoly7 const *)lua_touserdata(L, 1);
134 if (ctx)
136 a_float const x = (a_float)luaL_checknumber(L, 2);
137 lua_pushnumber(L, (lua_Number)a_trajpoly7_vel(ctx, x));
138 return 1;
140 return 0;
143 /***
144 calculate acceleration for hepta polynomial trajectory
145 @tparam a.trajpoly7 ctx hepta polynomial trajectory userdata
146 @tparam number x difference between current time and initial time
147 @treturn number acceleration output
148 @function acc
150 int liba_trajpoly7_acc(lua_State *L)
152 a_trajpoly7 const *const ctx = (a_trajpoly7 const *)lua_touserdata(L, 1);
153 if (ctx)
155 a_float const x = (a_float)luaL_checknumber(L, 2);
156 lua_pushnumber(L, (lua_Number)a_trajpoly7_acc(ctx, x));
157 return 1;
159 return 0;
162 /***
163 calculate jerk for hepta polynomial trajectory
164 @tparam a.trajpoly7 ctx hepta polynomial trajectory userdata
165 @tparam number x difference between current time and initial time
166 @treturn number jerk output
167 @function jer
169 int liba_trajpoly7_jer(lua_State *L)
171 a_trajpoly7 const *const ctx = (a_trajpoly7 const *)lua_touserdata(L, 1);
172 if (ctx)
174 a_float const x = (a_float)luaL_checknumber(L, 2);
175 lua_pushnumber(L, (lua_Number)a_trajpoly7_jer(ctx, x));
176 return 1;
178 return 0;
181 static int liba_trajpoly7_set(lua_State *L)
183 switch (a_hash_bkdr(lua_tostring(L, 2), 0))
185 case 0xE8859EEB: // __name
186 case 0xA65758B2: // __index
187 case 0xAEB551C6: // __newindex
188 break;
189 default:
190 lua_getmetatable(L, 1);
191 lua_replace(L, 1);
192 lua_rawset(L, 1);
194 return 0;
197 static int liba_trajpoly7_get(lua_State *L)
199 a_trajpoly7 const *const ctx = (a_trajpoly7 const *)lua_touserdata(L, 1);
200 switch (a_hash_bkdr(lua_tostring(L, 2), 0))
202 case 0x00000070: // p
203 lua_array_num_new(L, ctx->p, A_LEN(ctx->p));
204 break;
205 case 0x00000076: // v
206 lua_array_num_new(L, ctx->v, A_LEN(ctx->v));
207 break;
208 case 0x00000061: // a
209 lua_array_num_new(L, ctx->a, A_LEN(ctx->a));
210 break;
211 case 0x0000006A: // j
212 lua_array_num_new(L, ctx->j, A_LEN(ctx->j));
213 break;
214 case 0xA65758B2: // __index
215 lua_registry_get(L, liba_trajpoly7_new);
216 lua_pushstring(L, "p");
217 lua_array_num_new(L, ctx->p, A_LEN(ctx->p));
218 lua_rawset(L, -3);
219 lua_pushstring(L, "v");
220 lua_array_num_new(L, ctx->v, A_LEN(ctx->v));
221 lua_rawset(L, -3);
222 lua_pushstring(L, "a");
223 lua_array_num_new(L, ctx->a, A_LEN(ctx->a));
224 lua_rawset(L, -3);
225 lua_pushstring(L, "j");
226 lua_array_num_new(L, ctx->j, A_LEN(ctx->j));
227 lua_rawset(L, -3);
228 break;
229 default:
230 lua_getmetatable(L, 1);
231 lua_replace(L, 1);
232 lua_rawget(L, 1);
234 return 1;
237 static int liba_trajpoly7_(lua_State *L)
239 lua_pushcfunction(L, liba_trajpoly7_new);
240 lua_replace(L, 1);
241 lua_call(L, lua_gettop(L) - 1, 1);
242 return 1;
245 int luaopen_liba_trajpoly7(lua_State *L)
247 static lua_fun const funcs[] = {
248 {"new", liba_trajpoly7_new},
249 {"gen", liba_trajpoly7_gen},
250 {"pos", liba_trajpoly7_pos},
251 {"vel", liba_trajpoly7_vel},
252 {"acc", liba_trajpoly7_acc},
253 {"jer", liba_trajpoly7_jer},
255 lua_createtable(L, 0, A_LEN(funcs));
256 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
257 lua_createtable(L, 0, 1);
258 lua_fun_set(L, -1, "__call", liba_trajpoly7_);
259 lua_setmetatable(L, -2);
261 static lua_fun const metas[] = {
262 {"__newindex", liba_trajpoly7_set},
263 {"__index", liba_trajpoly7_get},
265 lua_createtable(L, 0, A_LEN(metas) + A_LEN(funcs) + 1);
266 lua_fun_reg(L, -1, metas, A_LEN(metas));
267 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
268 lua_str_set(L, -1, "__name", "a.trajpoly7");
269 lua_registry_set(L, liba_trajpoly7_new);
271 return 1;