1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! CSG (Constructive Solid Geometry) Operations
//!
//! Fast triangle clipping and boolean operations.
use crate::error::Result;
use crate::mesh::Mesh;
use crate::triangulation::{calculate_polygon_normal, project_to_2d, triangulate_polygon};
use nalgebra::{Point3, Vector3};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
/// Type alias for small triangle collections (typically 1-2 triangles from clipping)
pub type TriangleVec = SmallVec<[Triangle; 4]>;
/// Plane definition for clipping
#[derive(Debug, Clone, Copy)]
pub struct Plane {
/// Point on the plane
pub point: Point3<f64>,
/// Normal vector (must be normalized)
pub normal: Vector3<f64>,
}
impl Plane {
/// Create a new plane
pub fn new(point: Point3<f64>, normal: Vector3<f64>) -> Self {
Self {
point,
normal: normal.normalize(),
}
}
/// Calculate signed distance from point to plane
/// Positive = in front, Negative = behind
pub fn signed_distance(&self, point: &Point3<f64>) -> f64 {
(point - self.point).dot(&self.normal)
}
/// Check if point is in front of plane
pub fn is_front(&self, point: &Point3<f64>) -> bool {
self.signed_distance(point) >= 0.0
}
}
/// Triangle clipping result
#[derive(Debug, Clone)]
pub enum ClipResult {
/// Triangle is completely in front (keep it)
AllFront(Triangle),
/// Triangle is completely behind (discard it)
AllBehind,
/// Triangle intersects plane - returns new triangles (uses SmallVec to avoid heap allocation)
Split(TriangleVec),
}
/// Triangle definition
#[derive(Debug, Clone)]
pub struct Triangle {
pub v0: Point3<f64>,
pub v1: Point3<f64>,
pub v2: Point3<f64>,
}
impl Triangle {
/// Create a new triangle
#[inline]
pub fn new(v0: Point3<f64>, v1: Point3<f64>, v2: Point3<f64>) -> Self {
Self { v0, v1, v2 }
}
/// Calculate triangle normal
#[inline]
pub fn normal(&self) -> Vector3<f64> {
let edge1 = self.v1 - self.v0;
let edge2 = self.v2 - self.v0;
edge1.cross(&edge2).normalize()
}
/// Calculate the cross product of edges, which is twice the area vector.
///
/// Returns a `Vector3<f64>` perpendicular to the triangle plane.
/// For degenerate/collinear triangles, returns the zero vector.
/// Use `is_degenerate()` or `try_normalize()` on the result if you need
/// to detect and handle degenerate cases.
#[inline]
pub fn cross_product(&self) -> Vector3<f64> {
let edge1 = self.v1 - self.v0;
let edge2 = self.v2 - self.v0;
edge1.cross(&edge2)
}
/// Calculate triangle area (half the magnitude of the cross product).
#[inline]
pub fn area(&self) -> f64 {
self.cross_product().norm() * 0.5
}
/// Check if triangle is degenerate (zero area, collinear vertices).
///
/// Uses `try_normalize` on the cross product with the specified epsilon.
/// Returns `true` if the cross product cannot be normalized (i.e., degenerate).
#[inline]
pub fn is_degenerate(&self, epsilon: f64) -> bool {
self.cross_product().try_normalize(epsilon).is_none()
}
}
/// Maximum polygon count for either operand in a csgrs boolean operation.
///
/// Rectangular solids are 12 triangles, so this still allows the simple box-like
/// boolean cases we expect while avoiding the complex BSP trees that can overflow
/// the browser's native call stack in WASM.
const MAX_CSG_POLYGONS_PER_MESH: usize = 24;
/// Maximum combined polygon count for CSG operations.
const MAX_CSG_POLYGONS: usize = MAX_CSG_POLYGONS_PER_MESH * 2;
/// CSG Clipping Processor
pub struct ClippingProcessor {
/// Epsilon for floating point comparisons
pub epsilon: f64,
}
/// Create a box mesh from AABB min/max bounds
/// Returns a mesh with 12 triangles (2 per face, 6 faces)
fn aabb_to_mesh(min: Point3<f64>, max: Point3<f64>) -> Mesh {
let mut mesh = Mesh::with_capacity(8, 36);
// Define the 8 vertices of the box
let v0 = Point3::new(min.x, min.y, min.z); // 0: front-bottom-left
let v1 = Point3::new(max.x, min.y, min.z); // 1: front-bottom-right
let v2 = Point3::new(max.x, max.y, min.z); // 2: front-top-right
let v3 = Point3::new(min.x, max.y, min.z); // 3: front-top-left
let v4 = Point3::new(min.x, min.y, max.z); // 4: back-bottom-left
let v5 = Point3::new(max.x, min.y, max.z); // 5: back-bottom-right
let v6 = Point3::new(max.x, max.y, max.z); // 6: back-top-right
let v7 = Point3::new(min.x, max.y, max.z); // 7: back-top-left
// Add triangles for each face (counter-clockwise winding when viewed from outside)
// Front face (z = min.z) - normal points toward -Z
add_triangle_to_mesh(&mut mesh, &Triangle::new(v0, v2, v1));
add_triangle_to_mesh(&mut mesh, &Triangle::new(v0, v3, v2));
// Back face (z = max.z) - normal points toward +Z
add_triangle_to_mesh(&mut mesh, &Triangle::new(v4, v5, v6));
add_triangle_to_mesh(&mut mesh, &Triangle::new(v4, v6, v7));
// Left face (x = min.x) - normal points toward -X
add_triangle_to_mesh(&mut mesh, &Triangle::new(v0, v4, v7));
add_triangle_to_mesh(&mut mesh, &Triangle::new(v0, v7, v3));
// Right face (x = max.x) - normal points toward +X
add_triangle_to_mesh(&mut mesh, &Triangle::new(v1, v2, v6));
add_triangle_to_mesh(&mut mesh, &Triangle::new(v1, v6, v5));
// Bottom face (y = min.y) - normal points toward -Y
add_triangle_to_mesh(&mut mesh, &Triangle::new(v0, v1, v5));
add_triangle_to_mesh(&mut mesh, &Triangle::new(v0, v5, v4));
// Top face (y = max.y) - normal points toward +Y
add_triangle_to_mesh(&mut mesh, &Triangle::new(v3, v7, v6));
add_triangle_to_mesh(&mut mesh, &Triangle::new(v3, v6, v2));
mesh
}
impl ClippingProcessor {
#[inline]
fn can_run_csgrs_operation(
csg_a: &csgrs::mesh::Mesh<()>,
csg_b: &csgrs::mesh::Mesh<()>,
) -> bool {
let polygons_a = csg_a.polygons.len();
let polygons_b = csg_b.polygons.len();
if polygons_a < 4 || polygons_b < 4 {
return false;
}
if polygons_a > MAX_CSG_POLYGONS_PER_MESH || polygons_b > MAX_CSG_POLYGONS_PER_MESH {
return false;
}
polygons_a + polygons_b <= MAX_CSG_POLYGONS
}
/// Create a new clipping processor
pub fn new() -> Self {
Self { epsilon: 1e-6 }
}
/// Clip a triangle against a plane
/// Returns triangles that are in front of the plane
pub fn clip_triangle(&self, triangle: &Triangle, plane: &Plane) -> ClipResult {
// Calculate signed distances for all vertices
let d0 = plane.signed_distance(&triangle.v0);
let d1 = plane.signed_distance(&triangle.v1);
let d2 = plane.signed_distance(&triangle.v2);
// Count vertices in front of plane
let mut front_count = 0;
if d0 >= -self.epsilon {
front_count += 1;
}
if d1 >= -self.epsilon {
front_count += 1;
}
if d2 >= -self.epsilon {
front_count += 1;
}
match front_count {
// All vertices behind - discard triangle
0 => ClipResult::AllBehind,
// All vertices in front - keep triangle
3 => ClipResult::AllFront(triangle.clone()),
// One vertex in front - create 1 smaller triangle
1 => {
let (front, back1, back2) = if d0 >= -self.epsilon {
(triangle.v0, triangle.v1, triangle.v2)
} else if d1 >= -self.epsilon {
(triangle.v1, triangle.v2, triangle.v0)
} else {
(triangle.v2, triangle.v0, triangle.v1)
};
// Interpolate to find intersection points
let d_front = if d0 >= -self.epsilon {
d0
} else if d1 >= -self.epsilon {
d1
} else {
d2
};
let d_back1 = if d0 >= -self.epsilon {
d1
} else if d1 >= -self.epsilon {
d2
} else {
d0
};
let d_back2 = if d0 >= -self.epsilon {
d2
} else if d1 >= -self.epsilon {
d0
} else {
d1
};
let t1 = d_front / (d_front - d_back1);
let t2 = d_front / (d_front - d_back2);
let p1 = front + (back1 - front) * t1;
let p2 = front + (back2 - front) * t2;
ClipResult::Split(smallvec::smallvec![Triangle::new(front, p1, p2)])
}
// Two vertices in front - create 2 triangles
2 => {
let (front1, front2, back) = if d0 < -self.epsilon {
(triangle.v1, triangle.v2, triangle.v0)
} else if d1 < -self.epsilon {
(triangle.v2, triangle.v0, triangle.v1)
} else {
(triangle.v0, triangle.v1, triangle.v2)
};
// Interpolate to find intersection points
let d_back = if d0 < -self.epsilon {
d0
} else if d1 < -self.epsilon {
d1
} else {
d2
};
let d_front1 = if d0 < -self.epsilon {
d1
} else if d1 < -self.epsilon {
d2
} else {
d0
};
let d_front2 = if d0 < -self.epsilon {
d2
} else if d1 < -self.epsilon {
d0
} else {
d1
};
let t1 = d_front1 / (d_front1 - d_back);
let t2 = d_front2 / (d_front2 - d_back);
let p1 = front1 + (back - front1) * t1;
let p2 = front2 + (back - front2) * t2;
ClipResult::Split(smallvec::smallvec![
Triangle::new(front1, front2, p1),
Triangle::new(front2, p2, p1),
])
}
_ => unreachable!(),
}
}
/// Box subtraction - removes everything inside the box from the mesh
/// Uses proper CSG difference operation via subtract_mesh
pub fn subtract_box(&self, mesh: &Mesh, min: Point3<f64>, max: Point3<f64>) -> Result<Mesh> {
// Fast path: if mesh is empty, return empty mesh
if mesh.is_empty() {
return Ok(Mesh::new());
}
// Create a box mesh from the AABB bounds
let box_mesh = aabb_to_mesh(min, max);
// Use the CSG difference operation (mesh - box)
self.subtract_mesh(mesh, &box_mesh)
}
/// Extract opening profile from mesh (find largest face)
/// Returns profile points as an ordered contour and the face normal
/// Uses boundary extraction via edge counting to produce stable results
#[allow(dead_code)]
fn extract_opening_profile(
&self,
opening_mesh: &Mesh,
) -> Option<(Vec<Point3<f64>>, Vector3<f64>)> {
if opening_mesh.is_empty() {
return None;
}
// Group triangles by normal to find faces
let mut face_groups: FxHashMap<u64, Vec<(Point3<f64>, Point3<f64>, Point3<f64>)>> =
FxHashMap::default();
let normal_epsilon = 0.01; // Tolerance for normal comparison
let vertex_count = opening_mesh.positions.len() / 3;
for i in (0..opening_mesh.indices.len()).step_by(3) {
if i + 2 >= opening_mesh.indices.len() {
break;
}
let i0 = opening_mesh.indices[i] as usize;
let i1 = opening_mesh.indices[i + 1] as usize;
let i2 = opening_mesh.indices[i + 2] as usize;
// Bounds check vertex indices against positions
if i0 >= vertex_count || i1 >= vertex_count || i2 >= vertex_count {
continue;
}
let v0 = Point3::new(
opening_mesh.positions[i0 * 3] as f64,
opening_mesh.positions[i0 * 3 + 1] as f64,
opening_mesh.positions[i0 * 3 + 2] as f64,
);
let v1 = Point3::new(
opening_mesh.positions[i1 * 3] as f64,
opening_mesh.positions[i1 * 3 + 1] as f64,
opening_mesh.positions[i1 * 3 + 2] as f64,
);
let v2 = Point3::new(
opening_mesh.positions[i2 * 3] as f64,
opening_mesh.positions[i2 * 3 + 1] as f64,
opening_mesh.positions[i2 * 3 + 2] as f64,
);
let edge1 = v1 - v0;
let edge2 = v2 - v0;
// Use try_normalize to handle degenerate triangles
let normal = match edge1.cross(&edge2).try_normalize(1e-10) {
Some(n) => n,
None => continue, // Skip degenerate triangles
};
// Quantize normal for grouping (round to nearest 0.01)
let nx = (normal.x / normal_epsilon).round() as i32;
let ny = (normal.y / normal_epsilon).round() as i32;
let nz = (normal.z / normal_epsilon).round() as i32;
let key = ((nx as u64) << 32) | ((ny as u32 as u64) << 16) | (nz as u32 as u64);
face_groups.entry(key).or_default().push((v0, v1, v2));
}
// Find largest face group (most triangles = largest face)
let largest_face = face_groups
.iter()
.max_by_key(|(_, triangles)| triangles.len())?;
let triangles = largest_face.1;
if triangles.is_empty() {
return None;
}
// Build edge count map to find boundary edges
// An edge is a boundary if it appears exactly once (not shared between triangles)
// Use quantized vertex positions as keys
let quantize = |p: &Point3<f64>| -> (i64, i64, i64) {
let scale = 1e6; // Quantize to micrometer precision
(
(p.x * scale).round() as i64,
(p.y * scale).round() as i64,
(p.z * scale).round() as i64,
)
};
// Edge key: ordered pair of quantized vertices (smaller first for consistency)
let make_edge_key =
|a: (i64, i64, i64), b: (i64, i64, i64)| -> ((i64, i64, i64), (i64, i64, i64)) {
if a < b {
(a, b)
} else {
(b, a)
}
};
// Count edges and store original vertices
let mut edge_count: FxHashMap<
((i64, i64, i64), (i64, i64, i64)),
(usize, Point3<f64>, Point3<f64>),
> = FxHashMap::default();
for (v0, v1, v2) in triangles {
let q0 = quantize(v0);
let q1 = quantize(v1);
let q2 = quantize(v2);
// Three edges per triangle
for (qa, qb, pa, pb) in [(q0, q1, *v0, *v1), (q1, q2, *v1, *v2), (q2, q0, *v2, *v0)] {
let key = make_edge_key(qa, qb);
edge_count
.entry(key)
.and_modify(|(count, _, _)| *count += 1)
.or_insert((1, pa, pb));
}
}
// Collect boundary edges (count == 1)
let mut boundary_edges: Vec<(Point3<f64>, Point3<f64>)> = Vec::new();
for (_, (count, pa, pb)) in &edge_count {
if *count == 1 {
boundary_edges.push((*pa, *pb));
}
}
if boundary_edges.is_empty() {
// No boundary found (closed surface with no edges) - fall back to using centroid
return None;
}
// Build vertex adjacency map for boundary traversal
let mut adjacency: FxHashMap<(i64, i64, i64), Vec<(i64, i64, i64, Point3<f64>)>> =
FxHashMap::default();
for (pa, pb) in &boundary_edges {
let qa = quantize(pa);
let qb = quantize(pb);
adjacency
.entry(qa)
.or_default()
.push((qb.0, qb.1, qb.2, *pb));
adjacency
.entry(qb)
.or_default()
.push((qa.0, qa.1, qa.2, *pa));
}
// Build ordered contour by walking the boundary
let mut contour: Vec<Point3<f64>> = Vec::new();
let mut visited: FxHashMap<(i64, i64, i64), bool> = FxHashMap::default();
// Start from first boundary edge
if let Some((start_p, _)) = boundary_edges.first() {
let start_q = quantize(start_p);
contour.push(*start_p);
visited.insert(start_q, true);
let mut current_q = start_q;
// Walk around the boundary
loop {
let neighbors = match adjacency.get(¤t_q) {
Some(n) => n,
None => break,
};
// Find unvisited neighbor
let mut found_next = false;
for (nqx, nqy, nqz, np) in neighbors {
let nq = (*nqx, *nqy, *nqz);
if !visited.get(&nq).unwrap_or(&false) {
contour.push(*np);
visited.insert(nq, true);
current_q = nq;
found_next = true;
break;
}
}
if !found_next {
break; // Closed loop or no more unvisited neighbors
}
}
}
if contour.len() < 3 {
// Not enough points for a valid polygon
return None;
}
// Calculate normal from the ordered contour
let normal = calculate_polygon_normal(&contour);
// Normalize the result
let normalized_normal = match normal.try_normalize(1e-10) {
Some(n) => n,
None => return None, // Degenerate polygon
};
Some((contour, normalized_normal))
}
/// Convert our Mesh format to csgrs Mesh format
fn mesh_to_csgrs(mesh: &Mesh) -> Result<csgrs::mesh::Mesh<()>> {
use csgrs::mesh::{polygon::Polygon, vertex::Vertex, Mesh as CSGMesh};
use std::sync::OnceLock;
if mesh.is_empty() {
return Ok(CSGMesh {
polygons: Vec::new(),
bounding_box: OnceLock::new(),
metadata: None,
});
}
// Validate mesh has enough indices for at least one triangle
if mesh.indices.len() < 3 {
return Ok(CSGMesh {
polygons: Vec::new(),
bounding_box: OnceLock::new(),
metadata: None,
});
}
let vertex_count = mesh.positions.len() / 3;
let triangle_count = mesh.indices.len() / 3;
// Pre-allocate for expected number of triangles (avoids reallocations)
let mut polygons = Vec::with_capacity(triangle_count);
// Process each triangle using chunks_exact to ensure bounds safety
// (handles the case where indices.len() is not divisible by 3)
for chunk in mesh.indices.chunks_exact(3) {
let i0 = chunk[0] as usize;
let i1 = chunk[1] as usize;
let i2 = chunk[2] as usize;
// Bounds check for vertex indices - skip invalid triangles
if i0 >= vertex_count || i1 >= vertex_count || i2 >= vertex_count {
continue;
}
// Get triangle vertices
// Note: bounds are guaranteed by the vertex_count check above
let p0_idx = i0 * 3;
let p1_idx = i1 * 3;
let p2_idx = i2 * 3;
let v0 = Point3::new(
mesh.positions[p0_idx] as f64,
mesh.positions[p0_idx + 1] as f64,
mesh.positions[p0_idx + 2] as f64,
);
let v1 = Point3::new(
mesh.positions[p1_idx] as f64,
mesh.positions[p1_idx + 1] as f64,
mesh.positions[p1_idx + 2] as f64,
);
let v2 = Point3::new(
mesh.positions[p2_idx] as f64,
mesh.positions[p2_idx + 1] as f64,
mesh.positions[p2_idx + 2] as f64,
);
// Skip triangles with NaN or Infinity values
if !v0.x.is_finite()
|| !v0.y.is_finite()
|| !v0.z.is_finite()
|| !v1.x.is_finite()
|| !v1.y.is_finite()
|| !v1.z.is_finite()
|| !v2.x.is_finite()
|| !v2.y.is_finite()
|| !v2.z.is_finite()
{
continue;
}
// Calculate face normal from triangle edges
// Use try_normalize to handle degenerate (zero-area/collinear) triangles
let edge1 = v1 - v0;
let edge2 = v2 - v0;
let face_normal = match edge1.cross(&edge2).try_normalize(1e-10) {
Some(n) => n,
None => continue, // Skip degenerate triangles to avoid NaN propagation
};
// Note: try_normalize returns a unit vector, which is always finite
// Create csgrs vertices (use face normal for all vertices)
let vertices = vec![
Vertex::new(v0, face_normal),
Vertex::new(v1, face_normal),
Vertex::new(v2, face_normal),
];
polygons.push(Polygon::new(vertices, None));
}
Ok(CSGMesh::from_polygons(&polygons, None))
}
/// Convert csgrs Mesh format back to our Mesh format
fn csgrs_to_mesh(csg_mesh: &csgrs::mesh::Mesh<()>) -> Result<Mesh> {
let mut mesh = Mesh::new();
for polygon in &csg_mesh.polygons {
let vertices = &polygon.vertices;
if vertices.len() < 3 {
continue;
}
// Extract 3D positions
let points_3d: Vec<Point3<f64>> = vertices
.iter()
.map(|v| Point3::new(v.pos[0], v.pos[1], v.pos[2]))
.collect();
// Get the CSG polygon's intended normal (from first vertex)
// Validate and normalize to avoid NaN propagation in project_to_2d
let raw_normal = Vector3::new(
vertices[0].normal[0],
vertices[0].normal[1],
vertices[0].normal[2],
);
// Try to normalize the CSG normal; if it fails (zero or NaN), compute from points
let csg_normal = match raw_normal.try_normalize(1e-10) {
Some(n) if n.x.is_finite() && n.y.is_finite() && n.z.is_finite() => n,
_ => {
// Fall back to computing normal from polygon points
let computed = calculate_polygon_normal(&points_3d);
match computed.try_normalize(1e-10) {
Some(n) => n,
None => continue, // Skip degenerate polygon
}
}
};
// FAST PATH: Triangle - no triangulation needed
if points_3d.len() == 3 {
let base_idx = mesh.vertex_count();
for v in vertices {
mesh.add_vertex(v.pos, v.normal);
}
mesh.add_triangle(
base_idx as u32,
(base_idx + 1) as u32,
(base_idx + 2) as u32,
);
continue;
}
// Project 3D polygon to 2D using CSG normal (preserves winding intent)
let (points_2d, _, _, _) = project_to_2d(&points_3d, &csg_normal);
// Triangulate (handles convex AND concave polygons)
let indices = match triangulate_polygon(&points_2d) {
Ok(idx) => idx,
Err(_) => continue, // Skip degenerate polygons
};
// Add vertices and create triangles (winding is correct from projection)
let base_idx = mesh.vertex_count();
for v in vertices {
mesh.add_vertex(v.pos, v.normal);
}
for tri in indices.chunks(3) {
if tri.len() == 3 {
mesh.add_triangle(
(base_idx + tri[0]) as u32,
(base_idx + tri[1]) as u32,
(base_idx + tri[2]) as u32,
);
}
}
}
Ok(mesh)
}
/// Check if two meshes' bounding boxes overlap
fn bounds_overlap(host_mesh: &Mesh, opening_mesh: &Mesh) -> bool {
let (host_min, host_max) = host_mesh.bounds();
let (open_min, open_max) = opening_mesh.bounds();
// Check for overlap in all three dimensions
let overlap_x = open_min.x < host_max.x && open_max.x > host_min.x;
let overlap_y = open_min.y < host_max.y && open_max.y > host_min.y;
let overlap_z = open_min.z < host_max.z && open_max.z > host_min.z;
overlap_x && overlap_y && overlap_z
}
/// Subtract opening mesh from host mesh using csgrs CSG boolean operations
pub fn subtract_mesh(&self, host_mesh: &Mesh, opening_mesh: &Mesh) -> Result<Mesh> {
use csgrs::traits::CSG;
// Validate input meshes - early exit for empty host (no clone needed)
if host_mesh.is_empty() {
return Ok(Mesh::new());
}
if opening_mesh.is_empty() {
return Ok(host_mesh.clone());
}
// Check bounds overlap - early exit if no intersection possible
if !Self::bounds_overlap(host_mesh, opening_mesh) {
return Ok(host_mesh.clone());
}
// Convert meshes to csgrs format
let host_csg = match Self::mesh_to_csgrs(host_mesh) {
Ok(csg) => csg,
Err(_) => return Ok(host_mesh.clone()),
};
let opening_csg = match Self::mesh_to_csgrs(opening_mesh) {
Ok(csg) => csg,
Err(_) => return Ok(host_mesh.clone()),
};
// Validate CSG meshes have enough polygons for a valid operation
// Empty or near-empty meshes can cause panics in csgrs
if host_csg.polygons.is_empty() || opening_csg.polygons.is_empty() {
return Ok(host_mesh.clone());
}
// Safety: only allow simple low-polygon CSG cases. Complex operands are
// left uncut rather than risking runaway BSP recursion in csgrs.
if !Self::can_run_csgrs_operation(&host_csg, &opening_csg) {
return Ok(host_mesh.clone());
}
// Perform CSG difference (host - opening)
let result_csg = host_csg.difference(&opening_csg);
// Check if result is empty
if result_csg.polygons.is_empty() {
return Ok(host_mesh.clone());
}
// Convert back to our Mesh format
match Self::csgrs_to_mesh(&result_csg) {
Ok(result) => {
// Clean up degenerate triangles (thin slivers from CSG numerical issues)
// Note: We don't use remove_triangles_inside_bounds here because it uses
// the opening's bounding box, which can incorrectly remove valid triangles
// for complex non-rectangular openings.
let cleaned = Self::remove_degenerate_triangles(&result, host_mesh);
Ok(cleaned)
}
Err(_) => Ok(host_mesh.clone()),
}
}
/// Remove degenerate triangles from CSG result
///
/// CSG operations can create thin "sliver" triangles at intersection boundaries
/// due to numerical precision issues. This function removes triangles that:
/// 1. Have very small area (thin slivers)
/// 2. Are located inside the original host mesh bounds (not on the surface)
fn remove_degenerate_triangles(mesh: &Mesh, host_mesh: &Mesh) -> Mesh {
let (host_min, host_max) = host_mesh.bounds();
// Convert host bounds to f64 for calculations
let host_min_x = host_min.x as f64;
let host_min_y = host_min.y as f64;
let host_min_z = host_min.z as f64;
let host_max_x = host_max.x as f64;
let host_max_y = host_max.y as f64;
let host_max_z = host_max.z as f64;
// Calculate host dimensions to determine appropriate thresholds
let host_size_x = (host_max_x - host_min_x).abs();
let host_size_y = (host_max_y - host_min_y).abs();
let host_size_z = (host_max_z - host_min_z).abs();
let min_dim = host_size_x.min(host_size_y).min(host_size_z);
// Minimum area threshold - triangles smaller than this are likely artifacts
// Use 0.1% of the smallest host dimension squared
let min_area = (min_dim * 0.001).powi(2);
// Distance threshold for "inside" detection
let epsilon = min_dim * 0.01;
let mut cleaned = Mesh::new();
// Process each triangle
let vert_count = mesh.positions.len() / 3;
for i in (0..mesh.indices.len()).step_by(3) {
if i + 2 >= mesh.indices.len() {
break;
}
let i0 = mesh.indices[i] as usize;
let i1 = mesh.indices[i + 1] as usize;
let i2 = mesh.indices[i + 2] as usize;
// Bounds check vertex indices
if i0 >= vert_count || i1 >= vert_count || i2 >= vert_count {
continue;
}
// Get vertex positions
let v0 = Point3::new(
mesh.positions[i0 * 3] as f64,
mesh.positions[i0 * 3 + 1] as f64,
mesh.positions[i0 * 3 + 2] as f64,
);
let v1 = Point3::new(
mesh.positions[i1 * 3] as f64,
mesh.positions[i1 * 3 + 1] as f64,
mesh.positions[i1 * 3 + 2] as f64,
);
let v2 = Point3::new(
mesh.positions[i2 * 3] as f64,
mesh.positions[i2 * 3 + 1] as f64,
mesh.positions[i2 * 3 + 2] as f64,
);
// Calculate triangle area using cross product
let edge1 = v1 - v0;
let edge2 = v2 - v0;
let cross = edge1.cross(&edge2);
let area = cross.norm() / 2.0;
// Check if triangle is degenerate (very small area)
if area < min_area {
continue;
}
// Check if any vertex is significantly OUTSIDE the host bounds
// This catches CSG artifacts that create long thin triangles extending far from the model
let expansion = min_dim.max(1.0); // At least 1 meter expansion allowed
let far_outside = v0.x < (host_min_x - expansion)
|| v0.x > (host_max_x + expansion)
|| v0.y < (host_min_y - expansion)
|| v0.y > (host_max_y + expansion)
|| v0.z < (host_min_z - expansion)
|| v0.z > (host_max_z + expansion)
|| v1.x < (host_min_x - expansion)
|| v1.x > (host_max_x + expansion)
|| v1.y < (host_min_y - expansion)
|| v1.y > (host_max_y + expansion)
|| v1.z < (host_min_z - expansion)
|| v1.z > (host_max_z + expansion)
|| v2.x < (host_min_x - expansion)
|| v2.x > (host_max_x + expansion)
|| v2.y < (host_min_y - expansion)
|| v2.y > (host_max_y + expansion)
|| v2.z < (host_min_z - expansion)
|| v2.z > (host_max_z + expansion);
if far_outside {
continue;
}
// Check if triangle center is strictly inside the host bounds
// (not on the surface) - these are likely CSG artifacts
let center = Point3::new(
(v0.x + v1.x + v2.x) / 3.0,
(v0.y + v1.y + v2.y) / 3.0,
(v0.z + v1.z + v2.z) / 3.0,
);
// Check if center is inside host bounds (with epsilon margin)
let inside_x = center.x > (host_min_x + epsilon) && center.x < (host_max_x - epsilon);
let inside_y = center.y > (host_min_y + epsilon) && center.y < (host_max_y - epsilon);
let inside_z = center.z > (host_min_z + epsilon) && center.z < (host_max_z - epsilon);
// If triangle is strictly inside the host in ALL dimensions, it's likely an artifact
// Only remove if it's also relatively small
let max_area = min_dim * min_dim * 0.1; // 10% of smallest dimension squared
if inside_x && inside_y && inside_z && area < max_area {
continue;
}
// Get normals
let n0 = Vector3::new(
mesh.normals[i0 * 3] as f64,
mesh.normals[i0 * 3 + 1] as f64,
mesh.normals[i0 * 3 + 2] as f64,
);
let n1 = Vector3::new(
mesh.normals[i1 * 3] as f64,
mesh.normals[i1 * 3 + 1] as f64,
mesh.normals[i1 * 3 + 2] as f64,
);
let n2 = Vector3::new(
mesh.normals[i2 * 3] as f64,
mesh.normals[i2 * 3 + 1] as f64,
mesh.normals[i2 * 3 + 2] as f64,
);
// Add valid triangle to cleaned mesh
let base_idx = cleaned.vertex_count() as u32;
cleaned.add_vertex(v0, n0);
cleaned.add_vertex(v1, n1);
cleaned.add_vertex(v2, n2);
cleaned.add_triangle(base_idx, base_idx + 1, base_idx + 2);
}
cleaned
}
/// Remove triangles that are completely inside the opening bounds
///
/// This removes artifact faces that CSG operations may leave inside circular/curved openings.
/// Note: Currently unused because it can incorrectly remove valid triangles for complex
/// non-rectangular openings. Kept for potential future use with simple rectangular openings.
#[allow(dead_code)]
fn remove_triangles_inside_bounds(
mesh: &Mesh,
open_min: Point3<f64>,
open_max: Point3<f64>,
) -> Mesh {
let mut cleaned = Mesh::new();
// Process each triangle
let vert_count = mesh.positions.len() / 3;
for i in (0..mesh.indices.len()).step_by(3) {
if i + 2 >= mesh.indices.len() {
break;
}
let i0 = mesh.indices[i] as usize;
let i1 = mesh.indices[i + 1] as usize;
let i2 = mesh.indices[i + 2] as usize;
// Bounds check vertex indices
if i0 >= vert_count || i1 >= vert_count || i2 >= vert_count {
continue;
}
// Get vertex positions
let v0 = Point3::new(
mesh.positions[i0 * 3] as f64,
mesh.positions[i0 * 3 + 1] as f64,
mesh.positions[i0 * 3 + 2] as f64,
);
let v1 = Point3::new(
mesh.positions[i1 * 3] as f64,
mesh.positions[i1 * 3 + 1] as f64,
mesh.positions[i1 * 3 + 2] as f64,
);
let v2 = Point3::new(
mesh.positions[i2 * 3] as f64,
mesh.positions[i2 * 3 + 1] as f64,
mesh.positions[i2 * 3 + 2] as f64,
);
// Calculate triangle bounding box
let tri_min_x = v0.x.min(v1.x).min(v2.x);
let tri_max_x = v0.x.max(v1.x).max(v2.x);
let tri_min_y = v0.y.min(v1.y).min(v2.y);
let tri_max_y = v0.y.max(v1.y).max(v2.y);
let tri_min_z = v0.z.min(v1.z).min(v2.z);
let tri_max_z = v0.z.max(v1.z).max(v2.z);
// Check if triangle is completely inside opening bounds (remove it)
if tri_min_x >= open_min.x
&& tri_max_x <= open_max.x
&& tri_min_y >= open_min.y
&& tri_max_y <= open_max.y
&& tri_min_z >= open_min.z
&& tri_max_z <= open_max.z
{
// Triangle is inside opening - remove it
continue;
}
// Triangle is not completely inside - keep it
let n0 = Vector3::new(
mesh.normals[i0 * 3] as f64,
mesh.normals[i0 * 3 + 1] as f64,
mesh.normals[i0 * 3 + 2] as f64,
);
let n1 = Vector3::new(
mesh.normals[i1 * 3] as f64,
mesh.normals[i1 * 3 + 1] as f64,
mesh.normals[i1 * 3 + 2] as f64,
);
let n2 = Vector3::new(
mesh.normals[i2 * 3] as f64,
mesh.normals[i2 * 3 + 1] as f64,
mesh.normals[i2 * 3 + 2] as f64,
);
let base_idx = cleaned.vertex_count() as u32;
cleaned.add_vertex(v0, n0);
cleaned.add_vertex(v1, n1);
cleaned.add_vertex(v2, n2);
cleaned.add_triangle(base_idx, base_idx + 1, base_idx + 2);
}
cleaned
}
/// Union two meshes together using csgrs CSG boolean operations
pub fn union_mesh(&self, mesh_a: &Mesh, mesh_b: &Mesh) -> Result<Mesh> {
use csgrs::traits::CSG;
// Fast paths
if mesh_a.is_empty() {
return Ok(mesh_b.clone());
}
if mesh_b.is_empty() {
return Ok(mesh_a.clone());
}
// Convert meshes to csgrs format
let csg_a = Self::mesh_to_csgrs(mesh_a)?;
let csg_b = Self::mesh_to_csgrs(mesh_b)?;
// Validate CSG meshes - fall back to simple merge if invalid
if csg_a.polygons.is_empty() || csg_b.polygons.is_empty() {
let mut merged = mesh_a.clone();
merged.merge(mesh_b);
return Ok(merged);
}
if !Self::can_run_csgrs_operation(&csg_a, &csg_b) {
let mut merged = mesh_a.clone();
merged.merge(mesh_b);
return Ok(merged);
}
// Perform CSG union
let result_csg = csg_a.union(&csg_b);
// Convert back to our Mesh format
Self::csgrs_to_mesh(&result_csg)
}
/// Intersect two meshes using csgrs CSG boolean operations
///
/// Returns the intersection of two meshes (the volume where both overlap).
pub fn intersection_mesh(&self, mesh_a: &Mesh, mesh_b: &Mesh) -> Result<Mesh> {
use csgrs::traits::CSG;
// Fast paths: intersection with empty mesh is empty
if mesh_a.is_empty() || mesh_b.is_empty() {
return Ok(Mesh::new());
}
// Convert meshes to csgrs format
let csg_a = Self::mesh_to_csgrs(mesh_a)?;
let csg_b = Self::mesh_to_csgrs(mesh_b)?;
// Validate CSG meshes - return empty if invalid
if csg_a.polygons.is_empty() || csg_b.polygons.is_empty() {
return Ok(Mesh::new());
}
if !Self::can_run_csgrs_operation(&csg_a, &csg_b) {
return Ok(Mesh::new());
}
// Perform CSG intersection
let result_csg = csg_a.intersection(&csg_b);
// Convert back to our Mesh format
Self::csgrs_to_mesh(&result_csg)
}
/// Union multiple meshes together
///
/// Convenience method that sequentially unions all non-empty meshes.
/// Skips empty meshes to avoid unnecessary CSG operations.
pub fn union_meshes(&self, meshes: &[Mesh]) -> Result<Mesh> {
if meshes.is_empty() {
return Ok(Mesh::new());
}
if meshes.len() == 1 {
return Ok(meshes[0].clone());
}
// Start with first non-empty mesh
let mut result = Mesh::new();
let mut found_first = false;
for mesh in meshes {
if mesh.is_empty() {
continue;
}
if !found_first {
result = mesh.clone();
found_first = true;
continue;
}
result = self.union_mesh(&result, mesh)?;
}
Ok(result)
}
/// Subtract multiple meshes efficiently
///
/// When void count exceeds threshold, unions all voids first
/// then performs a single subtraction. This is much more efficient
/// for elements with many openings (e.g., floors with many penetrations).
///
/// # Arguments
/// * `host` - The host mesh to subtract from
/// * `voids` - List of void meshes to subtract
///
/// # Returns
/// The host mesh with all voids subtracted
pub fn subtract_meshes_batched(&self, host: &Mesh, voids: &[Mesh]) -> Result<Mesh> {
// Filter out empty meshes
let non_empty_voids: Vec<&Mesh> = voids.iter().filter(|m| !m.is_empty()).collect();
if non_empty_voids.is_empty() {
return Ok(host.clone());
}
if non_empty_voids.len() == 1 {
return self.subtract_mesh(host, non_empty_voids[0]);
}
// Threshold for batching: if more than 10 voids, union them first
const BATCH_THRESHOLD: usize = 10;
if non_empty_voids.len() > BATCH_THRESHOLD {
// Union all voids into a single mesh first
let void_refs: Vec<Mesh> = non_empty_voids.iter().map(|m| (*m).clone()).collect();
let combined = self.union_meshes(&void_refs)?;
// Single subtraction
self.subtract_mesh(host, &combined)
} else {
// Sequential subtraction for small counts
let mut result = host.clone();
for void in non_empty_voids {
result = self.subtract_mesh(&result, void)?;
}
Ok(result)
}
}
/// Subtract meshes with fallback on failure
///
/// Attempts batched subtraction, but if it fails, returns the host mesh
/// unchanged rather than propagating the error. This provides graceful
/// degradation for problematic void geometries.
pub fn subtract_meshes_with_fallback(&self, host: &Mesh, voids: &[Mesh]) -> Mesh {
match self.subtract_meshes_batched(host, voids) {
Ok(result) => {
// Validate result
if result.is_empty() || !self.validate_mesh(&result) {
host.clone()
} else {
result
}
}
Err(_) => host.clone(),
}
}
/// Validate mesh for common issues
fn validate_mesh(&self, mesh: &Mesh) -> bool {
// Check for NaN/Inf in positions
if mesh.positions.iter().any(|v| !v.is_finite()) {
return false;
}
// Check for NaN/Inf in normals
if mesh.normals.iter().any(|v| !v.is_finite()) {
return false;
}
// Check for valid triangle indices
let vertex_count = mesh.vertex_count();
for idx in &mesh.indices {
if *idx as usize >= vertex_count {
return false;
}
}
true
}
/// Clip mesh using bounding box (6 planes) - DEPRECATED: use subtract_box() instead
/// Subtracts everything inside the box from the mesh
#[deprecated(note = "Use subtract_box() for better performance")]
pub fn clip_mesh_with_box(
&self,
mesh: &Mesh,
min: Point3<f64>,
max: Point3<f64>,
) -> Result<Mesh> {
self.subtract_box(mesh, min, max)
}
/// Clip an entire mesh against a plane
pub fn clip_mesh(&self, mesh: &Mesh, plane: &Plane) -> Result<Mesh> {
let mut result = Mesh::new();
// Process each triangle
let vert_count = mesh.positions.len() / 3;
for i in (0..mesh.indices.len()).step_by(3) {
if i + 2 >= mesh.indices.len() {
break;
}
let i0 = mesh.indices[i] as usize;
let i1 = mesh.indices[i + 1] as usize;
let i2 = mesh.indices[i + 2] as usize;
// Bounds check vertex indices
if i0 >= vert_count || i1 >= vert_count || i2 >= vert_count {
continue;
}
// Get triangle vertices
let v0 = Point3::new(
mesh.positions[i0 * 3] as f64,
mesh.positions[i0 * 3 + 1] as f64,
mesh.positions[i0 * 3 + 2] as f64,
);
let v1 = Point3::new(
mesh.positions[i1 * 3] as f64,
mesh.positions[i1 * 3 + 1] as f64,
mesh.positions[i1 * 3 + 2] as f64,
);
let v2 = Point3::new(
mesh.positions[i2 * 3] as f64,
mesh.positions[i2 * 3 + 1] as f64,
mesh.positions[i2 * 3 + 2] as f64,
);
let triangle = Triangle::new(v0, v1, v2);
// Clip triangle
match self.clip_triangle(&triangle, plane) {
ClipResult::AllFront(tri) => {
// Keep original triangle
add_triangle_to_mesh(&mut result, &tri);
}
ClipResult::AllBehind => {
// Discard triangle
}
ClipResult::Split(triangles) => {
// Add clipped triangles
for tri in triangles {
add_triangle_to_mesh(&mut result, &tri);
}
}
}
}
Ok(result)
}
}
impl Default for ClippingProcessor {
fn default() -> Self {
Self::new()
}
}
/// Add a triangle to a mesh
fn add_triangle_to_mesh(mesh: &mut Mesh, triangle: &Triangle) {
let base_idx = mesh.vertex_count() as u32;
// Calculate normal
let normal = triangle.normal();
// Add vertices
mesh.add_vertex(triangle.v0, normal);
mesh.add_vertex(triangle.v1, normal);
mesh.add_vertex(triangle.v2, normal);
// Add triangle
mesh.add_triangle(base_idx, base_idx + 1, base_idx + 2);
}
/// Calculate smooth normals for a mesh.
/// On desktop, this is a no-op because the frontend computes normals in JS
/// after decoding (normals are not sent over IPC to save bandwidth).
/// WASM path keeps full normal calculation.
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub fn calculate_normals(_mesh: &mut Mesh) {}
#[cfg(target_arch = "wasm32")]
#[inline]
pub fn calculate_normals(mesh: &mut Mesh) {
let vertex_count = mesh.vertex_count();
if vertex_count == 0 {
return;
}
let positions_len = mesh.positions.len();
// Initialize normals to zero
let mut normals = vec![Vector3::zeros(); vertex_count];
// Accumulate face normals
for i in (0..mesh.indices.len()).step_by(3) {
// Bounds check for indices array
if i + 2 >= mesh.indices.len() {
break;
}
let i0 = mesh.indices[i] as usize;
let i1 = mesh.indices[i + 1] as usize;
let i2 = mesh.indices[i + 2] as usize;
// Bounds check for vertex indices - skip invalid triangles
if i0 >= vertex_count || i1 >= vertex_count || i2 >= vertex_count {
continue;
}
if i0 * 3 + 2 >= positions_len || i1 * 3 + 2 >= positions_len || i2 * 3 + 2 >= positions_len
{
continue;
}
// Get triangle vertices
let v0 = Point3::new(
mesh.positions[i0 * 3] as f64,
mesh.positions[i0 * 3 + 1] as f64,
mesh.positions[i0 * 3 + 2] as f64,
);
let v1 = Point3::new(
mesh.positions[i1 * 3] as f64,
mesh.positions[i1 * 3 + 1] as f64,
mesh.positions[i1 * 3 + 2] as f64,
);
let v2 = Point3::new(
mesh.positions[i2 * 3] as f64,
mesh.positions[i2 * 3 + 1] as f64,
mesh.positions[i2 * 3 + 2] as f64,
);
// Calculate face normal
let edge1 = v1 - v0;
let edge2 = v2 - v0;
let normal = edge1.cross(&edge2);
// Accumulate normal for each vertex
normals[i0] += normal;
normals[i1] += normal;
normals[i2] += normal;
}
// Normalize and write back
mesh.normals.clear();
mesh.normals.reserve(vertex_count * 3);
for normal in normals {
let normalized = normal
.try_normalize(1e-6)
.unwrap_or_else(|| Vector3::new(0.0, 0.0, 1.0));
mesh.normals.push(normalized.x as f32);
mesh.normals.push(normalized.y as f32);
mesh.normals.push(normalized.z as f32);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_plane_signed_distance() {
let plane = Plane::new(Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
assert_eq!(plane.signed_distance(&Point3::new(0.0, 0.0, 5.0)), 5.0);
assert_eq!(plane.signed_distance(&Point3::new(0.0, 0.0, -5.0)), -5.0);
assert_eq!(plane.signed_distance(&Point3::new(5.0, 5.0, 0.0)), 0.0);
}
#[test]
fn test_clip_triangle_all_front() {
let processor = ClippingProcessor::new();
let triangle = Triangle::new(
Point3::new(0.0, 0.0, 1.0),
Point3::new(1.0, 0.0, 1.0),
Point3::new(0.5, 1.0, 1.0),
);
let plane = Plane::new(Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
match processor.clip_triangle(&triangle, &plane) {
ClipResult::AllFront(_) => {}
_ => panic!("Expected AllFront"),
}
}
#[test]
fn test_clip_triangle_all_behind() {
let processor = ClippingProcessor::new();
let triangle = Triangle::new(
Point3::new(0.0, 0.0, -1.0),
Point3::new(1.0, 0.0, -1.0),
Point3::new(0.5, 1.0, -1.0),
);
let plane = Plane::new(Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
match processor.clip_triangle(&triangle, &plane) {
ClipResult::AllBehind => {}
_ => panic!("Expected AllBehind"),
}
}
#[test]
fn test_clip_triangle_split_one_front() {
let processor = ClippingProcessor::new();
let triangle = Triangle::new(
Point3::new(0.0, 0.0, 1.0), // Front
Point3::new(1.0, 0.0, -1.0), // Behind
Point3::new(0.5, 1.0, -1.0), // Behind
);
let plane = Plane::new(Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
match processor.clip_triangle(&triangle, &plane) {
ClipResult::Split(triangles) => {
assert_eq!(triangles.len(), 1);
}
_ => panic!("Expected Split"),
}
}
#[test]
fn test_clip_triangle_split_two_front() {
let processor = ClippingProcessor::new();
let triangle = Triangle::new(
Point3::new(0.0, 0.0, 1.0), // Front
Point3::new(1.0, 0.0, 1.0), // Front
Point3::new(0.5, 1.0, -1.0), // Behind
);
let plane = Plane::new(Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
match processor.clip_triangle(&triangle, &plane) {
ClipResult::Split(triangles) => {
assert_eq!(triangles.len(), 2);
}
_ => panic!("Expected Split with 2 triangles"),
}
}
#[test]
fn test_triangle_normal() {
let triangle = Triangle::new(
Point3::new(0.0, 0.0, 0.0),
Point3::new(1.0, 0.0, 0.0),
Point3::new(0.0, 1.0, 0.0),
);
let normal = triangle.normal();
assert!((normal.z - 1.0).abs() < 1e-6);
}
#[test]
fn test_triangle_area() {
let triangle = Triangle::new(
Point3::new(0.0, 0.0, 0.0),
Point3::new(1.0, 0.0, 0.0),
Point3::new(0.0, 1.0, 0.0),
);
let area = triangle.area();
assert!((area - 0.5).abs() < 1e-6);
}
#[test]
fn test_csgrs_operation_guard_allows_simple_boxes() {
let box_a = aabb_to_mesh(Point3::new(0.0, 0.0, 0.0), Point3::new(1.0, 1.0, 1.0));
let box_b = aabb_to_mesh(Point3::new(0.25, 0.25, 0.25), Point3::new(0.75, 0.75, 0.75));
let csg_a = ClippingProcessor::mesh_to_csgrs(&box_a).unwrap();
let csg_b = ClippingProcessor::mesh_to_csgrs(&box_b).unwrap();
assert!(ClippingProcessor::can_run_csgrs_operation(&csg_a, &csg_b));
}
#[test]
fn test_csgrs_operation_guard_rejects_complex_operands() {
let box_mesh = aabb_to_mesh(Point3::new(0.0, 0.0, 0.0), Point3::new(1.0, 1.0, 1.0));
let mut complex_mesh = Mesh::new();
complex_mesh.merge(&box_mesh);
complex_mesh.merge(&box_mesh);
complex_mesh.merge(&box_mesh);
let csg_a = ClippingProcessor::mesh_to_csgrs(&complex_mesh).unwrap();
let csg_b = ClippingProcessor::mesh_to_csgrs(&box_mesh).unwrap();
assert!(!ClippingProcessor::can_run_csgrs_operation(&csg_a, &csg_b));
}
}