fix: disable diagnostic
[liba.git] / lua / src / polytrack3.c
blobae212bd36dd1d1646bf528716ba19c7a2a6fd4cf
1 /***
2 cubic polynomial trajectory
3 @module liba.polytrack3
4 */
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))
14 case 6:
15 v1 = (a_float_t)luaL_checknumber(L, 6);
16 A_FALLTHROUGH;
17 case 5:
18 v0 = (a_float_t)luaL_checknumber(L, 5);
19 A_FALLTHROUGH;
20 case 4:
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);
25 A_FALLTHROUGH;
26 default:
27 break;
29 a_polytrack3_gen(ctx, t0, t1, q0, q1, v0, v1);
30 return 1;
33 /***
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
44 @function new
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);
68 a_polytrack3_gen(ctx,
69 source[0], target[0],
70 source[1], target[1],
71 source[2], target[2]);
72 return 1;
74 return 0;
77 /***
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
89 @function gen
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]);
115 lua_pushvalue(L, 1);
116 return 1;
118 return 0;
121 /***
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}
126 @function out
128 int liba_polytrack3_out(lua_State *const L)
130 a_polytrack3_s const *const ctx = (a_polytrack3_s const *)lua_touserdata(L, 1);
131 if (ctx)
133 a_float_t out[3];
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));
138 return 1;
140 return 0;
143 /***
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
148 @function pos
150 int liba_polytrack3_pos(lua_State *const L)
152 a_polytrack3_s const *const ctx = (a_polytrack3_s const *)lua_touserdata(L, 1);
153 if (ctx)
155 a_float_t const dt = (a_float_t)luaL_checknumber(L, 2);
156 lua_pushnumber(L, (lua_Number)a_polytrack3_pos(ctx, dt));
157 return 1;
159 return 0;
162 /***
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
167 @function vel
169 int liba_polytrack3_vel(lua_State *const L)
171 a_polytrack3_s const *const ctx = (a_polytrack3_s const *)lua_touserdata(L, 1);
172 if (ctx)
174 a_float_t const dt = (a_float_t)luaL_checknumber(L, 2);
175 lua_pushnumber(L, (lua_Number)a_polytrack3_vel(ctx, dt));
176 return 1;
178 return 0;
181 /***
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
186 @function acc
188 int liba_polytrack3_acc(lua_State *const L)
190 a_polytrack3_s const *const ctx = (a_polytrack3_s const *)lua_touserdata(L, 1);
191 if (ctx)
193 a_float_t const dt = (a_float_t)luaL_checknumber(L, 2);
194 lua_pushnumber(L, (lua_Number)a_polytrack3_acc(ctx, dt));
195 return 1;
197 return 0;
200 #undef funcs
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},
209 {NULL, NULL},
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);
216 switch (hash)
218 case 0xE8859EEB: // __name
219 case 0xE70C48C6: // __call
220 case 0xA65758B2: // __index
221 case 0xAEB551C6: // __newindex
222 return 0;
223 default:
224 lua_getmetatable(L, 1);
225 lua_pushvalue(L, 3);
226 lua_setfield(L, 4, field);
227 return 0;
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);
236 switch (hash)
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));
241 break;
242 case 0x001D0204: // new
243 lua_pushcfunction(L, liba_polytrack3_new);
244 break;
245 case 0x001B2CBC: // gen
246 lua_pushcfunction(L, liba_polytrack3_gen);
247 break;
248 case 0x001D4D3A: // out
249 lua_pushcfunction(L, liba_polytrack3_out);
250 break;
251 case 0x001D8D30: // pos
252 lua_pushcfunction(L, liba_polytrack3_pos);
253 break;
254 case 0x001F1A41: // vel
255 lua_pushcfunction(L, liba_polytrack3_vel);
256 break;
257 case 0x00199975: // acc
258 lua_pushcfunction(L, liba_polytrack3_acc);
259 break;
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");
267 break;
269 default:
270 lua_getmetatable(L, 1);
271 lua_getfield(L, 3, field);
273 return 1;
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},
288 {NULL, NULL},
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)
302 if (ret)
304 lua_rawgetp(L, LUA_REGISTRYINDEX, L_FUNC2P(liba_polytrack3_func_));
305 return 1;
307 lua_rawsetp(L, LUA_REGISTRYINDEX, L_FUNC2P(liba_polytrack3_func_));
308 return 0;
311 int liba_polytrack3_meta_(lua_State *const L, int const ret)
313 if (ret)
315 lua_rawgetp(L, LUA_REGISTRYINDEX, L_FUNC2P(liba_polytrack3_meta_));
316 return 1;
318 lua_rawsetp(L, LUA_REGISTRYINDEX, L_FUNC2P(liba_polytrack3_meta_));
319 return 0;