Skip to content

Commit

Permalink
Use math::mip instead of map::distance when precision is important
Browse files Browse the repository at this point in the history
  • Loading branch information
patowen committed May 1, 2024
1 parent 7c2bd56 commit c38184c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
7 changes: 4 additions & 3 deletions client/src/graphics/voxels/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ impl Voxels {
);
histogram!("frame.cpu.voxels.graph_traversal").record(graph_traversal_started.elapsed());
// Sort nodes by distance to the view to prioritize loading closer data and improve early Z
// performance
// performance. Sorting by `mip` in descending order is equivalent to sorting by distance
// in ascending order.
let view_pos = view.local * math::origin();
nodes.sort_unstable_by(|&(_, ref xf_a), &(_, ref xf_b)| {
math::distance(&view_pos, &(xf_a * math::origin()))
.partial_cmp(&math::distance(&view_pos, &(xf_b * math::origin())))
math::mip(&view_pos, &(xf_b * math::origin()))
.partial_cmp(&math::mip(&view_pos, &(xf_a * math::origin())))
.unwrap_or(std::cmp::Ordering::Less)
});
let node_scan_started = Instant::now();
Expand Down
27 changes: 25 additions & 2 deletions common/src/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn ensure_nearby(graph: &mut Graph, start: &Position, distance: f32) {
visited.insert(neighbor);
let neighbor_transform = current_transform * side.reflection();
let neighbor_p = neighbor_transform * math::origin();
if math::distance(&start_p, &neighbor_p) > distance {
if -math::mip(&start_p, &neighbor_p) > distance.cosh() {
continue;
}
pending.push_back((neighbor, neighbor_transform));
Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn nearby_nodes(

while let Some(current) = pending.pop_front() {
let current_p = current.transform * math::origin();
if math::distance(&start_p, &current_p) > distance {
if -math::mip(&start_p, &current_p) > distance.cosh() {
continue;
}
result.push((current.id, na::convert(current.transform)));
Expand Down Expand Up @@ -220,3 +220,26 @@ impl<'a> RayTraverser<'a> {
}
}
}

#[cfg(test)]
mod tests {
use approx::assert_abs_diff_eq;

use super::*;

// Make sure that ensure_nearby and nearby_nodes finish even for a relatively large radius
// and traverse the expected number of nodes
#[test]
fn traversal_functions_example() {
let mut graph = Graph::new(1);
ensure_nearby(&mut graph, &Position::origin(), 6.0);
assert_abs_diff_eq!(graph.len(), 502079, epsilon = 50);

// TODO: nearby_nodes has a stricter interpretation of distance than
// ensure_nearby, resulting in far fewer nodes. Getting these two
// functions to align may be a future performance improvement
// opportunity.
let nodes = nearby_nodes(&graph, &Position::origin(), 6.0);
assert_abs_diff_eq!(nodes.len(), 60137, epsilon = 5);
}
}

0 comments on commit c38184c

Please sign in to comment.