2 cubic polynomial trajectory
7 #include "a/trajpoly3.h"
9 static int liba_trajpoly3_gen_(lua_State
*L
, a_trajpoly3
*ctx
, int arg
, int top
)
11 a_float p0
= 0, v0
= 0;
12 a_float p1
= 0, v1
= 0;
18 v1
= (a_float
)luaL_checknumber(L
, arg
+ 5);
21 v0
= (a_float
)luaL_checknumber(L
, arg
+ 4);
24 p1
= (a_float
)luaL_checknumber(L
, arg
+ 3);
27 p0
= (a_float
)luaL_checknumber(L
, arg
+ 2);
30 ts
= (a_float
)luaL_checknumber(L
, arg
+ 1);
33 a_trajpoly3_gen(ctx
, ts
, p0
, p1
, v0
, v1
);
39 constructor for cubic polynomial trajectory
40 @tparam number ts difference between final time and initial time
41 @tparam number p0 initial position
42 @tparam number p1 final position
43 @tparam[opt] number v0 initial velocity
44 @tparam[opt] number v1 final velocity
45 @treturn a.trajpoly3 cubic polynomial trajectory userdata
48 int liba_trajpoly3_new(lua_State
*L
)
50 int const top
= lua_gettop(L
);
53 a_trajpoly3
*const ctx
= lua_newclass(L
, a_trajpoly3
);
54 lua_registry_get(L
, liba_trajpoly3_new
);
55 lua_setmetatable(L
, -2);
56 return liba_trajpoly3_gen_(L
, ctx
, 0, top
);
62 generate for cubic polynomial trajectory
63 @tparam a.trajpoly3 ctx cubic polynomial trajectory userdata
64 @tparam number ts difference between final time and initial time
65 @tparam number p0 initial position
66 @tparam number p1 final position
67 @tparam[opt] number v0 initial velocity
68 @tparam[opt] number v1 final velocity
69 @treturn a.trajpoly3 cubic polynomial trajectory userdata
72 int liba_trajpoly3_gen(lua_State
*L
)
74 int const top
= lua_gettop(L
);
77 luaL_checktype(L
, 1, LUA_TUSERDATA
);
78 a_trajpoly3
*const ctx
= (a_trajpoly3
*)lua_touserdata(L
, 1);
80 return liba_trajpoly3_gen_(L
, ctx
, 1, top
- 1);
86 calculate position for cubic polynomial trajectory
87 @tparam a.trajpoly3 ctx cubic polynomial trajectory userdata
88 @tparam number x difference between current time and initial time
89 @treturn number position output
92 int liba_trajpoly3_pos(lua_State
*L
)
94 a_trajpoly3
const *const ctx
= (a_trajpoly3
const *)lua_touserdata(L
, 1);
97 a_float
const x
= (a_float
)luaL_checknumber(L
, 2);
98 lua_pushnumber(L
, (lua_Number
)a_trajpoly3_pos(ctx
, x
));
105 calculate velocity for cubic polynomial trajectory
106 @tparam a.trajpoly3 ctx cubic polynomial trajectory userdata
107 @tparam number x difference between current time and initial time
108 @treturn number velocity output
111 int liba_trajpoly3_vel(lua_State
*L
)
113 a_trajpoly3
const *const ctx
= (a_trajpoly3
const *)lua_touserdata(L
, 1);
116 a_float
const x
= (a_float
)luaL_checknumber(L
, 2);
117 lua_pushnumber(L
, (lua_Number
)a_trajpoly3_vel(ctx
, x
));
124 calculate acceleration for cubic polynomial trajectory
125 @tparam a.trajpoly3 ctx cubic polynomial trajectory userdata
126 @tparam number x difference between current time and initial time
127 @treturn number acceleration output
130 int liba_trajpoly3_acc(lua_State
*L
)
132 a_trajpoly3
const *const ctx
= (a_trajpoly3
const *)lua_touserdata(L
, 1);
135 a_float
const x
= (a_float
)luaL_checknumber(L
, 2);
136 lua_pushnumber(L
, (lua_Number
)a_trajpoly3_acc(ctx
, x
));
142 static int liba_trajpoly3_set(lua_State
*L
)
144 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
146 case 0xE8859EEB: // __name
147 case 0xA65758B2: // __index
148 case 0xAEB551C6: // __newindex
151 lua_getmetatable(L
, 1);
158 static int liba_trajpoly3_get(lua_State
*L
)
160 a_trajpoly3
const *const ctx
= (a_trajpoly3
const *)lua_touserdata(L
, 1);
161 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
163 case 0x00000070: // p
164 lua_array_num_new(L
, ctx
->p
, A_LEN(ctx
->p
));
166 case 0x00000076: // v
167 lua_array_num_new(L
, ctx
->v
, A_LEN(ctx
->v
));
169 case 0x00000061: // a
170 lua_array_num_new(L
, ctx
->a
, A_LEN(ctx
->a
));
172 case 0xA65758B2: // __index
173 lua_registry_get(L
, liba_trajpoly3_new
);
174 lua_pushstring(L
, "p");
175 lua_array_num_new(L
, ctx
->p
, A_LEN(ctx
->p
));
177 lua_pushstring(L
, "v");
178 lua_array_num_new(L
, ctx
->v
, A_LEN(ctx
->v
));
180 lua_pushstring(L
, "a");
181 lua_array_num_new(L
, ctx
->a
, A_LEN(ctx
->a
));
185 lua_getmetatable(L
, 1);
192 static int liba_trajpoly3_(lua_State
*L
)
194 lua_pushcfunction(L
, liba_trajpoly3_new
);
196 lua_call(L
, lua_gettop(L
) - 1, 1);
200 int luaopen_liba_trajpoly3(lua_State
*L
)
202 static lua_fun
const funcs
[] = {
203 {"new", liba_trajpoly3_new
},
204 {"gen", liba_trajpoly3_gen
},
205 {"pos", liba_trajpoly3_pos
},
206 {"vel", liba_trajpoly3_vel
},
207 {"acc", liba_trajpoly3_acc
},
209 lua_createtable(L
, 0, A_LEN(funcs
));
210 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
211 lua_createtable(L
, 0, 1);
212 lua_fun_set(L
, -1, "__call", liba_trajpoly3_
);
213 lua_setmetatable(L
, -2);
215 static lua_fun
const metas
[] = {
216 {"__newindex", liba_trajpoly3_set
},
217 {"__index", liba_trajpoly3_get
},
219 lua_createtable(L
, 0, A_LEN(metas
) + A_LEN(funcs
) + 1);
220 lua_fun_reg(L
, -1, metas
, A_LEN(metas
));
221 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
222 lua_str_set(L
, -1, "__name", "a.trajpoly3");
223 lua_registry_set(L
, liba_trajpoly3_new
);