jagua_rs/entities/
item.rs1use 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#[derive(Clone, Debug)]
12pub struct Item {
13 pub(crate) idx: usize,
15 pub(crate) external_id: u64,
17 pub(crate) shape_orig: Arc<OriginalShape>,
19 pub(crate) shape_cd: Arc<SPolygon>,
21 pub(crate) allowed_orientations: AllowedOrientations,
23 pub(crate) min_quality: Option<usize>,
25 pub(crate) surrogate_config: SPSurrogateConfig,
27}
28
29impl Item {
30 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 #[must_use]
67 pub fn idx(&self) -> usize {
68 self.idx
69 }
70
71 #[must_use]
73 pub fn external_id(&self) -> u64 {
74 self.external_id
75 }
76
77 #[must_use]
79 pub fn shape_orig(&self) -> &Arc<OriginalShape> {
80 &self.shape_orig
81 }
82
83 #[must_use]
85 pub fn shape_cd(&self) -> &Arc<SPolygon> {
86 &self.shape_cd
87 }
88
89 #[must_use]
91 pub fn allowed_orientations(&self) -> &AllowedOrientations {
92 &self.allowed_orientations
93 }
94
95 #[must_use]
97 pub fn min_quality(&self) -> Option<usize> {
98 self.min_quality
99 }
100
101 #[must_use]
103 pub fn surrogate_config(&self) -> SPSurrogateConfig {
104 self.surrogate_config
105 }
106}