@beeson-group/tycho 0.1.0 → 0.2.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 (40) hide show
  1. package/AUTHORS +7 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +14 -0
  4. package/README.md +38 -0
  5. package/THIRD_PARTY_NOTICES.md +68 -0
  6. package/dist/tycho.esm.js +515 -261
  7. package/dist/tycho.js +515 -261
  8. package/dist/types/container.d.ts +64 -0
  9. package/dist/types/errors.d.ts +10 -0
  10. package/dist/types/evaluate/clock.d.ts +30 -0
  11. package/dist/types/evaluate/frames.d.ts +63 -0
  12. package/dist/types/evaluate/index.d.ts +14 -0
  13. package/dist/types/evaluate/layers.d.ts +47 -0
  14. package/dist/types/evaluate/resolve.d.ts +39 -0
  15. package/dist/types/evaluate/sets.d.ts +49 -0
  16. package/dist/types/evaluate/types.d.ts +36 -0
  17. package/dist/types/index.d.ts +22 -0
  18. package/dist/types/runtime/colormaps.d.ts +10 -0
  19. package/dist/types/runtime/colors.d.ts +94 -0
  20. package/dist/types/runtime/element.d.ts +224 -0
  21. package/dist/types/runtime/export.d.ts +119 -0
  22. package/dist/types/runtime/geometry.d.ts +214 -0
  23. package/dist/types/runtime/icons.d.ts +56 -0
  24. package/dist/types/runtime/layout.d.ts +34 -0
  25. package/dist/types/runtime/overlay.d.ts +33 -0
  26. package/dist/types/runtime/readouts.d.ts +129 -0
  27. package/dist/types/runtime/renderer.d.ts +303 -0
  28. package/dist/types/runtime/state.d.ts +106 -0
  29. package/dist/types/runtime/tiles.d.ts +33 -0
  30. package/dist/types/scene.d.ts +37 -0
  31. package/dist/types/scene.gen.d.ts +440 -0
  32. package/dist/types/validate.d.ts +9 -0
  33. package/package.json +63 -35
  34. package/src/colormaps.ts +0 -42
  35. package/src/container.ts +0 -126
  36. package/src/element.ts +0 -1034
  37. package/src/frames.ts +0 -204
  38. package/src/index.ts +0 -14
  39. package/src/layout.ts +0 -82
  40. package/src/viewer.ts +0 -864
