use super::Constraint;
pub struct Ball2 {
centre: Option<Vec<f64>>,
radius: f64,
}
impl Ball2 {
pub fn new_at_origin_with_radius(radius_: f64) -> Ball2 {
assert!(radius_ > 0.0);
Ball2 {
centre: None,
radius: radius_,
}
}
pub fn new(centre_: Vec<f64>, radius_: f64) -> Ball2 {
assert!(radius_ > 0.0);
Ball2 {
centre: Some(centre_),
radius: radius_,
}
}
}
impl Constraint for Ball2 {
fn project(&self, x: &mut [f64]) {
if let Some(centre) = &self.centre {
let mut norm_difference = 0.0;
x.iter().zip(centre.iter()).for_each(|(a, b)| {
let diff_ = *a - *b;
norm_difference += diff_ * diff_
});
norm_difference = norm_difference.sqrt();
if norm_difference > self.radius {
x.iter_mut().zip(centre.iter()).for_each(|(x, c)| {
*x = *c + (*x - *c) / norm_difference;
});
}
} else {
let norm_x = crate::matrix_operations::norm2(x);
if norm_x > self.radius {
let norm_over_radius = norm_x / self.radius;
x.iter_mut().for_each(|x_| *x_ /= norm_over_radius);
}
}
}
}