@beeson-group/tycho 0.1.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/dist/tycho.esm.js +3910 -0
- package/dist/tycho.js +3910 -0
- package/package.json +45 -0
- package/src/colormaps.ts +42 -0
- package/src/container.ts +126 -0
- package/src/element.ts +1034 -0
- package/src/frames.ts +204 -0
- package/src/index.ts +14 -0
- package/src/layout.ts +82 -0
- package/src/viewer.ts +864 -0
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@beeson-group/tycho",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Tycho browser runtime: animated, interactive astrodynamics visualizations as a web component",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "npm run build:iife && npm run build:esm && npm run build:py",
|
|
7
|
+
"build:iife": "esbuild src/index.ts --bundle --format=iife --global-name=tycho --minify --target=es2020 --outfile=dist/tycho.js",
|
|
8
|
+
"build:esm": "esbuild src/index.ts --bundle --format=esm --minify --target=es2020 --outfile=dist/tycho.esm.js",
|
|
9
|
+
"build:py": "node -e \"require('fs').mkdirSync('../src/pytycho/_static',{recursive:true});require('fs').copyFileSync('dist/tycho.js','../src/pytycho/_static/tycho.js')\"",
|
|
10
|
+
"build:dev": "esbuild src/index.ts --bundle --format=iife --global-name=tycho --sourcemap=inline --target=es2020 --outfile=dist/tycho.dev.js",
|
|
11
|
+
"typecheck": "tsc --noEmit"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/three": "^0.180.0",
|
|
15
|
+
"esbuild": "^0.28.0",
|
|
16
|
+
"typescript": "^5.6.0"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"three": "^0.180.0"
|
|
20
|
+
},
|
|
21
|
+
"main": "dist/tycho.esm.js",
|
|
22
|
+
"module": "dist/tycho.esm.js",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": "./dist/tycho.esm.js",
|
|
25
|
+
"./iife": "./dist/tycho.js"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"src"
|
|
30
|
+
],
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/beeson-group/tycho.git",
|
|
34
|
+
"directory": "web"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/beeson-group/tycho",
|
|
37
|
+
"keywords": [
|
|
38
|
+
"astrodynamics",
|
|
39
|
+
"visualization",
|
|
40
|
+
"three-body",
|
|
41
|
+
"webgl",
|
|
42
|
+
"web-component"
|
|
43
|
+
],
|
|
44
|
+
"sideEffects": true
|
|
45
|
+
}
|
package/src/colormaps.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small colormap lookup tables, sampled from matplotlib's maps, for colouring
|
|
3
|
+
* family members by their continuation parameter. The two single-hue maps are
|
|
4
|
+
* restricted to their brighter half so the low end stays visible on black.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type ColormapId = 'viridis' | 'plasma' | 'cividis' | 'blues' | 'reds';
|
|
8
|
+
|
|
9
|
+
const STOPS: Record<ColormapId, number[][]> = {
|
|
10
|
+
viridis: [
|
|
11
|
+
[0.267, 0.005, 0.329], [0.283, 0.141, 0.458], [0.254, 0.265, 0.53], [0.207, 0.372, 0.553],
|
|
12
|
+
[0.164, 0.471, 0.558], [0.128, 0.567, 0.551], [0.135, 0.659, 0.518], [0.267, 0.749, 0.441],
|
|
13
|
+
[0.478, 0.821, 0.318], [0.741, 0.873, 0.15], [0.993, 0.906, 0.144],
|
|
14
|
+
],
|
|
15
|
+
plasma: [
|
|
16
|
+
[0.05, 0.03, 0.528], [0.294, 0.012, 0.615], [0.492, 0.012, 0.658], [0.665, 0.139, 0.586],
|
|
17
|
+
[0.798, 0.28, 0.47], [0.902, 0.418, 0.36], [0.973, 0.585, 0.252], [0.993, 0.771, 0.155],
|
|
18
|
+
[0.94, 0.975, 0.131],
|
|
19
|
+
],
|
|
20
|
+
cividis: [
|
|
21
|
+
[0.0, 0.135, 0.304], [0.086, 0.218, 0.396], [0.243, 0.301, 0.42], [0.365, 0.383, 0.432],
|
|
22
|
+
[0.478, 0.466, 0.448], [0.588, 0.55, 0.437], [0.702, 0.638, 0.402], [0.822, 0.73, 0.343],
|
|
23
|
+
[0.945, 0.827, 0.244], [0.996, 0.91, 0.149],
|
|
24
|
+
],
|
|
25
|
+
blues: [[0.19, 0.42, 0.71], [0.33, 0.6, 0.8], [0.53, 0.76, 0.87], [0.7, 0.85, 0.92], [0.85, 0.93, 0.98]],
|
|
26
|
+
reds: [[0.72, 0.1, 0.1], [0.89, 0.27, 0.2], [0.98, 0.5, 0.36], [0.99, 0.7, 0.58], [1.0, 0.86, 0.78]],
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** RGB in [0, 1] at parameter t in [0, 1]. */
|
|
30
|
+
export function colormap(id: ColormapId, t: number, out: number[] = [0, 0, 0]): number[] {
|
|
31
|
+
const stops = STOPS[id];
|
|
32
|
+
const x = Math.max(0, Math.min(1, t)) * (stops.length - 1);
|
|
33
|
+
const i = Math.min(stops.length - 2, Math.floor(x));
|
|
34
|
+
const u = x - i;
|
|
35
|
+
for (let c = 0; c < 3; c++) out[c] = stops[i][c] + (stops[i + 1][c] - stops[i][c]) * u;
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function colormapCss(id: ColormapId, t: number): string {
|
|
40
|
+
const [r, g, b] = colormap(id, t);
|
|
41
|
+
return `rgb(${Math.round(r * 255)},${Math.round(g * 255)},${Math.round(b * 255)})`;
|
|
42
|
+
}
|
package/src/container.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reader for the Tycho data container, .tyd (see src/pytycho/container.py).
|
|
3
|
+
* A JSON header followed by 8-byte aligned typed buffers; no dependencies.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface ViewInfo {
|
|
7
|
+
offset: number;
|
|
8
|
+
length: number;
|
|
9
|
+
dtype: 'float32' | 'float64' | 'int32' | 'uint8';
|
|
10
|
+
shape: number[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Header {
|
|
14
|
+
format: string;
|
|
15
|
+
version: number;
|
|
16
|
+
generator: string;
|
|
17
|
+
meta: Record<string, unknown>;
|
|
18
|
+
views: Record<string, ViewInfo>;
|
|
19
|
+
datasets: DatasetRecord[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Dataset records are plain JSON; `kind` selects the interpretation. */
|
|
23
|
+
export interface DatasetRecord {
|
|
24
|
+
id: string;
|
|
25
|
+
kind: string;
|
|
26
|
+
title: string;
|
|
27
|
+
[key: string]: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type TypedArray = Float32Array | Float64Array | Int32Array | Uint8Array;
|
|
31
|
+
|
|
32
|
+
const MAGIC = 0x44435954; // "TYCD" little endian
|
|
33
|
+
|
|
34
|
+
export class Container {
|
|
35
|
+
readonly header: Header;
|
|
36
|
+
private readonly body: ArrayBuffer;
|
|
37
|
+
private readonly bodyOffset: number;
|
|
38
|
+
|
|
39
|
+
private constructor(header: Header, body: ArrayBuffer, bodyOffset: number) {
|
|
40
|
+
this.header = header;
|
|
41
|
+
this.body = body;
|
|
42
|
+
this.bodyOffset = bodyOffset;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static parse(buffer: ArrayBuffer): Container {
|
|
46
|
+
const dv = new DataView(buffer);
|
|
47
|
+
if (dv.getUint32(0, true) !== MAGIC) throw new Error('not a Tycho data container');
|
|
48
|
+
const version = dv.getUint32(4, true);
|
|
49
|
+
if (version !== 1) throw new Error(`unsupported container version ${version}`);
|
|
50
|
+
const hlen = dv.getUint32(8, true);
|
|
51
|
+
const header = JSON.parse(new TextDecoder().decode(new Uint8Array(buffer, 12, hlen))) as Header;
|
|
52
|
+
const headEnd = 12 + hlen;
|
|
53
|
+
const bodyOffset = headEnd + ((8 - (headEnd % 8)) % 8);
|
|
54
|
+
return new Container(header, buffer, bodyOffset);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get datasets(): DatasetRecord[] {
|
|
58
|
+
return this.header.datasets;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
viewInfo(name: string): ViewInfo {
|
|
62
|
+
const info = this.header.views[name];
|
|
63
|
+
if (!info) throw new Error(`unknown view ${name}`);
|
|
64
|
+
return info;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Returns a typed array over the view's bytes, zero-copy. */
|
|
68
|
+
view(name: string): TypedArray {
|
|
69
|
+
const info = this.viewInfo(name);
|
|
70
|
+
const start = this.bodyOffset + info.offset;
|
|
71
|
+
switch (info.dtype) {
|
|
72
|
+
case 'float32':
|
|
73
|
+
return new Float32Array(this.body, start, info.length / 4);
|
|
74
|
+
case 'float64':
|
|
75
|
+
return new Float64Array(this.body, start, info.length / 8);
|
|
76
|
+
case 'int32':
|
|
77
|
+
return new Int32Array(this.body, start, info.length / 4);
|
|
78
|
+
case 'uint8':
|
|
79
|
+
return new Uint8Array(this.body, start, info.length);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
f32(name: string): Float32Array {
|
|
84
|
+
const a = this.view(name);
|
|
85
|
+
return a instanceof Float32Array ? a : Float32Array.from(a as ArrayLike<number>);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
f64(name: string): Float64Array {
|
|
89
|
+
const a = this.view(name);
|
|
90
|
+
return a instanceof Float64Array ? a : Float64Array.from(a as ArrayLike<number>);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function base64ToBytes(text: string): Uint8Array<ArrayBuffer> {
|
|
95
|
+
const bin = atob(text.replace(/\s+/g, ''));
|
|
96
|
+
const out = new Uint8Array(new ArrayBuffer(bin.length));
|
|
97
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Decodes a container embedded in a script tag by src/pytycho/html.py. */
|
|
102
|
+
export async function decodeEmbedded(el: HTMLScriptElement): Promise<ArrayBuffer> {
|
|
103
|
+
const encoding = el.dataset.encoding ?? 'base64';
|
|
104
|
+
const bytes = base64ToBytes(el.textContent ?? '');
|
|
105
|
+
if (encoding === 'base64') return bytes.buffer;
|
|
106
|
+
if (encoding === 'gzip+base64') {
|
|
107
|
+
if (typeof DecompressionStream === 'undefined') {
|
|
108
|
+
throw new Error('this browser lacks DecompressionStream; rebuild the page with compress=False');
|
|
109
|
+
}
|
|
110
|
+
const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream('gzip'));
|
|
111
|
+
return new Response(stream).arrayBuffer();
|
|
112
|
+
}
|
|
113
|
+
throw new Error(`unknown data encoding ${encoding}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Loads a container from an element selector (embedded) or a URL (sidecar). */
|
|
117
|
+
export async function loadContainer(source: string): Promise<Container> {
|
|
118
|
+
if (source.startsWith('#')) {
|
|
119
|
+
const el = document.querySelector<HTMLScriptElement>(source);
|
|
120
|
+
if (!el) throw new Error(`no element matches ${source}`);
|
|
121
|
+
return Container.parse(await decodeEmbedded(el));
|
|
122
|
+
}
|
|
123
|
+
const res = await fetch(source);
|
|
124
|
+
if (!res.ok) throw new Error(`failed to fetch ${source}: ${res.status}`);
|
|
125
|
+
return Container.parse(await res.arrayBuffer());
|
|
126
|
+
}
|