Skip to content

Commit

Permalink
Make find_nonzero_neighbor operate on owned tensors directly
Browse files Browse the repository at this point in the history
Given how frequently it is used, there is a small gain from being able to pass
`&mask` instead of `&mask.view()`.
  • Loading branch information
robertknight committed Jan 27, 2024
1 parent c471a6c commit f9827c7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions rten-imageproc/src/contours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ enum Direction {
///
/// If `skip_first` is true, start the search from the next neighbor of `start`
/// in the order given by `dir`.
fn find_nonzero_neighbor<T: Default + std::cmp::PartialEq>(
mask: &NdTensorView<T, 2>,
fn find_nonzero_neighbor<
T: Default + std::cmp::PartialEq,
// Use a generic for `mask` rather than `NdTensorView` to avoid the (small)
// overhead of repeated view creation.
M: std::ops::Index<[usize; 2], Output = T>,
>(
mask: &M,
center: Point,
start: Point,
dir: Direction,
Expand Down Expand Up @@ -164,7 +169,7 @@ pub fn find_contours(mask: NdTensorView<i32, 2>, mode: RetrievalMode) -> Polygon
border.clear();

let nonzero_start_neighbor = find_nonzero_neighbor(
&mask.view(),
&mask,
start_point,
start_neighbor,
Direction::Clockwise,
Expand All @@ -177,7 +182,7 @@ pub fn find_contours(mask: NdTensorView<i32, 2>, mode: RetrievalMode) -> Polygon

loop {
let next_point = find_nonzero_neighbor(
&mask.view(),
&mask,
current_point,
prev_neighbor,
Direction::CounterClockwise,
Expand Down

0 comments on commit f9827c7

Please sign in to comment.