use indexmap::IndexMap;
use super::{GeometryError, Point};
pub type ClosedPath = Vec<Point>;
#[derive(Clone, Debug, PartialEq)]
pub struct ScalarGrid {
pub origin: Point,
pub cell: f64,
pub cols: usize,
pub rows: usize,
pub values: Vec<f64>,
}
impl ScalarGrid {
pub fn zeros(
origin: Point,
cell: f64,
cols: usize,
rows: usize,
) -> Result<Self, GeometryError> {
if !(cell.is_finite() && cell > 0.0) {
return Err(GeometryError::BadParameter {
name: "cell",
value: cell,
});
}
for value in [origin.0, origin.1] {
if !value.is_finite() {
return Err(GeometryError::BadParameter {
name: "origin",
value,
});
}
}
if cols == 0 {
return Err(GeometryError::BadParameter {
name: "cols",
value: 0.0,
});
}
if rows == 0 {
return Err(GeometryError::BadParameter {
name: "rows",
value: 0.0,
});
}
let nodes = cols.checked_mul(rows).ok_or(GeometryError::BadParameter {
name: "cols",
value: cols as f64,
})?;
Ok(Self {
origin,
cell,
cols,
rows,
values: vec![0.0; nodes],
})
}
#[must_use]
pub fn get(&self, col: usize, row: usize) -> f64 {
assert!(col < self.cols && row < self.rows, "node out of range");
self.check();
self.values[row * self.cols + col]
}
#[must_use]
pub fn sample(&self, at: Point) -> Option<f64> {
self.check();
if self.cols == 0 || self.rows == 0 {
return None;
}
let u = (at.0 - self.origin.0) / self.cell;
let v = (at.1 - self.origin.1) / self.cell;
if !(u >= 0.0 && u <= (self.cols - 1) as f64 && v >= 0.0 && v <= (self.rows - 1) as f64) {
return None;
}
let c0 = (u.floor() as usize).min(self.cols.saturating_sub(2));
let r0 = (v.floor() as usize).min(self.rows.saturating_sub(2));
let c1 = (c0 + 1).min(self.cols - 1);
let r1 = (r0 + 1).min(self.rows - 1);
let (fx, fy) = (u - c0 as f64, v - r0 as f64);
let at_node = |c: usize, r: usize| self.values[r * self.cols + c];
let top = at_node(c0, r0) + (at_node(c1, r0) - at_node(c0, r0)) * fx;
let bottom = at_node(c0, r1) + (at_node(c1, r1) - at_node(c0, r1)) * fx;
Some(top + (bottom - top) * fy)
}
fn check(&self) {
assert!(
self.values.len() == self.cols * self.rows,
"grid holds {} numbers, expected {} * {}",
self.values.len(),
self.cols,
self.rows
);
}
}
const NONE: usize = usize::MAX;
struct Edges {
cols: usize,
stride: usize,
}
impl Edges {
fn new(grid: &ScalarGrid) -> Self {
Self {
cols: grid.cols,
stride: 2 * grid.cols + 3,
}
}
fn count(&self, grid: &ScalarGrid) -> usize {
(grid.rows + 2) * self.stride
}
fn horizontal(&self, c: i64, r: i64) -> usize {
((r + 1) as usize) * self.stride + (c + 1) as usize
}
fn vertical(&self, c: i64, r: i64) -> usize {
((r + 1) as usize) * self.stride + self.cols + 1 + (c + 1) as usize
}
}
fn node_value(grid: &ScalarGrid, c: i64, r: i64) -> Option<f64> {
if c < 0 || r < 0 || c >= grid.cols as i64 || r >= grid.rows as i64 {
None
} else {
Some(grid.values[r as usize * grid.cols + c as usize])
}
}
fn is_inside(grid: &ScalarGrid, threshold: f64, c: i64, r: i64) -> bool {
node_value(grid, c, r).is_some_and(|v| v > threshold)
}
fn crossing_fraction(threshold: f64, a: f64, b: f64) -> f64 {
let t = (threshold - a) / (b - a);
if (0.0..=1.0).contains(&t) {
t
} else {
0.5
}
}
fn edge_vertex(grid: &ScalarGrid, threshold: f64, edges: &Edges, edge: usize) -> Point {
let row = (edge / edges.stride) as i64 - 1;
let rem = edge % edges.stride;
let (ox, oy) = grid.origin;
if rem < edges.cols + 1 {
let c = rem as i64 - 1;
let t = match (node_value(grid, c, row), node_value(grid, c + 1, row)) {
(Some(a), Some(b)) => crossing_fraction(threshold, a, b),
_ => 0.5,
};
(ox + (c as f64 + t) * grid.cell, oy + row as f64 * grid.cell)
} else {
let c = (rem - edges.cols - 1) as i64 - 1;
let t = match (node_value(grid, c, row), node_value(grid, c, row + 1)) {
(Some(a), Some(b)) => crossing_fraction(threshold, a, b),
_ => 0.5,
};
(ox + c as f64 * grid.cell, oy + (row as f64 + t) * grid.cell)
}
}
fn cell_segments(
grid: &ScalarGrid,
threshold: f64,
edges: &Edges,
cc: i64,
rr: i64,
mut emit: impl FnMut(usize, usize),
) {
let corners = [(cc, rr), (cc + 1, rr), (cc + 1, rr + 1), (cc, rr + 1)];
let inside = corners.map(|(c, r)| is_inside(grid, threshold, c, r));
let sides = [
edges.horizontal(cc, rr),
edges.vertical(cc + 1, rr),
edges.horizontal(cc, rr + 1),
edges.vertical(cc, rr),
];
let mut crossings = [(NONE, false); 4];
let mut n = 0;
for k in 0..4 {
let next = (k + 1) % 4;
if inside[k] != inside[next] {
crossings[n] = (sides[k], inside[k]);
n += 1;
}
}
match n {
2 => {
let (from, to) = if crossings[0].1 {
(crossings[0].0, crossings[1].0)
} else {
(crossings[1].0, crossings[0].0)
};
emit(from, to);
}
4 => {
let value = |c: i64, r: i64| node_value(grid, c, r).unwrap_or(f64::NEG_INFINITY);
let sum =
((value(cc, rr) + value(cc + 1, rr)) + value(cc + 1, rr + 1)) + value(cc, rr + 1);
let joined = sum / 4.0 > threshold;
for k in 0..4 {
if crossings[k].1 {
let other = if joined { (k + 1) % 4 } else { (k + 3) % 4 };
emit(crossings[k].0, crossings[other].0);
}
}
}
_ => {}
}
}
#[must_use]
pub fn iso_lines(grid: &ScalarGrid, threshold: f64) -> Vec<ClosedPath> {
grid.check();
let edges = Edges::new(grid);
let mut next = vec![NONE; edges.count(grid)];
for rr in -1..grid.rows as i64 {
for cc in -1..grid.cols as i64 {
cell_segments(grid, threshold, &edges, cc, rr, |from, to| next[from] = to);
}
}
let mut paths = Vec::new();
for start in 0..next.len() {
if next[start] == NONE {
continue;
}
let mut path: Vec<Point> = Vec::new();
let mut seen: IndexMap<(u64, u64), usize> = IndexMap::new();
let mut edge = start;
while next[edge] != NONE {
let vertex = edge_vertex(grid, threshold, &edges, edge);
let key = ((vertex.0 + 0.0).to_bits(), (vertex.1 + 0.0).to_bits());
if let Some(&at) = seen.get(&key) {
let cut = path.split_off(at + 1);
for dropped in &cut {
seen.swap_remove(&((dropped.0 + 0.0).to_bits(), (dropped.1 + 0.0).to_bits()));
}
keep_loop(&mut paths, std::iter::once(vertex).chain(cut).collect());
} else {
seen.insert(key, path.len());
path.push(vertex);
}
let following = next[edge];
next[edge] = NONE;
edge = following;
}
keep_loop(&mut paths, path);
}
paths
}
fn keep_loop(paths: &mut Vec<ClosedPath>, path: Vec<Point>) {
if path.len() >= 3 && signed_area(&path) != 0.0 {
paths.push(path);
}
}
#[must_use]
pub fn signed_area(path: &[Point]) -> f64 {
let mut twice = 0.0_f64;
for (k, p) in path.iter().enumerate() {
let q = path[(k + 1) % path.len()];
twice += p.0 * q.1 - q.0 * p.1;
}
twice / 2.0
}
fn crosses_ray(p: Point, q: Point, at: Point) -> bool {
if (p.1 > at.1) == (q.1 > at.1) {
return false;
}
let x = p.0 + (at.1 - p.1) * (q.0 - p.0) / (q.1 - p.1);
x > at.0
}
#[must_use]
pub fn contains(paths: &[ClosedPath], at: Point) -> bool {
let mut inside = false;
for path in paths {
for (k, p) in path.iter().enumerate() {
let q = path[(k + 1) % path.len()];
if crosses_ray(*p, q, at) {
inside = !inside;
}
}
}
inside
}
pub(crate) fn inside_cell(grid: &ScalarGrid, threshold: f64, at: Point) -> bool {
grid.check();
let u = ((at.0 - grid.origin.0) / grid.cell).floor();
let v = ((at.1 - grid.origin.1) / grid.cell).floor();
if !(u >= -1.0 && u < grid.cols as f64 && v >= -1.0 && v < grid.rows as f64) {
return false;
}
let (cc, rr) = (u as i64, v as i64);
let edges = Edges::new(grid);
let mut crossed = false;
cell_segments(grid, threshold, &edges, cc, rr, |from, to| {
let p = edge_vertex(grid, threshold, &edges, from);
let q = edge_vertex(grid, threshold, &edges, to);
if crosses_ray(p, q, at) {
crossed = !crossed;
}
});
let below = is_inside(grid, threshold, cc + 1, rr);
let above = is_inside(grid, threshold, cc + 1, rr + 1);
let at_edge = if below == above {
below
} else {
let y = edge_vertex(grid, threshold, &edges, edges.vertical(cc + 1, rr)).1;
if at.1 < y {
below
} else {
above
}
};
at_edge != crossed
}
#[cfg(test)]
mod tests {
use super::*;
fn lcg(state: &mut u64) -> f64 {
*state = state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
(*state >> 40) as f64 / (1_u64 << 24) as f64
}
#[test]
fn inside_cell_agrees_with_contains() {
let mut state = 20_261_121_u64;
let mut grid = ScalarGrid::zeros((0.0, 0.0), 1.0, 40, 30).unwrap();
for value in &mut grid.values {
*value = lcg(&mut state);
}
let paths = iso_lines(&grid, 0.5);
let mut second = 7_u64;
let mut inside = 0;
for _ in 0..2_000 {
let p = (lcg(&mut second) * 39.0, lcg(&mut second) * 29.0);
let expected = contains(&paths, p);
assert_eq!(inside_cell(&grid, 0.5, p), expected, "at {p:?}");
inside += usize::from(expected);
}
assert!(inside > 300 && inside < 1_700, "{inside} inside");
assert!(!inside_cell(&grid, 0.5, (-3.0, 4.0)));
}
}