use super::doc::{id_key, SketchDoc, SketchGeometry, SketchPoint};
use serde_json::{Map, Value};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ChamferSize {
Equal { distance: f64 },
DistanceAngle { distance: f64, angle_degrees: f64 },
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CornerOptions {
pub radius: f64,
pub chamfer_by_angle: bool,
pub chamfer_distance: f64,
pub chamfer_angle_degrees: f64,
}
impl Default for CornerOptions {
fn default() -> Self {
Self { radius: 1.0, chamfer_by_angle: false, chamfer_distance: 1.0, chamfer_angle_degrees: 45.0 }
}
}
impl CornerOptions {
pub fn chamfer_size(&self) -> ChamferSize {
if self.chamfer_by_angle {
ChamferSize::DistanceAngle { distance: self.chamfer_distance, angle_degrees: self.chamfer_angle_degrees }
} else {
ChamferSize::Equal { distance: self.chamfer_distance }
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct CornerEdit {
pub edge: Value,
pub constraints: Vec<Value>,
}
struct Corner {
corner: (f64, f64),
lines: [(Value, Value, (f64, f64)); 2],
}
fn point_xy(doc: &SketchDoc, id: &Value) -> Option<(f64, f64)> {
doc.point(id).map(|p| (p.x, p.y))
}
fn corner_lines(doc: &SketchDoc, corner: &Value) -> Result<Corner, String> {
let key = id_key(corner);
let at = point_xy(doc, corner).ok_or_else(|| "the corner point is not in the sketch".to_string())?;
let touching: Vec<&SketchGeometry> = doc.geometries.iter().filter(|g| g.points.iter().any(|p| id_key(p) == key)).collect();
if touching.len() != 2 {
return Err(format!("no shared corner: {} curves meet at this point (a corner is two)", touching.len()));
}
let mut lines = Vec::new();
for geometry in touching {
if geometry.geom_type != "line" {
return Err(format!("only corners of two lines are supported yet (this one has a {})", geometry.geom_type));
}
if geometry.points.len() != 2 {
return Err("a line of the corner is malformed".to_string());
}
let far = if id_key(&geometry.points[0]) == key { &geometry.points[1] } else { &geometry.points[0] };
if id_key(far) == key {
return Err("a line of the corner has no length".to_string());
}
let far_xy = point_xy(doc, far).ok_or_else(|| "a corner line's endpoint is missing".to_string())?;
if (far_xy.0 - at.0).hypot(far_xy.1 - at.1) <= 1e-9 {
return Err("a line of the corner has no length".to_string());
}
lines.push((geometry.id.clone(), far.clone(), far_xy));
}
let [first, second]: [(Value, Value, (f64, f64)); 2] = lines.try_into().map_err(|_| "no shared corner".to_string())?;
Ok(Corner { corner: at, lines: [first, second] })
}
pub fn corner_near(doc: &SketchDoc, u: f64, v: f64, radius: f64) -> Option<Value> {
doc.points
.iter()
.filter(|p| (p.x - u).hypot(p.y - v) <= radius)
.filter(|p| corner_lines(doc, &p.id).is_ok())
.min_by(|a, b| (a.x - u).hypot(a.y - v).total_cmp(&(b.x - u).hypot(b.y - v)))
.map(|p| p.id.clone())
}
fn add_point(doc: &mut SketchDoc, (x, y): (f64, f64)) -> Value {
let id = doc.next_point_id();
doc.points.push(SketchPoint { id: id.clone(), x, y, fixed: false, construction: false, external_reference: false });
id
}
fn add_geometry(doc: &mut SketchDoc, geom_type: &str, points: Vec<Value>) -> Value {
let id = doc.next_geometry_id();
let mut extra = Map::new();
extra.insert("construction".to_string(), Value::Bool(false));
doc.geometries.push(SketchGeometry { id: id.clone(), geom_type: geom_type.to_string(), points, extra });
id
}
fn add_constraint(doc: &mut SketchDoc, ctype: &str, points: Vec<Value>, display_style: &str) -> Value {
let id = doc.next_constraint_id();
let mut raw = Map::new();
raw.insert("id".to_string(), id.clone());
raw.insert("type".to_string(), Value::String(ctype.to_string()));
raw.insert("points".to_string(), Value::Array(points));
raw.insert("labelX".to_string(), Value::from(0));
raw.insert("labelY".to_string(), Value::from(0));
raw.insert("displayStyle".to_string(), Value::String(display_style.to_string()));
raw.insert("value".to_string(), Value::Null);
raw.insert("valueNeedsSetup".to_string(), Value::Bool(true));
doc.constraints.push(super::doc::SketchConstraint { raw });
id
}
fn retarget_line(doc: &mut SketchDoc, line: &Value, corner: &Value, with: &Value) {
let key = id_key(corner);
if let Some(geometry) = doc.geometry_mut(line) {
for point in geometry.points.iter_mut() {
if id_key(point) == key {
*point = with.clone();
}
}
}
}
fn keep_virtual_sharp(doc: &mut SketchDoc, corner: &Value, on: [(&Value, &Value); 2], constraints: &mut Vec<Value>) {
if let Some(point) = doc.point_mut(corner) {
point.construction = true;
}
for (far, cut) in on {
constraints.push(add_constraint(doc, "⏛", vec![far.clone(), cut.clone(), corner.clone()], ""));
}
}
fn unit((x, y): (f64, f64)) -> (f64, f64) {
let length = x.hypot(y);
(x / length, y / length)
}
fn directions(corner: &Corner) -> Result<([(f64, f64); 2], f64, [f64; 2]), String> {
let (px, py) = corner.corner;
let vectors = corner.lines.each_ref().map(|(_, _, (x, y))| (x - px, y - py));
let lengths = vectors.map(|(x, y)| x.hypot(y));
let units = vectors.map(unit);
let cross = units[0].0 * units[1].1 - units[0].1 * units[1].0;
let dot = units[0].0 * units[1].0 + units[0].1 * units[1].1;
let angle = cross.abs().atan2(dot);
if angle <= 1e-6 || angle >= std::f64::consts::PI - 1e-6 {
return Err("the two lines are parallel: there is no corner to round or cut".to_string());
}
Ok((units, angle, lengths))
}
pub fn fillet_corner(doc: &mut SketchDoc, corner: &Value, radius: f64) -> Result<CornerEdit, String> {
if !(radius.is_finite() && radius > 1e-9) {
return Err("the fillet radius must be positive".to_string());
}
let shape = corner_lines(doc, corner)?;
let (units, angle, lengths) = directions(&shape)?;
let back = radius / (angle / 2.0).tan();
let shorter = lengths[0].min(lengths[1]);
if back >= shorter {
return Err(format!(
"the radius is too large for this corner: it needs {back:.6} along each line, the shorter is {shorter:.6} (largest radius {:.6})",
shorter * (angle / 2.0).tan()
));
}
let (px, py) = shape.corner;
let tangent = units.map(|(x, y)| (px + x * back, py + y * back));
let bisector = unit((units[0].0 + units[1].0, units[0].1 + units[1].1));
let reach = radius / (angle / 2.0).sin();
let centre = (px + bisector.0 * reach, py + bisector.1 * reach);
let [(first, first_far, _), (second, second_far, _)] = shape.lines;
let first_cut = add_point(doc, tangent[0]);
let second_cut = add_point(doc, tangent[1]);
let centre_id = add_point(doc, centre);
retarget_line(doc, &first, corner, &first_cut);
retarget_line(doc, &second, corner, &second_cut);
let turn = (tangent[0].0 - centre.0) * (tangent[1].1 - centre.1) - (tangent[0].1 - centre.1) * (tangent[1].0 - centre.0);
let (start, end) = if turn > 0.0 { (first_cut.clone(), second_cut.clone()) } else { (second_cut.clone(), first_cut.clone()) };
let edge = add_geometry(doc, "arc", vec![centre_id.clone(), start.clone(), end]);
let mut constraints = Vec::new();
constraints.push(add_constraint(doc, "⟂", vec![centre_id.clone(), first_cut.clone(), first_cut.clone(), first_far.clone()], ""));
constraints.push(add_constraint(doc, "⟂", vec![centre_id.clone(), second_cut.clone(), second_cut.clone(), second_far.clone()], ""));
constraints.push(add_constraint(doc, "⟺", vec![centre_id, start], "radius"));
keep_virtual_sharp(doc, corner, [(&first_far, &first_cut), (&second_far, &second_cut)], &mut constraints);
Ok(CornerEdit { edge, constraints })
}
pub fn fillet_corners(doc: &mut SketchDoc, corners: &[Value], radius: f64) -> (Vec<(Value, CornerEdit)>, Vec<(Value, String)>) {
let mut done: Vec<(Value, CornerEdit)> = Vec::new();
let mut refused = Vec::new();
let mut first: Option<(Value, Value)> = None;
for corner in corners {
match fillet_corner(doc, corner, radius) {
Ok(mut edit) => {
let arc = doc.geometries.iter().find(|g| id_key(&g.id) == id_key(&edit.edge)).map(|g| g.points.clone());
if let Some([centre, start, _end]) = arc.as_deref() {
match &first {
None => first = Some((centre.clone(), start.clone())),
Some((first_centre, first_start)) => {
if let Some(radius_id) = edit.constraints.iter().find(|id| {
doc.constraints.iter().any(|c| {
c.raw.get("id").map(id_key) == Some(id_key(id))
&& c.ctype() == Some("⟺")
&& c.raw.get("displayStyle").and_then(Value::as_str) == Some("radius")
})
}) {
let radius_id = radius_id.clone();
doc.constraints.retain(|c| c.raw.get("id").map(id_key) != Some(id_key(&radius_id)));
edit.constraints.retain(|id| id_key(id) != id_key(&radius_id));
}
let equal = add_constraint(
doc,
"⊜",
vec![first_centre.clone(), first_start.clone(), centre.clone(), start.clone()],
"",
);
edit.constraints.push(equal);
}
}
}
done.push((corner.clone(), edit));
}
Err(message) => refused.push((corner.clone(), message)),
}
}
(done, refused)
}
pub fn chamfer_corner(doc: &mut SketchDoc, corner: &Value, size: ChamferSize) -> Result<CornerEdit, String> {
let shape = corner_lines(doc, corner)?;
let (units, angle, lengths) = directions(&shape)?;
let (first_back, second_back) = match size {
ChamferSize::Equal { distance } => {
if !(distance.is_finite() && distance > 1e-9) {
return Err("the chamfer distance must be positive".to_string());
}
(distance, distance)
}
ChamferSize::DistanceAngle { distance, angle_degrees } => {
if !(distance.is_finite() && distance > 1e-9) {
return Err("the chamfer distance must be positive".to_string());
}
let cut = angle_degrees.to_radians();
let far = std::f64::consts::PI - angle - cut;
if !(cut > 1e-6 && far > 1e-6) {
return Err(format!(
"the chamfer angle must lie between 0 and {:.4} degrees at this corner",
(std::f64::consts::PI - angle).to_degrees()
));
}
(distance, distance * cut.sin() / far.sin())
}
};
for (back, length) in [(first_back, lengths[0]), (second_back, lengths[1])] {
if back >= length {
return Err(format!("the chamfer is too large for this corner: it needs {back:.6} along a line {length:.6} long"));
}
}
let (px, py) = shape.corner;
let cuts = [(px + units[0].0 * first_back, py + units[0].1 * first_back), (px + units[1].0 * second_back, py + units[1].1 * second_back)];
let [(first, first_far, _), (second, second_far, _)] = shape.lines;
let first_cut = add_point(doc, cuts[0]);
let second_cut = add_point(doc, cuts[1]);
retarget_line(doc, &first, corner, &first_cut);
retarget_line(doc, &second, corner, &second_cut);
let edge = add_geometry(doc, "line", vec![first_cut.clone(), second_cut.clone()]);
let mut constraints = Vec::new();
constraints.push(add_constraint(doc, "⟺", vec![corner.clone(), first_cut.clone()], ""));
match size {
ChamferSize::Equal { .. } => {
constraints.push(add_constraint(doc, "⇌", vec![corner.clone(), first_cut.clone(), corner.clone(), second_cut.clone()], ""));
}
ChamferSize::DistanceAngle { .. } => {
constraints.push(add_constraint(doc, "∠", vec![first_cut.clone(), corner.clone(), first_cut.clone(), second_cut.clone()], ""));
}
}
keep_virtual_sharp(doc, corner, [(&first_far, &first_cut), (&second_far, &second_cut)], &mut constraints);
Ok(CornerEdit { edge, constraints })
}