@zodiacs/engine 0.1.1-rc.15 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CHANGELOG.md +891 -0
  2. package/LICENSING.md +192 -12
  3. package/NOTICE +96 -2
  4. package/README.md +296 -66
  5. package/dist/{api-CoB_kqE3.d.ts → api-WbxJ4Wj-.d.ts} +15 -10
  6. package/dist/ayanamsa--YQ46fET.d.ts +134 -0
  7. package/dist/calc.d.ts +390 -0
  8. package/dist/calc.js +1039 -0
  9. package/dist/chunk-47ERTK5X.js +27 -0
  10. package/dist/chunk-7TRLIFFI.js +56 -0
  11. package/dist/chunk-A3U2ZS2W.js +29 -0
  12. package/dist/{chunk-KHBLFXFG.js → chunk-B5LKF437.js} +29 -28
  13. package/dist/{chunk-ONBZ4HK7.js → chunk-BHJXUY67.js} +4 -2
  14. package/dist/{chunk-OYGRVGXD.js → chunk-CHSYDKBL.js} +3 -3
  15. package/dist/{chunk-ZHVI3KWJ.js → chunk-DMGHYCDI.js} +4 -4
  16. package/dist/{chunk-CWI4YUDL.js → chunk-FTX3JDJA.js} +21 -18
  17. package/dist/{chunk-MFF3VKO3.js → chunk-OG4OYTJJ.js} +47 -42
  18. package/dist/{chunk-ZZPITHQS.js → chunk-SRXBTP5H.js} +199 -65
  19. package/dist/{chunk-AECKV73C.js → chunk-TSS2SZWQ.js} +8 -2
  20. package/dist/chunk-WS3TDSP3.js +474 -0
  21. package/dist/chunk-YIITY7Q7.js +483 -0
  22. package/dist/crossings.d.ts +8 -7
  23. package/dist/crossings.js +1 -1
  24. package/dist/deltat.d.ts +28 -3
  25. package/dist/deltat.js +1 -1
  26. package/dist/geo.d.ts +33 -12
  27. package/dist/geo.js +17 -458
  28. package/dist/{internal-math-BbktkHCc.d.ts → houses-CFERsAlK.d.ts} +14 -160
  29. package/dist/houses-extra.d.ts +95 -0
  30. package/dist/houses-extra.js +339 -0
  31. package/dist/index.d.ts +34 -48
  32. package/dist/index.js +31 -23
  33. package/dist/internal-math-DJJ8BYex.d.ts +174 -0
  34. package/dist/internal-math.d.ts +3 -2
  35. package/dist/internal-math.js +2 -2
  36. package/dist/internal.d.ts +1 -1
  37. package/dist/internal.js +5 -5
  38. package/dist/receipt.d.ts +2 -243
  39. package/dist/receipt.js +78 -37
  40. package/dist/reference-span-CILpclrz.d.ts +40 -0
  41. package/dist/rulers-Dla6h_Sm.d.ts +15 -0
  42. package/dist/sky.d.ts +173 -0
  43. package/dist/sky.js +379 -0
  44. package/dist/techniques.d.ts +453 -0
  45. package/dist/techniques.js +823 -0
  46. package/dist/timing.d.ts +113 -17
  47. package/dist/timing.js +98 -64
  48. package/dist/{types-BsWaqwU_.d.ts → types-B-yb_tGL.d.ts} +23 -11
  49. package/dist/vedic.d.ts +42 -124
  50. package/dist/vedic.js +44 -458
  51. package/dist/window.d.ts +180 -0
  52. package/dist/window.js +960 -0
  53. package/dist/zone-history-ghdyO0Sc.d.ts +268 -0
  54. package/package.json +25 -3
