From b8adfec81722dee0ac72e42e72fd5f520d44c542 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Wed, 4 Dec 2024 04:45:39 +0100 Subject: [PATCH] fix(geometry): fix bend joint order and correct tangent clockwiseness Now bends should cease to seemingly randomly invert their order. --- src/geometry/geometry.rs | 67 ++++++++++++++++++++++++++++++---------- src/math.rs | 2 +- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/geometry/geometry.rs b/src/geometry/geometry.rs index 0142c44..95af85e 100644 --- a/src/geometry/geometry.rs +++ b/src/geometry/geometry.rs @@ -6,7 +6,7 @@ use geo::Point; use petgraph::{ stable_graph::{NodeIndex, StableDiGraph}, visit::EdgeRef, - Direction::Incoming, + Direction::{self, Incoming}, }; use crate::{ @@ -489,22 +489,6 @@ impl< .map(|ni| self.primitive_weight(ni).retag(ni)) } - pub fn seg_joints(&self, seg: SI) -> (DI, DI) { - let v: Vec<_> = self.joineds(seg.into()).collect(); - ( - v[0].try_into().unwrap_or_else(|_| unreachable!()), - v[1].try_into().unwrap_or_else(|_| unreachable!()), - ) - } - - pub fn bend_joints(&self, bend: BI) -> (DI, DI) { - let v: Vec<_> = self.joineds(bend.into()).collect(); - ( - v[0].try_into().unwrap_or_else(|_| unreachable!()), - v[1].try_into().unwrap_or_else(|_| unreachable!()), - ) - } - pub fn joined_segs(&self, dot: DI) -> impl Iterator + '_ { self.joineds(dot.into()).filter_map(|ni| ni.try_into().ok()) } @@ -513,6 +497,55 @@ impl< self.joineds(dot.into()).filter_map(|ni| ni.try_into().ok()) } + fn joints(&self, node: PI) -> (DI, DI) { + ( + self.graph + .neighbors_directed(node.petgraph_index(), Direction::Incoming) + .find(move |ni| { + matches!( + self.graph + .edge_weight( + self.graph + .find_edge_undirected(node.petgraph_index(), *ni) + .unwrap() + .0, + ) + .unwrap(), + GeometryLabel::Joined + ) + }) + .map(|ni| self.primitive_weight(ni).retag(ni)) + .map(|pi| pi.try_into().unwrap_or_else(|_| unreachable!())) + .unwrap(), + self.graph + .neighbors_directed(node.petgraph_index(), Direction::Outgoing) + .find(move |ni| { + matches!( + self.graph + .edge_weight( + self.graph + .find_edge_undirected(node.petgraph_index(), *ni) + .unwrap() + .0, + ) + .unwrap(), + GeometryLabel::Joined + ) + }) + .map(|ni| self.primitive_weight(ni).retag(ni)) + .map(|pi| pi.try_into().unwrap_or_else(|_| unreachable!())) + .unwrap(), + ) + } + + pub fn seg_joints(&self, seg: SI) -> (DI, DI) { + self.joints(seg.into()) + } + + pub fn bend_joints(&self, bend: BI) -> (DI, DI) { + self.joints(bend.into()) + } + pub fn compound_members(&self, compound: GenericIndex) -> impl Iterator + '_ { self.graph .neighbors_directed(compound.petgraph_index(), Incoming) diff --git a/src/math.rs b/src/math.rs index 5bac7a4..b712350 100644 --- a/src/math.rs +++ b/src/math.rs @@ -147,7 +147,7 @@ pub fn tangent_segments( let cross2 = seq_cross_product(tangent_point_pair.0, tangent_point_pair.1, circle2.pos); - if (cw2 && cross2 <= 0.0) || (!cw2 && cross2 >= 0.0) { + if (cw2 && cross2 >= 0.0) || (!cw2 && cross2 <= 0.0) { return None; } }