#![allow(clippy::needless_range_loop)]
use crate::kernel::geom::{self, V3};
use super::fingerprint::{FaceFingerprint, FeatureFingerprint, Fingerprints};
use super::{Alignment, Thresholds, multiset_distance, same_value};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Motion {
pub r: [[f64; 3]; 3],
pub t: V3,
}
impl Motion {
pub const IDENTITY: Motion = Motion {
r: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
t: [0.0; 3],
};
pub fn point(&self, p: V3) -> V3 {
geom::add(self.dir(p), self.t)
}
pub fn dir(&self, d: V3) -> V3 {
[0, 1, 2].map(|i| geom::dot(self.r[i], d))
}
}
#[derive(Clone, Copy, Debug)]
struct Anchor {
point: V3,
dir: Option<V3>,
line: bool,
}
type Pair = (usize, usize);
fn feature_anchor(f: &FeatureFingerprint) -> Option<Anchor> {
Some(Anchor {
point: f.position?,
dir: f.axis,
line: true,
})
}
fn face_anchor(f: &FaceFingerprint) -> Anchor {
Anchor {
point: f.centroid,
dir: f.normal,
line: false,
}
}
fn same_feature(a: &FeatureFingerprint, b: &FeatureFingerprint, rel: f64) -> bool {
a.family == b.family
&& a.traits == b.traits
&& a.sizes.len() == b.sizes.len()
&& a.sizes
.iter()
.all(|(k, x)| b.sizes.get(k).is_some_and(|y| same_value(*x, *y, rel)))
&& a.neighbourhood == b.neighbourhood
}
fn same_face(a: &FaceFingerprint, b: &FaceFingerprint, rel: f64) -> bool {
a.surface == b.surface
&& a.parameters.len() == b.parameters.len()
&& a.parameters
.iter()
.all(|(k, x)| b.parameters.get(k).is_some_and(|y| same_value(*x, *y, rel)))
&& match (a.area, b.area) {
(Some(x), Some(y)) => same_value(x, y, rel),
_ => false,
}
&& multiset_distance(&a.adjacency, &b.adjacency) == 0.0
}
fn candidates<T>(
old: &[T],
new: &[T],
same: impl Fn(&T, &T) -> bool,
max: usize,
) -> Vec<(usize, usize, usize)> {
let mut pairs = Vec::new();
let mut per_new = vec![0usize; new.len()];
for (i, a) in old.iter().enumerate() {
let mine: Vec<usize> = (0..new.len()).filter(|&j| same(a, &new[j])).collect();
for &j in &mine {
per_new[j] += 1;
}
if !mine.is_empty() && mine.len() <= max {
for &j in &mine {
pairs.push((i, j, mine.len()));
}
}
}
pairs.retain(|&(_, j, _)| per_new[j] <= max);
pairs
.into_iter()
.map(|(i, j, n)| (i, j, n.max(per_new[j])))
.collect()
}
struct Candidates {
old: Vec<Anchor>,
new: Vec<Anchor>,
pairs: Vec<Pair>,
}
impl Candidates {
fn of(old: &Fingerprints, new: &Fingerprints, th: &Thresholds) -> Candidates {
let mut old_anchors = Vec::new();
let mut new_anchors = Vec::new();
let mut pairs: Vec<(Pair, usize, f64)> = Vec::new();
let feature_pairs = candidates(
&old.features,
&new.features,
|a, b| same_feature(a, b, th.same_rel),
th.anchor_candidates,
);
let mut index_old = vec![usize::MAX; old.features.len()];
let mut index_new = vec![usize::MAX; new.features.len()];
for (i, j, n) in feature_pairs {
let (Some(a), Some(b)) = (
feature_anchor(&old.features[i]),
feature_anchor(&new.features[j]),
) else {
continue;
};
if index_old[i] == usize::MAX {
index_old[i] = old_anchors.len();
old_anchors.push(a);
}
if index_new[j] == usize::MAX {
index_new[j] = new_anchors.len();
new_anchors.push(b);
}
pairs.push(((index_old[i], index_new[j]), n, f64::INFINITY));
}
let face_pairs = candidates(
&old.faces,
&new.faces,
|a, b| same_face(a, b, th.same_rel),
th.anchor_candidates,
);
let mut index_old = vec![usize::MAX; old.faces.len()];
let mut index_new = vec![usize::MAX; new.faces.len()];
for (i, j, n) in face_pairs {
let Some(area) = old.faces[i].area else {
continue;
};
if index_old[i] == usize::MAX {
index_old[i] = old_anchors.len();
old_anchors.push(face_anchor(&old.faces[i]));
}
if index_new[j] == usize::MAX {
index_new[j] = new_anchors.len();
new_anchors.push(face_anchor(&new.faces[j]));
}
pairs.push(((index_old[i], index_new[j]), n, area));
}
pairs.sort_by(|a, b| a.1.cmp(&b.1).then(b.2.total_cmp(&a.2)));
pairs.truncate(th.anchor_pairs);
Candidates {
old: old_anchors,
new: new_anchors,
pairs: pairs.into_iter().map(|p| p.0).collect(),
}
}
fn inliers(&self, m: &Motion, tol: f64, cos_tol: f64) -> (usize, f64, Vec<Pair>) {
let mut hit_old = vec![false; self.old.len()];
let mut hit_new = vec![false; self.new.len()];
let (mut count, mut sq) = (0, 0.0);
let mut kept = Vec::new();
for &(i, j) in &self.pairs {
let (a, b) = (&self.old[i], &self.new[j]);
let d = geom::dist(m.point(a.point), b.point);
if d > tol {
continue;
}
if let (Some(da), Some(db)) = (a.dir, b.dir) {
let c = geom::dot(m.dir(da), db);
if (if a.line { c.abs() } else { c }) < cos_tol {
continue;
}
}
kept.push((i, j));
if !hit_old[i] && !hit_new[j] {
hit_old[i] = true;
hit_new[j] = true;
count += 1;
sq += d * d;
}
}
(count, (sq / count.max(1) as f64).sqrt(), kept)
}
fn points(&self, pairs: impl IntoIterator<Item = Pair>, scale: f64) -> (Vec<V3>, Vec<V3>) {
let mut from = Vec::new();
let mut to = Vec::new();
for (i, j) in pairs {
let (a, b) = (&self.old[i], &self.new[j]);
from.push(a.point);
to.push(b.point);
if let (Some(da), Some(db), false) = (a.dir, b.dir, a.line) {
from.push(geom::add(a.point, geom::scale(da, scale)));
to.push(geom::add(b.point, geom::scale(db, scale)));
}
}
(from, to)
}
}
type Best = Option<(Motion, usize, f64)>;
pub fn align(old: &Fingerprints, new: &Fingerprints, th: &Thresholds) -> (Motion, Alignment) {
let scale = old.scale.max(new.scale);
let tol = th.position_tol * scale;
let cos_tol = th.angle_tol.cos();
let c = Candidates::of(old, new, th);
let anchors = {
let mut seen: Vec<usize> = c.pairs.iter().map(|p| p.0).collect();
seen.sort_unstable();
seen.dedup();
seen.len()
};
let (best, rivals) = search(&c, th, scale, tol, cos_tol);
let accepted = |b: &Best| {
b.filter(|(_, count, _)| {
*count >= th.min_inliers && *count as f64 >= th.min_inlier_fraction * anchors as f64
})
};
let (m, count, axisymmetric) = match (accepted(&best[0]), accepted(&best[1])) {
(Some((m, c, _)), _) => (m, c, false),
(None, Some((m, c, _))) => (m, c, true),
(None, None) => {
let most = best.iter().flatten().map(|b| b.1).max().unwrap_or(0);
return unaligned(anchors, most);
}
};
let (_, _, kept) = c.inliers(&m, tol, cos_tol);
let (from, to) = c.points(kept, scale);
let refined = horn(&from, &to).unwrap_or(m);
let (count2, rms2, _) = c.inliers(&refined, tol, cos_tol);
let (m, count, rms) = if count2 >= count {
(refined, count2, rms2)
} else {
(m, count, c.inliers(&m, tol, cos_tol).1)
};
let axis = if axisymmetric {
line_through(&to)
} else {
None
};
if let Some([o, d]) = axis
&& let Some((turned, support, fold)) = resolve_turn(old, new, th, &m, o, d, scale)
{
return (
turned,
Alignment {
found: true,
rotation: turned.r,
translation: turned.t,
anchors,
inliers: count + support,
symmetric: fold > 1,
axisymmetric: false,
axis: None,
fold: Some(fold),
rms,
},
);
}
let symmetric = axisymmetric
|| rivals
.iter()
.any(|(r, c)| *c >= count && !same_motion(r, &m, tol, th.angle_tol, scale));
(
m,
Alignment {
found: true,
rotation: m.r,
translation: m.t,
anchors,
inliers: count,
symmetric,
axisymmetric,
axis,
fold: None,
rms,
},
)
}
fn unaligned(anchors: usize, inliers: usize) -> (Motion, Alignment) {
(
Motion::IDENTITY,
Alignment {
found: false,
rotation: Motion::IDENTITY.r,
translation: [0.0; 3],
anchors,
inliers,
symmetric: false,
axisymmetric: false,
axis: None,
fold: None,
rms: 0.0,
},
)
}
fn search(
c: &Candidates,
th: &Thresholds,
scale: f64,
tol: f64,
cos_tol: f64,
) -> ([Best; 2], Vec<(Motion, usize)>) {
let mut best: [Best; 2] = [None, None];
let mut rivals: Vec<(Motion, usize)> = Vec::new();
for_each_sample(c.pairs.len(), th.hypotheses, |sample| {
let Some((m, line)) = hypothesis(c, sample, scale, tol) else {
return;
};
let best = &mut best[usize::from(line)];
let (count, rms, _) = c.inliers(&m, tol, cos_tol);
rivals.push((m, count));
if better(&m, count, rms, best, tol, scale) {
*best = Some((m, count, rms));
}
});
(best, rivals)
}
fn for_each_sample(n: usize, hypotheses: usize, mut consider: impl FnMut(&[usize])) {
let mut rng = 0x9e37_79b9_7f4a_7c15u64;
let mut next = |bound: usize| {
rng ^= rng << 13;
rng ^= rng >> 7;
rng ^= rng << 17;
(rng % bound as u64) as usize
};
if n >= 2 {
if n * (n - 1) / 2 <= hypotheses {
for a in 0..n {
for b in a + 1..n {
consider(&[a, b]);
}
}
} else {
for _ in 0..hypotheses {
let (a, b) = (next(n), next(n));
if a != b {
consider(&[a, b]);
}
}
}
}
if n >= 3 {
if n * (n - 1) * (n - 2) / 6 <= hypotheses {
for a in 0..n {
for b in a + 1..n {
for c in b + 1..n {
consider(&[a, b, c]);
}
}
}
} else {
for _ in 0..hypotheses {
let (a, b, c) = (next(n), next(n), next(n));
if a != b && b != c && a != c {
consider(&[a, b, c]);
}
}
}
}
}
fn hypothesis(c: &Candidates, sample: &[usize], scale: f64, tol: f64) -> Option<(Motion, bool)> {
let (from, to) = c.points(sample.iter().map(|&k| c.pairs[k]), scale);
let line = !spread(&from, tol);
if line && !far_apart(&from, tol) {
return None;
}
let m = horn(&from, &to)?;
if from
.iter()
.zip(&to)
.any(|(p, q)| geom::dist(m.point(*p), *q) > tol)
{
return None;
}
Some((m, line))
}
fn better(m: &Motion, count: usize, rms: f64, best: &Best, tol: f64, scale: f64) -> bool {
match best {
None => true,
Some((bm, bc, br)) => {
count > *bc
|| (count == *bc
&& (rms < br - 0.1 * tol
|| ((rms - br).abs() <= 0.1 * tol
&& distance_from_identity(m, scale)
< distance_from_identity(bm, scale) - 1e-9)))
}
}
}
fn line_through(points: &[V3]) -> Option<[V3; 2]> {
let a = points[0];
let far = points
.iter()
.copied()
.max_by(|p, q| geom::dist(*p, a).total_cmp(&geom::dist(*q, a)))
.unwrap_or(a);
let b = points
.iter()
.copied()
.max_by(|p, q| geom::dist(*p, far).total_cmp(&geom::dist(*q, far)))
.unwrap_or(a);
geom::unit(geom::sub(b, far)).map(|d| [far, d])
}
fn rotation_about(d: V3, angle: f64) -> [[f64; 3]; 3] {
let (s, c) = angle.sin_cos();
let k = [[0.0, -d[2], d[1]], [d[2], 0.0, -d[0]], [-d[1], d[0], 0.0]];
let mut r = [[0.0; 3]; 3];
for i in 0..3 {
for j in 0..3 {
r[i][j] = (1.0 - c) * d[i] * d[j] + s * k[i][j] + if i == j { c } else { 0.0 };
}
}
r
}
fn resolve_turn(
old: &Fingerprints,
new: &Fingerprints,
th: &Thresholds,
m: &Motion,
o: V3,
d: V3,
scale: f64,
) -> Option<(Motion, usize, usize)> {
let tol = th.position_tol * scale;
let cos_tol = th.angle_tol.cos();
let mut items: Vec<(usize, Anchor, Anchor)> = Vec::new();
let feature_pairs = candidates(
&old.features,
&new.features,
|a, b| same_feature(a, b, th.same_rel),
th.turn_candidates,
);
for (i, j, _) in feature_pairs {
if let (Some(a), Some(b)) = (
feature_anchor(&old.features[i]),
feature_anchor(&new.features[j]),
) {
items.push((i, a, b));
}
}
let face_pairs = candidates(
&old.faces,
&new.faces,
|a, b| same_face(a, b, th.same_rel),
th.turn_candidates,
);
for (i, j, _) in face_pairs {
items.push((
old.features.len() + i,
face_anchor(&old.faces[i]),
face_anchor(&new.faces[j]),
));
}
let split = |x: V3| {
let v = geom::sub(x, o);
let along = geom::dot(v, d);
(along, geom::sub(v, geom::scale(d, along)))
};
let mut votes: Vec<(f64, f64, usize)> = Vec::new();
for (k, (_, a, b)) in items.iter().enumerate() {
let (a1, ra) = split(m.point(a.point));
let (a2, rb) = split(b.point);
let r = geom::norm(rb);
if r <= 10.0 * tol || (a1 - a2).abs() > tol || (geom::norm(ra) - r).abs() > tol {
continue;
}
let angle = geom::dot(d, geom::cross(ra, rb)).atan2(geom::dot(ra, rb));
votes.push((angle, tol / r + th.angle_tol, k));
}
votes.sort_by(|x, y| x.0.total_cmp(&y.0));
let mut clusters: Vec<Vec<usize>> = Vec::new();
for v in 0..votes.len() {
match clusters.last_mut() {
Some(c) if votes[v].0 - votes[v - 1].0 <= votes[v].1 + votes[v - 1].1 => c.push(v),
_ => clusters.push(vec![v]),
}
}
if clusters.len() > 1 {
let (first, last) = (votes[0], votes[votes.len() - 1]);
if first.0 + 2.0 * std::f64::consts::PI - last.0 <= first.1 + last.1 {
let mut joined = clusters.pop().unwrap();
joined.append(&mut clusters[0]);
clusters[0] = joined;
}
}
let turned = |angle: f64| {
let rt = rotation_about(d, angle);
let turn = Motion { r: rt, t: [0.0; 3] };
let mut r = [[0.0; 3]; 3];
for i in 0..3 {
for j in 0..3 {
r[i][j] = (0..3).map(|k| rt[i][k] * m.r[k][j]).sum();
}
}
Motion {
r,
t: geom::add(o, turn.dir(geom::sub(m.t, o))),
}
};
let mut scored: Vec<(Motion, usize)> = Vec::new();
for c in &clusters {
let Some(&best) = c
.iter()
.min_by(|x, y| votes[**x].1.total_cmp(&votes[**y].1))
else {
continue;
};
let motion = turned(votes[best].0);
let mut keys: Vec<usize> = c
.iter()
.map(|&v| votes[v].2)
.filter(|&k| {
let (_, a, b) = &items[k];
geom::dist(motion.point(a.point), b.point) <= tol
&& match (a.dir, b.dir) {
(Some(da), Some(db)) => {
let c = geom::dot(motion.dir(da), db);
(if a.line { c.abs() } else { c }) >= cos_tol
}
_ => true,
}
})
.map(|k| items[k].0)
.collect();
keys.sort_unstable();
keys.dedup();
scored.push((motion, keys.len()));
}
let most = scored.iter().map(|s| s.1).max()?;
if most < 2 {
return None;
}
let tied: Vec<&(Motion, usize)> = scored.iter().filter(|s| s.1 == most).collect();
let chosen = tied.iter().min_by(|x, y| {
distance_from_identity(&x.0, scale).total_cmp(&distance_from_identity(&y.0, scale))
})?;
Some((chosen.0, most, tied.len()))
}
fn distance_from_identity(m: &Motion, scale: f64) -> f64 {
let trace = m.r[0][0] + m.r[1][1] + m.r[2][2];
(3.0 - trace) + geom::norm(m.t) / scale
}
fn same_motion(a: &Motion, b: &Motion, tol: f64, angle_tol: f64, scale: f64) -> bool {
let rotation = (0..3).all(|i| geom::dot(a.dir(b_axis(i)), b.dir(b_axis(i))) >= angle_tol.cos());
rotation && geom::dist(a.t, b.t) <= tol + scale * angle_tol
}
fn b_axis(i: usize) -> V3 {
let mut v = [0.0; 3];
v[i] = 1.0;
v
}
fn far_apart(points: &[V3], tol: f64) -> bool {
points
.iter()
.any(|p| geom::dist(*p, points[0]) > 10.0 * tol)
}
fn spread(points: &[V3], tol: f64) -> bool {
if points.len() < 3 {
return false;
}
let a = points[0];
let Some(far) = points
.iter()
.max_by(|p, q| geom::dist(**p, a).total_cmp(&geom::dist(**q, a)))
else {
return false;
};
let Some(u) = geom::unit(geom::sub(*far, a)) else {
return false;
};
if geom::dist(*far, a) <= 10.0 * tol {
return false;
}
points.iter().any(|p| {
let d = geom::sub(*p, a);
geom::norm(geom::sub(d, geom::scale(u, geom::dot(d, u)))) > 10.0 * tol
})
}
pub fn horn(from: &[V3], to: &[V3]) -> Option<Motion> {
if from.len() != to.len() || from.is_empty() {
return None;
}
let n = from.len() as f64;
let mean = |ps: &[V3]| geom::scale(ps.iter().fold([0.0; 3], |s, p| geom::add(s, *p)), 1.0 / n);
let (ca, cb) = (mean(from), mean(to));
let mut s = [[0.0; 3]; 3];
for (p, q) in from.iter().zip(to) {
let (a, b) = (geom::sub(*p, ca), geom::sub(*q, cb));
for i in 0..3 {
for j in 0..3 {
s[i][j] += a[i] * b[j];
}
}
}
let [[sxx, sxy, sxz], [syx, syy, syz], [szx, szy, szz]] = s;
let mut nm = [
[sxx + syy + szz, syz - szy, szx - sxz, sxy - syx],
[syz - szy, sxx - syy - szz, sxy + syx, szx + sxz],
[szx - sxz, sxy + syx, -sxx + syy - szz, syz + szy],
[sxy - syx, szx + sxz, syz + szy, -sxx - syy + szz],
];
let (values, vectors) = jacobi4(&mut nm);
let k = (0..4).max_by(|&a, &b| values[a].total_cmp(&values[b]))?;
let q = [0, 1, 2, 3].map(|i| vectors[i][k]);
let norm = q.iter().map(|x| x * x).sum::<f64>().sqrt();
if norm == 0.0 || norm.is_nan() {
return None;
}
let [w, x, y, z] = q.map(|c| c / norm);
let r = [
[
w * w + x * x - y * y - z * z,
2.0 * (x * y - w * z),
2.0 * (x * z + w * y),
],
[
2.0 * (x * y + w * z),
w * w - x * x + y * y - z * z,
2.0 * (y * z - w * x),
],
[
2.0 * (x * z - w * y),
2.0 * (y * z + w * x),
w * w - x * x - y * y + z * z,
],
];
let m = Motion { r, t: [0.0; 3] };
Some(Motion {
r,
t: geom::sub(cb, m.dir(ca)),
})
}
fn jacobi4(a: &mut [[f64; 4]; 4]) -> ([f64; 4], [[f64; 4]; 4]) {
let mut v = [[0.0; 4]; 4];
for (i, row) in v.iter_mut().enumerate() {
row[i] = 1.0;
}
for _ in 0..100 {
let off: f64 = (0..4)
.flat_map(|i| (0..4).filter(move |&j| j != i).map(move |j| (i, j)))
.map(|(i, j)| a[i][j] * a[i][j])
.sum();
let diag: f64 = (0..4).map(|i| a[i][i] * a[i][i]).sum();
if off <= 1e-30 * diag.max(1e-300) {
break;
}
for p in 0..4 {
for q in p + 1..4 {
if a[p][q].abs() < 1e-300 {
continue;
}
let theta = (a[q][q] - a[p][p]) / (2.0 * a[p][q]);
let t = theta.signum() / (theta.abs() + (theta * theta + 1.0).sqrt());
let t = if theta == 0.0 { 1.0 } else { t };
let c = 1.0 / (t * t + 1.0).sqrt();
let s = t * c;
for k in 0..4 {
let (akp, akq) = (a[k][p], a[k][q]);
a[k][p] = c * akp - s * akq;
a[k][q] = s * akp + c * akq;
}
for k in 0..4 {
let (apk, aqk) = (a[p][k], a[q][k]);
a[p][k] = c * apk - s * aqk;
a[q][k] = s * apk + c * aqk;
}
for row in v.iter_mut() {
let (vp, vq) = (row[p], row[q]);
row[p] = c * vp - s * vq;
row[q] = s * vp + c * vq;
}
}
}
}
([a[0][0], a[1][1], a[2][2], a[3][3]], v)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn horn_recovers_a_rotation_and_translation() {
let r = [[0.0, -1.0, 0.0], [0.0, 0.0, -1.0], [1.0, 0.0, 0.0]];
let m = Motion {
r,
t: [5.0, -2.0, 7.5],
};
let from = [
[0.0, 0.0, 0.0],
[10.0, 0.0, 0.0],
[0.0, 4.0, 0.0],
[1.0, 2.0, 3.0],
];
let to: Vec<V3> = from.iter().map(|p| m.point(*p)).collect();
let got = horn(&from, &to).unwrap();
for i in 0..3 {
for j in 0..3 {
assert!((got.r[i][j] - r[i][j]).abs() < 1e-12, "{got:?}");
}
assert!((got.t[i] - m.t[i]).abs() < 1e-12);
}
}
#[test]
fn collinear_points_do_not_fix_a_rotation() {
assert!(!spread(&[[0.0; 3], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]], 1e-6));
assert!(spread(&[[0.0; 3], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], 1e-6));
}
}