jagua_rs/geometry/geo_traits.rs
1use crate::geometry::Transformation;
2use crate::geometry::geo_enums::GeoPosition;
3
4/// Trait for types that can detect collisions between `self` and `other` of type `T`.
5pub trait CollidesWith<T> {
6 fn collides_with(&self, other: &T) -> bool;
7}
8
9/// Trait for types that can detect 'almost-collisions' between `self` and `other` of type `T`.
10///
11/// Due to floating point arithmetic precision, two objects that are very close to each other may have unexpected behavior with
12/// the [`CollidesWith`] trait. This trait errors on the side of false positives, so that if two objects are very close to each other,
13/// they will be considered as colliding.
14pub trait AlmostCollidesWith<T> {
15 fn almost_collides_with(&self, other: &T) -> bool;
16}
17
18/// Trait for types that can compute the minimum distance between `self` and `other` of type `T`.
19pub trait DistanceTo<T> {
20 /// Minimum distance between two primitives. Will be 0 in case of a collision.
21 fn distance_to(&self, other: &T) -> f32;
22
23 /// Squared version of [`DistanceTo::distance_to`]
24 fn sq_distance_to(&self, other: &T) -> f32;
25}
26
27/// Trait for types that can compute the minimum distance to separate `self` from `other` of type `T`.
28pub trait SeparationDistance<T>: DistanceTo<T> {
29 /// In case of a collision between `self` and `other`, returns [`GeoPosition::Interior`] and the minimum distance to separate the two primitives.
30 /// Otherwise, returns [`GeoPosition::Exterior`] and the minimum distance between the two primitives. (similar to [`DistanceTo::distance_to`])
31 fn separation_distance(&self, other: &T) -> (GeoPosition, f32);
32
33 /// Squared version of [`SeparationDistance::separation_distance`]
34 fn sq_separation_distance(&self, other: &T) -> (GeoPosition, f32);
35}
36
37/// Trait for types that can modify themselves by a [`Transformation`].
38pub trait Transformable: Clone {
39 /// Applies a transformation to `self`.
40 fn transform(&mut self, t: &Transformation) -> &mut Self;
41
42 /// Applies a transformation to a clone.
43 #[must_use]
44 fn transform_clone(&self, t: &Transformation) -> Self {
45 let mut clone = self.clone();
46 clone.transform(t);
47 clone
48 }
49}
50
51/// Trait for types that can modify themselves to a reference object with a [`Transformation`] applied.
52///
53/// Useful when repeatedly transforming a single object without having to reallocate new memory each time.
54pub trait TransformableFrom: Transformable {
55 /// Applies a transformation on the reference object and stores the result in `self`.
56 fn transform_from(&mut self, reference: &Self, t: &Transformation) -> &mut Self;
57}