cyclecad 3.7.0 → 3.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,61 @@
1
1
  /**
2
- * Generative Design / Topology Optimization Module
2
+ * @fileoverview Generative Design / Topology Optimization Module
3
+ * @module CycleCAD/GenerativeDesign
4
+ * @version 3.7.0
5
+ * @author cycleCAD Team
6
+ * @license MIT
3
7
  *
8
+ * @description
4
9
  * Voxel-based SIMP (Solid Isotropic Material with Penalization) topology optimization
5
- * with marching cubes isosurface extraction, multi-objective support, and CAD integration.
10
+ * with marching cubes isosurface extraction, multi-objective support (minimize weight + stress),
11
+ * and CAD integration. Runs iterative optimization in non-blocking requestAnimationFrame chunks.
12
+ * Supports keep/avoid regions, point loads, fixed supports, and multi-material design spaces.
6
13
  *
7
- * Non-blocking iterative optimization in requestAnimationFrame chunks.
14
+ * @example
15
+ * // Initialize and set up design space
16
+ * window.CycleCAD.GenerativeDesign.init(scene);
17
+ * window.CycleCAD.GenerativeDesign.setDesignSpace({min: {x: -50, y: -50, z: -50}, max: {x: 50, y: 50, z: 50}});
18
+ *
19
+ * // Add constraints and loads
20
+ * window.CycleCAD.GenerativeDesign.addKeepRegion(criticalPart);
21
+ * window.CycleCAD.GenerativeDesign.addPointLoad({x: 0, y: 50, z: 0}, {x: 0, y: -1, z: 0}, 1000);
22
+ *
23
+ * // Run optimization
24
+ * window.CycleCAD.GenerativeDesign.execute('runOptimization', {iterations: 50});
25
+ *
26
+ * @requires THREE (Three.js r170)
27
+ * @see {@link https://cyclecad.com/docs/killer-features|Killer Features Guide}
28
+ */
29
+
30
+ /**
31
+ * @typedef {Object} VoxelGrid
32
+ * @property {Float32Array} densities - Voxel density array (0-1 per voxel, flattened N×N×N)
33
+ * @property {number} resolution - Grid resolution per dimension (typically 20-40)
34
+ * @property {THREE.Box3} bounds - Bounding box of design space
35
+ */
36
+
37
+ /**
38
+ * @typedef {Object} DesignConstraints
39
+ * @property {Array<THREE.Mesh>} keepRegions - Geometry that must remain solid
40
+ * @property {Array<THREE.Mesh>} avoidRegions - Geometry that must remain empty
41
+ * @property {Array<{position: Vector3, force: Vector3, magnitude: number}>} loads - Applied loads
42
+ * @property {Array<Vector3>} fixedPoints - Fixed/clamped regions (no displacement)
43
+ */
44
+
45
+ /**
46
+ * @typedef {Object} OptimizationResult
47
+ * @property {VoxelGrid} voxelGrid - Final optimized voxel density field
48
+ * @property {Array<number>} convergenceHistory - Compliance at each iteration
49
+ * @property {number} finalCompliance - Final compliance (deformation energy)
50
+ * @property {number} volumeUsed - Fraction of design space used (0-1)
51
+ * @property {THREE.BufferGeometry} geometry - Extracted surface mesh
52
+ */
53
+
54
+ /**
55
+ * @typedef {Object} MarchingCubesResult
56
+ * @property {THREE.BufferGeometry} geometry - Isosurface mesh
57
+ * @property {number} vertexCount - Number of vertices in result
58
+ * @property {number} faceCount - Number of triangles in result
8
59
  */
9
60
 
10
61
  window.CycleCAD = window.CycleCAD || {};
