Skip to main content

binpack_3d/
items.rs

1use serde::{
2    Deserialize,
3    Serialize,
4};
5
6use crate::vector::Vector3;
7
8/// The Item which should be sorted in the bin
9#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
10pub struct Item {
11    /// Primary key
12    pub id: i32,
13    /// The size of a Item
14    pub size_cube: Vector3<u32>,
15    /// Weight
16    pub weight: u32,
17    /// The order of a item should come out
18    pub order: u32,
19}
20impl Item {
21    /// Default Constructor
22    pub const fn new(id: i32, position: Vector3<u32>, weight: u32, order: u32) -> Self {
23        Self {
24            id,
25            size_cube: position,
26            weight,
27            order,
28        }
29    }
30    /// How to rotate the item in all directions
31    pub fn rotation(&self) -> Vec<Vector3<u32>> {
32        let (x, y, z) = (self.size_cube.x, self.size_cube.y, self.size_cube.z);
33        let first_rotation = Vector3::new(y, x, z);
34        let second_rotation = Vector3::new(x, z, y);
35        let four_rotation = Vector3::new(z, x, y);
36        let five_rotation = Vector3::new(y, z, x);
37        let sixs_rotation = Vector3::new(z, y, x);
38        let normal_rotation = Vector3::new(x, y, z);
39        vec![
40            normal_rotation,
41            first_rotation,
42            second_rotation,
43            four_rotation,
44            five_rotation,
45            sixs_rotation,
46        ]
47    }
48    /// ratation v2
49    pub fn rotation_v2(&self) -> Vec<Item> {
50        let (x, y, z) = (self.size_cube.x, self.size_cube.y, self.size_cube.z);
51        let first_rotation = Item::new(self.id, Vector3::new(y, x, z), self.weight, self.order);
52        let second_rotation = Item::new(self.id, Vector3::new(x, z, y), self.weight, self.order);
53        let four_rotation = Item::new(self.id, Vector3::new(z, x, y), self.weight, self.order);
54        let five_rotation = Item::new(self.id, Vector3::new(y, z, x), self.weight, self.order);
55        let sixs_rotation = Item::new(self.id, Vector3::new(z, y, x), self.weight, self.order);
56        let normal_rotation = Item::new(self.id, Vector3::new(x, y, z), self.weight, self.order);
57        vec![
58            normal_rotation,
59            first_rotation,
60            second_rotation,
61            four_rotation,
62            five_rotation,
63            sixs_rotation,
64        ]
65    }
66    /// get volume of a item
67    pub const fn volume_item(&self) -> u32 {
68        self.size_cube.x * self.size_cube.y * self.size_cube.z
69    }
70}
71/// A item which is in a bin
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct ItemsPlaced {
74    /// The Position of a Item
75    pub position: Vector3<u32>,
76    /// Item
77    pub item: Item,
78}
79impl ItemsPlaced {
80    /// Default Constructor
81    pub const fn new(position: Vector3<u32>, item: Item) -> Self {
82        // Self { x, y, z, item }
83        Self { position, item }
84    }
85}