naev 0.11.5
nlua_transform.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11
12#include "naev.h"
15#include "nlua_transform.h"
16
17#include "log.h"
18#include "ndata.h"
19#include "nluadef.h"
20
21/* Transform metatable methods. */
22static int transformL_eq( lua_State *L );
23static int transformL_new( lua_State *L );
24static int transformL_mul( lua_State *L );
25static int transformL_get( lua_State *L );
26static int transformL_set( lua_State *L );
27static int transformL_scale( lua_State *L );
28static int transformL_translate( lua_State *L );
29static int transformL_rotate2d( lua_State *L );
30static int transformL_ortho( lua_State *L );
31static int transformL_applyPoint( lua_State *L );
32static int transformL_applyDim( lua_State *L );
33static const luaL_Reg transformL_methods[] = {
34 { "__eq", transformL_eq },
35 { "__mul", transformL_mul },
36 { "get", transformL_get },
37 { "set", transformL_set },
38 { "new", transformL_new },
39 { "scale", transformL_scale },
40 { "translate", transformL_translate },
41 { "rotate2d", transformL_rotate2d },
42 { "ortho", transformL_ortho },
43 { "applyPoint", transformL_applyPoint },
44 { "applyDim", transformL_applyDim },
45 {0,0}
46};
54int nlua_loadTransform( nlua_env env )
55{
56 nlua_register(env, TRANSFORM_METATABLE, transformL_methods, 1);
57 return 0;
58}
59
72mat4* lua_totransform( lua_State *L, int ind )
73{
74 return (mat4*) lua_touserdata(L,ind);
75}
83mat4* luaL_checktransform( lua_State *L, int ind )
84{
85 if (lua_istransform(L,ind))
86 return lua_totransform(L,ind);
87 luaL_typerror(L, ind, TRANSFORM_METATABLE);
88 return NULL;
89}
97mat4* lua_pushtransform( lua_State *L, mat4 transform )
98{
99 mat4 *t = (mat4*) lua_newuserdata(L, sizeof(mat4));
100 *t = transform;
101 luaL_getmetatable(L, TRANSFORM_METATABLE);
102 lua_setmetatable(L, -2);
103 return t;
104}
112int lua_istransform( lua_State *L, int ind )
113{
114 int ret;
115
116 if (lua_getmetatable(L,ind)==0)
117 return 0;
118 lua_getfield(L, LUA_REGISTRYINDEX, TRANSFORM_METATABLE);
119
120 ret = 0;
121 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
122 ret = 1;
123
124 lua_pop(L, 2); /* remove both metatables */
125 return ret;
126}
127
136static int transformL_eq( lua_State *L )
137{
138 const mat4 *t1 = luaL_checktransform(L,1);
139 const mat4 *t2 = luaL_checktransform(L,2);
140 lua_pushboolean( L, (memcmp( t1, t2, sizeof(mat4) )==0) );
141 return 1;
142}
143
150static int transformL_new( lua_State *L )
151{
152 if (lua_istransform(L,1)) {
153 const mat4 *M = lua_totransform(L,1);
154 lua_pushtransform( L, *M );
155 }
156 else
158 return 1;
159}
160
169static int transformL_mul( lua_State *L )
170{
171 const mat4 *A = luaL_checktransform(L, 1);
172 const mat4 *B = luaL_checktransform(L, 2);
173 mat4 C;
174 mat4_mul( &C, A, B );
176 return 1;
177}
178
188static int transformL_get( lua_State *L )
189{
190 mat4 *M = luaL_checktransform(L, 1);
191 lua_newtable(L); /* t */
192 for (int i=0; i<4; i++) {
193 lua_newtable(L); /* t, t */
194 for (int j=0; j<4; j++) {
195 lua_pushnumber(L,M->m[j][i]); /* t, t, n */
196 lua_rawseti(L,-2,j+1); /* t, t */
197 }
198 lua_rawseti(L,-2,i+1); /* t */
199 }
200 return 1;
201}
202
212static int transformL_set( lua_State *L )
213{
214 mat4 *M = luaL_checktransform(L, 1);
215 int i = luaL_checkinteger(L, 2)-1;
216 int j = luaL_checkinteger(L, 3)-1;
217 double v = luaL_checknumber(L, 4);
218#if DEBUGGING
219 if (i < 0 || i > 3) {
220 WARN(_("Matrix column value not in range: %d"),i);
221 i = CLAMP(0,3,i);
222 }
223 if (j < 0 || j > 3) {
224 WARN(_("Matrix row value not in range: %d"),j);
225 j = CLAMP(0,3,j);
226 }
227#endif /* DEBUGGING */
228 M->m[i][j] = v;
229 return 0;
230}
231
242static int transformL_scale( lua_State *L )
243{
244 const mat4 *M = luaL_checktransform(L, 1);
245 double x = luaL_checknumber(L,2);
246 double y = luaL_checknumber(L,3);
247 double z = luaL_optnumber(L,4,1.);
248 mat4 out = *M;
249 mat4_scale( &out, x, y, z );
250 lua_pushtransform(L, out);
251 return 1;
252}
253
264static int transformL_translate( lua_State *L )
265{
266 const mat4 *M = luaL_checktransform(L, 1);
267 double x = luaL_checknumber(L,2);
268 double y = luaL_checknumber(L,3);
269 double z = luaL_optnumber(L,4,0.);
270 mat4 out = *M;
271 mat4_translate( &out, x, y, z );
272 lua_pushtransform(L, out);
273 return 1;
274}
275
283static int transformL_rotate2d( lua_State *L )
284{
285 const mat4 *M = luaL_checktransform(L, 1);
286 double a = luaL_checknumber(L,2);
287 mat4 out = *M;
288 mat4_rotate2d( &out, a );
289 lua_pushtransform(L, out);
290 return 1;
291}
292
305static int transformL_ortho( lua_State *L )
306{
307 double left = luaL_checknumber(L,1);
308 double right = luaL_checknumber(L,2);
309 double bottom = luaL_checknumber(L,3);
310 double top = luaL_checknumber(L,4);
311 double nearVal = luaL_checknumber(L,5);
312 double farVal = luaL_checknumber(L,6);
313 lua_pushtransform(L, mat4_ortho(left, right, bottom, top, nearVal, farVal) );
314 return 1;
315}
316
329static int transformL_applyPoint( lua_State *L )
330{
331 double gp[3], p[3];
332 mat4 *M = luaL_checktransform(L, 1);
333 gp[0] = luaL_checknumber(L,2);
334 gp[1] = luaL_checknumber(L,3);
335 gp[2] = luaL_checknumber(L,4);
336
337 for (int i=0; i<3; i++)
338 p[i] = M->m[0][i]*gp[0] + M->m[1][i]*gp[1] + M->m[2][i]*gp[2] + M->m[3][i];
339
340 lua_pushnumber(L, p[0]);
341 lua_pushnumber(L, p[1]);
342 lua_pushnumber(L, p[2]);
343 return 3;
344}
345
360static int transformL_applyDim( lua_State *L )
361{
362 double gp[3], p[3];
363 mat4 *M = luaL_checktransform(L, 1);
364 gp[0] = luaL_checknumber(L,2);
365 gp[1] = luaL_checknumber(L,3);
366 gp[2] = luaL_checknumber(L,4);
367
368 for (int i=0; i<3; i++)
369 p[i] = M->m[0][i]*gp[0] + M->m[1][i]*gp[1] + M->m[2][i]*gp[2];
370
371 lua_pushnumber(L, p[0]);
372 lua_pushnumber(L, p[1]);
373 lua_pushnumber(L, p[2]);
374 return 3;
375}
void mat4_translate(mat4 *m, double x, double y, double z)
Translates a homogenous transformation matrix.
Definition mat4.c:99
mat4 mat4_identity(void)
Creates an identity matrix.
Definition mat4.c:195
void mat4_scale(mat4 *m, double x, double y, double z)
Scales a homogeneous transformation matrix.
Definition mat4.c:82
void mat4_mul(mat4 *out, const mat4 *m1, const mat4 *m2)
Multiplies two matrices (out = m1 * m2).
Definition mat4.c:35
mat4 mat4_ortho(double left, double right, double bottom, double top, double nearVal, double farVal)
Creates an orthographic projection matrix.
Definition mat4.c:209
void mat4_rotate2d(mat4 *m, double angle)
Rotates an angle, in radians, around the z axis.
Definition mat4.c:111
Header file with generic functions and naev-specifics.
#define CLAMP(a, b, x)
Definition naev.h:41
static int transformL_applyDim(lua_State *L)
Applies a transformation to a dimension.
static int transformL_scale(lua_State *L)
Applies scaling to a transform.
static int transformL_applyPoint(lua_State *L)
Applies a transformation to a point.
static int transformL_mul(lua_State *L)
Multiplies two transforms (A*B).
static int transformL_rotate2d(lua_State *L)
Applies a 2D rotation (along Z-axis) to a transform.
static int transformL_ortho(lua_State *L)
Creates an orthogonal matrix.
static int transformL_new(lua_State *L)
Creates a new identity transform.Gets a transform.
mat4 * lua_pushtransform(lua_State *L, mat4 transform)
Pushes a transform on the stack.
int lua_istransform(lua_State *L, int ind)
Checks to see if ind is a transform.
mat4 * luaL_checktransform(lua_State *L, int ind)
Gets transform at index or raises error if there is no transform at index.
static int transformL_set(lua_State *L)
Sets an element of a transform.
static int transformL_translate(lua_State *L)
Applies translation to a transform.
static int transformL_get(lua_State *L)
Gets all the values of the transform.
mat4 * lua_totransform(lua_State *L, int ind)
Lua bindings to interact with transforms.
static const luaL_Reg transformL_methods[]
static int transformL_eq(lua_State *L)
Compares two transforms to see if they are the same.
int nlua_loadTransform(nlua_env env)
Loads the transform library.
static cholmod_common C
Definition safelanes.c:95
Definition mat4.h:10