parry3d/query/sat/sat_cuboid_point.rs
1use crate::math::{Isometry, Point, Real, Vector};
2use crate::shape::{Cuboid, SupportMap};
3
4use na::Unit;
5
6/// Computes the separation distance between a point and a cuboid along a specified normal direction.
7///
8/// This function is used in SAT (Separating Axis Theorem) implementations for shapes that can be
9/// treated as having a single representative point with an associated normal vector. Examples include:
10/// - Segments (in 2D, using one endpoint and the segment's normal)
11/// - Triangles (in 3D, using one vertex and the triangle's face normal)
12///
13/// # Why This Works
14///
15/// For a cuboid centered at the origin with symmetry, we only need to test one direction of the
16/// normal (not both +normal and -normal) because the cuboid looks the same from both directions.
17/// This optimization makes the function more efficient than the general support map approach.
18///
19/// # Parameters
20///
21/// - `point1`: A point in the first shape's local coordinate space
22/// - `normal1`: Optional unit normal vector associated with the point (e.g., triangle face normal)
23///   - If `None`, the function returns maximum negative separation (indicating overlap)
24/// - `shape2`: The cuboid to test against
25/// - `pos12`: The position of `shape2` (cuboid) relative to `point1`'s coordinate frame
26///
27/// # Returns
28///
29/// A tuple containing:
30/// - `Real`: The separation distance along the normal direction
31///   - **Positive**: The point and cuboid are separated
32///   - **Negative**: The point penetrates the cuboid (or normal is None)
33///   - **Zero**: The point exactly touches the cuboid surface
34/// - `Vector<Real>`: The oriented normal direction used for the test (pointing from point toward cuboid)
35///
36/// # Example
37///
38/// ```rust
39/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
40/// use parry2d::shape::Cuboid;
41/// use parry2d::query::sat::point_cuboid_find_local_separating_normal_oneway;
42/// use nalgebra::{Point2, Vector2, Isometry2, Unit};
43///
44/// let point = Point2::origin();
45/// let normal = Some(Unit::new_normalize(Vector2::x()));
46/// let cuboid = Cuboid::new(Vector2::new(1.0, 1.0));
47///
48/// // Position cuboid 3 units to the right
49/// let pos12 = Isometry2::translation(3.0, 0.0);
50///
51/// let (separation, _dir) = point_cuboid_find_local_separating_normal_oneway(
52///     point,
53///     normal,
54///     &cuboid,
55///     &pos12
56/// );
57///
58/// // Should be separated by 1.0 (distance 3.0 - cuboid extent 1.0 - point distance 0.0)
59/// assert!(separation > 0.0);
60/// # }
61/// ```
62///
63/// # Implementation Note
64///
65/// This function only works correctly when the **cuboid is on the right-hand side** (as shape2)
66/// because it exploits the cuboid's symmetry around the origin. The cuboid must be centered at
67/// its local origin for this optimization to be valid.
68pub fn point_cuboid_find_local_separating_normal_oneway(
69    point1: Point<Real>,
70    normal1: Option<Unit<Vector<Real>>>,
71    shape2: &Cuboid,
72    pos12: &Isometry<Real>,
73) -> (Real, Vector<Real>) {
74    let mut best_separation = -Real::MAX;
75    let mut best_dir = Vector::zeros();
76
77    if let Some(normal1) = normal1 {
78        let axis1 = if (pos12.translation.vector - point1.coords).dot(&normal1) >= 0.0 {
79            normal1
80        } else {
81            -normal1
82        };
83
84        let pt2 = shape2.support_point_toward(pos12, &-axis1);
85        let separation = (pt2 - point1).dot(&axis1);
86
87        if separation > best_separation {
88            best_separation = separation;
89            best_dir = *axis1;
90        }
91    }
92
93    (best_separation, best_dir)
94}