Skip to main content

jagua_rs/entities/
placed_item.rs

1use crate::collision_detection::hazards::{BasicHazardEntity, PItemKey};
2use crate::entities::Item;
3use crate::geometry::DTransformation;
4use crate::geometry::geo_traits::Transformable;
5use crate::geometry::primitives::SPolygon;
6use std::borrow::Borrow;
7use std::sync::Arc;
8
9#[cfg(doc)]
10use crate::entities::Layout;
11
12/// Represents an [`Item`] that has been placed in a [`Layout`]
13#[derive(Clone, Debug)]
14pub struct PlacedItem {
15    /// Shared immutable item definition, retained by layouts and snapshots.
16    pub(crate) item: Arc<Item>,
17    /// The transformation that was applied to the `Item` before it was placed
18    pub(crate) d_transf: DTransformation,
19    /// The shape of the `Item` after it has been transformed and placed in a `Layout`
20    pub(crate) shape: Arc<SPolygon>,
21}
22
23impl PlacedItem {
24    #[must_use]
25    pub fn new(item: &Arc<Item>, d_transf: DTransformation) -> Self {
26        let transf = d_transf.compose();
27        let shape = item.shape_cd.transform_clone(&transf);
28
29        PlacedItem {
30            item: Arc::clone(item),
31            d_transf,
32            shape: Arc::new(shape),
33        }
34    }
35
36    /// Shared immutable item definition, retained by layouts and snapshots.
37    #[must_use]
38    pub fn item(&self) -> &Arc<Item> {
39        &self.item
40    }
41
42    /// The transformation that was applied to the `Item` before it was placed
43    #[must_use]
44    pub fn d_transf(&self) -> DTransformation {
45        self.d_transf
46    }
47
48    /// The shape of the `Item` after it has been transformed and placed in a `Layout`
49    #[must_use]
50    pub fn shape(&self) -> &Arc<SPolygon> {
51        &self.shape
52    }
53}
54
55impl<T> From<(PItemKey, T)> for BasicHazardEntity
56where
57    T: Borrow<PlacedItem>,
58{
59    fn from((pk, pi): (PItemKey, T)) -> Self {
60        BasicHazardEntity::PlacedItem {
61            id: pi.borrow().item.idx,
62            dt: pi.borrow().d_transf,
63            pk,
64        }
65    }
66}