@bitbybit-dev/base 1.1.0 → 1.2.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +14 -4
  2. package/index.js +0 -3
  3. package/lib/api/GlobalCDNProvider.js +1 -1
  4. package/lib/api/inputs/base-inputs.d.ts +114 -0
  5. package/lib/api/inputs/base-inputs.js +15 -0
  6. package/lib/api/inputs/color-inputs.d.ts +4 -0
  7. package/lib/api/inputs/color-inputs.js +4 -0
  8. package/lib/api/inputs/dates-inputs.d.ts +4 -0
  9. package/lib/api/inputs/dates-inputs.js +4 -0
  10. package/lib/api/inputs/io-inputs.d.ts +10 -6
  11. package/lib/api/inputs/io-inputs.js +4 -0
  12. package/lib/api/inputs/line-inputs.d.ts +36 -31
  13. package/lib/api/inputs/line-inputs.js +13 -0
  14. package/lib/api/inputs/lists-inputs.d.ts +28 -20
  15. package/lib/api/inputs/lists-inputs.js +16 -0
  16. package/lib/api/inputs/logic-inputs.d.ts +15 -4
  17. package/lib/api/inputs/logic-inputs.js +65 -0
  18. package/lib/api/inputs/math-inputs.d.ts +25 -0
  19. package/lib/api/inputs/math-inputs.js +40 -0
  20. package/lib/api/inputs/mesh-inputs.d.ts +16 -11
  21. package/lib/api/inputs/mesh-inputs.js +5 -0
  22. package/lib/api/inputs/point-inputs.d.ts +33 -27
  23. package/lib/api/inputs/point-inputs.js +14 -0
  24. package/lib/api/inputs/polyline-inputs.d.ts +33 -28
  25. package/lib/api/inputs/polyline-inputs.js +5 -0
  26. package/lib/api/inputs/text-inputs.d.ts +27 -14
  27. package/lib/api/inputs/text-inputs.js +13 -0
  28. package/lib/api/inputs/transforms-inputs.d.ts +9 -3
  29. package/lib/api/inputs/transforms-inputs.js +6 -0
  30. package/lib/api/inputs/vector-inputs.d.ts +5 -0
  31. package/lib/api/inputs/vector-inputs.js +69 -0
  32. package/lib/api/models/point/hex-grid-data.d.ts +8 -3
  33. package/lib/api/models/point/hex-grid-data.js +5 -0
  34. package/lib/api/models/simplex.d.ts +19 -250
  35. package/lib/api/models/simplex.js +7 -35
  36. package/lib/api/models/text/vector-char-data.d.ts +3 -3
  37. package/lib/api/models/text/vector-text-data.d.ts +3 -3
  38. package/lib/api/services/color.js +5 -4
  39. package/lib/api/services/geometry-helper.d.ts +5 -5
  40. package/lib/api/services/geometry-helper.js +101 -2
  41. package/lib/api/services/helpers/dxf/dxf-generator.js +20 -69
  42. package/lib/api/services/helpers/mesh-normals.d.ts +9 -0
  43. package/lib/api/services/helpers/mesh-normals.js +56 -0
  44. package/lib/api/services/io.d.ts +4 -0
  45. package/lib/api/services/io.js +4 -0
  46. package/lib/api/services/line.d.ts +2 -2
  47. package/lib/api/services/line.js +18 -51
  48. package/lib/api/services/lists.js +1 -6
  49. package/lib/api/services/mesh.d.ts +2 -1
  50. package/lib/api/services/mesh.js +14 -32
  51. package/lib/api/services/point.d.ts +3 -3
  52. package/lib/api/services/point.js +26 -82
  53. package/lib/api/services/polyline.d.ts +2 -2
  54. package/lib/api/services/polyline.js +13 -43
  55. package/lib/api/services/text.js +1 -9
  56. package/lib/api/services/transforms.js +10 -16
  57. package/lib/api/services/vector.d.ts +4 -4
  58. package/lib/api/services/vector.js +3 -3
  59. package/package.json +53 -91
  60. package/babel.config.cjs +0 -14
  61. package/babel.config.d.cts +0 -5
  62. package/lib/api/unit-test-helper.d.ts +0 -20
  63. package/lib/api/unit-test-helper.js +0 -130
