use crate::containers::mesh::Mesh;
use crate::error::{PyrucastError, Result};
pub fn project_on_sphere(mesh: &Mesh, center: &[f64], radius: f64) -> Result<Mesh> {
if !radius.is_finite() || radius <= 0.0 {
return Err(PyrucastError::Message(format!(
"project_on_sphere: radius must be positive and finite, got {radius}"
)));
}
let dim = mesh.coords()?.read().dim() as usize;
if center.len() != dim {
return Err(PyrucastError::Message(format!(
"project_on_sphere: center has {} components but the mesh is {dim}-D",
center.len()
)));
}
super::transform::map_coords(mesh, false, |p, out| {
let distance = p
.iter()
.zip(center)
.map(|(x, c)| (x - c) * (x - c))
.sum::<f64>()
.sqrt();
if distance == 0.0 {
return Err(PyrucastError::Message(format!(
"project_on_sphere: the node at {p:?} sits on the centre, it has no direction \
to be projected along"
)));
}
let scale = radius / distance;
out.extend(p.iter().zip(center).map(|(x, c)| c + scale * (x - c)));
Ok(())
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, Node, NodeId};
use crate::coords::Coords;
use crate::handle::Handle;
use crate::ops::mesh::{circle, line, to_quadratic};
use std::collections::HashSet;
fn distinct(mesh: &Mesh) -> HashSet<NodeId> {
mesh.get(0)
.unwrap()
.read()
.connectivity()
.iter()
.copied()
.collect()
}
#[test]
fn every_node_lands_at_the_radius_and_sharing_is_kept() {
let coords = Handle::new(Coords::new(3).unwrap());
let p = |x: &[f64]| Node::create_in(coords.clone(), x).unwrap();
let source = to_quadratic(
&line(
&p(&[3.0, -1.0, 0.5]),
&p(&[3.0, 4.0, 2.0]),
5,
ElementType::SEG2,
)
.unwrap(),
)
.unwrap();
let c = [1.0, 1.0, 1.0];
let projected = project_on_sphere(&source, &c, 0.75).unwrap();
assert_eq!(projected.element_types().unwrap(), vec![ElementType::SEG3]);
assert_eq!(projected.cell_count(), source.cell_count());
assert_eq!(distinct(&projected).len(), distinct(&source).len());
assert!(distinct(&projected).is_disjoint(&distinct(&source)));
for cell in 0..5 {
for k in 0..3 {
let x = projected.node(0, cell, k).unwrap().position().unwrap();
let r = x
.iter()
.zip(&c)
.map(|(a, b)| (a - b) * (a - b))
.sum::<f64>()
.sqrt();
assert!((r - 0.75).abs() < 1e-12, "cell {cell}, node {k}: r = {r}");
}
}
}
#[test]
fn a_closed_loop_stays_closed_and_a_node_on_the_sphere_stays_put() {
let coords = Handle::new(Coords::new(2).unwrap());
let o = Node::create_in(coords.clone(), &[0.5, -0.5]).unwrap();
let ring = circle(&o, &[0.0, 0.0, 1.0], 2.0, 8, ElementType::SEG2).unwrap();
let same = project_on_sphere(&ring, &[0.5, -0.5], 2.0).unwrap();
assert_eq!(distinct(&same).len(), 8);
assert_eq!(
same.node(0, 7, 1).unwrap().id(),
same.node(0, 0, 0).unwrap().id()
);
for cell in 0..8 {
let (a, b) = (
ring.node(0, cell, 0).unwrap().position().unwrap(),
same.node(0, cell, 0).unwrap().position().unwrap(),
);
assert!((a[0] - b[0]).abs() < 1e-15 && (a[1] - b[1]).abs() < 1e-15);
}
}
#[test]
fn bad_radius_wrong_centre_and_a_node_at_the_centre_are_refused() {
let coords = Handle::new(Coords::new(2).unwrap());
let p = |x: &[f64]| Node::create_in(coords.clone(), x).unwrap();
let through_origin = line(&p(&[0.0, 0.0]), &p(&[1.0, 0.0]), 1, ElementType::SEG2).unwrap();
assert!(project_on_sphere(&through_origin, &[0.0, 5.0], 0.0).is_err());
assert!(project_on_sphere(&through_origin, &[0.0, 5.0], f64::NAN).is_err());
assert!(project_on_sphere(&through_origin, &[0.0, 5.0, 0.0], 1.0).is_err());
let before = coords.read().capacity();
assert!(project_on_sphere(&through_origin, &[0.0, 0.0], 1.0).is_err());
assert_eq!(
coords.read().capacity(),
before,
"a refused projection creates no node"
);
}
}