use alloc::vec::Vec;
use super::projection::{Rotation, deproject, project};
use super::{Direction, DistortionDirection, Solution};
pub(super) fn projective(matrix: &[[f64; 3]; 3], point: [f64; 2]) -> Option<[f64; 2]> {
let [x, y] = point;
let u = matrix[0][0] * x + matrix[0][1] * y + matrix[0][2];
let v = matrix[1][0] * x + matrix[1][1] * y + matrix[1][2];
let w = matrix[2][0] * x + matrix[2][1] * y + matrix[2][2];
if w == 0.0 || !w.is_finite() {
return None;
}
Some([u / w, v / w])
}
impl DistortionDirection {
pub fn residual(&self, point: [f64; 2], scratch: &mut Vec<f64>) -> [f64; 2] {
let mut weighted = [0.0f64; 2];
let mut total = 0.0f64;
if let Some(term) = &self.global {
let value = term.eval(point, self, scratch);
weighted[0] += value[0];
weighted[1] += value[1];
total += 1.0;
}
let mut local_coverage = 0.0f64;
let mut nearest: Option<(f64, [f64; 2])> = None;
for term in &self.local {
let dx = point[0] - term.center[0];
let dy = point[1] - term.center[1];
let t = dx.hypot(dy) / term.radius;
let weight = super::spline::wendland(t);
local_coverage += weight;
if nearest.is_none_or(|(best, _)| t < best) {
nearest = Some((t, term.spline.eval(point, self, scratch)));
}
if weight > 0.0 {
let value = term.spline.eval(point, self, scratch);
weighted[0] += weight * value[0];
weighted[1] += weight * value[1];
total += weight;
}
}
if let Some(fallback) = &self.fallback {
let weight = super::spline::wendland(local_coverage / fallback.threshold);
if weight > 0.0 {
let value = fallback.spline.eval(point, self, scratch);
weighted[0] += weight * value[0];
weighted[1] += weight * value[1];
total += weight;
}
}
if total > 0.0 {
return [weighted[0] / total, weighted[1] / total];
}
match nearest {
Some((_, value)) => value,
None => [0.0, 0.0],
}
}
}
impl Solution {
fn image_to_plane(&self, image: [f64; 2], scratch: &mut Vec<f64>) -> Option<[f64; 2]> {
if let Some(distortion) = &self.distortion {
let projective_matrix = &self.projective.as_ref()?.image_to_projection;
let base = projective(projective_matrix, image)?;
let residual = distortion.image_to_projection.residual(image, scratch);
return Some([base[0] + residual[0], base[1] + residual[1]]);
}
if let Some(transformation) = &self.projective {
return projective(&transformation.image_to_projection, image);
}
let [x0, y0] = self.projection.reference_image;
let dx = image[0] - x0;
let dy = image[1] - y0;
let m = &self.projection.linear_transformation;
Some([m[0][0] * dx + m[0][1] * dy, m[1][0] * dx + m[1][1] * dy])
}
fn plane_to_image(&self, plane: [f64; 2], scratch: &mut Vec<f64>) -> Option<[f64; 2]> {
if let Some(distortion) = &self.distortion {
let projective_matrix = &self.projective.as_ref()?.projection_to_image;
let base = projective(projective_matrix, plane)?;
let residual = distortion.projection_to_image.residual(plane, scratch);
return Some([base[0] + residual[0], base[1] + residual[1]]);
}
if let Some(transformation) = &self.projective {
return projective(&transformation.projection_to_image, plane);
}
let m = &self.projection.linear_transformation;
let det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
if det == 0.0 || !det.is_finite() {
return None;
}
let [u, v] = plane;
let dx = (m[1][1] * u - m[0][1] * v) / det;
let dy = (-m[1][0] * u + m[0][0] * v) / det;
let [x0, y0] = self.projection.reference_image;
Some([x0 + dx, y0 + dy])
}
fn rotation(&self) -> Option<Rotation> {
Rotation::new(
self.projection.reference_celestial,
self.projection
.reference_native
.unwrap_or_else(|| self.projection.system.default_reference_native()),
self.projection.celestial_pole_native,
)
}
pub fn image_to_celestial(&self, image: [f64; 2]) -> Option<[f64; 2]> {
let mut scratch = Vec::new();
let plane = self.image_to_plane(image, &mut scratch)?;
let native = deproject(plane, self.projection.system)?;
Some(super::native_to_celestial(native, self.rotation()?))
}
pub fn celestial_to_image(&self, celestial: [f64; 2]) -> Option<[f64; 2]> {
let mut scratch = Vec::new();
let native = super::celestial_to_native(celestial, self.rotation()?);
let plane = project(native, self.projection.system)?;
self.plane_to_image(plane, &mut scratch)
}
pub fn distortion_for(&self, direction: Direction) -> Option<&DistortionDirection> {
let model = self.distortion.as_ref()?;
Some(match direction {
Direction::ImageToProjection => &model.image_to_projection,
Direction::ProjectionToImage => &model.projection_to_image,
})
}
}