packer_3d/
sorting.rs

1//! Sorting functions used by [PackerInstance](crate::PackerInstance)
2
3use crate::box3d::Box3D;
4use std::cmp::{Ordering, Reverse};
5
6pub struct Sorting {}
7
8impl Sorting {
9	pub fn descending_volume(a: &Box3D, b: &Box3D) -> Ordering {
10		(b.volume(), b.size.z, b.size.x, b.size.y, Reverse(b.id))
11			.cmp(&(a.volume(), a.size.z, a.size.x, a.size.y, Reverse(a.id)))
12	}
13	pub fn ascending_volume(a: &Box3D, b: &Box3D) -> Ordering {
14		Self::descending_volume(b, a)
15	}
16
17	pub fn descending_width(a: &Box3D, b: &Box3D) -> Ordering {
18		(b.size.x, b.volume(), b.size.z, b.size.y, Reverse(b.id))
19			.cmp(&(a.size.x, a.volume(), a.size.z, a.size.y, Reverse(a.id)))
20	}
21	pub fn ascending_width(a: &Box3D, b: &Box3D) -> Ordering {
22		Self::descending_width(b, a)
23	}
24
25	pub fn descending_height(a: &Box3D, b: &Box3D) -> Ordering {
26		(b.size.y, b.volume(), b.size.z, b.size.x, Reverse(b.id))
27			.cmp(&(a.size.y, a.volume(), a.size.z, a.size.x, Reverse(a.id)))
28	}
29	pub fn ascending_height(a: &Box3D, b: &Box3D) -> Ordering {
30		Self::descending_height(b, a)
31	}
32
33	pub fn descending_length(a: &Box3D, b: &Box3D) -> Ordering {
34		(b.size.z, b.volume(), b.size.x, b.size.y, Reverse(b.id))
35			.cmp(&(a.size.z, a.volume(), a.size.x, a.size.y, Reverse(a.id)))
36	}
37	pub fn ascending_length(a: &Box3D, b: &Box3D) -> Ordering {
38		Self::descending_length(b, a)
39	}
40
41	pub fn descending_id(a: &Box3D, b: &Box3D) -> Ordering {
42		b.id.cmp(&a.id)
43	}
44	pub fn ascending_id(a: &Box3D, b: &Box3D) -> Ordering {
45		Self::descending_id(b, a)
46	}
47
48	/// Gets the sorting function from a string slice
49	pub fn get(name: &str) -> fn(&Box3D,&Box3D) -> Ordering
50	{
51		match name
52		{
53			"Descending Volume" => Sorting::descending_volume,
54			"Ascending Volume" => Sorting::ascending_volume,
55
56			"Descending Width" => Sorting::descending_width,
57			"Ascending Width" => Sorting::ascending_width,
58
59			"Descending Height" => Sorting::descending_height,
60			"Ascending Height" => Sorting::ascending_height,
61
62			"Descending Length" => Sorting::descending_length,
63			"Ascending Length" => Sorting::ascending_length,
64
65			"Descending Id" => Sorting::descending_id,
66			"Ascending Id" => Sorting::ascending_id,
67
68			_ => Sorting::descending_volume,
69		}
70	}
71}