jagua_rs/entities/
container.rs

1use std::sync::Arc;
2
3use itertools::Itertools;
4
5use crate::collision_detection::hazards::Hazard;
6use crate::collision_detection::hazards::HazardEntity;
7use crate::collision_detection::{CDEConfig, CDEngine};
8use crate::geometry::OriginalShape;
9use crate::geometry::primitives::SPolygon;
10
11use anyhow::{Result, ensure};
12
13/// A container in which [`Item`](crate::entities::Item)'s can be placed.
14#[derive(Clone, Debug)]
15pub struct Container {
16    pub id: usize,
17    /// Original contour of the container as defined in the input
18    pub outer_orig: Arc<OriginalShape>,
19    /// Contour of the container to be used for collision detection
20    pub outer_cd: Arc<SPolygon>,
21    /// Zones of different qualities in the container, stored per quality.
22    pub quality_zones: [Option<InferiorQualityZone>; N_QUALITIES],
23    /// The initial state of the `CDEngine` for this container. (equivalent to an empty layout using this container)
24    pub base_cde: Arc<CDEngine>,
25}
26
27impl Container {
28    pub fn new(
29        id: usize,
30        original_outer: OriginalShape,
31        quality_zones: Vec<InferiorQualityZone>,
32        cde_config: CDEConfig,
33    ) -> Result<Self> {
34        let outer = Arc::new(original_outer.convert_to_internal()?);
35        let outer_orig = Arc::new(original_outer);
36        ensure!(
37            quality_zones.len() == quality_zones.iter().map(|qz| qz.quality).unique().count(),
38            "Quality zones must have unique qualities"
39        );
40        ensure!(
41            quality_zones
42                .iter()
43                .map(|qz| qz.quality)
44                .all(|q| q < N_QUALITIES),
45            "All quality zones must be below N_QUALITIES: {N_QUALITIES}"
46        );
47        let quality_zones = {
48            let mut qz = <[_; N_QUALITIES]>::default();
49            for q in quality_zones {
50                let quality = q.quality;
51                qz[quality] = Some(q);
52            }
53            qz
54        };
55
56        let base_cde = {
57            let mut hazards = vec![Hazard::new(
58                HazardEntity::Exterior,
59                outer.as_ref().clone(),
60                false,
61            )];
62            let qz_hazards = quality_zones
63                .iter()
64                .flatten()
65                .flat_map(|qz| qz.to_hazards());
66            hazards.extend(qz_hazards);
67            let base_cde = CDEngine::new(outer.bbox.inflate_to_square(), hazards, cde_config);
68            Arc::new(base_cde)
69        };
70
71        Ok(Self {
72            id,
73            outer_cd: outer,
74            outer_orig,
75            quality_zones,
76            base_cde,
77        })
78    }
79
80    /// The area of the contour of the container, excluding holes
81    pub fn area(&self) -> f32 {
82        self.outer_orig.area() - self.quality_zones[0].as_ref().map_or(0.0, |qz| qz.area())
83    }
84}
85
86/// Maximum number of qualities that can be used for quality zones in a container.
87pub const N_QUALITIES: usize = 10;
88
89/// Represents a zone of inferior quality in the [`Container`]
90#[derive(Clone, Debug)]
91pub struct InferiorQualityZone {
92    /// Quality of this zone. Higher qualities are superior. A zone with quality 0 is treated as a hole.
93    pub quality: usize,
94    /// Contours of this quality-zone as defined in the input file
95    pub shapes_orig: Vec<Arc<OriginalShape>>,
96    /// Contours of this quality-zone to be used for collision detection
97    pub shapes_cd: Vec<Arc<SPolygon>>,
98}
99
100impl InferiorQualityZone {
101    pub fn new(quality: usize, original_shapes: Vec<OriginalShape>) -> Result<Self> {
102        assert!(
103            quality < N_QUALITIES,
104            "Quality must be in range of N_QUALITIES"
105        );
106        let shapes: Result<Vec<Arc<SPolygon>>> = original_shapes
107            .iter()
108            .map(|orig| orig.convert_to_internal().map(Arc::new))
109            .collect();
110
111        let original_shapes = original_shapes.into_iter().map(Arc::new).collect_vec();
112
113        Ok(Self {
114            quality,
115            shapes_cd: shapes?,
116            shapes_orig: original_shapes,
117        })
118    }
119
120    /// Returns the set of hazards induced by this zone.
121    pub fn to_hazards(&self) -> impl Iterator<Item = Hazard> {
122        self.shapes_cd.iter().enumerate().map(|(idx, shape)| {
123            let entity = match self.quality {
124                0 => HazardEntity::Hole { idx },
125                _ => HazardEntity::InferiorQualityZone {
126                    quality: self.quality,
127                    idx,
128                },
129            };
130            Hazard::new(entity, shape.as_ref().clone(), false)
131        })
132    }
133
134    pub fn area(&self) -> f32 {
135        self.shapes_orig.iter().map(|shape| shape.area()).sum()
136    }
137}