use crate::math::{Pose, Real, Vector};
use crate::shape::FeatureId;
#[derive(Copy, Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
pub struct PointProjection {
pub is_inside: bool,
pub point: Vector,
}
impl PointProjection {
pub fn new(is_inside: bool, point: Vector) -> Self {
PointProjection { is_inside, point }
}
pub fn transform_by(&self, pos: &Pose) -> Self {
PointProjection {
is_inside: self.is_inside,
point: pos * self.point,
}
}
pub fn is_inside_eps(&self, original_point: Vector, min_dist: Real) -> bool {
self.is_inside || (original_point - self.point).length_squared() < min_dist * min_dist
}
}
pub trait PointQuery {
fn project_local_point_with_max_dist(
&self,
pt: Vector,
solid: bool,
max_dist: Real,
) -> Option<PointProjection> {
let proj = self.project_local_point(pt, solid);
if (proj.point - pt).length() > max_dist {
None
} else {
Some(proj)
}
}
fn project_point_with_max_dist(
&self,
m: &Pose,
pt: Vector,
solid: bool,
max_dist: Real,
) -> Option<PointProjection> {
self.project_local_point_with_max_dist(m.inverse_transform_point(pt), solid, max_dist)
.map(|proj| proj.transform_by(m))
}
fn project_local_point(&self, pt: Vector, solid: bool) -> PointProjection;
fn project_local_point_and_get_feature(&self, pt: Vector) -> (PointProjection, FeatureId);
fn distance_to_local_point(&self, pt: Vector, solid: bool) -> Real {
let proj = self.project_local_point(pt, solid);
let dist = (pt - proj.point).length();
if solid || !proj.is_inside {
dist
} else {
-dist
}
}
fn contains_local_point(&self, pt: Vector) -> bool {
self.project_local_point(pt, true).is_inside
}
fn project_point(&self, m: &Pose, pt: Vector, solid: bool) -> PointProjection {
self.project_local_point(m.inverse_transform_point(pt), solid)
.transform_by(m)
}
#[inline]
fn distance_to_point(&self, m: &Pose, pt: Vector, solid: bool) -> Real {
self.distance_to_local_point(m.inverse_transform_point(pt), solid)
}
fn project_point_and_get_feature(&self, m: &Pose, pt: Vector) -> (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: &Pose, pt: Vector) -> bool {
self.contains_local_point(m.inverse_transform_point(pt))
}
}
pub trait PointQueryWithLocation {
type Location;
fn project_local_point_and_get_location(
&self,
pt: Vector,
solid: bool,
) -> (PointProjection, Self::Location);
fn project_point_and_get_location(
&self,
m: &Pose,
pt: Vector,
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)
}
fn project_local_point_and_get_location_with_max_dist(
&self,
pt: Vector,
solid: bool,
max_dist: Real,
) -> Option<(PointProjection, Self::Location)> {
let (proj, location) = self.project_local_point_and_get_location(pt, solid);
if (proj.point - pt).length() > max_dist {
None
} else {
Some((proj, location))
}
}
fn project_point_and_get_location_with_max_dist(
&self,
m: &Pose,
pt: Vector,
solid: bool,
max_dist: Real,
) -> Option<(PointProjection, Self::Location)> {
self.project_local_point_and_get_location_with_max_dist(
m.inverse_transform_point(pt),
solid,
max_dist,
)
.map(|res| (res.0.transform_by(m), res.1))
}
}