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
use crate::angle::{Angle, PI, PI_FOURTH, PI_HALF};
use crate::time::GMST;
pub trait ConstrainedAngle {
fn new(angle: &Angle) -> Self;
fn value(&self) -> Angle;
}
#[derive(Debug, Copy, Clone)]
pub struct ZenithAngle(Angle);
impl ConstrainedAngle for ZenithAngle {
fn new(angle: &Angle) -> Self {
let rad = angle.to_rad();
if !(0.0..=PI).contains(&rad) {
panic!("ZenithAngle must be between 0 and pi!")
}
Self(*angle)
}
fn value(&self) -> Angle {
self.0
}
}
#[derive(Debug, Copy, Clone)]
pub struct Declination(pub Angle);
impl ConstrainedAngle for Declination {
fn new(angle: &Angle) -> Self {
let rad = angle.to_rad();
if !(-PI_HALF..=PI_HALF).contains(&rad) {
panic!("Declination must be between -pi/2 and pi/2!")
}
Self(*angle)
}
fn value(&self) -> Angle {
self.0
}
}
#[derive(Debug, Copy, Clone)]
pub struct Altitude(pub Angle);
impl ConstrainedAngle for Altitude {
fn new(angle: &Angle) -> Self {
let rad = angle.to_rad();
if !(-PI_HALF..=PI_HALF).contains(&rad) {
panic!("Altitude must be between -pi/2 and pi/2!")
}
Self(*angle)
}
fn value(&self) -> Angle {
self.0
}
}
#[derive(Debug, Copy, Clone)]
pub struct Latitude(pub Angle);
impl ConstrainedAngle for Latitude {
fn new(angle: &Angle) -> Self {
let rad = angle.to_rad();
if !(-PI_HALF..=PI_HALF).contains(&rad) {
panic!("Latitude must be between -pi/2 and pi/2!")
}
Self(*angle)
}
fn value(&self) -> Angle {
self.0
}
}
#[derive(Debug, Copy, Clone)]
pub struct RightAscension(pub Angle);
#[derive(Debug, Copy, Clone)]
pub struct Azimuth(pub Angle);
#[derive(Debug, Copy, Clone)]
pub struct Longitude(pub Angle);
#[derive(Debug, Copy, Clone)]
#[allow(dead_code)]
pub struct Cartesian {
pub x: f64,
pub y: f64,
pub z: f64,
}
#[derive(Debug, Copy, Clone)]
pub struct Geographic {
pub latitude: Latitude,
pub longitude: Longitude,
}
#[derive(Debug, Copy, Clone)]
#[allow(dead_code)]
pub struct Polar {
pub radius: f64,
pub angle: Angle,
}
#[derive(Debug, Copy, Clone)]
pub struct Equitorial {
pub right_ascension: RightAscension,
pub declination: Declination,
}
#[derive(Debug, Copy, Clone)]
pub struct Horizontal {
pub altitude: Altitude,
pub azimuth: Azimuth,
}
impl Horizontal {
pub fn from_equitorial(eq: &Equitorial, geo: &Geographic, sidereal_time: &GMST) -> Self {
let hour_local: Angle = sidereal_time.0 + geo.longitude.0 - eq.right_ascension.0;
let x_horiz: f64 = -(geo.latitude.0.sin()) * (eq.declination.0.cos()) * (hour_local.cos())
+ geo.latitude.0.cos() * (eq.declination.0.sin());
let y_horiz: f64 = eq.declination.0.cos() * hour_local.sin();
let azimuth_rad: Angle = Angle::Radian(-(y_horiz.atan2(x_horiz)));
let altitude_rad: Angle = Angle::Radian(
(geo.latitude.0.sin() * eq.declination.0.sin()
+ geo.latitude.0.cos() * eq.declination.0.cos() * hour_local.cos())
.asin(),
);
Self {
altitude: Altitude(azimuth_rad),
azimuth: Azimuth(altitude_rad),
}
}
pub fn stereo_project(&self) -> Polar {
Polar {
radius: 2.0 * (PI_FOURTH - self.altitude.0.to_rad() / 2.0).tan(),
angle: self.azimuth.0,
}
}
}