jagua_rs/io/ext_repr.rs
1use crate::geometry::DTransformation;
2use serde::{Deserialize, Serialize};
3
4/// External representation of an [`Item`](crate::entities::Item).
5#[derive(Serialize, Deserialize, Clone)]
6pub struct ExtItem {
7 /// Unique identifier of the item
8 pub id: u64,
9 /// List of allowed orientations angles (in degrees).
10 /// Continuous rotation if not specified
11 #[serde(skip_serializing_if = "Option::is_none")]
12 pub allowed_orientations: Option<Vec<f32>>,
13 /// Shape of the item
14 pub shape: ExtShape,
15 /// The minimum required quality of the item.
16 /// Maximum quality required if not specified.
17 pub min_quality: Option<usize>,
18}
19
20/// External representation of a [`Container`](crate::entities::Container).
21/// Items can be placed inside containers.
22#[derive(Serialize, Deserialize, Clone)]
23pub struct ExtContainer {
24 /// Unique identifier of the container
25 pub id: u64,
26 /// Shape of the container
27 pub shape: ExtShape,
28 /// Zones within the container with varying quality. Holes in the container shape are treated as zones with quality 0.
29 #[serde(skip_serializing_if = "Vec::is_empty", default)]
30 pub zones: Vec<ExtQualityZone>,
31}
32
33/// Various ways to represent a shape
34#[derive(Serialize, Deserialize, Clone)]
35#[serde(tag = "type", content = "data")]
36#[serde(rename_all = "snake_case")]
37pub enum ExtShape {
38 /// Axis-aligned rectangle. With its left bottom corner at (x_min, y_min), a width and height
39 Rectangle {
40 x_min: f32,
41 y_min: f32,
42 width: f32,
43 height: f32,
44 },
45 /// Polygon with a single outer boundary
46 SimplePolygon(ExtSPolygon),
47 /// Polygon with a single outer boundary and a set of holes
48 Polygon(ExtPolygon),
49 /// Multiple disjoint polygons
50 MultiPolygon(Vec<ExtPolygon>),
51}
52
53/// A polygon represented as an outer boundary and a list of holes
54#[derive(Serialize, Deserialize, Clone)]
55pub struct ExtPolygon {
56 /// The outer boundary of the polygon
57 pub outer: ExtSPolygon,
58 /// A list of holes in the polygon
59 #[serde(default)]
60 pub inner: Vec<ExtSPolygon>,
61}
62
63/// External representation of a [`SPolygon`](crate::geometry::primitives::SPolygon).
64/// A polygon with no holes and no self-intersections.
65#[derive(Serialize, Deserialize, Clone)]
66pub struct ExtSPolygon(pub Vec<(f32, f32)>);
67
68/// A zone with a specific quality level
69#[derive(Serialize, Deserialize, Clone)]
70pub struct ExtQualityZone {
71 /// The quality level of this zone
72 pub quality: usize,
73 /// The polygon shape of this zone
74 pub shape: ExtShape,
75}
76
77/// External representation of a [`Layout`](crate::entities::Layout).
78/// A layout consists of a container with items placed in a specific configuration.
79#[derive(Serialize, Deserialize, Clone)]
80pub struct ExtLayout {
81 /// The container that was used
82 pub container_id: u64,
83 /// The items placed in the container and where they were placed
84 pub placed_items: Vec<ExtPlacedItem>,
85 /// Some statistics about the layout
86 pub density: f32,
87}
88
89/// External representation of a [`PlacedItem`](crate::entities::PlacedItem).
90/// An item placed in a container with a specific transformation.
91#[derive(Serialize, Deserialize, Clone)]
92pub struct ExtPlacedItem {
93 /// The id of the item in the instance
94 pub item_id: u64,
95 /// The transformation applied to the item to place it in the container
96 pub transformation: ExtTransformation,
97}
98
99/// Represents a proper rigid transformation defined as a rotation followed by translation
100#[derive(Serialize, Deserialize, Clone)]
101pub struct ExtTransformation {
102 /// The rotation angle in radians
103 pub rotation: f32,
104 /// The translation vector (x, y)
105 pub translation: (f32, f32),
106}
107
108impl From<DTransformation> for ExtTransformation {
109 fn from(dt: DTransformation) -> Self {
110 ExtTransformation {
111 rotation: dt.rotation(),
112 translation: dt.translation(),
113 }
114 }
115}