Skip to content

Commit

Permalink
expose march_edges function as... is it called a static function in r…
Browse files Browse the repository at this point in the history
…ust?
  • Loading branch information
shnewto committed Mar 4, 2024
1 parent 0cfda38 commit faee752
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Edges {
processed.push(b);
}

self.march_edges(&processed, rows, cols, translate)
Edges::march_edges(&processed, rows, cols, translate)
}

/// Marching squares adjacent, walks all the pixels in the provided data and keeps track of
Expand All @@ -63,7 +63,6 @@ impl Edges {
/// Accepts a flag indicating whether or not to translate coordinates to either side of (0,0)
/// or leave it all in positive x,y
pub fn march_edges(
&self,
data: &[usize],
rows: usize,
cols: usize,
Expand All @@ -72,22 +71,22 @@ impl Edges {
let mut edge_points: Vec<Vec2> = vec![];

for d in 0..data.len() {
let (x, y) = self.get_xy(d, rows);
let (x, y) = Edges::get_xy(d, rows);
let (c, r) = (x as isize, y as isize);

if self.get_at(r, c, rows, cols, data) == 0 {
if Edges::get_at(r, c, rows, cols, data) == 0 {
continue;
}

let neighbors = [
self.get_at(r + 1, c, rows, cols, data),
self.get_at(r - 1, c, rows, cols, data),
self.get_at(r, c + 1, rows, cols, data),
self.get_at(r, c - 1, rows, cols, data),
self.get_at(r + 1, c + 1, rows, cols, data),
self.get_at(r - 1, c - 1, rows, cols, data),
self.get_at(r + 1, c - 1, rows, cols, data),
self.get_at(r - 1, c + 1, rows, cols, data),
Edges::get_at(r + 1, c, rows, cols, data),
Edges::get_at(r - 1, c, rows, cols, data),
Edges::get_at(r, c + 1, rows, cols, data),
Edges::get_at(r, c - 1, rows, cols, data),
Edges::get_at(r + 1, c + 1, rows, cols, data),
Edges::get_at(r - 1, c - 1, rows, cols, data),
Edges::get_at(r + 1, c - 1, rows, cols, data),
Edges::get_at(r - 1, c + 1, rows, cols, data),
];

let n: usize = neighbors.iter().sum();
Expand All @@ -97,15 +96,14 @@ impl Edges {
}
}

self.points_to_drawing_order(&edge_points, translate, rows, cols)
Edges::points_to_drawing_order(&edge_points, translate, rows, cols)
}

/// Takes a collection of coordinates and attempts to sort them according to drawing order
///
/// Pixel sorted so that the distance to previous and next is 1. When there is no pixel left
/// with distance 1, another group is created and sorted the same way.
fn points_to_drawing_order(
&self,
points: &[Vec2],
translate: bool,
rows: usize,
Expand All @@ -124,7 +122,7 @@ impl Edges {
let neighbor = edge_points
.iter()
.enumerate()
.find(|(_, p)| self.distance(prev, **p) == 1.0);
.find(|(_, p)| Edges::distance(prev, **p) == 1.0);

if let Some((i, _)) = neighbor {
let next = edge_points.remove(i);
Expand All @@ -145,28 +143,28 @@ impl Edges {
if translate {
groups = groups
.into_iter()
.map(|p| self.translate_vec(p, rows, cols))
.map(|p| Edges::translate_vec(p, rows, cols))
.collect();
}

groups
}

/// conceptual helper, access a 1D vector like it's a 2D vector
fn get_xy(&self, idx: usize, offset: usize) -> (f32, f32) {
fn get_xy(idx: usize, offset: usize) -> (f32, f32) {
let quot = idx / offset;
let rem = idx % offset;
(quot as f32, rem as f32)
}

/// pythagoras, distance between two points
fn distance(&self, a: Vec2, b: Vec2) -> f32 {
fn distance(a: Vec2, b: Vec2) -> f32 {
// d=√((x2-x1)²+(y2-y1)²)
((a.x - b.x).abs().powi(2) + (a.y - b.y).abs().powi(2)).sqrt()
}

/// get zero or non-zero pixel the value at given coordinate
fn get_at(&self, row: isize, col: isize, rows: usize, cols: usize, data: &[usize]) -> usize {
fn get_at(row: isize, col: isize, rows: usize, cols: usize, data: &[usize]) -> usize {
if row < 0 || col < 0 || row >= rows as isize || col >= cols as isize {
0
} else {
Expand All @@ -178,17 +176,17 @@ impl Edges {
}

/// translate point in positive x,y to either side of (0,0)
fn xy_translate(&self, p: Vec2, rows: usize, cols: usize) -> Vec2 {
fn xy_translate(p: Vec2, rows: usize, cols: usize) -> Vec2 {
Vec2::new(
p.x - (cols as f32 / 2. - 1.0),
-p.y + (rows as f32 / 2. - 1.0),
)
}

/// Translate vector of points in positive x,y to either side of (0,0)
pub fn translate_vec(&self, v: Vec<Vec2>, rows: usize, cols: usize) -> Vec<Vec2> {
pub fn translate_vec(v: Vec<Vec2>, rows: usize, cols: usize) -> Vec<Vec2> {
v.into_iter()
.map(|p| self.xy_translate(p, rows, cols))
.map(|p| Edges::xy_translate(p, rows, cols))
.collect()
}

Expand Down Expand Up @@ -261,7 +259,7 @@ impl fmt::Debug for Edges {
}

#[cfg(feature = "bevy")]
mod test {
mod tests {
#[allow(unused_imports)]
use super::Edges;
#[allow(unused_imports)]
Expand Down

0 comments on commit faee752

Please sign in to comment.