use super::pyramid::GrayImage;
const CIRCLE: [(i32, i32); 16] = [
(0, -3),
(1, -3),
(2, -2),
(3, -1),
(3, 0),
(3, 1),
(2, 2),
(1, 3),
(0, 3),
(-1, 3),
(-2, 2),
(-3, 1),
(-3, 0),
(-3, -1),
(-2, -2),
(-1, -3),
];
pub const EDGE_THRESHOLD: usize = 19;
pub fn best_fast_in_cell(
image: &GrayImage,
x0: usize,
y0: usize,
x1: usize,
y1: usize,
) -> Option<(f32, f32)> {
let mut threshold = 40i32;
while threshold >= 5 {
if let Some(hit) = best_fast_at_threshold(image, x0, y0, x1, y1, threshold) {
return Some(hit);
}
threshold /= 2;
}
None
}
fn best_fast_at_threshold(
image: &GrayImage,
x0: usize,
y0: usize,
x1: usize,
y1: usize,
threshold: i32,
) -> Option<(f32, f32)> {
let margin = 3usize;
let xs = (x0 + margin)..x1.saturating_sub(margin);
let ys = (y0 + margin)..y1.saturating_sub(margin);
let mut best_score = 0i32;
let mut best: Option<(f32, f32)> = None;
for y in ys {
for x in xs.clone() {
if x < EDGE_THRESHOLD
|| y < EDGE_THRESHOLD
|| x + EDGE_THRESHOLD >= image.width
|| y + EDGE_THRESHOLD >= image.height
{
continue;
}
if let Some(score) = fast9_score(image, x, y, threshold) {
if score > best_score {
best_score = score;
best = Some((x as f32, y as f32));
}
}
}
}
best
}
fn fast9_score(image: &GrayImage, x: usize, y: usize, threshold: i32) -> Option<i32> {
let c = image.data[y * image.width + x] as i32;
let mut brighter = [false; 16];
let mut darker = [false; 16];
let mut diffs = [0i32; 16];
for (i, &(dx, dy)) in CIRCLE.iter().enumerate() {
let px = (x as i32 + dx) as usize;
let py = (y as i32 + dy) as usize;
let v = image.data[py * image.width + px] as i32;
let d = v - c;
diffs[i] = d;
if d > threshold {
brighter[i] = true;
} else if d < -threshold {
darker[i] = true;
}
}
let mut best_run_score = 0i32;
for start in 0..16 {
let mut run = 0;
let mut score = 0i32;
for k in 0..16 {
let i = (start + k) % 16;
if brighter[i] {
run += 1;
score += diffs[i];
} else {
break;
}
}
if run >= 9 {
best_run_score = best_run_score.max(score);
}
run = 0;
score = 0;
for k in 0..16 {
let i = (start + k) % 16;
if darker[i] {
run += 1;
score += -diffs[i];
} else {
break;
}
}
if run >= 9 {
best_run_score = best_run_score.max(score);
}
}
if best_run_score > 0 {
Some(best_run_score)
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_bright_corner() {
let mut data = vec![40u8; 64 * 64];
for y in 29..36 {
for x in 29..36 {
if x >= 32 || y >= 32 {
data[y * 64 + x] = 220;
}
}
}
let img = GrayImage::from_luma8(64, 64, data).unwrap();
let hit = best_fast_in_cell(&img, 20, 20, 44, 44).expect("FAST corner");
assert!(
(hit.0 - 32.0).abs() < 4.0 && (hit.1 - 32.0).abs() < 4.0,
"got {hit:?}"
);
}
#[test]
fn flat_cell_returns_none() {
let img = GrayImage::from_luma8(64, 64, vec![128u8; 64 * 64]).unwrap();
assert!(best_fast_in_cell(&img, 20, 20, 44, 44).is_none());
}
}