copper3d 3.10.3 → 3.11.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 (34) hide show
  1. package/README.md +162 -4
  2. package/ai-index/guides/4D-MRI-LV-rendering.md +580 -0
  3. package/ai-index/guides/ai-assist-api.md +382 -0
  4. package/ai-index/guides/camera-framing.md +238 -0
  5. package/ai-index/guides/control-axes.md +124 -0
  6. package/ai-index/guides/copper3d-4D-API.md +219 -0
  7. package/ai-index/guides/copper3d-rendering-optimization.md +155 -0
  8. package/ai-index/guides/gltf-decoders.md +105 -0
  9. package/ai-index/guides/heart-sensor.md +137 -0
  10. package/ai-index/guides/load-callbacks.md +244 -0
  11. package/ai-index/guides/nrrd-tools.md +1868 -0
  12. package/ai-index/guides/on-demand-controls.md +154 -0
  13. package/ai-index/guides/rotation-pivot.md +197 -0
  14. package/ai-index/guides/scene-resources.md +227 -0
  15. package/ai-index/guides/segmentation-module.md +1516 -0
  16. package/ai-index/guides/surface-annotation-api.md +388 -0
  17. package/ai-index/guides/volume-display.md +194 -0
  18. package/ai-index/manifest.json +136 -0
  19. package/ai-index/symbols.json +1 -0
  20. package/dist/Utils/segmentation/DrawToolCore.d.ts +8 -0
  21. package/dist/Utils/segmentation/DrawToolCore.js +11 -0
  22. package/dist/Utils/segmentation/DrawToolCore.js.map +1 -1
  23. package/dist/Utils/segmentation/NrrdTools.d.ts +8 -0
  24. package/dist/Utils/segmentation/NrrdTools.js +10 -0
  25. package/dist/Utils/segmentation/NrrdTools.js.map +1 -1
  26. package/dist/Utils/segmentation/core/UndoManager.d.ts +12 -1
  27. package/dist/Utils/segmentation/core/UndoManager.js +33 -17
  28. package/dist/Utils/segmentation/core/UndoManager.js.map +1 -1
  29. package/dist/bundle.esm.js +57 -20
  30. package/dist/bundle.umd.js +57 -20
  31. package/dist/types/Utils/segmentation/DrawToolCore.d.ts +8 -0
  32. package/dist/types/Utils/segmentation/NrrdTools.d.ts +8 -0
  33. package/dist/types/Utils/segmentation/core/UndoManager.d.ts +12 -1
  34. package/package.json +7 -4
