2 quintic polynomial trajectory
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;
18 a1
= (a_float
)luaL_checknumber(L
, arg
+ 7);
21 a0
= (a_float
)luaL_checknumber(L
, arg
+ 6);
24 v1
= (a_float
)luaL_checknumber(L
, arg
+ 5);
27 v0
= (a_float
)luaL_checknumber(L
, arg
+ 4);
30 p1
= (a_float
)luaL_checknumber(L
, arg
+ 3);
33 p0
= (a_float
)luaL_checknumber(L
, arg
+ 2);
36 ts
= (a_float
)luaL_checknumber(L
, arg
+ 1);
39 a_trajpoly5_gen(ctx
, ts
, p0
, p1
, v0
, v1
, a0
, a1
);
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
56 int liba_trajpoly5_new(lua_State
*L
)
58 int const top
= lua_gettop(L
);
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
);
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
82 int liba_trajpoly5_gen(lua_State
*L
)
84 int const top
= lua_gettop(L
);
87 luaL_checktype(L
, 1, LUA_TUSERDATA
);
88 a_trajpoly5
*const ctx
= (a_trajpoly5
*)lua_touserdata(L
, 1);
90 return liba_trajpoly5_gen_(L
, ctx
, 1, top
- 1);
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
102 int liba_trajpoly5_pos(lua_State
*L
)
104 a_trajpoly5
const *const ctx
= (a_trajpoly5
const *)lua_touserdata(L
, 1);
107 a_float
const x
= (a_float
)luaL_checknumber(L
, 2);
108 lua_pushnumber(L
, (lua_Number
)a_trajpoly5_pos(ctx
, x
));
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
121 int liba_trajpoly5_vel(lua_State
*L
)
123 a_trajpoly5
const *const ctx
= (a_trajpoly5
const *)lua_touserdata(L
, 1);
126 a_float
const x
= (a_float
)luaL_checknumber(L
, 2);
127 lua_pushnumber(L
, (lua_Number
)a_trajpoly5_vel(ctx
, x
));
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
140 int liba_trajpoly5_acc(lua_State
*L
)
142 a_trajpoly5
const *const ctx
= (a_trajpoly5
const *)lua_touserdata(L
, 1);
145 a_float
const x
= (a_float
)luaL_checknumber(L
, 2);
146 lua_pushnumber(L
, (lua_Number
)a_trajpoly5_acc(ctx
, x
));
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
161 lua_getmetatable(L
, 1);
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
));
176 case 0x00000076: // v
177 lua_array_num_new(L
, ctx
->v
, A_LEN(ctx
->v
));
179 case 0x00000061: // a
180 lua_array_num_new(L
, ctx
->a
, A_LEN(ctx
->a
));
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
));
187 lua_pushstring(L
, "v");
188 lua_array_num_new(L
, ctx
->v
, A_LEN(ctx
->v
));
190 lua_pushstring(L
, "a");
191 lua_array_num_new(L
, ctx
->a
, A_LEN(ctx
->a
));
195 lua_getmetatable(L
, 1);
202 static int liba_trajpoly5_(lua_State
*L
)
204 lua_pushcfunction(L
, liba_trajpoly5_new
);
206 lua_call(L
, lua_gettop(L
) - 1, 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
);