-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_util.ks
141 lines (107 loc) · 2.66 KB
/
lib_util.ks
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
@LAZYGLOBAL OFF.
@CLOBBERBUILTINS OFF.
function rotxy {
parameter vec.
parameter ang.
local sang to sin(ang).
local cang to cos(ang).
return V(
vec:x * cang - vec:z * sang,
vec:y,
vec:x * sang + vec:z * cang
).
}
function toinertial {
parameter vec.
local ang to vang(SOLARPRIMEVECTOR, V(1, 0, 0)).
return rotxy(vec, ang).
}
function frominertial {
parameter vec.
local ang to vang(SOLARPRIMEVECTOR, V(1, 0, 0)).
return rotxy(vec, -ang).
}
function swizzle {
parameter vec.
return V(
vec:x,
vec:z,
vec:y
).
}
// clamps to [0,360)
function mod360 {
parameter x.
local x is mod(x, 360).
if x >= 0 {
return x.
}
return 360 + x.
}
// clamps to (-180,180]
function mod180 {
parameter x.
local x is mod360(x).
if x <= 180 { return x. }
return x - 360.
}
function clamp {
parameter x.
parameter xmin.
parameter xmax.
if x < xmin { return xmin. }.
if x > xmax { return xmax. }.
return x.
}
function safe_arcsin {
parameter x.
return arcsin(clamp(x, -1.0, 1.0)).
}
function safe_arccos {
parameter x.
return arccos(clamp(x, -1.0, 1.0)).
}
function angle_for_inclination {
parameter inc. // inclination in degrees.
parameter lat. // latitude of position.
local cosAngle is cos(inc) / cos(lat).
if (abs(cosAngle) > 1.0)
{
// for impossible inclinations return due east or west
if abs(mod180(inc)) < 90 {
return 0.
} else {
return 180.
}
}
local angle is safe_arccos(cosAngle).
// negative inclinations are conventionally south-going
if inc < 0 { local angle is -angle. }.
return mod360(90 - angle).
}
function heading_for_inclination {
parameter inc. // inclination in degrees.
parameter rv. // position in left-handed frame.
local lat is safe_arcsin(rv:x / r:mag).
return angle_for_inclination(inc, lat).
}
function horizontal_velocity_for_inclination {
parameter inc. // inclination in degrees.
parameter rv. // position in left-handed frame.
parameter vmag. // magnitude of constructed vector.
local lat is safe_arcsin(rv:y / rv:mag).
local lng is arctan2(rv:z, rv:x). // not real lng
local angle is angle_for_inclination(inc, lat).
local eval is sin(angle).
local nval is cos(angle).
local slat is sin(lat).
local slng is sin(lng).
local clat is cos(lat).
local clng is cos(lng).
local vec is V(
-slng * eval + (-slat * clng) * nval,
clat * nval,
clng * eval + (-slat * slng) * nval
).
return vec * vmag.
}