package/src/frames.ts DELETED
@@ -1,204 +0,0 @@
1
- /**
2
- * Kinematic frame transforms for the ER3BP.
3
- *
4
- * This is the only "physics" the runtime owns: pure coordinate changes that
5
- * need nothing but the mass ratio, the eccentricity and the current true
6
- * anomaly. Nothing here integrates an equation of motion. The formulas mirror
7
- * the conversions exposed by PyDylan's ER3BP class
8
- * (rotating_pulsating_to_rotating, rotating_to_barycentric_inertial,
9
- * rotating_to_inertial with which_body).
10
- *
11
- * Conventions: distances in DU (the primaries' semi-major axis), primary of
12
- * mass 1-mu at rotating (-mu, 0), secondary of mass mu at (1-mu, 0), rotating
13
- * frame angle relative to the inertial frame equal to the true anomaly f.
14
- */
15
-
16
- export type FrameId =
17
- | 'rotating_pulsating'
18
- | 'rotating'
19
- | 'barycentric_inertial'
20
- | 'inertial_primary'
21
- | 'inertial_secondary';
22
-
23
- export interface FrameSpec {
24
- id: FrameId;
25
- label: string;
26
- axes: [string, string];
27
- /** colormap for family members in this frame, following the reference figures */
28
- cmap: 'viridis' | 'plasma' | 'cividis' | 'blues' | 'reds';
29
- }
30
-
31
- export const FRAMES: FrameSpec[] = [
32
- { id: 'rotating_pulsating', label: 'rotating-pulsating', axes: ['x [DU]', 'y [DU]'], cmap: 'viridis' },
33
- { id: 'rotating', label: 'rotating', axes: ['x̃ [DU]', 'ỹ [DU]'], cmap: 'blues' },
34
- { id: 'barycentric_inertial', label: 'barycentric inertial', axes: ['ξ [DU]', 'η [DU]'], cmap: 'plasma' },
35
- { id: 'inertial_primary', label: 'inertial, centred on m₁', axes: ['ξ₁ [DU]', 'η₁ [DU]'], cmap: 'reds' },
36
- { id: 'inertial_secondary', label: 'inertial, centred on m₂', axes: ['ξ₂ [DU]', 'η₂ [DU]'], cmap: 'cividis' },
37
- ];
38
-
39
- export interface ER3BPSystem {
40
- mu: number;
41
- e: number;
42
- /** True anomaly at the start of the trajectory (0 periapsis, pi apoapsis). */
43
- f0: number;
44
- }
45
-
46
- /** Primary-secondary separation over the semi-major axis at true anomaly f. */
47
- export function rho(e: number, f: number): number {
48
- return (1 - e * e) / (1 + e * Math.cos(f));
49
- }
50
-
51
- /** Mean anomaly for true anomaly f, unwrapped so it grows monotonically with f. */
52
- function meanAnomaly(e: number, f: number): number {
53
- const k = Math.round(f / (2 * Math.PI));
54
- const fr = f - 2 * Math.PI * k;
55
- const E = 2 * Math.atan2(Math.sqrt(1 - e) * Math.sin(fr / 2), Math.sqrt(1 + e) * Math.cos(fr / 2));
56
- return E - e * Math.sin(E) + 2 * Math.PI * k;
57
- }
58
-
59
- /** Nondimensional time elapsed between true anomalies f0 and f (mean motion 1). */
60
- export function elapsedTime(e: number, f0: number, f: number): number {
61
- return meanAnomaly(e, f) - meanAnomaly(e, f0);
62
- }
63
-
64
- /**
65
- * Position of a body in a given frame at absolute true anomaly f.
66
- * Writes [x, y] into `out` at `offset`.
67
- */
68
- export function bodyPosition(
69
- body: 'primary' | 'secondary',
70
- sys: ER3BPSystem,
71
- frame: FrameId,
72
- f: number,
73
- out: Float32Array,
74
- offset = 0,
75
- ): void {
76
- const xr = body === 'primary' ? -sys.mu : 1 - sys.mu; // rotating-pulsating x, y = 0
77
- const r = rho(sys.e, f);
78
- const c = Math.cos(f);
79
- const s = Math.sin(f);
80
- switch (frame) {
81
- case 'rotating_pulsating':
82
- out[offset] = xr;
83
- out[offset + 1] = 0;
84
- return;
85
- case 'rotating':
86
- out[offset] = r * xr;
87
- out[offset + 1] = 0;
88
- return;
89
- case 'barycentric_inertial':
90
- out[offset] = r * xr * c;
91
- out[offset + 1] = r * xr * s;
92
- return;
93
- case 'inertial_primary': {
94
- const d = body === 'primary' ? 0 : r; // separation between the bodies is rho
95
- out[offset] = d * c;
96
- out[offset + 1] = d * s;
97
- return;
98
- }
99
- case 'inertial_secondary': {
100
- const d = body === 'secondary' ? 0 : -r;
101
- out[offset] = d * c;
102
- out[offset + 1] = d * s;
103
- return;
104
- }
105
- }
106
- }
107
-
108
- /**
109
- * Transforms a rotating-pulsating trajectory into `frame`.
110
- * `xy` holds M pairs, `fRel` M true anomalies relative to sys.f0. Output is
111
- * M triples (z = 0) suitable for a three.js position attribute.
112
- */
113
- export function transformTrajectory(
114
- xy: Float32Array,
115
- fRel: ArrayLike<number>,
116
- sys: ER3BPSystem,
117
- frame: FrameId,
118
- out: Float32Array,
119
- ): Float32Array {
120
- const m = fRel.length;
121
- const tmp = new Float32Array(2);
122
- for (let i = 0; i < m; i++) {
123
- const x = xy[2 * i];
124
- const y = xy[2 * i + 1];
125
- const f = sys.f0 + fRel[i];
126
- let X: number;
127
- let Y: number;
128
- if (frame === 'rotating_pulsating') {
129
- X = x;
130
- Y = y;
131
- } else {
132
- const r = rho(sys.e, f);
133
- if (frame === 'rotating') {
134
- X = r * x;
135
- Y = r * y;
136
- } else {
137
- const c = Math.cos(f);
138
- const s = Math.sin(f);
139
- X = r * (x * c - y * s);
140
- Y = r * (x * s + y * c);
141
- if (frame !== 'barycentric_inertial') {
142
- bodyPosition(frame === 'inertial_primary' ? 'primary' : 'secondary', sys, 'barycentric_inertial', f, tmp);
143
- X -= tmp[0];
144
- Y -= tmp[1];
145
- }
146
- }
147
- }
148
- out[3 * i] = X;
149
- out[3 * i + 1] = Y;
150
- out[3 * i + 2] = 0;
151
- }
152
- return out;
153
- }
154
-
155
- /** Path of a body over the trajectory's true anomaly grid, as M triples. */
156
- export function bodyTrail(
157
- body: 'primary' | 'secondary',
158
- fRel: ArrayLike<number>,
159
- sys: ER3BPSystem,
160
- frame: FrameId,
161
- out: Float32Array,
162
- ): Float32Array {
163
- const tmp = new Float32Array(2);
164
- for (let i = 0; i < fRel.length; i++) {
165
- bodyPosition(body, sys, frame, sys.f0 + fRel[i], tmp);
166
- out[3 * i] = tmp[0];
167
- out[3 * i + 1] = tmp[1];
168
- out[3 * i + 2] = 0;
169
- }
170
- return out;
171
- }
172
-
173
- /**
174
- * True anomaly reached after nondimensional time `tau` from `f0` (mean motion 1),
175
- * unwrapped so it grows monotonically. Newton iteration on Kepler's equation.
176
- */
177
- export function trueAnomalyFromTime(e: number, f0: number, tau: number): number {
178
- const M = meanAnomaly(e, f0) + tau;
179
- const k = Math.floor(M / (2 * Math.PI));
180
- const Mr = M - 2 * Math.PI * k;
181
- let E = e < 0.8 ? Mr : Math.PI;
182
- for (let i = 0; i < 30; i++) {
183
- const d = (E - e * Math.sin(E) - Mr) / (1 - e * Math.cos(E));
184
- E -= d;
185
- if (Math.abs(d) < 1e-13) break;
186
- }
187
- let f = 2 * Math.atan2(Math.sqrt(1 + e) * Math.sin(E / 2), Math.sqrt(1 - e) * Math.cos(E / 2));
188
- if (f < 0) f += 2 * Math.PI;
189
- return f + 2 * Math.PI * k;
190
- }
191
-
192
- /** Index i with a[i] <= v < a[i+1] for a sorted array (clamped to [0, n-2]). */
193
- export function segmentIndex(a: ArrayLike<number>, v: number): number {
194
- let lo = 0;
195
- let hi = a.length - 1;
196
- if (v <= a[0]) return 0;
197
- if (v >= a[hi]) return hi - 1;
198
- while (hi - lo > 1) {
199
- const mid = (lo + hi) >> 1;
200
- if (a[mid] <= v) lo = mid;
201
- else hi = mid;
202
- }
203
- return lo;
204
- }
package/src/index.ts DELETED
@@ -1,14 +0,0 @@
1
- /**
2
- * Tycho browser runtime entry point.
3
- * Bundled as an IIFE exposing `tycho` on window (and as an ES module); registers <tycho-viewer>.
4
- */
5
-
6
- export { Container, loadContainer, decodeEmbedded } from './container';
7
- export { FRAMES, rho, elapsedTime, trueAnomalyFromTime, segmentIndex, transformTrajectory, bodyPosition, bodyTrail } from './frames';
8
- export { colormap, colormapCss } from './colormaps';
9
- export { OrbitViewer, DARK } from './viewer';
10
- export { TychoViewerElement, define } from './element';
11
-
12
- import { define } from './element';
13
-
14
- define();
package/src/layout.ts DELETED
@@ -1,82 +0,0 @@
1
- /**
2
- * Tiling layout: a binary tree of splits whose leaves are panels.
3
- *
4
- * Dropping a new panel on an existing panel's edge splits that panel there;
5
- * dropping on its centre replaces it. Removing a panel collapses its parent
6
- * split. The tree is plain JSON so it can be persisted and put in an export.
7
- */
8
-
9
- export type SplitDir = 'row' | 'col';
10
- export type DropZone = 'left' | 'right' | 'top' | 'bottom' | 'centre';
11
-
12
- export type LayoutNode =
13
- | { kind: 'panel'; id: string }
14
- | { kind: 'split'; dir: SplitDir; ratio: number; a: LayoutNode; b: LayoutNode };
15
-
16
- export function panelIds(node: LayoutNode | null): string[] {
17
- if (!node) return [];
18
- return node.kind === 'panel' ? [node.id] : [...panelIds(node.a), ...panelIds(node.b)];
19
- }
20
-
21
- export function hasPanel(node: LayoutNode | null, id: string): boolean {
22
- return panelIds(node).includes(id);
23
- }
24
-
25
- /** Returns a new tree with `newId` placed relative to `targetId` in `zone`. */
26
- export function splitAt(node: LayoutNode, targetId: string, newId: string, zone: DropZone): LayoutNode {
27
- if (node.kind === 'panel') {
28
- if (node.id !== targetId) return node;
29
- if (zone === 'centre') return { kind: 'panel', id: newId };
30
- const fresh: LayoutNode = { kind: 'panel', id: newId };
31
- const dir: SplitDir = zone === 'left' || zone === 'right' ? 'row' : 'col';
32
- const first = zone === 'left' || zone === 'top';
33
- return { kind: 'split', dir, ratio: 0.5, a: first ? fresh : node, b: first ? node : fresh };
34
- }
35
- return { ...node, a: splitAt(node.a, targetId, newId, zone), b: splitAt(node.b, targetId, newId, zone) };
36
- }
37
-
38
- /** Returns the tree without `id`, or null if it was the only panel. */
39
- export function removePanel(node: LayoutNode, id: string): LayoutNode | null {
40
- if (node.kind === 'panel') return node.id === id ? null : node;
41
- const a = removePanel(node.a, id);
42
- const b = removePanel(node.b, id);
43
- if (!a) return b;
44
- if (!b) return a;
45
- return { ...node, a, b };
46
- }
47
-
48
- export function setRatio(node: LayoutNode, split: LayoutNode, ratio: number): LayoutNode {
49
- if (node === split && node.kind === 'split') return { ...node, ratio };
50
- if (node.kind === 'panel') return node;
51
- return { ...node, a: setRatio(node.a, split, ratio), b: setRatio(node.b, split, ratio) };
52
- }
53
-
54
- /** Panel ids with the area fraction each one occupies, for picking a split target. */
55
- export function panelAreas(node: LayoutNode, area = 1): { id: string; area: number }[] {
56
- if (node.kind === 'panel') return [{ id: node.id, area }];
57
- return [...panelAreas(node.a, area * node.ratio), ...panelAreas(node.b, area * (1 - node.ratio))];
58
- }
59
-
60
- /** Drop zone for a pointer at (x, y) inside a rect of size (w, h): edges within 28% of the side, else centre. */
61
- export function zoneFor(x: number, y: number, w: number, h: number): DropZone {
62
- const fx = x / Math.max(1, w);
63
- const fy = y / Math.max(1, h);
64
- const edge = 0.28;
65
- const d = [
66
- { zone: 'left' as DropZone, v: fx },
67
- { zone: 'right' as DropZone, v: 1 - fx },
68
- { zone: 'top' as DropZone, v: fy },
69
- { zone: 'bottom' as DropZone, v: 1 - fy },
70
- ].sort((p, q) => p.v - q.v)[0];
71
- return d.v < edge ? d.zone : 'centre';
72
- }
73
-
74
- export function isLayoutNode(v: unknown): v is LayoutNode {
75
- if (!v || typeof v !== 'object') return false;
76
- const n = v as Record<string, unknown>;
77
- if (n.kind === 'panel') return typeof n.id === 'string';
78
- if (n.kind === 'split') {
79
- return (n.dir === 'row' || n.dir === 'col') && typeof n.ratio === 'number' && isLayoutNode(n.a) && isLayoutNode(n.b);
80
- }
81
- return false;
82
- }