jagua_rs/collision_detection/hazards/
hazard.rs

1use crate::entities::{PItemKey, PlacedItem};
2use crate::geometry::DTransformation;
3use crate::geometry::geo_enums::GeoPosition;
4use crate::geometry::primitives::SPolygon;
5use slotmap::new_key_type;
6use std::borrow::Borrow;
7
8new_key_type! {
9    /// Key to identify hazards inside the CDE.
10    pub struct HazKey;
11}
12
13/// Any spatial constraint affecting the feasibility of a placement of an Item.
14/// See [`HazardEntity`] for the different entities that can induce a hazard.
15#[derive(Clone, Debug)]
16pub struct Hazard {
17    /// The entity inducing the hazard
18    pub entity: HazardEntity,
19    /// The shape of the hazard
20    pub shape: SPolygon,
21    /// Whether the hazard is dynamic, meaning it can change over time (e.g., moving items)
22    pub dynamic: bool,
23}
24
25impl Hazard {
26    pub fn new(entity: HazardEntity, shape: SPolygon, dynamic: bool) -> Self {
27        Self {
28            entity,
29            shape,
30            dynamic,
31        }
32    }
33}
34
35#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
36/// Entity inducing a [`Hazard`].
37/// All entities are uniquely identified.
38pub enum HazardEntity {
39    /// An item placed in the layout, defined by its id, applied transformation and key
40    PlacedItem {
41        id: usize,
42        dt: DTransformation,
43        pk: PItemKey,
44    },
45    /// Represents all regions outside the container
46    Exterior,
47    /// Represents a hole in the container.
48    Hole { idx: usize },
49    /// Represents a zone in the container with a specific quality level that is inferior to the base quality.
50    InferiorQualityZone { quality: usize, idx: usize },
51}
52
53impl HazardEntity {
54    /// Whether the entity induced a hazard within the entire interior or exterior of its shape
55    pub fn scope(&self) -> GeoPosition {
56        match self {
57            HazardEntity::PlacedItem { .. } => GeoPosition::Interior,
58            HazardEntity::Exterior => GeoPosition::Exterior,
59            HazardEntity::Hole { .. } => GeoPosition::Interior,
60            HazardEntity::InferiorQualityZone { .. } => GeoPosition::Interior,
61        }
62    }
63}
64
65impl<T> From<(PItemKey, T)> for HazardEntity
66where
67    T: Borrow<PlacedItem>,
68{
69    fn from((pk, pi): (PItemKey, T)) -> Self {
70        HazardEntity::PlacedItem {
71            id: pi.borrow().item_id,
72            dt: pi.borrow().d_transf,
73            pk,
74        }
75    }
76}