Bugfix: arc paths were stored as straight line paths (#4310)

Problem 1 of https://github.com/KittyCAD/modeling-app/issues/4297
This commit is contained in:
Adam Chalmers
2024-10-25 17:49:30 -05:00
committed by GitHub
parent 4925251c29
commit 39d76ed54f
7 changed files with 9375 additions and 9 deletions

View File

@ -1656,6 +1656,15 @@ pub enum Path {
#[serde(flatten)]
base: BasePath,
},
/// A circular arc, not necessarily tangential to the current point.
Arc {
#[serde(flatten)]
base: BasePath,
/// Center of the circle that this arc is drawn on.
center: [f64; 2],
/// Radius of the circle that this arc is drawn on.
radius: f64,
},
}
/// What kind of path is this?
@ -1668,10 +1677,11 @@ enum PathType {
Circle,
Horizontal,
AngledLineTo,
Arc,
}
impl From<Path> for PathType {
fn from(value: Path) -> Self {
impl From<&Path> for PathType {
fn from(value: &Path) -> Self {
match value {
Path::ToPoint { .. } => Self::ToPoint,
Path::TangentialArcTo { .. } => Self::TangentialArcTo,
@ -1680,6 +1690,7 @@ impl From<Path> for PathType {
Path::Horizontal { .. } => Self::Horizontal,
Path::AngledLineTo { .. } => Self::AngledLineTo,
Path::Base { .. } => Self::Base,
Path::Arc { .. } => Self::Arc,
}
}
}
@ -1694,6 +1705,7 @@ impl Path {
Path::TangentialArcTo { base, .. } => base.geo_meta.id,
Path::TangentialArc { base, .. } => base.geo_meta.id,
Path::Circle { base, .. } => base.geo_meta.id,
Path::Arc { base, .. } => base.geo_meta.id,
}
}
@ -1706,6 +1718,7 @@ impl Path {
Path::TangentialArcTo { base, .. } => base.tag.clone(),
Path::TangentialArc { base, .. } => base.tag.clone(),
Path::Circle { base, .. } => base.tag.clone(),
Path::Arc { base, .. } => base.tag.clone(),
}
}
@ -1718,6 +1731,7 @@ impl Path {
Path::TangentialArcTo { base, .. } => base,
Path::TangentialArc { base, .. } => base,
Path::Circle { base, .. } => base,
Path::Arc { base, .. } => base,
}
}
@ -1732,8 +1746,33 @@ impl Path {
/// Length of this path segment, in cartesian plane.
pub fn length(&self) -> f64 {
// TODO 4297: check what type of line this path is, don't assume linear.
((self.get_from()[1] - self.get_to()[1]).powi(2) + (self.get_from()[0] - self.get_to()[0]).powi(2)).sqrt()
match self {
Self::ToPoint { .. } | Self::Base { .. } | Self::Horizontal { .. } | Self::AngledLineTo { .. } => {
linear_distance(self.get_from(), self.get_to())
}
Self::TangentialArc {
base: _,
center,
ccw: _,
}
| Self::TangentialArcTo {
base: _,
center,
ccw: _,
} => {
// The radius can be calculated as the linear distance between `to` and `center`,
// or between `from` and `center`. They should be the same.
let radius = linear_distance(self.get_from(), center);
debug_assert_eq!(radius, linear_distance(self.get_to(), center));
// TODO: Call engine utils to figure this out.
linear_distance(self.get_from(), self.get_to())
}
Self::Circle { radius, .. } => 2.0 * std::f64::consts::PI * radius,
Self::Arc { .. } => {
// TODO: Call engine utils to figure this out.
linear_distance(self.get_from(), self.get_to())
}
}
}
pub fn get_base_mut(&mut self) -> Option<&mut BasePath> {
@ -1745,10 +1784,22 @@ impl Path {
Path::TangentialArcTo { base, .. } => Some(base),
Path::TangentialArc { base, .. } => Some(base),
Path::Circle { base, .. } => Some(base),
Path::Arc { base, .. } => Some(base),
}
}
}
/// Compute the straight-line distance between a pair of (2D) points.
#[rustfmt::skip]
fn linear_distance(
[x0, y0]: &[f64; 2],
[x1, y1]: &[f64; 2]
) -> f64 {
let y_sq = (y1 - y0).powi(2);
let x_sq = (x1 - x0).powi(2);
(y_sq + x_sq).sqrt()
}
/// An extrude surface.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]

View File

@ -149,7 +149,7 @@ pub(crate) async fn do_post_extrude(
}
let edge_id = sketch.paths.iter().find_map(|segment| match segment {
Path::ToPoint { base } | Path::Circle { base, .. } => Some(base.geo_meta.id),
Path::ToPoint { base } | Path::Circle { base, .. } | Path::Arc { base, .. } => Some(base.geo_meta.id),
_ => None,
});
@ -234,7 +234,10 @@ pub(crate) async fn do_post_extrude(
.flat_map(|path| {
if let Some(Some(actual_face_id)) = face_id_map.get(&path.get_base().geo_meta.id) {
match path {
Path::TangentialArc { .. } | Path::TangentialArcTo { .. } | Path::Circle { .. } => {
Path::Arc { .. }
| Path::TangentialArc { .. }
| Path::TangentialArcTo { .. }
| Path::Circle { .. } => {
let extrude_surface = ExtrudeSurface::ExtrudeArc(crate::executor::ExtrudeArc {
face_id: *actual_face_id,
tag: path.get_base().tag.clone(),

View File

@ -1530,7 +1530,7 @@ pub(crate) async fn inner_arc(
)
.await?;
let current_path = Path::ToPoint {
let current_path = Path::Arc {
base: BasePath {
from: from.into(),
to: end.into(),
@ -1540,6 +1540,8 @@ pub(crate) async fn inner_arc(
metadata: args.source_range.into(),
},
},
center: center.into(),
radius,
};
let mut new_sketch = sketch.clone();

View File

@ -21,7 +21,7 @@ pub fn normalize(angle: Angle) -> Angle {
Angle::from_degrees(if result > 180.0 { result - 360.0 } else { result })
}
/// Gives the ▲-angle between from and to angles (shortest path), use radians.
/// Gives the ▲-angle between from and to angles (shortest path)
///
/// Sign of the returned angle denotes direction, positive means counterClockwise 🔄
/// # Examples