@@ -0,0 +1,137 @@
1
+ # Heart Sensor (Kiwrious)
2
+
3
+ `copper3d` can read a Kiwrious heart-rate sensor over Web Serial, through the
4
+ `copper3d_plugin_heart_k` plugin.
5
+
6
+ **As of 3.9.0 the plugin is loaded on demand.** Existing code keeps working
7
+ unchanged, but there is one behavioural difference worth knowing about, and one
8
+ new export. Both are covered below.
9
+
10
+ ## Quick start
11
+
12
+ ```ts
13
+ import Copper from "copper3d";
14
+
15
+ await Copper.configKiwriousHeart(
16
+ connectButton,
17
+ disconnectButton,
18
+ "/kiwrious/bin/",
19
+ "/kiwrious/wasm/",
20
+ (connected) => console.log("connected:", connected),
21
+ (data, status, heartRate) => console.log(status, heartRate)
22
+ );
23
+ ```
24
+
25
+ `configKiwriousHeart` wires both buttons, forwards connection changes to the
26
+ fifth argument, and forwards each decoded reading to the sixth.
27
+
28
+ ## What changed in 3.9.0
29
+
30
+ ### The plugin is no longer imported at module scope
31
+
32
+ `copper3d_plugin_heart_k` ships as a webpack UMD bundle. Its runtime resolves a
33
+ public path the moment the module evaluates, reading `document.currentScript`
34
+ or the last `<script src>` on the page. Native ESM provides neither, so the
35
+ plugin threw during evaluation:
36
+
37
+ ```
38
+ Error: Automatic publicPath is not supported in this browser
39
+ ```
40
+
41
+ Because copper3d's entry point imported it statically, and because the code is
42
+ a side-effectful IIFE that tree-shaking cannot remove, **every** project paid
43
+ for it — `import "copper3d"` threw before a single line of copper3d ran, under
44
+ Vite, Nuxt, and anything else serving native ES modules.
45
+
46
+ The plugin now loads through a dynamic import that supplies the value the
47
+ webpack runtime is looking for and restores the document afterwards. Nothing is
48
+ fetched until you actually reach for the sensor.
49
+
50
+ **What you get:** `import "copper3d"` works under native ESM, and projects that
51
+ never touch the sensor never download it.
52
+
53
+ ### `configKiwriousHeart` now returns a promise
54
+
55
+ It awaits the plugin before wiring the buttons, so a click landing immediately
56
+ after the call cannot race the import.
57
+
58
+ ```ts
59
+ // Before -- still valid, buttons just go live a moment later
60
+ Copper.configKiwriousHeart(connectBtn, disconnectBtn, binUrl, wasmUrl, onConn, onData);
61
+
62
+ // Recommended: await it, then enable your UI
63
+ await Copper.configKiwriousHeart(connectBtn, disconnectBtn, binUrl, wasmUrl, onConn, onData);
64
+ connectBtn.disabled = false;
65
+ ```
66
+
67
+ If you already render the buttons disabled and enable them yourself, awaiting is
68
+ the only change you need.
69
+
70
+ ### `Copper.kiwrious` still works synchronously
71
+
72
+ The default export is a facade over the real plugin, so none of these calls
73
+ need an `await`:
74
+
75
+ ```ts
76
+ Copper.kiwrious.setBinUrl("/kiwrious/bin/");
77
+ Copper.kiwrious.setWasm("/kiwrious/wasm/");
78
+ Copper.kiwrious.serialService.onSerialData = (reading) => { /* ... */ };
79
+ Copper.kiwrious.serialService.onSerialConnection = (connected) => { /* ... */ };
80
+
81
+ // This is the call that triggers the download.
82
+ await Copper.kiwrious.serialService.connectAndReadAsync();
83
+ ```
84
+
85
+ URLs and callbacks set before the plugin arrives are replayed onto it as soon as
86
+ it does, in the order you set them.
87
+
88
+ Two consequences of the facade, both intentional:
89
+
90
+ - `isReading` and `canResumeReading` report `false` until the plugin has
91
+ loaded. Nothing can be reading before it exists.
92
+ - `disconnectAsync()`, `resumeReading()` and `triggerStopReading()` are no-ops
93
+ when nothing has loaded yet. Tearing down a session you never started must
94
+ not be what pulls a multi-megabyte bundle over the wire.
95
+
96
+ ## `loadKiwrious()`
97
+
98
+ New in 3.9.0. Imports the plugin and resolves with it. Use it when you want to
99
+ control *when* the download happens — for example to warm it up while the
100
+ reader is still on an introduction screen, so pressing **Connect** is instant.
101
+
102
+ ```ts
103
+ import Copper from "copper3d";
104
+
105
+ // Somewhere well before the user can press Connect
106
+ Copper.loadKiwrious().catch(() => {
107
+ // Optional: the sensor page is unavailable, degrade the UI
108
+ });
109
+ ```
110
+
111
+ The import is memoized: concurrent callers share one download, and later calls
112
+ resolve immediately. Calling it is never required — `connectAndReadAsync()` and
113
+ `configKiwriousHeart()` both trigger the same load.
114
+
115
+ ## Serving the sensor assets
116
+
117
+ The plugin fetches its own `.bin` and `.wasm` from the URLs you pass to
118
+ `setBinUrl` / `setWasm`, so those must be reachable from your deployed site.
119
+ Copy them out of the package at build time rather than hot-linking a CDN:
120
+
121
+ ```
122
+ public/
123
+ kiwrious/
124
+ bin/…
125
+ wasm/…
126
+ ```
127
+
128
+ Both paths must be absolute or resolvable against your site's base URL. On a
129
+ subpath deploy (GitHub Pages project sites, for instance) prefix them with the
130
+ base — a bare `/kiwrious/bin/` resolves against the domain root and 404s.
131
+
132
+ ## Requirements
133
+
134
+ Web Serial is required. It is available in Chromium-based browsers over HTTPS
135
+ (or `localhost`) and is not implemented in Firefox or Safari. `connectAndReadAsync()`
136
+ opens the browser's port-picker dialog, so it must be called from a user
137
+ gesture — a click handler, not a lifecycle hook.
@@ -0,0 +1,244 @@
1
+ # Load Progress & Errors
2
+
3
+ Every loader in copper3d gained `onProgress` and `onError` in **3.9.0**. Before
4
+ that there was no way to find out that a load had failed — a broken URL, a 404,
5
+ a corrupt file and a very slow download all looked identical from outside: the
6
+ callback simply never fired.
7
+
8
+ ## What was there before
9
+
10
+ ```ts
11
+ // pre-3.9.0, copperSceneOnDemond.loadGltf
12
+ loader.load(
13
+ url,
14
+ (gltf) => { /* ... */ },
15
+ (error) => {} // <- the onPROGRESS slot, named `error`, empty
16
+ );
17
+ ```
18
+
19
+ `GLTFLoader.load(url, onLoad, onProgress, onError)` takes four arguments. The
20
+ third was an empty function named `error` — which reads correctly at a glance
21
+ and never fires on one — and there was no fourth. `loadNrrd` had a real progress
22
+ handler for its built-in loading bar and no error slot either.
23
+
24
+ ## `scene.loadGltf`
25
+
26
+ ```ts
27
+ scene.loadGltf(url, callback?, {
28
+ onProgress: (event) => console.log(event.loaded, event.total),
29
+ onError: (error) => showFailure(error),
30
+ });
31
+ ```
32
+
33
+ Both are optional and both default to nothing, so existing calls are unaffected.
34
+
35
+ Available on `copperSceneOnDemond`, `copperScene` and `copperMScene`. (
36
+ `copperScene.loadPureGLB` already took an `onError` and is unchanged.)
37
+
38
+ ## `scene.loadNrrd`
39
+
40
+ The callbacks go on the existing `opts` object:
41
+
42
+ ```ts
43
+ scene.loadNrrd(url, loadingBar, segmentation, callback, {
44
+ openGui: false,
45
+ onProgress: (event) => { /* ... */ },
46
+ onError: (error) => showFailure(error),
47
+ });
48
+ ```
49
+
50
+ `onProgress` fires **in addition to** the built-in loading bar, which keeps
51
+ writing its percentage into `loadingBar.progress` exactly as before. Nothing
52
+ about the bar changes.
53
+
54
+ ::: warning One visible behaviour change
55
+ The NRRD loading bar is now **hidden when a load fails**. It previously had no
56
+ failure state at all, so a failed volume left the bar up forever, frozen at
57
+ whatever percentage it reached — which is precisely what made a failure
58
+ indistinguishable from a slow load. Nothing else about the bar changed.
59
+ :::
60
+
61
+ ## `opts.axes`: extract only the planes you show <Badge type="tip" text="3.9.0" />
62
+
63
+ `loadNrrd` has always extracted **all three** slice planes — x, y and z —
64
+ whether or not you display them. `extractSlice` walks the whole volume per
65
+ call, and the result is retained on `volume.sliceList` for the volume's
66
+ lifetime. On a 50MB MRI, an axis nobody looks at costs a full pass over the
67
+ buffer plus a geometry, a material and a canvas-backed texture that nothing
68
+ frees.
69
+
70
+ ```ts
71
+ scene.loadNrrd(url, loadingBar, false, callback, {
72
+ openGui: false,
73
+ axes: ["z"], // an axial-only viewer
74
+ });
75
+ ```
76
+
77
+ Defaults to `["x", "y", "z"]`, i.e. exactly the previous behaviour.
78
+
79
+ ::: warning
80
+ Omitted axes are `undefined` on the `nrrdMeshes` / `nrrdSlices` your callback
81
+ receives. Their types still declare all three present — widening them to
82
+ optional would break every existing caller's compile — so if you narrow
83
+ `axes`, only read what you asked for.
84
+ :::
85
+
86
+ ### Extracting a skipped axis later <Badge type="tip" text="3.10.0" />
87
+
88
+ Narrowing `axes` is only safe if you never show the other planes. `ensureAxisExtracted`
89
+ lifts that restriction: it extracts a plane the load skipped, on demand, the first time
90
+ something asks for it.
91
+
92
+ ```ts
93
+ import { ensureAxisExtracted } from "copper3d";
94
+
95
+ // slices came from loadNrrd with axes: ["z"] — no sagittal plane yet
96
+ ensureAxisExtracted(slices, "x"); // extracts it now
97
+ ensureAxisExtracted(slices, "x", meshes); // also fills meshes.x
98
+ ```
99
+
100
+ It **mutates `slices` (and `meshes`, when given) in place**, so every existing reference
101
+ to the same object picks up the new plane — you do not replace anything. It is a no-op
102
+ when the axis is already present, so calling it unconditionally is fine.
103
+
104
+ All three axes share one `Volume` instance, so whichever axis *is* already extracted is
105
+ where the dimensions, spacing and RAS dimensions come from. A plane extracted late also
106
+ inherits the `contrastOrder` stamped on its siblings, making it indistinguishable from
107
+ one extracted at load time.
108
+
109
+ ::: tip You usually do not need to call it yourself
110
+ `NrrdTools.setSliceOrientation(axis)` already calls `ensureAxisExtracted` for every
111
+ loaded contrast before the display pipeline reads the new plane. So an axial-only load
112
+ (`axes: ["z"]`) that later switches to sagittal or coronal just works — you pay the
113
+ extraction cost at switch time instead of at load time.
114
+ :::
115
+
116
+ ## `opts.knownMinMax`: skip the whole-volume intensity scan <Badge type="tip" text="3.10.0" />
117
+
118
+ After parsing, three's `Volume.computeMinMax()` walks every voxel to find the intensity
119
+ range. On a large MRI that single function is typically the largest block in a case-load
120
+ CPU profile — and the answer is usually already known server-side, because the backend
121
+ decompressed the same bytes to write its headers.
122
+
123
+ ```ts
124
+ const { min, max } = await fetch(`/files/${caseId}/headers`).then((r) => r.json());
125
+
126
+ scene.loadNrrd(url, loadingBar, false, callback, {
127
+ openGui: false,
128
+ knownMinMax: [min, max],
129
+ });
130
+ ```
131
+
132
+ Omit it — or pass a stale value — and behaviour is exactly as before: three's own scan
133
+ runs, and the image comes out identical, just slower.
134
+
135
+ ## `opts.signal`: cancel a superseded load <Badge type="tip" text="3.10.0" />
136
+
137
+ A case switch or series switch used to leave the old transfer running to completion and
138
+ merely discard its result. Pass an `AbortSignal` and the transfer itself stops.
139
+
140
+ ```ts
141
+ let inflight: AbortController | undefined;
142
+
143
+ function loadCase(url: string) {
144
+ inflight?.abort(); // stop the previous download
145
+ inflight = new AbortController();
146
+
147
+ scene.loadNrrd(url, loadingBar, false, onLoaded, {
148
+ openGui: false,
149
+ signal: inflight.signal,
150
+ onError: (error) => {
151
+ if ((error as DOMException).name === "AbortError") return; // expected
152
+ showFailure(error);
153
+ },
154
+ });
155
+ }
156
+ ```
157
+
158
+ `onError` still fires for an aborted load — the rejection reason is a `DOMException` with
159
+ `name === "AbortError"`. Callers that already ignore errors from a stale load need no
160
+ further change.
161
+
162
+ Aborting rejects **your** promise immediately; it does not wait for the worker. The
163
+ worker is notified separately so it can drop its own attached-count for that URL and stop
164
+ the real network transfer once nobody is left waiting on it.
165
+
166
+ ## NRRD parsing runs in a Web Worker <Badge type="tip" text="3.10.0" />
167
+
168
+ `loadNrrd` no longer fetches and parses on the main thread. The fetch, the gunzip and the
169
+ NRRD header/typed-array parse all happen in a shared Web Worker
170
+ (`Loader/nrrdWorker.ts`, whose logic lives in `Loader/nrrdWorkerCore.ts`); the volume's
171
+ pixel buffer comes back as a **transferable**, and the main thread only rehydrates a real
172
+ `Volume` around it — microseconds regardless of volume size.
173
+
174
+ Nothing about the public API changes. `onProgress`, `onError`, the built-in loading bar
175
+ and the `callback` signature all behave exactly as documented above.
176
+
177
+ Two consequences worth knowing:
178
+
179
+ - **URL-level fetch dedup lives in the worker.** Two concurrent `loadNrrd` calls for the
180
+ same URL share one network transfer, and each still gets its own parsed `Volume`.
181
+ - **The worker is inlined into the bundle** (`?worker&inline`), so there is no second
182
+ asset file to serve. A UMD bundle mounted at a base path unknown at build time still
183
+ works.
184
+
185
+ ## Detecting a stalled download
186
+
187
+ A flat timeout is the wrong instrument for a large volume: a 53MB NRRD on a
188
+ shared 6 Mbps connection legitimately takes over a minute, and a fixed deadline
189
+ eventually fails a download that is perfectly healthy. What you want is *no
190
+ progress for N seconds*, which `onProgress` makes a few lines:
191
+
192
+ ```ts
193
+ const STALL_MS = 15_000;
194
+ let timer: ReturnType<typeof setTimeout>;
195
+
196
+ function armStallTimer() {
197
+ clearTimeout(timer);
198
+ timer = setTimeout(() => showFailure(new Error("download stalled")), STALL_MS);
199
+ }
200
+
201
+ armStallTimer();
202
+ scene.loadNrrd(url, loadingBar, false, (volume, meshes, slices) => {
203
+ clearTimeout(timer);
204
+ /* ... */
205
+ }, {
206
+ openGui: false,
207
+ onProgress: armStallTimer,
208
+ onError: (error) => { clearTimeout(timer); showFailure(error); },
209
+ });
210
+ ```
211
+
212
+ Before 3.9.0 the only liveness signal copper3d exposed was the loading bar's
213
+ own text, so this had to be done by pointing a `MutationObserver` at that DOM
214
+ node.
215
+
216
+ ## `event.total` is not always there
217
+
218
+ `ProgressEvent.total` is `0` unless the server sends a `Content-Length`, and it
219
+ does not send one for a gzipped or chunked response. `loaded / total` is then
220
+ `Infinity`, and the built-in bar renders `Infinity % loaded`.
221
+
222
+ Treat that as *indeterminate*, not as stuck at zero:
223
+
224
+ ```ts
225
+ onProgress: (event) => {
226
+ const pct = event.total > 0 ? (event.loaded / event.total) * 100 : null;
227
+ setProgress(pct); // null -> show an indeterminate spinner
228
+ }
229
+ ```
230
+
231
+ ## Upgrading from 3.8.x
232
+
233
+ Purely additive apart from the loading-bar note above. Every argument is
234
+ optional and every default is what happened before.
235
+
236
+ ## Upgrading from 3.9.x
237
+
238
+ Also purely additive. `knownMinMax`, `signal` and `ensureAxisExtracted` are all opt-in,
239
+ and the move to a worker is invisible from the outside — same callbacks, same loading
240
+ bar, same `Volume`.
241
+
242
+ The one thing to check is any code that was reaching into `copperNrrdLoader`'s internals
243
+ rather than going through `loadNrrd`: the old `sharedFetchNrrdArrayBuffer` + per-caller
244
+ `loader.parse()` pair is gone, replaced by the worker's own fetch dedup and parse.