vox_geometry_rust/
intersection_query_engine2.rs

1/*
2 * // Copyright (c) 2021 Feng Yang
3 * //
4 * // I am making my contributions/submissions to this project solely in my
5 * // personal capacity and am not conveying any rights to any intellectual
6 * // property of any third parties.
7 */
8
9use crate::vector2::Vector2D;
10use crate::ray2::Ray2D;
11use crate::bounding_box2::BoundingBox2D;
12
13/// Closest intersection query result.
14pub struct ClosestIntersectionQueryResult2<T> {
15    pub item: Option<T>,
16    pub distance: f64,
17}
18
19impl<T> ClosestIntersectionQueryResult2<T> {
20    pub fn new() -> ClosestIntersectionQueryResult2<T> {
21        return ClosestIntersectionQueryResult2 {
22            item: None,
23            distance: f64::MAX,
24        };
25    }
26}
27
28///Closest intersection distance measure function.
29pub trait ClosestIntersectionDistanceFunc2<T>: FnMut(&T, &Vector2D) -> f64 {}
30
31impl<T, Super: FnMut(&T, &Vector2D) -> f64> ClosestIntersectionDistanceFunc2<T> for Super {}
32
33/// Box-item intersection test function.
34pub trait BoxIntersectionTestFunc2<T>: FnMut(&T, &BoundingBox2D) -> bool {}
35
36impl<T, Super: FnMut(&T, &BoundingBox2D) -> bool> BoxIntersectionTestFunc2<T> for Super {}
37
38/// Ray-item intersection test function.
39pub trait RayIntersectionTestFunc2<T>: FnMut(&T, &Ray2D) -> bool {}
40
41impl<T, Super: FnMut(&T, &Ray2D) -> bool> RayIntersectionTestFunc2<T> for Super {}
42
43/// Ray-item closest intersection evaluation function.
44pub trait GetRayIntersectionFunc2<T>: FnMut(&T, &Ray2D) -> f64 {}
45
46impl<T, Super: FnMut(&T, &Ray2D) -> f64> GetRayIntersectionFunc2<T> for Super {}
47
48/// Visitor function which is invoked for each intersecting item.
49pub trait IntersectionVisitorFunc2<T>: FnMut(&T) {}
50
51impl<T, Super: FnMut(&T)> IntersectionVisitorFunc2<T> for Super {}
52
53/// Abstract base class for 2-D intersection test query engine.
54pub trait IntersectionQueryEngine2<T> {
55    /// Returns true if given \p box intersects with any of the stored items.
56    fn intersects_aabb<Callback>(&self, aabb: &BoundingBox2D,
57                                 test_func: &mut Callback) -> bool
58        where Callback: BoxIntersectionTestFunc2<T>;
59
60    /// Returns true if given \p ray intersects with any of the stored items.
61    fn intersects_ray<Callback>(&self, ray: &Ray2D,
62                                test_func: &mut Callback) -> bool
63        where Callback: RayIntersectionTestFunc2<T>;
64
65    /// Invokes \p visitor_func for every intersecting items.
66    fn for_each_intersecting_item_aabb<Callback, Visitor>(&self, aabb: &BoundingBox2D,
67                                                          test_func: &mut Callback,
68                                                          visitor_func: &mut Visitor)
69        where Callback: BoxIntersectionTestFunc2<T>,
70              Visitor: IntersectionVisitorFunc2<T>;
71
72    /// Invokes \p visitor_func for every intersecting items.
73    fn for_each_intersecting_item_ray<Callback, Visitor>(&self, ray: &Ray2D,
74                                                         test_func: &mut Callback,
75                                                         visitor_func: &mut Visitor)
76        where Callback: RayIntersectionTestFunc2<T>,
77              Visitor: IntersectionVisitorFunc2<T>;
78
79    /// Returns the closest intersection for given \p ray.
80    fn closest_intersection<Callback>(&self, ray: &Ray2D,
81                                      test_func: &mut Callback) -> ClosestIntersectionQueryResult2<T>
82        where Callback: GetRayIntersectionFunc2<T>;
83}