const GRID: usize = 20;
#[derive(Debug, Clone)]
pub struct Mask {
pub ink: Vec<f32>,
pub width: usize,
pub height: usize,
pub origin_x: f32,
pub origin_y: f32,
}
impl Mask {
pub fn new(ink: Vec<f32>, width: usize, height: usize, origin_x: f32, origin_y: f32) -> Self {
Self {
ink,
width,
height,
origin_x,
origin_y,
}
}
pub fn at(&self, x: usize, y: usize) -> bool {
self.cover(x, y) > 0.0
}
pub fn cover(&self, x: usize, y: usize) -> f32 {
if x < self.width && y < self.height {
self.ink[y * self.width + x]
} else {
0.0
}
}
pub fn cropped(&self) -> Option<Self> {
let (mut x0, mut y0, mut x1, mut y1) = (usize::MAX, usize::MAX, 0, 0);
for y in 0..self.height {
for x in 0..self.width {
if self.at(x, y) {
(x0, y0, x1, y1) = (x0.min(x), y0.min(y), x1.max(x + 1), y1.max(y + 1));
}
}
}
if x1 <= x0 {
return None;
}
let (width, height) = (x1 - x0, y1 - y0);
let ink = (y0..y1)
.flat_map(|y| (x0..x1).map(move |x| (x, y)))
.map(|(x, y)| self.cover(x, y))
.collect();
Some(Self::new(
ink,
width,
height,
self.origin_x + x0 as f32,
self.origin_y + y0 as f32,
))
}
pub fn pieces(&self) -> Vec<Vec<(usize, usize)>> {
let mut seen = vec![false; self.ink.len()];
let mut pieces = Vec::new();
for start in 0..self.ink.len() {
if self.ink[start] <= 0.0 || seen[start] {
continue;
}
seen[start] = true;
let mut stack = vec![start];
let mut piece = Vec::new();
while let Some(idx) = stack.pop() {
let (x, y) = (idx % self.width, idx / self.width);
piece.push((x, y));
for (dx, dy) in [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)] {
let (nx, ny) = (x as isize + dx, y as isize + dy);
if nx < 0 || ny < 0 || nx as usize >= self.width || ny as usize >= self.height {
continue;
}
let next = ny as usize * self.width + nx as usize;
if self.ink[next] > 0.0 && !seen[next] {
seen[next] = true;
stack.push(next);
}
}
}
pieces.push(piece);
}
pieces
}
pub fn parts(&self) -> usize {
self.pieces().len()
}
}
#[derive(Debug, Clone)]
pub struct Shape {
grid: Vec<f32>,
pub aspect: f32,
}
const THIN_MAX_ASPECT: f32 = 0.1;
const SAMPLES: usize = 4;
impl Shape {
pub fn of(mask: &Mask) -> Self {
let mut grid = vec![0.0f32; GRID * GRID];
for (cell, value) in grid.iter_mut().enumerate() {
let (cx, cy) = (cell % GRID, cell / GRID);
let mut hits = 0.0;
for sy in 0..SAMPLES {
for sx in 0..SAMPLES {
let fx = (cx * SAMPLES + sx) as f32 + 0.5;
let fy = (cy * SAMPLES + sy) as f32 + 0.5;
let x = (fx / (GRID * SAMPLES) as f32 * mask.width as f32) as usize;
let y = (fy / (GRID * SAMPLES) as f32 * mask.height as f32) as usize;
hits += mask.cover(x, y);
}
}
*value = hits / (SAMPLES * SAMPLES) as f32;
}
let blurred: Vec<f32> = (0..GRID * GRID)
.map(|cell| {
let (cx, cy) = ((cell % GRID) as isize, (cell / GRID) as isize);
let mut sum = 0.0;
for dy in -1..=1 {
for dx in -1..=1 {
let (x, y) = (
(cx + dx).clamp(0, GRID as isize - 1),
(cy + dy).clamp(0, GRID as isize - 1),
);
sum += grid[y as usize * GRID + x as usize];
}
}
sum / 9.0
})
.collect();
let mean = blurred.iter().sum::<f32>() / blurred.len() as f32;
let centred: Vec<f32> = blurred.iter().map(|value| value - mean).collect();
let norm = centred.iter().map(|value| value * value).sum::<f32>().sqrt();
let aspect = mask.width as f32 / mask.height.max(1) as f32;
let thin = !(THIN_MAX_ASPECT..=1.0 / THIN_MAX_ASPECT).contains(&aspect);
let grid = if norm > 1e-3 && !thin {
centred.iter().map(|value| value / norm).collect()
} else {
vec![0.0; GRID * GRID]
};
Self { grid, aspect }
}
pub fn likeness(&self, other: &Shape) -> f32 {
let uniform = |shape: &Shape| shape.grid.iter().all(|value| *value == 0.0);
if uniform(self) || uniform(other) {
return if uniform(self) && uniform(other) { 1.0 } else { 0.0 };
}
self.grid.iter().zip(&other.grid).map(|(a, b)| a * b).sum()
}
}