Skip to content

Commit

Permalink
Tags should refer to full paths, not just base paths.
Browse files Browse the repository at this point in the history
See "Problem 2" in #4297

This is a pure refactor, it should not change any behaviour at all.
It adds more information into the tag system, but nothing reads that
extra information yet. It will be used to address problem 3 of the above
issue.
  • Loading branch information
adamchalmers committed Oct 25, 2024
1 parent 32b7dda commit 775b966
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
45 changes: 43 additions & 2 deletions src/wasm-lib/kcl/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ pub struct TagEngineInfo {
/// The sketch the tag is on.
pub sketch: uuid::Uuid,
/// The path the tag is on.
pub path: Option<BasePath>,
pub path: Option<Path>,
/// The surface information for the tag.
pub surface: Option<ExtrudeSurface>,
}
Expand Down Expand Up @@ -1206,7 +1206,7 @@ impl Sketch {
tag_identifier.info = Some(TagEngineInfo {
id: base.geo_meta.id,
sketch: self.id,
path: Some(base.clone()),
path: Some(current_path.clone()),
surface: None,
});

Expand Down Expand Up @@ -1658,6 +1658,32 @@ pub enum Path {
},
}

/// What kind of path is this?
#[derive(Display)]
enum PathType {
ToPoint,
Base,
TangentialArc,
TangentialArcTo,
Circle,
Horizontal,
AngledLineTo,
}

impl From<Path> for PathType {
fn from(value: Path) -> Self {
match value {
Path::ToPoint { .. } => Self::ToPoint,
Path::TangentialArcTo { .. } => Self::TangentialArcTo,
Path::TangentialArc { .. } => Self::TangentialArc,
Path::Circle { .. } => Self::Circle,
Path::Horizontal { .. } => Self::Horizontal,
Path::AngledLineTo { .. } => Self::AngledLineTo,
Path::Base { .. } => Self::Base,
}
}
}

impl Path {
pub fn get_id(&self) -> uuid::Uuid {
match self {
Expand Down Expand Up @@ -1695,6 +1721,21 @@ impl Path {
}
}

/// Where does this path segment start?
pub fn get_from(&self) -> &[f64; 2] {
&self.get_base().from
}
/// Where does this path segment end?
pub fn get_to(&self) -> &[f64; 2] {
&self.get_base().to
}

/// 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()
}

pub fn get_base_mut(&mut self) -> Option<&mut BasePath> {
match self {
Path::ToPoint { base } => Some(base),
Expand Down
12 changes: 6 additions & 6 deletions src/wasm-lib/kcl/src/std/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn inner_segment_end_x(tag: &TagIdentifier, exec_state: &mut ExecState, args: Ar
})
})?;

Ok(path.to[0])
Ok(path.get_base().to[0])
}

/// Returns the segment end of y.
Expand Down Expand Up @@ -79,7 +79,7 @@ fn inner_segment_end_y(tag: &TagIdentifier, exec_state: &mut ExecState, args: Ar
})
})?;

Ok(path.to[1])
Ok(path.get_to()[1])
}

/// Returns the last segment of x.
Expand Down Expand Up @@ -202,7 +202,7 @@ fn inner_segment_length(tag: &TagIdentifier, exec_state: &mut ExecState, args: A
})
})?;

let result = ((path.from[1] - path.to[1]).powi(2) + (path.from[0] - path.to[0]).powi(2)).sqrt();
let result = path.length();

Ok(result)
}
Expand Down Expand Up @@ -242,7 +242,7 @@ fn inner_segment_angle(tag: &TagIdentifier, exec_state: &mut ExecState, args: Ar
})
})?;

let result = between(path.from.into(), path.to.into());
let result = between(path.get_from().into(), path.get_to().into());

Ok(result.to_degrees())
}
Expand Down Expand Up @@ -286,7 +286,7 @@ fn inner_angle_to_match_length_x(
})
})?;

let length = ((path.from[1] - path.to[1]).powi(2) + (path.from[0] - path.to[0]).powi(2)).sqrt();
let length = path.length();

let last_line = sketch
.paths
Expand Down Expand Up @@ -350,7 +350,7 @@ fn inner_angle_to_match_length_y(
})
})?;

let length = ((path.from[1] - path.to[1]).powi(2) + (path.from[0] - path.to[0]).powi(2)).sqrt();
let length = path.length();

let last_line = sketch
.paths
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/kcl/src/std/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ async fn inner_angled_line_that_intersects(

let from = sketch.current_pen_position()?;
let to = intersection_with_parallel_line(
&[path.from.into(), path.to.into()],
&[path.get_from().into(), path.get_to().into()],
data.offset.unwrap_or_default(),
data.angle,
from,
Expand Down Expand Up @@ -1244,7 +1244,9 @@ pub(crate) async fn inner_start_profile_at(
tag_identifier.info = Some(TagEngineInfo {
id: current_path.geo_meta.id,
sketch: path_id,
path: Some(current_path.clone()),
path: Some(Path::Base {
base: current_path.clone(),
}),
surface: None,
});
HashMap::from([(tag.name.to_string(), tag_identifier)])
Expand Down

0 comments on commit 775b966

Please sign in to comment.