/
orbits.py
274 lines (233 loc) · 9.46 KB
/
orbits.py
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
#
#This file is part of Cosmonium.
#
#Copyright (C) 2018-2019 Laurent Deru.
#
#Cosmonium 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 3 of the License, or
#(at your option) any later version.
#
#Cosmonium is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with Cosmonium. If not, see <https://www.gnu.org/licenses/>.
#
from __future__ import print_function
from __future__ import absolute_import
from panda3d.core import LPoint3d, LVector3d, LQuaterniond
from . import units
from .frame import J2000EclipticReferenceFrame, J2000EquatorialReferenceFrame
from .kepler import kepler_pos
from .astro import calc_orientation
from math import pi, asin, atan2
class Orbit(object):
dynamic = False
def __init__(self, frame=None):
if frame is None:
frame = J2000EclipticReferenceFrame()
self.frame = frame
self.origin = LPoint3d()
def set_frame(self, frame):
self.frame = frame
def is_periodic(self):
return False
def get_period(self):
return 0.0
def get_mean_motion(self):
return 0.0
def get_global_position_at(self, time):
return self.origin
def get_frame_position_at(self, time):
return None
def get_frame_rotation_at(self, time):
return None
def get_position_at(self, time):
return self.frame.get_local_position(self.get_frame_rotation_at(time).xform(self.get_frame_position_at(time)))
def get_rotation_at(self, time):
return self.frame.get_abs_orientation(self.get_frame_rotation_at(time))
def project(self, time, center, radius):
return None
def get_apparent_radius(self):
return 0.0
class FixedPosition(Orbit):
#TODO: Rename into something like GlobalFixedPosition
def __init__(self, position=None,
right_asc=0.0, right_asc_unit=units.Deg,
declination=0.0, declination_unit=units.Deg,
distance=0.0, distance_unit=units.Ly,
frame=None):
Orbit.__init__(self, frame)
if position is None:
self.right_asc = right_asc * right_asc_unit
self.declination = declination * declination_unit
distance = distance * distance_unit
else:
self.right_asc = None
self.declination = None
if not isinstance(position, LPoint3d):
position = LPoint3d(*position)
if position is None:
self.orientation = calc_orientation(self.right_asc, self.declination)
position = self.orientation.xform(LVector3d(0, 0, distance))
self.global_position = position
self.position=LPoint3d()
self.rotation=LQuaterniond()
def calc_asc_decl(self):
distance = self.global_position.length()
if distance > 0:
position = J2000EquatorialReferenceFrame.orientation.conjugate().xform(self.global_position)
self.declination = asin(position[2] / distance)
self.right_asc = atan2(position[1], position[0])
else:
self.right_asc = 0.0
self.declination = 0.0
def get_right_asc(self):
if self.right_asc is None:
self.calc_asc_decl()
return self.right_asc
def get_declination(self):
if self.declination is None:
self.calc_asc_decl()
return self.declination
def project(self, time, center, radius):
vector = self.global_position - center
vector /= vector.length()
vector *= radius
return vector
def get_global_position_at(self, time):
return self.global_position
def get_frame_position_at(self, time):
return self.position
def get_frame_rotation_at(self, time):
return self.rotation
class InfinitePosition(Orbit):
def __init__(self,
right_asc=0.0, right_asc_unit=units.Deg,
declination=0.0, declination_unit=units.Deg,
frame=None):
Orbit.__init__(self, frame)
self.right_asc = right_asc * right_asc_unit
self.declination = declination * declination_unit
self.orientation = calc_orientation(self.right_asc, self.declination)
self.position=LPoint3d()
self.rotation=LQuaterniond()
def project(self, time, center, radius):
return self.orientation.xform(LVector3d(0, 0, radius))
def get_frame_position_at(self, time):
return self.position
def get_frame_rotation_at(self, time):
return self.rotation
class FixedOrbit(Orbit):
def __init__(self, position=LPoint3d(0, 0, 0), rotation=LQuaterniond(), frame=None):
Orbit.__init__(self, frame)
self.position = position
self.rotation = rotation
def get_frame_position_at(self, time):
return self.position
def get_frame_rotation_at(self, time):
return self.rotation
class EllipticalOrbit(Orbit):
dynamic = True
def __init__(self,
pericenter_distance,
period,
eccentricity,
inclination,
ascending_node,
arg_of_periapsis,
mean_anomaly,
epoch,
frame):
Orbit.__init__(self, frame)
self.pericenter_distance = pericenter_distance
self.apocenter_distance = pericenter_distance * (1.0 + eccentricity) / (1.0 - eccentricity)
self.period = period
self.mean_motion = 2 * pi / period
self.eccentricity = eccentricity
self.inclination = inclination * pi / 180
self.ascending_node = ascending_node * pi / 180
self.arg_of_periapsis = arg_of_periapsis * pi / 180
self.mean_anomaly = mean_anomaly * pi / 180
self.epoch = epoch
inclination_quat = LQuaterniond()
inclination_quat.setFromAxisAngleRad(self.inclination, LVector3d.unitX())
arg_of_periapsis_quat = LQuaterniond()
arg_of_periapsis_quat.setFromAxisAngleRad(self.arg_of_periapsis, LVector3d.unitZ())
ascending_node_quat = LQuaterniond()
ascending_node_quat.setFromAxisAngleRad(self.ascending_node, LVector3d.unitZ())
self.rotation = arg_of_periapsis_quat * inclination_quat * ascending_node_quat
def is_periodic(self):
return self.eccentricity < 1.0
def get_period(self):
return self.period
def get_mean_motion(self):
return self.mean_motion
def get_time_of_perihelion(self):
return self.epoch - self.mean_anomaly / self.mean_motion
def get_apparent_radius(self):
return abs(self.apocenter_distance)
def get_frame_position_at(self, time):
mean_anomaly = (time - self.epoch) * self.mean_motion + self.mean_anomaly
return kepler_pos(self.pericenter_distance, self.eccentricity, mean_anomaly)
def get_frame_rotation_at(self, time):
return self.rotation
def create_elliptical_orbit(semi_major_axis=None,
semi_major_axis_units=units.AU,
pericenter_distance=None,
pericenter_distance_units=units.AU,
mean_motion=None,
mean_motion_units=units.Deg_Per_Day,
period=None,
period_units=units.JYear,
eccentricity=0.0,
inclination=0,
ascending_node=0.0,
arg_of_periapsis=None,
long_of_pericenter=None,
mean_anomaly=None,
time_of_perihelion=None,
mean_longitude=0.0,
epoch = units.J2000,
frame=None):
if pericenter_distance is None:
if semi_major_axis is None:
#TODO: raise error
pericenter_distance = 1
else:
pericenter_distance = semi_major_axis * semi_major_axis_units * (1.0 - eccentricity)
else:
pericenter_distance = pericenter_distance * pericenter_distance_units
if period is None:
if mean_motion is None:
#TODO: raise error
period = 0.0
else:
period = 2 * pi / (mean_motion * mean_motion_units)
else:
period = period * period_units
if arg_of_periapsis is None:
if long_of_pericenter is None:
arg_of_periapsis = 0.0
else:
arg_of_periapsis = long_of_pericenter - ascending_node
if inclination == 0.0:
#Ascending node is undefined if there is no inclination
ascending_node = 0.0
if mean_anomaly is None:
if mean_longitude is None:
mean_anomaly = (epoch - time_of_perihelion) / period * 2 * pi
else:
mean_anomaly = mean_longitude - (arg_of_periapsis + ascending_node)
return EllipticalOrbit(pericenter_distance,
period,
eccentricity,
inclination,
ascending_node,
arg_of_periapsis,
mean_anomaly,
epoch,
frame)