Skip to content

Commit

Permalink
patch linestring_to_wire to re-use actual vertex
Browse files Browse the repository at this point in the history
  • Loading branch information
MattFerraro committed May 30, 2024
1 parent 2beccda commit 643f35a
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/cadmium/src/solid/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,39 @@ 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<f64>, sketch: Rc<RefCell<ISketch>>) -> Result<TruckPoint3, anyhow::Error> {
pub fn geopoint_to_truckpoint(
point: geo::Point<f64>,
sketch: Rc<RefCell<ISketch>>,
) -> Result<TruckPoint3, anyhow::Error> {
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<RefCell<ISketch>>) -> Result<Wire, anyhow::Error> {
pub fn linestring_to_wire(
line: &LineString,
sketch: Rc<RefCell<ISketch>>,
) -> Result<Wire, anyhow::Error> {
let mut vertices: Vec<Vertex> = Vec::new();
for point in line.points() {
let vertex = builder::vertex(geopoint_to_truckpoint(point, sketch.clone())?);
vertices.push(vertex);
}

let mut edges: Vec<Edge> = 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()))
}
Expand Down

0 comments on commit 643f35a

Please sign in to comment.