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