1use std::f64::consts::PI;
9
10use crate::pyfloat;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[non_exhaustive]
15pub enum HouseSystem {
16 Placidus,
18 Koch,
20 Regiomontanus,
22 Campanus,
24 Topocentric,
26 Alcabitius,
28 Morinus,
30 Porphyry,
32 Equal,
34 Vehlow,
36 WholeSign,
38 Sripati,
40}
41
42impl HouseSystem {
43 pub fn from_code(code: &str) -> Self {
46 match code.chars().next().map(|c| c.to_ascii_uppercase()) {
47 Some('K') => HouseSystem::Koch,
48 Some('R') => HouseSystem::Regiomontanus,
49 Some('C') => HouseSystem::Campanus,
50 Some('T') => HouseSystem::Topocentric,
51 Some('B') => HouseSystem::Alcabitius,
52 Some('M') => HouseSystem::Morinus,
53 Some('O') => HouseSystem::Porphyry,
54 Some('E') => HouseSystem::Equal,
55 Some('V') => HouseSystem::Vehlow,
56 Some('W') => HouseSystem::WholeSign,
57 Some('S') => HouseSystem::Sripati,
58 _ => HouseSystem::Placidus,
59 }
60 }
61}
62
63#[derive(Debug, Clone, PartialEq)]
65pub struct Houses {
66 pub cusps: [f64; 12],
68 pub ascendant: f64,
70 pub midheaven: f64,
72}
73
74pub fn houses_at_sidereal_time(
78 jd: f64,
79 gast_hours: f64,
80 lat: f64,
81 lon: f64,
82 system: HouseSystem,
83 shift: f64,
84) -> Houses {
85 let ramc = pyfloat::rem(pyfloat::rem(gast_hours * 15.0 + lon, 360.0) + 360.0, 360.0);
86 let ramc_r = ramc.to_radians();
87 let lat_r = lat.to_radians();
88
89 let t_cent = (jd - 2_451_545.0) / 36525.0;
91 let eps_deg = 23.4392911 - 0.0130042 * t_cent - 0.00000016 * t_cent.powf(2.0)
92 + 0.000000504 * t_cent.powf(3.0);
93 let eps_r = eps_deg.to_radians();
94
95 let mc_deg = pyfloat::rem(
96 ramc_r.sin().atan2(ramc_r.cos() * eps_r.cos()).to_degrees(),
97 360.0,
98 );
99 let y = ramc_r.cos();
100 let x = -ramc_r.sin() * eps_r.cos() - lat_r.tan() * eps_r.sin();
101 let asc_deg = pyfloat::rem(y.atan2(x).to_degrees(), 360.0);
102
103 let mut c = [0.0_f64; 13]; match system {
105 HouseSystem::WholeSign => {
106 let asc_sign = pyfloat::floordiv(asc_deg, 30.0);
107 for (i, cusp) in c.iter_mut().enumerate().skip(1) {
108 *cusp = pyfloat::rem((asc_sign + i as f64 - 1.0) * 30.0, 360.0);
109 }
110 }
111 HouseSystem::Equal => {
112 for (i, cusp) in c.iter_mut().enumerate().skip(1) {
113 *cusp = pyfloat::rem(asc_deg + (i as f64 - 1.0) * 30.0, 360.0);
114 }
115 }
116 HouseSystem::Vehlow => {
117 for (i, cusp) in c.iter_mut().enumerate().skip(1) {
119 *cusp = pyfloat::rem(asc_deg - 15.0 + (i as f64 - 1.0) * 30.0 + 360.0, 360.0);
120 }
121 }
122 HouseSystem::Morinus => {
123 for i in 1..=12 {
126 let ra = (ramc + 30.0 * i as f64).to_radians();
127 c[(i + 9) % 12 + 1] = norm((ra.sin() * eps_r.cos()).atan2(ra.cos()).to_degrees());
128 }
129 }
130 HouseSystem::Koch if lat.abs() >= 90.0 - eps_deg => {
131 porphyry(&mut c, asc_deg, mc_deg);
133 }
134 HouseSystem::Koch
135 | HouseSystem::Regiomontanus
136 | HouseSystem::Campanus
137 | HouseSystem::Topocentric
138 | HouseSystem::Alcabitius => {
139 set_angles(&mut c, asc_deg, mc_deg);
140 let circles: [(f64, f64); 4] = match system {
143 HouseSystem::Koch => {
144 let sin_a =
145 (mc_deg.to_radians().sin() * eps_r.sin() / lat_r.cos()).clamp(-1.0, 1.0);
146 let cos_a = (1.0 - sin_a * sin_a).sqrt();
147 let pole = (lat_r.tan() / cos_a).atan();
148 let ad3 = (pole.sin() * sin_a).asin().to_degrees() / 3.0;
149 [
150 (ramc + 30.0 - 2.0 * ad3, lat),
151 (ramc + 60.0 - ad3, lat),
152 (ramc + 120.0 + ad3, lat),
153 (ramc + 150.0 + 2.0 * ad3, lat),
154 ]
155 }
156 HouseSystem::Regiomontanus | HouseSystem::Topocentric => {
157 let (k1, k2) = if system == HouseSystem::Regiomontanus {
158 (0.5, 30.0_f64.to_radians().cos())
159 } else {
160 (1.0 / 3.0, 2.0 / 3.0)
161 };
162 let p1 = (lat_r.tan() * k1).atan().to_degrees();
163 let p2 = (lat_r.tan() * k2).atan().to_degrees();
164 [
165 (ramc + 30.0, p1),
166 (ramc + 60.0, p2),
167 (ramc + 120.0, p2),
168 (ramc + 150.0, p1),
169 ]
170 }
171 HouseSystem::Campanus => {
172 let p1 = (lat_r.sin() / 2.0).asin().to_degrees();
173 let p2 = (3.0_f64.sqrt() / 2.0 * lat_r.sin()).asin().to_degrees();
174 let x1 = (3.0_f64.sqrt() / lat_r.cos()).atan().to_degrees();
175 let x2 = (1.0 / 3.0_f64.sqrt() / lat_r.cos()).atan().to_degrees();
176 [
177 (ramc + 90.0 - x1, p1),
178 (ramc + 90.0 - x2, p2),
179 (ramc + 90.0 + x2, p2),
180 (ramc + 90.0 + x1, p1),
181 ]
182 }
183 _ => {
184 let dec = (asc_deg.to_radians().sin() * eps_r.sin()).asin();
187 let sda = (-lat_r.tan() * dec.tan())
188 .clamp(-1.0, 1.0)
189 .acos()
190 .to_degrees();
191 let sna = 180.0 - sda;
192 [
193 (ramc + sda / 3.0, 0.0),
194 (ramc + 2.0 * sda / 3.0, 0.0),
195 (ramc + 180.0 - 2.0 * sna / 3.0, 0.0),
196 (ramc + 180.0 - sna / 3.0, 0.0),
197 ]
198 }
199 };
200 for (house, (ra, pole)) in [11, 12, 2, 3].into_iter().zip(circles) {
201 c[house] = ecliptic_intersection(ra, pole, eps_r);
202 }
203 opposite_cusps(&mut c);
204 }
205 HouseSystem::Porphyry => porphyry(&mut c, asc_deg, mc_deg),
206 HouseSystem::Sripati => sripati(&mut c, asc_deg, mc_deg),
207 HouseSystem::Placidus => {
208 set_angles(&mut c, asc_deg, mc_deg);
209 let solve = |offset: f64, semi_factor: f64| {
210 let target_ra = pyfloat::rem(ramc + offset, 360.0).to_radians();
211 let mut lon = pyfloat::rem(ramc + offset, 360.0);
212 for _ in 0..25 {
213 let l_r = lon.to_radians();
214 let sin_d = eps_r.sin() * l_r.sin();
215 let decl = sin_d.clamp(-1.0, 1.0).asin();
216 let ra = pyfloat::rem((eps_r.cos() * l_r.sin()).atan2(l_r.cos()), 2.0 * PI);
217 let tan_d = decl.tan() * lat_r.tan();
218 if tan_d.abs() >= 1.0 {
219 break;
220 }
221 let ad = tan_d.asin();
222 let diff = (ra - target_ra) - semi_factor * ad;
223 let diff = pyfloat::rem(diff + PI, 2.0 * PI) - PI;
224 if diff.abs() < 1e-8 {
225 break;
226 }
227 lon = pyfloat::rem(lon - diff.to_degrees(), 360.0);
228 }
229 lon
230 };
231 c[11] = solve(30.0, 1.0 / 3.0);
232 c[12] = solve(60.0, 2.0 / 3.0);
233 c[9] = solve(-30.0, -1.0 / 3.0);
234 c[8] = solve(-60.0, -2.0 / 3.0);
235 c[5] = pyfloat::rem(c[11] + 180.0, 360.0);
236 c[6] = pyfloat::rem(c[12] + 180.0, 360.0);
237 c[2] = pyfloat::rem(c[8] + 180.0, 360.0);
238 c[3] = pyfloat::rem(c[9] + 180.0, 360.0);
239 }
240 }
241
242 let mut cusps = [0.0; 12];
243 for i in 0..12 {
244 cusps[i] = pyfloat::rem(c[i + 1] - shift, 360.0);
245 }
246 Houses {
247 cusps,
248 ascendant: pyfloat::rem(asc_deg - shift, 360.0),
249 midheaven: pyfloat::rem(mc_deg - shift, 360.0),
250 }
251}
252
253fn norm(deg: f64) -> f64 {
254 pyfloat::rem(pyfloat::rem(deg, 360.0) + 360.0, 360.0)
255}
256
257fn ecliptic_intersection(ra: f64, pole: f64, eps_r: f64) -> f64 {
261 let ra = ra.to_radians();
262 let x = ra.cos() * eps_r.cos() - pole.to_radians().tan() * eps_r.sin();
263 norm(ra.sin().atan2(x).to_degrees())
264}
265
266fn porphyry(c: &mut [f64; 13], asc_deg: f64, mc_deg: f64) {
268 set_angles(c, asc_deg, mc_deg);
269 let q1 = pyfloat::rem(asc_deg - mc_deg, 360.0);
270 c[11] = pyfloat::rem(mc_deg + q1 / 3.0, 360.0);
271 c[12] = pyfloat::rem(mc_deg + (2.0 * q1) / 3.0, 360.0);
272 let q2 = pyfloat::rem(c[4] - asc_deg, 360.0);
273 c[2] = pyfloat::rem(asc_deg + q2 / 3.0, 360.0);
274 c[3] = pyfloat::rem(asc_deg + (2.0 * q2) / 3.0, 360.0);
275 opposite_cusps(c);
276}
277
278fn sripati(c: &mut [f64; 13], asc_deg: f64, mc_deg: f64) {
281 let mut p = [0.0; 13];
282 porphyry(&mut p, asc_deg, mc_deg);
283 for i in 1..=12 {
284 let prev = if i == 1 { p[12] } else { p[i - 1] };
285 let curr = p[i];
286 let diff = pyfloat::rem(curr - prev + 360.0, 360.0);
287 c[i] = pyfloat::rem(prev + diff / 2.0, 360.0);
288 }
289}
290
291fn opposite_cusps(c: &mut [f64; 13]) {
293 c[5] = pyfloat::rem(c[11] + 180.0, 360.0);
294 c[6] = pyfloat::rem(c[12] + 180.0, 360.0);
295 c[8] = pyfloat::rem(c[2] + 180.0, 360.0);
296 c[9] = pyfloat::rem(c[3] + 180.0, 360.0);
297}
298
299fn set_angles(c: &mut [f64; 13], asc: f64, mc: f64) {
300 c[1] = asc;
301 c[10] = mc;
302 c[7] = pyfloat::rem(asc + 180.0, 360.0);
303 c[4] = pyfloat::rem(mc + 180.0, 360.0);
304}
305
306#[cfg(test)]
307mod tests {
308 use super::*;
309 use crate::frames::Orientation;
310 use crate::time::Time;
311
312 fn calculate_houses(jd: f64, lat: f64, lon: f64, system: HouseSystem, shift: f64) -> Houses {
314 let gast_hours = Orientation::at(&Time::from_ut1(jd)).gast_hours;
315 houses_at_sidereal_time(jd, gast_hours, lat, lon, system, shift)
316 }
317
318 const JD: f64 = 2_448_027.104_166_666_5; fn gap(a: f64, b: f64) -> f64 {
321 let d = (a - b).rem_euclid(360.0);
322 d.min(360.0 - d)
323 }
324
325 #[test]
327 fn systems_match_swiss_ephemeris() {
328 let reference: [(&str, [f64; 12]); 8] = [
329 (
330 "K",
331 [
332 190.2408, 219.3528, 248.6584, 282.0137, 311.8995, 340.9197, 10.2408, 39.3528,
333 68.6584, 102.0137, 131.8995, 160.9197,
334 ],
335 ),
336 (
337 "R",
338 [
339 190.2408, 214.8788, 245.0128, 282.0137, 317.7672, 346.2050, 10.2408, 34.8788,
340 65.0128, 102.0137, 137.7672, 166.2050,
341 ],
342 ),
343 (
344 "C",
345 [
346 190.2408, 222.0070, 252.8709, 282.0137, 310.3369, 339.4310, 10.2408, 42.0070,
347 72.8709, 102.0137, 130.3369, 159.4310,
348 ],
349 ),
350 (
351 "T",
352 [
353 190.2408, 216.9311, 248.0089, 282.0137, 315.5716, 345.3618, 10.2408, 36.9311,
354 68.0089, 102.0137, 135.5716, 165.3618,
355 ],
356 ),
357 (
358 "B",
359 [
360 190.2408, 223.0794, 253.2543, 282.0137, 309.4047, 339.0304, 10.2408, 43.0794,
361 73.2543, 102.0137, 129.4047, 159.0304,
362 ],
363 ),
364 (
365 "M",
366 [
367 192.0137, 220.6069, 251.6333, 284.1881, 315.5252, 344.3861, 12.0137, 40.6069,
368 71.6333, 104.1881, 135.5252, 164.3861,
369 ],
370 ),
371 (
372 "V",
373 [
374 175.2408, 205.2408, 235.2408, 265.2408, 295.2408, 325.2408, 355.2408, 25.2408,
375 55.2408, 85.2408, 115.2408, 145.2408,
376 ],
377 ),
378 (
379 "S",
380 [
381 175.5363, 205.5363, 236.1272, 266.7182, 296.7182, 326.1272,
382 355.5363, 25.5363, 56.1272, 86.7182, 116.7182, 146.1272,
383 ],
384 ),
385 ];
386 for (code, expected) in reference {
387 let houses = calculate_houses(JD, 41.9, 12.5, HouseSystem::from_code(code), 0.0);
388 for (i, (ours, theirs)) in houses.cusps.iter().zip(expected).enumerate() {
389 assert!(
390 gap(*ours, theirs) < 1.0 / 60.0,
391 "{code} cusp {}: {ours:.4} vs {theirs:.4}",
392 i + 1
393 );
394 }
395 }
396 }
397
398 #[test]
399 fn codes_select_their_systems() {
400 for (code, system) in [
401 ("P", HouseSystem::Placidus),
402 ("koch", HouseSystem::Koch),
403 ("R", HouseSystem::Regiomontanus),
404 ("C", HouseSystem::Campanus),
405 ("T", HouseSystem::Topocentric),
406 ("B", HouseSystem::Alcabitius),
407 ("M", HouseSystem::Morinus),
408 ("O", HouseSystem::Porphyry),
409 ("E", HouseSystem::Equal),
410 ("V", HouseSystem::Vehlow),
411 ("W", HouseSystem::WholeSign),
412 ("S", HouseSystem::Sripati),
413 ("sripati", HouseSystem::Sripati),
414 ("?", HouseSystem::Placidus),
415 ("", HouseSystem::Placidus),
416 ] {
417 assert_eq!(HouseSystem::from_code(code), system, "{code}");
418 }
419 }
420
421 #[test]
422 fn koch_falls_back_to_porphyry_inside_the_polar_circles() {
423 let koch = calculate_houses(JD, 70.0, 25.0, HouseSystem::Koch, 0.0);
424 let porphyry = calculate_houses(JD, 70.0, 25.0, HouseSystem::Porphyry, 0.0);
425 assert_eq!(koch, porphyry);
426 }
427}