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