@gridspace/raster-path 1.0.4 → 1.0.5

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.
@@ -1,2800 +0,0 @@
1
- /**
2
- * ═══════════════════════════════════════════════════════════════════════════
3
- * WebGPU Worker - GPU Compute Operations
4
- * ═══════════════════════════════════════════════════════════════════════════
5
- *
6
- * Offloads all WebGPU compute operations to a worker thread to prevent UI blocking.
7
- * Handles both planar (XY grid) and radial (cylindrical) rasterization modes.
8
- *
9
- * MESSAGE PROTOCOL:
10
- * ─────────────────
11
- * Main Thread → Worker:
12
- * 'init' - Initialize WebGPU device
13
- * 'rasterize-planar' - Rasterize geometry to XY grid
14
- * 'generate-toolpath-planar' - Generate planar toolpath from rasters
15
- * 'radial-generate-toolpaths'- Generate radial toolpaths (does rasterization + toolpath)
16
- *
17
- * Worker → Main Thread:
18
- * 'webgpu-ready' - Initialization complete
19
- * 'rasterize-complete' - Planar rasterization complete
20
- * 'rasterize-progress' - Progress update (0-1)
21
- * 'toolpath-complete' - Planar toolpath complete
22
- * 'toolpath-progress' - Progress update (0-1)
23
- * 'radial-toolpaths-complete' - Radial toolpaths complete
24
- *
25
- * ARCHITECTURE:
26
- * ─────────────
27
- * 1. PLANAR MODE:
28
- * - Rasterize terrain: XY grid, keep max Z per cell
29
- * - Rasterize tool: XY grid, keep min Z per cell
30
- * - Generate toolpath: Scan tool over terrain, compute Z-heights
31
- *
32
- * 2. RADIAL MODE:
33
- * - Batched processing: 360 angles per batch
34
- * - X-bucketing: Spatial partitioning to reduce triangle tests
35
- * - For each angle:
36
- * * Cast ray from origin
37
- * * Rasterize terrain triangles along ray
38
- * * Calculate tool-terrain collision
39
- * * Output Z-heights along X-axis
40
- *
41
- * MEMORY MANAGEMENT:
42
- * ──────────────────
43
- * - All GPU buffers are preallocated to known maximum sizes
44
- * - Triangle data transferred once per operation
45
- * - Output buffers mapped asynchronously to avoid blocking
46
- * - Worker maintains pipeline cache to avoid recompilation
47
- *
48
- * ═══════════════════════════════════════════════════════════════════════════
49
- */
50
-
51
- let config = {};
52
- let device = null;
53
- let deviceCapabilities = null;
54
- let isInitialized = false;
55
- let cachedRasterizePipeline = null;
56
- let cachedRasterizeShaderModule = null;
57
- let cachedToolpathPipeline = null;
58
- let cachedToolpathShaderModule = null;
59
- let cachedRadialBatchPipeline = null;
60
- let cachedRadialBatchShaderModule = null;
61
- let lastlog;
62
-
63
- const EMPTY_CELL = -1e10;
64
- const log_pre = '[Worker]';
65
- const diagnostic = false;
66
-
67
- const debug = {
68
- error: function() { console.error(log_pre, ...arguments) },
69
- warn: function() { console.warn(log_pre, ...arguments) },
70
- log: function() {
71
- if (!config.quiet) {
72
- let now = performance.now();
73
- let since = ((now - (lastlog ?? now)) | 0).toString().padStart(4,' ');
74
- console.log(log_pre, `[${since}]`, ...arguments);
75
- lastlog = now;
76
- }
77
- },
78
- ok: function() { console.log(log_pre, '✅', ...arguments) },
79
- };
80
-
81
- function round(v, d = 1) {
82
- return parseFloat(v.toFixed(d));
83
- }
84
-
85
- // Global error handler for uncaught errors in worker
86
- self.addEventListener('error', (event) => {
87
- debug.error('Uncaught error:', event.error || event.message);
88
- debug.error('Stack:', event.error?.stack);
89
- });
90
-
91
- self.addEventListener('unhandledrejection', (event) => {
92
- debug.error('Unhandled promise rejection:', event.reason);
93
- });
94
-
95
- // Initialize WebGPU device in worker context
96
- async function initWebGPU() {
97
- if (isInitialized) return true;
98
-
99
- if (!navigator.gpu) {
100
- debug.warn('WebGPU not supported');
101
- return false;
102
- }
103
-
104
- try {
105
- const adapter = await navigator.gpu.requestAdapter();
106
- if (!adapter) {
107
- debug.warn('WebGPU adapter not available');
108
- return false;
109
- }
110
-
111
- // Request device with higher limits for large meshes
112
- const adapterLimits = adapter.limits;
113
- debug.log('Adapter limits:', {
114
- maxStorageBufferBindingSize: adapterLimits.maxStorageBufferBindingSize,
115
- maxBufferSize: adapterLimits.maxBufferSize
116
- });
117
-
118
- device = await adapter.requestDevice({
119
- requiredLimits: {
120
- maxStorageBufferBindingSize: Math.min(
121
- adapterLimits.maxStorageBufferBindingSize,
122
- 1024 * 1024 * 1024 // Request up to 1GB
123
- ),
124
- maxBufferSize: Math.min(
125
- adapterLimits.maxBufferSize,
126
- 1024 * 1024 * 1024 // Request up to 1GB
127
- )
128
- }
129
- });
130
-
131
- // Pre-compile rasterize shader module (expensive operation)
132
- cachedRasterizeShaderModule = device.createShaderModule({ code: rasterizeShaderCode });
133
-
134
- // Pre-create rasterize pipeline (very expensive operation)
135
- cachedRasterizePipeline = device.createComputePipeline({
136
- layout: 'auto',
137
- compute: { module: cachedRasterizeShaderModule, entryPoint: 'main' },
138
- });
139
-
140
- // Pre-compile toolpath shader module
141
- cachedToolpathShaderModule = device.createShaderModule({ code: toolpathShaderCode });
142
-
143
- // Pre-create toolpath pipeline
144
- cachedToolpathPipeline = device.createComputePipeline({
145
- layout: 'auto',
146
- compute: { module: cachedToolpathShaderModule, entryPoint: 'main' },
147
- });
148
-
149
- // Pre-compile radial batch shader module
150
- cachedRadialBatchShaderModule = device.createShaderModule({ code: radialRasterizeShaderCode });
151
-
152
- // Pre-create radial batch pipeline
153
- cachedRadialBatchPipeline = device.createComputePipeline({
154
- layout: 'auto',
155
- compute: { module: cachedRadialBatchShaderModule, entryPoint: 'main' },
156
- });
157
-
158
- // Store device capabilities
159
- deviceCapabilities = {
160
- maxStorageBufferBindingSize: device.limits.maxStorageBufferBindingSize,
161
- maxBufferSize: device.limits.maxBufferSize,
162
- maxComputeWorkgroupSizeX: device.limits.maxComputeWorkgroupSizeX,
163
- maxComputeWorkgroupSizeY: device.limits.maxComputeWorkgroupSizeY,
164
- };
165
-
166
- isInitialized = true;
167
- debug.log('Initialized (pipelines cached)');
168
- return true;
169
- } catch (error) {
170
- debug.error('Failed to initialize:', error);
171
- return false;
172
- }
173
- }
174
-
175
- // Planar rasterization with spatial partitioning
176
- const rasterizeShaderCode = `// Planar rasterization with spatial partitioning
177
- // Sentinel value for empty cells (far below any real geometry)
178
- const EMPTY_CELL: f32 = -1e10;
179
-
180
- struct Uniforms {
181
- bounds_min_x: f32,
182
- bounds_min_y: f32,
183
- bounds_min_z: f32,
184
- bounds_max_x: f32,
185
- bounds_max_y: f32,
186
- bounds_max_z: f32,
187
- step_size: f32,
188
- grid_width: u32,
189
- grid_height: u32,
190
- triangle_count: u32,
191
- filter_mode: u32, // 0 = UPWARD (terrain, keep highest), 1 = DOWNWARD (tool, keep lowest)
192
- spatial_grid_width: u32,
193
- spatial_grid_height: u32,
194
- spatial_cell_size: f32,
195
- }
196
-
197
- @group(0) @binding(0) var<storage, read> triangles: array<f32>;
198
- @group(0) @binding(1) var<storage, read_write> output_points: array<f32>;
199
- @group(0) @binding(2) var<storage, read_write> valid_mask: array<u32>;
200
- @group(0) @binding(3) var<uniform> uniforms: Uniforms;
201
- @group(0) @binding(4) var<storage, read> spatial_cell_offsets: array<u32>;
202
- @group(0) @binding(5) var<storage, read> spatial_triangle_indices: array<u32>;
203
-
204
- // Fast 2D bounding box check for XY plane
205
- fn ray_hits_triangle_bbox_2d(ray_x: f32, ray_y: f32, v0: vec3<f32>, v1: vec3<f32>, v2: vec3<f32>) -> bool {
206
- // Add small epsilon to catch near-misses (mesh vertex gaps, FP rounding)
207
- let epsilon = 0.001; // 1 micron tolerance
208
- let min_x = min(min(v0.x, v1.x), v2.x) - epsilon;
209
- let max_x = max(max(v0.x, v1.x), v2.x) + epsilon;
210
- let min_y = min(min(v0.y, v1.y), v2.y) - epsilon;
211
- let max_y = max(max(v0.y, v1.y), v2.y) + epsilon;
212
-
213
- return ray_x >= min_x && ray_x <= max_x && ray_y >= min_y && ray_y <= max_y;
214
- }
215
-
216
- // Ray-triangle intersection using Möller-Trumbore algorithm
217
- fn ray_triangle_intersect(
218
- ray_origin: vec3<f32>,
219
- ray_dir: vec3<f32>,
220
- v0: vec3<f32>,
221
- v1: vec3<f32>,
222
- v2: vec3<f32>
223
- ) -> vec2<f32> { // Returns (hit: 0.0 or 1.0, z: intersection_z)
224
- // Larger epsilon needed because near-parallel triangles (small 'a') amplify errors via f=1/a
225
- let EPSILON = 0.0001;
226
-
227
- // Early rejection using 2D bounding box (very cheap!)
228
- if (!ray_hits_triangle_bbox_2d(ray_origin.x, ray_origin.y, v0, v1, v2)) {
229
- return vec2<f32>(0.0, 0.0);
230
- }
231
-
232
- // Calculate edges
233
- let edge1 = v1 - v0;
234
- let edge2 = v2 - v0;
235
-
236
- // Cross product: ray_dir × edge2
237
- let h = cross(ray_dir, edge2);
238
-
239
- // Dot product: edge1 · h
240
- let a = dot(edge1, h);
241
-
242
- if (a > -EPSILON && a < EPSILON) {
243
- return vec2<f32>(0.0, 0.0); // Ray parallel to triangle
244
- }
245
-
246
- let f = 1.0 / a;
247
-
248
- // s = ray_origin - v0
249
- let s = ray_origin - v0;
250
-
251
- // u = f * (s · h)
252
- let u = f * dot(s, h);
253
-
254
- // Allow tolerance for edges/vertices to ensure watertight coverage
255
- if (u < -EPSILON || u > 1.0 + EPSILON) {
256
- return vec2<f32>(0.0, 0.0);
257
- }
258
-
259
- // Cross product: s × edge1
260
- let q = cross(s, edge1);
261
-
262
- // v = f * (ray_dir · q)
263
- let v = f * dot(ray_dir, q);
264
-
265
- // Allow tolerance for edges/vertices to ensure watertight coverage
266
- if (v < -EPSILON || u + v > 1.0 + EPSILON) {
267
- return vec2<f32>(0.0, 0.0);
268
- }
269
-
270
- // t = f * (edge2 · q)
271
- let t = f * dot(edge2, q);
272
-
273
- if (t > EPSILON) {
274
- // Intersection found - calculate Z coordinate
275
- let intersection_z = ray_origin.z + ray_dir.z * t;
276
- return vec2<f32>(1.0, intersection_z);
277
- }
278
-
279
- return vec2<f32>(0.0, 0.0);
280
- }
281
-
282
- @compute @workgroup_size(16, 16)
283
- fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
284
- let grid_x = global_id.x;
285
- let grid_y = global_id.y;
286
-
287
- if (grid_x >= uniforms.grid_width || grid_y >= uniforms.grid_height) {
288
- return;
289
- }
290
-
291
- // Calculate world position for this grid point (center of cell)
292
- let world_x = uniforms.bounds_min_x + f32(grid_x) * uniforms.step_size;
293
- let world_y = uniforms.bounds_min_y + f32(grid_y) * uniforms.step_size;
294
-
295
- // Initialize best_z based on filter mode
296
- var best_z: f32;
297
- if (uniforms.filter_mode == 0u) {
298
- best_z = -1e10; // Terrain: keep highest Z
299
- } else {
300
- best_z = 1e10; // Tool: keep lowest Z
301
- }
302
-
303
- var found = false;
304
-
305
- // Ray from below mesh pointing up (+Z direction)
306
- let ray_origin = vec3<f32>(world_x, world_y, uniforms.bounds_min_z - 1.0);
307
- let ray_dir = vec3<f32>(0.0, 0.0, 1.0);
308
-
309
- // Find which spatial grid cell this ray belongs to
310
- let spatial_cell_x = u32((world_x - uniforms.bounds_min_x) / uniforms.spatial_cell_size);
311
- let spatial_cell_y = u32((world_y - uniforms.bounds_min_y) / uniforms.spatial_cell_size);
312
-
313
- // Clamp to spatial grid bounds
314
- let clamped_cx = min(spatial_cell_x, uniforms.spatial_grid_width - 1u);
315
- let clamped_cy = min(spatial_cell_y, uniforms.spatial_grid_height - 1u);
316
-
317
- let spatial_cell_idx = clamped_cy * uniforms.spatial_grid_width + clamped_cx;
318
-
319
- // Get triangle range for this cell
320
- let start_idx = spatial_cell_offsets[spatial_cell_idx];
321
- let end_idx = spatial_cell_offsets[spatial_cell_idx + 1u];
322
-
323
- // Test only triangles in this spatial cell
324
- for (var idx = start_idx; idx < end_idx; idx++) {
325
- let tri_idx = spatial_triangle_indices[idx];
326
- let tri_base = tri_idx * 9u;
327
-
328
- // Read triangle vertices (already in local space and rotated if needed)
329
- let v0 = vec3<f32>(
330
- triangles[tri_base],
331
- triangles[tri_base + 1u],
332
- triangles[tri_base + 2u]
333
- );
334
- let v1 = vec3<f32>(
335
- triangles[tri_base + 3u],
336
- triangles[tri_base + 4u],
337
- triangles[tri_base + 5u]
338
- );
339
- let v2 = vec3<f32>(
340
- triangles[tri_base + 6u],
341
- triangles[tri_base + 7u],
342
- triangles[tri_base + 8u]
343
- );
344
-
345
- let result = ray_triangle_intersect(ray_origin, ray_dir, v0, v1, v2);
346
- let hit = result.x;
347
- let intersection_z = result.y;
348
-
349
- if (hit > 0.5) {
350
- if (uniforms.filter_mode == 0u) {
351
- // Terrain: keep highest
352
- if (intersection_z > best_z) {
353
- best_z = intersection_z;
354
- found = true;
355
- }
356
- } else {
357
- // Tool: keep lowest
358
- if (intersection_z < best_z) {
359
- best_z = intersection_z;
360
- found = true;
361
- }
362
- }
363
- }
364
- }
365
-
366
- // Write output based on filter mode
367
- let output_idx = grid_y * uniforms.grid_width + grid_x;
368
-
369
- if (uniforms.filter_mode == 0u) {
370
- // Terrain: Dense output (Z-only, sentinel value for empty cells)
371
- if (found) {
372
- output_points[output_idx] = best_z;
373
- } else {
374
- output_points[output_idx] = EMPTY_CELL;
375
- }
376
- } else {
377
- // Tool: Sparse output (X, Y, Z triplets with valid mask)
378
- output_points[output_idx * 3u] = f32(grid_x);
379
- output_points[output_idx * 3u + 1u] = f32(grid_y);
380
- output_points[output_idx * 3u + 2u] = best_z;
381
-
382
- if (found) {
383
- valid_mask[output_idx] = 1u;
384
- } else {
385
- valid_mask[output_idx] = 0u;
386
- }
387
- }
388
- }
389
- `;
390
-
391
- // Planar toolpath generation
392
- const toolpathShaderCode = `// Planar toolpath generation
393
- // Sentinel value for empty terrain cells (must match rasterize shader)
394
- const EMPTY_CELL: f32 = -1e10;
395
- const MAX_F32: f32 = 3.402823466e+38;
396
-
397
- struct SparseToolPoint {
398
- x_offset: i32,
399
- y_offset: i32,
400
- z_value: f32,
401
- padding: f32,
402
- }
403
-
404
- struct Uniforms {
405
- terrain_width: u32,
406
- terrain_height: u32,
407
- tool_count: u32,
408
- x_step: u32,
409
- y_step: u32,
410
- oob_z: f32,
411
- points_per_line: u32,
412
- num_scanlines: u32,
413
- y_offset: u32, // Offset to center Y position (for single-scanline radial mode)
414
- }
415
-
416
- @group(0) @binding(0) var<storage, read> terrain_map: array<f32>;
417
- @group(0) @binding(1) var<storage, read> sparse_tool: array<SparseToolPoint>;
418
- @group(0) @binding(2) var<storage, read_write> output_path: array<f32>;
419
- @group(0) @binding(3) var<uniform> uniforms: Uniforms;
420
-
421
- @compute @workgroup_size(16, 16)
422
- fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
423
- let scanline = global_id.y;
424
- let point_idx = global_id.x;
425
-
426
- if (scanline >= uniforms.num_scanlines || point_idx >= uniforms.points_per_line) {
427
- return;
428
- }
429
-
430
- let tool_center_x = i32(point_idx * uniforms.x_step);
431
- let tool_center_y = i32(scanline * uniforms.y_step) + i32(uniforms.y_offset);
432
-
433
- // var max_collision_z = -MAX_F32; // Track maximum collision height
434
- var max_collision_z = uniforms.oob_z; // Track maximum collision height
435
- var found_collision = false;
436
-
437
- for (var i = 0u; i < uniforms.tool_count; i++) {
438
- let tool_point = sparse_tool[i];
439
- let terrain_x = tool_center_x + tool_point.x_offset;
440
- let terrain_y = tool_center_y + tool_point.y_offset;
441
-
442
- // Bounds check: X must always be in bounds
443
- if (terrain_x < 0 || terrain_x >= i32(uniforms.terrain_width)) {
444
- continue;
445
- }
446
-
447
- // Bounds check: Y must be within terrain strip bounds
448
- // For single-scanline OUTPUT mode, the tool center is at Y=0 but the terrain
449
- // strip contains the full Y range (tool width), so tool offsets access different Y rows
450
- if (terrain_y < 0 || terrain_y >= i32(uniforms.terrain_height)) {
451
- continue;
452
- }
453
-
454
- let terrain_idx = u32(terrain_y) * uniforms.terrain_width + u32(terrain_x);
455
- let terrain_z = terrain_map[terrain_idx];
456
-
457
- // Check if terrain cell has geometry (not empty sentinel value)
458
- if (terrain_z > EMPTY_CELL + 1.0) {
459
- // Tool z_value is positive offset from tip (tip=0, shaft=+50)
460
- // Subtract from terrain to find where tool center needs to be
461
- let collision_z = terrain_z + tool_point.z_value;
462
- max_collision_z = max(max_collision_z, collision_z);
463
- found_collision = true;
464
- }
465
- }
466
-
467
- var output_z = uniforms.oob_z;
468
- if (found_collision) {
469
- output_z = max_collision_z;
470
- }
471
-
472
- let output_idx = scanline * uniforms.points_per_line + point_idx;
473
- output_path[output_idx] = output_z;
474
- }
475
- `;
476
-
477
- // Radial: Rasterization with rotating ray planes and X-bucketing
478
- const radialRasterizeShaderCode = `// Radial V2 rasterization with X-bucketing and rotating ray planes
479
- // Sentinel value for empty cells (far below any real geometry)
480
- const EMPTY_CELL: f32 = -1e10;
481
- const PI: f32 = 3.14159265359;
482
-
483
- struct Uniforms {
484
- resolution: f32, // Grid step size (mm)
485
- angle_step: f32, // Radians between angles
486
- num_angles: u32, // Total number of angular strips
487
- max_radius: f32, // Ray origin distance from X-axis (maxHypot * 1.01)
488
- tool_width: f32, // Tool width in mm
489
- grid_y_height: u32, // Tool width in pixels (toolWidth / resolution)
490
- bucket_width: f32, // Width of each X-bucket (mm)
491
- bucket_grid_width: u32, // Bucket width in pixels
492
- global_min_x: f32, // Global minimum X coordinate
493
- z_floor: f32, // Z value for empty cells
494
- filter_mode: u32, // 0 = max Z (terrain), 1 = min Z (tool)
495
- num_buckets: u32, // Total number of X-buckets
496
- start_angle: f32, // Starting angle offset in radians (for batching)
497
- bucket_offset: u32, // Offset for bucket batching (bucket_idx in batch writes to bucket_offset + bucket_idx in output)
498
- }
499
-
500
- struct BucketInfo {
501
- min_x: f32, // Bucket X range start (mm)
502
- max_x: f32, // Bucket X range end (mm)
503
- start_index: u32, // Index into triangle_indices array
504
- count: u32 // Number of triangles in this bucket
505
- }
506
-
507
- @group(0) @binding(0) var<storage, read> triangles: array<f32>;
508
- @group(0) @binding(1) var<storage, read_write> output: array<f32>;
509
- @group(0) @binding(2) var<uniform> uniforms: Uniforms;
510
- @group(0) @binding(3) var<storage, read> bucket_info: array<BucketInfo>;
511
- @group(0) @binding(4) var<storage, read> triangle_indices: array<u32>;
512
-
513
- // Note: AABB early rejection removed - X-bucketing already provides spatial filtering
514
- // A proper ray-AABB intersection test would be needed if we wanted bounding box culling,
515
- // but checking if ray_origin is inside AABB was incorrect and rejected valid triangles
516
-
517
- // Ray-triangle intersection using Möller-Trumbore algorithm
518
- fn ray_triangle_intersect(
519
- ray_origin: vec3<f32>,
520
- ray_dir: vec3<f32>,
521
- v0: vec3<f32>,
522
- v1: vec3<f32>,
523
- v2: vec3<f32>
524
- ) -> vec2<f32> { // Returns (hit: 0.0 or 1.0, z: intersection_z)
525
- let EPSILON = 0.0001;
526
-
527
- // Calculate edges
528
- let edge1 = v1 - v0;
529
- let edge2 = v2 - v0;
530
-
531
- // Cross product: ray_dir × edge2
532
- let h = cross(ray_dir, edge2);
533
-
534
- // Dot product: edge1 · h
535
- let a = dot(edge1, h);
536
-
537
- if (a > -EPSILON && a < EPSILON) {
538
- return vec2<f32>(0.0, 0.0); // Ray parallel to triangle
539
- }
540
-
541
- let f = 1.0 / a;
542
-
543
- // s = ray_origin - v0
544
- let s = ray_origin - v0;
545
-
546
- // u = f * (s · h)
547
- let u = f * dot(s, h);
548
-
549
- if (u < -EPSILON || u > 1.0 + EPSILON) {
550
- return vec2<f32>(0.0, 0.0);
551
- }
552
-
553
- // Cross product: s × edge1
554
- let q = cross(s, edge1);
555
-
556
- // v = f * (ray_dir · q)
557
- let v = f * dot(ray_dir, q);
558
-
559
- if (v < -EPSILON || u + v > 1.0 + EPSILON) {
560
- return vec2<f32>(0.0, 0.0);
561
- }
562
-
563
- // t = f * (edge2 · q)
564
- let t = f * dot(edge2, q);
565
-
566
- if (t > EPSILON) {
567
- // Intersection found - return distance along ray (t parameter)
568
- return vec2<f32>(1.0, t);
569
- }
570
-
571
- return vec2<f32>(0.0, 0.0);
572
- }
573
-
574
- @compute @workgroup_size(8, 8, 1)
575
- fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
576
- let bucket_idx = global_id.z;
577
- let grid_y = global_id.y;
578
- let angle_idx = global_id.x;
579
-
580
- // Bounds check
581
- if (angle_idx >= uniforms.num_angles ||
582
- grid_y >= uniforms.grid_y_height ||
583
- bucket_idx >= uniforms.num_buckets) {
584
- return;
585
- }
586
-
587
- let bucket = bucket_info[bucket_idx];
588
- let angle = uniforms.start_angle + (f32(angle_idx) * uniforms.angle_step);
589
-
590
- // Calculate bucket min grid X
591
- let bucket_min_grid_x = u32((bucket.min_x - uniforms.global_min_x) / uniforms.resolution);
592
-
593
- // Loop over X within this bucket
594
- for (var local_x = 0u; local_x < uniforms.bucket_grid_width; local_x++) {
595
- let grid_x = bucket_min_grid_x + local_x;
596
- let world_x = uniforms.global_min_x + f32(grid_x) * uniforms.resolution;
597
-
598
- // Rotating top-down scan: normal XY planar scan, rotated around X-axis
599
- // Step 1: Define scan position in planar frame (X, Y, Z_above)
600
- let scan_x = world_x;
601
- let scan_y = f32(grid_y) * uniforms.resolution - uniforms.tool_width / 2.0;
602
- let scan_z = uniforms.max_radius; // Start above the model
603
-
604
- // Step 2: Rotate position (scan_x, scan_y, scan_z) around X-axis by 'angle'
605
- // X stays the same, rotate YZ plane: y' = y*cos - z*sin, z' = y*sin + z*cos
606
- // NOTE: This uses right-handed rotation (positive angle rotates +Y towards +Z)
607
- // To reverse rotation direction (left-handed or opposite), flip signs:
608
- // y' = y*cos + z*sin (flip sign on z term)
609
- // z' = -y*sin + z*cos (flip sign on y term)
610
- let ray_origin_x = scan_x;
611
- let ray_origin_y = scan_y * cos(angle) - scan_z * sin(angle);
612
- let ray_origin_z = scan_y * sin(angle) + scan_z * cos(angle);
613
- let ray_origin = vec3<f32>(ray_origin_x, ray_origin_y, ray_origin_z);
614
-
615
- // Step 3: Rotate ray direction (0, 0, -1) around X-axis by 'angle'
616
- // X component stays 0, rotate YZ: dy = 0*cos - (-1)*sin = sin, dz = 0*sin + (-1)*cos = -cos
617
- // NOTE: For reversed rotation, use: vec3<f32>(0.0, -sin(angle), -cos(angle))
618
- let ray_dir = vec3<f32>(0.0, sin(angle), -cos(angle));
619
-
620
- // Initialize best distance (closest hit)
621
- var best_t: f32 = 1e10; // Start with very large distance
622
- var found = false;
623
-
624
- // Ray-cast against triangles in this bucket
625
- for (var i = 0u; i < bucket.count; i++) {
626
- let tri_idx = triangle_indices[bucket.start_index + i];
627
- let tri_base = tri_idx * 9u;
628
-
629
- // Read triangle vertices
630
- let v0 = vec3<f32>(
631
- triangles[tri_base],
632
- triangles[tri_base + 1u],
633
- triangles[tri_base + 2u]
634
- );
635
- let v1 = vec3<f32>(
636
- triangles[tri_base + 3u],
637
- triangles[tri_base + 4u],
638
- triangles[tri_base + 5u]
639
- );
640
- let v2 = vec3<f32>(
641
- triangles[tri_base + 6u],
642
- triangles[tri_base + 7u],
643
- triangles[tri_base + 8u]
644
- );
645
-
646
- let result = ray_triangle_intersect(ray_origin, ray_dir, v0, v1, v2);
647
- let hit = result.x;
648
- let t = result.y; // Distance along ray
649
-
650
- if (hit > 0.5) {
651
- // Keep closest hit (minimum t)
652
- if (t < best_t) {
653
- best_t = t;
654
- found = true;
655
- }
656
- }
657
- }
658
-
659
- // Write output
660
- // Layout: (bucket_offset + bucket_idx) * numAngles * bucketWidth * gridHeight
661
- // + angle_idx * bucketWidth * gridHeight
662
- // + grid_y * bucketWidth
663
- // + local_x
664
- let output_idx = (uniforms.bucket_offset + bucket_idx) * uniforms.num_angles * uniforms.bucket_grid_width * uniforms.grid_y_height
665
- + angle_idx * uniforms.bucket_grid_width * uniforms.grid_y_height
666
- + grid_y * uniforms.bucket_grid_width
667
- + local_x;
668
-
669
- if (found) {
670
- // Terrain height = distance from scan origin minus ray travel distance
671
- // Ray started at max_radius from X-axis, traveled best_t distance to hit
672
- let terrain_height = uniforms.max_radius - best_t;
673
- output[output_idx] = terrain_height;
674
- } else {
675
- output[output_idx] = uniforms.z_floor;
676
- }
677
- }
678
- }
679
- `;
680
-
681
- // Calculate bounding box from triangle vertices
682
- function calculateBounds(triangles) {
683
- let min_x = Infinity, min_y = Infinity, min_z = Infinity;
684
- let max_x = -Infinity, max_y = -Infinity, max_z = -Infinity;
685
-
686
- for (let i = 0; i < triangles.length; i += 3) {
687
- const x = triangles[i];
688
- const y = triangles[i + 1];
689
- const z = triangles[i + 2];
690
-
691
- if (x < min_x) min_x = x;
692
- if (y < min_y) min_y = y;
693
- if (z < min_z) min_z = z;
694
- if (x > max_x) max_x = x;
695
- if (y > max_y) max_y = y;
696
- if (z > max_z) max_z = z;
697
- }
698
-
699
- return {
700
- min: { x: min_x, y: min_y, z: min_z },
701
- max: { x: max_x, y: max_y, z: max_z }
702
- };
703
- }
704
-
705
- // Build spatial grid for efficient triangle culling
706
- function buildSpatialGrid(triangles, bounds, cellSize = 5.0) {
707
- const gridWidth = Math.max(1, Math.ceil((bounds.max.x - bounds.min.x) / cellSize));
708
- const gridHeight = Math.max(1, Math.ceil((bounds.max.y - bounds.min.y) / cellSize));
709
- const totalCells = gridWidth * gridHeight;
710
-
711
- const grid = new Array(totalCells);
712
- for (let i = 0; i < totalCells; i++) {
713
- grid[i] = [];
714
- }
715
-
716
- const triangleCount = triangles.length / 9;
717
- for (let t = 0; t < triangleCount; t++) {
718
- const base = t * 9;
719
-
720
- const v0x = triangles[base], v0y = triangles[base + 1];
721
- const v1x = triangles[base + 3], v1y = triangles[base + 4];
722
- const v2x = triangles[base + 6], v2y = triangles[base + 7];
723
-
724
- // Add small epsilon to catch triangles near cell boundaries
725
- const epsilon = cellSize * 0.01; // 1% of cell size
726
- const minX = Math.min(v0x, v1x, v2x) - epsilon;
727
- const maxX = Math.max(v0x, v1x, v2x) + epsilon;
728
- const minY = Math.min(v0y, v1y, v2y) - epsilon;
729
- const maxY = Math.max(v0y, v1y, v2y) + epsilon;
730
-
731
- let minCellX = Math.floor((minX - bounds.min.x) / cellSize);
732
- let maxCellX = Math.floor((maxX - bounds.min.x) / cellSize);
733
- let minCellY = Math.floor((minY - bounds.min.y) / cellSize);
734
- let maxCellY = Math.floor((maxY - bounds.min.y) / cellSize);
735
-
736
- minCellX = Math.max(0, Math.min(gridWidth - 1, minCellX));
737
- maxCellX = Math.max(0, Math.min(gridWidth - 1, maxCellX));
738
- minCellY = Math.max(0, Math.min(gridHeight - 1, minCellY));
739
- maxCellY = Math.max(0, Math.min(gridHeight - 1, maxCellY));
740
-
741
- for (let cy = minCellY; cy <= maxCellY; cy++) {
742
- for (let cx = minCellX; cx <= maxCellX; cx++) {
743
- const cellIdx = cy * gridWidth + cx;
744
- grid[cellIdx].push(t);
745
- }
746
- }
747
- }
748
-
749
- let totalTriangleRefs = 0;
750
- for (let i = 0; i < totalCells; i++) {
751
- totalTriangleRefs += grid[i].length;
752
- }
753
-
754
- const cellOffsets = new Uint32Array(totalCells + 1);
755
- const triangleIndices = new Uint32Array(totalTriangleRefs);
756
-
757
- let currentOffset = 0;
758
- for (let i = 0; i < totalCells; i++) {
759
- cellOffsets[i] = currentOffset;
760
- for (let j = 0; j < grid[i].length; j++) {
761
- triangleIndices[currentOffset++] = grid[i][j];
762
- }
763
- }
764
- cellOffsets[totalCells] = currentOffset;
765
-
766
- const avgPerCell = totalTriangleRefs / totalCells;
767
-
768
- // Calculate actual tool diameter from bounds for logging
769
- const toolWidth = bounds.max.x - bounds.min.x;
770
- const toolHeight = bounds.max.y - bounds.min.y;
771
- const toolDiameter = Math.max(toolWidth, toolHeight);
772
-
773
- debug.log(`Spatial grid: ${gridWidth}x${gridHeight} ${totalTriangleRefs} tri-refs ~${avgPerCell.toFixed(0)}/${cellSize}mm cell (tool: ${toolDiameter.toFixed(2)}mm)`);
774
-
775
- return {
776
- gridWidth,
777
- gridHeight,
778
- cellSize,
779
- cellOffsets,
780
- triangleIndices,
781
- avgTrianglesPerCell: avgPerCell
782
- };
783
- }
784
-
785
- // Create reusable GPU buffers for multiple rasterization passes (e.g., radial rotations)
786
-
787
- // Rasterize mesh to point cloud
788
- // Internal function - rasterize without tiling (do not modify this function!)
789
- async function rasterizeMeshSingle(triangles, stepSize, filterMode, options = {}) {
790
- const startTime = performance.now();
791
-
792
- if (!isInitialized) {
793
- const initStart = performance.now();
794
- const success = await initWebGPU();
795
- if (!success) {
796
- throw new Error('WebGPU not available');
797
- }
798
- const initEnd = performance.now();
799
- debug.log(`First-time init: ${(initEnd - initStart).toFixed(1)}ms`);
800
- }
801
-
802
- // debug.log(`Raster ${triangles.length / 9} triangles (step ${stepSize}mm, mode ${filterMode})...`);
803
-
804
- // Extract options
805
- // boundsOverride: Optional manual bounds to avoid recalculating from triangles
806
- // Useful when bounds are already known (e.g., from tiling operations)
807
- const boundsOverride = options.bounds || options.min ? options : null;
808
-
809
- // Use bounds override if provided, otherwise calculate from triangles
810
- const bounds = boundsOverride || calculateBounds(triangles);
811
-
812
- if (boundsOverride) {
813
- // debug.log(`Using bounds override: min(${bounds.min.x.toFixed(2)}, ${bounds.min.y.toFixed(2)}, ${bounds.min.z.toFixed(2)}) max(${bounds.max.x.toFixed(2)}, ${bounds.max.y.toFixed(2)}, ${bounds.max.z.toFixed(2)})`);
814
-
815
- // Validate bounds
816
- if (bounds.min.x >= bounds.max.x || bounds.min.y >= bounds.max.y || bounds.min.z >= bounds.max.z) {
817
- throw new Error(`Invalid bounds: min must be less than max. Got min(${bounds.min.x}, ${bounds.min.y}, ${bounds.min.z}) max(${bounds.max.x}, ${bounds.max.y}, ${bounds.max.z})`);
818
- }
819
- }
820
-
821
- const gridWidth = Math.ceil((bounds.max.x - bounds.min.x) / stepSize) + 1;
822
- const gridHeight = Math.ceil((bounds.max.y - bounds.min.y) / stepSize) + 1;
823
- const totalGridPoints = gridWidth * gridHeight;
824
-
825
- // debug.log(`Grid: ${gridWidth}x${gridHeight} = ${totalGridPoints.toLocaleString()} points`);
826
-
827
- // Calculate buffer size based on filter mode
828
- // filterMode 0 (terrain): Dense Z-only output (1 float per grid cell)
829
- // filterMode 1 (tool): Sparse X,Y,Z output (3 floats per grid cell)
830
- const floatsPerPoint = filterMode === 0 ? 1 : 3;
831
- const outputSize = totalGridPoints * floatsPerPoint * 4;
832
- const maxBufferSize = device.limits.maxBufferSize || 268435456; // 256MB default
833
- // const modeStr = filterMode === 0 ? 'terrain (dense Z-only)' : 'tool (sparse XYZ)';
834
- // debug.log(`Output buffer size: ${(outputSize / 1024 / 1024).toFixed(2)} MB for ${modeStr} (max: ${(maxBufferSize / 1024 / 1024).toFixed(2)} MB)`);
835
-
836
- if (outputSize > maxBufferSize) {
837
- throw new Error(`Output buffer too large: ${(outputSize / 1024 / 1024).toFixed(2)} MB exceeds device limit of ${(maxBufferSize / 1024 / 1024).toFixed(2)} MB. Try a larger step size.`);
838
- }
839
-
840
- console.time(`${log_pre} Build Spatial Grid`);
841
- const spatialGrid = buildSpatialGrid(triangles, bounds);
842
- console.timeEnd(`${log_pre} Build Spatial Grid`);
843
-
844
- // Create buffers
845
- const triangleBuffer = device.createBuffer({
846
- size: triangles.byteLength,
847
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
848
- });
849
- device.queue.writeBuffer(triangleBuffer, 0, triangles);
850
-
851
- // Create and INITIALIZE output buffer (GPU buffers contain garbage by default!)
852
- const outputBuffer = device.createBuffer({
853
- size: outputSize,
854
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
855
- });
856
-
857
- // Initialize output buffer with sentinel value for terrain, zeros for tool
858
- if (filterMode === 0) {
859
- // Terrain: initialize with EMPTY_CELL sentinel value
860
- const initData = new Float32Array(totalGridPoints);
861
- initData.fill(EMPTY_CELL);
862
- device.queue.writeBuffer(outputBuffer, 0, initData);
863
- }
864
- // Tool mode: zeros are fine (will check valid mask)
865
-
866
- const validMaskBuffer = device.createBuffer({
867
- size: totalGridPoints * 4,
868
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
869
- });
870
-
871
- const spatialCellOffsetsBuffer = device.createBuffer({
872
- size: spatialGrid.cellOffsets.byteLength,
873
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
874
- });
875
- device.queue.writeBuffer(spatialCellOffsetsBuffer, 0, spatialGrid.cellOffsets);
876
-
877
- const spatialTriangleIndicesBuffer = device.createBuffer({
878
- size: spatialGrid.triangleIndices.byteLength,
879
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
880
- });
881
- device.queue.writeBuffer(spatialTriangleIndicesBuffer, 0, spatialGrid.triangleIndices);
882
-
883
- // Uniforms
884
- const uniformData = new Float32Array([
885
- bounds.min.x, bounds.min.y, bounds.min.z,
886
- bounds.max.x, bounds.max.y, bounds.max.z,
887
- stepSize,
888
- 0, 0, 0, 0, 0, 0, 0 // Padding for alignment
889
- ]);
890
- const uniformDataU32 = new Uint32Array(uniformData.buffer);
891
- uniformDataU32[7] = gridWidth;
892
- uniformDataU32[8] = gridHeight;
893
- uniformDataU32[9] = triangles.length / 9;
894
- uniformDataU32[10] = filterMode;
895
- uniformDataU32[11] = spatialGrid.gridWidth;
896
- uniformDataU32[12] = spatialGrid.gridHeight;
897
- const uniformDataF32 = new Float32Array(uniformData.buffer);
898
- uniformDataF32[13] = spatialGrid.cellSize;
899
-
900
- // Check for u32 overflow
901
- const maxU32 = 4294967295;
902
- if (gridWidth > maxU32 || gridHeight > maxU32) {
903
- throw new Error(`Grid dimensions exceed u32 max: ${gridWidth}x${gridHeight}`);
904
- }
905
-
906
- // debug.log(`Uniforms: gridWidth=${gridWidth}, gridHeight=${gridHeight}, triangles=${triangles.length / 9}`);
907
-
908
- const uniformBuffer = device.createBuffer({
909
- size: uniformData.byteLength,
910
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
911
- });
912
- device.queue.writeBuffer(uniformBuffer, 0, uniformData);
913
-
914
- // CRITICAL: Wait for all writeBuffer operations to complete before compute dispatch
915
- await device.queue.onSubmittedWorkDone();
916
-
917
- // Use cached pipeline
918
- const bindGroup = device.createBindGroup({
919
- layout: cachedRasterizePipeline.getBindGroupLayout(0),
920
- entries: [
921
- { binding: 0, resource: { buffer: triangleBuffer } },
922
- { binding: 1, resource: { buffer: outputBuffer } },
923
- { binding: 2, resource: { buffer: validMaskBuffer } },
924
- { binding: 3, resource: { buffer: uniformBuffer } },
925
- { binding: 4, resource: { buffer: spatialCellOffsetsBuffer } },
926
- { binding: 5, resource: { buffer: spatialTriangleIndicesBuffer } },
927
- ],
928
- });
929
-
930
- // Dispatch compute shader
931
- const commandEncoder = device.createCommandEncoder();
932
- const passEncoder = commandEncoder.beginComputePass();
933
- passEncoder.setPipeline(cachedRasterizePipeline);
934
- passEncoder.setBindGroup(0, bindGroup);
935
-
936
- const workgroupsX = Math.ceil(gridWidth / 16);
937
- const workgroupsY = Math.ceil(gridHeight / 16);
938
-
939
- // Check dispatch limits
940
- const maxWorkgroupsPerDim = device.limits.maxComputeWorkgroupsPerDimension || 65535;
941
-
942
- if (workgroupsX > maxWorkgroupsPerDim || workgroupsY > maxWorkgroupsPerDim) {
943
- throw new Error(`Workgroup dispatch too large: ${workgroupsX}x${workgroupsY} exceeds limit of ${maxWorkgroupsPerDim}. Try a larger step size.`);
944
- }
945
-
946
- passEncoder.dispatchWorkgroups(workgroupsX, workgroupsY);
947
- passEncoder.end();
948
-
949
- // Create staging buffers for readback
950
- const stagingOutputBuffer = device.createBuffer({
951
- size: outputSize,
952
- usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
953
- });
954
-
955
- const stagingValidMaskBuffer = device.createBuffer({
956
- size: totalGridPoints * 4,
957
- usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
958
- });
959
-
960
- commandEncoder.copyBufferToBuffer(outputBuffer, 0, stagingOutputBuffer, 0, outputSize);
961
- commandEncoder.copyBufferToBuffer(validMaskBuffer, 0, stagingValidMaskBuffer, 0, totalGridPoints * 4);
962
-
963
- device.queue.submit([commandEncoder.finish()]);
964
-
965
- // Wait for GPU to finish
966
- await device.queue.onSubmittedWorkDone();
967
-
968
- // Read back results
969
- await stagingOutputBuffer.mapAsync(GPUMapMode.READ);
970
- await stagingValidMaskBuffer.mapAsync(GPUMapMode.READ);
971
-
972
- const outputData = new Float32Array(stagingOutputBuffer.getMappedRange());
973
- const validMaskData = new Uint32Array(stagingValidMaskBuffer.getMappedRange());
974
-
975
- let result, pointCount;
976
-
977
- if (filterMode === 0) {
978
- // Terrain: Dense output (Z-only), no compaction needed
979
- // Copy the full array (already has NaN for empty cells)
980
- result = new Float32Array(outputData);
981
- pointCount = totalGridPoints;
982
-
983
- if (config.debug) {
984
- // Count valid points for logging (sentinel value = -1e10)
985
- let zeroCount = 0;
986
- let validCount = 0;
987
- for (let i = 0; i < totalGridPoints; i++) {
988
- if (result[i] > EMPTY_CELL + 1) validCount++; // Any value significantly above sentinel
989
- if (result[i] === 0) zeroCount++;
990
- }
991
-
992
- let percentHit = validCount/totalGridPoints;
993
- if (zeroCount > 0 || percentHit < 0.5 ) {
994
- debug.log(totalGridPoints, 'cells,', round(percentHit*100), '% coverage,', zeroCount, 'zeros');
995
- }
996
- }
997
- } else {
998
- // Tool: Sparse output (X,Y,Z triplets), compact to remove invalid points
999
- const validPoints = [];
1000
- for (let i = 0; i < totalGridPoints; i++) {
1001
- if (validMaskData[i] === 1) {
1002
- validPoints.push(
1003
- outputData[i * 3],
1004
- outputData[i * 3 + 1],
1005
- outputData[i * 3 + 2]
1006
- );
1007
- }
1008
- }
1009
- result = new Float32Array(validPoints);
1010
- pointCount = validPoints.length / 3;
1011
- }
1012
-
1013
- stagingOutputBuffer.unmap();
1014
- stagingValidMaskBuffer.unmap();
1015
-
1016
- // Cleanup
1017
- triangleBuffer.destroy();
1018
- outputBuffer.destroy();
1019
- validMaskBuffer.destroy();
1020
- uniformBuffer.destroy();
1021
- spatialCellOffsetsBuffer.destroy();
1022
- spatialTriangleIndicesBuffer.destroy();
1023
- stagingOutputBuffer.destroy();
1024
- stagingValidMaskBuffer.destroy();
1025
-
1026
- const endTime = performance.now();
1027
- const conversionTime = endTime - startTime;
1028
- // debug.log(`Rasterize complete: ${pointCount} points in ${conversionTime.toFixed(1)}ms`);
1029
- // debug.log(`Bounds: min(${bounds.min.x.toFixed(2)}, ${bounds.min.y.toFixed(2)}, ${bounds.min.z.toFixed(2)}) max(${bounds.max.x.toFixed(2)}, ${bounds.max.y.toFixed(2)}, ${bounds.max.z.toFixed(2)})`);
1030
-
1031
- // Verify result data integrity
1032
- if (filterMode === 0) {
1033
- // Terrain: Dense Z-only format
1034
- if (result.length > 0) {
1035
- const firstZ = result[0] <= EMPTY_CELL + 1 ? 'EMPTY' : result[0].toFixed(3);
1036
- const lastZ = result[result.length-1] <= EMPTY_CELL + 1 ? 'EMPTY' : result[result.length-1].toFixed(3);
1037
- // debug.log(`First Z: ${firstZ}, Last Z: ${lastZ}`);
1038
- }
1039
- } else {
1040
- // Tool: Sparse X,Y,Z format
1041
- if (result.length > 0) {
1042
- const firstPoint = `(${result[0].toFixed(3)}, ${result[1].toFixed(3)}, ${result[2].toFixed(3)})`;
1043
- const lastIdx = result.length - 3;
1044
- const lastPoint = `(${result[lastIdx].toFixed(3)}, ${result[lastIdx+1].toFixed(3)}, ${result[lastIdx+2].toFixed(3)})`;
1045
- // debug.log(`First point: ${firstPoint}, Last point: ${lastPoint}`);
1046
- }
1047
- }
1048
-
1049
- return {
1050
- positions: result,
1051
- pointCount: pointCount,
1052
- bounds: bounds,
1053
- conversionTime: conversionTime,
1054
- gridWidth: gridWidth,
1055
- gridHeight: gridHeight,
1056
- isDense: filterMode === 0 // True for terrain (dense), false for tool (sparse)
1057
- };
1058
- }
1059
-
1060
- // Create tiles for tiled rasterization
1061
- function createTiles(bounds, stepSize, maxMemoryBytes) {
1062
- const width = bounds.max.x - bounds.min.x;
1063
- const height = bounds.max.y - bounds.min.y;
1064
- const aspectRatio = width / height;
1065
-
1066
- // Calculate how many grid points we can fit in one tile
1067
- // Terrain uses dense Z-only format: (gridW * gridH * 1 * 4) for output
1068
- // This is 4x more efficient than the old sparse format (16 bytes → 4 bytes per point)
1069
- const bytesPerPoint = 1 * 4; // 4 bytes per grid point (Z-only)
1070
- const maxPointsPerTile = Math.floor(maxMemoryBytes / bytesPerPoint);
1071
- debug.log(`Dense terrain format: ${bytesPerPoint} bytes/point (was 16), can fit ${(maxPointsPerTile/1e6).toFixed(1)}M points per tile`);
1072
-
1073
- // Calculate optimal tile grid dimensions while respecting aspect ratio
1074
- // We want: tileGridW * tileGridH <= maxPointsPerTile
1075
- // And: tileGridW / tileGridH ≈ aspectRatio
1076
-
1077
- let tileGridW, tileGridH;
1078
- if (aspectRatio >= 1) {
1079
- // Width >= Height
1080
- tileGridH = Math.floor(Math.sqrt(maxPointsPerTile / aspectRatio));
1081
- tileGridW = Math.floor(tileGridH * aspectRatio);
1082
- } else {
1083
- // Height > Width
1084
- tileGridW = Math.floor(Math.sqrt(maxPointsPerTile * aspectRatio));
1085
- tileGridH = Math.floor(tileGridW / aspectRatio);
1086
- }
1087
-
1088
- // Ensure we don't exceed limits
1089
- while (tileGridW * tileGridH * bytesPerPoint > maxMemoryBytes) {
1090
- if (tileGridW > tileGridH) {
1091
- tileGridW--;
1092
- } else {
1093
- tileGridH--;
1094
- }
1095
- }
1096
-
1097
- // Convert grid dimensions to world dimensions
1098
- const tileWidth = tileGridW * stepSize;
1099
- const tileHeight = tileGridH * stepSize;
1100
-
1101
- // Calculate number of tiles needed
1102
- const tilesX = Math.ceil(width / tileWidth);
1103
- const tilesY = Math.ceil(height / tileHeight);
1104
-
1105
- // Calculate actual tile dimensions (distribute evenly)
1106
- const actualTileWidth = width / tilesX;
1107
- const actualTileHeight = height / tilesY;
1108
-
1109
- debug.log(`Creating ${tilesX}x${tilesY} = ${tilesX * tilesY} tiles (${actualTileWidth.toFixed(2)}mm × ${actualTileHeight.toFixed(2)}mm each)`);
1110
- debug.log(`Tile grid: ${Math.ceil(actualTileWidth / stepSize)}x${Math.ceil(actualTileHeight / stepSize)} points per tile`);
1111
-
1112
- const tiles = [];
1113
- const overlap = stepSize * 2; // Overlap by 2 grid cells to ensure no gaps
1114
-
1115
- for (let ty = 0; ty < tilesY; ty++) {
1116
- for (let tx = 0; tx < tilesX; tx++) {
1117
- // Calculate base tile bounds (no overlap)
1118
- let tileMinX = bounds.min.x + (tx * actualTileWidth);
1119
- let tileMinY = bounds.min.y + (ty * actualTileHeight);
1120
- let tileMaxX = Math.min(bounds.max.x, tileMinX + actualTileWidth);
1121
- let tileMaxY = Math.min(bounds.max.y, tileMinY + actualTileHeight);
1122
-
1123
- // Add overlap (except at outer edges) - but DON'T extend beyond global bounds
1124
- if (tx > 0) tileMinX = Math.max(bounds.min.x, tileMinX - overlap);
1125
- if (ty > 0) tileMinY = Math.max(bounds.min.y, tileMinY - overlap);
1126
- if (tx < tilesX - 1) tileMaxX = Math.min(bounds.max.x, tileMaxX + overlap);
1127
- if (ty < tilesY - 1) tileMaxY = Math.min(bounds.max.y, tileMaxY + overlap);
1128
-
1129
- tiles.push({
1130
- id: `tile_${tx}_${ty}`,
1131
- bounds: {
1132
- min: { x: tileMinX, y: tileMinY, z: bounds.min.z },
1133
- max: { x: tileMaxX, y: tileMaxY, z: bounds.max.z }
1134
- }
1135
- });
1136
- }
1137
- }
1138
-
1139
- return { tiles, tilesX, tilesY };
1140
- }
1141
-
1142
- // Stitch tiles from multiple rasterization passes
1143
- function stitchTiles(tileResults, fullBounds, stepSize) {
1144
- if (tileResults.length === 0) {
1145
- throw new Error('No tile results to stitch');
1146
- }
1147
-
1148
- // Check if results are dense (terrain) or sparse (tool)
1149
- const isDense = tileResults[0].isDense;
1150
-
1151
- if (isDense) {
1152
- // DENSE TERRAIN STITCHING: Simple array copying (Z-only format)
1153
- debug.log(`Stitching ${tileResults.length} dense terrain tiles...`);
1154
-
1155
- // Calculate global grid dimensions
1156
- const globalWidth = Math.ceil((fullBounds.max.x - fullBounds.min.x) / stepSize) + 1;
1157
- const globalHeight = Math.ceil((fullBounds.max.y - fullBounds.min.y) / stepSize) + 1;
1158
- const totalGridCells = globalWidth * globalHeight;
1159
-
1160
- // Allocate global dense grid (Z-only), initialize to sentinel value
1161
- const globalGrid = new Float32Array(totalGridCells);
1162
- globalGrid.fill(EMPTY_CELL);
1163
-
1164
- debug.log(`Global grid: ${globalWidth}x${globalHeight} = ${totalGridCells.toLocaleString()} cells`);
1165
-
1166
- // Copy each tile's Z-values to the correct position in global grid
1167
- for (const tile of tileResults) {
1168
- // Calculate tile's position in global grid
1169
- const tileOffsetX = Math.round((tile.tileBounds.min.x - fullBounds.min.x) / stepSize);
1170
- const tileOffsetY = Math.round((tile.tileBounds.min.y - fullBounds.min.y) / stepSize);
1171
-
1172
- const tileWidth = tile.gridWidth;
1173
- const tileHeight = tile.gridHeight;
1174
-
1175
- // Copy Z-values row by row
1176
- for (let ty = 0; ty < tileHeight; ty++) {
1177
- const globalY = tileOffsetY + ty;
1178
- if (globalY >= globalHeight) continue;
1179
-
1180
- for (let tx = 0; tx < tileWidth; tx++) {
1181
- const globalX = tileOffsetX + tx;
1182
- if (globalX >= globalWidth) continue;
1183
-
1184
- const tileIdx = ty * tileWidth + tx;
1185
- const globalIdx = globalY * globalWidth + globalX;
1186
- const tileZ = tile.positions[tileIdx];
1187
-
1188
- // For overlapping cells, keep max Z (terrain surface)
1189
- // Skip empty cells (sentinel value)
1190
- if (tileZ > EMPTY_CELL + 1) {
1191
- const existingZ = globalGrid[globalIdx];
1192
- if (existingZ <= EMPTY_CELL + 1 || tileZ > existingZ) {
1193
- globalGrid[globalIdx] = tileZ;
1194
- }
1195
- }
1196
- }
1197
- }
1198
- }
1199
-
1200
- // Count valid cells (above sentinel value)
1201
- let validCount = 0;
1202
- for (let i = 0; i < totalGridCells; i++) {
1203
- if (globalGrid[i] > EMPTY_CELL + 1) validCount++;
1204
- }
1205
-
1206
- debug.log(`Stitched: ${totalGridCells} total cells, ${validCount} with geometry (${(validCount/totalGridCells*100).toFixed(1)}% coverage)`);
1207
-
1208
- return {
1209
- positions: globalGrid,
1210
- pointCount: totalGridCells,
1211
- bounds: fullBounds,
1212
- gridWidth: globalWidth,
1213
- gridHeight: globalHeight,
1214
- isDense: true,
1215
- conversionTime: tileResults.reduce((sum, r) => sum + (r.conversionTime || 0), 0),
1216
- tileCount: tileResults.length
1217
- };
1218
-
1219
- } else {
1220
- // SPARSE TOOL STITCHING: Keep existing deduplication logic (X,Y,Z triplets)
1221
- debug.log(`Stitching ${tileResults.length} sparse tool tiles...`);
1222
-
1223
- const pointMap = new Map();
1224
-
1225
- for (const result of tileResults) {
1226
- const positions = result.positions;
1227
-
1228
- // Calculate offset from tile origin to global origin (in grid cells)
1229
- const tileOffsetX = Math.round((result.tileBounds.min.x - fullBounds.min.x) / stepSize);
1230
- const tileOffsetY = Math.round((result.tileBounds.min.y - fullBounds.min.y) / stepSize);
1231
-
1232
- // Convert each point from tile-local to global grid coordinates
1233
- for (let i = 0; i < positions.length; i += 3) {
1234
- const localGridX = positions[i];
1235
- const localGridY = positions[i + 1];
1236
- const z = positions[i + 2];
1237
-
1238
- // Convert local grid indices to global grid indices
1239
- const globalGridX = localGridX + tileOffsetX;
1240
- const globalGridY = localGridY + tileOffsetY;
1241
-
1242
- const key = `${globalGridX},${globalGridY}`;
1243
- const existing = pointMap.get(key);
1244
-
1245
- // Keep lowest Z value (for tool)
1246
- if (!existing || z < existing.z) {
1247
- pointMap.set(key, { x: globalGridX, y: globalGridY, z });
1248
- }
1249
- }
1250
- }
1251
-
1252
- // Convert Map to flat array
1253
- const finalPointCount = pointMap.size;
1254
- const allPositions = new Float32Array(finalPointCount * 3);
1255
- let writeOffset = 0;
1256
-
1257
- for (const point of pointMap.values()) {
1258
- allPositions[writeOffset++] = point.x;
1259
- allPositions[writeOffset++] = point.y;
1260
- allPositions[writeOffset++] = point.z;
1261
- }
1262
-
1263
- debug.log(`Stitched: ${finalPointCount} unique sparse points`);
1264
-
1265
- return {
1266
- positions: allPositions,
1267
- pointCount: finalPointCount,
1268
- bounds: fullBounds,
1269
- isDense: false,
1270
- conversionTime: tileResults.reduce((sum, r) => sum + (r.conversionTime || 0), 0),
1271
- tileCount: tileResults.length
1272
- };
1273
- }
1274
- }
1275
-
1276
- // Check if tiling is needed (only called for terrain, which uses dense format)
1277
- function shouldUseTiling(bounds, stepSize) {
1278
- if (!config || !config.autoTiling) return false;
1279
- if (!deviceCapabilities) return false;
1280
-
1281
- const gridWidth = Math.ceil((bounds.max.x - bounds.min.x) / stepSize) + 1;
1282
- const gridHeight = Math.ceil((bounds.max.y - bounds.min.y) / stepSize) + 1;
1283
- const totalPoints = gridWidth * gridHeight;
1284
-
1285
- // Terrain uses dense Z-only format: 1 float (4 bytes) per grid cell
1286
- const gpuOutputBuffer = totalPoints * 1 * 4;
1287
- const totalGPUMemory = gpuOutputBuffer; // No mask needed for dense output
1288
-
1289
- // Use the smaller of configured limit or device capability
1290
- const configuredLimit = config.maxGPUMemoryMB * 1024 * 1024;
1291
- const deviceLimit = deviceCapabilities.maxStorageBufferBindingSize;
1292
- const maxSafeSize = Math.min(configuredLimit, deviceLimit) * config.gpuMemorySafetyMargin;
1293
-
1294
- return totalGPUMemory > maxSafeSize;
1295
- }
1296
-
1297
- // Rasterize mesh - wrapper that handles automatic tiling if needed
1298
- async function rasterizeMesh(triangles, stepSize, filterMode, options = {}) {
1299
- const boundsOverride = options.bounds || options.min ? options : null; // Support old and new format
1300
- const bounds = boundsOverride || calculateBounds(triangles);
1301
-
1302
- // Check if tiling is needed
1303
- if (shouldUseTiling(bounds, stepSize)) {
1304
- debug.log('Tiling required - switching to tiled rasterization');
1305
-
1306
- // Calculate max safe size per tile
1307
- const configuredLimit = config.maxGPUMemoryMB * 1024 * 1024;
1308
- const deviceLimit = deviceCapabilities.maxStorageBufferBindingSize;
1309
- const maxSafeSize = Math.min(configuredLimit, deviceLimit) * config.gpuMemorySafetyMargin;
1310
-
1311
- // Create tiles
1312
- const { tiles } = createTiles(bounds, stepSize, maxSafeSize);
1313
-
1314
- // Rasterize each tile
1315
- const tileResults = [];
1316
- for (let i = 0; i < tiles.length; i++) {
1317
- const tileStart = performance.now();
1318
- debug.log(`Processing tile ${i + 1}/${tiles.length}: ${tiles[i].id}`);
1319
- debug.log(` Tile bounds: min(${tiles[i].bounds.min.x.toFixed(2)}, ${tiles[i].bounds.min.y.toFixed(2)}) max(${tiles[i].bounds.max.x.toFixed(2)}, ${tiles[i].bounds.max.y.toFixed(2)})`);
1320
-
1321
- const tileResult = await rasterizeMeshSingle(triangles, stepSize, filterMode, {
1322
- ...tiles[i].bounds,
1323
- });
1324
-
1325
- const tileTime = performance.now() - tileStart;
1326
- debug.log(` Tile ${i + 1} complete: ${tileResult.pointCount} points in ${tileTime.toFixed(1)}ms`);
1327
-
1328
- // Store tile bounds with result for coordinate conversion during stitching
1329
- tileResult.tileBounds = tiles[i].bounds;
1330
- tileResults.push(tileResult);
1331
- }
1332
-
1333
- // Stitch tiles together (pass full bounds and step size for coordinate conversion)
1334
- return stitchTiles(tileResults, bounds, stepSize);
1335
- } else {
1336
- // Single-pass rasterization
1337
- return await rasterizeMeshSingle(triangles, stepSize, filterMode, options);
1338
- }
1339
- }
1340
-
1341
- // Helper: Create height map from dense terrain points (Z-only array)
1342
- // Terrain is ALWAYS dense (Z-only), never sparse
1343
- function createHeightMapFromPoints(points, gridStep, bounds = null) {
1344
- if (!points || points.length === 0) {
1345
- throw new Error('No points provided');
1346
- }
1347
-
1348
- // Calculate dimensions from bounds
1349
- if (!bounds) {
1350
- throw new Error('Bounds required for height map creation');
1351
- }
1352
-
1353
- const minX = bounds.min.x;
1354
- const minY = bounds.min.y;
1355
- const minZ = bounds.min.z;
1356
- const maxX = bounds.max.x;
1357
- const maxY = bounds.max.y;
1358
- const maxZ = bounds.max.z;
1359
- const width = Math.ceil((maxX - minX) / gridStep) + 1;
1360
- const height = Math.ceil((maxY - minY) / gridStep) + 1;
1361
-
1362
- // Terrain is ALWAYS dense (Z-only format from GPU rasterizer)
1363
- // debug.log(`Terrain dense format: ${width}x${height} = ${points.length} cells`);
1364
-
1365
- return {
1366
- grid: points, // Dense Z-only array
1367
- width,
1368
- height,
1369
- minX,
1370
- minY,
1371
- minZ,
1372
- maxX,
1373
- maxY,
1374
- maxZ
1375
- };
1376
- }
1377
-
1378
- // Helper: Create sparse tool representation
1379
- // Points come from GPU as [gridX, gridY, Z] - pure integer grid coordinates for X/Y
1380
- function createSparseToolFromPoints(points) {
1381
- if (!points || points.length === 0) {
1382
- throw new Error('No tool points provided');
1383
- }
1384
-
1385
- // Points are [gridX, gridY, Z] where gridX/gridY are grid indices (floats but integer values)
1386
- // Find bounds in grid space and tool tip Z
1387
- let minGridX = Infinity, minGridY = Infinity, minZ = Infinity;
1388
- let maxGridX = -Infinity, maxGridY = -Infinity;
1389
-
1390
- for (let i = 0; i < points.length; i += 3) {
1391
- const gridX = points[i]; // Already a grid index
1392
- const gridY = points[i + 1]; // Already a grid index
1393
- const z = points[i + 2];
1394
-
1395
- minGridX = Math.min(minGridX, gridX);
1396
- maxGridX = Math.max(maxGridX, gridX);
1397
- minGridY = Math.min(minGridY, gridY);
1398
- maxGridY = Math.max(maxGridY, gridY);
1399
- minZ = Math.min(minZ, z);
1400
- }
1401
-
1402
- // Calculate tool center in grid coordinates (pure integer)
1403
- const width = Math.floor(maxGridX - minGridX) + 1;
1404
- const height = Math.floor(maxGridY - minGridY) + 1;
1405
- const centerX = Math.floor(minGridX) + Math.floor(width / 2);
1406
- const centerY = Math.floor(minGridY) + Math.floor(height / 2);
1407
-
1408
- // Convert each point to offset from center (integer arithmetic only)
1409
- const xOffsets = [];
1410
- const yOffsets = [];
1411
- const zValues = [];
1412
-
1413
- for (let i = 0; i < points.length; i += 3) {
1414
- const gridX = Math.floor(points[i]); // Grid index (ensure integer)
1415
- const gridY = Math.floor(points[i + 1]); // Grid index (ensure integer)
1416
- const z = points[i + 2];
1417
-
1418
- // Calculate offset from tool center (pure integer arithmetic)
1419
- const xOffset = gridX - centerX;
1420
- const yOffset = gridY - centerY;
1421
- // Z relative to tool tip: tip=0, points above tip are positive
1422
- // minZ is the lowest Z (tip), so z - minZ gives positive offsets upward
1423
- const zValue = z;// - minZ;
1424
-
1425
- xOffsets.push(xOffset);
1426
- yOffsets.push(yOffset);
1427
- zValues.push(zValue);
1428
- }
1429
-
1430
- return {
1431
- count: xOffsets.length,
1432
- xOffsets: new Int32Array(xOffsets),
1433
- yOffsets: new Int32Array(yOffsets),
1434
- zValues: new Float32Array(zValues),
1435
- referenceZ: minZ
1436
- };
1437
- }
1438
-
1439
- // Generate toolpath with pre-created sparse tool (for batch operations)
1440
- async function generateToolpathWithSparseTools(terrainPoints, sparseToolData, xStep, yStep, oobZ, gridStep, terrainBounds = null, singleScanline = false) {
1441
- const startTime = performance.now();
1442
-
1443
- try {
1444
- // Create height map from terrain points (use terrain bounds if provided)
1445
- const terrainMapData = createHeightMapFromPoints(terrainPoints, gridStep, terrainBounds);
1446
-
1447
- // Run WebGPU compute with pre-created sparse tool
1448
- const result = await runToolpathCompute(
1449
- terrainMapData, sparseToolData, xStep, yStep, oobZ, startTime
1450
- );
1451
-
1452
- return result;
1453
- } catch (error) {
1454
- debug.error('Error generating toolpath:', error);
1455
- throw error;
1456
- }
1457
- }
1458
-
1459
- // Generate toolpath for a single region (internal)
1460
- async function generateToolpathSingle(terrainPoints, toolPoints, xStep, yStep, oobZ, gridStep, terrainBounds = null) {
1461
- const startTime = performance.now();
1462
- debug.log('Generating toolpath...');
1463
- debug.log(`Input: terrain ${terrainPoints.length/3} points, tool ${toolPoints.length/3} points, steps (${xStep}, ${yStep}), oobZ ${oobZ}, gridStep ${gridStep}`);
1464
-
1465
- if (terrainBounds) {
1466
- debug.log(`Using terrain bounds: min(${terrainBounds.min.x.toFixed(2)}, ${terrainBounds.min.y.toFixed(2)}, ${terrainBounds.min.z.toFixed(2)}) max(${terrainBounds.max.x.toFixed(2)}, ${terrainBounds.max.y.toFixed(2)}, ${terrainBounds.max.z.toFixed(2)})`);
1467
- }
1468
-
1469
- try {
1470
- // Create height map from terrain points (use terrain bounds if provided)
1471
- const terrainMapData = createHeightMapFromPoints(terrainPoints, gridStep, terrainBounds);
1472
- debug.log(`Created terrain map: ${terrainMapData.width}x${terrainMapData.height}`);
1473
-
1474
- // Create sparse tool representation
1475
- const sparseToolData = createSparseToolFromPoints(toolPoints);
1476
- debug.log(`Created sparse tool: ${sparseToolData.count} points`);
1477
-
1478
- // Run WebGPU compute
1479
- const result = await runToolpathCompute(
1480
- terrainMapData, sparseToolData, xStep, yStep, oobZ, startTime
1481
- );
1482
-
1483
- return result;
1484
- } catch (error) {
1485
- debug.error('Error generating toolpath:', error);
1486
- throw error;
1487
- }
1488
- }
1489
-
1490
- async function runToolpathCompute(terrainMapData, sparseToolData, xStep, yStep, oobZ, startTime) {
1491
- if (!isInitialized) {
1492
- const success = await initWebGPU();
1493
- if (!success) {
1494
- throw new Error('WebGPU not available');
1495
- }
1496
- }
1497
-
1498
- // Use WASM-generated terrain grid
1499
- const terrainBuffer = device.createBuffer({
1500
- size: terrainMapData.grid.byteLength,
1501
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
1502
- });
1503
- device.queue.writeBuffer(terrainBuffer, 0, terrainMapData.grid);
1504
-
1505
- // Use WASM-generated sparse tool
1506
- const toolBufferData = new ArrayBuffer(sparseToolData.count * 16);
1507
- const toolBufferI32 = new Int32Array(toolBufferData);
1508
- const toolBufferF32 = new Float32Array(toolBufferData);
1509
-
1510
- for (let i = 0; i < sparseToolData.count; i++) {
1511
- toolBufferI32[i * 4 + 0] = sparseToolData.xOffsets[i];
1512
- toolBufferI32[i * 4 + 1] = sparseToolData.yOffsets[i];
1513
- toolBufferF32[i * 4 + 2] = sparseToolData.zValues[i];
1514
- toolBufferF32[i * 4 + 3] = 0;
1515
- }
1516
-
1517
- const toolBuffer = device.createBuffer({
1518
- size: toolBufferData.byteLength,
1519
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
1520
- });
1521
- device.queue.writeBuffer(toolBuffer, 0, toolBufferData);
1522
-
1523
- // Calculate output dimensions
1524
- const pointsPerLine = Math.ceil(terrainMapData.width / xStep);
1525
- const numScanlines = Math.ceil(terrainMapData.height / yStep);
1526
- const outputSize = pointsPerLine * numScanlines;
1527
-
1528
- const outputBuffer = device.createBuffer({
1529
- size: outputSize * 4,
1530
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
1531
- });
1532
-
1533
- const uniformData = new Uint32Array([
1534
- terrainMapData.width,
1535
- terrainMapData.height,
1536
- sparseToolData.count,
1537
- xStep,
1538
- yStep,
1539
- 0,
1540
- pointsPerLine,
1541
- numScanlines,
1542
- 0, // y_offset (default 0 for planar mode)
1543
- ]);
1544
- const uniformDataFloat = new Float32Array(uniformData.buffer);
1545
- uniformDataFloat[5] = oobZ;
1546
-
1547
- const uniformBuffer = device.createBuffer({
1548
- size: uniformData.byteLength,
1549
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
1550
- });
1551
- device.queue.writeBuffer(uniformBuffer, 0, uniformData);
1552
-
1553
- // CRITICAL: Wait for all writeBuffer operations to complete before compute dispatch
1554
- await device.queue.onSubmittedWorkDone();
1555
-
1556
- // Use cached pipeline
1557
- const bindGroup = device.createBindGroup({
1558
- layout: cachedToolpathPipeline.getBindGroupLayout(0),
1559
- entries: [
1560
- { binding: 0, resource: { buffer: terrainBuffer } },
1561
- { binding: 1, resource: { buffer: toolBuffer } },
1562
- { binding: 2, resource: { buffer: outputBuffer } },
1563
- { binding: 3, resource: { buffer: uniformBuffer } },
1564
- ],
1565
- });
1566
-
1567
- const commandEncoder = device.createCommandEncoder();
1568
- const passEncoder = commandEncoder.beginComputePass();
1569
- passEncoder.setPipeline(cachedToolpathPipeline);
1570
- passEncoder.setBindGroup(0, bindGroup);
1571
-
1572
- const workgroupsX = Math.ceil(pointsPerLine / 16);
1573
- const workgroupsY = Math.ceil(numScanlines / 16);
1574
- passEncoder.dispatchWorkgroups(workgroupsX, workgroupsY);
1575
- passEncoder.end();
1576
-
1577
- const stagingBuffer = device.createBuffer({
1578
- size: outputSize * 4,
1579
- usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
1580
- });
1581
-
1582
- commandEncoder.copyBufferToBuffer(outputBuffer, 0, stagingBuffer, 0, outputSize * 4);
1583
-
1584
- device.queue.submit([commandEncoder.finish()]);
1585
-
1586
- // CRITICAL: Wait for GPU to finish before reading results
1587
- await device.queue.onSubmittedWorkDone();
1588
-
1589
- await stagingBuffer.mapAsync(GPUMapMode.READ);
1590
-
1591
- const outputData = new Float32Array(stagingBuffer.getMappedRange());
1592
- const result = new Float32Array(outputData);
1593
- stagingBuffer.unmap();
1594
-
1595
- terrainBuffer.destroy();
1596
- toolBuffer.destroy();
1597
- outputBuffer.destroy();
1598
- uniformBuffer.destroy();
1599
- stagingBuffer.destroy();
1600
-
1601
- const endTime = performance.now();
1602
-
1603
- return {
1604
- pathData: result,
1605
- numScanlines,
1606
- pointsPerLine,
1607
- generationTime: endTime - startTime
1608
- };
1609
- }
1610
-
1611
- // Create reusable GPU buffers for tiled toolpath generation
1612
- function createReusableToolpathBuffers(terrainWidth, terrainHeight, sparseToolData, xStep, yStep) {
1613
- const pointsPerLine = Math.ceil(terrainWidth / xStep);
1614
- const numScanlines = Math.ceil(terrainHeight / yStep);
1615
- const outputSize = pointsPerLine * numScanlines;
1616
-
1617
- // Create terrain buffer (will be updated for each tile)
1618
- const terrainBuffer = device.createBuffer({
1619
- size: terrainWidth * terrainHeight * 4,
1620
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
1621
- });
1622
-
1623
- // Create tool buffer (STATIC - same for all tiles!)
1624
- const toolBufferData = new ArrayBuffer(sparseToolData.count * 16);
1625
- const toolBufferI32 = new Int32Array(toolBufferData);
1626
- const toolBufferF32 = new Float32Array(toolBufferData);
1627
-
1628
- for (let i = 0; i < sparseToolData.count; i++) {
1629
- toolBufferI32[i * 4 + 0] = sparseToolData.xOffsets[i];
1630
- toolBufferI32[i * 4 + 1] = sparseToolData.yOffsets[i];
1631
- toolBufferF32[i * 4 + 2] = sparseToolData.zValues[i];
1632
- toolBufferF32[i * 4 + 3] = 0;
1633
- }
1634
-
1635
- const toolBuffer = device.createBuffer({
1636
- size: toolBufferData.byteLength,
1637
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
1638
- });
1639
- device.queue.writeBuffer(toolBuffer, 0, toolBufferData); // Write once!
1640
-
1641
- // Create output buffer (will be read for each tile)
1642
- const outputBuffer = device.createBuffer({
1643
- size: outputSize * 4,
1644
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
1645
- });
1646
-
1647
- // Create uniform buffer (will be updated for each tile)
1648
- const uniformBuffer = device.createBuffer({
1649
- size: 36, // 9 fields × 4 bytes (added y_offset field)
1650
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
1651
- });
1652
-
1653
- // Create staging buffer (will be reused for readback)
1654
- const stagingBuffer = device.createBuffer({
1655
- size: outputSize * 4,
1656
- usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
1657
- });
1658
-
1659
- return {
1660
- terrainBuffer,
1661
- toolBuffer,
1662
- outputBuffer,
1663
- uniformBuffer,
1664
- stagingBuffer,
1665
- maxOutputSize: outputSize,
1666
- maxTerrainWidth: terrainWidth,
1667
- maxTerrainHeight: terrainHeight,
1668
- sparseToolData
1669
- };
1670
- }
1671
-
1672
- // Destroy reusable GPU buffers
1673
- function destroyReusableToolpathBuffers(buffers) {
1674
- buffers.terrainBuffer.destroy();
1675
- buffers.toolBuffer.destroy();
1676
- buffers.outputBuffer.destroy();
1677
- buffers.uniformBuffer.destroy();
1678
- buffers.stagingBuffer.destroy();
1679
- }
1680
-
1681
- // Run toolpath compute using pre-created reusable buffers
1682
- async function runToolpathComputeWithBuffers(terrainData, terrainWidth, terrainHeight, xStep, yStep, oobZ, buffers, startTime) {
1683
- // Update terrain buffer with new tile data
1684
- device.queue.writeBuffer(buffers.terrainBuffer, 0, terrainData);
1685
-
1686
- // Calculate output dimensions
1687
- const pointsPerLine = Math.ceil(terrainWidth / xStep);
1688
- const numScanlines = Math.ceil(terrainHeight / yStep);
1689
- const outputSize = pointsPerLine * numScanlines;
1690
-
1691
- // Calculate Y offset for single-scanline radial mode
1692
- // When numScanlines=1 and terrainHeight > 1, center the tool at the midline
1693
- const yOffset = (numScanlines === 1 && terrainHeight > 1) ? Math.floor(terrainHeight / 2) : 0;
1694
-
1695
- // Update uniforms for this tile
1696
- const uniformData = new Uint32Array([
1697
- terrainWidth,
1698
- terrainHeight,
1699
- buffers.sparseToolData.count,
1700
- xStep,
1701
- yStep,
1702
- 0,
1703
- pointsPerLine,
1704
- numScanlines,
1705
- yOffset, // y_offset for radial single-scanline mode
1706
- ]);
1707
- const uniformDataFloat = new Float32Array(uniformData.buffer);
1708
- uniformDataFloat[5] = oobZ;
1709
- device.queue.writeBuffer(buffers.uniformBuffer, 0, uniformData);
1710
-
1711
- // CRITICAL: Wait for all writeBuffer operations to complete before compute dispatch
1712
- // Without this, compute shader may read stale/incomplete buffer data
1713
- await device.queue.onSubmittedWorkDone();
1714
-
1715
- // Create bind group (reusing cached pipeline)
1716
- const bindGroup = device.createBindGroup({
1717
- layout: cachedToolpathPipeline.getBindGroupLayout(0),
1718
- entries: [
1719
- { binding: 0, resource: { buffer: buffers.terrainBuffer } },
1720
- { binding: 1, resource: { buffer: buffers.toolBuffer } },
1721
- { binding: 2, resource: { buffer: buffers.outputBuffer } },
1722
- { binding: 3, resource: { buffer: buffers.uniformBuffer } },
1723
- ],
1724
- });
1725
-
1726
- // Dispatch compute shader
1727
- const commandEncoder = device.createCommandEncoder();
1728
- const passEncoder = commandEncoder.beginComputePass();
1729
- passEncoder.setPipeline(cachedToolpathPipeline);
1730
- passEncoder.setBindGroup(0, bindGroup);
1731
-
1732
- const workgroupsX = Math.ceil(pointsPerLine / 16);
1733
- const workgroupsY = Math.ceil(numScanlines / 16);
1734
- passEncoder.dispatchWorkgroups(workgroupsX, workgroupsY);
1735
- passEncoder.end();
1736
-
1737
- // Copy to staging buffer
1738
- commandEncoder.copyBufferToBuffer(buffers.outputBuffer, 0, buffers.stagingBuffer, 0, outputSize * 4);
1739
-
1740
- device.queue.submit([commandEncoder.finish()]);
1741
-
1742
- // CRITICAL: Wait for GPU to finish before reading results
1743
- await device.queue.onSubmittedWorkDone();
1744
-
1745
- await buffers.stagingBuffer.mapAsync(GPUMapMode.READ);
1746
-
1747
- // Create a true copy using slice() - new Float32Array(typedArray) only creates a view!
1748
- const outputData = new Float32Array(buffers.stagingBuffer.getMappedRange(), 0, outputSize);
1749
- const result = outputData.slice(); // slice() creates a new ArrayBuffer with copied data
1750
- buffers.stagingBuffer.unmap();
1751
-
1752
- const endTime = performance.now();
1753
-
1754
- // Debug: Log first few Z values to detect non-determinism
1755
- if (result.length > 0) {
1756
- const samples = [];
1757
- for (let i = 0; i < Math.min(10, result.length); i++) {
1758
- samples.push(result[i].toFixed(3));
1759
- }
1760
- // debug.log(`[Toolpath] Output samples (${result.length} total): ${samples.join(', ')}`);
1761
- }
1762
-
1763
- return {
1764
- pathData: result,
1765
- numScanlines,
1766
- pointsPerLine,
1767
- generationTime: endTime - startTime
1768
- };
1769
- }
1770
-
1771
- // Generate toolpath with tiling support (public API)
1772
- async function generateToolpath(terrainPoints, toolPoints, xStep, yStep, oobZ, gridStep, terrainBounds = null, singleScanline = false) {
1773
- // Calculate bounds if not provided
1774
- if (!terrainBounds) {
1775
- let minX = Infinity, minY = Infinity, minZ = Infinity;
1776
- let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
1777
- for (let i = 0; i < terrainPoints.length; i += 3) {
1778
- minX = Math.min(minX, terrainPoints[i]);
1779
- maxX = Math.max(maxX, terrainPoints[i]);
1780
- minY = Math.min(minY, terrainPoints[i + 1]);
1781
- maxY = Math.max(maxY, terrainPoints[i + 1]);
1782
- minZ = Math.min(minZ, terrainPoints[i + 2]);
1783
- maxZ = Math.max(maxZ, terrainPoints[i + 2]);
1784
- }
1785
- terrainBounds = {
1786
- min: { x: minX, y: minY, z: minZ },
1787
- max: { x: maxX, y: maxY, z: maxZ }
1788
- };
1789
- }
1790
-
1791
- // Note: singleScanline mode means OUTPUT only centerline, but terrain bounds stay full
1792
- // This ensures all terrain Y values contribute to tool interference at the centerline
1793
-
1794
- // Debug tool bounds and center
1795
- for (let i=0; i<toolPoints.length; i += 3) {
1796
- if (toolPoints[i] === 0 && toolPoints[i+1] === 0) {
1797
- debug.log('[WebGPU Worker]', { TOOL_CENTER: toolPoints[i+2] });
1798
- }
1799
- }
1800
- debug.log('[WebGPU Worker]',
1801
- 'toolZMin:', ([...toolPoints].filter((_,i) => i % 3 === 2).reduce((a,b) => Math.min(a,b), Infinity)),
1802
- 'toolZMax:', ([...toolPoints].filter((_,i) => i % 3 === 2).reduce((a,b) => Math.max(a,b), -Infinity))
1803
- );
1804
-
1805
- // Calculate tool dimensions for overlap
1806
- // Tool points are [gridX, gridY, Z] where X/Y are grid indices (not mm)
1807
- let toolMinX = Infinity, toolMaxX = -Infinity;
1808
- let toolMinY = Infinity, toolMaxY = -Infinity;
1809
- for (let i = 0; i < toolPoints.length; i += 3) {
1810
- toolMinX = Math.min(toolMinX, toolPoints[i]);
1811
- toolMaxX = Math.max(toolMaxX, toolPoints[i]);
1812
- toolMinY = Math.min(toolMinY, toolPoints[i + 1]);
1813
- toolMaxY = Math.max(toolMaxY, toolPoints[i + 1]);
1814
- }
1815
- // Tool dimensions in grid cells
1816
- const toolWidthCells = toolMaxX - toolMinX;
1817
- const toolHeightCells = toolMaxY - toolMinY;
1818
- // Convert to mm for logging
1819
- const toolWidthMm = toolWidthCells * gridStep;
1820
- const toolHeightMm = toolHeightCells * gridStep;
1821
-
1822
- // Check if tiling is needed based on output grid size
1823
- const outputWidth = Math.ceil((terrainBounds.max.x - terrainBounds.min.x) / gridStep) + 1;
1824
- const outputHeight = Math.ceil((terrainBounds.max.y - terrainBounds.min.y) / gridStep) + 1;
1825
- const outputPoints = Math.ceil(outputWidth / xStep) * Math.ceil(outputHeight / yStep);
1826
- const outputMemory = outputPoints * 4; // 4 bytes per float
1827
-
1828
- const configuredLimit = config.maxGPUMemoryMB * 1024 * 1024;
1829
- const deviceLimit = deviceCapabilities.maxStorageBufferBindingSize;
1830
- const maxSafeSize = Math.min(configuredLimit, deviceLimit) * config.gpuMemorySafetyMargin;
1831
-
1832
- if (outputMemory <= maxSafeSize) {
1833
- // No tiling needed
1834
- return await generateToolpathSingle(terrainPoints, toolPoints, xStep, yStep, oobZ, gridStep, terrainBounds);
1835
- }
1836
-
1837
- // Tiling needed (terrain is ALWAYS dense)
1838
- const tilingStartTime = performance.now();
1839
- debug.log('Using tiled toolpath generation');
1840
- debug.log(`Terrain: DENSE (${terrainPoints.length} cells = ${outputWidth}x${outputHeight})`);
1841
- debug.log(`Tool dimensions: ${toolWidthMm.toFixed(2)}mm × ${toolHeightMm.toFixed(2)}mm (${toolWidthCells}×${toolHeightCells} cells)`);
1842
-
1843
- // Create tiles with tool-size overlap (pass dimensions in grid cells)
1844
- const { tiles, maxTileGridWidth, maxTileGridHeight } = createToolpathTiles(terrainBounds, gridStep, xStep, yStep, toolWidthCells, toolHeightCells, maxSafeSize);
1845
- debug.log(`Created ${tiles.length} tiles`);
1846
-
1847
- // Pre-generate all tile terrain point arrays
1848
- const pregenStartTime = performance.now();
1849
- debug.log(`Pre-generating ${tiles.length} tile terrain arrays...`);
1850
- const allTileTerrainPoints = [];
1851
-
1852
- for (let i = 0; i < tiles.length; i++) {
1853
- const tile = tiles[i];
1854
-
1855
- // Extract terrain sub-grid for this tile (terrain is ALWAYS dense)
1856
- const tileMinGridX = Math.floor((tile.bounds.min.x - terrainBounds.min.x) / gridStep);
1857
- const tileMaxGridX = Math.ceil((tile.bounds.max.x - terrainBounds.min.x) / gridStep);
1858
- const tileMinGridY = Math.floor((tile.bounds.min.y - terrainBounds.min.y) / gridStep);
1859
- const tileMaxGridY = Math.ceil((tile.bounds.max.y - terrainBounds.min.y) / gridStep);
1860
-
1861
- const tileWidth = tileMaxGridX - tileMinGridX + 1;
1862
- const tileHeight = tileMaxGridY - tileMinGridY + 1;
1863
-
1864
- // Pad to max dimensions for consistent buffer sizing
1865
- const paddedTileTerrainPoints = new Float32Array(maxTileGridWidth * maxTileGridHeight);
1866
- paddedTileTerrainPoints.fill(EMPTY_CELL);
1867
-
1868
- // Copy relevant sub-grid from full terrain into top-left of padded array
1869
- for (let ty = 0; ty < tileHeight; ty++) {
1870
- const globalY = tileMinGridY + ty;
1871
- if (globalY < 0 || globalY >= outputHeight) continue;
1872
-
1873
- for (let tx = 0; tx < tileWidth; tx++) {
1874
- const globalX = tileMinGridX + tx;
1875
- if (globalX < 0 || globalX >= outputWidth) continue;
1876
-
1877
- const globalIdx = globalY * outputWidth + globalX;
1878
- const tileIdx = ty * maxTileGridWidth + tx; // Use maxTileGridWidth for stride
1879
- paddedTileTerrainPoints[tileIdx] = terrainPoints[globalIdx];
1880
- }
1881
- }
1882
-
1883
- allTileTerrainPoints.push({
1884
- data: paddedTileTerrainPoints,
1885
- actualWidth: tileWidth,
1886
- actualHeight: tileHeight
1887
- });
1888
- }
1889
-
1890
- const pregenTime = performance.now() - pregenStartTime;
1891
- debug.log(`Pre-generation complete in ${pregenTime.toFixed(1)}ms`);
1892
-
1893
- // Create reusable GPU buffers (sized for maximum tile)
1894
- if (!isInitialized) {
1895
- const success = await initWebGPU();
1896
- if (!success) {
1897
- throw new Error('WebGPU not available');
1898
- }
1899
- }
1900
-
1901
- const sparseToolData = createSparseToolFromPoints(toolPoints);
1902
- const reusableBuffers = createReusableToolpathBuffers(maxTileGridWidth, maxTileGridHeight, sparseToolData, xStep, yStep);
1903
- debug.log(`Created reusable GPU buffers for ${maxTileGridWidth}x${maxTileGridHeight} tiles`);
1904
-
1905
- // Process each tile with reusable buffers
1906
- const tileResults = [];
1907
- let totalTileTime = 0;
1908
- for (let i = 0; i < tiles.length; i++) {
1909
- const tile = tiles[i];
1910
- const tileStartTime = performance.now();
1911
- debug.log(`Processing tile ${i + 1}/${tiles.length}...`);
1912
-
1913
- // Report progress
1914
- const percent = Math.round(((i + 1) / tiles.length) * 100);
1915
- self.postMessage({
1916
- type: 'toolpath-progress',
1917
- data: {
1918
- percent,
1919
- current: i + 1,
1920
- total: tiles.length,
1921
- layer: i + 1 // Using tile index as "layer" for consistency
1922
- }
1923
- });
1924
-
1925
- debug.log(`Tile ${i+1} using pre-generated terrain: ${allTileTerrainPoints[i].actualWidth}x${allTileTerrainPoints[i].actualHeight} (padded to ${maxTileGridWidth}x${maxTileGridHeight})`);
1926
-
1927
- // Generate toolpath for this tile using reusable buffers
1928
- const tileToolpathResult = await runToolpathComputeWithBuffers(
1929
- allTileTerrainPoints[i].data,
1930
- maxTileGridWidth,
1931
- maxTileGridHeight,
1932
- xStep,
1933
- yStep,
1934
- oobZ,
1935
- reusableBuffers,
1936
- tileStartTime
1937
- );
1938
-
1939
- const tileTime = performance.now() - tileStartTime;
1940
- totalTileTime += tileTime;
1941
-
1942
- tileResults.push({
1943
- pathData: tileToolpathResult.pathData,
1944
- numScanlines: tileToolpathResult.numScanlines,
1945
- pointsPerLine: tileToolpathResult.pointsPerLine,
1946
- tile: tile
1947
- });
1948
-
1949
- debug.log(`Tile ${i + 1}/${tiles.length} complete: ${tileToolpathResult.numScanlines}×${tileToolpathResult.pointsPerLine} in ${tileTime.toFixed(1)}ms`);
1950
- }
1951
-
1952
- // Cleanup reusable buffers
1953
- destroyReusableToolpathBuffers(reusableBuffers);
1954
-
1955
- debug.log(`All tiles processed in ${totalTileTime.toFixed(1)}ms (avg ${(totalTileTime/tiles.length).toFixed(1)}ms per tile)`);
1956
-
1957
- // Stitch tiles together, dropping overlap regions
1958
- const stitchStartTime = performance.now();
1959
- const stitchedResult = stitchToolpathTiles(tileResults, terrainBounds, gridStep, xStep, yStep);
1960
- const stitchTime = performance.now() - stitchStartTime;
1961
-
1962
- const totalTime = performance.now() - tilingStartTime;
1963
- debug.log(`Stitching took ${stitchTime.toFixed(1)}ms`);
1964
- debug.log(`Tiled toolpath complete: ${stitchedResult.numScanlines}×${stitchedResult.pointsPerLine} in ${totalTime.toFixed(1)}ms total`);
1965
-
1966
- // Update generation time to reflect total tiled time
1967
- stitchedResult.generationTime = totalTime;
1968
-
1969
- return stitchedResult;
1970
- }
1971
-
1972
- // Create tiles for toolpath generation with overlap (using integer grid coordinates)
1973
- // toolWidth and toolHeight are in grid cells (not mm)
1974
- function createToolpathTiles(bounds, gridStep, xStep, yStep, toolWidthCells, toolHeightCells, maxMemoryBytes) {
1975
- // Calculate global grid dimensions
1976
- const globalGridWidth = Math.ceil((bounds.max.x - bounds.min.x) / gridStep) + 1;
1977
- const globalGridHeight = Math.ceil((bounds.max.y - bounds.min.y) / gridStep) + 1;
1978
-
1979
- // Calculate tool overlap in grid cells (use radius = half diameter)
1980
- // Tool centered at tile boundary needs terrain extending half tool width beyond boundary
1981
- const toolOverlapX = Math.ceil(toolWidthCells / 2);
1982
- const toolOverlapY = Math.ceil(toolHeightCells / 2);
1983
-
1984
- // Binary search for optimal tile size in grid cells
1985
- let low = Math.max(toolOverlapX, toolOverlapY) * 2; // At least 2x tool size
1986
- let high = Math.max(globalGridWidth, globalGridHeight);
1987
- let bestTileGridSize = high;
1988
-
1989
- while (low <= high) {
1990
- const mid = Math.floor((low + high) / 2);
1991
- const outputW = Math.ceil(mid / xStep);
1992
- const outputH = Math.ceil(mid / yStep);
1993
- const memoryNeeded = outputW * outputH * 4;
1994
-
1995
- if (memoryNeeded <= maxMemoryBytes) {
1996
- bestTileGridSize = mid;
1997
- low = mid + 1;
1998
- } else {
1999
- high = mid - 1;
2000
- }
2001
- }
2002
-
2003
- const tilesX = Math.ceil(globalGridWidth / bestTileGridSize);
2004
- const tilesY = Math.ceil(globalGridHeight / bestTileGridSize);
2005
- const coreGridWidth = Math.ceil(globalGridWidth / tilesX);
2006
- const coreGridHeight = Math.ceil(globalGridHeight / tilesY);
2007
-
2008
- // Calculate maximum tile dimensions (for buffer sizing)
2009
- const maxTileGridWidth = coreGridWidth + 2 * toolOverlapX;
2010
- const maxTileGridHeight = coreGridHeight + 2 * toolOverlapY;
2011
-
2012
- debug.log(`Creating ${tilesX}×${tilesY} tiles (${coreGridWidth}×${coreGridHeight} cells core + ${toolOverlapX}×${toolOverlapY} cells overlap)`);
2013
- debug.log(`Max tile dimensions: ${maxTileGridWidth}×${maxTileGridHeight} cells (for buffer sizing)`);
2014
-
2015
- const tiles = [];
2016
- for (let ty = 0; ty < tilesY; ty++) {
2017
- for (let tx = 0; tx < tilesX; tx++) {
2018
- // Core tile in grid coordinates
2019
- const coreGridStartX = tx * coreGridWidth;
2020
- const coreGridStartY = ty * coreGridHeight;
2021
- const coreGridEndX = Math.min((tx + 1) * coreGridWidth, globalGridWidth) - 1;
2022
- const coreGridEndY = Math.min((ty + 1) * coreGridHeight, globalGridHeight) - 1;
2023
-
2024
- // Extended tile with overlap in grid coordinates
2025
- let extGridStartX = coreGridStartX;
2026
- let extGridStartY = coreGridStartY;
2027
- let extGridEndX = coreGridEndX;
2028
- let extGridEndY = coreGridEndY;
2029
-
2030
- // Add overlap on sides that aren't at global boundary
2031
- if (tx > 0) extGridStartX -= toolOverlapX;
2032
- if (ty > 0) extGridStartY -= toolOverlapY;
2033
- if (tx < tilesX - 1) extGridEndX += toolOverlapX;
2034
- if (ty < tilesY - 1) extGridEndY += toolOverlapY;
2035
-
2036
- // Clamp to global bounds
2037
- extGridStartX = Math.max(0, extGridStartX);
2038
- extGridStartY = Math.max(0, extGridStartY);
2039
- extGridEndX = Math.min(globalGridWidth - 1, extGridEndX);
2040
- extGridEndY = Math.min(globalGridHeight - 1, extGridEndY);
2041
-
2042
- // Calculate actual dimensions for this tile
2043
- const tileGridWidth = extGridEndX - extGridStartX + 1;
2044
- const tileGridHeight = extGridEndY - extGridStartY + 1;
2045
-
2046
- // Convert grid coordinates to world coordinates
2047
- const extMinX = bounds.min.x + extGridStartX * gridStep;
2048
- const extMinY = bounds.min.y + extGridStartY * gridStep;
2049
- const extMaxX = bounds.min.x + extGridEndX * gridStep;
2050
- const extMaxY = bounds.min.y + extGridEndY * gridStep;
2051
-
2052
- const coreMinX = bounds.min.x + coreGridStartX * gridStep;
2053
- const coreMinY = bounds.min.y + coreGridStartY * gridStep;
2054
- const coreMaxX = bounds.min.x + coreGridEndX * gridStep;
2055
- const coreMaxY = bounds.min.y + coreGridEndY * gridStep;
2056
-
2057
- tiles.push({
2058
- id: `tile_${tx}_${ty}`,
2059
- tx, ty,
2060
- tilesX, tilesY,
2061
- gridWidth: tileGridWidth,
2062
- gridHeight: tileGridHeight,
2063
- bounds: {
2064
- min: { x: extMinX, y: extMinY, z: bounds.min.z },
2065
- max: { x: extMaxX, y: extMaxY, z: bounds.max.z }
2066
- },
2067
- core: {
2068
- gridStart: { x: coreGridStartX, y: coreGridStartY },
2069
- gridEnd: { x: coreGridEndX, y: coreGridEndY },
2070
- min: { x: coreMinX, y: coreMinY },
2071
- max: { x: coreMaxX, y: coreMaxY }
2072
- }
2073
- });
2074
- }
2075
- }
2076
-
2077
- return { tiles, maxTileGridWidth, maxTileGridHeight };
2078
- }
2079
-
2080
- // Stitch toolpath tiles together, dropping overlap regions (using integer grid coordinates)
2081
- function stitchToolpathTiles(tileResults, globalBounds, gridStep, xStep, yStep) {
2082
- // Calculate global output dimensions
2083
- const globalWidth = Math.ceil((globalBounds.max.x - globalBounds.min.x) / gridStep) + 1;
2084
- const globalHeight = Math.ceil((globalBounds.max.y - globalBounds.min.y) / gridStep) + 1;
2085
- const globalPointsPerLine = Math.ceil(globalWidth / xStep);
2086
- const globalNumScanlines = Math.ceil(globalHeight / yStep);
2087
-
2088
- debug.log(`Stitching toolpath: global grid ${globalWidth}x${globalHeight}, output ${globalPointsPerLine}x${globalNumScanlines}`);
2089
-
2090
- const result = new Float32Array(globalPointsPerLine * globalNumScanlines);
2091
- result.fill(NaN);
2092
-
2093
- // Fast path for 1x1 stepping: use bulk row copying
2094
- const use1x1FastPath = (xStep === 1 && yStep === 1);
2095
-
2096
- for (const tileResult of tileResults) {
2097
- const tile = tileResult.tile;
2098
- const tileData = tileResult.pathData;
2099
-
2100
- // Use the pre-calculated integer grid coordinates from tile.core
2101
- const coreGridStartX = tile.core.gridStart.x;
2102
- const coreGridStartY = tile.core.gridStart.y;
2103
- const coreGridEndX = tile.core.gridEnd.x;
2104
- const coreGridEndY = tile.core.gridEnd.y;
2105
-
2106
- // Calculate tile's extended grid coordinates
2107
- const extGridStartX = Math.round((tile.bounds.min.x - globalBounds.min.x) / gridStep);
2108
- const extGridStartY = Math.round((tile.bounds.min.y - globalBounds.min.y) / gridStep);
2109
-
2110
- let copiedCount = 0;
2111
-
2112
- // Calculate output coordinate ranges for this tile's core
2113
- // Core region in grid coordinates
2114
- const coreGridWidth = coreGridEndX - coreGridStartX + 1;
2115
- const coreGridHeight = coreGridEndY - coreGridStartY + 1;
2116
-
2117
- // Core region in output coordinates (sampled by xStep/yStep)
2118
- const coreOutStartX = Math.floor(coreGridStartX / xStep);
2119
- const coreOutStartY = Math.floor(coreGridStartY / yStep);
2120
- const coreOutEndX = Math.floor(coreGridEndX / xStep);
2121
- const coreOutEndY = Math.floor(coreGridEndY / yStep);
2122
- const coreOutWidth = coreOutEndX - coreOutStartX + 1;
2123
- const coreOutHeight = coreOutEndY - coreOutStartY + 1;
2124
-
2125
- // Tile's extended region start in grid coordinates
2126
- const extOutStartX = Math.floor(extGridStartX / xStep);
2127
- const extOutStartY = Math.floor(extGridStartY / yStep);
2128
-
2129
- // Copy entire rows at once (works for all stepping values)
2130
- for (let outY = 0; outY < coreOutHeight; outY++) {
2131
- const globalOutY = coreOutStartY + outY;
2132
- const tileOutY = globalOutY - extOutStartY;
2133
-
2134
- if (globalOutY >= 0 && globalOutY < globalNumScanlines &&
2135
- tileOutY >= 0 && tileOutY < tileResult.numScanlines) {
2136
-
2137
- const globalRowStart = globalOutY * globalPointsPerLine + coreOutStartX;
2138
- const tileRowStart = tileOutY * tileResult.pointsPerLine + (coreOutStartX - extOutStartX);
2139
-
2140
- // Bulk copy entire row of output values
2141
- result.set(tileData.subarray(tileRowStart, tileRowStart + coreOutWidth), globalRowStart);
2142
- copiedCount += coreOutWidth;
2143
- }
2144
- }
2145
-
2146
- debug.log(` Tile ${tile.id}: copied ${copiedCount} values`);
2147
- }
2148
-
2149
- // Count how many output values are still NaN (gaps)
2150
- let nanCount = 0;
2151
- for (let i = 0; i < result.length; i++) {
2152
- if (isNaN(result[i])) nanCount++;
2153
- }
2154
- debug.log(`Stitching complete: ${result.length} total values, ${nanCount} still NaN`);
2155
-
2156
- return {
2157
- pathData: result,
2158
- numScanlines: globalNumScanlines,
2159
- pointsPerLine: globalPointsPerLine,
2160
- generationTime: 0 // Sum from tiles if needed
2161
- };
2162
- }
2163
-
2164
- // Radial: Rasterize model with rotating ray planes and X-bucketing
2165
- async function radialRasterize({
2166
- triangles,
2167
- bucketData,
2168
- resolution,
2169
- angleStep,
2170
- numAngles,
2171
- maxRadius,
2172
- toolWidth,
2173
- zFloor,
2174
- bounds,
2175
- startAngle = 0,
2176
- reusableBuffers = null,
2177
- returnBuffersForReuse = false,
2178
- batchInfo = {}
2179
- }) {
2180
- if (!device) {
2181
- throw new Error('WebGPU not initialized');
2182
- }
2183
-
2184
- const timings = {
2185
- start: performance.now(),
2186
- prep: 0,
2187
- gpu: 0,
2188
- stitch: 0
2189
- };
2190
-
2191
- // Calculate grid dimensions based on BUCKET range (not model bounds)
2192
- // Buckets may extend slightly beyond model bounds due to rounding
2193
- const bucketMinX = bucketData.buckets[0].minX;
2194
- const bucketMaxX = bucketData.buckets[bucketData.numBuckets - 1].maxX;
2195
- const gridWidth = Math.ceil((bucketMaxX - bucketMinX) / resolution);
2196
- const gridYHeight = Math.ceil(toolWidth / resolution);
2197
- const bucketGridWidth = Math.ceil((bucketData.buckets[0].maxX - bucketData.buckets[0].minX) / resolution);
2198
-
2199
- // Calculate workgroup load distribution for timeout analysis
2200
- const bucketTriangleCounts = bucketData.buckets.map(b => b.count);
2201
- const minTriangles = Math.min(...bucketTriangleCounts);
2202
- const maxTriangles = Math.max(...bucketTriangleCounts);
2203
- const avgTriangles = bucketTriangleCounts.reduce((a, b) => a + b, 0) / bucketTriangleCounts.length;
2204
- const workPerWorkgroup = maxTriangles * numAngles * bucketGridWidth * gridYHeight;
2205
-
2206
- // Determine bucket batching to avoid GPU timeouts
2207
- // Target: keep max work per batch under ~1M ray-triangle tests
2208
- const maxWorkPerBatch = 1e6;
2209
- const estimatedWorkPerBucket = avgTriangles * numAngles * bucketGridWidth * gridYHeight;
2210
-
2211
- // Calculate buckets per batch, but enforce reasonable limits
2212
- // - Minimum: 10 buckets per batch (unless total < 10)
2213
- // - Maximum: all buckets if work is reasonable
2214
- let maxBucketsPerBatch;
2215
- if (estimatedWorkPerBucket === 0) {
2216
- maxBucketsPerBatch = bucketData.numBuckets; // Empty model
2217
- } else {
2218
- const idealBucketsPerBatch = Math.floor(maxWorkPerBatch / estimatedWorkPerBucket);
2219
- const minBucketsPerBatch = Math.min(4, bucketData.numBuckets);
2220
- maxBucketsPerBatch = Math.max(minBucketsPerBatch, idealBucketsPerBatch);
2221
- // Cap at total buckets
2222
- maxBucketsPerBatch = Math.min(maxBucketsPerBatch, bucketData.numBuckets);
2223
- }
2224
-
2225
- const numBucketBatches = Math.ceil(bucketData.numBuckets / maxBucketsPerBatch);
2226
-
2227
- if (diagnostic) {
2228
- debug.log(`Radial: ${gridWidth}x${gridYHeight} grid, ${numAngles} angles, ${bucketData.buckets.length} buckets`);
2229
- debug.log(`Load: min=${minTriangles} max=${maxTriangles} avg=${avgTriangles.toFixed(0)} (${(maxTriangles/avgTriangles).toFixed(2)}x imbalance, worst=${(workPerWorkgroup/1e6).toFixed(1)}M tests)`);
2230
- debug.log(`Estimated work/bucket: ${(estimatedWorkPerBucket/1e6).toFixed(1)}M tests`);
2231
- if (numBucketBatches > 1) {
2232
- debug.log(`Bucket batching: ${numBucketBatches} batches of ~${maxBucketsPerBatch} buckets to avoid timeout`);
2233
- }
2234
- }
2235
-
2236
- // Reuse buffers if provided, otherwise create new ones
2237
- let triangleBuffer, triangleIndicesBuffer;
2238
- let shouldCleanupBuffers = false;
2239
-
2240
- if (reusableBuffers) {
2241
- // Reuse cached buffers from previous angle batch
2242
- triangleBuffer = reusableBuffers.triangleBuffer;
2243
- triangleIndicesBuffer = reusableBuffers.triangleIndicesBuffer;
2244
- } else {
2245
- // Create new GPU buffers (first batch or non-batched operation)
2246
- shouldCleanupBuffers = true;
2247
-
2248
- triangleBuffer = device.createBuffer({
2249
- size: triangles.byteLength,
2250
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
2251
- mappedAtCreation: true
2252
- });
2253
- new Float32Array(triangleBuffer.getMappedRange()).set(triangles);
2254
- triangleBuffer.unmap();
2255
-
2256
- // Create triangle indices buffer
2257
- triangleIndicesBuffer = device.createBuffer({
2258
- size: bucketData.triangleIndices.byteLength,
2259
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
2260
- mappedAtCreation: true
2261
- });
2262
- new Uint32Array(triangleIndicesBuffer.getMappedRange()).set(bucketData.triangleIndices);
2263
- triangleIndicesBuffer.unmap();
2264
- }
2265
-
2266
- // Create output buffer (all angles, all buckets)
2267
- const outputSize = numAngles * bucketData.numBuckets * bucketGridWidth * gridYHeight * 4;
2268
- const outputBuffer = device.createBuffer({
2269
- size: outputSize,
2270
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST
2271
- });
2272
-
2273
- // CRITICAL: Initialize output buffer with zFloor to avoid reading garbage data
2274
- const initData = new Float32Array(outputSize / 4);
2275
- initData.fill(zFloor);
2276
- device.queue.writeBuffer(outputBuffer, 0, initData);
2277
- // Note: No need to wait - GPU will execute writeBuffer before compute shader
2278
-
2279
- // Prep complete, GPU starting
2280
- timings.prep = performance.now() - timings.start;
2281
- const gpuStart = performance.now();
2282
-
2283
- // Use cached pipeline (created in initWebGPU)
2284
- const pipeline = cachedRadialBatchPipeline;
2285
-
2286
- // Process buckets in batches to avoid GPU timeouts
2287
- const commandEncoder = device.createCommandEncoder();
2288
- const passEncoder = commandEncoder.beginComputePass();
2289
- passEncoder.setPipeline(pipeline);
2290
-
2291
- const dispatchX = Math.ceil(numAngles / 8);
2292
- const dispatchY = Math.ceil(gridYHeight / 8);
2293
-
2294
- // Collect buffers to destroy after GPU completes
2295
- const batchBuffersToDestroy = [];
2296
- debug.log(`Dispatch (${dispatchX}, ${dispatchY}, ${maxBucketsPerBatch}) in ${numBucketBatches} Chunks`);
2297
-
2298
- for (let batchIdx = 0; batchIdx < numBucketBatches; batchIdx++) {
2299
- const startBucket = batchIdx * maxBucketsPerBatch;
2300
- const endBucket = Math.min(startBucket + maxBucketsPerBatch, bucketData.numBuckets);
2301
- const bucketsInBatch = endBucket - startBucket;
2302
-
2303
- // Create bucket info buffer for this batch
2304
- const bucketInfoSize = bucketsInBatch * 16; // 4 fields * 4 bytes per bucket
2305
- const bucketInfoBuffer = device.createBuffer({
2306
- size: bucketInfoSize,
2307
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
2308
- mappedAtCreation: true
2309
- });
2310
-
2311
- const bucketView = new ArrayBuffer(bucketInfoSize);
2312
- const bucketFloatView = new Float32Array(bucketView);
2313
- const bucketUintView = new Uint32Array(bucketView);
2314
-
2315
- for (let i = 0; i < bucketsInBatch; i++) {
2316
- const bucket = bucketData.buckets[startBucket + i];
2317
- const offset = i * 4;
2318
- bucketFloatView[offset] = bucket.minX; // f32
2319
- bucketFloatView[offset + 1] = bucket.maxX; // f32
2320
- bucketUintView[offset + 2] = bucket.startIndex; // u32
2321
- bucketUintView[offset + 3] = bucket.count; // u32
2322
- }
2323
-
2324
- new Uint8Array(bucketInfoBuffer.getMappedRange()).set(new Uint8Array(bucketView));
2325
- bucketInfoBuffer.unmap();
2326
-
2327
- // Create uniforms for this batch
2328
- // Struct layout: f32, f32, u32, f32, f32, u32, f32, u32, f32, f32, u32, u32, f32, u32
2329
- const uniformBuffer = device.createBuffer({
2330
- size: 56, // 14 fields * 4 bytes
2331
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
2332
- mappedAtCreation: true
2333
- });
2334
-
2335
- const uniformView = new ArrayBuffer(56);
2336
- const floatView = new Float32Array(uniformView);
2337
- const uintView = new Uint32Array(uniformView);
2338
-
2339
- floatView[0] = resolution; // f32
2340
- floatView[1] = angleStep * (Math.PI / 180); // f32
2341
- uintView[2] = numAngles; // u32
2342
- floatView[3] = maxRadius; // f32
2343
- floatView[4] = toolWidth; // f32
2344
- uintView[5] = gridYHeight; // u32
2345
- floatView[6] = bucketData.buckets[0].maxX - bucketData.buckets[0].minX; // f32 bucketWidth
2346
- uintView[7] = bucketGridWidth; // u32
2347
- floatView[8] = bucketMinX; // f32 global_min_x
2348
- floatView[9] = zFloor; // f32
2349
- uintView[10] = 0; // u32 filterMode
2350
- uintView[11] = bucketData.numBuckets; // u32 (total buckets, for validation)
2351
- floatView[12] = startAngle * (Math.PI / 180); // f32 start_angle (radians)
2352
- uintView[13] = startBucket; // u32 bucket_offset
2353
-
2354
- new Uint8Array(uniformBuffer.getMappedRange()).set(new Uint8Array(uniformView));
2355
- uniformBuffer.unmap();
2356
-
2357
- // Create bind group for this batch
2358
- const bindGroup = device.createBindGroup({
2359
- layout: pipeline.getBindGroupLayout(0),
2360
- entries: [
2361
- { binding: 0, resource: { buffer: triangleBuffer } },
2362
- { binding: 1, resource: { buffer: outputBuffer } },
2363
- { binding: 2, resource: { buffer: uniformBuffer } },
2364
- { binding: 3, resource: { buffer: bucketInfoBuffer } },
2365
- { binding: 4, resource: { buffer: triangleIndicesBuffer } }
2366
- ]
2367
- });
2368
-
2369
- passEncoder.setBindGroup(0, bindGroup);
2370
-
2371
- // Dispatch for this batch
2372
- const dispatchZ = bucketsInBatch;
2373
- if (diagnostic) {
2374
- debug.log(` Batch ${batchIdx + 1}/${numBucketBatches}: Dispatch (${dispatchX}, ${dispatchY}, ${dispatchZ}) = buckets ${startBucket}-${endBucket - 1}`);
2375
- }
2376
-
2377
- passEncoder.dispatchWorkgroups(dispatchX, dispatchY, dispatchZ);
2378
-
2379
- // Save buffers to destroy after GPU completes
2380
- batchBuffersToDestroy.push(uniformBuffer, bucketInfoBuffer);
2381
- }
2382
-
2383
- passEncoder.end();
2384
-
2385
- // Read back
2386
- const stagingBuffer = device.createBuffer({
2387
- size: outputSize,
2388
- usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
2389
- });
2390
-
2391
- commandEncoder.copyBufferToBuffer(outputBuffer, 0, stagingBuffer, 0, outputSize);
2392
- device.queue.submit([commandEncoder.finish()]);
2393
-
2394
- // Wait for GPU to finish before reading results
2395
- await device.queue.onSubmittedWorkDone();
2396
- await stagingBuffer.mapAsync(GPUMapMode.READ);
2397
- // const outputData = new Float32Array(stagingBuffer.getMappedRange());
2398
- // const outputCopy = new Float32Array(outputData);
2399
- const outputCopy = new Float32Array(stagingBuffer.getMappedRange().slice());
2400
- stagingBuffer.unmap();
2401
-
2402
- // Now safe to destroy batch buffers (GPU has completed)
2403
- for (const buffer of batchBuffersToDestroy) {
2404
- buffer.destroy();
2405
- }
2406
-
2407
- // Cleanup main buffers
2408
- outputBuffer.destroy();
2409
- stagingBuffer.destroy();
2410
-
2411
- timings.gpu = performance.now() - gpuStart;
2412
-
2413
- // Stitch strips
2414
- const stitchStart = performance.now();
2415
- const strips = [];
2416
-
2417
- for (let angleIdx = 0; angleIdx < numAngles; angleIdx++) {
2418
- const stripData = new Float32Array(gridWidth * gridYHeight);
2419
- stripData.fill(zFloor); // Initialize with zFloor, not zeros!
2420
-
2421
- // Gather from each bucket
2422
- for (let bucketIdx = 0; bucketIdx < bucketData.numBuckets; bucketIdx++) {
2423
- const bucket = bucketData.buckets[bucketIdx];
2424
- const bucketMinGridX = Math.floor((bucket.minX - bucketMinX) / resolution);
2425
-
2426
- for (let localX = 0; localX < bucketGridWidth; localX++) {
2427
- const gridX = bucketMinGridX + localX;
2428
- if (gridX >= gridWidth) continue;
2429
-
2430
- for (let gridY = 0; gridY < gridYHeight; gridY++) {
2431
- const srcIdx = bucketIdx * numAngles * bucketGridWidth * gridYHeight
2432
- + angleIdx * bucketGridWidth * gridYHeight
2433
- + gridY * bucketGridWidth
2434
- + localX;
2435
- const dstIdx = gridY * gridWidth + gridX;
2436
- stripData[dstIdx] = outputCopy[srcIdx];
2437
- }
2438
- }
2439
- }
2440
-
2441
- // Keep as DENSE Z-only format (toolpath generator expects this!)
2442
- // Count valid points
2443
- let validCount = 0;
2444
- for (let i = 0; i < stripData.length; i++) {
2445
- if (stripData[i] !== zFloor) validCount++;
2446
- }
2447
-
2448
- strips.push({
2449
- angle: startAngle + (angleIdx * angleStep),
2450
- positions: stripData, // DENSE Z-only format!
2451
- gridWidth,
2452
- gridHeight: gridYHeight,
2453
- pointCount: validCount, // Number of non-floor cells
2454
- bounds: {
2455
- min: { x: bucketMinX, y: 0, z: zFloor },
2456
- max: { x: bucketMaxX, y: toolWidth, z: bounds.max.z }
2457
- }
2458
- });
2459
- }
2460
-
2461
- timings.stitch = performance.now() - stitchStart;
2462
- const totalTime = performance.now() - timings.start;
2463
-
2464
- Object.assign(batchInfo, {
2465
- 'prep': (timings.prep | 0),
2466
- 'raster': (timings.gpu | 0),
2467
- 'stitch': (timings.stitch | 0)
2468
- });
2469
-
2470
- const result = { strips, timings };
2471
-
2472
- // Decide what to do with triangle/indices buffers
2473
- // Note: bucketInfoBuffer is now created/destroyed per bucket batch within the loop
2474
- if (returnBuffersForReuse && shouldCleanupBuffers) {
2475
- // First batch in multi-batch operation: return buffers for subsequent batches to reuse
2476
- result.reusableBuffers = {
2477
- triangleBuffer,
2478
- triangleIndicesBuffer
2479
- };
2480
- } else if (shouldCleanupBuffers) {
2481
- // Single batch operation OR we're NOT supposed to return buffers: destroy them now
2482
- triangleBuffer.destroy();
2483
- triangleIndicesBuffer.destroy();
2484
- }
2485
- // else: we're reusing buffers from a previous angle batch, don't destroy them (caller will destroy after all angle batches)
2486
-
2487
- return result;
2488
- }
2489
-
2490
- // Radial: Complete pipeline - rasterize model + generate toolpaths for all strips
2491
- async function generateRadialToolpaths({
2492
- triangles,
2493
- bucketData,
2494
- toolData,
2495
- resolution,
2496
- angleStep,
2497
- numAngles,
2498
- maxRadius,
2499
- toolWidth,
2500
- zFloor,
2501
- bounds,
2502
- xStep,
2503
- yStep
2504
- }) {
2505
- debug.log('radial-generate-toolpaths', { triangles: triangles.length, numAngles, resolution });
2506
-
2507
- // Batch processing: rasterize angle ranges to avoid memory allocation failure
2508
- // Calculate safe batch size based on available GPU memory
2509
- const MAX_BUFFER_SIZE_MB = 1800; // Stay under 2GB WebGPU limit with headroom
2510
- const bytesPerCell = 4; // f32
2511
-
2512
- const xSize = bounds.max.x - bounds.min.x;
2513
- const ySize = bounds.max.y - bounds.min.y;
2514
- const gridXSize = Math.ceil(xSize / resolution);
2515
- const gridYHeight = Math.ceil(ySize / resolution);
2516
-
2517
- // Calculate total memory requirement
2518
- const cellsPerAngle = gridXSize * gridYHeight;
2519
- const bytesPerAngle = cellsPerAngle * bytesPerCell;
2520
- const totalMemoryMB = (numAngles * bytesPerAngle) / (1024 * 1024);
2521
-
2522
- // Only batch if total memory exceeds threshold
2523
- const batchDivisor = config?.batchDivisor || 1;
2524
- let ANGLES_PER_BATCH, numBatches;
2525
- if (totalMemoryMB > MAX_BUFFER_SIZE_MB) {
2526
- // Need to batch
2527
- const maxAnglesPerBatch = Math.floor((MAX_BUFFER_SIZE_MB * 1024 * 1024) / bytesPerAngle);
2528
- // Apply batch divisor for overhead testing
2529
- const adjustedMaxAngles = Math.floor(maxAnglesPerBatch / batchDivisor);
2530
-
2531
- ANGLES_PER_BATCH = Math.max(1, Math.min(adjustedMaxAngles, numAngles));
2532
- numBatches = Math.ceil(numAngles / ANGLES_PER_BATCH);
2533
- const batchSizeMB = (ANGLES_PER_BATCH * bytesPerAngle / 1024 / 1024).toFixed(1);
2534
- debug.log(`Grid: ${gridXSize} x ${gridYHeight}, ${cellsPerAngle.toLocaleString()} cells/angle`);
2535
- debug.log(`Total memory: ${totalMemoryMB.toFixed(1)}MB exceeds limit, batching required`);
2536
- if (batchDivisor > 1) {
2537
- debug.log(`batchDivisor: ${batchDivisor}x (testing overhead: ${maxAnglesPerBatch} → ${adjustedMaxAngles} angles/batch)`);
2538
- }
2539
- debug.log(`Batch size: ${ANGLES_PER_BATCH} angles (~${batchSizeMB}MB per batch)`);
2540
- debug.log(`Processing ${numAngles} angles in ${numBatches} batch(es)`);
2541
- } else {
2542
- // Process all angles at once (but still respect batchDivisor for testing)
2543
- if (batchDivisor > 1) {
2544
- ANGLES_PER_BATCH = Math.max(10, Math.floor(numAngles / batchDivisor));
2545
- numBatches = Math.ceil(numAngles / ANGLES_PER_BATCH);
2546
- debug.log(`Grid: ${gridXSize} x ${gridYHeight}, ${cellsPerAngle.toLocaleString()} cells/angle`);
2547
- debug.log(`Total memory: ${totalMemoryMB.toFixed(1)}MB (fits in buffer normally)`);
2548
- debug.log(`batchDivisor: ${batchDivisor}x (artificially creating ${numBatches} batches for overhead testing)`);
2549
- } else {
2550
- ANGLES_PER_BATCH = numAngles;
2551
- numBatches = 1;
2552
- debug.log(`Grid: ${gridXSize} x ${gridYHeight}, ${cellsPerAngle.toLocaleString()} cells/angle`);
2553
- debug.log(`Total memory: ${totalMemoryMB.toFixed(1)}MB fits in buffer, processing all ${numAngles} angles in single batch`);
2554
- }
2555
- }
2556
-
2557
- const allStripToolpaths = [];
2558
- let totalToolpathPoints = 0;
2559
- const pipelineStartTime = performance.now();
2560
-
2561
- // Prepare sparse tool once
2562
- const sparseToolData = createSparseToolFromPoints(toolData.positions);
2563
- debug.log(`Created sparse tool: ${sparseToolData.count} points (reusing for all strips)`);
2564
-
2565
- // Create reusable rasterization buffers if batching (numBatches > 1)
2566
- // These buffers (triangles, buckets, indices) don't change between batches
2567
- let batchReuseBuffers = null;
2568
- let batchTracking = [];
2569
-
2570
- for (let batchIdx = 0; batchIdx < numBatches; batchIdx++) {
2571
- const batchStartTime = performance.now();
2572
- const startAngleIdx = batchIdx * ANGLES_PER_BATCH;
2573
- const endAngleIdx = Math.min(startAngleIdx + ANGLES_PER_BATCH, numAngles);
2574
- const batchNumAngles = endAngleIdx - startAngleIdx;
2575
- const batchStartAngle = startAngleIdx * angleStep;
2576
-
2577
- const batchInfo = {
2578
- from: startAngleIdx,
2579
- to: endAngleIdx
2580
- };
2581
- batchTracking.push(batchInfo);
2582
-
2583
- debug.log(`Batch ${batchIdx + 1}/${numBatches}: angles ${startAngleIdx}-${endAngleIdx - 1} (${batchNumAngles} angles), startAngle=${batchStartAngle.toFixed(1)}°`);
2584
-
2585
- // Rasterize this batch of strips
2586
- const rasterStartTime = performance.now();
2587
- const shouldReturnBuffers = (batchIdx === 0 && numBatches > 1); // First batch of multi-batch operation
2588
- const batchModelResult = await radialRasterize({
2589
- triangles,
2590
- bucketData,
2591
- resolution,
2592
- angleStep,
2593
- numAngles: batchNumAngles,
2594
- maxRadius,
2595
- toolWidth,
2596
- zFloor,
2597
- bounds,
2598
- startAngle: batchStartAngle,
2599
- reusableBuffers: batchReuseBuffers,
2600
- returnBuffersForReuse: shouldReturnBuffers,
2601
- batchInfo
2602
- });
2603
-
2604
- const rasterTime = performance.now() - rasterStartTime;
2605
-
2606
- // Capture buffers from first batch for reuse
2607
- if (batchIdx === 0 && batchModelResult.reusableBuffers) {
2608
- batchReuseBuffers = batchModelResult.reusableBuffers;
2609
- }
2610
-
2611
- // Find max dimensions for this batch
2612
- let maxStripWidth = 0;
2613
- let maxStripHeight = 0;
2614
- for (const strip of batchModelResult.strips) {
2615
- maxStripWidth = Math.max(maxStripWidth, strip.gridWidth);
2616
- maxStripHeight = Math.max(maxStripHeight, strip.gridHeight);
2617
- }
2618
-
2619
- // Create reusable buffers for this batch
2620
- const reusableBuffers = createReusableToolpathBuffers(maxStripWidth, maxStripHeight, sparseToolData, xStep, maxStripHeight);
2621
-
2622
- // Generate toolpaths for this batch
2623
- const toolpathStartTime = performance.now();
2624
-
2625
- for (let i = 0; i < batchModelResult.strips.length; i++) {
2626
- const strip = batchModelResult.strips[i];
2627
- const globalStripIdx = startAngleIdx + i;
2628
-
2629
- if (globalStripIdx % 10 === 0 || globalStripIdx === numAngles - 1) {
2630
- self.postMessage({
2631
- type: 'toolpath-progress',
2632
- data: {
2633
- percent: Math.round(((globalStripIdx + 1) / numAngles) * 100),
2634
- current: globalStripIdx + 1,
2635
- total: numAngles,
2636
- layer: globalStripIdx + 1
2637
- }
2638
- });
2639
- }
2640
-
2641
- if (!strip.positions || strip.positions.length === 0) continue;
2642
-
2643
- // DEBUG: Diagnostic logging
2644
- if (diagnostic && (globalStripIdx === 0 || globalStripIdx === 360)) {
2645
- debug.log(`NF3EWAE6 | Strip ${globalStripIdx} (${strip.angle.toFixed(1)}°) INPUT terrain first 5 Z values: ${strip.positions.slice(0, 5).map(v => v.toFixed(3)).join(',')}`);
2646
- }
2647
-
2648
- const stripToolpathResult = await runToolpathComputeWithBuffers(
2649
- strip.positions,
2650
- strip.gridWidth,
2651
- strip.gridHeight,
2652
- xStep,
2653
- strip.gridHeight,
2654
- zFloor,
2655
- reusableBuffers,
2656
- pipelineStartTime
2657
- );
2658
-
2659
- // DEBUG: Verify toolpath generation output
2660
- if (diagnostic && (globalStripIdx === 0 || globalStripIdx === 360)) {
2661
- debug.log(`NF3EWAE6 | Strip ${globalStripIdx} (${strip.angle.toFixed(1)}°) OUTPUT toolpath first 5 Z values: ${stripToolpathResult.pathData.slice(0, 5).map(v => v.toFixed(3)).join(',')}`);
2662
- }
2663
-
2664
- allStripToolpaths.push({
2665
- angle: strip.angle,
2666
- pathData: stripToolpathResult.pathData,
2667
- numScanlines: stripToolpathResult.numScanlines,
2668
- pointsPerLine: stripToolpathResult.pointsPerLine,
2669
- terrainBounds: strip.bounds
2670
- });
2671
-
2672
- totalToolpathPoints += stripToolpathResult.pathData.length;
2673
- }
2674
- const toolpathTime = performance.now() - toolpathStartTime;
2675
-
2676
- // Free batch terrain data
2677
- for (const strip of batchModelResult.strips) {
2678
- strip.positions = null;
2679
- }
2680
- destroyReusableToolpathBuffers(reusableBuffers);
2681
-
2682
- const batchTotalTime = performance.now() - batchStartTime;
2683
-
2684
- Object.assign(batchInfo, {
2685
- 'prep': batchInfo.prep || 0,
2686
- 'gpu': batchInfo.gpu || 0,
2687
- 'stitch': batchInfo.stitch || 0,
2688
- 'raster': batchInfo.raster || 0,
2689
- 'mkbuf': 0,
2690
- 'paths': (toolpathTime | 0),
2691
- 'strips': allStripToolpaths.length,
2692
- 'total': (batchTotalTime | 0)
2693
- });
2694
- }
2695
-
2696
- console.table(batchTracking);
2697
-
2698
- // Cleanup cached rasterization buffers after all batches complete
2699
- if (batchReuseBuffers) {
2700
- batchReuseBuffers.triangleBuffer.destroy();
2701
- batchReuseBuffers.triangleIndicesBuffer.destroy();
2702
- // Note: bucketInfoBuffer is no longer in reusableBuffers (created/destroyed per bucket batch)
2703
- debug.log(`Destroyed cached GPU buffers after all batches`);
2704
- }
2705
-
2706
- const pipelineTotalTime = performance.now() - pipelineStartTime;
2707
- debug.log(`Complete radial toolpath: ${allStripToolpaths.length} strips, ${totalToolpathPoints} total points in ${pipelineTotalTime.toFixed(0)}ms`);
2708
-
2709
- return {
2710
- strips: allStripToolpaths,
2711
- totalPoints: totalToolpathPoints,
2712
- numStrips: allStripToolpaths.length
2713
- };
2714
- }
2715
-
2716
- // Handle messages from main thread
2717
- self.onmessage = async function(e) {
2718
- const { type, data } = e.data;
2719
-
2720
- try {
2721
- switch (type) {
2722
- case 'init':
2723
- // Store config
2724
- config = data?.config || {
2725
- maxGPUMemoryMB: 256,
2726
- gpuMemorySafetyMargin: 0.8,
2727
- tileOverlapMM: 10,
2728
- autoTiling: true,
2729
- minTileSize: 50,
2730
- batchDivisor: 1 // For testing batching overhead: 1=optimal, 2=2x batches, 4=4x batches, etc.
2731
- };
2732
- const success = await initWebGPU();
2733
- self.postMessage({
2734
- type: 'webgpu-ready',
2735
- data: {
2736
- success,
2737
- capabilities: deviceCapabilities
2738
- }
2739
- });
2740
- break;
2741
-
2742
- case 'update-config':
2743
- config = data.config;
2744
- debug.log('Config updated:', config);
2745
- break;
2746
-
2747
- case 'rasterize':
2748
- const { triangles, stepSize, filterMode, boundsOverride } = data;
2749
- const rasterOptions = boundsOverride || {};
2750
- const rasterResult = await rasterizeMesh(triangles, stepSize, filterMode, rasterOptions);
2751
- self.postMessage({
2752
- type: 'rasterize-complete',
2753
- data: rasterResult,
2754
- }, [rasterResult.positions.buffer]);
2755
- break;
2756
-
2757
- case 'generate-toolpath':
2758
- const { terrainPositions, toolPositions, xStep, yStep, zFloor, gridStep, terrainBounds, singleScanline } = data;
2759
- const toolpathResult = await generateToolpath(
2760
- terrainPositions, toolPositions, xStep, yStep, zFloor, gridStep, terrainBounds, singleScanline
2761
- );
2762
- self.postMessage({
2763
- type: 'toolpath-complete',
2764
- data: toolpathResult
2765
- }, [toolpathResult.pathData.buffer]);
2766
- break;
2767
-
2768
- case 'radial-rasterize':
2769
- const { triangles: radialTriangles, stepSize: radialStep, rotationStep: radialRotationStep, zFloor: radialZFloor = 0, boundsOverride: radialBounds, maxConcurrentTiles, trianglesPerTile, radialRotationOffset } = data;
2770
- const radialResult = await radialRasterize(radialTriangles, radialStep, radialRotationStep, radialZFloor, radialBounds, { maxConcurrentTiles, trianglesPerTile, radialRotationOffset });
2771
- self.postMessage({
2772
- type: 'radial-rasterize-complete',
2773
- data: radialResult
2774
- }, [radialResult.positions.buffer]);
2775
- break;
2776
-
2777
- case 'radial-generate-toolpaths':
2778
- const radialToolpathResult = await generateRadialToolpaths(data);
2779
- const toolpathTransferBuffers = radialToolpathResult.strips.map(strip => strip.pathData.buffer);
2780
- self.postMessage({
2781
- type: 'radial-toolpaths-complete',
2782
- data: radialToolpathResult
2783
- }, toolpathTransferBuffers);
2784
- break;
2785
-
2786
- default:
2787
- self.postMessage({
2788
- type: 'error',
2789
- message: 'Unknown message type: ' + type
2790
- });
2791
- }
2792
- } catch (error) {
2793
- debug.error('Error:', error);
2794
- self.postMessage({
2795
- type: 'error',
2796
- message: error.message,
2797
- stack: error.stack
2798
- });
2799
- }
2800
- };