copper3d 3.4.9 → 3.5.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 (45) hide show
  1. package/dist/Renderer/baseRenderer.d.ts +3 -0
  2. package/dist/Renderer/baseRenderer.js +20 -0
  3. package/dist/Renderer/baseRenderer.js.map +1 -1
  4. package/dist/Renderer/copperMSceneRenderer.d.ts +3 -0
  5. package/dist/Renderer/copperMSceneRenderer.js +22 -0
  6. package/dist/Renderer/copperMSceneRenderer.js.map +1 -1
  7. package/dist/Renderer/copperRenderer.d.ts +0 -2
  8. package/dist/Renderer/copperRenderer.js +0 -5
  9. package/dist/Renderer/copperRenderer.js.map +1 -1
  10. package/dist/Utils/segmentation/DrawToolCore.d.ts +3 -0
  11. package/dist/Utils/segmentation/DrawToolCore.js +29 -1
  12. package/dist/Utils/segmentation/DrawToolCore.js.map +1 -1
  13. package/dist/Utils/segmentation/NrrdTools.d.ts +54 -0
  14. package/dist/Utils/segmentation/NrrdTools.js +119 -0
  15. package/dist/Utils/segmentation/NrrdTools.js.map +1 -1
  16. package/dist/Utils/segmentation/core/index.d.ts +1 -1
  17. package/dist/Utils/segmentation/core/index.js +1 -1
  18. package/dist/Utils/segmentation/core/index.js.map +1 -1
  19. package/dist/Utils/segmentation/core/types.d.ts +11 -1
  20. package/dist/Utils/segmentation/core/types.js +30 -0
  21. package/dist/Utils/segmentation/core/types.js.map +1 -1
  22. package/dist/Utils/segmentation/eventRouter/EventRouter.d.ts +2 -0
  23. package/dist/Utils/segmentation/eventRouter/EventRouter.js +37 -6
  24. package/dist/Utils/segmentation/eventRouter/EventRouter.js.map +1 -1
  25. package/dist/Utils/segmentation/eventRouter/types.d.ts +3 -2
  26. package/dist/Utils/segmentation/tools/AiAssistTool.d.ts +146 -0
  27. package/dist/Utils/segmentation/tools/AiAssistTool.js +470 -0
  28. package/dist/Utils/segmentation/tools/AiAssistTool.js.map +1 -0
  29. package/dist/bundle.esm.js +726 -14
  30. package/dist/bundle.umd.js +727 -13
  31. package/dist/index.d.ts +5 -4
  32. package/dist/index.js +3 -3
  33. package/dist/index.js.map +1 -1
  34. package/dist/types/Renderer/baseRenderer.d.ts +3 -0
  35. package/dist/types/Renderer/copperMSceneRenderer.d.ts +3 -0
  36. package/dist/types/Renderer/copperRenderer.d.ts +0 -2
  37. package/dist/types/Utils/segmentation/DrawToolCore.d.ts +3 -0
  38. package/dist/types/Utils/segmentation/NrrdTools.d.ts +54 -0
  39. package/dist/types/Utils/segmentation/core/index.d.ts +1 -1
  40. package/dist/types/Utils/segmentation/core/types.d.ts +11 -1
  41. package/dist/types/Utils/segmentation/eventRouter/EventRouter.d.ts +2 -0
  42. package/dist/types/Utils/segmentation/eventRouter/types.d.ts +3 -2
  43. package/dist/types/Utils/segmentation/tools/AiAssistTool.d.ts +146 -0
  44. package/dist/types/index.d.ts +5 -4
  45. package/package.json +1 -1
