fix(face3d): repair edge editing

This commit is contained in:
Hakan Seven committed 2026-08-28 20:14:41 +03:00
commit 56bb652521
44 files changed
+5310 -1656

No files matched your search

+62 -166
View File
@@ -2,143 +2,56 @@ use acadrust::entities::{mesh::Mesh, polygon_mesh::PolygonMesh, Face3D, Polyface
use cadkernel::geom2d::{triangulate, Tolerance};
use cadkernel::space::{polygon, Plane, Vec3 as KernelVec3};
use glam::Vec3;
use crate::t;
use crate::command::EntityTransform;
use crate::entities::common::{
edit_prop as edit, parse_f64, ro_prop as ro, stepper_prop as stepper, square_grip,
};
use crate::entities::traits::{Grippable, PropertyEditable, Transformable, RenderConvertible};
use crate::entities::traits::{Grippable, PropertyEditable, RenderConvertible, Transformable};
use crate::scene::convert::acad_to_render::{RenderEntity, RenderObject};
use crate::scene::model::object::{GripApply, GripDef, PropSection, PropValue, Property};
use crate::scene::model::wire_model::SnapHint;
use crate::t;
/// Triangulate a planar (possibly concave) polygon into a flat triangle-soup
/// (3 vertices per triangle), preserving the polygon's winding. A simple fan
/// from vertex 0 is only valid for convex faces — a concave face (e.g. an
/// L-shaped mesh face) fans into triangles that spill outside the outline. Ear
/// clipping handles both. Falls back to a fan when the polygon is degenerate.
/// Triangulate a planar polygon through the kernel.
pub(crate) fn triangulate_planar(poly: &[[f64; 3]]) -> Vec<[f64; 3]> {
let n = poly.len();
if n < 3 {
if poly.len() < 3 {
return Vec::new();
}
if n == 3 {
return vec![poly[0], poly[1], poly[2]];
}
let cross = |a: [f64; 3], b: [f64; 3]| {
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
let origin = KernelVec3::from(poly[0]);
let tolerance = Tolerance::default().linear();
let Some(axis) = poly[1..]
.iter()
.map(|point| KernelVec3::from(*point) - origin)
.find(|axis| axis.length() > tolerance)
else {
return Vec::new();
};
let dot = |a: [f64; 3], b: [f64; 3]| a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
let fan = || {
let mut out = Vec::new();
for i in 1..n - 1 {
out.push(poly[0]);
out.push(poly[i]);
out.push(poly[i + 1]);
}
out
let Some(normal) = polygon::normal(poly) else {
return Vec::new();
};
// Face normal via Newell's method (robust for near-planar polygons).
let mut normal = [0.0f64; 3];
for i in 0..n {
let a = poly[i];
let b = poly[(i + 1) % n];
normal[0] += (a[1] - b[1]) * (a[2] + b[2]);
normal[1] += (a[2] - b[2]) * (a[0] + b[0]);
normal[2] += (a[0] - b[0]) * (a[1] + b[1]);
}
let nlen = dot(normal, normal).sqrt();
if nlen < 1e-12 {
return fan();
}
let normal = [normal[0] / nlen, normal[1] / nlen, normal[2] / nlen];
// Orthonormal in-plane basis.
let seed = if normal[0].abs() < 0.9 { [1.0, 0.0, 0.0] } else { [0.0, 1.0, 0.0] };
let mut u = cross(seed, normal);
let ul = dot(u, u).sqrt();
if ul < 1e-12 {
return fan();
}
u = [u[0] / ul, u[1] / ul, u[2] / ul];
let v = cross(normal, u);
let p2: Vec<[f64; 2]> = poly.iter().map(|&p| [dot(p, u), dot(p, v)]).collect();
// Signed area → winding (CCW when positive in the (u, v) frame).
let mut area = 0.0;
for i in 0..n {
let a = p2[i];
let b = p2[(i + 1) % n];
area += a[0] * b[1] - b[0] * a[1];
}
let ccw = area > 0.0;
let tri_area2 = |a: [f64; 2], b: [f64; 2], c: [f64; 2]| {
(b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
let Some(plane) = Plane::orthonormal(poly[0], axis.to_array(), normal) else {
return Vec::new();
};
let in_tri = |p: [f64; 2], a: [f64; 2], b: [f64; 2], c: [f64; 2]| {
let d1 = tri_area2(a, b, p);
let d2 = tri_area2(b, c, p);
let d3 = tri_area2(c, a, p);
let neg = d1 < 0.0 || d2 < 0.0 || d3 < 0.0;
let pos = d1 > 0.0 || d2 > 0.0 || d3 > 0.0;
!(neg && pos)
};
let mut idx: Vec<usize> = (0..n).collect();
let mut out: Vec<[f64; 3]> = Vec::with_capacity((n - 2) * 3);
let mut guard = 0usize;
while idx.len() > 3 && guard < n * n {
guard += 1;
let m = idx.len();
let mut clipped = false;
for k in 0..m {
let i0 = idx[(k + m - 1) % m];
let i1 = idx[k];
let i2 = idx[(k + 1) % m];
let (a, b, c) = (p2[i0], p2[i1], p2[i2]);
let convex = if ccw { tri_area2(a, b, c) > 0.0 } else { tri_area2(a, b, c) < 0.0 };
if !convex {
continue;
}
let mut contains = false;
for &j in &idx {
if j == i0 || j == i1 || j == i2 {
continue;
}
if in_tri(p2[j], a, b, c) {
contains = true;
break;
}
}
if contains {
continue;
}
out.push(poly[i0]);
out.push(poly[i1]);
out.push(poly[i2]);
idx.remove(k);
clipped = true;
break;
}
if !clipped {
// No ear found (self-intersecting / numerically degenerate) — bail
// to a fan of the remainder rather than loop forever.
for i in 1..idx.len() - 1 {
out.push(poly[idx[0]]);
out.push(poly[idx[i]]);
out.push(poly[idx[i + 1]]);
}
return out;
}
let projected: Vec<[f64; 2]> = poly
.iter()
.filter_map(|&point| plane.project(point))
.collect();
if projected.len() != poly.len() {
return Vec::new();
}
if idx.len() == 3 {
out.push(poly[idx[0]]);
out.push(poly[idx[1]]);
out.push(poly[idx[2]]);
let (points, triangles) = triangulate(&projected, &[]);
let source: Vec<usize> = points
.iter()
.filter_map(|point| projected.iter().position(|candidate| candidate == point))
.collect();
if source.len() != points.len() {
return Vec::new();
}
out
triangles
.into_iter()
.flat_map(|triangle| triangle.map(|index| poly[source[index]]))
.collect()
}
// ── Face3D ────────────────────────────────────────────────────────────────────
@@ -176,32 +89,7 @@ fn face3d_fill(corners: [[f64; 3]; 4]) -> Vec<[f64; 3]> {
return Vec::new();
}
let Some(normal) = polygon::normal(&ring) else {
return Vec::new();
};
let axis = (KernelVec3::from(ring[1]) - KernelVec3::from(ring[0])).to_array();
let Some(plane) = Plane::orthonormal(ring[0], axis, normal) else {
return Vec::new();
};
let projected: Vec<[f64; 2]> = ring
.iter()
.filter_map(|&point| plane.project(point))
.collect();
if projected.len() != ring.len() {
return Vec::new();
}
let (points, triangles) = triangulate(&projected, &[]);
let source: Vec<usize> = points
.iter()
.filter_map(|point| projected.iter().position(|candidate| candidate == point))
.collect();
if source.len() != points.len() {
return Vec::new();
}
triangles
.into_iter()
.flat_map(|triangle| triangle.map(|index| ring[source[index]]))
.collect()
triangulate_planar(&ring)
}
impl RenderConvertible for Face3D {
@@ -271,6 +159,9 @@ impl Grippable for Face3D {
fn apply_grip(&mut self, grip_id: usize, apply: GripApply) {
let was_triangle = self.is_triangle();
if was_triangle && grip_id == 3 {
return;
}
let corner = match grip_id {
0 => &mut self.first_corner,
1 => &mut self.second_corner,
@@ -278,18 +169,17 @@ impl Grippable for Face3D {
3 => &mut self.fourth_corner,
_ => return,
};
match apply {
GripApply::Translate(d) => {
corner.x += d.x as f64;
corner.y += d.y as f64;
corner.z += d.z as f64;
}
GripApply::Absolute(p) => {
corner.x = p.x as f64;
corner.y = p.y as f64;
corner.z = p.z as f64;
}
let current = dvec3(corner);
let updated = match apply {
GripApply::Translate(delta) => current + delta,
GripApply::Absolute(position) => position,
};
if !updated.is_finite() {
return;
}
corner.x = updated.x;
corner.y = updated.y;
corner.z = updated.z;
if was_triangle && grip_id == 2 {
self.fourth_corner = self.third_corner;
}
@@ -341,18 +231,24 @@ impl PropertyEditable for Face3D {
}
fn apply_geom_prop(&mut self, field: &str, value: &str) {
let invisible = value == t!("Invisible").as_ref();
match field {
"f3_edge1" => self.invisible_edges.set_first_invisible(invisible),
"f3_edge2" => self.invisible_edges.set_second_invisible(invisible),
"f3_edge3" => self.invisible_edges.set_third_invisible(invisible),
"f3_edge4" => self.invisible_edges.set_fourth_invisible(invisible),
_ => {}
}
if field.starts_with("f3_edge") {
let invisible = if value == t!("Invisible").as_ref() {
true
} else if value == t!("Visible").as_ref() {
false
} else {
return;
};
match field {
"f3_edge1" => self.invisible_edges.set_first_invisible(invisible),
"f3_edge2" => self.invisible_edges.set_second_invisible(invisible),
"f3_edge3" => self.invisible_edges.set_third_invisible(invisible),
"f3_edge4" => self.invisible_edges.set_fourth_invisible(invisible),
_ => {}
}
return;
}
let Some(value) = parse_f64(value) else {
let Some(value) = parse_f64(value).filter(|value| value.is_finite()) else {
return;
};
let was_triangle = self.is_triangle();