package/dist/window.js ADDED
@@ -0,0 +1,960 @@
1
+ import {
2
+ dateFrom
3
+ } from "./chunk-CPALRSPC.js";
4
+ import {
5
+ validateBirthSettings
6
+ } from "./chunk-PFGUCNOK.js";
7
+ import {
8
+ bodyLongitude,
9
+ gastHours,
10
+ onChartClock,
11
+ tilt
12
+ } from "./chunk-SRXBTP5H.js";
13
+ import {
14
+ REFERENCE_SPAN,
15
+ elapsedDays,
16
+ timeBasis
17
+ } from "./chunk-B5LKF437.js";
18
+ import "./chunk-BHJXUY67.js";
19
+ import {
20
+ computeAngles,
21
+ computeHouses,
22
+ houseOf,
23
+ isPolarUndefinedHouseSystem
24
+ } from "./chunk-DMGHYCDI.js";
25
+ import {
26
+ ASPECTS,
27
+ ASPECT_TYPES,
28
+ ENGINE_VERSION,
29
+ SIGN_SLUGS,
30
+ matchAspect,
31
+ normalizeLongitude,
32
+ separation,
33
+ signIndexForLongitude
34
+ } from "./chunk-OG4OYTJJ.js";
35
+
36
+ // src/window-ranges.ts
37
+ var DEG = Math.PI / 180;
38
+ var RAD = 180 / Math.PI;
39
+ function wrap360(value) {
40
+ return (value % 360 + 360) % 360;
41
+ }
42
+ function signedDelta(from, to) {
43
+ const difference = wrap360(to - from);
44
+ return difference > 180 ? difference - 360 : difference;
45
+ }
46
+ function advance(from, to) {
47
+ const step = wrap360(to - from);
48
+ return step > 300 ? step - 360 : step;
49
+ }
50
+ function reaches(range, period, offset, pad) {
51
+ if (range === null) return true;
52
+ return Math.floor((range[1] + pad - offset) / period) * period + offset >= range[0] - pad;
53
+ }
54
+ function hull(from, step, pad) {
55
+ return [Math.min(from, from + step) - pad, Math.max(from, from + step) + pad];
56
+ }
57
+ function shift(range, by) {
58
+ return range === null ? null : [range[0] + by, range[1] + by];
59
+ }
60
+ function trigRange(f, top, a1, a2) {
61
+ const v1 = f(a1 * DEG);
62
+ const v2 = f(a2 * DEG);
63
+ const has = (at) => Math.floor((a2 - at) / 360) * 360 + at >= a1;
64
+ return [has(top + 180) ? -1 : Math.min(v1, v2), has(top) ? 1 : Math.max(v1, v2)];
65
+ }
66
+ function cosRange(a1, a2) {
67
+ return trigRange(Math.cos, 0, a1, a2);
68
+ }
69
+ function sinRange(a1, a2) {
70
+ return trigRange(Math.sin, 90, a1, a2);
71
+ }
72
+ function oblique(a, K, c) {
73
+ const r = a * DEG;
74
+ return Math.atan2(Math.sin(r), Math.cos(r) * c - K) * RAD;
75
+ }
76
+ function obliqueRange(a1, a2, K, c, tanPole, dEps) {
77
+ const s2 = 1 - c * c;
78
+ const [v1, v2] = cosRange(a1, a2);
79
+ const d = (v) => 1 + K * K - s2 * v * v - 2 * c * K * v;
80
+ const d2 = Math.min(d(v1), d(v2));
81
+ if (!(d2 >= 1e-12)) return null;
82
+ const distance = Math.sqrt(d2);
83
+ const points = [a1];
84
+ const monotone = Math.abs(K) < c;
85
+ if (!monotone) {
86
+ const turn = Math.acos(c / K) * RAD;
87
+ for (const base of [turn, -turn]) {
88
+ for (let x = base + 360 * Math.ceil((a1 - base) / 360); x < a2; x += 360) {
89
+ if (x > a1) points.push(x);
90
+ }
91
+ }
92
+ points.sort((x, y) => x - y);
93
+ }
94
+ points.push(a2);
95
+ let previous = oblique(a1, K, c);
96
+ let at = previous;
97
+ let lo = previous;
98
+ let hi = previous;
99
+ for (let index = 1; index < points.length; index += 1) {
100
+ const current = oblique(points[index], K, c);
101
+ at += monotone ? advance(previous, current) : signedDelta(previous, current);
102
+ lo = Math.min(lo, at);
103
+ hi = Math.max(hi, at);
104
+ previous = current;
105
+ }
106
+ const rate = Math.max(Math.abs(c - K * v1), Math.abs(c - K * v2)) / d2;
107
+ const pad = 1e-15 / distance * RAD + rate * 1e-12 + 2 * ((Math.sqrt(s2) + Math.abs(tanPole) * c) / distance) * dEps;
108
+ return { range: [lo - pad, hi + pad], rate };
109
+ }
110
+ function morinusRange(a1, a2, c, dEps) {
111
+ const at = (a) => Math.atan2(Math.sin(a * DEG) * c, Math.cos(a * DEG)) * RAD;
112
+ const from = at(a1);
113
+ const pad = 1e-15 / c * RAD + Math.sqrt(1 - c * c) / c * dEps;
114
+ return hull(from, advance(from, at(a2)), pad);
115
+ }
116
+
117
+ // src/window.ts
118
+ var DAY = 864e5;
119
+ var DEG2 = Math.PI / 180;
120
+ var RAD2 = 180 / Math.PI;
121
+ var J2000 = Date.UTC(2e3, 0, 1, 12);
122
+ var BIRTH_WINDOW_SCHEMA = "zodiacs.birth-window.v1";
123
+ var WINDOW_VERIFICATION = "sampled at one-second resolution";
124
+ var MAX_WINDOW_MS = 1728e5;
125
+ var WINDOW_RATE_BOUNDS = /* @__PURE__ */ Object.freeze({
126
+ Sun: 2.1,
127
+ Moon: 31,
128
+ Mercury: 4.5,
129
+ Venus: 2.6,
130
+ Mars: 1.6,
131
+ Jupiter: 0.49,
132
+ Saturn: 0.27,
133
+ Uranus: 0.13,
134
+ Neptune: 0.077,
135
+ Pluto: 0.082,
136
+ "North Node": 0.53,
137
+ "South Node": 0.53
138
+ });
139
+ function nodeJitter(utcMilliseconds) {
140
+ return 5e-5 * (1 + Math.abs((utcMilliseconds - J2000) / (36525 * DAY)));
141
+ }
142
+ var OBLIQUITY_RATE = 5e-5;
143
+ var SIDEREAL_RATE = 360.98562;
144
+ var MAX_ANGLE_MS = Math.floor(45 / SIDEREAL_RATE * DAY);
145
+ var BAND = 1e-9;
146
+ var MAX_EVALUATIONS = 2e6;
147
+ var SPAN_FROM = /* @__PURE__ */ (() => Date.parse(REFERENCE_SPAN.from))();
148
+ var SPAN_TO = /* @__PURE__ */ (() => Date.parse(REFERENCE_SPAN.to))();
149
+ var POLE_MARGIN = 1e-6;
150
+ var NODE_COARSE_MS = 36e5;
151
+ var NODE_FINE_MS = 6e4;
152
+ var BAND_EDGE = 5;
153
+ var BAND_COST = 3;
154
+ var UNRESOLVED = -2;
155
+ var BODIES = [
156
+ "Sun",
157
+ "Moon",
158
+ "Mercury",
159
+ "Venus",
160
+ "Mars",
161
+ "Jupiter",
162
+ "Saturn",
163
+ "Uranus",
164
+ "Neptune",
165
+ "Pluto",
166
+ "North Node",
167
+ "South Node"
168
+ ];
169
+ var NORTH = 10;
170
+ var SOUTH = 11;
171
+ var RATES = /* @__PURE__ */ BODIES.map((body) => WINDOW_RATE_BOUNDS[body]);
172
+ var PAIRS = [];
173
+ for (let first = 0; first < 10; first += 1) {
174
+ for (let second = first + 1; second < 10; second += 1) PAIRS.push([first, second]);
175
+ }
176
+ var orbEdges = (luminary) => ASPECTS.flatMap(({ angle, orb, luminaryOrb }) => {
177
+ const width = luminary ? luminaryOrb : orb;
178
+ return [angle - width, angle + width].filter((edge) => edge > 0 && edge < 180);
179
+ });
180
+ var ORB_EDGES = /* @__PURE__ */ orbEdges(false);
181
+ var LUMINARY_ORB_EDGES = /* @__PURE__ */ orbEdges(true);
182
+ var ASC = 12;
183
+ var MC = 13;
184
+ var HOUSE = 14;
185
+ var ASPECT = 26;
186
+ var SYSTEM = ASPECT + PAIRS.length;
187
+ var SENSITIVE_RAMC = {
188
+ placidus: [
189
+ [30, 150, 210, 270, 330],
190
+ [30, 90, 150, 210, 330]
191
+ ],
192
+ koch: [
193
+ [90, 270],
194
+ [90, 270]
195
+ ]
196
+ };
197
+ var WindowBudgetError = class extends Error {
198
+ name = "WindowBudgetError";
199
+ /** The instants the search allows; a later release may allow more. */
200
+ limit = MAX_EVALUATIONS;
201
+ constructor() {
202
+ super(
203
+ `The window needs more than ${MAX_EVALUATIONS} evaluated instants.`
204
+ );
205
+ }
206
+ };
207
+ function firstWhere(holds, from, to) {
208
+ let low = from;
209
+ let high = to;
210
+ while (high - low > 1) {
211
+ const middle = low + Math.floor((high - low) / 2);
212
+ if (holds(middle)) high = middle;
213
+ else low = middle;
214
+ }
215
+ return high;
216
+ }
217
+ var onePiece = (a, b) => Number.isNaN(elapsedDays(timeBasis(a, "utc"), timeBasis(b, "utc"), Number.NaN));
218
+ function seamsIn(start, end) {
219
+ const steps = [];
220
+ let from = start - 1;
221
+ while (from < end && !onePiece(from, end)) {
222
+ const at = firstWhere((time) => !onePiece(from, time), from, end);
223
+ steps.push(at - 1, at, at + 1);
224
+ from = at;
225
+ }
226
+ return { bodies: BODIES.map(() => steps), angles: steps };
227
+ }
228
+ var Sky = class {
229
+ constructor(latitude, longitude, system, seams) {
230
+ this.latitude = latitude;
231
+ this.longitude = longitude;
232
+ this.system = system;
233
+ this.seams = seams;
234
+ this.tanPhi = Math.tan(latitude * DEG2);
235
+ const values = SENSITIVE_RAMC[system];
236
+ this.sensitive = values ? values[latitude >= 0 ? 0 : 1] : [];
237
+ }
238
+ latitude;
239
+ longitude;
240
+ system;
241
+ seams;
242
+ instants = /* @__PURE__ */ new Map();
243
+ evaluations = 0;
244
+ violations = 0;
245
+ tanPhi;
246
+ sensitive;
247
+ instant(time) {
248
+ let found = this.instants.get(time);
249
+ if (!found) {
250
+ if (++this.evaluations > MAX_EVALUATIONS) throw new WindowBudgetError();
251
+ found = { lon: new Float64Array(12).fill(Number.NaN), angles: void 0 };
252
+ this.instants.set(time, found);
253
+ }
254
+ return found;
255
+ }
256
+ forget(time) {
257
+ this.instants.delete(time);
258
+ }
259
+ lon(time, body) {
260
+ const at = this.instant(time);
261
+ let value = at.lon[body];
262
+ if (Number.isNaN(value)) {
263
+ if (body === SOUTH) value = normalizeLongitude(this.lon(time, NORTH) + 180);
264
+ else value = bodyLongitude(BODIES[body], new Date(time));
265
+ at.lon[body] = value;
266
+ }
267
+ return value;
268
+ }
269
+ node(time) {
270
+ return bodyLongitude("North Node", new Date(time));
271
+ }
272
+ angles(time) {
273
+ const at = this.instant(time);
274
+ if (!at.angles) {
275
+ const input = onChartClock(time, "utc", void 0, (astroTime) => ({
276
+ gastHours: gastHours(astroTime),
277
+ latitude: this.latitude,
278
+ longitude: this.longitude,
279
+ obliquity: tilt(astroTime.tt).tobl
280
+ }));
281
+ const angles = computeAngles(input);
282
+ const houses = computeHouses(this.system, input, angles);
283
+ at.angles = {
284
+ ramc: normalizeLongitude(input.gastHours * 15 + input.longitude),
285
+ obliquity: input.obliquity,
286
+ asc: angles.asc,
287
+ mc: angles.mc,
288
+ cusps: houses.houses.cusps,
289
+ fellBack: houses.fellBack
290
+ };
291
+ }
292
+ return at.angles;
293
+ }
294
+ value(time, id) {
295
+ if (id < ASC) return signIndexForLongitude(this.lon(time, id));
296
+ if (id === ASC) return signIndexForLongitude(this.angles(time).asc);
297
+ if (id === MC) return signIndexForLongitude(this.angles(time).mc);
298
+ if (id < ASPECT) return houseOf(this.lon(time, id - HOUSE), this.angles(time).cusps);
299
+ if (id < SYSTEM) {
300
+ const [first, second] = PAIRS[id - ASPECT];
301
+ const match = matchAspect(
302
+ BODIES[first],
303
+ this.lon(time, first),
304
+ BODIES[second],
305
+ this.lon(time, second)
306
+ );
307
+ return match ? ASPECT_TYPES.indexOf(match.definition.type) : -1;
308
+ }
309
+ return this.angles(time).fellBack ? 1 : 0;
310
+ }
311
+ };
312
+ var Interval = class {
313
+ constructor(sky, t1, t2) {
314
+ this.sky = sky;
315
+ this.t1 = t1;
316
+ this.t2 = t2;
317
+ }
318
+ sky;
319
+ t1;
320
+ t2;
321
+ bodies = [];
322
+ context;
323
+ cuspList;
324
+ spans;
325
+ stable;
326
+ guard;
327
+ mayChange(id) {
328
+ if (id < ASC) return reaches(this.body(id), 30, 0, BAND);
329
+ if (id === ASC) return reaches(this.angleContext()?.asc ?? null, 30, 0, BAND);
330
+ if (id === MC) return reaches(this.angleContext()?.mc ?? null, 30, 0, BAND);
331
+ if (id < ASPECT) return this.houseMayChange(id - HOUSE);
332
+ if (id < SYSTEM) return this.aspectMayChange(id - ASPECT);
333
+ return this.guarded() || !this.systemStable();
334
+ }
335
+ violation() {
336
+ this.sky.violations += 1;
337
+ return null;
338
+ }
339
+ jumps(seams) {
340
+ return seams.some((z) => this.t1 < z && z <= this.t2);
341
+ }
342
+ body(index) {
343
+ const known = this.bodies[index];
344
+ if (known !== void 0) return known;
345
+ const { sky, t1, t2 } = this;
346
+ const reach = RATES[index] * (t2 - t1) / DAY;
347
+ const jitter = index >= NORTH ? 2 * Math.max(nodeJitter(t1), nodeJitter(t2)) : 0;
348
+ let range;
349
+ if (reach + jitter >= 90 || this.jumps(sky.seams.bodies[index])) range = null;
350
+ else {
351
+ const l1 = sky.lon(t1, index);
352
+ const step = signedDelta(l1, sky.lon(t2, index));
353
+ if (Math.abs(step) > reach + jitter + 1e-12) range = this.violation();
354
+ else {
355
+ const middle = l1 + step / 2;
356
+ const half = reach / 2 + jitter;
357
+ range = [middle - half, middle + half];
358
+ }
359
+ }
360
+ this.bodies[index] = range;
361
+ return range;
362
+ }
363
+ aspectMayChange(pair) {
364
+ const { sky, t1, t2 } = this;
365
+ const [first, second] = PAIRS[pair];
366
+ if (this.jumps(sky.seams.bodies[first]) || this.jumps(sky.seams.bodies[second])) return true;
367
+ const s1 = separation(sky.lon(t1, first), sky.lon(t1, second));
368
+ const s2 = separation(sky.lon(t2, first), sky.lon(t2, second));
369
+ const reach = (RATES[first] + RATES[second]) * (t2 - t1) / DAY;
370
+ if (Math.abs(s2 - s1) > reach + 1e-12) {
371
+ this.violation();
372
+ return true;
373
+ }
374
+ const lo = (s1 + s2 - reach) / 2 - BAND;
375
+ const hi = (s1 + s2 + reach) / 2 + BAND;
376
+ const edges = first < 2 ? LUMINARY_ORB_EDGES : ORB_EDGES;
377
+ return edges.some((edge) => edge >= lo && edge <= hi);
378
+ }
379
+ systemStable() {
380
+ if (this.stable !== void 0) return this.stable;
381
+ const { sky, t1, t2 } = this;
382
+ let stable = true;
383
+ if (isPolarUndefinedHouseSystem(sky.system)) {
384
+ if (this.jumps(sky.seams.angles)) stable = false;
385
+ else {
386
+ const e1 = sky.angles(t1).obliquity;
387
+ const dEps = OBLIQUITY_RATE * (t2 - t1) / DAY;
388
+ if (Math.abs(sky.angles(t2).obliquity - e1) > dEps + 1e-12) {
389
+ this.violation();
390
+ stable = false;
391
+ } else {
392
+ const limit = 90 - Math.abs(sky.latitude);
393
+ stable = limit < e1 - dEps - 1e-12 || limit > e1 + dEps + 1e-12;
394
+ }
395
+ }
396
+ }
397
+ this.stable = stable;
398
+ return stable;
399
+ }
400
+ guarded() {
401
+ if (this.guard !== void 0) return this.guard;
402
+ const { sky, t1, t2 } = this;
403
+ let guard = false;
404
+ if (sky.sensitive.length > 0) {
405
+ const a1 = sky.angles(t1);
406
+ const dEps = OBLIQUITY_RATE * (t2 - t1) / DAY;
407
+ if (Math.abs(90 - Math.abs(sky.latitude) - a1.obliquity) <= dEps + 1e-8) {
408
+ const turn = SIDEREAL_RATE * (t2 - t1) / DAY * (1 + 1e-6) + 1e-3;
409
+ guard = turn >= 360 || sky.sensitive.some((value) => reaches([a1.ramc, a1.ramc + turn], 360, value, 0.1));
410
+ }
411
+ }
412
+ this.guard = guard;
413
+ return guard;
414
+ }
415
+ angleContext() {
416
+ if (this.context !== void 0) return this.context;
417
+ this.context = null;
418
+ const { sky, t1, t2 } = this;
419
+ if (t2 - t1 > MAX_ANGLE_MS || this.jumps(sky.seams.angles)) return null;
420
+ const a1 = sky.angles(t1);
421
+ const a2 = sky.angles(t2);
422
+ const turn = wrap360(a2.ramc - a1.ramc);
423
+ const expected = SIDEREAL_RATE * (t2 - t1) / DAY;
424
+ const dEps = OBLIQUITY_RATE * (t2 - t1) / DAY;
425
+ if (Math.abs(turn - expected) > 1e-3 + 1e-6 * expected) return this.violation();
426
+ if (Math.abs(a2.obliquity - a1.obliquity) > dEps + 1e-12) return this.violation();
427
+ const theta1 = a1.ramc;
428
+ const theta2 = theta1 + turn;
429
+ const c = Math.cos(a1.obliquity * DEG2);
430
+ const s = Math.sin(a1.obliquity * DEG2);
431
+ const tanPhi = sky.tanPhi;
432
+ const mc = obliqueRange(theta1, theta2, 0, c, 0, dEps)?.range ?? null;
433
+ const raw = obliqueRange(theta1 + 90, theta2 + 90, tanPhi * s, c, tanPhi, dEps)?.range ?? null;
434
+ let gap = null;
435
+ let turned = null;
436
+ let asc = null;
437
+ if (mc && raw) {
438
+ const difference = [raw[0] - mc[1], raw[1] - mc[0]];
439
+ if (!reaches(difference, 180, 0, BAND)) {
440
+ gap = difference;
441
+ turned = wrap360((difference[0] + difference[1]) / 2) >= 180;
442
+ asc = turned ? shift(raw, 180) : raw;
443
+ }
444
+ }
445
+ const context = {
446
+ theta1,
447
+ theta2,
448
+ c,
449
+ s,
450
+ dEps,
451
+ mc: this.holds(mc, a1.mc, a2.mc),
452
+ raw,
453
+ gap,
454
+ turned,
455
+ asc: this.holds(asc, a1.asc, a2.asc)
456
+ };
457
+ this.context = context;
458
+ return context;
459
+ }
460
+ holds(range, v1, v2) {
461
+ if (range === null) return null;
462
+ return reaches(range, 360, v1, 1e-7) && reaches(range, 360, v2, 1e-7) ? range : this.violation();
463
+ }
464
+ houseMayChange(index) {
465
+ if (this.guarded() || !this.systemStable()) return true;
466
+ const body = this.body(index);
467
+ const context = this.angleContext();
468
+ if (body === null || context === null) return true;
469
+ const { sky, t1 } = this;
470
+ if (sky.system === "whole" || sky.angles(t1).fellBack) {
471
+ return reaches(body, 30, 0, BAND) || reaches(context.asc, 30, 0, BAND);
472
+ }
473
+ const cusps = this.cusps();
474
+ if (cusps === null || this.spansMayClose()) return true;
475
+ return cusps.some(
476
+ (cusp) => cusp === null || reaches([body[0] - cusp[1], body[1] - cusp[0]], 360, 0, BAND)
477
+ );
478
+ }
479
+ spansMayClose() {
480
+ if (this.spans !== void 0) return this.spans;
481
+ const cusps = this.cusps();
482
+ const system = this.sky.system;
483
+ let spans = false;
484
+ if (cusps === null) spans = true;
485
+ else if (system !== "equal" && system !== "vehlow" && system !== "equal-mc") {
486
+ spans = cusps.some((cusp, index) => {
487
+ const next = cusps[(index + 1) % 12];
488
+ return cusp === null || next === null || reaches([next[0] - cusp[1], next[1] - cusp[0]], 360, 0, BAND);
489
+ });
490
+ }
491
+ this.spans = spans;
492
+ return spans;
493
+ }
494
+ cusps() {
495
+ if (this.cuspList !== void 0) return this.cuspList;
496
+ this.cuspList = null;
497
+ const context = this.angleContext();
498
+ if (context === null) return null;
499
+ const list = this.cuspRanges(context);
500
+ if (list === null) return null;
501
+ const a1 = this.sky.angles(this.t1);
502
+ const a2 = this.sky.angles(this.t2);
503
+ const checked = list.map((range, index) => this.holds(range, a1.cusps[index], a2.cusps[index]));
504
+ if (checked.some((range) => range === null)) return null;
505
+ this.cuspList = checked;
506
+ return checked;
507
+ }
508
+ cuspRanges(context) {
509
+ const { sky, t1, t2 } = this;
510
+ const { theta1, theta2, c, s, dEps, mc, asc, raw, turned, gap } = context;
511
+ const every = (make) => Array.from({ length: 12 }, (_, index) => make(index));
512
+ const tanPhi = sky.tanPhi;
513
+ switch (sky.system) {
514
+ case "equal":
515
+ return every((index) => shift(asc, index * 30));
516
+ case "vehlow":
517
+ return every((index) => shift(asc, index * 30 - 15));
518
+ case "equal-mc":
519
+ return every((index) => shift(mc, (index - 9) * 30));
520
+ case "porphyry": {
521
+ if (mc === null || gap === null || turned === null) return null;
522
+ const middle = (gap[0] + gap[1]) / 2;
523
+ const upper = shift(gap, -Math.floor(middle / 180) * 180);
524
+ const parts = [
525
+ [0, 1],
526
+ [60, 2 / 3],
527
+ [120, 1 / 3],
528
+ [180, 0],
529
+ [180, 1 / 3],
530
+ [180, 2 / 3],
531
+ [180, 1],
532
+ [240, 2 / 3],
533
+ [300, 1 / 3],
534
+ [0, 0],
535
+ [0, 1 / 3],
536
+ [0, 2 / 3]
537
+ ];
538
+ return parts.map(([arc, fraction]) => [
539
+ mc[0] + arc + fraction * upper[0],
540
+ mc[1] + arc + fraction * upper[1]
541
+ ]);
542
+ }
543
+ case "placidus": {
544
+ const a1 = sky.angles(t1);
545
+ const a2 = sky.angles(t2);
546
+ const q = Math.abs(tanPhi) * (s / c);
547
+ if (!(q < 1)) return null;
548
+ const g = 2 / 3 * Math.abs(tanPhi) / (c * c * c * Math.sqrt(1 - q * q) * (1 - 2 * q / 3)) + s / c;
549
+ const pad = 2 * g * dEps + 1e-8;
550
+ return every((index) => {
551
+ if (index === 0) return asc;
552
+ if (index === 3) return shift(mc, 180);
553
+ if (index === 6) return shift(asc, 180);
554
+ if (index === 9) return mc;
555
+ const from = a1.cusps[index];
556
+ return hull(from, advance(from, a2.cusps[index]), pad);
557
+ });
558
+ }
559
+ case "koch": {
560
+ const a1 = sky.angles(t1);
561
+ const a2 = sky.angles(t2);
562
+ const q = Math.abs(tanPhi) * (s / c);
563
+ if (!(q < 1)) return null;
564
+ const third = (mcLongitude, obliquity) => (90 + Math.asin(
565
+ tanPhi * Math.tan(Math.asin(Math.sin(obliquity * DEG2) * Math.sin(mcLongitude * DEG2)))
566
+ ) * RAD2) / 3;
567
+ const third1 = third(a1.mc, a1.obliquity);
568
+ const third2 = third(a2.mc, a2.obliquity);
569
+ const k = tanPhi * s;
570
+ const coupling = 2 / 3 * Math.abs(tanPhi) / (c * c * Math.sqrt(1 - q * q));
571
+ const at = (m) => {
572
+ const from = theta1 + 90 + m * third1;
573
+ const to = theta2 + 90 + m * third2;
574
+ const found = obliqueRange(Math.min(from, to), Math.max(from, to), k, c, tanPhi, dEps);
575
+ if (found === null) return null;
576
+ const pad = 2 * coupling * found.rate * dEps;
577
+ return [found.range[0] - pad, found.range[1] + pad];
578
+ };
579
+ return quadrant(asc, mc, at(-2), at(-1), at(1), at(2));
580
+ }
581
+ case "regiomontanus":
582
+ case "campanus":
583
+ case "topocentric": {
584
+ if (turned === null) return null;
585
+ const circles = circleSystem(sky.system, sky.latitude);
586
+ const [c11, c12, c2, c3] = circles.map(([ascension, pole]) => {
587
+ const tanPole = Math.tan(pole * DEG2);
588
+ return obliqueRange(theta1 + ascension, theta2 + ascension, tanPole * s, c, tanPole, dEps)?.range ?? null;
589
+ });
590
+ const cusps = quadrant(raw, mc, c11, c12, c2, c3);
591
+ return turned ? cusps.map((range) => shift(range, 180)) : cusps;
592
+ }
593
+ case "alcabitius": {
594
+ if (asc === null) return null;
595
+ const [u1, u2] = sinRange(asc[0], asc[1]);
596
+ const obliquity = sky.angles(t1).obliquity;
597
+ const sines = [Math.sin((obliquity - dEps) * DEG2), Math.sin((obliquity + dEps) * DEG2)];
598
+ let xLo = Infinity;
599
+ let xHi = -Infinity;
600
+ for (const u of [u1, u2]) {
601
+ for (const sine of sines) {
602
+ const x = -tanPhi * Math.tan(Math.asin(sine * u));
603
+ xLo = Math.min(xLo, x);
604
+ xHi = Math.max(xHi, x);
605
+ }
606
+ }
607
+ const rounding = 1e-12 * (1 + Math.max(Math.abs(xLo), Math.abs(xHi)));
608
+ const clamp = (x) => Math.min(1, Math.max(-1, x));
609
+ const dayLo = Math.acos(clamp(xHi + rounding)) * RAD2;
610
+ const dayHi = Math.acos(clamp(xLo - rounding)) * RAD2;
611
+ const at = (base, fraction) => obliqueRange(theta1 + base + fraction * dayLo, theta2 + base + fraction * dayHi, 0, c, 0, dEps)?.range ?? null;
612
+ return quadrant(asc, mc, at(0, 1 / 3), at(0, 2 / 3), at(60, 2 / 3), at(120, 1 / 3));
613
+ }
614
+ case "meridian":
615
+ return every((index) => obliqueRange(theta1 + 90 + index * 30, theta2 + 90 + index * 30, 0, c, 0, dEps)?.range ?? null);
616
+ case "morinus":
617
+ return every((index) => morinusRange(theta1 + 90 + index * 30, theta2 + 90 + index * 30, c, dEps));
618
+ case "whole":
619
+ return null;
620
+ default:
621
+ throw new RangeError(`Unknown house system ${String(sky.system)}.`);
622
+ }
623
+ }
624
+ };
625
+ function quadrant(asc, mc, c11, c12, c2, c3) {
626
+ return [asc, c2, c3, shift(mc, 180), shift(c11, 180), shift(c12, 180), shift(asc, 180), shift(c2, 180), shift(c3, 180), mc, c11, c12];
627
+ }
628
+ function circleSystem(system, latitude) {
629
+ const phi = latitude * DEG2;
630
+ if (system === "campanus") {
631
+ const sine = Math.sin(phi);
632
+ const cosine = Math.cos(phi);
633
+ const near2 = Math.asin(sine / 2) * RAD2;
634
+ const far2 = Math.asin(Math.sqrt(3) / 2 * sine) * RAD2;
635
+ const wide = Math.atan(Math.sqrt(3) / cosine) * RAD2;
636
+ const narrow = Math.atan(1 / (Math.sqrt(3) * cosine)) * RAD2;
637
+ return [
638
+ [90 - wide, near2],
639
+ [90 - narrow, far2],
640
+ [90 + narrow, far2],
641
+ [90 + wide, near2]
642
+ ];
643
+ }
644
+ const tangent = Math.tan(phi);
645
+ const [near, far] = system === "regiomontanus" ? [Math.atan(tangent * 0.5) * RAD2, Math.atan(tangent * Math.cos(30 * DEG2)) * RAD2] : [Math.atan(tangent / 3) * RAD2, Math.atan(tangent * 2 / 3) * RAD2];
646
+ return [
647
+ [30, near],
648
+ [60, far],
649
+ [120, far],
650
+ [150, near]
651
+ ];
652
+ }
653
+ function search(sky, t1, t2, active, changes, bands) {
654
+ for (const band of bands) {
655
+ if (t1 >= band.a - 1 && t2 <= band.b) active = active.filter((id) => !band.ids.includes(id));
656
+ }
657
+ if (active.length === 0) return;
658
+ if (t2 - t1 === 1) {
659
+ for (const id of active) {
660
+ const from = sky.value(t1, id);
661
+ const to = sky.value(t2, id);
662
+ if (from !== to) changes.push({ at: t2, id, from, to });
663
+ }
664
+ return;
665
+ }
666
+ const interval = new Interval(sky, t1, t2);
667
+ const next = [];
668
+ for (const id of active) {
669
+ const may = interval.mayChange(id);
670
+ const differs = sky.value(t1, id) !== sky.value(t2, id);
671
+ if (differs && !may) sky.violations += 1;
672
+ if (may || differs) next.push(id);
673
+ }
674
+ if (next.length === 0) return;
675
+ const middle = t1 + Math.floor((t2 - t1) / 2);
676
+ search(sky, t1, middle, next, changes, bands);
677
+ search(sky, middle, t2, next, changes, bands);
678
+ sky.forget(middle);
679
+ }
680
+ function nodeBands(sky, start, last) {
681
+ const jitter = Math.max(nodeJitter(start), nodeJitter(last));
682
+ const edge = BAND_EDGE * jitter;
683
+ const width = BAND_COST * jitter;
684
+ const bands = [];
685
+ let open = null;
686
+ const include = (t0, u0, t1, u1) => {
687
+ const lo = Math.min(u0, u1);
688
+ const hi = Math.max(u0, u1);
689
+ if (!reaches([lo, hi], 30, 0, edge)) return;
690
+ let near = 0;
691
+ for (let boundary = Math.ceil((lo - width) / 30) * 30; boundary <= hi + width; boundary += 30) {
692
+ const overlap = Math.min(hi, boundary + width) - Math.max(lo, boundary - width);
693
+ if (overlap < 0) continue;
694
+ near += hi > lo ? overlap / (hi - lo) * (t1 - t0) : t1 - t0;
695
+ }
696
+ if (open && open.b >= t0) {
697
+ open.b = t1;
698
+ open.cost += near;
699
+ } else {
700
+ if (open) bands.push(open);
701
+ open = { a: t0, b: t1, cost: near };
702
+ }
703
+ };
704
+ let previous = start;
705
+ let value = sky.node(start);
706
+ for (let time = Math.min(start + NODE_COARSE_MS, last); time > previous; time = Math.min(time + NODE_COARSE_MS, last)) {
707
+ const next = value + signedDelta(value, sky.node(time));
708
+ const reach = RATES[NORTH] * (time - previous) / DAY;
709
+ const half = reach / 2 + 2 * jitter;
710
+ const middle = (value + next) / 2;
711
+ if (reaches([middle - half, middle + half], 30, 0, edge)) {
712
+ let t0 = previous;
713
+ let u0 = value;
714
+ for (let t1 = Math.min(t0 + NODE_FINE_MS, time); t1 > t0; t1 = Math.min(t1 + NODE_FINE_MS, time)) {
715
+ const u1 = t1 === time ? next : u0 + signedDelta(u0, sky.node(t1));
716
+ include(t0, u0, t1, u1);
717
+ t0 = t1;
718
+ u0 = u1;
719
+ }
720
+ }
721
+ previous = time;
722
+ value = next;
723
+ }
724
+ if (open) bands.push(open);
725
+ return bands;
726
+ }
727
+ function finiteMinutes(value, label) {
728
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
729
+ throw new RangeError(`${label} must be a positive, finite number of minutes.`);
730
+ }
731
+ const milliseconds = Math.round(value * 6e4);
732
+ if (milliseconds < 1) throw new RangeError(`${label} must be at least one millisecond.`);
733
+ return milliseconds;
734
+ }
735
+ function resolvedWindow(input) {
736
+ let rounding = null;
737
+ if (input.rounding !== void 0) {
738
+ const model = input.rounding;
739
+ if (!model || typeof model !== "object") throw new RangeError("rounding must be an object.");
740
+ const recorded = dateFrom(model.recorded, "rounding.recorded");
741
+ const unit = finiteMinutes(model.minutes, "rounding.minutes");
742
+ const mode = model.mode ?? "nearest";
743
+ if (mode !== "nearest" && mode !== "down") throw new RangeError('rounding.mode must be "nearest" or "down".');
744
+ const from = recorded.getTime() - (mode === "nearest" ? Math.floor(unit / 2) : 0);
745
+ rounding = {
746
+ recorded,
747
+ minutes: model.minutes,
748
+ mode,
749
+ start: new Date(from),
750
+ end: new Date(from + unit)
751
+ };
752
+ }
753
+ const hasRange = input.start !== void 0 || input.end !== void 0;
754
+ const hasCentre = input.at !== void 0 || input.minutes !== void 0;
755
+ if (hasRange && hasCentre) throw new RangeError("Give start and end, or at and minutes, not both.");
756
+ let start;
757
+ let end;
758
+ if (hasRange) {
759
+ start = dateFrom(input.start, "start").getTime();
760
+ end = dateFrom(input.end, "end").getTime();
761
+ } else if (hasCentre) {
762
+ const centre = dateFrom(input.at, "at").getTime();
763
+ const half = finiteMinutes(input.minutes, "minutes");
764
+ start = centre - half;
765
+ end = centre + half;
766
+ } else if (rounding) {
767
+ start = rounding.start.getTime();
768
+ end = rounding.end.getTime();
769
+ } else {
770
+ throw new RangeError("A window needs start and end, at and minutes, or a rounding model.");
771
+ }
772
+ if (!(end > start)) throw new RangeError("The window must end after it starts.");
773
+ if (end - start > MAX_WINDOW_MS) throw new RangeError("The window must be at most 48 hours long.");
774
+ if (!(start >= SPAN_FROM && end <= SPAN_TO)) {
775
+ throw new RangeError("The window must lie inside REFERENCE_SPAN, 1800 to 2200.");
776
+ }
777
+ if (rounding && (rounding.start.getTime() < start || rounding.end.getTime() > end)) {
778
+ throw new RangeError("The rounding model's unit must lie inside the window.");
779
+ }
780
+ return { start, end, rounding };
781
+ }
782
+ function features(values, system) {
783
+ const signs = {};
784
+ const houses = {};
785
+ BODIES.forEach((body, index) => {
786
+ const sign = values[index];
787
+ const house = values[HOUSE + index];
788
+ signs[body] = sign === UNRESOLVED ? null : SIGN_SLUGS[sign];
789
+ houses[body] = house === UNRESOLVED ? null : house;
790
+ });
791
+ const aspects = [];
792
+ PAIRS.forEach(([first, second], pair) => {
793
+ const type = values[ASPECT + pair];
794
+ if (type >= 0) aspects.push({ a: BODIES[first], b: BODIES[second], type: ASPECT_TYPES[type] });
795
+ });
796
+ return {
797
+ signs,
798
+ ascendant: SIGN_SLUGS[values[ASC]],
799
+ midheaven: SIGN_SLUGS[values[MC]],
800
+ houses,
801
+ aspects,
802
+ houseSystem: values[SYSTEM] === 1 ? "whole" : system
803
+ };
804
+ }
805
+ function change(id, from, to, system) {
806
+ const sign = (value) => value === UNRESOLVED ? null : SIGN_SLUGS[value];
807
+ const house = (value) => value === UNRESOLVED ? null : value;
808
+ if (id < ASC) return { feature: "sign", body: BODIES[id], from: sign(from), to: sign(to) };
809
+ if (id === ASC || id === MC) {
810
+ return { feature: id === ASC ? "ascendant" : "midheaven", from: SIGN_SLUGS[from], to: SIGN_SLUGS[to] };
811
+ }
812
+ if (id < ASPECT) return { feature: "house", body: BODIES[id - HOUSE], from: house(from), to: house(to) };
813
+ if (id < SYSTEM) {
814
+ const [first, second] = PAIRS[id - ASPECT];
815
+ return {
816
+ feature: "aspect",
817
+ a: BODIES[first],
818
+ b: BODIES[second],
819
+ from: from < 0 ? null : ASPECT_TYPES[from],
820
+ to: to < 0 ? null : ASPECT_TYPES[to]
821
+ };
822
+ }
823
+ return { feature: "house-system", from: from === 1 ? "whole" : system, to: to === 1 ? "whole" : system };
824
+ }
825
+ function birthWindow(input) {
826
+ if (!input || typeof input !== "object") throw new RangeError("input must be an object.");
827
+ const settings = validateBirthSettings({
828
+ latitude: input.latitude,
829
+ longitude: input.longitude,
830
+ ...input.houseSystem === void 0 ? {} : { houseSystem: input.houseSystem }
831
+ });
832
+ if (settings.latitude === void 0 || settings.longitude === void 0) {
833
+ throw new RangeError("A window needs a latitude and a longitude.");
834
+ }
835
+ const latitude = settings.latitude;
836
+ const longitude = settings.longitude;
837
+ if (Math.abs(latitude) > 90 - POLE_MARGIN) {
838
+ throw new RangeError("latitude must be at least 1e-6 degrees from either pole.");
839
+ }
840
+ const system = settings.houseSystem ?? "whole";
841
+ const { start, end, rounding } = resolvedWindow(input);
842
+ const sky = new Sky(latitude, longitude, system, seamsIn(start, end));
843
+ const ids = Array.from({ length: SYSTEM + 1 }, (_, id) => id).filter(
844
+ (id) => id !== SYSTEM || isPolarUndefinedHouseSystem(system)
845
+ );
846
+ const last = end - 1;
847
+ const bands = nodeBands(sky, start, last).map(({ a, b, cost }) => {
848
+ const until = b >= last ? end : b;
849
+ const top = Math.min(until, last);
850
+ const whole = system === "whole" || isPolarUndefinedHouseSystem(system) && (sky.angles(a).fellBack || sky.angles(top).fellBack || new Interval(sky, a, top).mayChange(SYSTEM));
851
+ return { a, b: until, ids: whole ? [NORTH, SOUTH, HOUSE + NORTH, HOUSE + SOUTH] : [NORTH, SOUTH], cost };
852
+ });
853
+ const resume = bands.map((band) => band.b < end ? band.ids.map((id) => sky.value(band.b, id)) : []);
854
+ const values = Array.from({ length: SYSTEM + 1 }, (_, id) => id === SYSTEM && !ids.includes(SYSTEM) ? 0 : sky.value(start, id));
855
+ const changes = [];
856
+ if (last > start) search(sky, start, last, ids, changes, bands);
857
+ const unresolved = [];
858
+ for (const index of bands.map((_, index2) => index2).sort((x, y) => bands[x].cost - bands[y].cost)) {
859
+ const band = bands[index];
860
+ const from = Math.max(band.a - 1, start);
861
+ const to = Math.min(band.b, last);
862
+ const remaining = MAX_EVALUATIONS - sky.evaluations;
863
+ if (to - from + 1 <= remaining || band.cost + 1e3 <= remaining) {
864
+ const found = [];
865
+ try {
866
+ search(sky, from, to, band.ids, found, []);
867
+ changes.push(...found);
868
+ continue;
869
+ } catch (error) {
870
+ if (!(error instanceof WindowBudgetError)) throw error;
871
+ }
872
+ }
873
+ unresolved.push(index);
874
+ }
875
+ unresolved.sort((x, y) => bands[x].a - bands[y].a);
876
+ for (const index of unresolved) {
877
+ const band = bands[index];
878
+ band.ids.forEach((id, position) => {
879
+ if (band.a === start) values[id] = UNRESOLVED;
880
+ else changes.push({ at: band.a, id, from: Number.NaN, to: UNRESOLVED });
881
+ if (band.b < end) changes.push({ at: band.b, id, from: UNRESOLVED, to: resume[index][position] });
882
+ });
883
+ }
884
+ changes.sort((a, b) => a.at - b.at || a.id - b.id);
885
+ const cells = [];
886
+ const switches = [];
887
+ const length = end - start;
888
+ const support = rounding ? [rounding.start.getTime(), rounding.end.getTime()] : null;
889
+ let cellStart = start;
890
+ let fallback = values[SYSTEM] === 1;
891
+ const close = (cellEnd) => {
892
+ const milliseconds = cellEnd - cellStart;
893
+ const overlap = support && Math.max(0, Math.min(cellEnd, support[1]) - Math.max(cellStart, support[0]));
894
+ cells.push({
895
+ start: new Date(cellStart),
896
+ end: new Date(cellEnd),
897
+ milliseconds,
898
+ share: milliseconds / length,
899
+ features: features(values, system),
900
+ ...support ? { roundedShare: overlap / (support[1] - support[0]) } : {}
901
+ });
902
+ cellStart = cellEnd;
903
+ };
904
+ for (let index = 0; index < changes.length; ) {
905
+ const at = changes[index].at;
906
+ const list = [];
907
+ const updates = [];
908
+ for (; index < changes.length && changes[index].at === at; index += 1) {
909
+ const entry = changes[index];
910
+ const from = Number.isNaN(entry.from) ? values[entry.id] : entry.from;
911
+ if (values[entry.id] !== from) sky.violations += 1;
912
+ if (from !== entry.to) list.push(change(entry.id, from, entry.to, system));
913
+ updates.push(entry);
914
+ }
915
+ if (list.length === 0) continue;
916
+ close(at);
917
+ for (const { id, to } of updates) values[id] = to;
918
+ if (values[SYSTEM] === 1) fallback = true;
919
+ switches.push({ at: new Date(at), changes: list });
920
+ }
921
+ close(end);
922
+ const flags = [];
923
+ if (fallback) flags.push("polar-fallback");
924
+ if (sky.violations > 0) flags.push("bound-exceeded");
925
+ if (unresolved.length > 0) flags.push("node-unresolved");
926
+ return {
927
+ schema: BIRTH_WINDOW_SCHEMA,
928
+ verification: WINDOW_VERIFICATION,
929
+ start: new Date(start),
930
+ end: new Date(end),
931
+ latitude,
932
+ longitude,
933
+ houseSystem: system,
934
+ rounding,
935
+ cells,
936
+ switches,
937
+ unresolved: unresolved.map((index) => {
938
+ const { a, b, ids: bandIds } = bands[index];
939
+ return {
940
+ start: new Date(a),
941
+ end: new Date(b),
942
+ milliseconds: b - a,
943
+ features: bandIds.map((id) => ({
944
+ feature: id < ASC ? "sign" : "house",
945
+ body: BODIES[id < ASC ? id : id - HOUSE]
946
+ }))
947
+ };
948
+ }),
949
+ flags,
950
+ engineVersion: ENGINE_VERSION
951
+ };
952
+ }
953
+ export {
954
+ BIRTH_WINDOW_SCHEMA,
955
+ MAX_WINDOW_MS,
956
+ WINDOW_RATE_BOUNDS,
957
+ WINDOW_VERIFICATION,
958
+ WindowBudgetError,
959
+ birthWindow
960
+ };