parry3d/query/clip/
clip_aabb_polygon.rs

1use crate::bounding_volume::Aabb;
2use crate::math::{Point, Real, Vector};
3use alloc::vec::Vec;
4
5impl Aabb {
6    /// Computes the intersections between this Aabb and the given polygon.
7    ///
8    /// The results is written into `points` directly. The input points are
9    /// assumed to form a convex polygon where all points lie on the same plane.
10    /// In order to avoid internal allocations, uses `self.clip_polygon_with_workspace`
11    /// instead.
12    #[inline]
13    pub fn clip_polygon(&self, points: &mut Vec<Point<Real>>) {
14        let mut workspace = Vec::new();
15        self.clip_polygon_with_workspace(points, &mut workspace)
16    }
17
18    /// Computes the intersections between this Aabb and the given polygon.
19    ///
20    /// The results is written into `points` directly. The input points are
21    /// assumed to form a convex polygon where all points lie on the same plane.
22    #[inline]
23    pub fn clip_polygon_with_workspace(
24        &self,
25        points: &mut Vec<Point<Real>>,
26        workspace: &mut Vec<Point<Real>>,
27    ) {
28        super::clip_halfspace_polygon(&self.mins, &-Vector::x(), points, workspace);
29        super::clip_halfspace_polygon(&self.maxs, &Vector::x(), workspace, points);
30
31        super::clip_halfspace_polygon(&self.mins, &-Vector::y(), points, workspace);
32        super::clip_halfspace_polygon(&self.maxs, &Vector::y(), workspace, points);
33
34        #[cfg(feature = "dim3")]
35        {
36            super::clip_halfspace_polygon(&self.mins, &-Vector::z(), points, workspace);
37            super::clip_halfspace_polygon(&self.maxs, &Vector::z(), workspace, points);
38        }
39    }
40}