forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcelx_phase.cpp
More file actions
220 lines (169 loc) · 5.2 KB
/
celx_phase.cpp
File metadata and controls
220 lines (169 loc) · 5.2 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
// celx_phase.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: phase 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 <assert.h>
#include "celx.h"
#include "celx_internal.h"
#include "celx_phase.h"
#include <celengine/timelinephase.h>
#include <celephem/orbit.h>
#include <celephem/rotation.h>
using namespace std;
int phase_new(lua_State* l, const TimelinePhase::SharedConstPtr& phase)
{
CelxLua celx(l);
// Use placement new to put the new phase reference in the userdata block.
void* block = lua_newuserdata(l, sizeof(TimelinePhase::SharedConstPtr));
new (block) TimelinePhase::SharedConstPtr(phase);
celx.setClass(Celx_Phase);
return 1;
}
static TimelinePhase::SharedConstPtr* to_phase(lua_State* l, int index)
{
CelxLua celx(l);
return celx.safeGetClass<TimelinePhase::SharedConstPtr>(index);
}
static const TimelinePhase::SharedConstPtr& this_phase(lua_State* l)
{
CelxLua celx(l);
auto phase = to_phase(l, 1);
assert(phase != nullptr);
if (phase == nullptr)
{
celx.doError("Bad phase object!");
}
return *phase;
}
/*! phase:timespan()
*
* Return the start and end times for this timeline phase.
*
* \verbatim
* -- Example: retrieve the start and end times of the first phase
* -- of Cassini's timeline:
* --
* cassini = celestia:find("Sol/Cassini")
* phases = cassini:timeline()
* begintime, endtime = phases[1]:timespan()
*
* \endverbatim
*/
static int phase_timespan(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments allowed for to phase:timespan");
auto phase = this_phase(l);
celx.push(phase->startTime(), phase->endTime());
//lua_pushnumber(l, phase->startTime());
//lua_pushnumber(l, phase->endTime());
return 2;
}
/*! frame phase:orbitframe()
*
* Return the orbit frame for this timeline phase.
*/
static int phase_orbitframe(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments allowed for to phase:orbitframe");
auto phase = this_phase(l);
auto f = phase->orbitFrame();
celx.newFrame(ObserverFrame(f));
return 1;
}
/*! frame phase:bodyframe()
*
* Return the body frame for this timeline phase.
*/
static int phase_bodyframe(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments allowed for to phase:bodyframe");
auto phase = this_phase(l);
auto f = phase->bodyFrame();
celx.newFrame(ObserverFrame(f));
return 1;
}
/*! position phase:getposition(time: t)
*
* Return the position in frame coordinates at the specified time.
* Times outside the span covered by the phase are automatically clamped
* to either the beginning or ending of the span.
*/
static int phase_getposition(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for phase:getposition");
auto phase = this_phase(l);
double tdb = celx.safeGetNumber(2, WrongType, "Argument to phase:getposition() must be number", 0.0);
if (tdb < phase->startTime())
tdb = phase->startTime();
else if (tdb > phase->endTime())
tdb = phase->endTime();
celx.newPosition(UniversalCoord(phase->orbit()->positionAtTime(tdb) * astro::kilometersToMicroLightYears(1.0)));
return 1;
}
/*! rotation phase:getorientation(time: t)
*
* Return the orientation in frame coordinates at the specified time.
* Times outside the span covered by the phase are automatically clamped
* to either the beginning or ending of the span.
*/
static int phase_getorientation(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for phase:getorientation");
auto phase = this_phase(l);
double tdb = celx.safeGetNumber(2, WrongType, "Argument to phase:getorientation() must be number", 0.0);
if (tdb < phase->startTime())
tdb = phase->startTime();
else if (tdb > phase->endTime())
tdb = phase->endTime();
celx.newRotation(phase->rotationModel()->orientationAtTime(tdb));
return 1;
}
/*! __tostring metamethod
* Convert a phase to a string (currently just "[Phase]")
*/
static int phase_tostring(lua_State* l)
{
lua_pushstring(l, "[Phase]");
return 1;
}
/*! __gc metamethod
* Garbage collection for phases.
*/
static int phase_gc(lua_State* l)
{
CelxLua celx(l);
auto ref = this_phase(l);
if (ref)
{
ref.reset();
}
else
{
celx.doError("Bad phase object during garbage collection!");
}
return 0;
}
void CreatePhaseMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Phase);
celx.registerMethod("__tostring", phase_tostring);
celx.registerMethod("__gc", phase_gc);
celx.registerMethod("timespan", phase_timespan);
celx.registerMethod("orbitframe", phase_orbitframe);
celx.registerMethod("bodyframe", phase_bodyframe);
celx.registerMethod("getposition", phase_getposition);
celx.registerMethod("getorientation", phase_getorientation);
lua_pop(l, 1); // remove metatable from stack
}