parry2d/query/gjk/
gjk.rs

1//! The Gilbert-Johnson-Keerthi distance algorithm.
2//!
3//! # What is GJK?
4//!
5//! The **Gilbert-Johnson-Keerthi (GJK)** algorithm is a fundamental geometric algorithm used
6//! to compute the distance between two convex shapes. It's one of the most important algorithms
7//! in collision detection and is used extensively in physics engines, robotics, and computer graphics.
8//!
9//! ## How GJK Works (Simplified)
10//!
11//! GJK works by operating on the **Minkowski difference** (also called Configuration Space Obstacle or CSO)
12//! of two shapes. Instead of directly comparing the shapes, GJK:
13//!
14//! 1. Constructs a simplex (triangle in 2D, tetrahedron in 3D) within the Minkowski difference
15//! 2. Iteratively refines this simplex to find the point closest to the origin
16//! 3. The distance from the origin to this closest point equals the distance between the shapes
17//!
18//! If the origin is **inside** the Minkowski difference, the shapes are intersecting.
19//! If the origin is **outside**, the distance to the closest point gives the separation distance.
20//!
21//! ## When is GJK Used?
22//!
23//! GJK is used whenever you need to:
24//! - **Check if two convex shapes intersect** (collision detection)
25//! - **Find the minimum distance** between two convex shapes
26//! - **Compute closest points** on two shapes
27//! - **Cast a shape along a direction** to find the time of impact (continuous collision detection)
28//!
29//! ## Key Advantages of GJK
30//!
31//! - Works with **any convex shape** that can provide a support function
32//! - Does **not require the full geometry** of the shapes (only support points)
33//! - Very **fast convergence** in most practical cases
34//! - Forms the basis for many collision detection systems
35//!
36//! ## Limitations
37//!
38//! - Only works with **convex shapes** (use convex decomposition for concave shapes)
39//! - When shapes are penetrating, GJK can only detect intersection but not penetration depth
40//!   (use EPA - Expanding Polytope Algorithm - for penetration depth)
41//!
42//! # Main Functions in This Module
43//!
44//! - [`closest_points`] - The core GJK algorithm for finding distance and closest points
45//! - [`project_origin`] - Projects the origin onto a shape's boundary
46//! - [`cast_local_ray`] - Casts a ray against a shape (used for raycasting)
47//! - [`directional_distance`] - Computes how far a shape can move before touching another
48//!
49//! # Example
50//!
51//! See individual function documentation for usage examples.
52
53use crate::math::ComplexField;
54
55use crate::query::gjk::{ConstantOrigin, CsoPoint, VoronoiSimplex};
56use crate::shape::SupportMap;
57// use query::Proximity;
58use crate::math::{Pose, Real, Vector, DIM};
59use crate::query::{self, Ray};
60
61use num::{Bounded, Zero};
62
63/// Results of the GJK algorithm.
64///
65/// This enum represents the different outcomes when running the GJK algorithm to find
66/// the distance between two shapes. The result depends on whether the shapes are intersecting,
67/// how far apart they are, and what information was requested.
68///
69/// # Understanding the Results
70///
71/// - **Intersection**: The shapes are overlapping. The origin lies inside the Minkowski difference.
72/// - **ClosestPoints**: The exact closest points on both shapes were computed, along with the
73///   separation direction.
74/// - **Proximity**: The shapes are close but not intersecting. Only an approximate separation
75///   direction is provided (used when exact distance computation is not needed).
76/// - **NoIntersection**: The shapes are too far apart (beyond the specified `max_dist` threshold).
77///
78/// # Coordinate Spaces
79///
80/// All points and vectors in this result are expressed in the **local-space of the first shape**
81/// (the shape passed as `g1` to the GJK functions). This is important when working with
82/// transformed shapes.
83#[derive(Clone, Debug, PartialEq)]
84pub enum GJKResult {
85    /// The shapes are intersecting (overlapping).
86    ///
87    /// This means the origin is inside the Minkowski difference of the two shapes.
88    /// GJK cannot compute penetration depth; use the EPA (Expanding Polytope Algorithm)
89    /// for that purpose.
90    Intersection,
91
92    /// The closest points on both shapes were found.
93    ///
94    /// # Fields
95    ///
96    /// - First `Vector`: The closest point on the first shape (in local-space of shape 1)
97    /// - Second `Vector`: The closest point on the second shape (in local-space of shape 1)
98    /// - `Vector`: The unit direction vector from shape 1 to shape 2 (separation axis)
99    ///
100    /// This variant is returned when `exact_dist` is `true` in the GJK algorithm and the
101    /// shapes are not intersecting.
102    ClosestPoints(Vector, Vector, Vector),
103
104    /// The shapes are in proximity (close but not intersecting).
105    ///
106    /// # Fields
107    ///
108    /// - `Vector`: An approximate separation axis (unit direction from shape 1 to shape 2)
109    ///
110    /// This variant is returned when `exact_dist` is `false` and the algorithm determines
111    /// the shapes are close but not intersecting. It's faster than computing exact closest
112    /// points when you only need to know if shapes are nearby.
113    Proximity(Vector),
114
115    /// The shapes are too far apart.
116    ///
117    /// # Fields
118    ///
119    /// - `Vector`: A separation axis (unit direction from shape 1 to shape 2)
120    ///
121    /// This variant is returned when the minimum distance between the shapes exceeds
122    /// the `max_dist` parameter passed to the GJK algorithm.
123    NoIntersection(Vector),
124}
125
126/// The absolute tolerance used by the GJK algorithm.
127///
128/// This function returns the epsilon (tolerance) value that GJK uses to determine when
129/// it has converged to a solution. The tolerance affects:
130///
131/// - When two points are considered "close enough" to be the same
132/// - When the algorithm decides it has found the minimum distance
133/// - Numerical stability in edge cases
134///
135/// The returned value is 10 times the default machine epsilon for the current floating-point
136/// precision (f32 or f64). This provides a balance between accuracy and robustness.
137///
138/// # Returns
139///
140/// The absolute tolerance value (10 * DEFAULT_EPSILON)
141pub fn eps_tol() -> Real {
142    let _eps = crate::math::DEFAULT_EPSILON;
143    _eps * 10.0
144}
145
146/// Projects the origin onto the boundary of the given shape.
147///
148/// This function finds the point on the shape's surface that is closest to the origin (0, 0)
149/// in 2D or (0, 0, 0) in 3D. This is useful for distance queries and collision detection
150/// when you need to know the closest point on a shape.
151///
152/// # Important: Origin Must Be Outside
153///
154/// **The origin is assumed to be outside of the shape.** If the origin is inside the shape,
155/// this function returns `None`. For penetrating cases, use the EPA (Expanding Polytope Algorithm)
156/// instead.
157///
158/// # Parameters
159///
160/// - `m`: The position and orientation (isometry) of the shape in world space
161/// - `g`: The shape to project onto (must implement `SupportMap`)
162/// - `simplex`: A reusable simplex structure for the GJK algorithm. Initialize with
163///   `VoronoiSimplex::new()` before first use.
164///
165/// # Returns
166///
167/// - `Some(Vector)`: The closest point on the shape's boundary, in the shape's **local space**
168/// - `None`: If the origin is inside the shape
169///
170/// # Example
171///
172/// ```rust
173/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
174/// use parry3d::shape::Ball;
175/// use parry3d::query::gjk::{project_origin, VoronoiSimplex};
176/// use parry3d::math::Pose;
177///
178/// // Create a ball at position (5, 0, 0)
179/// let ball = Ball::new(1.0);
180/// let position = Pose::translation(5.0, 0.0, 0.0);
181///
182/// // Project the origin onto the ball
183/// let mut simplex = VoronoiSimplex::new();
184/// if let Some(closest_point) = project_origin(&position, &ball, &mut simplex) {
185///     println!("Closest point on ball: {:?}", closest_point);
186///     // The point will be approximately (-1, 0, 0) in local space
187///     // which is the left side of the ball facing the origin
188/// }
189/// # }
190/// ```
191///
192/// # Performance Note
193///
194/// The `simplex` parameter can be reused across multiple calls to avoid allocations.
195/// This is particularly beneficial when performing many projection queries.
196pub fn project_origin<G: ?Sized + SupportMap>(
197    m: &Pose,
198    g: &G,
199    simplex: &mut VoronoiSimplex,
200) -> Option<Vector> {
201    match closest_points(
202        &m.inverse(),
203        g,
204        &ConstantOrigin,
205        Real::max_value(),
206        true,
207        simplex,
208    ) {
209        GJKResult::Intersection => None,
210        GJKResult::ClosestPoints(p, _, _) => Some(p),
211        _ => unreachable!(),
212    }
213}
214
215/*
216 * Separating Axis GJK
217 */
218/// Computes the closest points between two shapes using the GJK algorithm.
219///
220/// This is the **core function** of the GJK implementation in Parry. It can compute:
221/// - Whether two shapes are intersecting
222/// - The distance between two separated shapes
223/// - The closest points on both shapes
224/// - An approximate separation axis when exact distance isn't needed
225///
226/// # How It Works
227///
228/// The algorithm operates on the Minkowski difference (CSO) of the two shapes and iteratively
229/// builds a simplex that approximates the point closest to the origin. The algorithm terminates
230/// when:
231/// - The shapes are proven to intersect (origin is inside the CSO)
232/// - The minimum distance is found within the tolerance
233/// - The shapes are proven to be farther than `max_dist` apart
234///
235/// # Parameters
236///
237/// - `pos12`: The relative position of shape 2 with respect to shape 1. This is the isometry
238///   that transforms from shape 1's space to shape 2's space.
239/// - `g1`: The first shape (must implement `SupportMap`)
240/// - `g2`: The second shape (must implement `SupportMap`)
241/// - `max_dist`: Maximum distance to check. If shapes are farther than this, the algorithm
242///   returns `GJKResult::NoIntersection` early. Use `Real::max_value()` to disable this check.
243/// - `exact_dist`: Whether to compute exact closest points:
244///   - `true`: Computes exact distance and returns `GJKResult::ClosestPoints`
245///   - `false`: May return `GJKResult::Proximity` with only an approximate separation axis,
246///     which is faster when you only need to know if shapes are nearby
247/// - `simplex`: A reusable simplex structure. Initialize with `VoronoiSimplex::new()` before
248///   first use. Can be reused across calls for better performance.
249///
250/// # Returns
251///
252/// Returns a [`GJKResult`] which can be:
253/// - `Intersection`: The shapes are overlapping
254/// - `ClosestPoints(p1, p2, normal)`: The closest points on each shape (when `exact_dist` is true)
255/// - `Proximity(axis)`: An approximate separation axis (when `exact_dist` is false)
256/// - `NoIntersection(axis)`: The shapes are farther than `max_dist` apart
257///
258/// # Example: Basic Distance Query
259///
260/// ```rust
261/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
262/// use parry3d::shape::{Ball, Cuboid};
263/// use parry3d::query::gjk::{closest_points, VoronoiSimplex};
264/// use parry3d::math::{Pose, Vector};
265///
266/// // Create two shapes
267/// let ball = Ball::new(1.0);
268/// let cuboid = Cuboid::new(Vector::new(1.0, 1.0, 1.0));
269///
270/// // Position them in space
271/// let pos1 = Pose::translation(0.0, 0.0, 0.0);
272/// let pos2 = Pose::translation(5.0, 0.0, 0.0);
273///
274/// // Compute relative position
275/// let pos12 = pos1.inv_mul(&pos2);
276///
277/// // Run GJK
278/// let mut simplex = VoronoiSimplex::new();
279/// let result = closest_points(
280///     &pos12,
281///     &ball,
282///     &cuboid,
283///     f32::MAX,  // No distance limit
284///     true,      // Compute exact distance
285///     &mut simplex,
286/// );
287///
288/// match result {
289///     parry3d::query::gjk::GJKResult::ClosestPoints(p1, p2, normal) => {
290///         println!("Closest point on ball: {:?}", p1);
291///         println!("Closest point on cuboid: {:?}", p2);
292///         println!("Separation direction: {:?}", normal);
293///         let distance = (p2 - p1).length();
294///         println!("Distance: {}", distance);
295///     }
296///     parry3d::query::gjk::GJKResult::Intersection => {
297///         println!("Shapes are intersecting!");
298///     }
299///     _ => {}
300/// }
301/// # }
302/// ```
303///
304/// # Example: Fast Proximity Check
305///
306/// ```rust
307/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
308/// use parry3d::shape::Ball;
309/// use parry3d::query::gjk::{closest_points, VoronoiSimplex};
310/// use parry3d::math::Pose;
311///
312/// let ball1 = Ball::new(1.0);
313/// let ball2 = Ball::new(1.0);
314/// let pos12 = Pose::translation(3.0, 0.0, 0.0);
315///
316/// let mut simplex = VoronoiSimplex::new();
317/// let result = closest_points(
318///     &pos12,
319///     &ball1,
320///     &ball2,
321///     5.0,       // Only check up to distance 5.0
322///     false,     // Don't compute exact distance
323///     &mut simplex,
324/// );
325///
326/// match result {
327///     parry3d::query::gjk::GJKResult::Proximity(_axis) => {
328///         println!("Shapes are close but not intersecting");
329///     }
330///     parry3d::query::gjk::GJKResult::Intersection => {
331///         println!("Shapes are intersecting");
332///     }
333///     parry3d::query::gjk::GJKResult::NoIntersection(_) => {
334///         println!("Shapes are too far apart (> 5.0 units)");
335///     }
336///     _ => {}
337/// }
338/// # }
339/// ```
340///
341/// # Performance Tips
342///
343/// 1. Reuse the `simplex` across multiple queries to avoid allocations
344/// 2. Set `exact_dist` to `false` when you only need proximity information
345/// 3. Use a reasonable `max_dist` to allow early termination
346/// 4. GJK converges fastest when shapes are well-separated
347///
348/// # Notes
349///
350/// - All returned points and vectors are in the local-space of shape 1
351/// - The algorithm typically converges in 5-10 iterations for well-separated shapes
352/// - Maximum iteration count is 100 to prevent infinite loops
353pub fn closest_points<G1, G2>(
354    pos12: &Pose,
355    g1: &G1,
356    g2: &G2,
357    max_dist: Real,
358    exact_dist: bool,
359    simplex: &mut VoronoiSimplex,
360) -> GJKResult
361where
362    G1: ?Sized + SupportMap,
363    G2: ?Sized + SupportMap,
364{
365    let _eps = crate::math::DEFAULT_EPSILON;
366    let _eps_tol: Real = eps_tol();
367    let _eps_rel: Real = <Real as ComplexField>::sqrt(_eps_tol);
368
369    // TODO: reset the simplex if it is empty?
370    let mut proj = simplex.project_origin_and_reduce();
371
372    let mut old_dir;
373
374    if let Some(proj_dir) = proj.try_normalize() {
375        old_dir = -proj_dir;
376    } else {
377        return GJKResult::Intersection;
378    }
379
380    let mut max_bound = Real::max_value();
381    let mut dir;
382    let mut niter = 0;
383
384    loop {
385        let old_max_bound = max_bound;
386
387        let neg_proj_coords = -proj;
388        let (normalized, dist) = neg_proj_coords.normalize_and_length();
389        if dist > _eps_tol {
390            dir = normalized;
391            max_bound = dist;
392        } else {
393            // The origin is on the simplex.
394            return GJKResult::Intersection;
395        }
396
397        if max_bound >= old_max_bound {
398            if exact_dist {
399                let (p1, p2) = result(simplex, true);
400                return GJKResult::ClosestPoints(p1, p2, old_dir); // upper bounds inconsistencies
401            } else {
402                return GJKResult::Proximity(old_dir);
403            }
404        }
405
406        let cso_point = CsoPoint::from_shapes(pos12, g1, g2, dir);
407        let min_bound = -dir.dot(cso_point.point);
408
409        assert!(min_bound.is_finite());
410
411        if min_bound > max_dist {
412            return GJKResult::NoIntersection(dir);
413        } else if !exact_dist && min_bound > 0.0 && max_bound <= max_dist {
414            return GJKResult::Proximity(old_dir);
415        } else if max_bound - min_bound <= _eps_rel * max_bound {
416            if exact_dist {
417                let (p1, p2) = result(simplex, false);
418                return GJKResult::ClosestPoints(p1, p2, dir); // the distance found has a good enough precision
419            } else {
420                return GJKResult::Proximity(dir);
421            }
422        }
423
424        if !simplex.add_point(cso_point) {
425            if exact_dist {
426                let (p1, p2) = result(simplex, false);
427                return GJKResult::ClosestPoints(p1, p2, dir);
428            } else {
429                return GJKResult::Proximity(dir);
430            }
431        }
432
433        old_dir = dir;
434        proj = simplex.project_origin_and_reduce();
435
436        if simplex.dimension() == DIM {
437            if min_bound >= _eps_tol {
438                if exact_dist {
439                    let (p1, p2) = result(simplex, true);
440                    return GJKResult::ClosestPoints(p1, p2, old_dir);
441                } else {
442                    // NOTE: previous implementation used old_proj here.
443                    return GJKResult::Proximity(old_dir);
444                }
445            } else {
446                return GJKResult::Intersection; // Vector inside of the cso.
447            }
448        }
449        niter += 1;
450
451        if niter == 100 {
452            return GJKResult::NoIntersection(Vector::X);
453        }
454    }
455}
456
457/// Casts a ray against a shape using the GJK algorithm.
458///
459/// This function performs raycasting by testing a ray against a shape to find if and where
460/// the ray intersects the shape. It uses a specialized version of GJK that works with rays.
461///
462/// # What is Raycasting?
463///
464/// Raycasting shoots a ray (infinite line starting from a point in a direction) and finds
465/// where it first hits a shape. This is essential for:
466/// - Mouse picking in 3D scenes
467/// - Line-of-sight checks
468/// - Projectile collision detection
469/// - Laser/scanner simulations
470///
471/// # Parameters
472///
473/// - `shape`: The shape to cast the ray against (must implement `SupportMap`)
474/// - `simplex`: A reusable simplex structure. Initialize with `VoronoiSimplex::new()`.
475/// - `ray`: The ray to cast, containing an origin point and direction vector
476/// - `max_time_of_impact`: Maximum distance along the ray to check. The ray will be treated
477///   as a line segment of length `max_time_of_impact * ray.dir.length()`.
478///
479/// # Returns
480///
481/// - `Some((toi, normal))`: If the ray hits the shape
482///   - `toi`: Time of impact - multiply by `ray.dir.length()` to get the actual distance
483///   - `normal`: Surface normal at the hit point
484/// - `None`: If the ray doesn't hit the shape within the maximum distance
485///
486/// # Example
487///
488/// ```rust
489/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
490/// use parry3d::shape::Ball;
491/// use parry3d::query::{Ray, gjk::{cast_local_ray, VoronoiSimplex}};
492/// use parry3d::math::Vector;
493///
494/// // Create a ball at the origin
495/// let ball = Ball::new(1.0);
496///
497/// // Create a ray starting at (0, 0, -5) pointing toward +Z
498/// let ray = Ray::new(
499///     Vector::new(0.0, 0.0, -5.0),
500///     Vector::new(0.0, 0.0, 1.0)
501/// );
502///
503/// let mut simplex = VoronoiSimplex::new();
504/// if let Some((toi, normal)) = cast_local_ray(&ball, &mut simplex, &ray, f32::MAX) {
505///     let hit_point = ray.point_at(toi);
506///     println!("Ray hit at: {:?}", hit_point);
507///     println!("Surface normal: {:?}", normal);
508///     println!("Distance: {}", toi);
509/// } else {
510///     println!("Ray missed the shape");
511/// }
512/// # }
513/// ```
514///
515/// # Notes
516///
517/// - The ray is specified in the local-space of the shape
518/// - The returned normal points outward from the shape
519/// - For normalized ray directions, `toi` equals the distance to the hit point
520/// - This function is typically called by higher-level raycasting APIs
521pub fn cast_local_ray<G: ?Sized + SupportMap>(
522    shape: &G,
523    simplex: &mut VoronoiSimplex,
524    ray: &Ray,
525    max_time_of_impact: Real,
526) -> Option<(Real, Vector)> {
527    let g2 = ConstantOrigin;
528    minkowski_ray_cast(
529        &Pose::IDENTITY,
530        shape,
531        &g2,
532        ray,
533        max_time_of_impact,
534        simplex,
535    )
536}
537
538/// Computes how far a shape can move in a direction before touching another shape.
539///
540/// This function answers the question: "If I move shape 1 along this direction, how far
541/// can it travel before it touches shape 2?" This is useful for:
542/// - Continuous collision detection (CCD)
543/// - Movement planning and obstacle avoidance
544/// - Computing time-of-impact for moving objects
545/// - Safe navigation distances
546///
547/// # How It Works
548///
549/// The function casts shape 1 along the given direction vector and finds the first point
550/// where it would contact shape 2. It returns:
551/// - The distance that can be traveled
552/// - The contact normal at the point of first contact
553/// - The witness points (closest points) on both shapes at contact
554///
555/// # Parameters
556///
557/// - `pos12`: The relative position of shape 2 with respect to shape 1 (isometry from
558///   shape 1's space to shape 2's space)
559/// - `g1`: The first shape being moved (must implement `SupportMap`)
560/// - `g2`: The second shape (static target, must implement `SupportMap`)
561/// - `dir`: The direction vector to move shape 1 in (in local-space of shape 1)
562/// - `simplex`: A reusable simplex structure. Initialize with `VoronoiSimplex::new()`.
563///
564/// # Returns
565///
566/// - `Some((distance, normal, witness1, witness2))`: If contact would occur
567///   - `distance`: How far shape 1 can travel before touching shape 2
568///   - `normal`: The contact normal at the point of first contact
569///   - `witness1`: The contact point on shape 1 (in local-space of shape 1)
570///   - `witness2`: The contact point on shape 2 (in local-space of shape 1)
571/// - `None`: If no contact would occur (shapes don't intersect along this direction)
572///
573/// # Example
574///
575/// ```rust
576/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
577/// use parry3d::shape::Ball;
578/// use parry3d::query::gjk::{directional_distance, VoronoiSimplex};
579/// use parry3d::math::{Pose, Vector};
580///
581/// // Two balls: one at origin, one at (10, 0, 0)
582/// let ball1 = Ball::new(1.0);
583/// let ball2 = Ball::new(1.0);
584/// let pos12 = Pose::translation(10.0, 0.0, 0.0);
585///
586/// // Move ball1 toward ball2 along the +X axis
587/// let direction = Vector::new(1.0, 0.0, 0.0);
588///
589/// let mut simplex = VoronoiSimplex::new();
590/// if let Some((distance, normal, w1, w2)) = directional_distance(
591///     &pos12,
592///     &ball1,
593///     &ball2,
594///     direction,
595///     &mut simplex
596/// ) {
597///     println!("Ball1 can move {} units before contact", distance);
598///     println!("Contact normal: {:?}", normal);
599///     println!("Contact point on ball1: {:?}", w1);
600///     println!("Contact point on ball2: {:?}", w2);
601///     // Expected: distance ≈ 8.0 (10.0 - 1.0 - 1.0)
602/// }
603/// # }
604/// ```
605///
606/// # Use Cases
607///
608/// **1. Continuous Collision Detection:**
609/// ```ignore
610/// let movement_dir = velocity * time_step;
611/// if let Some((toi, normal, _, _)) = directional_distance(...) {
612///     if toi < 1.0 {
613///         // Collision will occur during this timestep
614///         let collision_time = toi * time_step;
615///     }
616/// }
617/// ```
618///
619/// **2. Safe Movement Distance:**
620/// ```ignore
621/// let desired_movement = Vector::new(5.0, 0.0, 0.0);
622/// if let Some((max_safe_dist, _, _, _)) = directional_distance(...) {
623///     let actual_movement = desired_movement.normalize() * max_safe_dist.min(5.0);
624/// }
625/// ```
626///
627/// # Notes
628///
629/// - All inputs and outputs are in the local-space of shape 1
630/// - If the shapes are already intersecting, the returned distance is 0.0 and witness
631///   points are undefined (set to origin)
632/// - The direction vector does not need to be normalized
633/// - This function internally uses GJK raycasting on the Minkowski difference
634pub fn directional_distance<G1, G2>(
635    pos12: &Pose,
636    g1: &G1,
637    g2: &G2,
638    dir: Vector,
639    simplex: &mut VoronoiSimplex,
640) -> Option<(Real, Vector, Vector, Vector)>
641where
642    G1: ?Sized + SupportMap,
643    G2: ?Sized + SupportMap,
644{
645    let ray = Ray::new(Vector::ZERO, dir);
646    minkowski_ray_cast(pos12, g1, g2, &ray, Real::max_value(), simplex).map(
647        |(time_of_impact, normal)| {
648            let witnesses = if !time_of_impact.is_zero() {
649                result(simplex, simplex.dimension() == DIM)
650            } else {
651                // If there is penetration, the witness points
652                // are undefined.
653                (Vector::ZERO, Vector::ZERO)
654            };
655
656            (time_of_impact, normal, witnesses.0, witnesses.1)
657        },
658    )
659}
660
661// Ray-cast on the Minkowski Difference `g1 - pos12 * g2`.
662fn minkowski_ray_cast<G1, G2>(
663    pos12: &Pose,
664    g1: &G1,
665    g2: &G2,
666    ray: &Ray,
667    max_time_of_impact: Real,
668    simplex: &mut VoronoiSimplex,
669) -> Option<(Real, Vector)>
670where
671    G1: ?Sized + SupportMap,
672    G2: ?Sized + SupportMap,
673{
674    let _eps = crate::math::DEFAULT_EPSILON;
675    let _eps_tol: Real = eps_tol();
676    let _eps_rel: Real = <Real as ComplexField>::sqrt(_eps_tol);
677
678    let ray_length = ray.dir.length();
679
680    if relative_eq!(ray_length, 0.0) {
681        return None;
682    }
683
684    let mut ltoi = 0.0;
685    let mut curr_ray = Ray::new(ray.origin, ray.dir / ray_length);
686    let dir = -curr_ray.dir;
687    let mut ldir = dir;
688
689    // Initialize the simplex.
690    let support_point = CsoPoint::from_shapes(pos12, g1, g2, dir);
691    simplex.reset(support_point.translate(-curr_ray.origin));
692
693    // TODO: reset the simplex if it is empty?
694    let mut proj = simplex.project_origin_and_reduce();
695    let mut max_bound = Real::max_value();
696    let mut dir;
697    let mut niter = 0;
698    let mut last_chance = false;
699
700    loop {
701        let old_max_bound = max_bound;
702
703        let neg_proj_coords = -proj;
704        let (normalized, dist) = neg_proj_coords.normalize_and_length();
705        if dist > _eps_tol {
706            dir = normalized;
707            max_bound = dist;
708        } else {
709            return Some((ltoi / ray_length, ldir));
710        }
711
712        let support_point = if max_bound >= old_max_bound {
713            // Upper bounds inconsistencies. Consider the projection as a valid support point.
714            last_chance = true;
715            CsoPoint::single_point(proj + curr_ray.origin)
716        } else {
717            CsoPoint::from_shapes(pos12, g1, g2, dir)
718        };
719
720        if last_chance && ltoi > 0.0 {
721            // last_chance && ltoi > 0.0 && (support_point.point - curr_ray.origin).dot(ldir) >= 0.0 {
722            return Some((ltoi / ray_length, ldir));
723        }
724
725        // Clip the ray on the support halfspace (None <=> t < 0)
726        // The configurations are:
727        //   dir.dot(curr_ray.dir)  |   t   |               Action
728        // −−−−−−−−−−−−−−−−−−−−-----+−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
729        //          < 0             |  < 0  | Continue.
730        //          < 0             |  > 0  | New lower bound, move the origin.
731        //          > 0             |  < 0  | Miss. No intersection.
732        //          > 0             |  > 0  | New higher bound.
733        match query::details::ray_toi_with_halfspace(support_point.point, dir, &curr_ray) {
734            Some(t) => {
735                if dir.dot(curr_ray.dir) < 0.0 && t > 0.0 {
736                    // new lower bound
737                    ldir = dir;
738                    ltoi += t;
739
740                    // NOTE: we divide by ray_length instead of doing max_time_of_impact * ray_length
741                    // because the multiplication may cause an overflow if max_time_of_impact is set
742                    // to Real::max_value() by users that want to have an infinite ray.
743                    if ltoi / ray_length > max_time_of_impact {
744                        return None;
745                    }
746
747                    let shift = curr_ray.dir * t;
748                    curr_ray.origin += shift;
749                    max_bound = Real::max_value();
750                    simplex.modify_pnts(&|pt| pt.translate_mut(-shift));
751                    last_chance = false;
752                }
753            }
754            None => {
755                if dir.dot(curr_ray.dir) > _eps_tol {
756                    // miss
757                    return None;
758                }
759            }
760        }
761
762        if last_chance {
763            return None;
764        }
765
766        let min_bound = -dir.dot(support_point.point - curr_ray.origin);
767
768        assert!(min_bound.is_finite());
769
770        if max_bound - min_bound <= _eps_rel * max_bound {
771            // This is needed when using fixed-points to avoid missing
772            // some castes.
773            // TODO: I feel like we should always return `Some` in
774            // this case, even with floating-point numbers. Though it
775            // has not been sufficiently tested with floats yet to be sure.
776            if cfg!(feature = "improved_fixed_point_support") {
777                return Some((ltoi / ray_length, ldir));
778            } else {
779                return None;
780            }
781        }
782
783        let _ = simplex.add_point(support_point.translate(-curr_ray.origin));
784        proj = simplex.project_origin_and_reduce();
785
786        if simplex.dimension() == DIM {
787            if min_bound >= _eps_tol {
788                return None;
789            } else {
790                return Some((ltoi / ray_length, ldir)); // Vector inside of the cso.
791            }
792        }
793
794        niter += 1;
795        if niter == 100 {
796            return None;
797        }
798    }
799}
800
801fn result(simplex: &VoronoiSimplex, prev: bool) -> (Vector, Vector) {
802    let mut res = (Vector::ZERO, Vector::ZERO);
803    if prev {
804        for i in 0..simplex.prev_dimension() + 1 {
805            let coord = simplex.prev_proj_coord(i);
806            let point = simplex.prev_point(i);
807            res.0 += point.orig1 * coord;
808            res.1 += point.orig2 * coord;
809        }
810
811        res
812    } else {
813        for i in 0..simplex.dimension() + 1 {
814            let coord = simplex.proj_coord(i);
815            let point = simplex.point(i);
816            res.0 += point.orig1 * coord;
817            res.1 += point.orig2 * coord;
818        }
819
820        res
821    }
822}