Skip to content

Commit

Permalink
Merge pull request #5 from hakolao/fix-stackoverflow-in-bfs
Browse files Browse the repository at this point in the history
Fix stackoverflow in dfs
  • Loading branch information
hakolao authored Jan 19, 2022
2 parents 34cf48c + 46c022b commit 25fdee5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 43 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: cargo clippy
run: cargo clippy --all-targets -- -D warnings

windows_stable:
windows_nightly:
runs-on: windows-latest
steps:
- name: Ninja Install
Expand All @@ -41,26 +41,38 @@ jobs:
with:
args: install python3 --params "/InstallAllUsers"
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- name: Build
run: cargo build --verbose --release
- name: Run tests
run: cargo test --verbose --release

linux_stable:
linux_nightly:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- name: init
run: sudo apt-get install build-essential git python cmake libvulkan-dev vulkan-utils libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev
- name: Build
run: cargo build --verbose --release
- name: Run tests
run: cargo test --verbose --release

macos_stable:
macos_nightly:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- name: Build
run: cargo build --verbose --release
- name: Run tests
Expand Down
1 change: 1 addition & 0 deletions sandbox/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(map_first_last)]
#![allow(
clippy::needless_question_mark,
clippy::too_many_arguments,
Expand Down
85 changes: 45 additions & 40 deletions sandbox/src/object/deformation_utils.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,59 @@
use std::collections::BTreeSet;

use cgmath::Vector2;

fn depth_first_label_mark(
/// Performs a depth first search of connected pixels and labels them with current label.
/// Tracks the connected pixel mins & maxes for bitmap formation purposes later.
fn mark_connected_pixels_depth_first(
bitmap: &[f64],
labels: &mut Vec<u32>,
width: u32,
height: u32,
x: i32,
y: i32,
x_in: i32,
y_in: i32,
current_label: u32,
min_x: &mut i32,
min_y: &mut i32,
max_x: &mut i32,
max_y: &mut i32,
) {
if x < 0 || x == width as i32 || y < 0 || y == height as i32 {
return;
};
let index = (y * width as i32 + x) as usize;
if labels[index] != 0 || bitmap[index] == 0.0 {
return;
}
labels[index] = current_label;
// Find maxes
*min_x = (*min_x).min(x);
*min_y = (*min_y).min(y);
*max_x = (*max_x).max(x);
*max_y = (*max_y).max(y);
for (neigh_x, neigh_y) in &[
(x - 1, y - 1),
(x, y - 1),
(x + 1, y - 1),
(x + 1, y),
(x + 1, y + 1),
(x, y + 1),
(x - 1, y + 1),
(x - 1, y),
] {
depth_first_label_mark(
bitmap,
labels,
width,
height,
*neigh_x,
*neigh_y,
current_label,
min_x,
min_y,
max_x,
max_y,
);
let mut to_visit = BTreeSet::new();
to_visit.insert((x_in, y_in));
while !to_visit.is_empty() {
// Get current pixel
let (x, y) = to_visit.pop_first().unwrap();
// Track min maxes
*min_x = (*min_x).min(x);
*min_y = (*min_y).min(y);
*max_x = (*max_x).max(x);
*max_y = (*max_y).max(y);
// Label it
let index = (y * width as i32 + x) as usize;
labels[index] = current_label;
// Add neighbors for labeling & inspection if necessary
for &(neigh_x, neigh_y) in &[
(x - 1, y - 1),
(x, y - 1),
(x + 1, y - 1),
(x + 1, y),
(x + 1, y + 1),
(x, y + 1),
(x - 1, y + 1),
(x - 1, y),
] {
// The pixel should be labeled and is within bounds. (It wasn't labeled yet, and object isn't empty there)
if neigh_x >= 0
&& neigh_x < width as i32
&& neigh_y >= 0
&& neigh_y < height as i32
&& !to_visit.contains(&(neigh_x, neigh_y))
{
let neigh_index = (neigh_y * width as i32 + neigh_x) as usize;
if labels[neigh_index] == 0 && bitmap[neigh_index] != 0.0 {
to_visit.insert((neigh_x, neigh_y));
}
};
}
}
}

Expand All @@ -72,7 +77,7 @@ pub fn extract_connected_components_from_bitmap(
let index = (y * width + x) as usize;
if labels[index] == 0 && bitmap[index] == 1.0 {
current_label += 1;
depth_first_label_mark(
mark_connected_pixels_depth_first(
bitmap,
&mut labels,
width,
Expand Down

0 comments on commit 25fdee5

Please sign in to comment.