Skip to content

Commit

Permalink
Clean up neighbourhood tools
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Aug 10, 2024
1 parent 05a01ee commit 05c38a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
62 changes: 26 additions & 36 deletions core/common/src/maths/neighbours.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
use super::*;

/// Provides a Von Neumann neighbourhood expansion for points in 2-space.
pub trait VonNeumannNeighbourhood: Sized {
/// Computes the Von Neumann neighbourhood of a point.
fn von_neighbours(&self) -> [Self; 4];
/// Provides a neighbourhood of adjacent adn diagonal points.
pub trait Neighbourhood: Sized {
fn adjacent_neighbours(&self) -> [Self; 4];
fn diagonal_neighbours(&self) -> [Self; 8];
}

impl VonNeumannNeighbourhood for Vec2 {
fn von_neighbours(&self) -> [Self; 4] {
impl Neighbourhood for Vec2 {
fn adjacent_neighbours(&self) -> [Self; 4] {
[
vec2(self.x - 1., self.y), // left
vec2(self.x, self.y + 1.), // top
vec2(self.x + 1., self.y), // right
vec2(self.x, self.y - 1.), // bottom
]
}
}

impl VonNeumannNeighbourhood for IVec2 {
fn von_neighbours(&self) -> [Self; 4] {
[
ivec2(self.x - 1, self.y), // left
ivec2(self.x, self.y + 1), // top
ivec2(self.x + 1, self.y), // right
ivec2(self.x, self.y - 1), // bottom
]
}
}

/// Provides a Moore neighbourhood expansion for points in 2-space.
pub trait MooreNeighbourhood: Sized {
/// Calculates the Moore neighbourhood of a point in 2-space.
fn moore_neighbours(&self) -> [Self; 8];
}

impl MooreNeighbourhood for Vec2 {
fn moore_neighbours(&self) -> [Self; 8] {
fn diagonal_neighbours(&self) -> [Self; 8] {
[
vec2(self.x - 1., self.y - 1.), // bottom left
vec2(self.x - 1., self.y), // left
Expand All @@ -49,8 +30,17 @@ impl MooreNeighbourhood for Vec2 {
}
}

impl MooreNeighbourhood for IVec2 {
fn moore_neighbours(&self) -> [Self; 8] {
impl Neighbourhood for IVec2 {
fn adjacent_neighbours(&self) -> [Self; 4] {
[
ivec2(self.x - 1, self.y), // left
ivec2(self.x, self.y + 1), // top
ivec2(self.x + 1, self.y), // right
ivec2(self.x, self.y - 1), // bottom
]
}

fn diagonal_neighbours(&self) -> [Self; 8] {
[
ivec2(self.x - 1, self.y - 1), // bottom left
ivec2(self.x - 1, self.y), // left
Expand All @@ -69,10 +59,10 @@ mod tests {
use super::*;

#[test]
fn test_von_neumann_neighbours_for_vec2() {
fn test_adjacent_neighbours_for_vec2() {
let point = vec2(0.0, 0.0);

let neighbours = point.von_neighbours();
let neighbours = point.adjacent_neighbours();

assert_eq!(neighbours[0], vec2(-1.0, 0.0)); // left
assert_eq!(neighbours[1], vec2(0.0, 1.0)); // top
Expand All @@ -81,10 +71,10 @@ mod tests {
}

#[test]
fn test_von_neumann_neighbours_for_ivec2() {
fn test_adjacent_neighbours_for_ivec2() {
let point = ivec2(0, 0);

let neighbours = point.von_neighbours();
let neighbours = point.adjacent_neighbours();

assert_eq!(neighbours[0], ivec2(-1, 0)); // left
assert_eq!(neighbours[1], ivec2(0, 1)); // top
Expand All @@ -93,10 +83,10 @@ mod tests {
}

#[test]
fn test_moore_neighbours_for_vec2() {
fn test_diagonal_neighbours_for_vec2() {
let point = vec2(0.0, 0.0);

let neighbours = point.moore_neighbours();
let neighbours = point.diagonal_neighbours();

assert_eq!(neighbours[0], vec2(-1.0, -1.0)); // bottom left
assert_eq!(neighbours[1], vec2(-1.0, 0.0)); // left
Expand All @@ -109,10 +99,10 @@ mod tests {
}

#[test]
fn test_moore_neighbours_for_ivec2() {
fn test_diagonal_neighbours_for_ivec2() {
let point = ivec2(0, 0);

let neighbours = point.moore_neighbours();
let neighbours = point.diagonal_neighbours();

assert_eq!(neighbours[0], ivec2(-1, -1)); // bottom left
assert_eq!(neighbours[1], ivec2(-1, 0)); // left
Expand Down
10 changes: 4 additions & 6 deletions core/common/src/maths/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,10 @@ mod tests {

impl PathFindingGrid for DenseGrid<bool> {
fn get_neighbours(&self, center: IVec2, results: &mut NeighbourList<IVec2>) {
for neighbour in center.von_neighbours() {
if self.is_valid(neighbour.x, neighbour.y) {
unsafe {
if *self.get_unchecked(neighbour.x, neighbour.y) {
results.push(neighbour);
}
unsafe {
for neighbour in center.adjacent_neighbours() {
if self.is_valid(neighbour.x, neighbour.y) && *self.get_unchecked(neighbour.x, neighbour.y) {
results.push(neighbour);
}
}
}
Expand Down

0 comments on commit 05c38a5

Please sign in to comment.