Skip to content

Commit

Permalink
feature to limit impact
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Jul 23, 2024
1 parent ecc1cbd commit 183a62b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ stats = []
verbose = []
async = []
no-default-baking = []
detailed-layers = []
serde = ["glam/serde", "bvh2d/serde", "dep:serde"]

[dependencies]
Expand Down
1 change: 1 addition & 0 deletions src/async_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl<'m> Future for FuturePath<'m> {
return Poll::Ready(Some(Path {
length: self.from.distance(self.to),
path: vec![self.to],
#[cfg(feature = "detailed-layers")]
path_with_layers: vec![(self.to, ending_polygon.layer())],
}));
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tracing::instrument;

use crate::instance::EdgeSide;

const EPSILON: f32 = 1e-4;
pub(crate) const EPSILON: f32 = 1e-4;

pub(crate) trait Vec2Helper {
fn side(self, edge: (Vec2, Vec2)) -> EdgeSide;
Expand Down
78 changes: 45 additions & 33 deletions src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bvh2d::EPSILON;
use smallvec::SmallVec;
#[cfg(feature = "tracing")]
use tracing::instrument;
Expand All @@ -11,6 +10,8 @@ use std::time::Instant;
use glam::Vec2;
use hashbrown::{hash_map::Entry, HashMap};

#[cfg(feature = "detailed-layers")]
use crate::helpers::EPSILON;
use crate::{
helpers::{heuristic, line_intersect_segment, turning_point, Vec2Helper},
Mesh, Path, SearchNode, PRECISION,
Expand Down Expand Up @@ -61,6 +62,7 @@ pub(crate) struct SearchInstance<'m> {
pub(crate) queue: BinaryHeap<SearchNode>,
pub(crate) node_buffer: Vec<SearchNode>,
pub(crate) root_history: HashMap<Root, f32>,
#[cfg(feature = "detailed-layers")]
pub(crate) from: Vec2,
pub(crate) to: Vec2,
pub(crate) polygon_to: u32,
Expand Down Expand Up @@ -130,6 +132,7 @@ impl<'m> SearchInstance<'m> {
queue: BinaryHeap::with_capacity(15),
node_buffer: Vec::with_capacity(10),
root_history: HashMap::with_capacity(10),
#[cfg(feature = "detailed-layers")]
from: from.0,
to: to.0,
polygon_to: to.1,
Expand All @@ -156,6 +159,7 @@ impl<'m> SearchInstance<'m> {

let empty_node = SearchNode {
path: vec![],
#[cfg(feature = "detailed-layers")]
path_with_layers: vec![],
root: from.0,
interval: (Vec2::new(0.0, 0.0), Vec2::new(0.0, 0.0)),
Expand Down Expand Up @@ -268,45 +272,49 @@ impl<'m> SearchInstance<'m> {
path.push(self.to);
path_with_layers_end.push((self.to, next.polygon_to.layer()));
}

let mut path_with_layers = vec![];
let mut from = self.from;
for (index, potential_point) in next.path_with_layers.iter().enumerate() {
if potential_point.0 == potential_point.1 {
from = potential_point.0;
path_with_layers.push((potential_point.0, potential_point.2));
} else {
// look for next fixed point to find the intersection
let to = next
.path_with_layers
.iter()
.skip(index + 1)
.find(|point| point.0 == point.1)
.map(|point| point.0)
.unwrap_or(path_with_layers_end[0].0);
if let Some(intersection) = line_intersect_segment(
(from, to),
(potential_point.0, potential_point.1),
) {
from = intersection;
path_with_layers.push((intersection, potential_point.2));
#[cfg(feature = "detailed-layers")]
let path_with_layers = {
let mut path_with_layers = vec![];
let mut from = self.from;
for (index, potential_point) in next.path_with_layers.iter().enumerate() {
if potential_point.0 == potential_point.1 {
from = potential_point.0;
path_with_layers.push((potential_point.0, potential_point.2));
} else {
// look for next fixed point to find the intersection
let to = next
.path_with_layers
.iter()
.skip(index + 1)
.find(|point| point.0 == point.1)
.map(|point| point.0)
.unwrap_or(path_with_layers_end[0].0);
if let Some(intersection) = line_intersect_segment(
(from, to),
(potential_point.0, potential_point.1),
) {
from = intersection;
path_with_layers.push((intersection, potential_point.2));
}
}
}
}
path_with_layers.extend(path_with_layers_end);
let mut path_with_layers_peekable = path_with_layers.iter().peekable();
let mut path_with_layers = vec![];
while let Some(p) = path_with_layers_peekable.next() {
if let Some(n) = path_with_layers_peekable.peek() {
if p.0.distance_squared(n.0) < EPSILON {
continue;
path_with_layers.extend(path_with_layers_end);
let mut path_with_layers_peekable = path_with_layers.iter().peekable();
let mut path_with_layers = vec![];
while let Some(p) = path_with_layers_peekable.next() {
if let Some(n) = path_with_layers_peekable.peek() {
if p.0.distance_squared(n.0) < EPSILON {
continue;
}
}
path_with_layers.push(*p);
}
path_with_layers.push(*p);
}
path_with_layers
};
return InstanceStep::Found(Path {
path,
length: next.f + next.g,
#[cfg(feature = "detailed-layers")]
path_with_layers,
});
}
Expand Down Expand Up @@ -508,11 +516,14 @@ impl<'m> SearchInstance<'m> {
}

let mut path = node.path.clone();
#[cfg(feature = "detailed-layers")]
let mut path_with_layers = node.path_with_layers.clone();
if root != node.root {
path.push(root);
#[cfg(feature = "detailed-layers")]
path_with_layers.push((root, root, node.polygon_to.layer()));
}
#[cfg(feature = "detailed-layers")]
if other_side.layer() != node.polygon_to.layer() {
path_with_layers.push((start.0, end.0, other_side.layer()));
}
Expand All @@ -530,6 +541,7 @@ impl<'m> SearchInstance<'m> {

let new_node = SearchNode {
path,
#[cfg(feature = "detailed-layers")]
path_with_layers,
root,
interval: (start.0, end.0),
Expand Down
23 changes: 18 additions & 5 deletions src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,11 @@ impl Layer {
mod tests {
use std::collections::HashSet;

#[cfg(feature = "detailed-layers")]
use crate::helpers::line_intersect_segment;
use crate::{instance::U32Layer, Coords, Layer, Mesh, Path, Polygon, SearchNode, Vertex};
use glam::{vec2, IVec2, Vec2};

use crate::{
helpers::line_intersect_segment, instance::U32Layer, Coords, Layer, Mesh, Path, Polygon,
SearchNode, Vertex,
};

fn mesh_u_grid() -> Mesh {
let main_layer = Layer {
vertices: vec![
Expand Down Expand Up @@ -299,6 +297,7 @@ mod tests {
let to = vec2(1.1, 0.1);
let search_node = SearchNode {
path: vec![],
#[cfg(feature = "detailed-layers")]
path_with_layers: vec![],
root: from,
interval: (vec2(0.0, 1.0), vec2(1.0, 1.0)),
Expand All @@ -316,6 +315,7 @@ mod tests {
Path {
path: vec![to],
length: from.distance(to),
#[cfg(feature = "detailed-layers")]
path_with_layers: vec![(to, 0)],
}
);
Expand All @@ -329,6 +329,7 @@ mod tests {
let to = vec2(2.1, 1.9);
let search_node = SearchNode {
path: vec![],
#[cfg(feature = "detailed-layers")]
path_with_layers: vec![],
root: from,
interval: (vec2(0.0, 1.0), vec2(1.0, 1.0)),
Expand Down Expand Up @@ -360,6 +361,7 @@ mod tests {
length: from.distance(vec2(1.0, 1.0))
+ vec2(1.0, 1.0).distance(vec2(2.0, 1.0))
+ vec2(2.0, 1.0).distance(to),
#[cfg(feature = "detailed-layers")]
path_with_layers: vec![(vec2(1.0, 1.0), 0), (vec2(2.0, 1.0), 2), (to, 2)],
}
);
Expand Down Expand Up @@ -435,6 +437,7 @@ mod tests {
blocked.insert(1);
let path = dbg!(mesh.path_on_layers(from, to, blocked).unwrap());
assert_eq!(path.path, vec![vec2(2.0, 2.0), vec2(3.0, 1.0), to]);
#[cfg(feature = "detailed-layers")]
assert_eq!(
path.path_with_layers,
vec![(vec2(2.0, 2.0), 0), (vec2(3.0, 1.0), 0), (to, 0)]
Expand All @@ -450,6 +453,7 @@ mod tests {
let to = vec2(5.0 - i as f32 / 10.0, 0.9);
let path = dbg!(mesh.path(from, to).unwrap());
assert_eq!(path.path, vec![to]);
#[cfg(feature = "detailed-layers")]
assert_eq!(
reduce_path_precision(path.path_with_layers),
reduce_path_precision(vec![
Expand Down Expand Up @@ -477,6 +481,7 @@ mod tests {
let to = vec2(i as f32 / 10.0, 2.1);
let path = dbg!(mesh.path(from, to).unwrap());
assert_eq!(path.path, vec![to]);
#[cfg(feature = "detailed-layers")]
assert_eq!(
reduce_path_precision(path.path_with_layers),
reduce_path_precision(vec![
Expand Down Expand Up @@ -506,20 +511,23 @@ mod tests {
match i {
7 => {
assert_eq!(path.path, vec![vec2(1.0, 2.0), to]);
#[cfg(feature = "detailed-layers")]
assert_eq!(
path.path_with_layers,
vec![(vec2(1.0, 2.0), 1), (vec2(4.0, 1.0), 0), (to, 0)]
);
}
_ if i < 11 => {
assert_eq!(path.path, vec![vec2(1.0, 2.0), vec2(4.0, 1.0), to]);
#[cfg(feature = "detailed-layers")]
assert_eq!(
path.path_with_layers,
vec![(vec2(1.0, 2.0), 1), (vec2(4.0, 1.0), 0), (to, 0)]
);
}
_ if i < 15 => {
assert_eq!(path.path, vec![vec2(2.0, 2.0), vec2(3.0, 1.0), to]);
#[cfg(feature = "detailed-layers")]
assert_eq!(
path.path_with_layers,
vec![(vec2(2.0, 2.0), 0), (vec2(3.0, 1.0), 0), (to, 0)]
Expand All @@ -540,20 +548,23 @@ mod tests {
match i {
7 => {
assert_eq!(path.path, vec![vec2(4.0, 1.0), to]);
#[cfg(feature = "detailed-layers")]
assert_eq!(
path.path_with_layers,
vec![(vec2(4.0, 1.0), 1), (vec2(0.9999997, 2.0), 0), (to, 0)]
);
}
_ if i < 11 => {
assert_eq!(path.path, vec![vec2(4.0, 1.0), vec2(1.0, 2.0), to]);
#[cfg(feature = "detailed-layers")]
assert_eq!(
path.path_with_layers,
vec![(vec2(4.0, 1.0), 1), (vec2(1.0, 2.0), 0), (to, 0)]
);
}
_ if i < 15 => {
assert_eq!(path.path, vec![vec2(3.0, 1.0), vec2(2.0, 2.0), to]);
#[cfg(feature = "detailed-layers")]
assert_eq!(
path.path_with_layers,
vec![(vec2(3.0, 1.0), 0), (vec2(2.0, 2.0), 0), (to, 0)]
Expand Down Expand Up @@ -583,6 +594,7 @@ mod tests {
path.path,
vec![vec2(3.0, 1.0,), vec2(4.0, 1.0,), vec2(2.5, 1.5,),],
);
#[cfg(feature = "detailed-layers")]
assert_eq!(
path.path_with_layers,
vec![
Expand All @@ -608,6 +620,7 @@ mod tests {
path_back.path,
vec![vec2(4.0, 1.0,), vec2(3.0, 1.0,), vec2(2.5, 1.5,),],
);
#[cfg(feature = "detailed-layers")]
assert_eq!(
path_back.path_with_layers,
vec![
Expand Down
Loading

0 comments on commit 183a62b

Please sign in to comment.