parry3d/query/intersection_test/
intersection_test_cuboid_segment.rs

1use crate::bounding_volume::Aabb;
2use crate::math::{Pose, Rotation};
3use crate::query::sat;
4use crate::shape::{Cuboid, Segment};
5
6/// Test if a segment intersects an Aabb.
7pub fn intersection_test_aabb_segment(aabb1: &Aabb, segment2: &Segment) -> bool {
8    let cuboid1 = Cuboid::new(aabb1.half_extents());
9    let pos12 = Pose::from_parts(-aabb1.center(), Rotation::IDENTITY);
10    intersection_test_cuboid_segment(&pos12, &cuboid1, segment2)
11}
12
13/// Test if a segment intersects a cuboid.
14#[inline]
15pub fn intersection_test_segment_cuboid(
16    pos12: &Pose,
17    segment1: &Segment,
18    cuboid2: &Cuboid,
19) -> bool {
20    intersection_test_cuboid_segment(&pos12.inverse(), cuboid2, segment1)
21}
22
23/// Test if a segment intersects a cuboid.
24#[inline]
25pub fn intersection_test_cuboid_segment(pos12: &Pose, cube1: &Cuboid, segment2: &Segment) -> bool {
26    let sep1 =
27        sat::cuboid_support_map_find_local_separating_normal_oneway(cube1, segment2, pos12).0;
28    if sep1 > 0.0 {
29        return false;
30    }
31
32    // This case does not exist in 3D.
33    #[cfg(feature = "dim2")]
34    {
35        let sep2 = sat::segment_cuboid_find_local_separating_normal_oneway(
36            segment2,
37            cube1,
38            &pos12.inverse(),
39        )
40        .0;
41        if sep2 > 0.0 {
42            return false;
43        }
44    }
45
46    #[cfg(feature = "dim2")]
47    return true; // This case does not exist in 2D.
48    #[cfg(feature = "dim3")]
49    {
50        let sep3 = sat::cuboid_segment_find_local_separating_edge_twoway(cube1, segment2, pos12).0;
51        sep3 <= 0.0
52    }
53}