Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaled layers #1

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub(crate) struct SearchInstance<'m> {
pub(crate) debug: bool,
#[cfg(debug_assertions)]
pub(crate) fail_fast: i32,
pub(crate) min_layer_cost: f32,
}

pub(crate) enum InstanceStep {
Expand Down Expand Up @@ -153,6 +154,7 @@ impl<'m> SearchInstance<'m> {
debug: false,
#[cfg(debug_assertions)]
fail_fast: -1,
min_layer_cost: 1.0,
};
search_instance.root_history.insert(Root(from.0), 0.0);

Expand Down Expand Up @@ -318,8 +320,10 @@ impl<'m> SearchInstance<'m> {
#[cfg(feature = "detailed-layers")]
length: {
let a = path_with_layers.iter().fold((0.0, self.from), |acc, p| {
let scale = self.mesh.layers[acc.1 .1 as usize].scale;
let to_point = (acc.1 .0 * scale).distance(p.0 * scale);
let layer = &self.mesh.layers[acc.1 .1 as usize];
let scale = layer.scale;
let cost = layer.cost;
let to_point = (acc.1 .0 * scale).distance(p.0 * scale) * cost;
(acc.0 + to_point, *p)
});
a.0
Expand Down Expand Up @@ -562,32 +566,36 @@ impl<'m> SearchInstance<'m> {
}
#[cfg(feature = "detailed-layers")]
{
let layer = &self.mesh.layers[node.polygon_to.layer() as usize];
new_f += node
.root
.distance(root * self.mesh.layers[node.polygon_to.layer() as usize].scale);
.distance(root * layer.scale) * layer.cost;
}
}
#[cfg(feature = "detailed-layers")]
if other_side.layer() != node.polygon_to.layer() {
path_with_layers.push((start.0, end.0, other_side.layer()));
}

let heuristic_to_end: f32;
let mut heuristic_to_end: f32;
#[cfg(not(feature = "detailed-layers"))]
{
heuristic_to_end = heuristic(root, self.to, (start.0, end.0));
}
#[cfg(feature = "detailed-layers")]
{
let start_layer = &self.mesh.layers[start.1.layer() as usize];
let end_layer = &self.mesh.layers[end.1.layer() as usize];
heuristic_to_end = heuristic(
root,
self.to,
(
start.0 * self.mesh.layers[start.1.layer() as usize].scale,
end.0 * self.mesh.layers[end.1.layer() as usize].scale,
start.0 * start_layer.scale,
end.0 * end_layer.scale,
),
);
}
heuristic_to_end *= self.min_layer_cost;
if new_f.is_nan() || heuristic_to_end.is_nan() {
#[cfg(debug_assertions)]
if self.debug {
Expand Down
3 changes: 3 additions & 0 deletions src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct Layer {
pub scale: Vec2,
pub(crate) baked_polygons: Option<BVH2d>,
pub(crate) islands: Option<Vec<usize>>,
/// Cost coefficient of the layer
pub cost: f32,
}

impl Default for Layer {
Expand All @@ -37,6 +39,7 @@ impl Default for Layer {
scale: Vec2::ONE,
baked_polygons: None,
islands: None,
cost: 1.0,
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ impl Mesh {
debug: false,
#[cfg(debug_assertions)]
fail_fast: -1,
min_layer_cost: 1.0,
};
search_instance.successors(node);
search_instance.queue.drain().collect()
Expand All @@ -415,6 +416,16 @@ impl Mesh {
use hashbrown::HashMap;
use std::collections::BinaryHeap;

let min_layer_cost: f32;
#[cfg(feature="detailed-layers")]
{
min_layer_cost = self.layers.map(|l| l.cost).min();
}
#[cfg(not(feature = "detailed-layers"))]
{
min_layer_cost = 1.0;
}

let search_instance = SearchInstance {
#[cfg(feature = "stats")]
start: Instant::now(),
Expand All @@ -441,6 +452,7 @@ impl Mesh {
debug: false,
#[cfg(debug_assertions)]
fail_fast: -1,
min_layer_cost,
};
search_instance.edges_between(node).to_vec()
}
Expand Down