* claim: #5662 * Show imported mesh outlines in viewport (#5662) * test: update mesh regression origin visibility setup
This commit is contained in:
committed by
GitHub
parent
16a5254e97
commit
699f526429
@@ -87,6 +87,7 @@ impl PlacedMesh {
|
||||
positions: source.positions.iter().map(|p| crate::apply_point(&self.transform, *p)).collect(),
|
||||
normals: source.normals.iter().map(|n| crate::apply_vector(&self.transform, *n)).collect(),
|
||||
edges: source.edges.iter().map(|edge| edge.iter().map(|p| crate::apply_point(&self.transform, *p)).collect()).collect(),
|
||||
display_only_edges: source.display_only_edges,
|
||||
triangles: source.triangles.clone(),
|
||||
tri_face: source.tri_face.clone(),
|
||||
edge_faces: source.edge_faces.clone(),
|
||||
@@ -115,7 +116,51 @@ pub(super) fn definition_plan(
|
||||
}
|
||||
|
||||
pub(super) fn reference_mesh(body: &Body, mesh: &OnceLock<Arc<Mesh>>, tolerance: f64) -> Arc<Mesh> {
|
||||
mesh.get_or_init(|| Arc::new(body.reference_display_mesh_angular(tolerance, kernel::DISPLAY_ANGLE).unwrap_or_default())).clone()
|
||||
mesh.get_or_init(|| {
|
||||
let mut mesh = body.reference_display_mesh_angular(tolerance, kernel::DISPLAY_ANGLE).unwrap_or_default();
|
||||
if let Some((positions, triangles)) = body.triangle_mesh() {
|
||||
mesh.edges = mesh_outlines(positions, triangles);
|
||||
mesh.display_only_edges = true;
|
||||
}
|
||||
Arc::new(mesh)
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// Display lines for an imported triangle mesh: open boundaries, non-manifold sides and
|
||||
/// creases of at least 30 degrees. The shared vertex indices come from the existing mesh body;
|
||||
/// the geometry, winding and triangle topology are left to the importer.
|
||||
fn mesh_outlines(positions: &[Vec3], triangles: &[[u32; 3]]) -> Vec<Vec<Vec3>> {
|
||||
let normal = |index: usize| {
|
||||
let t = triangles.get(index)?;
|
||||
let (a, b, c) = (positions.get(t[0] as usize)?, positions.get(t[1] as usize)?, positions.get(t[2] as usize)?);
|
||||
(*b - *a).cross(*c - *a).normalized()
|
||||
};
|
||||
// One compact record per side, sorted for deterministic output, instead of a map of
|
||||
// separately allocated adjacency lists. Mesh imports already cap the triangle count.
|
||||
let mut sides = Vec::with_capacity(triangles.len().saturating_mul(3));
|
||||
for (i, t) in triangles.iter().enumerate() {
|
||||
let Ok(triangle) = u32::try_from(i) else { continue };
|
||||
if normal(i).is_some() {
|
||||
for (a, b) in [(t[0], t[1]), (t[1], t[2]), (t[2], t[0])] {
|
||||
sides.push((a.min(b), a.max(b), triangle));
|
||||
}
|
||||
}
|
||||
}
|
||||
sides.sort_unstable();
|
||||
let cos = 30f64.to_radians().cos();
|
||||
let mut edges = Vec::new();
|
||||
for side in sides.chunk_by(|a, b| (a.0, a.1) == (b.0, b.1)) {
|
||||
let Some(&(a, b, _)) = side.first() else { continue };
|
||||
let visible = match side {
|
||||
[(_, _, i), (_, _, j)] => normal(*i as usize).zip(normal(*j as usize)).is_some_and(|(n, m)| n.dot(m) <= cos),
|
||||
_ => true,
|
||||
};
|
||||
if visible && let (Some(&a), Some(&b)) = (positions.get(a as usize), positions.get(b as usize)) {
|
||||
edges.push(vec![a, b]);
|
||||
}
|
||||
}
|
||||
edges
|
||||
}
|
||||
|
||||
/// Only proper rotations and translations preserve triangle winding, normal lengths and the
|
||||
@@ -135,6 +180,45 @@ fn proper_rigid(m: &crate::Mat) -> bool {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn mesh_outlines_keep_boundaries_and_creases_without_smooth_facet_lines() {
|
||||
for (degrees, expected) in [(0.0f64, 4), (15.0, 4), (30.0, 5), (45.0, 5), (90.0, 5)] {
|
||||
let angle = degrees.to_radians();
|
||||
let p = [Vec3::ZERO, Vec3::X, Vec3::Y, Vec3::new(0., -angle.cos(), angle.sin())];
|
||||
let triangles = [[0, 1, 2], [1, 0, 3]];
|
||||
let edges = mesh_outlines(&p, &triangles);
|
||||
assert_eq!(edges.len(), expected, "hinge angle {degrees}");
|
||||
assert_eq!(edges.iter().any(|e| e == &[Vec3::ZERO, Vec3::X]), degrees >= 30.);
|
||||
assert_eq!(edges, mesh_outlines(&p, &triangles), "deterministic order");
|
||||
}
|
||||
// A third triangle makes the common side non-manifold: keep it once, even if coplanar.
|
||||
let p = [Vec3::ZERO, Vec3::X, Vec3::Y, -Vec3::Y, Vec3::Y * 2.];
|
||||
let edges = mesh_outlines(&p, &[[0, 1, 2], [1, 0, 3], [0, 1, 4]]);
|
||||
assert_eq!(edges.len(), 7);
|
||||
assert_eq!(edges.iter().filter(|e| **e == [Vec3::ZERO, Vec3::X]).count(), 1);
|
||||
// The rings of an open faceted cylinder stay visible, but its smooth wall has no
|
||||
// diagonals or longitudinal facet lines.
|
||||
let n = 36u32;
|
||||
let mut p = Vec::new();
|
||||
for z in [0., 10.] {
|
||||
p.extend((0..n).map(|i| {
|
||||
let a = f64::from(i) * std::f64::consts::TAU / f64::from(n);
|
||||
Vec3::new(a.cos(), a.sin(), z)
|
||||
}));
|
||||
}
|
||||
let triangles: Vec<_> = (0..n)
|
||||
.flat_map(|i| {
|
||||
let j = (i + 1) % n;
|
||||
[[i, j, j + n], [i, j + n, i + n]]
|
||||
})
|
||||
.collect();
|
||||
let edges = mesh_outlines(&p, &triangles);
|
||||
assert_eq!(edges.len(), 2 * n as usize);
|
||||
assert!(edges.iter().all(|e| e[0].z == e[1].z));
|
||||
// Bad indices and zero-area facets do not invent outlines or panic.
|
||||
assert!(mesh_outlines(&[Vec3::ZERO, Vec3::X, Vec3::X * 2.], &[[0, 1, 99], [0, 1, 2]]).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placed_mesh_is_lazy_and_composes_from_one_immutable_source() {
|
||||
let source = ModelBody::new("Part".into(), kernel::box_solid(Vec3::ZERO, Vec3::new(2., 3., 4.)).unwrap(), 1);
|
||||
|
||||
+13
-4
@@ -92,7 +92,7 @@ impl Extend<Vec3> for Aabb3 {
|
||||
}
|
||||
|
||||
/// An indexed triangle mesh with per-vertex normals, a face id per triangle (the B-rep face it
|
||||
/// came from) and edge polylines (the B-rep edges, for display and picking).
|
||||
/// came from) and edge polylines (B-rep edges for display and picking, or mesh outlines).
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct Mesh {
|
||||
pub positions: Vec<Vec3>,
|
||||
@@ -101,8 +101,12 @@ pub struct Mesh {
|
||||
pub triangles: Vec<[u32; 3]>,
|
||||
/// B-rep face index per triangle.
|
||||
pub tri_face: Vec<u32>,
|
||||
/// B-rep edges as polylines.
|
||||
/// Edge polylines; mesh outlines have `display_only_edges` set.
|
||||
pub edges: Vec<Vec<Vec3>>,
|
||||
/// These polylines belong to a triangle mesh, not selectable B-rep edges. Rebuilt with
|
||||
/// the display mesh; never part of the saved design's geometry.
|
||||
#[serde(skip)]
|
||||
pub display_only_edges: bool,
|
||||
/// Per edge: a seam between two pieces of the same smooth surface (not drawn).
|
||||
#[serde(default)]
|
||||
pub seams: Vec<bool>,
|
||||
@@ -369,11 +373,16 @@ impl Mesh {
|
||||
impl Mesh {
|
||||
/// Visibility in shaded edge presentation, independent of selection/topology.
|
||||
pub fn shaded_edge_visible(&self, index: usize) -> bool {
|
||||
self.edge_pickable(index) && !self.smooth_edges.get(index).copied().unwrap_or(false)
|
||||
self.edge_visible(index) && !self.smooth_edges.get(index).copied().unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Visibility in wireframe, including triangle-mesh outlines without B-rep identities.
|
||||
pub fn edge_visible(&self, index: usize) -> bool {
|
||||
!self.seams.get(index).copied().unwrap_or(false) || self.sharp_edges.get(index).copied().unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn edge_pickable(&self, index: usize) -> bool {
|
||||
!self.seams.get(index).copied().unwrap_or(false) || self.sharp_edges.get(index).copied().unwrap_or(false)
|
||||
!self.display_only_edges && self.edge_visible(index)
|
||||
}
|
||||
|
||||
/// Edges bounding face `f`.
|
||||
|
||||
@@ -12,6 +12,7 @@ fn cube_mesh(s: f64) -> Mesh {
|
||||
triangles,
|
||||
tri_face: vec![0; n],
|
||||
edges: vec![],
|
||||
display_only_edges: false,
|
||||
seams: vec![],
|
||||
smooth_edges: vec![],
|
||||
sharp_edges: vec![],
|
||||
@@ -37,6 +38,9 @@ fn display_edge_masks_keep_identity_and_break_only_certified_sharp_patches() {
|
||||
assert_eq!(mesh.surface_patch(1), vec![1, 2]);
|
||||
assert_eq!(mesh.edges.len(), 2);
|
||||
assert_eq!(mesh.face_edges(1), vec![0, 1]);
|
||||
mesh.display_only_edges = true;
|
||||
assert!(mesh.edge_visible(0) && mesh.shaded_edge_visible(0));
|
||||
assert!(!mesh.edge_pickable(0));
|
||||
assert_eq!(serde_json::to_value(&mesh).unwrap(), stored);
|
||||
let reopened: Mesh = serde_json::from_value(stored).unwrap();
|
||||
assert!(reopened.smooth_edges.is_empty() && reopened.sharp_edges.is_empty());
|
||||
|
||||
@@ -382,6 +382,7 @@ mod tests {
|
||||
triangles: vec![[0, 1, 2]],
|
||||
tri_face: vec![0],
|
||||
edges: vec![],
|
||||
display_only_edges: false,
|
||||
seams: vec![],
|
||||
smooth_edges: vec![],
|
||||
sharp_edges: vec![],
|
||||
|
||||
@@ -767,7 +767,7 @@ impl BodyDraw {
|
||||
}
|
||||
let at = |p: Vec3| self.place.as_ref().map_or(p, |m| solidcraft_engine::doc::apply_point(m, p)).to_f32();
|
||||
for (ei, e) in m.edges.iter().enumerate().filter(|_| edges) {
|
||||
if !m.edge_pickable(ei) || (shaded && !m.shaded_edge_visible(ei)) {
|
||||
if !m.edge_visible(ei) || (shaded && !m.shaded_edge_visible(ei)) {
|
||||
continue;
|
||||
}
|
||||
for w in e.windows(2) {
|
||||
@@ -5720,6 +5720,43 @@ mod tests {
|
||||
assert_eq!(merged.lines.len(), 3 * segments * crate::gpu::LINE_SIZE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imported_mesh_edges_draw_in_each_style_without_brep_picks() {
|
||||
let h = crate::scenario::Harness::new();
|
||||
let path = h.dir.join("box.3mf");
|
||||
let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());
|
||||
app.run("solid.box", json!({"length":20,"width":20,"height":10})).unwrap();
|
||||
app.run("file.save_mesh", json!({"path":path})).unwrap();
|
||||
app.run("file.new", json!({})).unwrap();
|
||||
let imported = app.run("file.insert_mesh", json!({"path":path})).unwrap();
|
||||
app.run("component.activate", json!({"component":"root"})).unwrap();
|
||||
app.run("occurrence.copy", json!({"component":imported["component"],"translate":[40,0,0]})).unwrap();
|
||||
app.ui.show_grid = false;
|
||||
app.set_origin_shown(false);
|
||||
app.ui.ground_shadow = false;
|
||||
app.fit_view();
|
||||
for style in 0..4 {
|
||||
app.ui.visual_style = style;
|
||||
let scene = build_scene(&app).merged();
|
||||
let lines = if style == 1 { 0 } else { 24 * crate::gpu::LINE_SIZE };
|
||||
assert_eq!(scene.lines.len(), lines, "style {style}: twelve edges per placed box");
|
||||
assert_eq!(scene.hidden.len(), if style == 3 { lines } else { 0 });
|
||||
assert_eq!(scene.tris.is_empty(), style == 2, "wireframe draws only the edges");
|
||||
}
|
||||
let proj = projection(&app, Rect::from_min_size(Pos2::ZERO, vec2(800., 600.)));
|
||||
for body in &app.session.world_state().bodies {
|
||||
let mesh = body.mesh();
|
||||
assert_eq!(mesh.edges.len(), 12, "the placed mesh keeps its outlines");
|
||||
for (i, edge) in mesh.edges.iter().enumerate() {
|
||||
assert!(!mesh.edge_pickable(i), "placements keep display-only edge semantics");
|
||||
for point in [edge[0], (edge[0] + edge[1]) * 0.5] {
|
||||
let hits = pick_all(&app, &proj, proj.to_screen(point).unwrap());
|
||||
assert!(!hits.iter().any(|h| matches!(h, Hit::Edge { .. } | Hit::Vertex { .. })), "{hits:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Manual GPU benchmark: 25 placements of the mesh `SOLIDCRAFT_BENCH_MESH` (an STL, OBJ or
|
||||
/// 3MF) drawn offscreen at 3600 × 2190 px (divided by `SOLIDCRAFT_BENCH_DIV`) with 4× MSAA
|
||||
/// while the camera turns, each frame waited for: the placements' own triangles (`merged`, as
|
||||
|
||||
Reference in New Issue
Block a user