astraeus-data-constellations 0.9.3 → 0.9.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astraeus-data-constellations",
3
- "version": "0.9.3",
3
+ "version": "0.9.5",
4
4
  "description": "The 88 IAU constellations for the Astraeus engine: stick figures, boundary outlines in J2000 and names. Optional data; the engine draws without it.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/finder.js CHANGED
@@ -1,4 +1,3 @@
1
- /** Tolerance, in radians (1e-9, about 0.2 milliarcseconds), within which a direction counts as lying on a boundary edge. */
2
1
  const EDGE_EPS = 1e-9;
3
2
  const RAD = Math.PI / 180;
4
3
  const unit = (raDeg, decDeg) => {
@@ -20,7 +19,6 @@ const buildLoop = (abbr, flat) => {
20
19
  raDegs.push(flat[i]);
21
20
  decSum += flat[i + 1];
22
21
  }
23
- // A loop that goes once round the polar axis (its right ascension winds by 360 degrees) encloses a pole: the one on the side of its mean declination.
24
22
  let wind = 0;
25
23
  for (let i = 0; i < raDegs.length; i++) {
26
24
  let step = raDegs[(i + 1) % raDegs.length] - raDegs[i];
@@ -34,11 +32,9 @@ const buildLoop = (abbr, flat) => {
34
32
  const normals = points.map((a, i) => normalize(cross(a, points[(i + 1) % points.length])));
35
33
  return { abbr, points, normals, pole: !circles ? "none" : decSum > 0 ? "north" : "south" };
36
34
  };
37
- /** Does the loop enclose the direction? Ray casting along the direction's meridian up to the north pole, edges as great-circle arcs; crossings are counted half-open so a vertex on the ray counts once. */
38
35
  const contains = (loop, p, raDeg) => {
39
36
  const c = Math.cos(raDeg * RAD);
40
37
  const s = Math.sin(raDeg * RAD);
41
- // The meridian plane's normal and the half plane (the direction's own right ascension) the ray lies in.
42
38
  const plane = [s, -c, 0];
43
39
  let crossings = 0;
44
40
  const n = loop.points.length;
@@ -49,14 +45,12 @@ const contains = (loop, p, raDeg) => {
49
45
  const db = dot(b, plane);
50
46
  if (da >= 0 === db >= 0)
51
47
  continue;
52
- // The point of the arc on the plane: the convex mix of the ends that cancels the signed distances.
53
48
  const x = [a[0] * Math.abs(db) + b[0] * Math.abs(da), a[1] * Math.abs(db) + b[1] * Math.abs(da), a[2] * Math.abs(db) + b[2] * Math.abs(da)];
54
49
  if (x[0] * c + x[1] * s > 0 && x[2] / Math.hypot(x[0], x[1], x[2]) > p[2])
55
50
  crossings++;
56
51
  }
57
52
  return (crossings % 2 === 1) !== (loop.pole === "north");
58
53
  };
59
- /** Angular distance, in radians, from the direction to the loop's nearest edge. */
60
54
  const edgeDistance = (loop, p) => {
61
55
  let best = Infinity;
62
56
  const n = loop.points.length;
@@ -79,9 +73,7 @@ const checked = (direction) => {
79
73
  if (typeof direction.decDeg !== "number" || !Number.isFinite(direction.decDeg) || direction.decDeg < -90 || direction.decDeg > 90)
80
74
  throw new RangeError(`constellationFinder: decDeg must be a number from -90 to 90, got ${String(direction.decDeg)}`);
81
75
  };
82
- /** Builds the finder from boundary outlines (`loadBoundaries()`'s result). */
83
76
  export const buildMatcher = (boundaries) => {
84
- // Alphabetical, so every tie below resolves to the first abbreviation in this order.
85
77
  const loops = Object.keys(boundaries).sort().flatMap((abbr) => (boundaries[abbr] ?? []).map((flat) => buildLoop(abbr, flat)));
86
78
  const prepare = (direction) => {
87
79
  checked(direction);
@@ -90,9 +82,6 @@ export const buildMatcher = (boundaries) => {
90
82
  };
91
83
  const find = (direction) => {
92
84
  const { p, raDeg } = prepare(direction);
93
- // 1. On an edge (within EDGE_EPS): the first abbreviation of the constellations whose edge it lies on.
94
- // 2. Else inside a loop: that loop's abbreviation (the first, should rounding give more than one).
95
- // 3. Else (rounding left it in no loop): the constellation of the nearest edge, the first on a tie.
96
85
  let inside;
97
86
  for (const loop of loops) {
98
87
  if (edgeDistance(loop, p) <= EDGE_EPS)
@@ -119,5 +108,4 @@ export const buildMatcher = (boundaries) => {
119
108
  };
120
109
  return { find, enclosing };
121
110
  };
122
- /** Builds the finder from boundary outlines (`loadBoundaries()`'s result). */
123
111
  export const buildFinder = (boundaries) => buildMatcher(boundaries).find;
package/src/index.js CHANGED
@@ -1,26 +1,13 @@
1
1
  import { buildFinder } from "./finder.js";
2
- // A dynamic import keeps each file its own chunk, so a bundler loads only what the app asks for (as astraeus-data-pluto does for its index).
3
2
  const read = async (load) => {
4
3
  const loaded = (await load());
5
- return loaded.default ?? loaded; // Parcel hands back the JSON itself, Vite and webpack a module with a default
4
+ return loaded.default ?? loaded;
6
5
  };
7
- /** The stick figures of all 88 constellations (IAU, as drawn by Sky & Telescope). */
8
6
  export const loadFigures = () => read(() => import("../data/figures.json", { with: { type: "json" } }));
9
- /** The boundary outlines of all 88 constellations in J2000 (Delporte, 1930). */
10
7
  export const loadBoundaries = () => read(() => import("../data/boundaries.json", { with: { type: "json" } }));
11
- /** The IAU names of all 88 constellations, by abbreviation. */
12
8
  export const loadNames = () => read(() => import("../data/names.json", { with: { type: "json" } }));
13
- /** Sources, licences, changes and the boundary cross-check. */
14
9
  export const loadProvenance = () => read(() => import("../data/provenance.json", { with: { type: "json" } }));
15
10
  let finder;
16
- /**
17
- * Which of the 88 constellations holds a J2000 direction. Loads the boundary outlines once (later calls reuse them) and resolves to a synchronous function giving the IAU abbreviation (`"Ori"`); both Serpens loops answer `"Ser"`.
18
- * Right ascension is taken modulo 360; a declination outside -90..90 or a non-finite number throws a `RangeError` naming the field.
19
- * A direction exactly on a shared edge (within 1e-9 radian) answers the first abbreviation in alphabetical order of the constellations that edge touches.
20
- * @example
21
- * const which = await constellationFinder();
22
- * which({ raDeg: 88.79, decDeg: 7.41 }); // "Ori"
23
- */
24
11
  export const constellationFinder = () => {
25
12
  finder ??= loadBoundaries().then(buildFinder, (error) => { finder = undefined; throw error; });
26
13
  return finder;
@@ -35,17 +22,7 @@ const FIGURE_SOURCES = ["IAU constellation figures (drawn by Sky & Telescope), v
35
22
  const BOUNDARY_SOURCES = ["Constellation boundaries: Delporte (1930), via Stellarium skycultures/modern_iau, CC BY-SA 4.0, precessed to J2000", "Names: IAU, CC BY 4.0"];
36
23
  const SERPENS_PARTS = ["Caput", "Cauda"];
37
24
  const endOf = (end) => (typeof end === "number" ? { star: end } : { raDeg: end[0], decDeg: end[1] });
38
- /**
39
- * Ready sky figures (and, when asked, boundary outlines) for constellations, to give to `scene.addSkyFigures`. Pass IAU abbreviations (`["Ori", "UMa"]`) or `"all"` for the 88.
40
- * Ids are the abbreviation for a figure (`"Ori"`) and `"Ori-boundary"` for its outline; Serpens' two outlines are `"Ser-boundary-1"` (Caput) and `"Ser-boundary-2"` (Cauda). `idPrefix` goes before every id.
41
- * An abbreviation that is unknown or repeated throws a `RangeError` naming it. Cards carry provenance and no facts; a story app adds those.
42
- * @example
43
- * const { constellations } = await import("astraeus-data-constellations");
44
- * scene.addSkyFigures(await constellations(["Ori", "Tau"], { shown: "whenTraced" }));
45
- * scene.addSkyFigures(await constellations("all", { outlines: true })); // all 88, with outlines
46
- */
47
25
  export const constellations = async (which, options = {}) => {
48
- // The outlines are the biggest file (about 150 kB raw), so an app that asks for none never downloads it.
49
26
  const [figures, boundaries, names] = await Promise.all([loadFigures(), options.outlines === true ? loadBoundaries() : Promise.resolve({}), loadNames()]);
50
27
  const abbrs = which === "all" ? Object.keys(names).sort() : which;
51
28
  const seen = new Set();