2 cubic polynomial trajectory
3 @module liba.polytrack3
6 #include "polytrack3.h"
8 static int liba_polytrack3_gen_(lua_State
*const L
, a_polytrack3_s
*const ctx
)
10 a_float_t t0
= 0, q0
= 0, v0
= 0;
11 a_float_t t1
= 0, q1
= 0, v1
= 0;
12 switch (lua_gettop(L
) - lua_isuserdata(L
, -1))
15 v1
= (a_float_t
)luaL_checknumber(L
, 6);
18 v0
= (a_float_t
)luaL_checknumber(L
, 5);
21 q1
= (a_float_t
)luaL_checknumber(L
, 4);
22 q0
= (a_float_t
)luaL_checknumber(L
, 3);
23 t1
= (a_float_t
)luaL_checknumber(L
, 2);
24 t0
= (a_float_t
)luaL_checknumber(L
, 1);
29 a_polytrack3_gen(ctx
, t0
, t1
, q0
, q1
, v0
, v1
);
34 constructor for cubic polynomial trajectory
35 @tparam number t0 time for source
36 @tparam number t1 time for target
37 @tparam number q0 position for source
38 @tparam number q1 position for target
39 @tparam[opt] number v0 velocity for source
40 @tparam[opt] number v1 velocity for target
41 @tparam[opt] table source source for trajectory
42 @tparam[opt] table target target for trajectory
43 @treturn a.polytrack3 cubic polynomial trajectory userdata
46 int liba_polytrack3_new(lua_State
*const L
)
48 int const top
= lua_gettop(L
);
49 int const type
= lua_type(L
, 1);
50 if (top
> 3 && type
== LUA_TNUMBER
)
52 a_polytrack3_s
*const ctx
= (a_polytrack3_s
*)lua_newuserdata(L
, sizeof(a_polytrack3_s
));
53 liba_polytrack3_meta_(L
, 1);
54 lua_setmetatable(L
, -2);
55 return liba_polytrack3_gen_(L
, ctx
);
57 if (top
> 1 && type
== LUA_TTABLE
)
59 a_float_t source
[3] = {0};
60 a_float_t target
[3] = {0};
61 luaL_checktype(L
, 1, LUA_TTABLE
);
62 luaL_checktype(L
, 2, LUA_TTABLE
);
63 l_array_num_get(L
, 1, source
, A_LEN(source
));
64 l_array_num_get(L
, 2, target
, A_LEN(target
));
65 a_polytrack3_s
*const ctx
= (a_polytrack3_s
*)lua_newuserdata(L
, sizeof(a_polytrack3_s
));
66 liba_polytrack3_meta_(L
, 1);
67 lua_setmetatable(L
, -2);
71 source
[2], target
[2]);
78 generate for cubic polynomial trajectory
79 @tparam a.polytrack3 ctx cubic polynomial trajectory userdata
80 @tparam number t0 time for source
81 @tparam number t1 time for target
82 @tparam number q0 position for source
83 @tparam number q1 position for target
84 @tparam[opt] number v0 velocity for source
85 @tparam[opt] number v1 velocity for target
86 @tparam[opt] table source source for trajectory
87 @tparam[opt] table target target for trajectory
88 @treturn a.polytrack3 cubic polynomial trajectory userdata
91 int liba_polytrack3_gen(lua_State
*const L
)
93 int const top
= lua_gettop(L
);
94 int const type
= lua_type(L
, 2);
95 if (top
> 4 && type
== LUA_TNUMBER
)
97 luaL_checktype(L
, 1, LUA_TUSERDATA
);
98 a_polytrack3_s
*const ctx
= (a_polytrack3_s
*)lua_touserdata(L
, 1);
99 return liba_polytrack3_gen_(L
, ctx
);
101 if (top
> 2 && type
== LUA_TTABLE
)
103 a_float_t source
[3] = {0};
104 a_float_t target
[3] = {0};
105 luaL_checktype(L
, 1, LUA_TUSERDATA
);
106 luaL_checktype(L
, 2, LUA_TTABLE
);
107 luaL_checktype(L
, 3, LUA_TTABLE
);
108 a_polytrack3_s
*const ctx
= (a_polytrack3_s
*)lua_touserdata(L
, 1);
109 l_array_num_get(L
, 2, source
, A_LEN(source
));
110 l_array_num_get(L
, 3, target
, A_LEN(target
));
111 a_polytrack3_gen(ctx
,
112 source
[0], target
[0],
113 source
[1], target
[1],
114 source
[2], target
[2]);
122 calculate for cubic polynomial trajectory
123 @tparam a.polytrack3 ctx cubic polynomial trajectory userdata
124 @tparam number dt difference between current time and initial time
125 @treturn table {position,velocity,acceleration}
128 int liba_polytrack3_out(lua_State
*const L
)
130 a_polytrack3_s
const *const ctx
= (a_polytrack3_s
const *)lua_touserdata(L
, 1);
134 a_float_t
const dt
= (a_float_t
)luaL_checknumber(L
, 2);
135 a_polytrack3_out(ctx
, dt
, out
);
136 lua_createtable(L
, A_LEN(out
), 0);
137 l_array_num_set(L
, -1, out
, A_LEN(out
));
144 calculate for cubic polynomial trajectory position
145 @tparam a.polytrack3 ctx cubic polynomial trajectory userdata
146 @tparam number dt difference between current time and initial time
147 @treturn number position output
150 int liba_polytrack3_pos(lua_State
*const L
)
152 a_polytrack3_s
const *const ctx
= (a_polytrack3_s
const *)lua_touserdata(L
, 1);
155 a_float_t
const dt
= (a_float_t
)luaL_checknumber(L
, 2);
156 lua_pushnumber(L
, (lua_Number
)a_polytrack3_pos(ctx
, dt
));
163 calculate for cubic polynomial trajectory velocity
164 @tparam a.polytrack3 ctx cubic polynomial trajectory userdata
165 @tparam number dt difference between current time and initial time
166 @treturn number velocity output
169 int liba_polytrack3_vel(lua_State
*const L
)
171 a_polytrack3_s
const *const ctx
= (a_polytrack3_s
const *)lua_touserdata(L
, 1);
174 a_float_t
const dt
= (a_float_t
)luaL_checknumber(L
, 2);
175 lua_pushnumber(L
, (lua_Number
)a_polytrack3_vel(ctx
, dt
));
182 calculate for cubic polynomial trajectory acceleration
183 @tparam a.polytrack3 ctx cubic polynomial trajectory userdata
184 @tparam number dt difference between current time and initial time
185 @treturn number acceleration output
188 int liba_polytrack3_acc(lua_State
*const L
)
190 a_polytrack3_s
const *const ctx
= (a_polytrack3_s
const *)lua_touserdata(L
, 1);
193 a_float_t
const dt
= (a_float_t
)luaL_checknumber(L
, 2);
194 lua_pushnumber(L
, (lua_Number
)a_polytrack3_acc(ctx
, dt
));
201 #define funcs liba_polytrack3_funcs
202 static l_func_s
const funcs
[] = {
203 {"new", liba_polytrack3_new
},
204 {"gen", liba_polytrack3_gen
},
205 {"out", liba_polytrack3_out
},
206 {"pos", liba_polytrack3_pos
},
207 {"vel", liba_polytrack3_vel
},
208 {"acc", liba_polytrack3_acc
},
212 static int liba_polytrack3_set(lua_State
*const L
)
214 char const *const field
= lua_tostring(L
, 2);
215 a_u32_t
const hash
= (a_u32_t
)a_hash_bkdr(field
, 0);
218 case 0xE8859EEB: // __name
219 case 0xE70C48C6: // __call
220 case 0xA65758B2: // __index
221 case 0xAEB551C6: // __newindex
224 lua_getmetatable(L
, 1);
226 lua_setfield(L
, 4, field
);
231 static int liba_polytrack3_get(lua_State
*const L
)
233 char const *const field
= lua_tostring(L
, 2);
234 a_polytrack3_s
const *const ctx
= (a_polytrack3_s
const *)lua_touserdata(L
, 1);
235 a_u32_t
const hash
= (a_u32_t
)a_hash_bkdr(field
, 0);
238 case 0x0000006B: // k
239 lua_createtable(L
, A_LEN(ctx
->k
), 0);
240 l_array_num_set(L
, -1, ctx
->k
, A_LEN(ctx
->k
));
242 case 0x001D0204: // new
243 lua_pushcfunction(L
, liba_polytrack3_new
);
245 case 0x001B2CBC: // gen
246 lua_pushcfunction(L
, liba_polytrack3_gen
);
248 case 0x001D4D3A: // out
249 lua_pushcfunction(L
, liba_polytrack3_out
);
251 case 0x001D8D30: // pos
252 lua_pushcfunction(L
, liba_polytrack3_pos
);
254 case 0x001F1A41: // vel
255 lua_pushcfunction(L
, liba_polytrack3_vel
);
257 case 0x00199975: // acc
258 lua_pushcfunction(L
, liba_polytrack3_acc
);
260 case 0xA65758B2: // __index
262 lua_createtable(L
, 0, A_LEN(funcs
));
263 l_func_reg(L
, -1, funcs
);
264 lua_createtable(L
, A_LEN(ctx
->k
), 0);
265 l_array_num_set(L
, -1, ctx
->k
, A_LEN(ctx
->k
));
266 lua_setfield(L
, -2, "k");
270 lua_getmetatable(L
, 1);
271 lua_getfield(L
, 3, field
);
276 int luaopen_liba_polytrack3(lua_State
*const L
)
278 lua_createtable(L
, 0, A_LEN(funcs
) - 1);
279 l_func_reg(L
, -1, funcs
);
280 lua_createtable(L
, 0, 1);
281 l_func_set(L
, -1, L_SET
, liba_setter
);
282 lua_setmetatable(L
, -2);
284 l_func_s
const metas
[] = {
285 {L_FUN
, liba_polytrack3_out
},
286 {L_SET
, liba_polytrack3_set
},
287 {L_GET
, liba_polytrack3_get
},
290 lua_createtable(L
, 0, A_LEN(metas
));
291 l_str_set(L
, -1, L_NAME
, "a.polytrack3");
292 l_func_reg(L
, -1, metas
);
294 liba_polytrack3_meta_(L
, 0);
295 liba_polytrack3_func_(L
, 0);
297 return liba_polytrack3_func_(L
, 1);
300 int liba_polytrack3_func_(lua_State
*const L
, int const ret
)
304 lua_rawgetp(L
, LUA_REGISTRYINDEX
, L_FUNC2P(liba_polytrack3_func_
));
307 lua_rawsetp(L
, LUA_REGISTRYINDEX
, L_FUNC2P(liba_polytrack3_func_
));
311 int liba_polytrack3_meta_(lua_State
*const L
, int const ret
)
315 lua_rawgetp(L
, LUA_REGISTRYINDEX
, L_FUNC2P(liba_polytrack3_meta_
));
318 lua_rawsetp(L
, LUA_REGISTRYINDEX
, L_FUNC2P(liba_polytrack3_meta_
));