@@ -93,10 +144,15 @@ window.CycleCAD.GenerativeDesign = (() => {
93
144
  }
94
145
 
95
146
  /**
96
- * Add a point load
97
- * @param {THREE.Vector3} position - Load position
98
- * @param {THREE.Vector3} direction - Load direction (normalized)
147
+ * Add a point load force to the design space
148
+ *
149
+ * Applied forces drive the topology optimization. Multiple loads can be combined
150
+ * to model complex loading scenarios. Each load affects nearby voxels based on distance.
151
+ *
152
+ * @param {THREE.Vector3} position - Load position in world space
153
+ * @param {THREE.Vector3} direction - Load direction (should be normalized)
99
154
  * @param {number} magnitude - Load magnitude in Newtons
155
+ * @returns {void}
100
156
  */
101
157
  function addLoad(position, direction, magnitude) {
102
158
  const dir = direction.clone().normalize();
@@ -207,6 +263,18 @@ window.CycleCAD.GenerativeDesign = (() => {
207
263
  /**
208
264
  * Initialize voxel density grid
209
265
  */
266
+ /**
267
+ * Initialize voxel grid for topology optimization (internal)
268
+ *
269
+ * Creates NxNxN grid of density values (0-1). Populates based on constraints:
270
+ * - Keep regions set to 1.0 (solid)
271
+ * - Avoid regions set to 0.0 (empty)
272
+ * - Free space set to volumeFraction (e.g., 0.3 = 30% target)
273
+ *
274
+ * Uses spatial hashing for efficient point-in-mesh tests.
275
+ *
276
+ * @returns {void}
277
+ */
210
278
  function initializeVoxelGrid() {
211
279
  const res = optimizationState.resolution;
212
280
  optimizationState.densities = new Float32Array(res * res * res);
@@ -293,6 +361,19 @@ window.CycleCAD.GenerativeDesign = (() => {
293
361
  /**
294
362
  * Compute stress sensitivity for each voxel
295
363
  */
364
+ /**
365
+ * Compute sensitivity (∂Compliance/∂density) for each voxel (internal)
366
+ *
367
+ * SIMP algorithm core: measures how much each voxel's removal increases deformation.
368
+ * Sensitivities guide density updates toward optimal design.
369
+ *
370
+ * Formula: sensitivity[v] = -p * ρ^(p-1) * u[v]^T * K[v] * u[v]
371
+ * where p = penaltyFactor (typically 3), ρ = density, u = displacement, K = stiffness
372
+ *
373
+ * Uses aggregation (neighborhood averaging) to prevent checkerboard patterns.
374
+ *
375
+ * @returns {Float32Array} Sensitivity values (one per voxel)
376
+ */
296
377
  function computeSensitivities() {
297
378
  const res = optimizationState.resolution;
298
379
  const sensitivities = new Float32Array(res * res * res);
@@ -333,6 +414,18 @@ window.CycleCAD.GenerativeDesign = (() => {
333
414
  /**
334
415
  * Apply sensitivity filter to prevent checkerboard patterns
335
416
  */
417
+ /**
418
+ * Apply density filter to sensitivities (internal)
419
+ *
420
+ * Smooths sensitivity field with Gaussian kernel to enforce minimum feature size.
421
+ * Prevents creation of unrealizable small features. Inverse weighting: smaller
422
+ * sensitivities are damped more, protecting thin features.
423
+ *
424
+ * Prevents checkerboard patterns in SIMP optimization by penalizing rapid density changes.
425
+ *
426
+ * @param {Float32Array} sensitivities - Raw sensitivity field
427
+ * @returns {Float32Array} Filtered sensitivity field
428
+ */
336
429
  function applySensitivityFilter(sensitivities) {
337
430
  const res = optimizationState.resolution;
338
431
  const filtered = new Float32Array(sensitivities.length);
@@ -375,6 +468,19 @@ window.CycleCAD.GenerativeDesign = (() => {
375
468
  /**
376
469
  * Update densities using optimality criteria method
377
470
  */
471
+ /**
472
+ * Update voxel densities using Optimality Criteria method (internal)
473
+ *
474
+ * SIMP optimality criterion: each voxel moves to a Pareto-optimal density.
475
+ * Iterates binary search for Lagrange multiplier that maintains volume constraint.
476
+ * Update rule: ρ_new = max(0, min(1, ρ_old * (λ * sensitivity)^0.3))
477
+ *
478
+ * The 0.3 exponent (move limit) prevents oscillation and ensures convergence.
479
+ * Volume constraint is maintained: sum(ρ) = volumeFraction * total_voxels
480
+ *
481
+ * @param {Float32Array} sensitivities - Filtered sensitivity field
482
+ * @returns {void}
483
+ */
378
484
  function updateDensities(sensitivities) {
379
485
  const res = optimizationState.resolution;
380
486
  const newDensities = new Float32Array(optimizationState.densities.length);
@@ -479,6 +585,19 @@ window.CycleCAD.GenerativeDesign = (() => {
479
585
  /**
480
586
  * Extract isosurface from voxel grid using simplified marching cubes
481
587
  */
588
+ /**
589
+ * Extract surface mesh from voxel density field using Marching Cubes algorithm
590
+ *
591
+ * Marching Cubes: processes each cube of 8 voxels, looks up triangle configuration
592
+ * from edge table based on which vertices are solid vs. empty. Interpolates vertex
593
+ * positions on edges where density crosses threshold.
594
+ *
595
+ * Resulting mesh is smoothed and optimized for export (merged vertex buffers,
596
+ * computed normals, indexed geometry).
597
+ *
598
+ * @param {number} [threshold=0.3] - Density threshold for solid voxels (0-1)
599
+ * @returns {MarchingCubesResult} Surface mesh and statistics
600
+ */
482
601
  function extractIsosurface(threshold = 0.3) {
483
602
  const res = optimizationState.resolution;
484
603
  const vertices = [];
@@ -709,6 +828,17 @@ window.CycleCAD.GenerativeDesign = (() => {
709
828
  /**
710
829
  * Initialize module in scene
711
830
  */
831
+ /**
832
+ * Initialize GenerativeDesign module with Three.js scene
833
+ *
834
+ * Sets up Three.js scene, camera, renderer references. Creates material definitions,
835
+ * visualization groups, and event listeners. Must be called once before execute() calls.
836
+ *
837
+ * @param {THREE.Scene} sceneRef - The Three.js scene object
838
+ * @param {THREE.Camera} cameraRef - The Three.js camera
839
+ * @param {THREE.WebGLRenderer} rendererRef - The Three.js renderer
840
+ * @returns {void}
841
+ */
712
842
  function init(sceneRef, cameraRef, rendererRef) {
713
843
  scene = sceneRef;
714
844
  camera = cameraRef;
@@ -843,6 +973,29 @@ window.CycleCAD.GenerativeDesign = (() => {
843
973
  /**
844
974
  * Execute commands from UI
845
975
  */
976
+ /**
977
+ * Execute command in GenerativeDesign module (public API)
978
+ *
979
+ * Commands:
980
+ * - 'setDesignSpace': Define optimization region
981
+ * - 'addKeepRegion': Mark geometry that must stay solid
982
+ * - 'addAvoidRegion': Mark geometry that must stay empty
983
+ * - 'addLoad': Apply point force to design space
984
+ * - 'addFixedPoint': Fix a region (boundary condition)
985
+ * - 'runOptimization': Execute topology optimization loop
986
+ * - 'extractMesh': Convert density field to surface mesh
987
+ * - 'exportSTL': Export optimized geometry as STL
988
+ * - 'clear': Reset all constraints and state
989
+ *
990
+ * @param {string} command - Command name
991
+ * @param {Object} [params={}] - Command parameters (varies by command)
992
+ * @param {number} params.iterations - For 'runOptimization': number of iterations
993
+ * @param {number} params.volumeFraction - For setup: target volume fraction (0-1)
994
+ * @param {number} params.threshold - For 'extractMesh': density threshold
995
+ * @returns {Object} Command result (varies by command)
996
+ * @example
997
+ * window.CycleCAD.GenerativeDesign.execute('runOptimization', {iterations: 50});
998
+ */
846
999
  function execute(command, params = {}) {
847
1000
  switch (command) {
848
1001
  case 'setResolution':