From 24646755fdca5df631a944a63245b36b4562953e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sat, 20 Jul 2024 18:24:52 +0200 Subject: [PATCH] fix paths on overlapping layers --- src/instance.rs | 8 ++++---- src/layers.rs | 18 ++++++++++++++---- src/lib.rs | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/instance.rs b/src/instance.rs index 1a560a4..7ebb172 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -155,9 +155,9 @@ impl<'m> SearchInstance<'m> { root: from.0, interval: (Vec2::new(0.0, 0.0), Vec2::new(0.0, 0.0)), edge: (0, 0), - polygon_from: u32::MAX, + polygon_from: from.1, polygon_to: from.1, - previous_polygon_layer: to.1.layer(), + previous_polygon_layer: from.1.layer(), f: 0.0, g: 0.0, }; @@ -620,7 +620,7 @@ impl<'m> SearchInstance<'m> { } continue; } - let vertex = self.mesh.layers[node.polygon_to.layer() as usize] + let vertex = self.mesh.layers[node.previous_polygon_layer as usize] .vertices .get(node.edge.0 as usize) .unwrap(); @@ -645,7 +645,7 @@ impl<'m> SearchInstance<'m> { } continue; } - let vertex = self.mesh.layers[node.polygon_to.layer() as usize] + let vertex = self.mesh.layers[node.previous_polygon_layer as usize] .vertices .get(node.edge.1 as usize) .unwrap(); diff --git a/src/layers.rs b/src/layers.rs index 8139473..80dc5ee 100644 --- a/src/layers.rs +++ b/src/layers.rs @@ -436,24 +436,34 @@ mod tests { } #[test] - fn take_long_way() { + fn shortcut_with_corner() { let mesh = mesh_overlapping_layers(); for i in 7..15 { let from = Vec2::new(i as f32 / 10.0, 2.1); let to = Vec2::new(5.0 - i as f32 / 10.0, 0.9); let path = dbg!(mesh.path(from, to).unwrap()); - assert_eq!(path.path, vec![vec2(2.0, 2.0), vec2(3.0, 1.0), to]); + match i { + 7 => assert_eq!(path.path, vec![vec2(1.0, 2.0), to]), + _ if i < 11 => assert_eq!(path.path, vec![vec2(1.0, 2.0), vec2(4.0, 1.0), to]), + _ if i < 15 => assert_eq!(path.path, vec![vec2(2.0, 2.0), vec2(3.0, 1.0), to]), + _ => unreachable!(), + } } } #[test] - fn take_long_way_back() { + fn shortcut_with_corner_back() { let mesh = mesh_overlapping_layers(); for i in 7..15 { let from = Vec2::new(5.0 - i as f32 / 10.0, 0.9); let to = Vec2::new(i as f32 / 10.0, 2.1); let path = dbg!(mesh.path(from, to).unwrap()); - assert_eq!(path.path, vec![vec2(3.0, 1.0), vec2(2.0, 2.0), to]); + match i { + 7 => assert_eq!(path.path, vec![vec2(4.0, 1.0), to]), + _ if i < 11 => assert_eq!(path.path, vec![vec2(4.0, 1.0), vec2(1.0, 2.0), to]), + _ if i < 15 => assert_eq!(path.path, vec![vec2(3.0, 1.0), vec2(2.0, 2.0), to]), + _ => unreachable!(), + } } } diff --git a/src/lib.rs b/src/lib.rs index 50331a0..f296a99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -285,7 +285,7 @@ impl Mesh { queue: BinaryHeap::new(), node_buffer: Vec::new(), root_history: HashMap::new(), - from: Vec2::ZERO, + from: node.root, to, polygon_to: self.get_point_location(to), mesh: self,