From 643f35a39bb0bae2e8435fafcd5312a4046bb82e Mon Sep 17 00:00:00 2001 From: Matt Ferraro Date: Wed, 29 May 2024 21:33:38 -0400 Subject: [PATCH] patch linestring_to_wire to re-use actual vertex --- packages/cadmium/src/solid/helpers.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/cadmium/src/solid/helpers.rs b/packages/cadmium/src/solid/helpers.rs index a740403b..0a613f2e 100644 --- a/packages/cadmium/src/solid/helpers.rs +++ b/packages/cadmium/src/solid/helpers.rs @@ -4,17 +4,25 @@ use std::rc::Rc; use geo::LineString; use truck_modeling::{builder, Edge, Vertex, Wire}; -use crate::isketch::ISketch; use super::prelude::*; +use crate::isketch::ISketch; -pub fn geopoint_to_truckpoint(point: geo::Point, sketch: Rc>) -> Result { +pub fn geopoint_to_truckpoint( + point: geo::Point, + sketch: Rc>, +) -> Result { let sketch_ref = sketch.borrow(); - let sketch_point = sketch_ref.find_point_ref(point.x(), point.y()).ok_or(anyhow::anyhow!("geo::Point not found in sketch"))?; + let sketch_point = sketch_ref + .find_point_ref(point.x(), point.y()) + .ok_or(anyhow::anyhow!("geo::Point not found in sketch"))?; let point_3d = sketch_ref.get_point_3d(sketch_point)?.1; Ok(point_3d.into()) } -pub fn linestring_to_wire(line: &LineString, sketch: Rc>) -> Result { +pub fn linestring_to_wire( + line: &LineString, + sketch: Rc>, +) -> Result { let mut vertices: Vec = Vec::new(); for point in line.points() { let vertex = builder::vertex(geopoint_to_truckpoint(point, sketch.clone())?); @@ -22,10 +30,13 @@ pub fn linestring_to_wire(line: &LineString, sketch: Rc>) -> Re } let mut edges: Vec = Vec::new(); - for i in 0..vertices.len() - 1 { + for i in 0..vertices.len() - 2 { let edge = builder::line(&vertices[i], &vertices[i + 1]); edges.push(edge); } + // Close the loop by connecting the last vertex to the first + let last_edge = builder::line(&vertices[vertices.len() - 2], &vertices[0]); + edges.push(last_edge); Ok(Wire::from_iter(edges.into_iter())) }