parry3d/query/closest_points/
closest_points.rs

1use crate::math::{Isometry, Point, Real};
2
3use core::mem;
4
5/// Closest points information.
6#[derive(Debug, PartialEq, Clone, Copy)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8#[cfg_attr(
9    feature = "rkyv",
10    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize),
11    archive(check_bytes)
12)]
13pub enum ClosestPoints {
14    /// The two objects are intersecting.
15    Intersecting,
16    /// The two objects are non-intersecting but closer than a given user-defined distance.
17    WithinMargin(Point<Real>, Point<Real>),
18    /// The two objects are non-intersecting and further than a given user-defined distance.
19    Disjoint,
20}
21
22impl ClosestPoints {
23    /// Swaps the two points.
24    pub fn flip(&mut self) {
25        if let ClosestPoints::WithinMargin(ref mut p1, ref mut p2) = *self {
26            mem::swap(p1, p2)
27        }
28    }
29
30    /// Returns the result of swapping the two points if `self` is `WithinMargin`.
31    #[must_use]
32    pub fn flipped(&self) -> Self {
33        if let ClosestPoints::WithinMargin(p1, p2) = *self {
34            ClosestPoints::WithinMargin(p2, p1)
35        } else {
36            *self
37        }
38    }
39
40    /// Transform the points in `self` by `pos1` and `pos2`.
41    #[must_use]
42    pub fn transform_by(self, pos1: &Isometry<Real>, pos2: &Isometry<Real>) -> Self {
43        if let ClosestPoints::WithinMargin(p1, p2) = self {
44            ClosestPoints::WithinMargin(pos1 * p1, pos2 * p2)
45        } else {
46            self
47        }
48    }
49}