Skip to main content

jagua_rs/entities/
item.rs

1use std::sync::Arc;
2
3use crate::geometry::fail_fast::SPSurrogateConfig;
4use crate::geometry::primitives::SPolygon;
5use crate::geometry::{AllowedOrientations, OriginalShape};
6
7use crate::entities::N_QUALITIES;
8use anyhow::{Result, ensure};
9
10/// Item to be produced.
11#[derive(Clone, Debug)]
12pub struct Item {
13    /// Dense index in the owning instance's item list.
14    pub(crate) idx: usize,
15    /// Original caller-supplied identifier, preserved on export.
16    pub(crate) external_id: u64,
17    /// Original contour of the item as defined in the input
18    pub(crate) shape_orig: Arc<OriginalShape>,
19    /// Contour of the item to be used for collision detection
20    pub(crate) shape_cd: Arc<SPolygon>,
21    /// Allowed rotations and reflections in which the item can be placed
22    pub(crate) allowed_orientations: AllowedOrientations,
23    /// The minimum quality the item should be produced out of, if `None` the item requires full quality
24    pub(crate) min_quality: Option<usize>,
25    /// Configuration for the surrogate generation
26    pub(crate) surrogate_config: SPSurrogateConfig,
27}
28
29impl Item {
30    /// Returns an error for invalid geometry or a minimum quality outside `0..N_QUALITIES`.
31    pub fn new(
32        idx: usize,
33        external_id: u64,
34        original_shape: OriginalShape,
35        allowed_orientations: AllowedOrientations,
36        min_quality: Option<usize>,
37        surrogate_config: SPSurrogateConfig,
38    ) -> Result<Item> {
39        ensure!(
40            min_quality.is_none_or(|quality| quality < N_QUALITIES),
41            "Item minimum quality must be below {N_QUALITIES}"
42        );
43        let shape_orig = Arc::new(original_shape);
44        let shape_int = {
45            let mut shape_int = shape_orig.convert_to_internal()?;
46            shape_int.generate_surrogate(surrogate_config)?;
47            Arc::new(shape_int)
48        };
49        Ok(Item {
50            idx,
51            external_id,
52            shape_orig,
53            shape_cd: shape_int,
54            allowed_orientations,
55            min_quality,
56            surrogate_config,
57        })
58    }
59
60    #[must_use]
61    pub fn area(&self) -> f32 {
62        self.shape_orig.area()
63    }
64
65    /// Dense index in the owning instance's item list.
66    #[must_use]
67    pub fn idx(&self) -> usize {
68        self.idx
69    }
70
71    /// Original caller-supplied identifier, preserved on export.
72    #[must_use]
73    pub fn external_id(&self) -> u64 {
74        self.external_id
75    }
76
77    /// Original contour of the item as defined in the input
78    #[must_use]
79    pub fn shape_orig(&self) -> &Arc<OriginalShape> {
80        &self.shape_orig
81    }
82
83    /// Contour of the item to be used for collision detection
84    #[must_use]
85    pub fn shape_cd(&self) -> &Arc<SPolygon> {
86        &self.shape_cd
87    }
88
89    /// Allowed rotations and reflections in which the item can be placed
90    #[must_use]
91    pub fn allowed_orientations(&self) -> &AllowedOrientations {
92        &self.allowed_orientations
93    }
94
95    /// The minimum quality the item should be produced out of, if `None` the item requires full quality
96    #[must_use]
97    pub fn min_quality(&self) -> Option<usize> {
98        self.min_quality
99    }
100
101    /// Configuration for the surrogate generation
102    #[must_use]
103    pub fn surrogate_config(&self) -> SPSurrogateConfig {
104        self.surrogate_config
105    }
106}