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 induced a hazard within the entire interior or exterior of its shape
50    pub fn scope(&self) -> GeoPosition {
51        match self {
52            HazardEntity::PlacedItem { .. } => GeoPosition::Interior,
53            HazardEntity::Exterior => GeoPosition::Exterior,
54            HazardEntity::Hole { .. } => GeoPosition::Interior,
55            HazardEntity::InferiorQualityZone { .. } => GeoPosition::Interior,
56        }
57    }
58}
59
60impl<T> From<(PItemKey, T)> for HazardEntity
61where
62    T: Borrow<PlacedItem>,
63{
64    fn from((pk, pi): (PItemKey, T)) -> Self {
65        HazardEntity::PlacedItem {
66            id: pi.borrow().item_id,
67            dt: pi.borrow().d_transf,
68            pk,
69        }
70    }
71}