@johnmorrisdotca/chikyu-earth 0.34.0 → 0.35.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.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/element.js +1 -1
- package/dist/realistic/earth.js +1 -1
- package/dist/realistic/element.d.ts +19 -0
- package/dist/realistic/element.js +27 -2
- package/dist/realistic/index.d.ts +1 -0
- package/dist/sky/cover.d.ts +48 -0
- package/dist/sky/cover.js +100 -0
- package/dist/sky/culture-lines.d.ts +56 -0
- package/dist/sky/culture-lines.js +82 -0
- package/dist/sky/figure-pick.d.ts +75 -0
- package/dist/sky/figure-pick.js +200 -0
- package/dist/sky/host.d.ts +17 -1
- package/dist/sky/host.js +633 -59
- package/dist/sky/keep-clear.d.ts +63 -0
- package/dist/sky/keep-clear.js +55 -0
- package/dist/space/element.d.ts +24 -3
- package/dist/space/element.js +30 -3
- package/dist/space/index.d.ts +1 -1
- package/dist/space/index.js +1 -1
- package/dist/space/satellite-model.d.ts +22 -0
- package/dist/space/satellite-model.js +38 -0
- package/dist/space/scene.d.ts +5 -2
- package/dist/space/scene.js +38 -17
- package/package.json +1 -1
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// Where a reader's pointer is on the constellation figures, and where a figure's name goes. Pure: the lines are a list of segments (x0, y0, x1, y1 in the picture's pixels), a figure being a run of them.
|
|
2
|
+
/**
|
|
3
|
+
* The squared distance from a point to the nearest segment of a figure.
|
|
4
|
+
*
|
|
5
|
+
* @param segments - The segments, four numbers each.
|
|
6
|
+
* @param start - The figure's first segment.
|
|
7
|
+
* @param end - One past its last.
|
|
8
|
+
* @param x - The point, x.
|
|
9
|
+
* @param y - The point, y.
|
|
10
|
+
* @returns The squared distance to the nearest, or infinity where the figure has none.
|
|
11
|
+
*/
|
|
12
|
+
export function nearestSegment(segments, start, end, x, y) {
|
|
13
|
+
let best = Number.POSITIVE_INFINITY;
|
|
14
|
+
for (let s = start; s < end; s += 1) {
|
|
15
|
+
const at = s * 4;
|
|
16
|
+
const x0 = segments[at];
|
|
17
|
+
const y0 = segments[at + 1];
|
|
18
|
+
const dx = segments[at + 2] - x0;
|
|
19
|
+
const dy = segments[at + 3] - y0;
|
|
20
|
+
const length = dx * dx + dy * dy;
|
|
21
|
+
const t = length === 0 ? 0 : Math.max(0, Math.min(1, ((x - x0) * dx + (y - y0) * dy) / length));
|
|
22
|
+
const d = (x0 + t * dx - x) ** 2 + (y0 + t * dy - y) ** 2;
|
|
23
|
+
if (d < best)
|
|
24
|
+
best = d;
|
|
25
|
+
}
|
|
26
|
+
return best;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The convex hull of a figure's segment ends (Andrew's monotone chain), as x y pairs: made once for a figure and kept while the picture holds, then asked of with `areaInHull` at each pointer.
|
|
30
|
+
*
|
|
31
|
+
* @param segments - The segments, four numbers each.
|
|
32
|
+
* @param start - The figure's first segment.
|
|
33
|
+
* @param end - One past its last.
|
|
34
|
+
* @returns The hull's corners, x then y for each, round the figure.
|
|
35
|
+
*/
|
|
36
|
+
export function hullOf(segments, start, end) {
|
|
37
|
+
const points = [];
|
|
38
|
+
for (let s = start; s < end; s += 1) {
|
|
39
|
+
points.push([segments[s * 4], segments[s * 4 + 1]], [segments[s * 4 + 2], segments[s * 4 + 3]]);
|
|
40
|
+
}
|
|
41
|
+
points.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
42
|
+
const cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
43
|
+
const chain = [];
|
|
44
|
+
for (const point of points) {
|
|
45
|
+
while (chain.length >= 2 && cross(chain[chain.length - 2], chain[chain.length - 1], point) <= 0)
|
|
46
|
+
chain.pop();
|
|
47
|
+
chain.push(point);
|
|
48
|
+
}
|
|
49
|
+
const floor = chain.length + 1;
|
|
50
|
+
for (let i = points.length - 2; i >= 0; i -= 1) {
|
|
51
|
+
const point = points[i];
|
|
52
|
+
while (chain.length >= floor && cross(chain[chain.length - 2], chain[chain.length - 1], point) <= 0)
|
|
53
|
+
chain.pop();
|
|
54
|
+
chain.push(point);
|
|
55
|
+
}
|
|
56
|
+
chain.pop();
|
|
57
|
+
return chain.flat();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The area of the convex hull of a figure when a point is inside it, or -1 when it is not. A pointer inside a figure (not only on its lines) picks it; where figures overlap, the smaller hull is the one meant.
|
|
61
|
+
*
|
|
62
|
+
* @param segments - The segments, four numbers each.
|
|
63
|
+
* @param start - The figure's first segment.
|
|
64
|
+
* @param end - One past its last.
|
|
65
|
+
* @param x - The point, x.
|
|
66
|
+
* @param y - The point, y.
|
|
67
|
+
* @returns The hull's area where the point is inside it, else -1.
|
|
68
|
+
*/
|
|
69
|
+
export function hullAreaAround(segments, start, end, x, y) {
|
|
70
|
+
return areaInHull(hullOf(segments, start, end), x, y);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The area of a hull when a point is inside it, or -1 when it is not (and for a hull with fewer than three corners).
|
|
74
|
+
*
|
|
75
|
+
* @param h - The hull, from `hullOf`.
|
|
76
|
+
* @param x - The point, x.
|
|
77
|
+
* @param y - The point, y.
|
|
78
|
+
* @returns The area where the point is inside, else -1.
|
|
79
|
+
*/
|
|
80
|
+
export function areaInHull(h, x, y) {
|
|
81
|
+
const n = h.length / 2;
|
|
82
|
+
if (n < 3)
|
|
83
|
+
return -1;
|
|
84
|
+
let area = 0;
|
|
85
|
+
let sign = 0;
|
|
86
|
+
for (let i = 0; i < n; i += 1) {
|
|
87
|
+
const j = (i + 1) % n;
|
|
88
|
+
const ax = h[i * 2];
|
|
89
|
+
const ay = h[i * 2 + 1];
|
|
90
|
+
const bx = h[j * 2];
|
|
91
|
+
const by = h[j * 2 + 1];
|
|
92
|
+
area += ax * by - bx * ay;
|
|
93
|
+
const side = (bx - ax) * (y - ay) - (by - ay) * (x - ax);
|
|
94
|
+
if (side !== 0) {
|
|
95
|
+
if (sign === 0)
|
|
96
|
+
sign = Math.sign(side);
|
|
97
|
+
else if (Math.sign(side) !== sign)
|
|
98
|
+
return -1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return Math.abs(area) / 2;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The visual middle of the part of a figure that is on the picture: the middle of its lines, each counted by its length, taken only where they are inside the picture and not under a body or a place kept clear. Pieces outside the picture, and the parts of a figure that run off it, count for nothing.
|
|
105
|
+
*
|
|
106
|
+
* @param segments - The segments, four numbers each.
|
|
107
|
+
* @param start - The figure's first segment.
|
|
108
|
+
* @param end - One past its last.
|
|
109
|
+
* @param width - The picture's width.
|
|
110
|
+
* @param height - The picture's height.
|
|
111
|
+
* @param blocked - Whether a point of the picture is on a body or in a place kept clear, or `undefined` where none is.
|
|
112
|
+
* @param out - Where to write the middle.
|
|
113
|
+
* @returns True where any of the figure is in view, with `out` written.
|
|
114
|
+
*/
|
|
115
|
+
export function visibleCentre(segments, start, end, width, height, blocked, out) {
|
|
116
|
+
let sx = 0;
|
|
117
|
+
let sy = 0;
|
|
118
|
+
let sum = 0;
|
|
119
|
+
for (let s = start; s < end; s += 1) {
|
|
120
|
+
const at = s * 4;
|
|
121
|
+
const x0 = segments[at];
|
|
122
|
+
const y0 = segments[at + 1];
|
|
123
|
+
const dx = segments[at + 2] - x0;
|
|
124
|
+
const dy = segments[at + 3] - y0;
|
|
125
|
+
// Liang and Barsky: the part of the segment inside the picture, as parameters t0 to t1
|
|
126
|
+
let t0 = 0;
|
|
127
|
+
let t1 = 1;
|
|
128
|
+
let inside = true;
|
|
129
|
+
for (let side = 0; side < 4 && inside; side += 1) {
|
|
130
|
+
const p = side === 0 ? -dx : side === 1 ? dx : side === 2 ? -dy : dy;
|
|
131
|
+
const q = side === 0 ? x0 : side === 1 ? width - x0 : side === 2 ? y0 : height - y0;
|
|
132
|
+
if (p === 0) {
|
|
133
|
+
if (q < 0)
|
|
134
|
+
inside = false;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
const r = q / p;
|
|
138
|
+
if (p < 0) {
|
|
139
|
+
if (r > t1)
|
|
140
|
+
inside = false;
|
|
141
|
+
else if (r > t0)
|
|
142
|
+
t0 = r;
|
|
143
|
+
}
|
|
144
|
+
else if (r < t0)
|
|
145
|
+
inside = false;
|
|
146
|
+
else if (r < t1)
|
|
147
|
+
t1 = r;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!inside || t1 <= t0)
|
|
151
|
+
continue;
|
|
152
|
+
const mid = (t0 + t1) / 2;
|
|
153
|
+
const mx = x0 + dx * mid;
|
|
154
|
+
const my = y0 + dy * mid;
|
|
155
|
+
if (blocked?.(mx, my))
|
|
156
|
+
continue;
|
|
157
|
+
const length = (t1 - t0) * Math.hypot(dx, dy) || 0.01;
|
|
158
|
+
sx += mx * length;
|
|
159
|
+
sy += my * length;
|
|
160
|
+
sum += length;
|
|
161
|
+
}
|
|
162
|
+
if (sum <= 0)
|
|
163
|
+
return false;
|
|
164
|
+
out.x = sx / sum;
|
|
165
|
+
out.y = sy / sum;
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
/** Offsets tried, in steps of the box's own width (across) and height (down), nearest first. */
|
|
169
|
+
const ACROSS = [0, -0.4, 0.4, -0.8, 0.8, -1.2, 1.2, -1.6, 1.6, -2.2, 2.2];
|
|
170
|
+
const DOWN = [0, -1.2, 1.2, -2.4, 2.4, -3.6, 3.6, -4.8, 4.8];
|
|
171
|
+
/**
|
|
172
|
+
* Where a box goes: its middle at a point, or the nearest place round the point where it is inside the picture and `free` says it can stand.
|
|
173
|
+
*
|
|
174
|
+
* @param x - The wanted middle, x.
|
|
175
|
+
* @param y - The wanted middle, y.
|
|
176
|
+
* @param w - The box's width.
|
|
177
|
+
* @param h - The box's height.
|
|
178
|
+
* @param width - The picture's width.
|
|
179
|
+
* @param height - The picture's height.
|
|
180
|
+
* @param free - Whether a box (left, top, right, bottom) may stand there.
|
|
181
|
+
* @param out - Where to write the middle of the place found.
|
|
182
|
+
* @returns True where a place was found.
|
|
183
|
+
*/
|
|
184
|
+
export function placeBox(x, y, w, h, width, height, free, out) {
|
|
185
|
+
const margin = 4;
|
|
186
|
+
let best = Number.POSITIVE_INFINITY;
|
|
187
|
+
for (const down of DOWN) {
|
|
188
|
+
for (const across of ACROSS) {
|
|
189
|
+
const cx = Math.max(margin + w / 2, Math.min(width - margin - w / 2, x + across * w));
|
|
190
|
+
const cy = Math.max(margin + h / 2, Math.min(height - margin - h / 2, y + down * h));
|
|
191
|
+
const away = Math.abs(cx - x) + Math.abs(cy - y);
|
|
192
|
+
if (away < best && free(cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2)) {
|
|
193
|
+
best = away;
|
|
194
|
+
out.x = cx;
|
|
195
|
+
out.y = cy;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return best !== Number.POSITIVE_INFINITY;
|
|
200
|
+
}
|
package/dist/sky/host.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Culture } from "@johnmorrisdotca/hoshi-stars/culture";
|
|
2
|
+
import { type SkyKeepClear } from "./keep-clear.ts";
|
|
1
3
|
import type { SkyFrame } from "./frame.ts";
|
|
2
4
|
import type { SkyLabelStyle } from "./style.ts";
|
|
3
5
|
/** The styles of the layer, for an element to add to its own (`#sk-canvas`, `#sk-names` and the labels in its shadow root). */
|
|
@@ -7,14 +9,24 @@ export interface SkyHost {
|
|
|
7
9
|
frame(frame: SkyFrame | null): void;
|
|
8
10
|
/** What the settings are now (`star-names`, `star-names-magnitude`, `constellation-lines`, the language): read again after any of them changed. */
|
|
9
11
|
refresh(): void;
|
|
12
|
+
/** The places where the sky's names must not go (the property `keepClear`): rectangles in the element's pixels, or elements of the page. `null` for none. */
|
|
13
|
+
setKeepClear(areas: readonly SkyKeepClear[] | null): void;
|
|
10
14
|
/** The look of the names, as a partial hoshi-stars `LabelStyle` (the property `skyStyle`). */
|
|
11
15
|
setStyle(style: Partial<SkyLabelStyle>): void;
|
|
16
|
+
/** Picks a constellation by its IAU abbreviation (`Ori`), or lets it go (`null`): the property `constellation`. Sends no event. */
|
|
17
|
+
pick(abbreviation: string | null): void;
|
|
18
|
+
/** The picked constellation's IAU abbreviation, or `null`. */
|
|
19
|
+
readonly picked: string | null;
|
|
12
20
|
readonly stats: {
|
|
13
21
|
on: boolean;
|
|
14
22
|
loaded: boolean;
|
|
15
23
|
names: number;
|
|
16
24
|
figures: number;
|
|
17
25
|
picked: string | null;
|
|
26
|
+
pick: string | null;
|
|
27
|
+
hover: string | null;
|
|
28
|
+
constellations: number;
|
|
29
|
+
culture: string;
|
|
18
30
|
renders: number;
|
|
19
31
|
lang: string;
|
|
20
32
|
stars: {
|
|
@@ -38,9 +50,13 @@ export interface SkyHostOptions {
|
|
|
38
50
|
loadFont(): Promise<unknown>;
|
|
39
51
|
/** Brings hoshi-stars' `sky-labels` in: a dynamic import of the package by default, a page's own (or a test's) where it has one. */
|
|
40
52
|
importer?: () => Promise<typeof import("@johnmorrisdotca/hoshi-stars/sky-labels")>;
|
|
53
|
+
/** Brings hoshi-stars' `constellations` in, for the names of the figures: a dynamic import by default. */
|
|
54
|
+
constellations?: () => Promise<typeof import("@johnmorrisdotca/hoshi-stars/constellations")>;
|
|
55
|
+
/** Brings a sky culture of hoshi-stars in (`western`, `japanese` or `chinese`): a dynamic import of the culture by default, a page's own (or a test's) where it has one. */
|
|
56
|
+
cultures?: (id: string) => Promise<Culture>;
|
|
41
57
|
}
|
|
42
58
|
/** The attributes this layer reads. */
|
|
43
|
-
export declare const SKY_ATTRIBUTES: readonly ["star-names", "star-names-magnitude", "constellation-lines"];
|
|
59
|
+
export declare const SKY_ATTRIBUTES: readonly ["star-names", "star-names-magnitude", "constellation-lines", "constellation-names", "constellation-culture", "keep-clear"];
|
|
44
60
|
/**
|
|
45
61
|
* Makes the layer of one element, inside its shadow root. Nothing of hoshi-stars is loaded, and nothing is added to the root, until one of the three attributes is there.
|
|
46
62
|
*
|