forked from 1b-agent/opencadstudio-upstream
feat: ellipse arc endpoint snap and LWPolyline arc midpoint grips
Partial ellipse arcs now populate key_vertices with both endpoints so Endpoint snap works correctly. LWPolyline arc segments (bulge ≠ 0) get a diamond grip at the arc midpoint; dragging it recomputes the segment bulge via circumcircle of start/mid/end, giving interactive arc editing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
67022589df
commit
6fd740e38f
2 files changed
+114
-5
No files matched your search
@@ -96,11 +96,17 @@ fn to_truck(ell: &Ellipse) -> TruckEntity {
|
||||
let v_start = builder::vertex(*ctrl_pts.first().unwrap());
|
||||
let v_end = builder::vertex(*ctrl_pts.last().unwrap());
|
||||
let edge = Edge::new(&v_start, &v_end, Curve::BSplineCurve(bspline));
|
||||
let pt_start = ctrl_pts.first().unwrap();
|
||||
let pt_end = ctrl_pts.last().unwrap();
|
||||
let key_vertices = vec![
|
||||
[pt_start.x as f32, pt_start.y as f32, pt_start.z as f32],
|
||||
[pt_end.x as f32, pt_end.y as f32, pt_end.z as f32],
|
||||
];
|
||||
TruckEntity {
|
||||
object: TruckObject::Curve(edge),
|
||||
snap_pts: vec![(center_v3, SnapHint::Center)],
|
||||
tangent_geoms: vec![],
|
||||
key_vertices: vec![],
|
||||
key_vertices,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+107
-4
@@ -3,7 +3,7 @@ use glam::Vec3;
|
||||
use truck_modeling::{builder, Edge, Point3, Wire};
|
||||
|
||||
use crate::command::EntityTransform;
|
||||
use crate::entities::common::{edit_prop as edit, parse_f64, ro_prop as ro, square_grip};
|
||||
use crate::entities::common::{diamond_grip, edit_prop as edit, parse_f64, ro_prop as ro, square_grip};
|
||||
use crate::entities::traits::{Grippable, PropertyEditable, Transformable, TruckConvertible};
|
||||
use crate::scene::acad_to_truck::{TruckEntity, TruckObject};
|
||||
use crate::scene::object::{GripApply, GripDef, PropSection};
|
||||
@@ -11,6 +11,59 @@ use crate::scene::wire_model::TangentGeom;
|
||||
|
||||
const TAU: f64 = std::f64::consts::TAU;
|
||||
|
||||
/// Midpoint position on an arc segment defined by its bulge.
|
||||
fn arc_midpoint(p0: [f64; 2], p1: [f64; 2], bulge: f64) -> [f64; 2] {
|
||||
let angle = 4.0 * bulge.atan();
|
||||
let dx = p1[0] - p0[0];
|
||||
let dy = p1[1] - p0[1];
|
||||
let d = (dx * dx + dy * dy).sqrt();
|
||||
if d < 1e-12 {
|
||||
return [(p0[0] + p1[0]) * 0.5, (p0[1] + p1[1]) * 0.5];
|
||||
}
|
||||
let r = (d / 2.0) / (angle / 2.0).sin().abs();
|
||||
let mx = (p0[0] + p1[0]) * 0.5;
|
||||
let my = (p0[1] + p1[1]) * 0.5;
|
||||
let px = -dy / d;
|
||||
let py = dx / d;
|
||||
let sign = if bulge > 0.0 { 1.0_f64 } else { -1.0_f64 };
|
||||
let h = r - (r * r - d * d / 4.0).max(0.0).sqrt();
|
||||
let cx = mx - sign * px * (r - h);
|
||||
let cy = my - sign * py * (r - h);
|
||||
let a0 = (p0[1] - cy).atan2(p0[0] - cx);
|
||||
let a1 = (p1[1] - cy).atan2(p1[0] - cx);
|
||||
let (sa, mut ea) = if bulge > 0.0 { (a0, a1) } else { (a1, a0) };
|
||||
if ea < sa { ea += TAU; }
|
||||
let mid_a = sa + (ea - sa) * 0.5;
|
||||
[cx + r * mid_a.cos(), cy + r * mid_a.sin()]
|
||||
}
|
||||
|
||||
/// Compute the DXF bulge for an arc that passes through p0, mid_pt, and p1.
|
||||
/// Returns None when the three points are collinear (straight segment).
|
||||
fn bulge_from_midpoint(p0: [f64; 2], p1: [f64; 2], mid: [f64; 2]) -> Option<f64> {
|
||||
// Circumcircle of (p0, mid, p1)
|
||||
let ax = 2.0 * (mid[0] - p0[0]);
|
||||
let ay = 2.0 * (mid[1] - p0[1]);
|
||||
let bx = 2.0 * (p1[0] - p0[0]);
|
||||
let by = 2.0 * (p1[1] - p0[1]);
|
||||
let ca = mid[0] * mid[0] + mid[1] * mid[1] - p0[0] * p0[0] - p0[1] * p0[1];
|
||||
let cb = p1[0] * p1[0] + p1[1] * p1[1] - p0[0] * p0[0] - p0[1] * p0[1];
|
||||
let det = ax * by - ay * bx;
|
||||
if det.abs() < 1e-12 {
|
||||
return None; // collinear
|
||||
}
|
||||
let cx = (ca * by - cb * ay) / det;
|
||||
let cy = (ax * cb - bx * ca) / det;
|
||||
let a0 = (p0[1] - cy).atan2(p0[0] - cx);
|
||||
let a1 = (p1[1] - cy).atan2(p1[0] - cx);
|
||||
// Determine arc direction: cross product (p1-p0) × (mid-p0)
|
||||
let cross = (p1[0] - p0[0]) * (mid[1] - p0[1]) - (p1[1] - p0[1]) * (mid[0] - p0[0]);
|
||||
let (sa, mut ea) = if cross > 0.0 { (a0, a1) } else { (a1, a0) };
|
||||
if ea < sa { ea += TAU; }
|
||||
let span = ea - sa; // central angle in (0, TAU]
|
||||
let bulge = (span / 4.0).tan();
|
||||
Some(if cross >= 0.0 { bulge } else { -bulge })
|
||||
}
|
||||
|
||||
fn to_truck(pline: &LwPolyline) -> TruckEntity {
|
||||
let verts = &pline.vertices;
|
||||
if verts.is_empty() {
|
||||
@@ -96,12 +149,31 @@ fn to_truck(pline: &LwPolyline) -> TruckEntity {
|
||||
|
||||
fn grips(pline: &LwPolyline) -> Vec<GripDef> {
|
||||
let elev = pline.elevation as f32;
|
||||
pline
|
||||
let n = pline.vertices.len();
|
||||
let seg_count = if pline.is_closed { n } else { n.saturating_sub(1) };
|
||||
|
||||
let mut out: Vec<GripDef> = pline
|
||||
.vertices
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, v)| square_grip(i, Vec3::new(v.location.x as f32, v.location.y as f32, elev)))
|
||||
.collect()
|
||||
.collect();
|
||||
|
||||
// Diamond midpoint grip for each arc segment
|
||||
for i in 0..seg_count {
|
||||
let v0 = &pline.vertices[i];
|
||||
if v0.bulge.abs() < 1e-9 {
|
||||
continue;
|
||||
}
|
||||
let v1 = &pline.vertices[(i + 1) % n];
|
||||
let mid = arc_midpoint(
|
||||
[v0.location.x, v0.location.y],
|
||||
[v1.location.x, v1.location.y],
|
||||
v0.bulge,
|
||||
);
|
||||
out.push(diamond_grip(n + i, Vec3::new(mid[0] as f32, mid[1] as f32, elev)));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn properties(pline: &LwPolyline) -> PropSection {
|
||||
@@ -129,7 +201,10 @@ fn apply_geom_prop(pline: &mut LwPolyline, field: &str, value: &str) {
|
||||
}
|
||||
|
||||
fn apply_grip(pline: &mut LwPolyline, grip_id: usize, apply: GripApply) {
|
||||
if let Some(v) = pline.vertices.get_mut(grip_id) {
|
||||
let n = pline.vertices.len();
|
||||
if grip_id < n {
|
||||
// Vertex position grip
|
||||
let v = &mut pline.vertices[grip_id];
|
||||
match apply {
|
||||
GripApply::Absolute(p) => {
|
||||
v.location.x = p.x as f64;
|
||||
@@ -140,6 +215,34 @@ fn apply_grip(pline: &mut LwPolyline, grip_id: usize, apply: GripApply) {
|
||||
v.location.y += d.y as f64;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Arc midpoint grip for segment (grip_id - n)
|
||||
let seg = grip_id - n;
|
||||
let count = if pline.is_closed { n } else { n.saturating_sub(1) };
|
||||
if seg >= count {
|
||||
return;
|
||||
}
|
||||
let new_mid: [f64; 2] = match apply {
|
||||
GripApply::Absolute(p) => [p.x as f64, p.y as f64],
|
||||
GripApply::Translate(d) => {
|
||||
let v0 = &pline.vertices[seg];
|
||||
let v1 = &pline.vertices[(seg + 1) % n];
|
||||
let old = arc_midpoint(
|
||||
[v0.location.x, v0.location.y],
|
||||
[v1.location.x, v1.location.y],
|
||||
v0.bulge,
|
||||
);
|
||||
[old[0] + d.x as f64, old[1] + d.y as f64]
|
||||
}
|
||||
};
|
||||
let p0 = [pline.vertices[seg].location.x, pline.vertices[seg].location.y];
|
||||
let p1 = [
|
||||
pline.vertices[(seg + 1) % n].location.x,
|
||||
pline.vertices[(seg + 1) % n].location.y,
|
||||
];
|
||||
if let Some(new_bulge) = bulge_from_midpoint(p0, p1, new_mid) {
|
||||
pline.vertices[seg].bulge = new_bulge.clamp(-1e6, 1e6);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user