1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::bounding_volume::Aabb;
use crate::math::{Point, Real, Vector};

impl Aabb {
    /// Computes the intersections between this Aabb and the given polygon.
    ///
    /// The results is written into `points` directly. The input points are
    /// assumed to form a convex polygon where all points lie on the same plane.
    /// In order to avoid internal allocations, uses `self.clip_polygon_with_workspace`
    /// instead.
    #[inline]
    pub fn clip_polygon(&self, points: &mut Vec<Point<Real>>) {
        let mut workspace = Vec::new();
        self.clip_polygon_with_workspace(points, &mut workspace)
    }

    /// Computes the intersections between this Aabb and the given polygon.
    ///
    /// The results is written into `points` directly. The input points are
    /// assumed to form a convex polygon where all points lie on the same plane.
    #[inline]
    pub fn clip_polygon_with_workspace(
        &self,
        points: &mut Vec<Point<Real>>,
        workspace: &mut Vec<Point<Real>>,
    ) {
        super::clip_halfspace_polygon(&self.mins, &-Vector::x(), points, workspace);
        super::clip_halfspace_polygon(&self.maxs, &Vector::x(), workspace, points);

        super::clip_halfspace_polygon(&self.mins, &-Vector::y(), points, workspace);
        super::clip_halfspace_polygon(&self.maxs, &Vector::y(), workspace, points);

        #[cfg(feature = "dim3")]
        {
            super::clip_halfspace_polygon(&self.mins, &-Vector::z(), points, workspace);
            super::clip_halfspace_polygon(&self.maxs, &Vector::z(), workspace, points);
        }
    }
}