use std::collections::{HashMap, HashSet};
use crate::interrupt::NoCancel;
use super::delaunay::TetMesh;
use super::predicates::orient3d;
pub const EXHAUSTIVE_LIMIT: usize = 12;
pub const DEFAULT_BUDGET: usize = 3_000;
#[derive(Debug, Default, Clone, Copy)]
pub struct Constraints<'a> {
pub without_edges: &'a [(u32, u32)],
pub with_faces: &'a [[u32; 3]],
pub with_edges: &'a [(u32, u32)],
}
pub fn delaunay_fill(
points: &[[f64; 3]],
boundary: &[[u32; 3]],
want: Constraints<'_>,
) -> Result<Vec<[u32; 4]>, Vec<[u32; 3]>> {
let mut vs: Vec<u32> = boundary.iter().flatten().copied().collect();
vs.sort_unstable();
vs.dedup();
if vs.len() < 4 {
return Err(Vec::new());
}
let local: Vec<[f64; 3]> = vs.iter().map(|&i| points[i as usize]).collect();
let down = |g: u32| vs.binary_search(&g).ok().map(|i| i as u32);
let up = |l: u32| vs[l as usize];
let mut walls: Vec<[u32; 3]> = Vec::with_capacity(boundary.len());
for f in boundary {
let Some(w) = (|| Some([down(f[0])?, down(f[1])?, down(f[2])?]))() else {
return Err(Vec::new());
};
walls.push(w);
}
let Ok(mesh) = TetMesh::delaunay(&local, &NoCancel) else {
return Err(Vec::new());
};
let missing: Vec<[u32; 3]> = walls
.iter()
.filter(|f| !mesh.has_face(f))
.map(|f| [up(f[0]), up(f[1]), up(f[2])])
.collect();
if !missing.is_empty() {
return Err(missing);
}
let barrier: HashSet<[u32; 3]> = walls.iter().map(key).collect();
let mut inside = vec![false; mesh.slot_count()];
let mut stack: Vec<usize> = Vec::new();
for f in &walls {
let Some(owners) = mesh.face_owners(f) else {
return Err(Vec::new());
};
for (t, i) in owners {
let Some(apex) = mesh.tet(t as usize).map(|c| c[i]) else {
return Err(Vec::new());
};
let p = mesh.points();
if orient3d(
&p[f[0] as usize],
&p[f[1] as usize],
&p[f[2] as usize],
&p[apex as usize],
) < 0.0
&& !inside[t as usize]
{
inside[t as usize] = true;
stack.push(t as usize);
}
}
}
if stack.is_empty() {
return Err(Vec::new());
}
while let Some(t) = stack.pop() {
for i in 0..4 {
if barrier.contains(&key(&mesh.face(t, i))) {
continue;
}
if let Some(n) = mesh.neighbour(t, i) {
if !inside[n] {
inside[n] = true;
stack.push(n);
}
}
}
}
let cells: Vec<[u32; 4]> = mesh
.iter()
.filter(|(t, _)| inside[*t])
.map(|(_, v)| [up(v[0]), up(v[1]), up(v[2]), up(v[3])])
.collect();
if cells.is_empty() {
return Err(Vec::new());
}
let enclosed = enclosed_volume(points, boundary);
let filled: f64 = cells
.iter()
.map(|c| {
let p = |i: u32| points[i as usize];
orient3d(&p(c[0]), &p(c[1]), &p(c[2]), &p(c[3])) / 6.0
})
.sum();
if enclosed <= 0.0 || (filled - enclosed).abs() > 1e-9 * enclosed {
return Err(Vec::new());
}
let ok = want.with_faces.iter().all(|f| {
cells
.iter()
.any(|c| faces_of(c).iter().any(|g| key(g) == key(f)))
}) && want
.with_edges
.iter()
.all(|&(u, v)| cells.iter().any(|c| c.contains(&u) && c.contains(&v)))
&& !want
.without_edges
.iter()
.any(|&(u, v)| cells.iter().any(|c| c.contains(&u) && c.contains(&v)));
if ok {
Ok(cells)
} else {
Err(Vec::new())
}
}
fn enclosed_volume(points: &[[f64; 3]], boundary: &[[u32; 3]]) -> f64 {
const O: [f64; 3] = [0.0, 0.0, 0.0];
boundary
.iter()
.map(|f| {
let p = |i: u32| points[i as usize];
-orient3d(&p(f[0]), &p(f[1]), &p(f[2]), &O) / 6.0
})
.sum()
}
pub fn fill(
points: &[[f64; 3]],
boundary: &[[u32; 3]],
want: Constraints<'_>,
budget: usize,
) -> Option<Vec<[u32; 4]>> {
if boundary.len() < 4 {
return None;
}
let mut vertices: Vec<u32> = boundary.iter().flatten().copied().collect();
vertices.sort_unstable();
vertices.dedup();
if vertices.len() < 4 {
return None;
}
let origin = points[vertices[0] as usize];
let target: f64 = boundary
.iter()
.map(|f| {
let at = |i: u32| {
let p = points[i as usize];
[p[0] - origin[0], p[1] - origin[1], p[2] - origin[2]]
};
let (a, b, c) = (at(f[0]), at(f[1]), at(f[2]));
a[0] * (b[1] * c[2] - b[2] * c[1]) - a[1] * (b[0] * c[2] - b[2] * c[0])
+ a[2] * (b[0] * c[1] - b[1] * c[0])
})
.sum();
if target <= 0.0 {
return None; }
let slack = 1e-9 * target;
let mut open: HashMap<[u32; 3], [u32; 3]> = HashMap::with_capacity(boundary.len() * 2);
for f in boundary {
if open.insert(key(f), *f).is_some() {
return None; }
}
let mut placed: Vec<[u32; 4]> = Vec::new();
let mut budget = budget;
grow(
points,
&vertices,
want,
target,
slack,
&mut open,
&mut placed,
0.0,
&mut budget,
)
.then_some(placed)
}
#[allow(clippy::too_many_arguments)]
fn grow(
points: &[[f64; 3]],
vertices: &[u32],
want: Constraints<'_>,
target: f64,
slack: f64,
open: &mut HashMap<[u32; 3], [u32; 3]>,
placed: &mut Vec<[u32; 4]>,
volume: f64,
budget: &mut usize,
) -> bool {
if open.is_empty() {
if (volume - target).abs() > slack {
return false;
}
return want.with_faces.iter().all(|f| {
placed
.iter()
.any(|c| faces_of(c).iter().any(|g| key(g) == key(f)))
});
}
if *budget == 0 {
return false;
}
let face = *open
.iter()
.min_by_key(|(k, _)| **k)
.map(|(_, f)| f)
.expect("open is not empty");
for &apex in vertices {
if *budget == 0 {
return false;
}
if face.contains(&apex) {
continue;
}
let cell = [face[0], face[2], face[1], apex];
if want
.without_edges
.iter()
.any(|&(u, v)| cell.contains(&u) && cell.contains(&v))
{
continue;
}
if want.with_faces.iter().any(|f| cut_by(points, &cell, f)) {
continue;
}
if want
.with_edges
.iter()
.any(|&(u, v)| blocks_edge(points, &cell, u, v))
{
continue;
}
let v6 = orient3d(
&points[cell[0] as usize],
&points[cell[1] as usize],
&points[cell[2] as usize],
&points[cell[3] as usize],
);
if v6 <= 0.0 || volume + v6 > target + slack {
continue;
}
*budget -= 1;
let faces = faces_of(&cell);
let mut closed: Vec<([u32; 3], [u32; 3])> = Vec::with_capacity(4);
let mut opened: Vec<[u32; 3]> = Vec::with_capacity(4);
let mut ok = true;
for f in &faces {
let k = key(f);
match open.remove(&k) {
Some(expected) => {
if same_winding(&expected, f) {
closed.push((k, expected));
} else {
open.insert(k, expected);
ok = false;
break;
}
}
None => {
open.insert(k, [f[0], f[2], f[1]]);
opened.push(k);
}
}
}
if ok {
placed.push(cell);
if grow(
points,
vertices,
want,
target,
slack,
open,
placed,
volume + v6,
budget,
) {
return true;
}
placed.pop();
}
for k in opened {
open.remove(&k);
}
for (k, expected) in closed {
open.insert(k, expected);
}
}
false
}
fn faces_of(c: &[u32; 4]) -> [[u32; 3]; 4] {
[
[c[1], c[2], c[3]],
[c[0], c[3], c[2]],
[c[0], c[1], c[3]],
[c[0], c[2], c[1]],
]
}
fn cut_by(points: &[[f64; 3]], cell: &[u32; 4], f: &[u32; 3]) -> bool {
let side = |x: u32| {
orient3d(
&points[f[0] as usize],
&points[f[1] as usize],
&points[f[2] as usize],
&points[x as usize],
)
};
for a in 0..4 {
for b in a + 1..4 {
let (p, q) = (cell[a], cell[b]);
if f.contains(&p) || f.contains(&q) {
continue;
}
let (sp, sq) = (side(p), side(q));
if !((sp > 0.0 && sq < 0.0) || (sp < 0.0 && sq > 0.0)) {
continue;
}
let s = [
orient3d(
&points[p as usize],
&points[q as usize],
&points[f[0] as usize],
&points[f[1] as usize],
),
orient3d(
&points[p as usize],
&points[q as usize],
&points[f[1] as usize],
&points[f[2] as usize],
),
orient3d(
&points[p as usize],
&points[q as usize],
&points[f[2] as usize],
&points[f[0] as usize],
),
];
if s.iter().all(|&x| x > 0.0) || s.iter().all(|&x| x < 0.0) {
return true;
}
}
}
false
}
fn blocks_edge(points: &[[f64; 3]], cell: &[u32; 4], u: u32, v: u32) -> bool {
if cell.contains(&u) && cell.contains(&v) {
return false; }
let at = |x: u32| &points[x as usize];
for f in faces_of(cell) {
if f.contains(&u) || f.contains(&v) {
continue;
}
let side = |x: u32| orient3d(at(f[0]), at(f[1]), at(f[2]), at(x));
let (su, sv) = (side(u), side(v));
if (su > 0.0 && sv < 0.0) || (su < 0.0 && sv > 0.0) {
let s = [
orient3d(at(u), at(v), at(f[0]), at(f[1])),
orient3d(at(u), at(v), at(f[1]), at(f[2])),
orient3d(at(u), at(v), at(f[2]), at(f[0])),
];
if s.iter().all(|&x| x > 0.0) || s.iter().all(|&x| x < 0.0) {
return true;
}
}
}
for a in 0..4 {
for b in a + 1..4 {
let (p, q) = (cell[a], cell[b]);
if p == u || q == u || p == v || q == v {
continue;
}
if orient3d(at(u), at(v), at(p), at(q)) != 0.0 {
continue; }
let e1 = [
at(v)[0] - at(u)[0],
at(v)[1] - at(u)[1],
at(v)[2] - at(u)[2],
];
let e2 = [
at(p)[0] - at(u)[0],
at(p)[1] - at(u)[1],
at(p)[2] - at(u)[2],
];
let lift = [
at(u)[0] + e1[1] * e2[2] - e1[2] * e2[1],
at(u)[1] + e1[2] * e2[0] - e1[0] * e2[2],
at(u)[2] + e1[0] * e2[1] - e1[1] * e2[0],
];
let straddles = |a: u32, b: u32, c: u32, d: u32| {
let sc = orient3d(at(a), at(b), &lift, at(c));
let sd = orient3d(at(a), at(b), &lift, at(d));
(sc > 0.0 && sd < 0.0) || (sc < 0.0 && sd > 0.0)
};
if straddles(u, v, p, q) && straddles(p, q, u, v) {
return true;
}
}
}
false
}
fn key(f: &[u32; 3]) -> [u32; 3] {
let mut k = *f;
k.sort_unstable();
k
}
fn same_winding(a: &[u32; 3], b: &[u32; 3]) -> bool {
(0..3).any(|r| a[0] == b[r] && a[1] == b[(r + 1) % 3] && a[2] == b[(r + 2) % 3])
}
#[cfg(test)]
mod tests {
use super::*;
fn cube() -> Vec<[f64; 3]> {
vec![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
]
}
const ALTERNATING: [[u32; 3]; 12] = [
[0, 3, 2],
[0, 2, 1],
[4, 5, 7],
[5, 6, 7],
[0, 1, 5],
[0, 5, 4],
[1, 2, 5],
[2, 6, 5],
[2, 3, 7],
[2, 7, 6],
[3, 0, 7],
[0, 4, 7],
];
const UNFILLABLE: [[u32; 3]; 12] = [
[0, 3, 2],
[0, 2, 1],
[4, 5, 6],
[4, 6, 7],
[0, 1, 5],
[0, 5, 4],
[1, 2, 6],
[1, 6, 5],
[2, 3, 7],
[2, 7, 6],
[3, 0, 4],
[3, 4, 7],
];
fn volume_of(points: &[[f64; 3]], cells: &[[u32; 4]]) -> f64 {
cells
.iter()
.map(|c| {
orient3d(
&points[c[0] as usize],
&points[c[1] as usize],
&points[c[2] as usize],
&points[c[3] as usize],
)
})
.sum::<f64>()
/ 6.0
}
fn check_tiles(cells: &[[u32; 4]], boundary: &[[u32; 3]]) {
let mut seen: HashMap<[u32; 3], i32> = HashMap::new();
for c in cells {
for f in [
[c[1], c[2], c[3]],
[c[0], c[3], c[2]],
[c[0], c[1], c[3]],
[c[0], c[2], c[1]],
] {
*seen.entry(key(&f)).or_default() += 1;
}
}
for f in boundary {
assert_eq!(seen.remove(&key(f)), Some(1), "boundary face {f:?}");
}
for (f, n) in seen {
assert_eq!(n, 2, "interior face {f:?} used {n} time(s)");
}
}
#[test]
fn fills_a_box_whose_faces_allow_it() {
let p = cube();
let cells = fill(&p, &ALTERNATING, Constraints::default(), DEFAULT_BUDGET)
.expect("a filling exists");
assert!((volume_of(&p, &cells) - 1.0).abs() < 1e-14);
check_tiles(&cells, &ALTERNATING);
}
#[test]
fn refuses_a_box_whose_faces_forbid_it() {
let p = cube();
assert_eq!(
fill(&p, &UNFILLABLE, Constraints::default(), DEFAULT_BUDGET),
None
);
}
#[test]
fn honours_a_forbidden_edge() {
let p = cube();
assert_eq!(
fill(
&p,
&ALTERNATING,
Constraints {
without_edges: &[(0, 2)],
..Default::default()
},
DEFAULT_BUDGET
),
None
);
let without = fill(
&p,
&ALTERNATING,
Constraints {
without_edges: &[(0, 6)],
..Default::default()
},
DEFAULT_BUDGET,
)
.expect("a filling exists");
assert!(without.iter().all(|c| !(c.contains(&0) && c.contains(&6))));
assert!((volume_of(&p, &without) - 1.0).abs() < 1e-14);
}
#[test]
fn fills_a_tetrahedron_with_itself() {
let p = vec![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
];
let boundary = [[0, 2, 1], [0, 1, 3], [0, 3, 2], [1, 2, 3]];
let cells =
fill(&p, &boundary, Constraints::default(), DEFAULT_BUDGET).expect("a filling exists");
assert_eq!(cells.len(), 1);
assert!((volume_of(&p, &cells) - 1.0 / 6.0).abs() < 1e-15);
}
#[test]
fn refuses_a_surface_wound_inwards() {
let p = cube();
let flipped: Vec<[u32; 3]> = ALTERNATING.iter().map(|f| [f[0], f[2], f[1]]).collect();
assert_eq!(
fill(&p, &flipped, Constraints::default(), DEFAULT_BUDGET),
None
);
}
#[test]
fn the_filling_does_not_depend_on_the_run() {
let p = cube();
let reference = fill(&p, &ALTERNATING, Constraints::default(), DEFAULT_BUDGET).unwrap();
for _ in 0..5 {
assert_eq!(
fill(&p, &ALTERNATING, Constraints::default(), DEFAULT_BUDGET).unwrap(),
reference
);
}
}
fn bipyramid() -> (Vec<[f64; 3]>, [[u32; 3]; 6]) {
let p = vec![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.2, 0.2, -1.0],
[0.2, 0.2, 1.0],
];
let faces = [
[0, 1, 4],
[1, 2, 4],
[2, 0, 4],
[1, 0, 3],
[2, 1, 3],
[0, 2, 3],
];
(p, faces)
}
#[test]
fn delaunay_fills_a_convex_region() {
let (p, faces) = bipyramid();
let cells = delaunay_fill(&p, &faces, Constraints::default())
.expect("a convex region's own hull is always in its triangulation");
check_tiles(&cells, &faces);
assert!((volume_of(&p, &cells) - 1.0 / 3.0).abs() < 1e-14);
}
#[test]
fn delaunay_declines_the_diagonals_a_box_does_not_choose() {
let p = cube();
let missing = delaunay_fill(&p, &ALTERNATING, Constraints::default())
.expect_err("the triangulation does not hold these diagonals");
assert!(!missing.is_empty(), "it must say which faces are missing");
}
#[test]
fn delaunay_declines_a_surface_wound_inwards() {
let p = cube();
let flipped: Vec<[u32; 3]> = ALTERNATING.iter().map(|f| [f[0], f[2], f[1]]).collect();
assert!(delaunay_fill(&p, &flipped, Constraints::default()).is_err());
}
#[test]
fn delaunay_fills_a_non_convex_region() {
let p = cube();
let boundary = [
[0, 2, 1],
[0, 1, 5],
[0, 5, 2],
[1, 2, 5],
[0, 3, 2],
[0, 2, 7],
[0, 7, 3],
[2, 3, 7],
];
if let Ok(cells) = delaunay_fill(&p, &boundary, Constraints::default()) {
check_tiles(&cells, &boundary);
}
}
#[test]
fn delaunay_honours_what_the_caller_needs() {
let (p, faces) = bipyramid();
let cells = delaunay_fill(&p, &faces, Constraints::default()).unwrap();
let (u, v) = (cells[0][0], cells[0][1]);
assert!(delaunay_fill(
&p,
&faces,
Constraints {
with_edges: &[(u, v)],
..Default::default()
}
)
.is_ok());
assert!(delaunay_fill(
&p,
&faces,
Constraints {
without_edges: &[(u, v)],
..Default::default()
}
)
.is_err());
}
#[test]
fn a_tight_budget_gives_up_rather_than_running_on() {
let p = cube();
assert_eq!(fill(&p, &UNFILLABLE, Constraints::default(), 5), None);
}
}