use crate::math::{Isometry, Point, Real};
use crate::shape::FeatureId;
use na;
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PointProjection {
pub is_inside: bool,
pub point: Point<Real>,
}
impl PointProjection {
pub fn new(is_inside: bool, point: Point<Real>) -> Self {
PointProjection { is_inside, point }
}
pub fn transform_by(&self, pos: &Isometry<Real>) -> Self {
PointProjection {
is_inside: self.is_inside,
point: pos * self.point,
}
}
}
pub trait PointQuery {
fn project_local_point(&self, pt: &Point<Real>, solid: bool) -> PointProjection;
fn project_local_point_and_get_feature(&self, pt: &Point<Real>)
-> (PointProjection, FeatureId);
fn distance_to_local_point(&self, pt: &Point<Real>, solid: bool) -> Real {
let proj = self.project_local_point(pt, solid);
let dist = na::distance(pt, &proj.point);
if solid || !proj.is_inside {
dist
} else {
-dist
}
}
fn contains_local_point(&self, pt: &Point<Real>) -> bool {
self.project_local_point(pt, false).is_inside
}
fn project_point(&self, m: &Isometry<Real>, pt: &Point<Real>, solid: bool) -> PointProjection {
self.project_local_point(&m.inverse_transform_point(pt), solid)
.transform_by(m)
}
#[inline]
fn distance_to_point(&self, m: &Isometry<Real>, pt: &Point<Real>, solid: bool) -> Real {
self.distance_to_local_point(&m.inverse_transform_point(pt), solid)
}
fn project_point_and_get_feature(
&self,
m: &Isometry<Real>,
pt: &Point<Real>,
) -> (PointProjection, FeatureId) {
let res = self.project_local_point_and_get_feature(&m.inverse_transform_point(pt));
(res.0.transform_by(m), res.1)
}
#[inline]
fn contains_point(&self, m: &Isometry<Real>, pt: &Point<Real>) -> bool {
self.contains_local_point(&m.inverse_transform_point(pt))
}
}
pub trait PointQueryWithLocation {
type Location;
fn project_local_point_and_get_location(
&self,
pt: &Point<Real>,
solid: bool,
) -> (PointProjection, Self::Location);
fn project_point_and_get_location(
&self,
m: &Isometry<Real>,
pt: &Point<Real>,
solid: bool,
) -> (PointProjection, Self::Location) {
let res = self.project_local_point_and_get_location(&m.inverse_transform_point(pt), solid);
(res.0.transform_by(m), res.1)
}
}