@@ -1,3 +1,85 @@
1
+ const CELL_KEY_DIMENSIONS = 6;
2
+ const CELL_SIZE_IN_TOLERANCES = 8;
3
+ const CELL_PROBE_MARGIN_IN_TOLERANCES = 1.5;
4
+ const CELL_ARITHMETIC_SAFE_MULTIPLE_OF_TOLERANCE = Math.pow(2, 50);
5
+ const CELL_HASH_WORD = 4294967296;
6
+ const hashCells = (vectorLength, cells) => {
7
+ let hash = Math.imul(vectorLength + 1, 0x9E3779B1);
8
+ for (let i = 0; i < cells.length; i++) {
9
+ const cell = cells[i];
10
+ const lowWord = cell % CELL_HASH_WORD;
11
+ const highWord = (cell - lowWord) / CELL_HASH_WORD;
12
+ hash = Math.imul(hash ^ lowWord, 0x85EBCA6B);
13
+ hash = Math.imul(hash ^ highWord, 0xC2B2AE35);
14
+ hash ^= hash >>> 15;
15
+ }
16
+ return hash;
17
+ };
18
+ const neverMatchesAnyVector = (vector, tolerance) => {
19
+ for (let i = 0; i < vector.length; i++) {
20
+ if (!Number.isFinite(vector[i])) {
21
+ return true;
22
+ }
23
+ }
24
+ return vector.length > 0 && !(tolerance > 0);
25
+ };
26
+ const vectorCellRange = (vector, tolerance) => {
27
+ const keyDimensions = Math.min(vector.length, CELL_KEY_DIMENSIONS);
28
+ if (keyDimensions > 0 && !Number.isFinite(tolerance)) {
29
+ return undefined;
30
+ }
31
+ const cellSize = tolerance * CELL_SIZE_IN_TOLERANCES;
32
+ const probeMargin = tolerance * CELL_PROBE_MARGIN_IN_TOLERANCES;
33
+ const centre = [];
34
+ const lowest = [];
35
+ const highest = [];
36
+ for (let i = 0; i < keyDimensions; i++) {
37
+ const component = vector[i];
38
+ if (Math.abs(component) / tolerance >= CELL_ARITHMETIC_SAFE_MULTIPLE_OF_TOLERANCE) {
39
+ return undefined;
40
+ }
41
+ const lowestCell = Math.floor((component - probeMargin) / cellSize);
42
+ const highestCell = Math.floor((component + probeMargin) / cellSize);
43
+ const cellsToProbe = highestCell - lowestCell + 1;
44
+ if (!Number.isInteger(lowestCell) || !Number.isInteger(highestCell) || cellsToProbe < 1 || cellsToProbe > 2) {
45
+ return undefined;
46
+ }
47
+ centre.push(Math.floor(component / cellSize));
48
+ lowest.push(lowestCell);
49
+ highest.push(highestCell);
50
+ }
51
+ return { centre, lowest, highest };
52
+ };
53
+ const cellRangeHoldsSameVector = (keptVectorsByCell, vector, cellRange, isTheSameAsVector) => {
54
+ const lowest = cellRange.lowest;
55
+ const highest = cellRange.highest;
56
+ const probe = lowest.slice();
57
+ for (;;) {
58
+ const cellVectors = keptVectorsByCell.get(hashCells(vector.length, probe));
59
+ if (cellVectors !== undefined && cellVectors.some(isTheSameAsVector)) {
60
+ return true;
61
+ }
62
+ let dimension = probe.length - 1;
63
+ while (dimension >= 0 && probe[dimension] === highest[dimension]) {
64
+ probe[dimension] = lowest[dimension];
65
+ dimension--;
66
+ }
67
+ if (dimension < 0) {
68
+ return false;
69
+ }
70
+ probe[dimension] = probe[dimension] + 1;
71
+ }
72
+ };
73
+ const addVectorToCell = (keptVectorsByCell, vector, cellRange) => {
74
+ const cellKey = hashCells(vector.length, cellRange.centre);
75
+ const cellVectors = keptVectorsByCell.get(cellKey);
76
+ if (cellVectors === undefined) {
77
+ keptVectorsByCell.set(cellKey, [vector]);
78
+ }
79
+ else {
80
+ cellVectors.push(vector);
81
+ }
82
+ };
1
83
  export class GeometryHelper {
2
84
  constructor() {
3
85
  /**
@@ -65,10 +147,27 @@ export class GeometryHelper {
65
147
  */
66
148
  removeAllDuplicateVectors(vectors, tolerance = 1e-7) {
67
149
  const cleanVectors = [];
150
+ const keptVectorsByCell = new Map();
151
+ const keptVectorsOutsideCellIndex = [];
68
152
  vectors.forEach(vector => {
69
- // when there are no vectors in cleanVectors array that match the current vector, push it in.
70
- if (!cleanVectors.some(s => this.vectorsTheSame(vector, s, tolerance))) {
153
+ if (neverMatchesAnyVector(vector, tolerance)) {
154
+ cleanVectors.push(vector);
155
+ return;
156
+ }
157
+ const isTheSameAsVector = (kept) => this.vectorsTheSame(vector, kept, tolerance);
158
+ const cellRange = vectorCellRange(vector, tolerance);
159
+ if (cellRange === undefined) {
160
+ if (!cleanVectors.some(isTheSameAsVector)) {
161
+ cleanVectors.push(vector);
162
+ keptVectorsOutsideCellIndex.push(vector);
163
+ }
164
+ return;
165
+ }
166
+ const alreadyKept = cellRangeHoldsSameVector(keptVectorsByCell, vector, cellRange, isTheSameAsVector)
167
+ || keptVectorsOutsideCellIndex.some(isTheSameAsVector);
168
+ if (!alreadyKept) {
71
169
  cleanVectors.push(vector);
170
+ addVectorToCell(keptVectorsByCell, vector, cellRange);
72
171
  }
73
172
  });
74
173
  return cleanVectors;
@@ -1,6 +1,6 @@
1
1
  export class DxfGenerator {
2
2
  constructor() {
3
- this.entityHandle = 256; // Start at 256 to avoid conflicts with system handles
3
+ this.entityHandle = 256;
4
4
  this.colorFormat = "aci";
5
5
  this.acadVersion = "AC1009";
6
6
  }
@@ -8,19 +8,13 @@ export class DxfGenerator {
8
8
  * Generate a complete DXF file content from path-based entities
9
9
  */
10
10
  generateDxf(dxfInputs) {
11
- // Set format options from input
12
11
  this.colorFormat = dxfInputs.colorFormat || "aci";
13
12
  this.acadVersion = dxfInputs.acadVersion || "AC1009";
14
13
  const dxfContent = [];
15
- // Header section
16
14
  dxfContent.push(...this.generateHeader());
17
- // Tables section
18
15
  dxfContent.push(...this.generateTables(dxfInputs));
19
- // Blocks section (required by many CAD programs)
20
16
  dxfContent.push(...this.generateBlocks());
21
- // Entities section
22
17
  dxfContent.push(...this.generateEntities(dxfInputs));
23
- // End of file
24
18
  dxfContent.push("0", "EOF");
25
19
  return dxfContent.join("\n");
26
20
  }
@@ -39,11 +33,9 @@ export class DxfGenerator {
39
33
  this.acadVersion
40
34
  ];
41
35
  if (this.acadVersion === "AC1009") {
42
- // AC1009 (AutoCAD R12) - minimal header for maximum compatibility
43
36
  header.push("9", "$DWGCODEPAGE", "3", "ascii", "9", "$HANDSEED", "5", "0");
44
37
  }
45
38
  else {
46
- // AC1015 (AutoCAD 2000) - modern format
47
39
  header.push("9", "$DWGCODEPAGE", "3", "ANSI_1252", "9", "$LASTSAVEDBY", "1", "bitbybit.dev", "9", "$HANDSEED", "5", "20000");
48
40
  }
49
41
  header.push("0", "ENDSEC");
@@ -59,17 +51,12 @@ export class DxfGenerator {
59
51
  "2",
60
52
  "TABLES"
61
53
  ];
62
- // VPORT table (required for AC1009)
63
54
  if (this.acadVersion === "AC1009") {
64
55
  tables.push(...this.generateVportTable());
65
56
  }
66
- // Line type table (required)
67
57
  tables.push(...this.generateLineTypeTable());
68
- // Layer table
69
58
  tables.push(...this.generateLayerTable(dxfInputs));
70
- // Text style table (required for text entities, included for completeness)
71
59
  tables.push(...this.generateStyleTable());
72
- // Additional tables for AC1009
73
60
  if (this.acadVersion === "AC1009") {
74
61
  tables.push(...this.generateViewTable());
75
62
  tables.push(...this.generateUcsTable());
@@ -89,7 +76,7 @@ export class DxfGenerator {
89
76
  "2",
90
77
  "LTYPE",
91
78
  "70",
92
- "1" // Number of line types
79
+ "1"
93
80
  ];
94
81
  if (this.acadVersion === "AC1015") {
95
82
  ltype.push("5", "5", "100", "AcDbSymbolTable");
@@ -111,7 +98,7 @@ export class DxfGenerator {
111
98
  "2",
112
99
  "STYLE",
113
100
  "70",
114
- "1" // Number of styles
101
+ "1"
115
102
  ];
116
103
  if (this.acadVersion === "AC1015") {
117
104
  style.push("5", "3", "100", "AcDbSymbolTable");
@@ -133,7 +120,7 @@ export class DxfGenerator {
133
120
  "2",
134
121
  "VPORT",
135
122
  "70",
136
- "2", // Number of viewports
123
+ "2",
137
124
  "0",
138
125
  "VPORT",
139
126
  "2",
@@ -294,7 +281,6 @@ export class DxfGenerator {
294
281
  */
295
282
  generateLayerTable(dxfInputs) {
296
283
  const layers = new Set();
297
- // Collect all unique layer names
298
284
  if (dxfInputs.dxfPathsParts) {
299
285
  dxfInputs.dxfPathsParts.forEach(part => {
300
286
  if (part.layer) {
@@ -302,7 +288,6 @@ export class DxfGenerator {
302
288
  }
303
289
  });
304
290
  }
305
- // Add default layer if no layers specified
306
291
  if (layers.size === 0) {
307
292
  layers.add("0");
308
293
  }
@@ -317,12 +302,8 @@ export class DxfGenerator {
317
302
  if (this.acadVersion === "AC1015") {
318
303
  layerTable.splice(4, 0, "5", "2", "100", "AcDbSymbolTable");
319
304
  }
320
- // Generate layer entries
321
305
  layers.forEach(layerName => {
322
- layerTable.push("0", "LAYER", "2", layerName, "70", "0", "62", "7", // Default color (white)
323
- "6", "CONTINUOUS" // Line type
324
- );
325
- // Add AC1015-specific subclass markers
306
+ layerTable.push("0", "LAYER", "2", layerName, "70", "0", "62", "7", "6", "CONTINUOUS");
326
307
  if (this.acadVersion === "AC1015") {
327
308
  const insertIdx = layerTable.lastIndexOf("LAYER") + 1;
328
309
  layerTable.splice(insertIdx, 0, "5", this.getNextHandle(), "100", "AcDbSymbolTableRecord", "100", "AcDbLayerTableRecord");
@@ -361,7 +342,6 @@ export class DxfGenerator {
361
342
  * Generate entity for a single segment based on its type
362
343
  */
363
344
  generateSegmentEntity(segment, part) {
364
- // Check segment type and generate appropriate entity
365
345
  if (this.isLineSegment(segment)) {
366
346
  return this.generateLineEntity(segment, part);
367
347
  }
@@ -419,15 +399,11 @@ export class DxfGenerator {
419
399
  "8",
420
400
  part.layer || "0"
421
401
  ];
422
- // Add color if specified
423
402
  if (part.color !== undefined) {
424
403
  const colorCodes = this.convertColorToDxf(part.color);
425
404
  colorCodes.forEach(cc => entity.push(cc.code, cc.value));
426
405
  }
427
- entity.push("10", line.start[0].toFixed(6), "20", line.start[1].toFixed(6), "30", "0.00", // Z coordinate (2D)
428
- "11", line.end[0].toFixed(6), "21", line.end[1].toFixed(6), "31", "0.00" // Z coordinate (2D)
429
- );
430
- // Add AC1015-specific codes
406
+ entity.push("10", line.start[0].toFixed(6), "20", line.start[1].toFixed(6), "30", "0.00", "11", line.end[0].toFixed(6), "21", line.end[1].toFixed(6), "31", "0.00");
431
407
  if (this.acadVersion === "AC1015") {
432
408
  entity.splice(2, 0, "5", this.getNextHandle(), "100", "AcDbEntity");
433
409
  const coordIdx = entity.indexOf("10");
@@ -445,13 +421,11 @@ export class DxfGenerator {
445
421
  "8",
446
422
  part.layer || "0"
447
423
  ];
448
- // Add color if specified
449
424
  if (part.color !== undefined) {
450
425
  const colorCodes = this.convertColorToDxf(part.color);
451
426
  colorCodes.forEach(cc => entity.push(cc.code, cc.value));
452
427
  }
453
428
  entity.push("10", circle.center[0].toFixed(6), "20", circle.center[1].toFixed(6), "30", "0.00", "40", circle.radius.toFixed(6));
454
- // Add AC1015-specific codes
455
429
  if (this.acadVersion === "AC1015") {
456
430
  entity.splice(2, 0, "5", this.getNextHandle(), "100", "AcDbEntity");
457
431
  const coordIdx = entity.indexOf("10");
@@ -469,17 +443,14 @@ export class DxfGenerator {
469
443
  "8",
470
444
  part.layer || "0"
471
445
  ];
472
- // Add line type for AC1009 (optional empty)
473
446
  if (this.acadVersion === "AC1009") {
474
447
  entity.push("6", " ");
475
448
  }
476
- // Add color if specified
477
449
  if (part.color !== undefined) {
478
450
  const colorCodes = this.convertColorToDxf(part.color);
479
451
  colorCodes.forEach(cc => entity.push(cc.code, cc.value));
480
452
  }
481
453
  entity.push("10", arc.center[0].toFixed(6), "20", arc.center[1].toFixed(6), "30", this.acadVersion === "AC1009" ? "" : "0.0", "40", arc.radius.toFixed(6), "50", arc.startAngle.toFixed(6), "51", arc.endAngle.toFixed(6));
482
- // Add AC1015-specific codes
483
454
  if (this.acadVersion === "AC1015") {
484
455
  entity.splice(2, 0, "5", this.getNextHandle(), "100", "AcDbEntity");
485
456
  const coordIdx = entity.indexOf("10");
@@ -499,23 +470,19 @@ export class DxfGenerator {
499
470
  "8",
500
471
  part.layer || "0"
501
472
  ];
502
- // Add color if specified
503
473
  if (part.color !== undefined) {
504
474
  const colorCodes = this.convertColorToDxf(part.color);
505
475
  colorCodes.forEach(cc => entity.push(cc.code, cc.value));
506
476
  }
507
477
  const isClosed = polyline.closed || (polyline.points.length > 2 && this.isClosedPolyline(polyline.points));
508
478
  entity.push("90", polyline.points.length.toString(), "70", isClosed ? "1" : "0");
509
- // Add AC1015-specific codes
510
479
  if (this.acadVersion === "AC1015") {
511
480
  entity.splice(2, 0, "5", this.getNextHandle(), "100", "AcDbEntity");
512
481
  const pointIdx = entity.indexOf("90");
513
482
  entity.splice(pointIdx, 0, "100", "AcDbPolyline");
514
483
  }
515
- // Add vertices
516
484
  polyline.points.forEach((point, index) => {
517
485
  entity.push("10", point[0].toFixed(6), "20", point[1].toFixed(6));
518
- // Add bulge value if specified (for arc segments)
519
486
  if (polyline.bulges && polyline.bulges.length > index) {
520
487
  const bulge = polyline.bulges[index];
521
488
  if (bulge !== 0) {
@@ -535,7 +502,6 @@ export class DxfGenerator {
535
502
  "8",
536
503
  part.layer || "0"
537
504
  ];
538
- // Add color if specified
539
505
  if (part.color !== undefined) {
540
506
  const colorCodes = this.convertColorToDxf(part.color);
541
507
  colorCodes.forEach(cc => entity.push(cc.code, cc.value));
@@ -543,25 +509,18 @@ export class DxfGenerator {
543
509
  const degree = spline.degree || 3;
544
510
  const numControlPoints = spline.controlPoints.length;
545
511
  const numKnots = numControlPoints + degree + 1;
546
- // Spline flags: 1 = closed, 2 = periodic, 4 = rational, 8 = planar, 16 = linear
547
- const flags = spline.closed ? 9 : 8; // Add planar flag (8) for 2D splines
548
- entity.push("210", "0.0", "220", "0.0", "230", "1.0", // Normal vector (Z-axis for 2D)
549
- "70", flags.toString(), "71", degree.toString(), "72", numKnots.toString(), "73", numControlPoints.toString(), "74", "0" // Number of fit points (we're using control points)
550
- );
551
- // Add AC1015-specific codes
512
+ const flags = spline.closed ? 9 : 8;
513
+ entity.push("210", "0.0", "220", "0.0", "230", "1.0", "70", flags.toString(), "71", degree.toString(), "72", numKnots.toString(), "73", numControlPoints.toString(), "74", "0");
552
514
  if (this.acadVersion === "AC1015") {
553
515
  entity.splice(2, 0, "5", this.getNextHandle(), "100", "AcDbEntity");
554
516
  const normalIdx = entity.indexOf("210");
555
517
  entity.splice(normalIdx, 0, "100", "AcDbSpline");
556
518
  }
557
- // Generate knot values (uniform knot vector)
558
519
  for (let i = 0; i < numKnots; i++) {
559
520
  entity.push("40", i.toFixed(6));
560
521
  }
561
- // Add control points
562
522
  spline.controlPoints.forEach(point => {
563
- entity.push("10", point[0].toFixed(6), "20", point[1].toFixed(6), "30", "0.0" // Z coordinate (2D)
564
- );
523
+ entity.push("10", point[0].toFixed(6), "20", point[1].toFixed(6), "30", "0.0");
565
524
  });
566
525
  return entity;
567
526
  }
@@ -588,14 +547,12 @@ export class DxfGenerator {
588
547
  * Returns appropriate DXF color codes based on colorFormat setting
589
548
  */
590
549
  convertColorToDxf(color) {
591
- // If it's already a number (ACI index), use it directly
592
550
  if (/^\d+$/.test(color)) {
593
551
  const colorIndex = parseInt(color, 10);
594
552
  if (colorIndex >= 1 && colorIndex <= 255) {
595
553
  return [{ code: "62", value: color }];
596
554
  }
597
555
  }
598
- // If it's a hex color, handle based on format preference
599
556
  if (color.startsWith("#")) {
600
557
  const hex = color.substring(1);
601
558
  if (hex.length === 6) {
@@ -603,21 +560,18 @@ export class DxfGenerator {
603
560
  const g = parseInt(hex.substring(2, 4), 16);
604
561
  const b = parseInt(hex.substring(4, 6), 16);
605
562
  if (this.colorFormat === "truecolor") {
606
- // Use 24-bit true color for full color spectrum (newer CAD software)
607
563
  const trueColor = (r * 65536) + (g * 256) + b;
608
564
  return [
609
- { code: "62", value: "256" }, // 256 = ByEntity
565
+ { code: "62", value: "256" },
610
566
  { code: "420", value: trueColor.toString() }
611
567
  ];
612
568
  }
613
569
  else {
614
- // Use ACI color index for better compatibility (older CAD software)
615
570
  const aciIndex = this.rgbToAciColorIndex(r, g, b);
616
571
  return [{ code: "62", value: aciIndex.toString() }];
617
572
  }
618
573
  }
619
574
  }
620
- // Default to white (7) if color can't be parsed
621
575
  return [{ code: "62", value: "7" }];
622
576
  }
623
577
  /**
@@ -625,23 +579,20 @@ export class DxfGenerator {
625
579
  * Uses a simplified mapping to standard ACI colors
626
580
  */
627
581
  rgbToAciColorIndex(r, g, b) {
628
- // ACI standard colors (simplified mapping)
629
582
  const aciColors = {
630
- 1: [255, 0, 0], // Red
631
- 2: [255, 255, 0], // Yellow
632
- 3: [0, 255, 0], // Green
633
- 4: [0, 255, 255], // Cyan
634
- 5: [0, 0, 255], // Blue
635
- 6: [255, 0, 255], // Magenta
636
- 7: [255, 255, 255], // White
637
- 8: [128, 128, 128], // Gray
638
- 9: [192, 192, 192] // Light gray
583
+ 1: [255, 0, 0],
584
+ 2: [255, 255, 0],
585
+ 3: [0, 255, 0],
586
+ 4: [0, 255, 255],
587
+ 5: [0, 0, 255],
588
+ 6: [255, 0, 255],
589
+ 7: [255, 255, 255],
590
+ 8: [128, 128, 128],
591
+ 9: [192, 192, 192]
639
592
  };
640
- // Special case for black or very dark colors
641
593
  if (r < 30 && g < 30 && b < 30) {
642
- return 7; // Use white for visibility on dark backgrounds
594
+ return 7;
643
595
  }
644
- // Find nearest color
645
596
  let nearestIndex = 7;
646
597
  let minDistance = Infinity;
647
598
  for (const [index, [cr, cg, cb]] of Object.entries(aciColors)) {
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Compute smooth vertex normals for a mesh that arrives without them.
3
+ * Accumulates each triangle's cross product onto its three vertices, then normalizes, so a vertex
4
+ * shared by several triangles ends up with their average and the mesh shades smoothly.
5
+ * @param positions - Flat array of vertex positions [x,y,z,x,y,z,...]
6
+ * @param indices - Triangle indices
7
+ * @returns Flat array of normals [nx,ny,nz,nx,ny,nz,...]
8
+ */
9
+ export declare function computeVertexNormals(positions: number[], indices: number[]): number[];
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Compute smooth vertex normals for a mesh that arrives without them.
3
+ * Accumulates each triangle's cross product onto its three vertices, then normalizes, so a vertex
4
+ * shared by several triangles ends up with their average and the mesh shades smoothly.
5
+ * @param positions - Flat array of vertex positions [x,y,z,x,y,z,...]
6
+ * @param indices - Triangle indices
7
+ * @returns Flat array of normals [nx,ny,nz,nx,ny,nz,...]
8
+ */
9
+ export function computeVertexNormals(positions, indices) {
10
+ const numVertices = positions.length / 3;
11
+ const normals = new Float32Array(positions.length);
12
+ for (let i = 0; i < indices.length; i += 3) {
13
+ const i0 = indices[i];
14
+ const i1 = indices[i + 1];
15
+ const i2 = indices[i + 2];
16
+ const v0x = positions[i0 * 3];
17
+ const v0y = positions[i0 * 3 + 1];
18
+ const v0z = positions[i0 * 3 + 2];
19
+ const v1x = positions[i1 * 3];
20
+ const v1y = positions[i1 * 3 + 1];
21
+ const v1z = positions[i1 * 3 + 2];
22
+ const v2x = positions[i2 * 3];
23
+ const v2y = positions[i2 * 3 + 1];
24
+ const v2z = positions[i2 * 3 + 2];
25
+ const e1x = v1x - v0x;
26
+ const e1y = v1y - v0y;
27
+ const e1z = v1z - v0z;
28
+ const e2x = v2x - v0x;
29
+ const e2y = v2y - v0y;
30
+ const e2z = v2z - v0z;
31
+ const nx = e1y * e2z - e1z * e2y;
32
+ const ny = e1z * e2x - e1x * e2z;
33
+ const nz = e1x * e2y - e1y * e2x;
34
+ normals[i0 * 3] += nx;
35
+ normals[i0 * 3 + 1] += ny;
36
+ normals[i0 * 3 + 2] += nz;
37
+ normals[i1 * 3] += nx;
38
+ normals[i1 * 3 + 1] += ny;
39
+ normals[i1 * 3 + 2] += nz;
40
+ normals[i2 * 3] += nx;
41
+ normals[i2 * 3 + 1] += ny;
42
+ normals[i2 * 3 + 2] += nz;
43
+ }
44
+ for (let i = 0; i < numVertices; i++) {
45
+ const x = normals[i * 3];
46
+ const y = normals[i * 3 + 1];
47
+ const z = normals[i * 3 + 2];
48
+ const len = Math.sqrt(x * x + y * y + z * z);
49
+ if (len > 0) {
50
+ normals[i * 3] = x / len;
51
+ normals[i * 3 + 1] = y / len;
52
+ normals[i * 3 + 2] = z / len;
53
+ }
54
+ }
55
+ return Array.from(normals);
56
+ }
@@ -1,4 +1,8 @@
1
1
  import { Dxf } from "./helpers/dxf/dxf";
2
+ /**
3
+ * Reading and writing files: exporting geometry to the supported formats, importing it back, and
4
+ * the download and upload helpers that move files between the browser and the user's machine.
5
+ */
2
6
  export declare class IoBitByBit {
3
7
  dxf: Dxf;
4
8
  constructor();
@@ -1,4 +1,8 @@
1
1
  import { Dxf } from "./helpers/dxf/dxf";
2
+ /**
3
+ * Reading and writing files: exporting geometry to the supported formats, importing it back, and
4
+ * the download and upload helpers that move files between the browser and the user's machine.
5
+ */
2
6
  export class IoBitByBit {
3
7
  constructor() {
4
8
  this.dxf = new Dxf();
@@ -3,8 +3,8 @@ import * as Inputs from "../inputs";
3
3
  import { Point } from "./point";
4
4
  import { Vector } from "./vector";
5
5
  /**
6
- * Contains various methods for lines and segments. Line in bitbybit is a simple object that has start and end point properties.
7
- * { start: [ x, y, z ], end: [ x, y, z ] }
6
+ * Contains various methods for lines and segments. Line in bitbybit is a simple object that has start and
7
+ * end point properties. { start: [ x, y, z ], end: [ x, y, z ] }
8
8
  */
9
9
  export declare class Line {
10
10
  private readonly vector;