packer_3d/
sorting.rs

1//! Sorting functions used by [PackerInstance](crate::PackerInstance)
2
3use crate::box3d::Box3D;
4
5use std::cmp::Ordering;
6use Ordering::{
7	Greater,
8	Less,
9	Equal,
10};
11
12pub struct Sorting {}
13
14impl Sorting {
15	pub fn descending_volume(a: &Box3D, b: &Box3D) -> Ordering // return 1 if b is before a (switch a and b), -1 otherwise
16	{
17		let bv = b.volume();
18		let av = a.volume();
19
20		if bv > av { return Greater; }
21		if bv != av { return Less; }
22
23		if b.size.z > a.size.z { return Greater; }
24		if b.size.z != a.size.z { return Less; }
25
26		if b.size.x > a.size.x { return Greater; }
27		if b.size.x != a.size.x { return Less; }
28
29		if b.size.y > a.size.y { return Greater; }
30		if b.size.y != a.size.y { return Less; }
31
32		if b.id < a.id { return Greater; }
33
34		Equal
35	}
36	pub fn ascending_volume(a: &Box3D, b: &Box3D) -> Ordering
37	{
38		Self::descending_volume(b, a)
39	}
40
41	pub fn descending_width(a: &Box3D, b: &Box3D) -> Ordering
42	{
43		if b.size.x > a.size.x { return Greater; }
44		if b.size.x != a.size.x { return Less; }
45
46		let bv = b.volume();
47		let av = a.volume();
48
49		if bv > av { return Greater; }
50		if bv != av { return Less; }
51
52		if b.size.z > a.size.z { return Greater; }
53		if b.size.z != a.size.z { return Less; }
54		
55		if b.size.y > a.size.y { return Greater; }
56		if b.size.y != a.size.y { return Less; }
57
58		if b.id < a.id { return Greater; }
59
60		Equal
61	}
62	pub fn ascending_width(a: &Box3D, b: &Box3D) -> Ordering
63	{
64		Self::descending_width(b, a)
65	}
66
67	pub fn descending_height(a: &Box3D, b: &Box3D) -> Ordering
68	{
69		if b.size.y > a.size.y { return Greater; }
70		if b.size.y != a.size.y { return Less; }
71		
72		let bv = b.volume();
73		let av = a.volume();
74
75		if bv > av { return Greater; }
76		if bv != av { return Less; }
77
78		if b.size.z > a.size.z { return Greater; }
79		if b.size.z != a.size.z { return Less; }
80		
81		if b.size.x > a.size.x { return Greater; }
82		if b.size.x != a.size.x { return Less; }
83
84		if b.id < a.id { return Greater; }
85
86		Equal
87	}
88	pub fn ascending_height(a: &Box3D, b: &Box3D) -> Ordering
89	{
90		Self::descending_height(b, a)
91	}
92
93	pub fn descending_length(a: &Box3D, b: &Box3D) -> Ordering
94	{
95		if b.size.z > a.size.z { return Greater; }
96		if b.size.z != a.size.z { return Less; }
97		
98		let bv = b.volume();
99		let av = a.volume();
100		
101		if bv > av { return Greater; }
102		if bv != av { return Less; }
103
104		if b.size.x > a.size.x { return Greater; }
105		if b.size.x != a.size.x { return Less; }
106		
107		if b.size.y > a.size.y { return Greater; }
108		if b.size.y != a.size.y { return Less; }
109
110		if b.id < a.id { return Greater; }
111
112		Equal
113	}
114	pub fn ascending_length(a: &Box3D, b: &Box3D) -> Ordering
115	{
116		Self::descending_length(b, a)
117	}
118
119	pub fn descending_id(a: &Box3D, b: &Box3D) -> Ordering
120	{
121		if b.id < a.id { return Greater; }
122
123		Equal
124	}
125	pub fn ascending_id(a: &Box3D, b: &Box3D) -> Ordering
126	{
127		Self::descending_id(b, a)
128	}
129
130	/// Gets the sorting function from a string slice
131	pub fn get(name: &str) -> &'static dyn Fn(&Box3D,&Box3D) -> Ordering
132	{
133		match name
134		{
135			"Descending Volume" => &Sorting::descending_volume,
136			"Ascending Volume" => &Sorting::ascending_volume,
137
138			"Descending Width" => &Sorting::descending_width,
139			"Ascending Width" => &Sorting::ascending_width,
140
141			"Descending Height" => &Sorting::descending_height,
142			"Ascending Height" => &Sorting::ascending_height,
143
144			"Descending Length" => &Sorting::descending_length,
145			"Ascending Length" => &Sorting::ascending_length,
146
147			"Descending Id" => &Sorting::descending_id,
148			"Ascending Id" => &Sorting::ascending_id,
149
150			_ => &Sorting::descending_volume,
151		}
152	}
153}