diff --git a/crates/kernel/src/body.rs b/crates/kernel/src/body.rs index 9a6b49e3..f320e4b5 100644 --- a/crates/kernel/src/body.rs +++ b/crates/kernel/src/body.rs @@ -291,6 +291,21 @@ impl Body { }) } + /// Faces with no triangles in a coarse tessellation (the mesher failed or gave up). + pub fn unmeshed_faces(&self) -> usize { + if self.is_mesh() { + return 0; + } + let Ok(m) = self.tessellate((self.size() * 1e-2).max(1e-3)) else { return self.face_count() }; + let mut has = vec![false; self.face_count()]; + for f in &m.tri_face { + if let Some(h) = has.get_mut(*f as usize) { + *h = true; + } + } + has.iter().filter(|h| !**h).count() + } + /// Display mesh: like [`Body::tessellate`], with seam edges flagged. pub fn display_mesh(&self, tol: f64) -> Result { let mut m = self.tessellate(tol)?; diff --git a/crates/kernel/src/step_in/geom.rs b/crates/kernel/src/step_in/geom.rs index b4e1f08f..ef816dd6 100644 --- a/crates/kernel/src/step_in/geom.rs +++ b/crates/kernel/src/step_in/geom.rs @@ -566,6 +566,23 @@ fn seam_frame(f: &Frame, extent: &[mt::Point3], seam: &[mt::Point3]) -> Frame { } } +/// Length of the mean unit direction from the frame origin to the points (1: all in one +/// direction, 0: spread all round). +fn mean_direction(f: &Frame, pts: &[mt::Point3]) -> f64 { + let dirs: Vec = pts + .iter() + .filter_map(|p| { + let d = p - f.o; + let m = d.magnitude(); + (m > 1e-12).then(|| d / m) + }) + .collect(); + if dirs.is_empty() { + return 0.0; + } + (dirs.iter().fold(mt::Vector3::new(0.0, 0.0, 0.0), |a, d| a + d) / dirs.len() as f64).magnitude() +} + /// Polar axis for a sphere: far from every boundary point, and (for a face concentrated on /// one side) with both poles outside the face, so every loop closes in parameter space. fn sphere_axis(f: &Frame, extent: &[mt::Point3]) -> mt::Vector3 { @@ -810,7 +827,10 @@ pub(crate) fn surface(cx: &Ctx, id: u64, extent: &[mt::Point3], seam: &[mt::Poin } // With seam edges the file's own parameterisation fits the loops; else pick poles // away from the boundary. - let f = if seam.is_empty() { + // A small patch (a corner blend) gets poles away from it even when the file runs a + // seam through it: a seam ending at a pole on the patch boundary leaves the mesher + // no way to tell the patch from the rest of the sphere. + let f = if seam.is_empty() || mean_direction(&f, extent) > 0.3 { let z = sphere_axis(&f, extent); let x = any_perpendicular(z); seam_frame(&Frame { o: f.o, x, y: z.cross(x), z }, extent, seam) diff --git a/crates/kernel/src/step_in/mod.rs b/crates/kernel/src/step_in/mod.rs index d0be70af..7887f5b1 100644 --- a/crates/kernel/src/step_in/mod.rs +++ b/crates/kernel/src/step_in/mod.rs @@ -233,6 +233,8 @@ struct Reader<'a> { node_items: HashSet, /// Occurrences and mapped representations visited so far. visits: usize, + /// Items whose meshing was checked. + unmeshed_checked: HashSet, bodies: Vec, } @@ -454,6 +456,13 @@ impl<'a> Reader<'a> { Ok(body) => { let own = e.params().first().and_then(Param::as_str).unwrap_or("").trim().to_string(); let name = if own.is_empty() { product.to_string() } else { own }; + // Faces the mesher gives up on (or that the budget cut short) are reported. + if self.unmeshed_checked.insert(item) { + let n = body.unmeshed_faces(); + if n > 0 { + self.warn(format!("solid '{name}': {n} face(s) could not be meshed and are not shown")); + } + } out.push(self.bodies.len()); let color = self.colors.get(&item).copied(); self.bodies.push(ImportedBody { name, body: body.with_color(color), color, path: path.to_vec(), closed, file_faces }); @@ -682,6 +691,7 @@ pub fn step_import(text: &str) -> Result { used_items: HashSet::new(), node_items: HashSet::new(), visits: 0, + unmeshed_checked: HashSet::new(), bodies: Vec::new(), }; rd.index(); diff --git a/crates/kernel/src/step_in/tests.rs b/crates/kernel/src/step_in/tests.rs index 11aef063..8f7dedb9 100644 --- a/crates/kernel/src/step_in/tests.rs +++ b/crates/kernel/src/step_in/tests.rs @@ -367,3 +367,70 @@ fn mirrored_revolved_faces_round_trip() { round_trip(&boolean(&plate, &tool, BoolOp::Cut).unwrap().unwrap()); } } + +/// Faces whose boundaries are nowhere near their surfaces, or wild B-spline surfaces, mesh +/// within bounded work (the mesher once looped for ever on such input) and are reported if +/// they cannot be meshed. +#[test] +fn hostile_boundaries_mesh_in_bounded_time() { + let t0 = std::time::Instant::now(); + // A wild 24 × 24 B-spline patch (alternating ±50 mm) bounded by four straight edges that + // lie far from it, plus a plane face bounded by a circle 500 mm off the plane. + let n = 24; + let mut t = String::from(HEAD); + let mut rows = Vec::new(); + for i in 0..n { + let mut row = Vec::new(); + for j in 0..n { + let id = 1000 + i * n + j; + let z = if (i * 7 + j * 13) % 2 == 0 { 50.0 } else { -50.0 }; + t += &format!("#{id}=CARTESIAN_POINT('',({}.,{}.,{z}));\n", i * 10, j * 10); + row.push(format!("#{id}")); + } + rows.push(format!("({})", row.join(","))); + } + let knots = |m: usize| { + let mut mult = vec!["4".to_string()]; + mult.extend(std::iter::repeat_n("1".to_string(), m - 4)); + mult.push("4".into()); + let ks: Vec = (0..=(m - 3)).map(|k| format!("{k}.")).collect(); + (mult.join(","), ks.join(",")) + }; + let (mu, ku) = knots(n); + t += &format!( + "#10=B_SPLINE_SURFACE_WITH_KNOTS('',3,3,({}),.UNSPECIFIED.,.F.,.F.,.F.,({mu}),({mu}),({ku}),({ku}),.UNSPECIFIED.);\n", + rows.join(",") + ); + let corners = [(0, 0), (390, 0), (390, 390), (0, 390)]; + for (k, (x, y)) in corners.iter().enumerate() { + t += &format!("#{}=CARTESIAN_POINT('',({x}.,{y}.,300.));\n#{}=VERTEX_POINT('',#{});\n", 20 + k, 30 + k, 20 + k); + } + t += "#40=DIRECTION('',(1.,0.,0.));\n#41=VECTOR('',#40,1.);\n"; + for k in 0..4 { + t += &format!( + "#{}=LINE('',#{},#41);\n#{}=EDGE_CURVE('',#{},#{},#{},.T.);\n#{}=ORIENTED_EDGE('',*,*,#{},.T.);\n", + 50 + k, + 20 + k, + 60 + k, + 30 + k, + 30 + (k + 1) % 4, + 50 + k, + 70 + k, + 60 + k + ); + } + t += "#80=EDGE_LOOP('',(#70,#71,#72,#73));\n#81=FACE_OUTER_BOUND('',#80,.T.);\n#82=ADVANCED_FACE('',(#81),#10,.T.);\n"; + t += "#90=CARTESIAN_POINT('',(0.,0.,0.));\n#91=DIRECTION('',(0.,0.,1.));\n#92=AXIS2_PLACEMENT_3D('',#90,#91,#40);\n#93=PLANE('',#92);\n"; + t += "#94=CARTESIAN_POINT('',(0.,0.,500.));\n#95=AXIS2_PLACEMENT_3D('',#94,#91,#40);\n#96=CIRCLE('',#95,30.);\n#97=CARTESIAN_POINT('',(30.,0.,500.));\n#98=VERTEX_POINT('',#97);\n"; + t += "#99=EDGE_CURVE('',#98,#98,#96,.T.);\n#100=ORIENTED_EDGE('',*,*,#99,.T.);\n#101=EDGE_LOOP('',(#100));\n#102=FACE_OUTER_BOUND('',#101,.T.);\n#103=ADVANCED_FACE('',(#102),#93,.T.);\n"; + t += "#110=OPEN_SHELL('',(#82,#103));\n#111=SHELL_BASED_SURFACE_MODEL('Wild',(#110));\n#112=SHAPE_REPRESENTATION('',(#111),$);\n"; + t += TAIL; + let r = std::panic::catch_unwind(|| step_import(&t)); + assert!(r.is_ok(), "panicked"); + if let Ok(Ok(imp)) = r { + for b in &imp.bodies { + let _ = measure(&b.body); + } + } + assert!(t0.elapsed().as_secs_f64() < 60.0, "took {:?}", t0.elapsed()); +} diff --git a/docs/step-import.md b/docs/step-import.md index 54eb5831..39cfd172 100644 --- a/docs/step-import.md +++ b/docs/step-import.md @@ -12,73 +12,73 @@ Read: manifold solid breps (with voids), faceted breps, shell-based surface mode Generated by `cargo xtask step-corpus`. Each case is a part exported from Fusion as STEP (AP214; kept locally in `plan/fusion/oracle/`, never committed). SolveCraft opens `part.step` exactly as File → Open does (an Import base feature) and compares with Fusion's own measurements: body count, volume and area (relative tolerance 1e-3) and the number of faces read from the file. -**60 / 66 files pass.** +**65 / 66 files pass.** | Case | Result | Bodies | Volume mm³ (got / Fusion) | Area mm² (got / Fusion) | Faces (got / Fusion) | ms | Notes | |---|---|---|---|---|---|---|---| -| 01-box | pass | Box | 24000.000 / 24000.000 | 5200.000 / 5200.000 | 6 / 6 | 16 | | -| 02-cylinder | pass | Cylinder | 35339.346 / 35342.917 | 6125.844 / 6126.106 | 3 / 3 | 37 | | -| 03-box-through-hole | pass | Box | 45738.489 / 45738.053 | 9327.795 / 9327.788 | 7 / 7 | 28 | | -| 04-revolve-vase | pass | Vase | 112487.028 / 112499.479 | 13156.053 / 13156.615 | 5 / 5 | 371 | | -| 05-taper-extrude | pass | Frustum | 61814.826 / 61814.826 | 9676.915 / 9676.915 | 6 / 6 | 19 | | +| 01-box | pass | Box | 24000.000 / 24000.000 | 5200.000 / 5200.000 | 6 / 6 | 8 | | +| 02-cylinder | pass | Cylinder | 35339.429 / 35342.917 | 6125.845 / 6126.106 | 3 / 3 | 89 | | +| 03-box-through-hole | pass | Box | 45738.283 / 45738.053 | 9327.809 / 9327.788 | 7 / 7 | 147 | | +| 04-revolve-vase | pass | Vase | 112488.811 / 112499.479 | 13156.092 / 13156.615 | 5 / 5 | 502 | | +| 05-taper-extrude | pass | Frustum | 61814.826 / 61814.826 | 9676.915 / 9676.915 | 6 / 6 | 10 | | | 06-symmetric-extrude | pass | Block | 30000.000 / 30000.000 | 6200.000 / 6200.000 | 6 / 6 | 1 | | -| 07-fillet-all-edges | pass | Box | 23340.395 / 23340.849 | 4726.168 / 4726.265 | 26 / 26 | 63 | | -| 08-chamfer | pass | Box | 23730.667 / 23730.667 | 5029.352 / 5029.352 | 10 / 10 | 12 | | -| 09-shell | pass | Box | 13632.000 / 13632.000 | 13992.000 / 13992.000 | 11 / 11 | 8 | | -| 10-rect-pattern-holes | pass | Plate | 29803.869 / 29803.540 | 14652.467 / 14652.389 | 18 / 18 | 270 | | -| 11-circular-pattern-flange | pass | Flange | 66758.484 / 66758.844 | 19320.589 / 19320.795 | 10 / 10 | 90 | | -| 12-mirror-feature | pass | Base | 21392.261 / 21392.920 | 6530.917 / 6530.973 | 10 / 10 | 16 | | -| 13-combine-join | pass | A | 45547.074 / 45571.680 | 8633.463 / 8634.336 | 9 / 9 | 19 | | -| 14-combine-cut | pass | A | 42934.679 / 42931.417 | 9706.604 / 9706.858 | 7 / 7 | 95 | | -| 15-combine-intersect | pass | A | 54442.787 / 54454.273 | 7381.985 / 7382.743 | 7 / 7 | 223 | | -| 16-sweep | pass | Tube | 2584.167 / 2584.724 | 1779.580 / 1779.698 | 5 / 5 | 430 | | -| 17-loft | pass | Loft | 35124.785 / 35125.498 | 6496.433 / 6495.000 | 6 / 6 | 91 | | -| 18-hole-features | pass | Plate | 33890.986 / 33890.710 | 8568.685 / 8568.696 | 13 / 13 | 26 | | -| 19-l-bracket-constrained | pass | LBracket | 14720.000 / 14720.000 | 5472.000 / 5472.000 | 8 / 8 | 9 | | -| 20-slot-constrained | pass | Slot | 2965.374 / 2965.487 | 1774.635 / 1774.690 | 6 / 6 | 4 | | +| 07-fillet-all-edges | pass | Box | 23340.621 / 23340.849 | 4726.220 / 4726.265 | 26 / 26 | 286 | | +| 08-chamfer | pass | Box | 23730.667 / 23730.667 | 5029.352 / 5029.352 | 10 / 10 | 14 | | +| 09-shell | pass | Box | 13632.000 / 13632.000 | 13992.000 / 13992.000 | 11 / 11 | 17 | | +| 10-rect-pattern-holes | pass | Plate | 29803.713 / 29803.540 | 14652.481 / 14652.389 | 18 / 18 | 282 | | +| 11-circular-pattern-flange | pass | Flange | 66758.144 / 66758.844 | 19320.617 / 19320.795 | 10 / 10 | 236 | | +| 12-mirror-feature | pass | Base | 21392.575 / 21392.920 | 6530.944 / 6530.973 | 10 / 10 | 179 | | +| 13-combine-join | pass | A | 45570.295 / 45571.680 | 8634.196 / 8634.336 | 9 / 9 | 105 | | +| 14-combine-cut | pass | A | 42933.034 / 42931.417 | 9706.783 / 9706.858 | 7 / 7 | 823 | | +| 15-combine-intersect | pass | A | 54452.172 / 54454.273 | 7382.532 / 7382.743 | 7 / 7 | 1957 | | +| 16-sweep | pass | Tube | 2584.428 / 2584.724 | 1779.638 / 1779.698 | 5 / 5 | 1379 | | +| 17-loft | pass | Loft | 35121.851 / 35125.498 | 6497.868 / 6495.000 | 6 / 6 | 140 | | +| 18-hole-features | pass | Plate | 33890.921 / 33890.710 | 8568.694 / 8568.696 | 13 / 13 | 287 | | +| 19-l-bracket-constrained | pass | LBracket | 14720.000 / 14720.000 | 5472.000 / 5472.000 | 8 / 8 | 2 | | +| 20-slot-constrained | pass | Slot | 2965.428 / 2965.487 | 1774.644 / 1774.690 | 6 / 6 | 51 | | | 21-draft | pass | Box | 54574.737 / 54574.737 | 8781.998 / 8781.998 | 6 / 6 | 1 | | -| 22-split-body | pass | Cyl, Cyl (1) | 75380.935 / 75398.224 | 12954.433 / 12955.175 | 6 / 6 | 19 | | -| 23-bracket-multifeature | pass | Bracket | 59331.560 / 59331.129 | 18290.761 / 18290.737 | 19 / 19 | 37 | | -| 24-user-parameters | pass | Block | 34822.124 / 34821.903 | 8114.167 / 8114.159 | 7 / 7 | 4 | | -| 25-sketch-triangle | pass | Triangle | 4032.155 / 4032.155 | 2274.099 / 2274.099 | 5 / 5 | 18 | | -| 26-sketch-belt | pass | Belt | 11326.051 / 11326.170 | 5637.346 / 5637.404 | 7 / 7 | 24 | | -| 27-sketch-trapezoid | pass | Trapezoid | 5342.311 / 5342.257 | 3066.959 / 3066.946 | 8 / 8 | 9 | | -| 28-blind-pocket-fillets | pass | Block | 84299.400 / 84299.249 | 16279.542 / 16279.572 | 23 / 23 | 56 | | -| 29-two-sided-and-partial-revolve | pass | Block | 25731.359 / 25731.307 | 6312.589 / 6312.602 | 12 / 12 | 9 | | -| 30-fillet-after-shell | pass | Box | 21745.919 / 21745.876 | 14968.279 / 14968.332 | 27 / 27 | 82 | | -| 31-fillet-l-all-edges | pass | L | 14321.347 / 14321.626 | 5023.960 / 5024.045 | 38 / 38 | 68 | | -| 32-boss-plate-fillets | pass | Part | 33590.191 / 33590.350 | 9929.841 / 9929.864 | 10 / 10 | 620 | | -| 33-fillet-tangent-chain | pass | Slot | 8236.938 / 8237.122 | 2793.516 / 2793.569 | 14 / 14 | 202 | | -| 34-chamfer-then-fillet | pass | Box | 22892.369 / 22892.421 | 4753.674 / 4753.688 | 18 / 18 | 31 | | -| 35-fillet-curved-curved | FAIL | Tee | 63090.315 / 63171.443 | 10267.244 / 10269.348 | 6 / 6 | 1602 | | -| 36-corner-blend-two-radii | pass | Box | 57799.345 / 57800.240 | 8585.404 / 8585.524 | 18 / 18 | 88 | | -| 37-pyramid-apex-blend | FAIL | Pyramid | 15483.389 / 15465.575 | 3972.101 / 3914.581 | 18 / 18 | 786 | | -| 38-coincident-cylinders | pass | A | 15706.106 / 15707.963 | 3769.742 / 3769.911 | 3 / 3 | 27 | | -| 39-tee-pipe | FAIL | Tee | 24069.177 / 24096.237 | 16706.241 / 16710.048 | 7 / 7 | 64 | | -| 40-box-sphere-torus | pass | A | 36287.351 / 36287.433 | 10695.939 / 10696.165 | 10 / 10 | 764 | | -| 41-tube-radial-holes | FAIL | Tube | 20070.139 / 20243.981 | 14849.742 / 14873.773 | 22 / 22 | 428 | | -| 42-overlapping-patterns | pass | Plate | 38489.045 / 38487.064 | 21687.203 / 21686.603 | 43 / 43 | 469 | solid 'Plate': imported as an open body (the shell is not closed) | -| 43-cut-splits-body | pass | Bar, Bar (1) | 28000.000 / 28000.000 | 7200.000 / 7200.000 | 12 / 12 | 19 | | -| 44-shell-l-bracket | pass | L | 8384.000 / 8384.000 | 8784.000 / 8784.000 | 15 / 15 | 17 | | -| 45-shell-boss-plate | pass | Part | 13676.925 / 13676.602 | 14156.500 / 14156.602 | 15 / 15 | 11 | | -| 46-shell-two-open-faces | pass | Box | 16044.000 / 16044.000 | 11398.000 / 11398.000 | 10 / 10 | 2 | | -| 47-draft-curved-pocket | pass | Block | 111388.144 / 111387.712 | 17700.329 / 17700.361 | 11 / 11 | 11 | | -| 48-pulley | pass | Pulley | 168507.117 / 168510.841 | 23718.181 / 23718.644 | 12 / 12 | 83 | | -| 49-helical-sweep | pass | Spring | 4736.230 / 4738.099 | 4766.821 / 4767.652 | 3 / 3 | 390482 | solid 'Spring': face wrapping around a closed surface kept whole: split line does not reach the rings | -| 50-sweep-tight-bend | FAIL | Bend | 3822.321 / 3758.443 | 1708.995 / 2153.937 | 5 / 5 | 1797 | | -| 51-loft-rect-circle-offset | pass | Loft | 61143.729 / 61133.116 | 9415.804 / 9411.655 | 6 / 6 | 142 | | -| 52-circular-pattern-fillet | pass | Hub | 47206.223 / 47206.850 | 14142.022 / 14142.171 | 21 / 21 | 625 | | -| 53-path-pattern | pass | Plate | 35010.589 / 35010.398 | 16463.939 / 16463.894 | 13 / 13 | 87 | | -| 54-mirror-body-fillets | pass | Bracket | 22780.205 / 22780.089 | 6972.823 / 6972.832 | 12 / 12 | 38 | | -| 55-tapped-hole-cosmetic | pass | Plate | 17542.404 / 17542.314 | 4761.584 / 4761.596 | 8 / 8 | 13 | | -| 56-holes-on-curved-face | FAIL | Cyl | 77733.175 / 77942.388 | 10590.123 / 10593.139 | 7 / 7 | 93 | | -| 57-hole-pattern-sketch-points | pass | Plate | 36767.098 / 36766.712 | 11818.159 / 11818.153 | 24 / 24 | 165 | | -| 58-phone-stand | pass | Stand | 74559.445 / 74559.177 | 29148.741 / 29148.785 | 19 / 19 | 25 | | -| 59-enclosure-base | pass | Enclosure | 34309.026 / 34309.486 | 33795.404 / 33795.610 | 39 / 39 | 74 | | -| 60-pipe-flange | pass | Flange | 323660.116 / 323660.979 | 56984.492 / 56984.564 | 16 / 16 | 495 | | -| 61-shaft-keyway | pass | Shaft | 47493.351 / 47525.946 | 9675.779 / 9677.096 | 14 / 14 | 77 | | -| 62-heat-sink | pass | HeatSink | 43125.000 / 43125.000 | 45725.000 / 45725.000 | 66 / 66 | 41 | | -| 63-hinge-knuckle | pass | Hinge | 4781.312 / 4783.117 | 4109.841 / 4110.287 | 14 / 14 | 47 | | -| 64-sketch-tangent-arc-chain | pass | Wave | 4800.000 / 4800.000 | 3843.701 / 3843.712 | 9 / 9 | 47 | | -| 65-sketch-gear-tooth | pass | Tooth | 3987.829 / 3988.084 | 1945.416 / 1945.485 | 9 / 9 | 20 | | -| 66-sketch-linkage | pass | Link | 8045.431 / 8045.386 | 4308.168 / 4308.175 | 10 / 10 | 94 | | +| 22-split-body | pass | Cyl, Cyl (1) | 75390.813 / 75398.224 | 12954.440 / 12955.175 | 6 / 6 | 302 | | +| 23-bracket-multifeature | pass | Bracket | 59331.477 / 59331.129 | 18290.767 / 18290.737 | 19 / 19 | 190 | | +| 24-user-parameters | pass | Block | 34822.022 / 34821.903 | 8114.175 / 8114.159 | 7 / 7 | 113 | | +| 25-sketch-triangle | pass | Triangle | 4032.155 / 4032.155 | 2274.099 / 2274.099 | 5 / 5 | 11 | | +| 26-sketch-belt | pass | Belt | 11326.042 / 11326.170 | 5637.347 / 5637.404 | 7 / 7 | 91 | | +| 27-sketch-trapezoid | pass | Trapezoid | 5342.286 / 5342.257 | 3066.961 / 3066.946 | 8 / 8 | 148 | | +| 28-blind-pocket-fillets | pass | Block | 84299.345 / 84299.249 | 16279.560 / 16279.572 | 23 / 23 | 280 | | +| 29-two-sided-and-partial-revolve | pass | Block | 25731.356 / 25731.307 | 6312.589 / 6312.602 | 12 / 12 | 64 | | +| 30-fillet-after-shell | pass | Box | 21745.915 / 21745.876 | 14968.284 / 14968.332 | 27 / 27 | 273 | | +| 31-fillet-l-all-edges | pass | L | 14321.482 / 14321.626 | 5024.003 / 5024.045 | 38 / 38 | 335 | | +| 32-boss-plate-fillets | pass | Part | 33590.273 / 33590.350 | 9929.854 / 9929.864 | 10 / 10 | 3052 | | +| 33-fillet-tangent-chain | pass | Slot | 8237.030 / 8237.122 | 2793.540 / 2793.569 | 14 / 14 | 449 | | +| 34-chamfer-then-fillet | pass | Box | 22892.383 / 22892.421 | 4753.677 / 4753.688 | 18 / 18 | 46 | | +| 35-fillet-curved-curved | pass | Tee | 63169.683 / 63171.443 | 10269.167 / 10269.348 | 6 / 6 | 1868 | | +| 36-corner-blend-two-radii | pass | Box | 57799.502 / 57800.240 | 8585.435 / 8585.524 | 18 / 18 | 454 | | +| 37-pyramid-apex-blend | pass | Pyramid | 15465.376 / 15465.575 | 3914.529 / 3914.581 | 18 / 18 | 322 | | +| 38-coincident-cylinders | pass | A | 15706.406 / 15707.963 | 3769.760 / 3769.911 | 3 / 3 | 182 | | +| 39-tee-pipe | pass | Tee | 24095.763 / 24096.237 | 16709.772 / 16710.048 | 7 / 7 | 426 | | +| 40-box-sphere-torus | pass | A | 36287.563 / 36287.433 | 10696.145 / 10696.165 | 10 / 10 | 5171 | | +| 41-tube-radial-holes | pass | Tube | 20243.046 / 20243.981 | 14873.689 / 14873.773 | 22 / 22 | 2199 | | +| 42-overlapping-patterns | pass | Plate | 38488.411 / 38487.064 | 21687.215 / 21686.603 | 43 / 43 | 1012 | solid 'Plate': imported as an open body (the shell is not closed) | +| 43-cut-splits-body | pass | Bar, Bar (1) | 28000.000 / 28000.000 | 7200.000 / 7200.000 | 12 / 12 | 32 | | +| 44-shell-l-bracket | pass | L | 8384.000 / 8384.000 | 8784.000 / 8784.000 | 15 / 15 | 9 | | +| 45-shell-boss-plate | pass | Part | 13676.354 / 13676.602 | 14156.531 / 14156.602 | 15 / 15 | 149 | | +| 46-shell-two-open-faces | pass | Box | 16044.000 / 16044.000 | 11398.000 / 11398.000 | 10 / 10 | 22 | | +| 47-draft-curved-pocket | pass | Block | 111387.999 / 111387.712 | 17700.336 / 17700.361 | 11 / 11 | 16 | | +| 48-pulley | pass | Pulley | 168507.231 / 168510.841 | 23718.208 / 23718.644 | 12 / 12 | 235 | | +| 49-helical-sweep | pass | Spring | 4733.931 / 4738.099 | 4765.903 / 4767.652 | 3 / 3 | 11850 | solid 'Spring': face wrapping around a closed surface kept whole: split line does not reach the rings | +| 50-sweep-tight-bend | FAIL | Bend | 3822.427 / 3758.443 | 1708.948 / 2153.937 | 5 / 5 | 605 | | +| 51-loft-rect-circle-offset | pass | Loft | 61130.904 / 61133.116 | 9416.313 / 9411.655 | 6 / 6 | 39 | | +| 52-circular-pattern-fillet | pass | Hub | 47206.182 / 47206.850 | 14141.988 / 14142.171 | 21 / 21 | 526 | | +| 53-path-pattern | pass | Plate | 35010.499 / 35010.398 | 16463.947 / 16463.894 | 13 / 13 | 46 | | +| 54-mirror-body-fillets | pass | Bracket | 22780.113 / 22780.089 | 6972.831 / 6972.832 | 12 / 12 | 38 | | +| 55-tapped-hole-cosmetic | pass | Plate | 17542.366 / 17542.314 | 4761.590 / 4761.596 | 8 / 8 | 33 | | +| 56-holes-on-curved-face | pass | Cyl | 77941.179 / 77942.388 | 10592.950 / 10593.139 | 7 / 7 | 178 | | +| 57-hole-pattern-sketch-points | pass | Plate | 36767.029 / 36766.712 | 11818.165 / 11818.153 | 24 / 24 | 69 | | +| 58-phone-stand | pass | Stand | 74559.442 / 74559.177 | 29148.741 / 29148.785 | 19 / 19 | 11 | | +| 59-enclosure-base | pass | Enclosure | 34309.038 / 34309.486 | 33795.405 / 33795.610 | 39 / 39 | 81 | | +| 60-pipe-flange | pass | Flange | 323661.056 / 323660.979 | 56984.269 / 56984.564 | 16 / 16 | 463 | | +| 61-shaft-keyway | pass | Shaft | 47521.487 / 47525.946 | 9676.780 / 9677.096 | 14 / 14 | 80 | | +| 62-heat-sink | pass | HeatSink | 43125.000 / 43125.000 | 45725.000 / 45725.000 | 66 / 66 | 7 | | +| 63-hinge-knuckle | pass | Hinge | 4783.029 / 4783.117 | 4110.255 / 4110.287 | 14 / 14 | 55 | | +| 64-sketch-tangent-arc-chain | pass | Wave | 4800.000 / 4800.000 | 3843.701 / 3843.712 | 9 / 9 | 7 | | +| 65-sketch-gear-tooth | pass | Tooth | 3987.831 / 3988.084 | 1945.416 / 1945.485 | 9 / 9 | 5 | | +| 66-sketch-linkage | pass | Link | 8045.410 / 8045.386 | 4308.172 / 4308.175 | 10 / 10 | 49 | | diff --git a/vendor/README.md b/vendor/README.md index e3c35c16..df15a974 100644 --- a/vendor/README.md +++ b/vendor/README.md @@ -34,5 +34,11 @@ Changes: non-periodic domain are rejected (a hinted B-spline search can run off and extrapolate to thousands of parameter units, and meshing that grid never finishes); the last-resort nearest search is clamped into the domain. +- `tessellation/triangulation.rs`, bounded work (hostile files must not hang): edge polylines + and surface grids come from our own bounded divisions (at most 20 000 points per edge, a + grid budget per face shared out of 10 million points per shell, 24 refinement rounds; a + point that does not evaluate counts as flat instead of recursing). Surface grids refine only + the direction that bends, and a ruling left undivided gets cells comparable to the other + direction (long triangles from a trim loop to the grid's far ends cut through cylinders). - `lib.rs`: compiler warnings no longer fail the build. Example, test and bench targets are dropped (their sources are not vendored). diff --git a/vendor/truck-meshalgo/src/tessellation/triangulation.rs b/vendor/truck-meshalgo/src/tessellation/triangulation.rs index d483200f..ff259840 100644 --- a/vendor/truck-meshalgo/src/tessellation/triangulation.rs +++ b/vendor/truck-meshalgo/src/tessellation/triangulation.rs @@ -98,6 +98,7 @@ where C: PolylineableCurve + 'a, S: PreMeshableSurface + 'a, { + let cap = face_cap(shell.len()); let vmap: HashMap<_, _> = shell .vertex_par_iter() .map(|v| (v.id(), v.mapped(Point3::clone))) @@ -109,7 +110,7 @@ where let v0 = vmap.get(&edge.absolute_front().id()).unwrap(); let v1 = vmap.get(&edge.absolute_back().id()).unwrap(); let curve = edge.curve(); - let poly = PolylineCurve::from_curve(&curve, curve.range_tuple(), tol); + let poly = bounded_polyline(&curve, curve.range_tuple(), tol); (id, Edge::debug_new(v0, v1, poly)) }) .collect(); @@ -128,7 +129,7 @@ where .iter() .map(create_boundary) .collect(); - shell_create_polygon(&face.surface(), wires, face.orientation(), tol, &sp) + shell_create_polygon(&face.surface(), wires, face.orientation(), tol, &sp, cap) }; shell.face_par_iter().map(create_face).collect() } @@ -146,6 +147,7 @@ where { use truck_base::entry_map::FxEntryMap as EntryMap; use truck_topology::Vertex as TVertex; + let cap = face_cap(shell.len()); let mut vmap = EntryMap::new( move |v: &TVertex| v.id(), move |v| v.mapped(Point3::clone), @@ -158,7 +160,7 @@ where let vb = edge.absolute_back(); let v1 = vmap.entry_or_insert(vb).clone(); let curve = edge.curve(); - let poly = PolylineCurve::from_curve(&curve, curve.range_tuple(), tol); + let poly = bounded_polyline(&curve, curve.range_tuple(), tol); Edge::debug_new(&v0, &v1, poly) }, ); @@ -178,7 +180,7 @@ where .iter() .map(&mut create_boundary) .collect(); - shell_create_polygon(&face.surface(), wires, face.orientation(), tol, &sp) + shell_create_polygon(&face.surface(), wires, face.orientation(), tol, &sp, cap) }; shell.face_iter().map(create_face).collect() } @@ -194,11 +196,12 @@ where S: PreMeshableSurface + 'a, { let vertices = shell.vertices.clone(); + let cap = face_cap(shell.faces.len()); let tessellate_edge = |edge: &CompressedEdge| { let curve = &edge.curve; CompressedEdge { vertices: edge.vertices, - curve: PolylineCurve::from_curve(curve, curve.range_tuple(), tol), + curve: bounded_polyline(curve, curve.range_tuple(), tol), } }; #[cfg(not(target_arch = "wasm32"))] @@ -219,7 +222,7 @@ where let preboundary: Option> = boundaries.iter().map(create_boundary).collect(); let polygon: Option = (|| { let boundary = PolyBoundary::new(preboundary?, &surface, tol); - Some(trimming_tessellation(&surface, &boundary, tol)) + Some(trimming_tessellation(&surface, &boundary, tol, cap)) })(); CompressedFace { boundaries, @@ -244,6 +247,7 @@ fn shell_create_polygon( orientation: bool, tol: f64, sp: impl SP, + cap: usize, ) -> Face> { let preboundary = wires .iter() @@ -254,7 +258,7 @@ fn shell_create_polygon( .collect::>>(); let polygon: Option = (|| { let boundary = PolyBoundary::new(preboundary?, &surface, tol); - Some(trimming_tessellation(surface, &boundary, tol)) + Some(trimming_tessellation(surface, &boundary, tol, cap)) })(); let mut new_face = Face::debug_new(wires, polygon); if !orientation { @@ -559,11 +563,11 @@ fn spade_round(x: f64) -> f64 { } /// Tessellates one surface trimmed by polyline. -fn trimming_tessellation(surface: &S, polyboundary: &PolyBoundary, tol: f64) -> PolygonMesh +fn trimming_tessellation(surface: &S, polyboundary: &PolyBoundary, tol: f64, cap: usize) -> PolygonMesh where S: PreMeshableSurface { let mut triangulation = Cdt::new(); polyboundary.insert_to(&mut triangulation); - insert_surface(&mut triangulation, surface, polyboundary, tol); + insert_surface(&mut triangulation, surface, polyboundary, tol, cap); let mut mesh = triangulation_into_polymesh( triangulation.vertices(), triangulation.inner_faces(), @@ -580,10 +584,15 @@ fn insert_surface( surface: impl PreMeshableSurface, polyline: &PolyBoundary, tol: f64, + cap: usize, ) { let bdb: BoundingBox = polyline.0.iter().flatten().collect(); let range = ((bdb.min()[0], bdb.max()[0]), (bdb.min()[1], bdb.max()[1])); - let (udiv, vdiv) = surface.parameter_division(range, tol); + if !(range.0 .0.is_finite() && range.0 .1.is_finite() && range.1 .0.is_finite() && range.1 .1.is_finite()) { + return; + } + let (udiv, vdiv) = bounded_surface_division(&surface, range, tol, cap); + let (udiv, vdiv) = balanced_grid(&surface, udiv, vdiv, cap); let insert_res: Vec>> = udiv .into_iter() .map(|u| { @@ -619,6 +628,162 @@ fn insert_surface( }); } +/// SolveCraft: most grid points a face gets (keeps hostile or huge faces bounded). +pub(super) const MAX_GRID: usize = 150_000; +/// SolveCraft: grid points shared by all faces of one shell; many faces get fewer each. +pub(super) const SHELL_GRID_BUDGET: usize = 10_000_000; +/// SolveCraft: most points on one edge's polyline. +const MAX_EDGE_POINTS: usize = 20_000; + +/// SolveCraft: the grid budget of each face of a shell with `faces` faces. +pub(super) fn face_cap(faces: usize) -> usize { + (SHELL_GRID_BUDGET / faces.max(1)).clamp(2_000, MAX_GRID) +} + +/// SolveCraft: truck's curve division, bounded: at most `MAX_EDGE_POINTS` points and a +/// bisection depth of 24; a midpoint that does not evaluate (NaN) counts as flat (truck's +/// version recursed on it 100 levels deep, which never finishes). +fn bounded_polyline(curve: &C, range: (f64, f64), tol: f64) -> PolylineCurve { + let mut out = vec![curve.subs(range.0)]; + let mut stack: Vec<(f64, f64, usize)> = vec![(range.0, range.1, 0)]; + while let Some((a, b, depth)) = stack.pop() { + let (pa, pb) = (curve.subs(a), curve.subs(b)); + let t = a + (b - a) * 0.4729; + let chord = pa + (pb - pa) * 0.4729; + let d2 = curve.subs(t).distance2(chord); + let tm = 0.5 * (a + b); + let dm = curve.subs(tm).distance2(pa.midpoint(pb)); + let flat = !(d2 >= tol * tol || dm >= tol * tol); + if flat || depth >= 24 || out.len() + stack.len() >= MAX_EDGE_POINTS { + out.push(pb); + } else { + // Right half after the left one (stack order). + stack.push((tm, b, depth + 1)); + stack.push((a, tm, depth + 1)); + } + } + PolylineCurve(out) +} + +/// SolveCraft: truck's surface division, bounded: stops refining when the grid would exceed +/// `cap` points or after 24 rounds; cells that do not evaluate (NaN) are not refined. +fn bounded_surface_division(surface: &S, (ur, vr): ((f64, f64), (f64, f64)), tol: f64, cap: usize) -> (Vec, Vec) { + let (mut udiv, mut vdiv) = (vec![ur.0, ur.1], vec![vr.0, vr.1]); + for _ in 0..24 { + let mut fu = vec![false; udiv.len() - 1]; + let mut fv = vec![false; vdiv.len() - 1]; + for (i, u) in udiv.windows(2).enumerate() { + for (j, v) in vdiv.windows(2).enumerate() { + if fu[i] && fv[j] { + continue; + } + let (p, q) = (0.4729, 0.5271); + let (u0, v0) = (u[0] * (1.0 - p) + u[1] * p, v[0] * (1.0 - q) + v[1] * q); + let p0 = surface.subs(u0, v0); + let pt00 = surface.subs(u[0], v[0]).to_vec(); + let pt01 = surface.subs(u[0], v[1]).to_vec(); + let pt10 = surface.subs(u[1], v[0]).to_vec(); + let pt11 = surface.subs(u[1], v[1]).to_vec(); + let pt = Point3::from_vec(pt00 * (1.0 - p) * (1.0 - q) + pt01 * (1.0 - p) * q + pt10 * p * (1.0 - q) + pt11 * p * q); + let far = p0.distance2(pt) > tol * tol; + // Which direction bends: refine only that one (a cylinder needs no division + // along its rulings). + let alu = surface.subs(u[0], v0).to_vec() * (1.0 - p) + surface.subs(u[1], v0).to_vec() * p; + let alv = surface.subs(u0, v[0]).to_vec() * (1.0 - q) + surface.subs(u0, v[1]).to_vec() * q; + let bend_u = p0.distance2(Point3::from_vec(alu)) > tol * tol; + let bend_v = p0.distance2(Point3::from_vec(alv)) > tol * tol; + if bend_u || bend_v { + fu[i] |= bend_u; + fv[j] |= bend_v; + } else if far { + fu[i] = true; + fv[j] = true; + } + } + } + let nu = udiv.len() + fu.iter().filter(|x| **x).count(); + let nv = vdiv.len() + fv.iter().filter(|x| **x).count(); + if nu == udiv.len() && nv == vdiv.len() { + break; + } + if nu.saturating_mul(nv) > cap { + break; + } + let refine = |d: &[f64], f: &[bool]| { + let mut n = vec![d[0]]; + for (w, b) in d.windows(2).zip(f) { + if *b { + n.push(0.5 * (w[0] + w[1])); + } + n.push(w[1]); + } + n + }; + udiv = refine(&udiv, &fu); + vdiv = refine(&vdiv, &fv); + } + (udiv, vdiv) +} + +/// SolveCraft: refine the parameter grid so its cells are not much longer in one direction than +/// the other. A ruled surface (cylinder, cone) needs no division along its rulings, and the +/// triangles that then join a trimming loop to the far ends of the grid cut through the +/// surface (a cylinder with a hole lost volume this way). +fn balanced_grid(surface: &impl PreMeshableSurface, udiv: Vec, vdiv: Vec, cap: usize) -> (Vec, Vec) { + let mid = |d: &[f64]| if d.is_empty() { 0.0 } else { d[d.len() / 2] }; + let (um, vm) = (mid(&udiv), mid(&vdiv)); + let step = |d: &[f64], along_u: bool| -> Vec { + d.windows(2) + .map(|w| { + let (a, b) = (w[0], w[1]); + if along_u { surface.subs(a, vm).distance(surface.subs(b, vm)) } else { surface.subs(um, a).distance(surface.subs(um, b)) } + }) + .collect() + }; + let (lu, lv) = (step(&udiv, true), step(&vdiv, false)); + let typical = |l: &[f64]| { + let mut v: Vec = l.iter().copied().filter(|x| x.is_finite() && *x > 0.0).collect(); + v.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); + if v.is_empty() { None } else { Some(v[v.len() / 2]) } + }; + let refine = |d: &[f64], l: &[f64], target: Option| -> Vec { + let Some(t) = target.filter(|t| *t > 0.0) else { return d.to_vec() }; + // At most 24 cells along a ruling: enough for holes and trims to get short triangles. + let total: f64 = l.iter().filter(|x| x.is_finite()).sum(); + let t = t.max(total / 24.0 / 4.0); + let mut out = Vec::with_capacity(d.len()); + for (i, w) in d.windows(2).enumerate() { + let n = l.get(i).map(|x| (x / (4.0 * t)).ceil()).filter(|n| n.is_finite()).unwrap_or(1.0).clamp(1.0, 64.0) as usize; + for k in 0..n { + out.push(w[0] + (w[1] - w[0]) * k as f64 / n as f64); + } + } + if let Some(last) = d.last() { + out.push(*last); + } + out + }; + // Only a direction left undivided (a ruling) needs it; curved directions already follow + // the tolerance. + let u2 = if udiv.len() <= 3 { refine(&udiv, &lu, typical(&lv)) } else { udiv.clone() }; + let v2 = if vdiv.len() <= 3 { refine(&vdiv, &lv, typical(&lu)) } else { vdiv.clone() }; + let (u2, v2) = if u2.len().saturating_mul(v2.len()) > cap { (udiv, vdiv) } else { (u2, v2) }; + // Never more than MAX_GRID points: thin out evenly (the mesh gets coarser, not endless). + let total = u2.len().saturating_mul(v2.len()); + if total <= cap { + return (u2, v2); + } + let f = (total as f64 / cap.max(4) as f64).sqrt(); + let thin = |d: Vec| -> Vec { + let n = ((d.len() as f64 / f).ceil() as usize).max(2); + if d.len() <= n { + return d; + } + (0..n).map(|i| d[i * (d.len() - 1) / (n - 1)]).collect() + }; + (thin(u2), thin(v2)) +} + /// Converts triangulation into `PolygonMesh`. fn triangulation_into_polymesh<'a>( vertices: VertexIterator<'a, SPoint2, (), CdtEdge<()>, ()>,