-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathresource.py
179 lines (131 loc) · 5.94 KB
/
resource.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
import math
from body_class import *
class resource:
def __init__(self, name, value, equation, variable, obj1, obj2, coeffs, limits):
self.name = name
self.value = value
self.equation = equation
self.variable = variable
self.obj1 = obj1
self.obj2 = obj2
self.coeffs = coeffs
self.limits = limits
self.occultation = 0
self.last_delta = 0
def get_name(self):
return self.name
def update_occultation(self, bodies):
s = 0
if type(self.obj2) is body:
a = self.obj2.get_angular_radius_from(self.obj1)
for occulting_body in bodies:
if (not occulting_body == self.obj2) and self.obj1.get_dist_to(self.obj2) > occulting_body.get_dist_to(self.obj2):
b = occulting_body.get_angular_radius_from(self.obj1)
vec_to_illum_body = self.obj1.get_unit_vector_towards(self.obj2)
vec_to_occult_body = self.obj1.get_unit_vector_towards(occulting_body)
c_numerator = vec_to_illum_body.dot(vec_to_occult_body)
c_denominator = vec_to_illum_body.mag() * vec_to_occult_body.mag()
eq_c = math.acos(c_numerator / c_denominator)
# body is covering the star entirely
if b - eq_c > a:
c_s = 1
# the body-star separation is too large, no shadow
elif eq_c > b + a:
c_s = 0
else:
eq_x = (eq_c**2 + a**2 - b**2)/(2*eq_c)
eq_y = (a**2 - eq_x**2)**(0.5)
A = a**2 * math.acos(eq_x/a) + b**2 * math.acos((eq_c-eq_x)/b) - eq_c * eq_y
c_s = A/(math.pi * a**2)
if c_s > s:
s = c_s
else:
a = 0
for occulting_body in bodies:
if self.obj1.get_dist_to(self.obj2) > occulting_body.get_dist_to(self.obj2):
b = occulting_body.get_angular_radius_from(self.obj1)
vec_to_illum_body = self.obj1.get_unit_vector_towards(self.obj2)
vec_to_occult_body = self.obj1.get_unit_vector_towards(occulting_body)
c_numerator = vec_to_illum_body.dot(vec_to_occult_body)
c_denominator = vec_to_illum_body.mag() * vec_to_occult_body.mag()
eq_c = math.acos(c_numerator / c_denominator)
# body is covering the star entirely
if b - eq_c > a:
c_s = 1
# the body-star separation is too large, no shadow
elif eq_c > b + a:
c_s = 0
else:
eq_x = (eq_c ** 2 + a ** 2 - b ** 2) / (2 * eq_c)
eq_y = (a ** 2 - eq_x ** 2) ** (0.5)
A = a ** 2 * math.acos(eq_x / a) + b ** 2 * math.acos((eq_c - eq_x) / b) - eq_c * eq_y
c_s = A / (math.pi * a ** 2)
if c_s > s:
s = c_s
self.occultation = max(min(s, 1), 0)
def get_variable(self):
if self.variable == "dist":
return self.obj1.get_dist_to(self.obj2)
elif self.variable == "occultation":
return self.occultation
elif self.variable == "dist_occultation":
return self.obj1.get_dist_to(self.obj2)
elif self.variable == "alt":
return self.obj1.get_alt_above(self.obj2)
elif self.variable == "vel" or self.variable == "vel_mag":
return self.obj1.get_vel_mag_rel_to(self.obj2)
elif self.variable == "grav_accel":
return self.obj1.get_gravity_mag_by(self.obj2)
elif self.variable == "dummy":
return 0
def set_value(self, x):
self.value = x
def update_value(self, dt):
y = 1
if self.variable.endswith("_occultation"):
y = (1 - self.occultation)
x = self.get_variable()
last_val = self.value
# = = = INCREMENTAL EQUATIONS = = =
# y += a
# y += ax + b
# y += ax^2 + bx + c
# and so on...
if self.equation == "polynomial":
csum = 0
for i in range(len(self.coeffs)):
csum += self.coeffs[i] * x**(len(self.coeffs) - 1 - i) * y
self.value += csum * dt
# y += a * log_b(x) + c
elif self.equation == "logarithmic":
if not x <= 0:
self.value += (self.coeffs[0] * math.log(x, self.coeffs[1]) * y + self.coeffs[2]) * dt
else:
self.value = float("-inf")
# y += a * x^b + c
elif self.equation == "power":
if not (x == 0 and self.coeffs[1] < 0):
self.value += (self.coeffs[0] * x**self.coeffs[1] * y + self.coeffs[2]) * dt
else:
self.value += self.coeffs[2] * dt
# = = = ABSOLUTE EQUATIONS = = =
elif self.equation == "polynomial_abs":
csum = 0
for i in range(len(self.coeffs)):
csum += self.coeffs[i] * x ** (len(self.coeffs) - 1 - i)
self.value = csum * y
# y += a * log_b(x) + c
elif self.equation == "logarithmic_abs":
if not x <= 0:
self.value = (self.coeffs[0] * math.log(x, self.coeffs[1]) * y + self.coeffs[2])
else:
self.value = float("-inf")
# y += a * x^b + c
elif self.equation == "power_abs":
if not (x == 0 and self.coeffs[1] < 0):
self.value = (self.coeffs[0] * x ** self.coeffs[1] * y + self.coeffs[2])
else:
self.value = self.coeffs[2]
if self.limits:
self.value = max(min(self.value, self.limits[1]), self.limits[0])
self.last_delta = (self.value - last_val) / dt