From b66995b30c550bc3b2e95c6ece0be2e2b8ccd032 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Thu, 8 Feb 2024 18:11:34 +0000 Subject: [PATCH] shape,layout: restore and fix bend-seg collision detection --- src/layout/geometry/shape.rs | 6 +++--- src/layout/layout.rs | 9 +++++++++ src/main.rs | 4 ++-- src/math.rs | 28 ++++++++++++++++++++++------ 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/layout/geometry/shape.rs b/src/layout/geometry/shape.rs index c9dbc79..7bfc7de 100644 --- a/src/layout/geometry/shape.rs +++ b/src/layout/geometry/shape.rs @@ -120,8 +120,8 @@ impl ShapeTrait for SegShape { match other { Shape::Dot(..) => unreachable!(), Shape::Seg(other) => self.polygon().intersects(&other.polygon()), - Shape::Bend(_other) => { - /*for segment in self.polygon().exterior().lines() { + Shape::Bend(other) => { + for segment in self.polygon().exterior().lines() { let inner_circle = other.inner_circle(); let outer_circle = other.outer_circle(); @@ -136,7 +136,7 @@ impl ShapeTrait for SegShape { return true; } } - }*/ + } false } diff --git a/src/layout/layout.rs b/src/layout/layout.rs index 002f1ff..f23ff93 100644 --- a/src/layout/layout.rs +++ b/src/layout/layout.rs @@ -24,6 +24,7 @@ use crate::layout::geometry::{ SegWeightTrait, }; use crate::layout::guide::Guide; +use crate::layout::primitive::GetLimbs; use crate::layout::rules::{Conditions, GetConditions}; use crate::layout::{ bend::{FixedBendIndex, LooseBendIndex, LooseBendWeight}, @@ -852,6 +853,14 @@ impl Layout { let old_pos = self.geometry_with_rtree.geometry().dot_weight(dot).pos(); self.geometry_with_rtree.move_dot(dot, to); + for limb in dot.primitive(self).limbs() { + if let Some(infringement) = self.detect_infringement_except(limb.into(), infringables) { + // Restore original state. + self.geometry_with_rtree.move_dot(dot, old_pos); + return Err(infringement); + } + } + if let Some(infringement) = self.detect_infringement_except(dot.into(), infringables) { // Restore original state. self.geometry_with_rtree.move_dot(dot, old_pos); diff --git a/src/main.rs b/src/main.rs index 07c6838..44a0445 100644 --- a/src/main.rs +++ b/src/main.rs @@ -523,8 +523,8 @@ fn main() -> Result<(), anyhow::Error> { dot_start, dot_end, 3.0, - &mut EmptyRouterObserver, - //&mut DebugRouterObserver::new(&mut event_pump, &window, &mut renderer, &font_context), + //&mut EmptyRouterObserver, + &mut DebugRouterObserver::new(&mut event_pump, &window, &mut renderer, &font_context), )?; render_times( diff --git a/src/math.rs b/src/math.rs index 76431d9..b62134d 100644 --- a/src/math.rs +++ b/src/math.rs @@ -191,14 +191,30 @@ pub fn intersect_circle_segment(circle: &Circle, segment: &Line) -> Vec { } if discriminant == 0.0 { - return [from + (to - from) * -b / (2.0 * a)].into(); + let u = -b / (2.0 * a); + + if u >= 0.0 && u <= 1.0 { + return [from + (to - from) * -b / (2.0 * a)].into(); + } else { + return [].into(); + } } - [ - from + (to - from) * (-b + discriminant.sqrt()) / (2.0 * a), - from + (to - from) * (-b - discriminant.sqrt()) / (2.0 * a), - ] - .into() + let mut v = vec![]; + + let u1 = (-b + discriminant.sqrt()) / (2.0 * a); + + if u1 >= 0.0 && u1 <= 1.0 { + v.push(from + (to - from) * u1); + } + + let u2 = (-b - discriminant.sqrt()) / (2.0 * a); + + if u2 >= 0.0 && u2 <= 1.0 { + v.push(from + (to - from) * u2); + } + + v } pub fn between_vectors(p: Point, from: Point, to: Point) -> bool {