From 91a2fa17948afce3c2a78366de02d37be39e5f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20S=C3=B6derstr=C3=B6m?= Date: Fri, 3 Jun 2022 18:16:04 +0200 Subject: [PATCH] feat(path): add point segments Adds Point Segment type as an alternative to the Line Segment type. --- src/cuts/path.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/cuts/path.rs b/src/cuts/path.rs index da6ee69..971f4bf 100644 --- a/src/cuts/path.rs +++ b/src/cuts/path.rs @@ -36,6 +36,7 @@ impl Arc { pub enum Segment { Line(Line), Arc(Arc), + Point(Vector2), } impl Segment { @@ -48,6 +49,16 @@ impl Segment { pub fn arc(from: Vector2, to: Vector2, center: Vector2) -> Self { Self::Arc(Arc::new(from, to, center)) } + + #[must_use] + pub fn point(point: Vector2) -> Self { + Self::Point(point) + } + + #[must_use] + pub fn points(points: Vec) -> Vec { + points.into_iter().map(|point| Self::Point(point)).collect() + } } #[derive(Debug, Clone)] @@ -102,6 +113,37 @@ impl Path { bounds.min.y = min_y; } + let min_z = if self.start.z < self.end_z {self.start.z} else {self.end_z}; + if bounds.min.z > min_z { + bounds.min.z = min_z; + } + }, + Segment::Point(point) => { + let max_x = self.start.x + point.x; + if bounds.max.x < max_x { + bounds.max.x = max_x; + } + + let max_y = self.start.y + point.y; + if bounds.max.y < max_y { + bounds.max.y = max_y; + } + + let max_z = if self.start.z > self.end_z {self.start.z} else {self.end_z}; + if bounds.max.z < max_z { + bounds.max.z = max_z; + } + + let min_x = self.start.x + point.x; + if bounds.min.x > min_x { + bounds.min.x = min_x; + } + + let min_y = self.start.y + point.y; + if bounds.min.y > min_y { + bounds.min.y = min_y; + } + let min_z = if self.start.z < self.end_z {self.start.z} else {self.end_z}; if bounds.min.z > min_z { bounds.min.z = min_z; @@ -132,6 +174,11 @@ impl Path { y: line.from.y + self.start.y, z: self.start.z, }, + Segment::Point(point) => Vector3 { + x: point.x + self.start.x, + y: point.y + self.start.y, + z: self.start.z, + }, }; instructions.append(&mut vec![ @@ -169,6 +216,7 @@ impl Path { let end = match segment { Segment::Arc(arc) => arc.to, Segment::Line(line) => line.to, + Segment::Point(point) => *point, }; let distance = last_point.distance_to(end); distances.push(distance); @@ -244,7 +292,7 @@ impl Path { p: None, f: None, })); - } + }, Segment::Line(line) => { instructions.push(Instruction::G1(G1 { x: Some(self.start.x + line.from.x), @@ -259,6 +307,14 @@ impl Path { z: Some(to_z), f: None, })); + }, + Segment::Point(point) => { + instructions.push(Instruction::G1(G1 { + x: Some(self.start.x + point.x), + y: Some(self.start.y + point.y), + z: Some(to_z), + f: None, + })); } }