@johnmorrisdotca/wakusei-planets 0.62.0 → 0.64.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 +39 -1
- package/README.md +4 -3
- package/dist/asteroids.d.ts +15 -1
- package/dist/asteroids.js +28 -0
- package/dist/door/made.d.ts +4 -0
- package/dist/door/made.js +10 -0
- package/dist/door/own.d.ts +1 -4
- package/dist/door/own.js +1 -9
- package/dist/element.js +19 -22
- package/dist/fact-sheets.d.ts +103 -0
- package/dist/fact-sheets.js +135 -0
- package/dist/flat/index.d.ts +1 -0
- package/dist/flat/index.js +1 -0
- package/dist/flat/projections.d.ts +300 -0
- package/dist/flat/projections.js +414 -0
- package/dist/scene/tumble.d.ts +13 -0
- package/dist/scene/tumble.js +34 -0
- package/dist/sky/events.d.ts +2 -0
- package/dist/sky/events.js +3 -2
- package/dist/sky.d.ts +561 -0
- package/dist/sky.js +522 -0
- package/dist/space/asteroid-ids.d.ts +8 -0
- package/dist/space/asteroid-ids.js +19 -0
- package/dist/space/element.d.ts +63 -6
- package/dist/space/element.js +208 -26
- package/dist/space/scene.d.ts +32 -0
- package/dist/space/scene.js +36 -2
- package/dist/suncalc-legacy.d.ts +21 -0
- package/dist/suncalc-legacy.js +13 -0
- package/dist/suncalc.d.ts +21 -0
- package/dist/suncalc.js +23 -8
- package/dist/surface/element.d.ts +1 -1
- package/dist/surface/element.js +4 -2
- package/package.json +15 -1
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// Map projections for the painted maps: the sphere of any body flattened five ways, with the graticule, the outline and the Sun's mark, and a resampler that turns the body's own equirectangular picture into the projected one.
|
|
2
|
+
// Pure: numbers and pixel arrays in, numbers and pixel arrays out. No DOM, no clock reads, no dependency. Every map here is on the unit sphere: x grows east, y grows north, in radii.
|
|
3
|
+
import { subSolarPoint } from "../places.js";
|
|
4
|
+
import { wrapLongitude } from "./geometry.js";
|
|
5
|
+
const RAD = Math.PI / 180;
|
|
6
|
+
const HALF_PI = Math.PI / 2;
|
|
7
|
+
/**
|
|
8
|
+
* The projections: `equirectangular` (the picture as drawn, a degree is a degree both ways), `mercator` (round, angles kept true; it stops at 85.05 degrees because the poles are at infinity), `sinusoidal` (equal area, the parallels keep their true length, the shape is a leaf), `mollweide` (equal area, an ellipse twice as wide as tall) and `azimuthal` (equidistant: the whole sphere as a disc about a chosen point, distances from that point true; the opposite point is the rim).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { PROJECTIONS } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
13
|
+
*
|
|
14
|
+
* console.log(PROJECTIONS.join(", "));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export const PROJECTIONS = ["equirectangular", "mercator", "sinusoidal", "mollweide", "azimuthal"];
|
|
18
|
+
/**
|
|
19
|
+
* The furthest north or south the Mercator map goes, in degrees: where it becomes a square.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { MERCATOR_LIMIT } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
24
|
+
*
|
|
25
|
+
* console.log(MERCATOR_LIMIT.toFixed(5));
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export const MERCATOR_LIMIT = (2 * Math.atan(Math.exp(Math.PI)) - HALF_PI) / RAD;
|
|
29
|
+
const NO_CENTRE = { lon: 0, lat: 0 };
|
|
30
|
+
const finite = (n, fallback = 0) => (Number.isFinite(n) ? n : fallback);
|
|
31
|
+
/**
|
|
32
|
+
* Whether a name is one of the projections.
|
|
33
|
+
*
|
|
34
|
+
* @param value - Any value.
|
|
35
|
+
* @returns True for a name in `PROJECTIONS`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { isProjection } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
40
|
+
*
|
|
41
|
+
* console.log(isProjection("mollweide"), isProjection("robinson"));
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export const isProjection = (value) => typeof value === "string" && PROJECTIONS.includes(value);
|
|
45
|
+
/**
|
|
46
|
+
* How far the map reaches from its middle, in radii: the half width and half height of the box that holds it.
|
|
47
|
+
*
|
|
48
|
+
* @param id - The projection.
|
|
49
|
+
* @returns `halfWidth` and `halfHeight`.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { mapExtent } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
54
|
+
*
|
|
55
|
+
* console.log(mapExtent("mollweide"), mapExtent("azimuthal"));
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export function mapExtent(id) {
|
|
59
|
+
switch (id) {
|
|
60
|
+
case "equirectangular":
|
|
61
|
+
return { halfWidth: Math.PI, halfHeight: HALF_PI };
|
|
62
|
+
case "mercator":
|
|
63
|
+
return { halfWidth: Math.PI, halfHeight: Math.PI };
|
|
64
|
+
case "sinusoidal":
|
|
65
|
+
return { halfWidth: Math.PI, halfHeight: HALF_PI };
|
|
66
|
+
case "mollweide":
|
|
67
|
+
return { halfWidth: 2 * Math.SQRT2, halfHeight: Math.SQRT2 };
|
|
68
|
+
case "azimuthal":
|
|
69
|
+
return { halfWidth: Math.PI, halfHeight: Math.PI };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Mollweide's auxiliary angle: 2t + sin 2t = pi sin(lat), by Newton's method, with the poles set apart (the slope is zero there). */
|
|
73
|
+
function mollweideAngle(lat) {
|
|
74
|
+
if (Math.abs(lat) >= HALF_PI - 1e-9)
|
|
75
|
+
return Math.sign(lat) * HALF_PI;
|
|
76
|
+
const target = Math.PI * Math.sin(lat);
|
|
77
|
+
let t = lat;
|
|
78
|
+
for (let i = 0; i < 50; i += 1) {
|
|
79
|
+
const slope = 2 + 2 * Math.cos(2 * t);
|
|
80
|
+
if (slope < 1e-12)
|
|
81
|
+
break;
|
|
82
|
+
const step = (2 * t + Math.sin(2 * t) - target) / slope;
|
|
83
|
+
t -= step;
|
|
84
|
+
if (Math.abs(step) < 1e-14)
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
return Math.max(-HALF_PI, Math.min(HALF_PI, t));
|
|
88
|
+
}
|
|
89
|
+
/** A place by its longitude from the middle (`dlon`, radians, not wrapped) and latitude (radians) on the four maps that keep the equator across the middle. */
|
|
90
|
+
function across(id, dlon, lat) {
|
|
91
|
+
switch (id) {
|
|
92
|
+
case "equirectangular":
|
|
93
|
+
return { x: dlon, y: lat };
|
|
94
|
+
case "mercator": {
|
|
95
|
+
const limit = MERCATOR_LIMIT * RAD;
|
|
96
|
+
const clamped = Math.max(-limit, Math.min(limit, lat));
|
|
97
|
+
return { x: dlon, y: Math.atanh(Math.sin(clamped)) };
|
|
98
|
+
}
|
|
99
|
+
case "sinusoidal":
|
|
100
|
+
return { x: dlon * Math.cos(lat), y: lat };
|
|
101
|
+
case "mollweide": {
|
|
102
|
+
const t = mollweideAngle(lat);
|
|
103
|
+
return { x: ((2 * Math.SQRT2) / Math.PI) * dlon * Math.cos(t), y: Math.SQRT2 * Math.sin(t) };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** The azimuthal equidistant map about a centre: the angular distance and the bearing round to a point of the disc. At the opposite point the bearing is undefined, so it is the bottom of the rim. */
|
|
108
|
+
function disc(dlon, lat, centreLat) {
|
|
109
|
+
const cosC = Math.max(-1, Math.min(1, Math.sin(centreLat) * Math.sin(lat) + Math.cos(centreLat) * Math.cos(lat) * Math.cos(dlon)));
|
|
110
|
+
const c = Math.acos(cosC);
|
|
111
|
+
const sinC = Math.sin(c);
|
|
112
|
+
if (sinC < 1e-12)
|
|
113
|
+
return c < 1 ? { x: 0, y: 0 } : { x: 0, y: -Math.PI };
|
|
114
|
+
const k = c / sinC;
|
|
115
|
+
return { x: k * Math.cos(lat) * Math.sin(dlon), y: k * (Math.cos(centreLat) * Math.sin(lat) - Math.sin(centreLat) * Math.cos(lat) * Math.cos(dlon)) };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Where a longitude and latitude fall on the map, in radii from its middle (x east, y north). Longitude is taken the short way round from the centre, so a place on the far side of the picture's seam lands at the right edge. Mercator holds a latitude beyond 85.05 degrees at 85.05, so a pole is a place on the top edge, never infinity.
|
|
119
|
+
*
|
|
120
|
+
* @param id - The projection.
|
|
121
|
+
* @param lon - Degrees east.
|
|
122
|
+
* @param lat - Degrees north.
|
|
123
|
+
* @param centre - The point in the middle; the Greenwich-like prime meridian on the equator when left out.
|
|
124
|
+
* @returns `x` and `y`.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* import { projectMap } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
129
|
+
*
|
|
130
|
+
* console.log(projectMap("mollweide", 90, 45), projectMap("azimuthal", 0, 90, { lon: 0, lat: 90 }));
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export function projectMap(id, lon, lat, centre = NO_CENTRE) {
|
|
134
|
+
const dlon = wrapLongitude(finite(lon) - finite(centre.lon)) * RAD;
|
|
135
|
+
const phi = Math.max(-90, Math.min(90, finite(lat))) * RAD;
|
|
136
|
+
return id === "azimuthal" ? disc(dlon, phi, Math.max(-90, Math.min(90, finite(centre.lat))) * RAD) : across(id, dlon, phi);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The longitude and latitude at a point of the map: the opposite of `projectMap`.
|
|
140
|
+
*
|
|
141
|
+
* @param id - The projection.
|
|
142
|
+
* @param x - Radii east of the middle.
|
|
143
|
+
* @param y - Radii north of the middle.
|
|
144
|
+
* @param centre - The point in the middle.
|
|
145
|
+
* @returns `lon` in -180 up to 180 and `lat`, or `null` when the point is outside the map's shape (beyond the leaf, the ellipse or the disc).
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { unprojectMap } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
150
|
+
*
|
|
151
|
+
* console.log(unprojectMap("sinusoidal", 0, 0), unprojectMap("mollweide", 2.8, 1.4));
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export function unprojectMap(id, x, y, centre = NO_CENTRE) {
|
|
155
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
156
|
+
return null;
|
|
157
|
+
const lon0 = finite(centre.lon);
|
|
158
|
+
const done = (dlon, lat) => (Math.abs(dlon) > Math.PI + 1e-9 ? null : { lon: wrapLongitude(lon0 + dlon / RAD), lat: lat / RAD });
|
|
159
|
+
switch (id) {
|
|
160
|
+
case "equirectangular":
|
|
161
|
+
return Math.abs(y) > HALF_PI ? null : done(x, y);
|
|
162
|
+
case "mercator":
|
|
163
|
+
return Math.abs(y) > Math.PI ? null : done(x, 2 * Math.atan(Math.exp(y)) - HALF_PI);
|
|
164
|
+
case "sinusoidal": {
|
|
165
|
+
if (Math.abs(y) > HALF_PI)
|
|
166
|
+
return null;
|
|
167
|
+
const cos = Math.cos(y);
|
|
168
|
+
if (cos < 1e-12)
|
|
169
|
+
return Math.abs(x) < 1e-9 ? done(0, y) : null;
|
|
170
|
+
return done(x / cos, y);
|
|
171
|
+
}
|
|
172
|
+
case "mollweide": {
|
|
173
|
+
if (Math.abs(y) > Math.SQRT2)
|
|
174
|
+
return null;
|
|
175
|
+
const t = Math.asin(Math.max(-1, Math.min(1, y / Math.SQRT2)));
|
|
176
|
+
const lat = Math.asin(Math.max(-1, Math.min(1, (2 * t + Math.sin(2 * t)) / Math.PI)));
|
|
177
|
+
const cos = Math.cos(t);
|
|
178
|
+
if (cos < 1e-12)
|
|
179
|
+
return Math.abs(x) < 1e-9 ? done(0, lat) : null;
|
|
180
|
+
return done((Math.PI * x) / (2 * Math.SQRT2 * cos), lat);
|
|
181
|
+
}
|
|
182
|
+
case "azimuthal": {
|
|
183
|
+
const rho = Math.hypot(x, y);
|
|
184
|
+
if (rho > Math.PI)
|
|
185
|
+
return null;
|
|
186
|
+
const lat0 = Math.max(-90, Math.min(90, finite(centre.lat))) * RAD;
|
|
187
|
+
if (rho < 1e-12)
|
|
188
|
+
return { lon: wrapLongitude(lon0), lat: lat0 / RAD };
|
|
189
|
+
const sinC = Math.sin(rho);
|
|
190
|
+
const cosC = Math.cos(rho);
|
|
191
|
+
const lat = Math.asin(Math.max(-1, Math.min(1, cosC * Math.sin(lat0) + (y * sinC * Math.cos(lat0)) / rho)));
|
|
192
|
+
const dlon = Math.atan2(x * sinC, rho * Math.cos(lat0) * cosC - y * Math.sin(lat0) * sinC);
|
|
193
|
+
return { lon: wrapLongitude(lon0 + dlon / RAD), lat: lat / RAD };
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* The lines of longitude and latitude at every `step` degrees, as polylines on the map. The four maps keep a line whole from edge to edge (a meridian on the picture's seam is drawn on both edges); on the azimuthal disc a line is cut where it crosses the rim, so nothing is drawn across the map. The poles on Mercator, where they are at infinity, are not drawn as parallels, and the meridians stop at 85.05 degrees. A parallel at a pole is a point on the sinusoidal and Mollweide maps and is left out.
|
|
199
|
+
*
|
|
200
|
+
* @param id - The projection.
|
|
201
|
+
* @param step - Degrees between lines, 5 to 90 (30 when left out).
|
|
202
|
+
* @param centre - The point in the middle.
|
|
203
|
+
* @returns The lines, the equator and the prime meridian among them.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* import { graticule } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
208
|
+
*
|
|
209
|
+
* const lines = graticule("mollweide", 30);
|
|
210
|
+
* console.log(lines.filter((l) => l.kind === "meridian").length, lines.filter((l) => l.kind === "parallel").length);
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
export function graticule(id, step = 30, centre = NO_CENTRE) {
|
|
214
|
+
const every = Math.min(90, Math.max(5, finite(step, 30)));
|
|
215
|
+
const lon0 = finite(centre.lon);
|
|
216
|
+
const lat0 = finite(centre.lat);
|
|
217
|
+
const out = [];
|
|
218
|
+
const sample = 1;
|
|
219
|
+
const latLimit = id === "mercator" ? MERCATOR_LIMIT : 90;
|
|
220
|
+
const cut = (points) => {
|
|
221
|
+
const pieces = [];
|
|
222
|
+
let piece = [];
|
|
223
|
+
let last = null;
|
|
224
|
+
for (const point of points) {
|
|
225
|
+
// a jump of more than a quarter radius between one-degree samples is the rim of the disc, not a line
|
|
226
|
+
if (last !== null && Math.hypot(point.x - last.x, point.y - last.y) > 0.25) {
|
|
227
|
+
if (piece.length > 1)
|
|
228
|
+
pieces.push(piece);
|
|
229
|
+
piece = [];
|
|
230
|
+
}
|
|
231
|
+
piece.push(point);
|
|
232
|
+
last = point;
|
|
233
|
+
}
|
|
234
|
+
if (piece.length > 1)
|
|
235
|
+
pieces.push(piece);
|
|
236
|
+
return pieces;
|
|
237
|
+
};
|
|
238
|
+
// meridians: every multiple of the step across the 360 degrees from the left edge to the right edge of the map
|
|
239
|
+
const first = Math.ceil((lon0 - 180) / every) * every;
|
|
240
|
+
for (let lon = first; lon <= lon0 + 180 + 1e-9; lon += every) {
|
|
241
|
+
const points = [];
|
|
242
|
+
for (let lat = -latLimit; lat <= latLimit + 1e-9; lat += sample) {
|
|
243
|
+
const phi = Math.min(latLimit, lat) * RAD;
|
|
244
|
+
points.push(id === "azimuthal" ? disc(wrapLongitude(lon - lon0) * RAD, phi, lat0 * RAD) : across(id, (lon - lon0) * RAD, phi));
|
|
245
|
+
}
|
|
246
|
+
out.push({ kind: "meridian", value: wrapLongitude(lon), lines: cut(points) });
|
|
247
|
+
}
|
|
248
|
+
// parallels: the equator and every multiple of the step out from it, short of the poles (a point on two of the maps, at infinity on the third)
|
|
249
|
+
const parallel = (lat) => {
|
|
250
|
+
const points = [];
|
|
251
|
+
for (let lon = lon0 - 180; lon <= lon0 + 180 + 1e-9; lon += sample)
|
|
252
|
+
points.push(id === "azimuthal" ? disc(wrapLongitude(lon - lon0) * RAD, lat * RAD, lat0 * RAD) : across(id, (lon - lon0) * RAD, lat * RAD));
|
|
253
|
+
out.push({ kind: "parallel", value: lat, lines: cut(points) });
|
|
254
|
+
};
|
|
255
|
+
parallel(0);
|
|
256
|
+
for (let lat = every; lat < Math.min(90, latLimit) - 1e-9; lat += every) {
|
|
257
|
+
parallel(lat);
|
|
258
|
+
parallel(-lat);
|
|
259
|
+
}
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* The edge of the map as one closed line, for a frame: the rectangle, the square, the leaf, the ellipse or the disc.
|
|
264
|
+
*
|
|
265
|
+
* @param id - The projection.
|
|
266
|
+
* @returns The points, the first repeated at the end.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* import { mapOutline } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
271
|
+
*
|
|
272
|
+
* console.log(mapOutline("sinusoidal").length > 100);
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
export function mapOutline(id) {
|
|
276
|
+
if (id === "azimuthal") {
|
|
277
|
+
const ring = [];
|
|
278
|
+
for (let a = 0; a < 360; a += 2)
|
|
279
|
+
ring.push({ x: Math.PI * Math.sin(a * RAD), y: Math.PI * Math.cos(a * RAD) });
|
|
280
|
+
ring.push({ ...ring[0] });
|
|
281
|
+
return ring;
|
|
282
|
+
}
|
|
283
|
+
if (id === "equirectangular" || id === "mercator") {
|
|
284
|
+
const { halfWidth, halfHeight } = mapExtent(id);
|
|
285
|
+
return [
|
|
286
|
+
{ x: -halfWidth, y: halfHeight },
|
|
287
|
+
{ x: halfWidth, y: halfHeight },
|
|
288
|
+
{ x: halfWidth, y: -halfHeight },
|
|
289
|
+
{ x: -halfWidth, y: -halfHeight },
|
|
290
|
+
{ x: -halfWidth, y: halfHeight },
|
|
291
|
+
];
|
|
292
|
+
}
|
|
293
|
+
// the leaf and the ellipse: down the east edge, then up the west
|
|
294
|
+
const points = [];
|
|
295
|
+
for (let lat = 90; lat >= -90; lat -= 1)
|
|
296
|
+
points.push(across(id, Math.PI, lat * RAD));
|
|
297
|
+
for (let lat = -90; lat <= 90; lat += 1)
|
|
298
|
+
points.push(across(id, -Math.PI, lat * RAD));
|
|
299
|
+
points.push({ ...points[0] });
|
|
300
|
+
return points;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Fit a projection into a box so the whole map shows, with a margin of a few pixels.
|
|
304
|
+
*
|
|
305
|
+
* @param id - The projection.
|
|
306
|
+
* @param width - Box width in pixels.
|
|
307
|
+
* @param height - Box height in pixels.
|
|
308
|
+
* @param zoom - 1 or more; the map is held to its middle.
|
|
309
|
+
* @returns The scale and the middle.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```ts
|
|
313
|
+
* import { fitMap } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
314
|
+
*
|
|
315
|
+
* console.log(fitMap("mollweide", 800, 400));
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
318
|
+
export function fitMap(id, width, height, zoom = 1) {
|
|
319
|
+
const { halfWidth, halfHeight } = mapExtent(id);
|
|
320
|
+
const room = 4;
|
|
321
|
+
const scale = Math.max(0, Math.min((width - 2 * room) / (2 * halfWidth), (height - 2 * room) / (2 * halfHeight))) * Math.max(1, finite(zoom, 1));
|
|
322
|
+
return { scale, cx: width / 2, cy: height / 2 };
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* A point of the map in pixels of the box (y grows down).
|
|
326
|
+
*
|
|
327
|
+
* @param fit - From `fitMap`.
|
|
328
|
+
* @param point - A point in radii.
|
|
329
|
+
* @returns `x` and `y` in pixels.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```ts
|
|
333
|
+
* import { fitMap, mapPixels, projectMap } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
334
|
+
*
|
|
335
|
+
* console.log(mapPixels(fitMap("mercator", 400, 400), projectMap("mercator", 10, 20)));
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
export const mapPixels = (fit, point) => ({ x: fit.cx + point.x * fit.scale, y: fit.cy - point.y * fit.scale });
|
|
339
|
+
/**
|
|
340
|
+
* Where the Sun is overhead on a body, as a point of the map: the sub-solar point worked from the body's pole and spin and put through the projection. On a map that does not show that side of the body (the azimuthal disc shows all of it) the point is still found; the mark is on the picture whenever it can be.
|
|
341
|
+
*
|
|
342
|
+
* @param id - The projection.
|
|
343
|
+
* @param body - A body with a pole and spin (a planet, dwarf planet or moon; the Sun has none).
|
|
344
|
+
* @param ms - Milliseconds since 1970 (UTC).
|
|
345
|
+
* @param centre - The point in the middle.
|
|
346
|
+
* @returns The point on the map in radii, and the place on the body.
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```ts
|
|
350
|
+
* import { mapSun } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
351
|
+
*
|
|
352
|
+
* const sun = mapSun("equirectangular", "moon", Date.UTC(1969, 6, 20, 20, 17, 40));
|
|
353
|
+
* console.log(sun.lat.toFixed(0), sun.lon.toFixed(0));
|
|
354
|
+
* ```
|
|
355
|
+
*/
|
|
356
|
+
export function mapSun(id, body, ms, centre = NO_CENTRE) {
|
|
357
|
+
const sun = subSolarPoint(body, ms);
|
|
358
|
+
const lon = wrapLongitude(sun.lon);
|
|
359
|
+
return { ...projectMap(id, lon, sun.lat, centre), lon, lat: sun.lat };
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Turn a body's painted map (an equirectangular picture, the whole body from -180 to 180 east and 90 to -90 north) into the same body in another projection: for every pixel of the answer the place on the body it shows is found with `unprojectMap` and the colour is read from the picture, smoothly between its pixels, wrapping across the seam and holding at the poles. Pixels outside the map's shape are transparent.
|
|
363
|
+
*
|
|
364
|
+
* @param source - The painted map.
|
|
365
|
+
* @param id - The projection.
|
|
366
|
+
* @param width - Answer width in pixels.
|
|
367
|
+
* @param height - Answer height in pixels.
|
|
368
|
+
* @param options - `centre` (the middle) and `zoom` (1 fits the whole map).
|
|
369
|
+
* @returns RGBA bytes, `width` by `height`.
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* ```ts
|
|
373
|
+
* import { resampleMap } from "@johnmorrisdotca/wakusei-planets/flat";
|
|
374
|
+
*
|
|
375
|
+
* const grey = { width: 2, height: 1, data: [0, 0, 0, 255, 255, 255, 255, 255] };
|
|
376
|
+
* const out = resampleMap(grey, "equirectangular", 4, 2);
|
|
377
|
+
* console.log(out.length);
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
380
|
+
export function resampleMap(source, id, width, height, options = {}) {
|
|
381
|
+
const w = Math.max(0, Math.floor(width));
|
|
382
|
+
const h = Math.max(0, Math.floor(height));
|
|
383
|
+
const out = new Uint8ClampedArray(w * h * 4);
|
|
384
|
+
if (source.width < 1 || source.height < 1 || w === 0 || h === 0)
|
|
385
|
+
return out;
|
|
386
|
+
const centre = options.centre ?? NO_CENTRE;
|
|
387
|
+
const fit = fitMap(id, w, h, options.zoom);
|
|
388
|
+
const { width: sw, height: sh, data } = source;
|
|
389
|
+
for (let py = 0; py < h; py += 1) {
|
|
390
|
+
for (let px = 0; px < w; px += 1) {
|
|
391
|
+
const at = unprojectMap(id, (px + 0.5 - fit.cx) / fit.scale, (fit.cy - (py + 0.5)) / fit.scale, centre);
|
|
392
|
+
if (at === null)
|
|
393
|
+
continue;
|
|
394
|
+
// the picture's pixel centres are at (i + 0.5) / size of the way across; longitude wraps, latitude holds at the poles
|
|
395
|
+
const u = ((at.lon + 180) / 360) * sw - 0.5;
|
|
396
|
+
const v = Math.max(0, Math.min(sh - 1, ((90 - at.lat) / 180) * sh - 0.5));
|
|
397
|
+
const x0 = Math.floor(u);
|
|
398
|
+
const y0 = Math.floor(v);
|
|
399
|
+
const fx = u - x0;
|
|
400
|
+
const fy = v - y0;
|
|
401
|
+
const xa = ((x0 % sw) + sw) % sw;
|
|
402
|
+
const xb = (((x0 + 1) % sw) + sw) % sw;
|
|
403
|
+
const ya = y0;
|
|
404
|
+
const yb = Math.min(sh - 1, y0 + 1);
|
|
405
|
+
const o = (py * w + px) * 4;
|
|
406
|
+
for (let channel = 0; channel < 4; channel += 1) {
|
|
407
|
+
const top = data[(ya * sw + xa) * 4 + channel] * (1 - fx) + data[(ya * sw + xb) * 4 + channel] * fx;
|
|
408
|
+
const bottom = data[(yb * sw + xa) * 4 + channel] * (1 - fx) + data[(yb * sw + xb) * 4 + channel] * fx;
|
|
409
|
+
out[o + channel] = top * (1 - fy) + bottom * fy;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return out;
|
|
414
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Pose } from "../ephemeris.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The pose for a view. Usually `yaw` turns the body about its own pole. While the view tumbles (the controller turns it about all three axes) `yaw` turns the whole picture about the picture's vertical,
|
|
4
|
+
* as the controller's own orientation does, because a turn about a pole that is tipped in the picture is not the same pose for the two sets of angles `(yaw, tilt, roll)` and `(yaw + 180, 180 - tilt, roll + 180)`
|
|
5
|
+
* that the controller swaps between at a pole, and the body would jump there.
|
|
6
|
+
* @param base The body's pose before the view.
|
|
7
|
+
* @param yaw Degrees about the pole, or about the picture's vertical while `tumble` is true.
|
|
8
|
+
* @param tilt Degrees about the picture's horizontal axis, as the element turns it (minus the controller's).
|
|
9
|
+
* @param roll Degrees about the line of sight, as the element turns it: minus the controller's roll, except in a tumble, where the controller's roll goes in as it is. The orientation the controller tumbles has the three turns in the opposite handedness to the element's ordinary pose, and with the roll's sign kept the pose is a smooth function of it.
|
|
10
|
+
* @param tumble True while the controller tumbles the view.
|
|
11
|
+
* @returns A new pose.
|
|
12
|
+
*/
|
|
13
|
+
export declare function viewPose(base: Pose, yaw: number, tilt: number, roll: number, tumble: boolean): Pose;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { turn } from "../vec.js";
|
|
2
|
+
const VERTICAL = [0, 1, 0];
|
|
3
|
+
const ACROSS = [1, 0, 0];
|
|
4
|
+
const TOWARD = [0, 0, 1];
|
|
5
|
+
function turnAll(pose, axis, degrees) {
|
|
6
|
+
pose.pole = turn(pose.pole, axis, degrees);
|
|
7
|
+
pose.meridian = turn(pose.meridian, axis, degrees);
|
|
8
|
+
pose.light = turn(pose.light, axis, degrees);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The pose for a view. Usually `yaw` turns the body about its own pole. While the view tumbles (the controller turns it about all three axes) `yaw` turns the whole picture about the picture's vertical,
|
|
12
|
+
* as the controller's own orientation does, because a turn about a pole that is tipped in the picture is not the same pose for the two sets of angles `(yaw, tilt, roll)` and `(yaw + 180, 180 - tilt, roll + 180)`
|
|
13
|
+
* that the controller swaps between at a pole, and the body would jump there.
|
|
14
|
+
* @param base The body's pose before the view.
|
|
15
|
+
* @param yaw Degrees about the pole, or about the picture's vertical while `tumble` is true.
|
|
16
|
+
* @param tilt Degrees about the picture's horizontal axis, as the element turns it (minus the controller's).
|
|
17
|
+
* @param roll Degrees about the line of sight, as the element turns it: minus the controller's roll, except in a tumble, where the controller's roll goes in as it is. The orientation the controller tumbles has the three turns in the opposite handedness to the element's ordinary pose, and with the roll's sign kept the pose is a smooth function of it.
|
|
18
|
+
* @param tumble True while the controller tumbles the view.
|
|
19
|
+
* @returns A new pose.
|
|
20
|
+
*/
|
|
21
|
+
export function viewPose(base, yaw, tilt, roll, tumble) {
|
|
22
|
+
const pose = { pole: base.pole, meridian: base.meridian, light: base.light };
|
|
23
|
+
if (yaw !== 0) {
|
|
24
|
+
if (tumble)
|
|
25
|
+
turnAll(pose, VERTICAL, yaw);
|
|
26
|
+
else
|
|
27
|
+
pose.meridian = turn(pose.meridian, pose.pole, yaw);
|
|
28
|
+
}
|
|
29
|
+
if (tilt !== 0)
|
|
30
|
+
turnAll(pose, ACROSS, tilt);
|
|
31
|
+
if (roll !== 0)
|
|
32
|
+
turnAll(pose, TOWARD, roll);
|
|
33
|
+
return pose;
|
|
34
|
+
}
|
package/dist/sky/events.d.ts
CHANGED
|
@@ -20,6 +20,8 @@ export interface SunDay {
|
|
|
20
20
|
events: Map<string, number | null>;
|
|
21
21
|
alwaysUp: boolean;
|
|
22
22
|
alwaysDown: boolean;
|
|
23
|
+
/** Whether the Sun at solar noon is above the sunrise height: suncalc's rule for which flag to set when `sunrise` is null. */
|
|
24
|
+
noonHigh: boolean;
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
25
27
|
* The Sun's day at a place: `utcOffset` in minutes east of UTC picks the local calendar day holding `ms`, and `undefined` the solar day (one solar midnight to the next).
|
package/dist/sky/events.js
CHANGED
|
@@ -112,8 +112,9 @@ export function sunDay(ms, observer, utcOffset, rows) {
|
|
|
112
112
|
const sunrise = events.get("sunrise");
|
|
113
113
|
const sunset = events.get("sunset");
|
|
114
114
|
const none = sunrise === null && sunset === null;
|
|
115
|
-
const
|
|
116
|
-
|
|
115
|
+
const noonHigh = altitude(noon) > -0.833 - dip(height);
|
|
116
|
+
const high = none && noonHigh;
|
|
117
|
+
return { noon, nadir, events, alwaysUp: high, alwaysDown: none && !high, noonHigh };
|
|
117
118
|
}
|
|
118
119
|
/** The Moon between two instants at a place: rise and set (the upper limb at the horizon through 34 arc-minutes of refraction), the upper and lower meridian crossings, and whether it stays up or down. */
|
|
119
120
|
export function moonDay(start, end, observer) {
|