@@ -0,0 +1,146 @@
1
+ /**
2
+ * AiAssistTool — interactive prompt-based segmentation (experimental).
3
+ *
4
+ * The clinician left-clicks foreground / background points (or drags a box /
5
+ * scribbles) on the current slice; a backend model returns a region mask that
6
+ * is written into an INDEPENDENT scratch MaskVolume (`aiAssistMaskVolume`,
7
+ * registered as `maskData.volumes['aiScratch']`) — never the validated layers.
8
+ *
9
+ * Network lives in the app layer: this tool only (a) maps screen → voxel-slice
10
+ * coordinates, (b) accumulates the prompt for the current slice, (c) fires
11
+ * `onPrompt` so the app can call the backend, and (d) applies the returned mask
12
+ * via `applyMask`. The app's `useAiAssist` composable wires (c)→(d).
13
+ *
14
+ * Sandbox: snapshot on enter (`snapshot`), then on exit either `discard`
15
+ * (restore snapshot) or merge is handled by NrrdTools (clone + pushGroup).
16
+ *
17
+ * Coordinate mapping supports all three views (axial/sagittal/coronal); it
18
+ * inverts the per-axis display transform of RenderingUtils.renderSliceToCanvas
19
+ * (only coronal 'y' is vertically flipped). Merge captures painted voxels from
20
+ * any view (the scratch is a full 3D volume).
21
+ */
22
+ import { BaseTool } from "./BaseTool";
23
+ import type { ToolContext } from "./BaseTool";
24
+ import { MaskVolume } from "../core";
25
+ import type { SliceRenderHostDeps } from "./ToolHost";
26
+ /** Scratch volume key in maskData.volumes — kept OUT of image.layers so
27
+ * compositeAllLayers ignores it (we render it ourselves in start()). */
28
+ export declare const AI_SCRATCH_LAYER = "aiScratch";
29
+ export type AiPromptTool = "point" | "box" | "scribble";
30
+ export interface AiPromptPoint {
31
+ x: number;
32
+ y: number;
33
+ z?: number;
34
+ label: number;
35
+ }
36
+ export interface AiPromptPayload {
37
+ axis: "x" | "y" | "z";
38
+ sliceIndex: number;
39
+ width: number;
40
+ height: number;
41
+ points: AiPromptPoint[];
42
+ box?: {
43
+ x0: number;
44
+ y0: number;
45
+ x1: number;
46
+ y1: number;
47
+ label: number;
48
+ };
49
+ scribble?: AiPromptPoint[];
50
+ scribbleRadius?: number;
51
+ }
52
+ export interface AiMaskResult {
53
+ axis: "x" | "y" | "z";
54
+ sliceIndex: number;
55
+ width: number;
56
+ height: number;
57
+ rle: number[];
58
+ /** 3D result (engine B / nnInteractive): inclusive [lo,hi] slice span + one RLE per slice. */
59
+ sliceRange?: [number, number];
60
+ slices?: number[][];
61
+ }
62
+ type AiAssistHostDeps = Pick<SliceRenderHostDeps, "renderSliceToCanvas" | "getOrCreateSliceBuffer">;
63
+ export declare class AiAssistTool extends BaseTool {
64
+ private host;
65
+ /** Independent scratch volume (also registered at maskData.volumes['aiScratch']). */
66
+ private scratchVol;
67
+ /** Snapshot of the scratch buffer taken on enter (for discard). */
68
+ private snapshotData;
69
+ /** Frozen regions ("New region" commits here). Pixels recorded here survive a
70
+ * later re-prediction — even on the SAME channel — so multiple separate regions
71
+ * can be drawn without the newest prediction wiping the previous ones. Null =
72
+ * nothing frozen yet (applySliceRle then behaves as a single live region). */
73
+ private committedVol;
74
+ private promptTool;
75
+ private polarity;
76
+ /** Target channel/label (1-8) the AI paints into — the "AI layer" channel. */
77
+ private channel;
78
+ /** Scribble brush radius (px), user-adjustable via slider. */
79
+ private scribbleSize;
80
+ /** Accumulated prompts for the CURRENT slice; reset on slice/axis change. */
81
+ private points;
82
+ private scribble;
83
+ private box;
84
+ private activeSlice;
85
+ private dragging;
86
+ private dragStart;
87
+ private dragScreenStart;
88
+ private dragScreen;
89
+ private screenScribble;
90
+ private hoverScreen;
91
+ /** Fired when a prompt gesture completes — app calls backend, then applyMask(). */
92
+ onPrompt: ((payload: AiPromptPayload) => void) | null;
93
+ constructor(ctx: ToolContext, host: AiAssistHostDeps);
94
+ setPromptTool(tool: AiPromptTool): void;
95
+ setPolarity(label: number): void;
96
+ setScribbleSize(size: number): void;
97
+ setChannel(channel: number): void;
98
+ getChannel(): number;
99
+ getScratchVolume(): MaskVolume | null;
100
+ /** Serialize the scratch volume as per-z-slice RLE (only NON-empty slices),
101
+ * for persisting to ai_generated_nii_LPS on the backend. Binary (any channel →
102
+ * 1); RLE is alternating run lengths starting with a 0-run — the exact format
103
+ * the backend's rle_decode expects. Uses getSliceUint8 (the same path the
104
+ * clinician layer's /api/mask/replace uses) so orientation matches. */
105
+ getScratchSlices(): {
106
+ axis: "z";
107
+ width: number;
108
+ height: number;
109
+ slices: {
110
+ sliceIndex: number;
111
+ rle: number[];
112
+ }[];
113
+ } | null;
114
+ /** Create (or reuse) the scratch volume sized to the layer grid and register
115
+ * it under maskData.volumes['aiScratch']. Snapshots the empty buffer. */
116
+ enter(): void;
117
+ /** Remove the scratch volume from the registry and drop references. */
118
+ exit(): void;
119
+ /** Discard everything painted since enter() — restore the snapshot. */
120
+ discard(): void;
121
+ /** "New region": freeze every voxel painted so far so it survives later
122
+ * re-predictions (even on the same channel), then start a fresh prompt set.
123
+ * This is what makes drawing multiple separate regions actually work — without
124
+ * it the next prediction's same-channel cleanup erases the previous region. */
125
+ commitRegion(): void;
126
+ /** Clear the in-progress prompt set (e.g. slice/axis change). Does NOT freeze —
127
+ * use commitRegion() for the "New region" action. */
128
+ resetPrompts(): void;
129
+ /** Voxel-slice (width,height) for the current axis. */
130
+ private sliceDims;
131
+ /** Screen offset → voxel-slice (vx,vy) for the current axis. */
132
+ private toVoxel;
133
+ onPointerDown(e: MouseEvent): void;
134
+ onPointerMove(e: MouseEvent): void;
135
+ onPointerUp(_e: MouseEvent): void;
136
+ /** Reset the accumulated prompt set when the user scrubs to a new slice. */
137
+ private syncSlice;
138
+ private emit;
139
+ applyMask(result: AiMaskResult): void;
140
+ /** Merge one RLE-encoded predicted slice into the scratch volume at sliceIndex.
141
+ * Predicted pixels → current channel; stale current-channel pixels cleared
142
+ * UNLESS they belong to a frozen (committed) region; OTHER channels preserved. */
143
+ private applySliceRle;
144
+ renderOverlay(targetCtx: CanvasRenderingContext2D): void;
145
+ }
146
+ export {};
@@ -0,0 +1,470 @@
1
+ /**
2
+ * AiAssistTool — interactive prompt-based segmentation (experimental).
3
+ *
4
+ * The clinician left-clicks foreground / background points (or drags a box /
5
+ * scribbles) on the current slice; a backend model returns a region mask that
6
+ * is written into an INDEPENDENT scratch MaskVolume (`aiAssistMaskVolume`,
7
+ * registered as `maskData.volumes['aiScratch']`) — never the validated layers.
8
+ *
9
+ * Network lives in the app layer: this tool only (a) maps screen → voxel-slice
10
+ * coordinates, (b) accumulates the prompt for the current slice, (c) fires
11
+ * `onPrompt` so the app can call the backend, and (d) applies the returned mask
12
+ * via `applyMask`. The app's `useAiAssist` composable wires (c)→(d).
13
+ *
14
+ * Sandbox: snapshot on enter (`snapshot`), then on exit either `discard`
15
+ * (restore snapshot) or merge is handled by NrrdTools (clone + pushGroup).
16
+ *
17
+ * Coordinate mapping supports all three views (axial/sagittal/coronal); it
18
+ * inverts the per-axis display transform of RenderingUtils.renderSliceToCanvas
19
+ * (only coronal 'y' is vertically flipped). Merge captures painted voxels from
20
+ * any view (the scratch is a full 3D volume).
21
+ */
22
+ import { BaseTool } from "./BaseTool";
23
+ import { MaskVolume, AI_MASK_CHANNEL_COLORS } from "../core";
24
+ /** Scratch volume key in maskData.volumes — kept OUT of image.layers so
25
+ * compositeAllLayers ignores it (we render it ourselves in start()). */
26
+ export const AI_SCRATCH_LAYER = "aiScratch";
27
+ export class AiAssistTool extends BaseTool {
28
+ constructor(ctx, host) {
29
+ super(ctx);
30
+ /** Independent scratch volume (also registered at maskData.volumes['aiScratch']). */
31
+ this.scratchVol = null;
32
+ /** Snapshot of the scratch buffer taken on enter (for discard). */
33
+ this.snapshotData = null;
34
+ /** Frozen regions ("New region" commits here). Pixels recorded here survive a
35
+ * later re-prediction — even on the SAME channel — so multiple separate regions
36
+ * can be drawn without the newest prediction wiping the previous ones. Null =
37
+ * nothing frozen yet (applySliceRle then behaves as a single live region). */
38
+ this.committedVol = null;
39
+ this.promptTool = "point";
40
+ this.polarity = 1; // 1 = foreground (positive), 0 = background
41
+ /** Target channel/label (1-8) the AI paints into — the "AI layer" channel. */
42
+ this.channel = 1;
43
+ /** Scribble brush radius (px), user-adjustable via slider. */
44
+ this.scribbleSize = 5;
45
+ /** Accumulated prompts for the CURRENT slice; reset on slice/axis change. */
46
+ this.points = [];
47
+ this.scribble = [];
48
+ this.activeSlice = -1;
49
+ // Drag state (box / scribble) — voxel coords for the prompt
50
+ this.dragging = false;
51
+ this.dragStart = null;
52
+ // Screen-space (display px) copies for the LIVE preview drawn each frame.
53
+ this.dragScreenStart = null;
54
+ this.dragScreen = null;
55
+ this.screenScribble = [];
56
+ // Live cursor position (screen px) for the scribble brush-size preview ring —
57
+ // updated on every hover move so the ring follows the mouse like the paint brush.
58
+ this.hoverScreen = null;
59
+ /** Fired when a prompt gesture completes — app calls backend, then applyMask(). */
60
+ this.onPrompt = null;
61
+ this.host = host;
62
+ }
63
+ // ── Configuration (driven from the panel via NrrdTools) ────────────────────
64
+ setPromptTool(tool) {
65
+ // Switching tool starts a clean gesture — drop any stale points/box/scribble
66
+ // and drag state so the next click behaves predictably.
67
+ if (tool !== this.promptTool) {
68
+ this.resetPrompts();
69
+ this.dragging = false;
70
+ this.dragStart = null;
71
+ }
72
+ this.promptTool = tool;
73
+ }
74
+ setPolarity(label) { this.polarity = label === 0 ? 0 : 1; }
75
+ setScribbleSize(size) {
76
+ this.scribbleSize = Math.max(1, Math.min(40, Math.round(size)));
77
+ }
78
+ setChannel(channel) {
79
+ const c = Math.max(1, Math.min(8, Math.round(channel)));
80
+ // Changing channel starts a NEW region in the new colour — otherwise the
81
+ // accumulated points would be re-predicted and repainted in the new label,
82
+ // recolouring the previous channel's region.
83
+ if (c !== this.channel)
84
+ this.resetPrompts();
85
+ this.channel = c;
86
+ }
87
+ getChannel() { return this.channel; }
88
+ getScratchVolume() { return this.scratchVol; }
89
+ /** Serialize the scratch volume as per-z-slice RLE (only NON-empty slices),
90
+ * for persisting to ai_generated_nii_LPS on the backend. Binary (any channel →
91
+ * 1); RLE is alternating run lengths starting with a 0-run — the exact format
92
+ * the backend's rle_decode expects. Uses getSliceUint8 (the same path the
93
+ * clinician layer's /api/mask/replace uses) so orientation matches. */
94
+ getScratchSlices() {
95
+ if (!this.scratchVol)
96
+ return null;
97
+ const { depth } = this.scratchVol.getDimensions();
98
+ const out = [];
99
+ let width = 0;
100
+ let height = 0;
101
+ for (let z = 0; z < depth; z++) {
102
+ const { data, width: w, height: h } = this.scratchVol.getSliceUint8(z, "z");
103
+ width = w;
104
+ height = h;
105
+ // RLE-encode (binarized) in one pass; skip fully-empty slices.
106
+ const runs = [];
107
+ let value = 0; // always begin with a 0-run (matches backend rle_encode)
108
+ let count = 0;
109
+ let any = false;
110
+ for (let i = 0; i < data.length; i++) {
111
+ const v = data[i] !== 0 ? 1 : 0;
112
+ if (v)
113
+ any = true;
114
+ if (v === value) {
115
+ count++;
116
+ }
117
+ else {
118
+ runs.push(count);
119
+ value = v;
120
+ count = 1;
121
+ }
122
+ }
123
+ runs.push(count);
124
+ if (any)
125
+ out.push({ sliceIndex: z, rle: runs });
126
+ }
127
+ return { axis: "z", width, height, slices: out };
128
+ }
129
+ // ── Sandbox lifecycle ──────────────────────────────────────────────────────
130
+ /** Create (or reuse) the scratch volume sized to the layer grid and register
131
+ * it under maskData.volumes['aiScratch']. Snapshots the empty buffer. */
132
+ enter() {
133
+ const volumes = this.ctx.protectedData.maskData.volumes;
134
+ const baseId = this.ctx.nrrd_states.image.layers[0];
135
+ const base = volumes[baseId];
136
+ if (!base)
137
+ return;
138
+ const d = base.getDimensions();
139
+ if (!this.scratchVol ||
140
+ this.scratchVol.getDimensions().width !== d.width ||
141
+ this.scratchVol.getDimensions().height !== d.height ||
142
+ this.scratchVol.getDimensions().depth !== d.depth) {
143
+ this.scratchVol = new MaskVolume(d.width, d.height, d.depth, 1);
144
+ }
145
+ else {
146
+ this.scratchVol.clear();
147
+ }
148
+ volumes[AI_SCRATCH_LAYER] = this.scratchVol;
149
+ // Paint the AI scratch with the AI-Assist palette (channel 1 = cyan) so the 2D
150
+ // overlay matches the cyan ai_generated GLB. Scoped to THIS volume only — the
151
+ // global palette / clinician mask colours are untouched.
152
+ for (let ch = 1; ch <= 8; ch++) {
153
+ const c = AI_MASK_CHANNEL_COLORS[ch];
154
+ if (c)
155
+ this.scratchVol.setChannelColor(ch, { r: c.r, g: c.g, b: c.b, a: c.a });
156
+ }
157
+ this.snapshotData = this.scratchVol.getRawData().slice();
158
+ this.committedVol = null; // nothing frozen at session start
159
+ this.resetPrompts();
160
+ }
161
+ /** Remove the scratch volume from the registry and drop references. */
162
+ exit() {
163
+ const volumes = this.ctx.protectedData.maskData.volumes;
164
+ delete volumes[AI_SCRATCH_LAYER];
165
+ this.scratchVol = null;
166
+ this.snapshotData = null;
167
+ this.committedVol = null;
168
+ this.resetPrompts();
169
+ this.dragging = false;
170
+ this.dragStart = null;
171
+ }
172
+ /** Discard everything painted since enter() — restore the snapshot. */
173
+ discard() {
174
+ if (this.scratchVol && this.snapshotData) {
175
+ this.scratchVol.setRawData(this.snapshotData.slice());
176
+ }
177
+ this.committedVol = null; // discard wipes frozen regions too
178
+ this.resetPrompts();
179
+ }
180
+ /** "New region": freeze every voxel painted so far so it survives later
181
+ * re-predictions (even on the same channel), then start a fresh prompt set.
182
+ * This is what makes drawing multiple separate regions actually work — without
183
+ * it the next prediction's same-channel cleanup erases the previous region. */
184
+ commitRegion() {
185
+ if (this.scratchVol) {
186
+ const d = this.scratchVol.getDimensions();
187
+ if (!this.committedVol ||
188
+ this.committedVol.getDimensions().width !== d.width ||
189
+ this.committedVol.getDimensions().height !== d.height ||
190
+ this.committedVol.getDimensions().depth !== d.depth) {
191
+ this.committedVol = new MaskVolume(d.width, d.height, d.depth, 1);
192
+ }
193
+ this.committedVol.setRawData(this.scratchVol.getRawData().slice());
194
+ }
195
+ this.resetPrompts();
196
+ }
197
+ /** Clear the in-progress prompt set (e.g. slice/axis change). Does NOT freeze —
198
+ * use commitRegion() for the "New region" action. */
199
+ resetPrompts() {
200
+ this.points = [];
201
+ this.scribble = [];
202
+ this.box = undefined;
203
+ this.dragScreenStart = null;
204
+ this.dragScreen = null;
205
+ this.screenScribble = [];
206
+ }
207
+ // ── Coordinate mapping (all three views) ───────────────────────────────────
208
+ //
209
+ // Mapping must INVERT exactly what RenderingUtils.renderSliceToCanvas does to
210
+ // display the overlay (it is the renderer used by renderOverlay):
211
+ // - axial (z) / sagittal (x): no flip
212
+ // - coronal (y): vertical flip (scale(1,-1))
213
+ // Slice voxel dims & canvas mm extents per axis (matches MaskVolume
214
+ // getSliceDimensions + SphereTool.setSphereCanvasSize):
215
+ // z → (W=width, H=height) over (x_mm, y_mm)
216
+ // y → (W=width, H=depth ) over (x_mm, z_mm) [vertical flip]
217
+ // x → (W=depth, H=height) over (z_mm, y_mm)
218
+ /** Voxel-slice (width,height) for the current axis. */
219
+ sliceDims() {
220
+ if (!this.scratchVol)
221
+ return null;
222
+ const d = this.scratchVol.getDimensions();
223
+ switch (this.ctx.protectedData.axis) {
224
+ case "z": return { w: d.width, h: d.height };
225
+ case "y": return { w: d.width, h: d.depth };
226
+ case "x": return { w: d.depth, h: d.height };
227
+ default: return null;
228
+ }
229
+ }
230
+ /** Screen offset → voxel-slice (vx,vy) for the current axis. */
231
+ toVoxel(e) {
232
+ const sd = this.sliceDims();
233
+ if (!sd || !this.scratchVol)
234
+ return null;
235
+ const nrrd = this.ctx.nrrd_states;
236
+ const img = nrrd.image;
237
+ const mx = e.offsetX / nrrd.view.sizeFactor;
238
+ const my = e.offsetY / nrrd.view.sizeFactor;
239
+ let hMM, vMM, flipV;
240
+ switch (this.ctx.protectedData.axis) {
241
+ case "z":
242
+ hMM = img.nrrd_x_mm;
243
+ vMM = img.nrrd_y_mm;
244
+ flipV = false;
245
+ break;
246
+ case "y":
247
+ hMM = img.nrrd_x_mm;
248
+ vMM = img.nrrd_z_mm;
249
+ flipV = true;
250
+ break;
251
+ case "x":
252
+ hMM = img.nrrd_z_mm;
253
+ vMM = img.nrrd_y_mm;
254
+ flipV = false;
255
+ break;
256
+ default: return null;
257
+ }
258
+ const vx = Math.round(mx * sd.w / hMM);
259
+ let vy = Math.round(my * sd.h / vMM);
260
+ if (flipV)
261
+ vy = sd.h - 1 - vy;
262
+ if (vx < 0 || vx >= sd.w || vy < 0 || vy >= sd.h)
263
+ return null;
264
+ return { vx, vy, w: sd.w, h: sd.h };
265
+ }
266
+ // ── Pointer handlers (left button; right stays pan in EventRouter) ──────────
267
+ onPointerDown(e) {
268
+ const hit = this.toVoxel(e);
269
+ if (!hit)
270
+ return;
271
+ this.syncSlice();
272
+ if (this.promptTool === "point") {
273
+ this.points.push({ x: hit.vx, y: hit.vy, label: this.polarity });
274
+ this.emit();
275
+ }
276
+ else {
277
+ // box / scribble: begin a drag
278
+ this.dragging = true;
279
+ this.dragStart = { x: hit.vx, y: hit.vy };
280
+ this.dragScreenStart = { x: e.offsetX, y: e.offsetY };
281
+ this.dragScreen = { x: e.offsetX, y: e.offsetY };
282
+ this.screenScribble = [];
283
+ if (this.promptTool === "scribble") {
284
+ this.scribble.push({ x: hit.vx, y: hit.vy, label: this.polarity });
285
+ this.screenScribble.push({ x: e.offsetX, y: e.offsetY });
286
+ }
287
+ }
288
+ }
289
+ onPointerMove(e) {
290
+ // Track hover for the scribble preview ring even when not drawing.
291
+ this.hoverScreen = { x: e.offsetX, y: e.offsetY };
292
+ if (!this.dragging)
293
+ return;
294
+ // Track screen pos first so the live preview follows even slightly out of bounds.
295
+ this.dragScreen = { x: e.offsetX, y: e.offsetY };
296
+ const hit = this.toVoxel(e);
297
+ if (!hit)
298
+ return;
299
+ if (this.promptTool === "scribble") {
300
+ this.scribble.push({ x: hit.vx, y: hit.vy, label: this.polarity });
301
+ this.screenScribble.push({ x: e.offsetX, y: e.offsetY });
302
+ }
303
+ else if (this.promptTool === "box" && this.dragStart) {
304
+ this.box = {
305
+ x0: this.dragStart.x, y0: this.dragStart.y,
306
+ x1: hit.vx, y1: hit.vy,
307
+ label: this.polarity, // foreground box grows within; background box excludes
308
+ };
309
+ }
310
+ }
311
+ onPointerUp(_e) {
312
+ if (!this.dragging)
313
+ return;
314
+ this.dragging = false;
315
+ this.emit();
316
+ this.dragStart = null;
317
+ this.dragScreenStart = null;
318
+ this.dragScreen = null;
319
+ this.screenScribble = [];
320
+ }
321
+ /** Reset the accumulated prompt set when the user scrubs to a new slice. */
322
+ syncSlice() {
323
+ const slice = this.ctx.nrrd_states.view.currentSliceIndex;
324
+ if (slice !== this.activeSlice) {
325
+ this.activeSlice = slice;
326
+ this.resetPrompts();
327
+ }
328
+ }
329
+ emit() {
330
+ if (!this.onPrompt)
331
+ return;
332
+ const sd = this.sliceDims();
333
+ if (!sd)
334
+ return;
335
+ this.onPrompt({
336
+ axis: this.ctx.protectedData.axis,
337
+ sliceIndex: this.ctx.nrrd_states.view.currentSliceIndex,
338
+ width: sd.w,
339
+ height: sd.h,
340
+ points: [...this.points],
341
+ box: this.box,
342
+ scribble: this.scribble.length ? [...this.scribble] : undefined,
343
+ scribbleRadius: this.scribbleSize,
344
+ });
345
+ }
346
+ // ── Apply backend result → scratch volume ──────────────────────────────────
347
+ applyMask(result) {
348
+ if (!this.scratchVol)
349
+ return;
350
+ // 3D result (engine B): write each slice across the covered span.
351
+ if (result.slices && result.sliceRange) {
352
+ const [lo] = result.sliceRange;
353
+ for (let k = 0; k < result.slices.length; k++) {
354
+ this.applySliceRle(result.slices[k], result.axis, lo + k, result.width, result.height);
355
+ }
356
+ return;
357
+ }
358
+ // 2D result (engines mock / regiongrow / medsam2d): single slice.
359
+ this.applySliceRle(result.rle, result.axis, result.sliceIndex, result.width, result.height);
360
+ }
361
+ /** Merge one RLE-encoded predicted slice into the scratch volume at sliceIndex.
362
+ * Predicted pixels → current channel; stale current-channel pixels cleared
363
+ * UNLESS they belong to a frozen (committed) region; OTHER channels preserved. */
364
+ applySliceRle(rle, axis, sliceIndex, width, height) {
365
+ if (!this.scratchVol)
366
+ return;
367
+ const predicted = new Uint8Array(width * height);
368
+ let pos = 0;
369
+ let value = 0;
370
+ for (const run of rle) {
371
+ if (value === 1 && run > 0) {
372
+ const end = Math.min(pos + run, predicted.length);
373
+ for (let i = pos; i < end; i++)
374
+ predicted[i] = 1;
375
+ }
376
+ pos += run;
377
+ value ^= 1;
378
+ }
379
+ const ch = this.channel;
380
+ try {
381
+ const out = this.scratchVol.getSliceUint8(sliceIndex, axis).data; // copy
382
+ // Frozen same-channel pixels for this slice (if any region was committed).
383
+ let frozen = null;
384
+ if (this.committedVol) {
385
+ try {
386
+ frozen = this.committedVol.getSliceUint8(sliceIndex, axis).data;
387
+ }
388
+ catch (_a) {
389
+ frozen = null;
390
+ }
391
+ }
392
+ const n = Math.min(out.length, predicted.length);
393
+ for (let i = 0; i < n; i++) {
394
+ if (predicted[i])
395
+ out[i] = ch;
396
+ // Clear stale LIVE same-channel pixels (refinement), but never erase a
397
+ // frozen region committed via "New region".
398
+ else if (out[i] === ch && !(frozen && frozen[i] === ch))
399
+ out[i] = 0;
400
+ }
401
+ this.scratchVol.setSliceUint8(sliceIndex, out, axis);
402
+ }
403
+ catch (_b) {
404
+ // slice out of bounds / dims changed — ignore
405
+ }
406
+ }
407
+ // ── Overlay render (called from DrawToolCore.start() while in aiAssist) ──────
408
+ renderOverlay(targetCtx) {
409
+ if (!this.scratchVol)
410
+ return;
411
+ const axis = this.ctx.protectedData.axis;
412
+ const slice = this.ctx.nrrd_states.view.currentSliceIndex;
413
+ const buffer = this.host.getOrCreateSliceBuffer(axis);
414
+ if (!buffer)
415
+ return;
416
+ try {
417
+ this.host.renderSliceToCanvas(AI_SCRATCH_LAYER, axis, slice, buffer, targetCtx, this.ctx.nrrd_states.view.changedWidth, this.ctx.nrrd_states.view.changedHeight);
418
+ }
419
+ catch (_a) {
420
+ // volume not ready / slice out of bounds
421
+ }
422
+ // Live drag preview (screen space) — rubber-band box / scribble stroke so the
423
+ // user sees the gesture before the prediction lands on pointer-up.
424
+ if (this.dragging && this.dragScreenStart && this.dragScreen) {
425
+ targetCtx.save();
426
+ // Green-ish for foreground (include), red-ish for background (exclude).
427
+ const color = this.polarity === 1 ? "rgba(120,255,180,0.95)" : "rgba(255,120,120,0.95)";
428
+ targetCtx.strokeStyle = color;
429
+ if (this.promptTool === "box") {
430
+ targetCtx.setLineDash([5, 4]);
431
+ targetCtx.lineWidth = 1.5;
432
+ const x = Math.min(this.dragScreenStart.x, this.dragScreen.x);
433
+ const y = Math.min(this.dragScreenStart.y, this.dragScreen.y);
434
+ const w = Math.abs(this.dragScreen.x - this.dragScreenStart.x);
435
+ const h = Math.abs(this.dragScreen.y - this.dragScreenStart.y);
436
+ targetCtx.strokeRect(x, y, w, h);
437
+ }
438
+ else if (this.promptTool === "scribble" && this.screenScribble.length > 1) {
439
+ targetCtx.lineCap = "round";
440
+ targetCtx.lineJoin = "round";
441
+ // Stroke thickness reflects the scribble brush size (diameter ≈ 2·radius).
442
+ targetCtx.lineWidth = Math.max(2, this.scribbleSize * 2);
443
+ targetCtx.beginPath();
444
+ targetCtx.moveTo(this.screenScribble[0].x, this.screenScribble[0].y);
445
+ for (let i = 1; i < this.screenScribble.length; i++) {
446
+ targetCtx.lineTo(this.screenScribble[i].x, this.screenScribble[i].y);
447
+ }
448
+ targetCtx.stroke();
449
+ }
450
+ targetCtx.restore();
451
+ }
452
+ // Scribble brush-size preview ring (when NOT dragging) — a circle centred on
453
+ // the cursor whose radius = scribbleSize, so the user sees the brush size and
454
+ // how the slider changes it (mirrors the paint brush's preview ring). Drawn
455
+ // every frame from the live hover position by copper3d's render loop.
456
+ if (this.promptTool === "scribble" && !this.dragging && this.hoverScreen) {
457
+ targetCtx.save();
458
+ const fg = this.polarity === 1;
459
+ targetCtx.strokeStyle = fg ? "rgba(120,255,180,0.9)" : "rgba(255,120,120,0.9)";
460
+ targetCtx.lineWidth = 1.5;
461
+ if (!fg)
462
+ targetCtx.setLineDash([4, 4]); // dashed for background (exclude), like the eraser
463
+ targetCtx.beginPath();
464
+ targetCtx.arc(this.hoverScreen.x, this.hoverScreen.y, Math.max(2, this.scribbleSize), 0, Math.PI * 2);
465
+ targetCtx.stroke();
466
+ targetCtx.restore();
467
+ }
468
+ }
469
+ }
470
+ //# sourceMappingURL=AiAssistTool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AiAssistTool.js","sourceRoot":"","sources":["../../../../src/Utils/segmentation/tools/AiAssistTool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,OAAO,EAAE,UAAU,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAG7D;yEACyE;AACzE,MAAM,CAAC,MAAM,gBAAgB,GAAG,WAAW,CAAC;AAoC5C,MAAM,OAAO,YAAa,SAAQ,QAAQ;IAwCxC,YAAY,GAAgB,EAAE,IAAsB;QAClD,KAAK,CAAC,GAAG,CAAC,CAAC;QAtCb,qFAAqF;QAC7E,eAAU,GAAsB,IAAI,CAAC;QAC7C,mEAAmE;QAC3D,iBAAY,GAAsB,IAAI,CAAC;QAC/C;;;uFAG+E;QACvE,iBAAY,GAAsB,IAAI,CAAC;QAEvC,eAAU,GAAiB,OAAO,CAAC;QACn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