use crate::math::Vector;
use crate::query::{PointProjection, PointQuery, PointQueryWithLocation};
use crate::shape::{FeatureId, Segment, SegmentPointLocation};
use crate::utils::relative_eq_vector;
impl PointQuery for Segment {
#[inline]
fn project_local_point(&self, pt: Vector, solid: bool) -> PointProjection {
self.project_local_point_and_get_location(pt, solid).0
}
#[inline]
fn project_local_point_and_get_feature(&self, pt: Vector) -> (PointProjection, FeatureId) {
let (proj, loc) = self.project_local_point_and_get_location(pt, false);
let feature = match loc {
SegmentPointLocation::OnVertex(i) => FeatureId::Vertex(i),
SegmentPointLocation::OnEdge(..) => {
#[cfg(feature = "dim2")]
{
let dir = self.scaled_direction();
let dpt = pt - proj.point;
if dpt.perp_dot(dir) >= 0.0 {
FeatureId::Face(0)
} else {
FeatureId::Face(1)
}
}
#[cfg(feature = "dim3")]
{
FeatureId::Edge(0)
}
}
};
(proj, feature)
}
}
impl PointQueryWithLocation for Segment {
type Location = SegmentPointLocation;
#[inline]
fn project_local_point_and_get_location(
&self,
pt: Vector,
_: bool,
) -> (PointProjection, Self::Location) {
let ab = self.b - self.a;
let ap = pt - self.a;
let ab_ap = ab.dot(ap);
let sqnab = ab.length_squared();
let (location, proj) = if ab_ap <= 0.0 {
(SegmentPointLocation::OnVertex(0), self.a)
} else if ab_ap >= sqnab {
(SegmentPointLocation::OnVertex(1), self.b)
} else {
assert!(sqnab != 0.0);
let u = ab_ap / sqnab;
let bcoords = [1.0 - u, u];
(SegmentPointLocation::OnEdge(bcoords), self.a + ab * u)
};
let inside = relative_eq_vector(proj, pt);
(PointProjection::new(inside, proj), location)
}
}