parry3d/query/point/
point_ball.rs

1use na::{self, ComplexField};
2
3use crate::math::{Point, Real};
4use crate::query::{PointProjection, PointQuery};
5use crate::shape::{Ball, FeatureId};
6
7impl PointQuery for Ball {
8    #[inline]
9    fn project_local_point(&self, pt: &Point<Real>, solid: bool) -> PointProjection {
10        let distance_squared = pt.coords.norm_squared();
11
12        let inside = distance_squared <= self.radius * self.radius;
13
14        if inside && solid {
15            PointProjection::new(true, *pt)
16        } else {
17            let proj =
18                Point::from(pt.coords * (self.radius / ComplexField::sqrt(distance_squared)));
19            PointProjection::new(inside, proj)
20        }
21    }
22
23    #[inline]
24    fn project_local_point_and_get_feature(
25        &self,
26        pt: &Point<Real>,
27    ) -> (PointProjection, FeatureId) {
28        (self.project_local_point(pt, false), FeatureId::Face(0))
29    }
30
31    #[inline]
32    fn distance_to_local_point(&self, pt: &Point<Real>, solid: bool) -> Real {
33        let dist = pt.coords.norm() - self.radius;
34
35        if solid && dist < 0.0 {
36            0.0
37        } else {
38            dist
39        }
40    }
41
42    #[inline]
43    fn contains_local_point(&self, pt: &Point<Real>) -> bool {
44        pt.coords.norm_squared() <= self.radius * self.radius
45    }
46}