forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcelx_position.cpp
More file actions
342 lines (265 loc) · 7.87 KB
/
celx_position.cpp
File metadata and controls
342 lines (265 loc) · 7.87 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// celx_position.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: position 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_position.h"
#include <Eigen/Geometry>
using namespace std;
using namespace Eigen;
// ==================== Position ====================
// a 128-bit per component universal coordinate
int position_new(lua_State* l, const UniversalCoord& uc)
{
CelxLua celx(l);
UniversalCoord* ud = reinterpret_cast<UniversalCoord*>(lua_newuserdata(l, sizeof(UniversalCoord)));
*ud = uc;
celx.setClass(Celx_Position);
return 1;
}
UniversalCoord* to_position(lua_State* l, int index)
{
CelxLua celx(l);
return static_cast<UniversalCoord*>(celx.checkUserData(index, Celx_Position));
}
static UniversalCoord* this_position(lua_State* l)
{
CelxLua celx(l);
UniversalCoord* uc = to_position(l, 1);
if (uc == nullptr)
{
celx.doError("Bad position object!");
}
return uc;
}
static int position_get(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Invalid access of position-component");
UniversalCoord* uc = this_position(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in position-access");
double value = 0.0;
if (key == "x")
value = uc->x;
else if (key == "y")
value = uc->y;
else if (key == "z")
value = uc->z;
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 position_set(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Invalid access of position-component");
UniversalCoord* uc = this_position(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in position-access");
double value = celx.safeGetNumber(3, AllErrors, "Position components must be numbers");
if (key == "x")
uc->x = value;
else if (key == "y")
uc->y = value;
else if (key == "z")
uc->z = value;
else
{
celx.doError("Invalid key in position-access");
}
return 0;
}
static int position_getx(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for position:getx()");
UniversalCoord* uc = this_position(l);
lua_Number x;
x = uc->x;
lua_pushnumber(l, x);
return 1;
}
static int position_gety(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for position:gety()");
UniversalCoord* uc = this_position(l);
lua_Number y;
y = uc->y;
lua_pushnumber(l, y);
return 1;
}
static int position_getz(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for position:getz()");
UniversalCoord* uc = this_position(l);
lua_Number z;
z = uc->z;
lua_pushnumber(l, z);
return 1;
}
static int position_vectorto(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected to position:vectorto");
UniversalCoord* uc = this_position(l);
UniversalCoord* uc2 = to_position(l, 2);
if (uc2 == nullptr)
{
celx.doError("Argument to position:vectorto must be a position");
return 0;
}
celx.newVector(uc2->offsetFromUly(*uc));
return 1;
}
static int position_orientationto(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Two arguments expected for position:orientationto");
UniversalCoord* src = this_position(l);
UniversalCoord* target = to_position(l, 2);
if (target == nullptr)
{
celx.doError("First argument to position:orientationto must be a position");
return 1;
}
auto upd = celx.toVector(3);
if (upd == nullptr)
{
celx.doError("Second argument to position:orientationto must be a vector");
return 1;
}
Vector3d src2target = target->offsetFromKm(*src).normalized();
Vector3d v = src2target.cross(*upd).normalized();
Vector3d u = v.cross(src2target);
Matrix3d m;
m.row(0) = v; m.row(1) = u; m.row(2) = src2target * (-1);
Quaterniond qd(m);
celx.newRotation(qd);
return 1;
}
static int position_tostring(lua_State* l)
{
// TODO: print out the coordinate as it would appear in a cel:// URL
lua_pushstring(l, "[Position]");
return 1;
}
static int position_distanceto(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected to position:distanceto()");
UniversalCoord* uc = this_position(l);
UniversalCoord* uc2 = to_position(l, 2);
if (uc2 == nullptr)
{
celx.doError("Position expected as argument to position:distanceto");
return 0;
}
lua_pushnumber(l, uc2->offsetFromKm(*uc).norm());
return 1;
}
static int position_add(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for addition");
UniversalCoord* p1 = nullptr;
UniversalCoord* p2 = nullptr;
Vector3d* v2 = nullptr;
if (celx.isType(1, Celx_Position) && celx.isType(2, Celx_Position))
{
p1 = celx.toPosition(1);
p2 = celx.toPosition(2);
// this is not very intuitive, as p1-p2 is a vector
celx.newPosition(*p1 + *p2);
}
else
if (celx.isType(1, Celx_Position) && celx.isType(2, Celx_Vec3))
{
p1 = celx.toPosition(1);
v2 = celx.toVector(2);
celx.newPosition(p1->offsetUly(*v2));
}
else
{
celx.doError("Bad position addition!");
}
return 1;
}
static int position_sub(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for subtraction");
UniversalCoord* p1 = nullptr;
UniversalCoord* p2 = nullptr;
Vector3d* v2 = nullptr;
if (celx.isType(1, Celx_Position) && celx.isType(2, Celx_Position))
{
p1 = celx.toPosition(1);
p2 = celx.toPosition(2);
celx.newVector(p1->offsetFromUly(*p2));
}
else
if (celx.isType(1, Celx_Position) && celx.isType(2, Celx_Vec3))
{
p1 = celx.toPosition(1);
v2 = celx.toVector(2);
celx.newPosition(p1->offsetUly(*v2));
}
else
{
celx.doError("Bad position subtraction!");
}
return 1;
}
static int position_addvector(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected to position:addvector()");
UniversalCoord* uc = this_position(l);
if (uc == nullptr)
return 0;
auto v3d = celx.toVector(2);
if (v3d == nullptr)
{
celx.doError("Vector expected as argument to position:addvector");
return 0;
}
UniversalCoord ucnew = uc->offsetUly(*v3d);
position_new(l, ucnew);
return 1;
}
void CreatePositionMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Position);
celx.registerMethod("__tostring", position_tostring);
celx.registerMethod("distanceto", position_distanceto);
celx.registerMethod("vectorto", position_vectorto);
celx.registerMethod("orientationto", position_orientationto);
celx.registerMethod("addvector", position_addvector);
celx.registerMethod("__add", position_add);
celx.registerMethod("__sub", position_sub);
celx.registerMethod("__index", position_get);
celx.registerMethod("__newindex", position_set);
celx.registerMethod("getx", position_getx);
celx.registerMethod("gety", position_gety);
celx.registerMethod("getz", position_getz);
lua_pop(l, 1); // remove metatable from stack
}