forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcelx_rotation.cpp
More file actions
278 lines (231 loc) · 6.81 KB
/
celx_rotation.cpp
File metadata and controls
278 lines (231 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// celx_rotation.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: rotation object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "celx.h"
#include "celx_internal.h"
#include "celx_vector.h"
#include <celutil/align.h>
using namespace std;
using namespace Eigen;
int rotation_new(lua_State* l, const Quaterniond& qd)
{
CelxLua celx(l);
auto q = reinterpret_cast<Quaterniond*>(lua_newuserdata(l, aligned_sizeof<Quaterniond>()));
*aligned_addr(q) = qd;
celx.setClass(Celx_Rotation);
return 1;
}
Quaterniond* to_rotation(lua_State* l, int index)
{
CelxLua celx(l);
auto q = reinterpret_cast<Quaterniond*>(celx.checkUserData(index, Celx_Rotation));
return aligned_addr(q);
}
static Quaterniond* this_rotation(lua_State* l)
{
CelxLua celx(l);
auto q = to_rotation(l, 1);
if (q == nullptr)
{
celx.doError("Bad rotation object!");
}
return q;
}
static int rotation_add(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for add");
auto q1 = to_rotation(l, 1);
auto q2 = to_rotation(l, 2);
if (q1 == nullptr || q2 == nullptr)
{
celx.doError("Addition only defined for two rotations");
}
else
{
Quaterniond result(q1->w() + q2->w(),
q1->x() + q2->x(),
q1->y() + q2->y(),
q1->z() + q2->z());
rotation_new(l, result);
}
return 1;
}
static int rotation_mult(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for multiplication");
Quaterniond* r1 = nullptr;
Quaterniond* r2 = nullptr;
lua_Number s = 0.0;
if (celx.isType(1, Celx_Rotation) && celx.isType(2, Celx_Rotation))
{
r1 = to_rotation(l, 1);
r2 = to_rotation(l, 2);
rotation_new(l, *r1 * *r2);
}
else
if (celx.isType(1, Celx_Rotation) && lua_isnumber(l, 2))
{
r1 = to_rotation(l, 1);
s = lua_tonumber(l, 2);
Quaterniond r(r1->w() * s, r1->x() * s, r1->y() * s, r1->x() * s);
rotation_new(l, r);
}
else
if (lua_isnumber(l, 1) && celx.isType(2, Celx_Rotation))
{
s = lua_tonumber(l, 1);
r1 = to_rotation(l, 2);
Quaterniond r(r1->w() * s, r1->x() * s, r1->y() * s, r1->x() * s);
rotation_new(l, r);
}
else
{
celx.doError("Bad rotation multiplication!");
}
return 1;
}
static int rotation_imag(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for rotation_imag");
auto q = this_rotation(l);
vector_new(l, q->vec());
return 1;
}
static int rotation_real(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for rotation_real");
auto q = this_rotation(l);
lua_pushnumber(l, q->w());
return 1;
}
static int rotation_transform(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected for rotation:transform()");
auto q = this_rotation(l);
auto v = to_vector(l, 2);
if (v == nullptr)
{
celx.doError("Argument to rotation:transform() must be a vector");
return 0;
}
// XXX or transpose() instead of .adjoint()?
vector_new(l, q->toRotationMatrix().adjoint() * (*v));
return 1;
}
static int rotation_setaxisangle(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Two arguments expected for rotation:setaxisangle()");
auto q = this_rotation(l);
auto v = to_vector(l, 2);
if (v == nullptr)
{
celx.doError("setaxisangle: first argument must be a vector");
return 0;
}
double angle = celx.safeGetNumber(3, AllErrors, "second argument to rotation:setaxisangle must be a number");
*q = Quaterniond(AngleAxisd(angle, v->normalized()));
return 0;
}
static int rotation_slerp(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Two arguments expected for rotation:slerp()");
auto q1 = this_rotation(l);
auto q2 = to_rotation(l, 2);
if (q2 == nullptr)
{
celx.doError("slerp: first argument must be a rotation");
return 0;
}
double t = celx.safeGetNumber(3, AllErrors, "second argument to rotation:slerp must be a number");
rotation_new(l, q1->slerp(t, *q2));
return 1;
}
static int rotation_get(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Invalid access of rotation-component");
auto q3 = this_rotation(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in rotation-access");
double value = 0.0;
if (key == "x")
value = q3->x();
else if (key == "y")
value = q3->y();
else if (key == "z")
value = q3->z();
else if (key == "w")
value = q3->w();
else
{
if (!lua_getmetatable(l, 1))
{
celx.doError("Internal error: couldn't get metatable");
return 0;
}
lua_pushvalue(l, 2);
lua_rawget(l, -2);
return 1;
}
lua_pushnumber(l, (lua_Number)value);
return 1;
}
static int rotation_set(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Invalid access of rotation-component");
auto q3 = this_rotation(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in rotation-access");
double value = celx.safeGetNumber(3, AllErrors, "Rotation components must be numbers");
Vector3d v = q3->vec();
double w = q3->w();
if (key == "x")
v.x() = value;
else if (key == "y")
v.y() = value;
else if (key == "z")
v.z() = value;
else if (key == "w")
w = value;
else
{
celx.doError("Invalid key in rotation-access");
}
*q3 = Quaterniond(w, v.x(), v.y(), v.z());
return 0;
}
static int rotation_tostring(lua_State* l)
{
CelxLua celx(l);
lua_pushstring(l, "[Rotation]");
return 1;
}
void CreateRotationMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Rotation);
celx.registerMethod("real", rotation_real);
celx.registerMethod("imag", rotation_imag);
celx.registerMethod("transform", rotation_transform);
celx.registerMethod("setaxisangle", rotation_setaxisangle);
celx.registerMethod("slerp", rotation_slerp);
celx.registerMethod("__tostring", rotation_tostring);
celx.registerMethod("__add", rotation_add);
celx.registerMethod("__mul", rotation_mult);
celx.registerMethod("__index", rotation_get);
celx.registerMethod("__newindex", rotation_set);
lua_pop(l, 1); // remove metatable from stack
}