1
- interface IBinSize {
1
+ export interface IBinSize {
2
2
height : number ,
3
3
length : number ,
4
4
width : number ,
5
5
}
6
6
7
- interface IBinItem {
7
+ export interface IBinItem {
8
8
length : number ,
9
9
width : number ,
10
10
height : number ,
11
11
id : string ,
12
12
}
13
13
14
- interface IBinResult {
14
+ export interface IBinResult {
15
15
box : TBin ,
16
16
results : IBinItemResult [ ] ,
17
17
rotations : number ,
18
18
unfit : number ,
19
19
}
20
20
21
- interface IBinItemResult {
21
+ export interface IBinItemResult {
22
22
code : string ,
23
23
desc : string ,
24
24
id : string ,
25
25
success : boolean ,
26
26
}
27
27
28
- interface IBinItemSolution {
28
+ export interface IBinItemSolution {
29
29
pos : IBinItemPosition ,
30
30
item : IBinItem ,
31
31
desc : string ,
32
32
rotation : boolean ,
33
33
}
34
34
35
- interface IBinItemPosition {
35
+ export interface IBinItemPosition {
36
36
x : number ,
37
37
y : number ,
38
38
z : number
39
39
}
40
40
41
- type TBin = string [ ] [ ] [ ]
41
+ export type TBin = string [ ] [ ] [ ]
42
42
43
- const generateMatrix = ( height : number , width : number , depth : number ) : TBin => {
43
+ export const generateEmptyBin = ( height : number , width : number , depth : number ) : TBin => {
44
44
const layers = [ ]
45
45
for ( let j = 0 ; j < depth ; j ++ ) {
46
46
const matrix = new Array ( height )
@@ -52,8 +52,7 @@ const generateMatrix = (height: number, width: number, depth: number): TBin => {
52
52
return layers
53
53
}
54
54
55
- // todo - add 3d visualization
56
- const getPosition = ( matrix : any , item : any ) : IBinItemPosition => {
55
+ export const getPosition = ( matrix : any , item : any ) : IBinItemPosition => {
57
56
let solution : any = { x : false , y : false , z : false }
58
57
59
58
il:
@@ -89,7 +88,7 @@ const getPosition = (matrix: any, item: any): IBinItemPosition => {
89
88
return solution
90
89
}
91
90
92
- const bestFit = ( matrix : any [ ] [ ] [ ] , solutions : any [ ] ) => {
91
+ export const bestFit = ( matrix : any [ ] [ ] [ ] , solutions : any [ ] ) => {
93
92
let bsi : number = 0
94
93
solutions . forEach ( ( s , idx ) => {
95
94
if ( solutions [ bsi ] . pos . x === false ) {
@@ -105,8 +104,7 @@ const bestFit = (matrix: any[][][], solutions: any[]) => {
105
104
{ desc : solutions [ bsi ] . desc } )
106
105
}
107
106
108
- // orientation needed
109
- const placeItem = ( matrix : TBin , item : any ) : IBinItemResult => {
107
+ export const pack = ( matrix : TBin , item : any ) : IBinItemResult => {
110
108
const o : IBinItemPosition = getPosition ( matrix , item )
111
109
const solutions : IBinItemSolution [ ] = [ ]
112
110
solutions . push ( { pos : o , item, desc : 'Orientation: Default' , rotation : false } )
@@ -156,39 +154,14 @@ const placeItem = (matrix: TBin, item: any): IBinItemResult => {
156
154
}
157
155
}
158
156
159
- const binpack = ( boxSize : IBinSize , items : IBinItem [ ] ) : IBinResult => {
160
- const box = generateMatrix ( boxSize . height , boxSize . width , boxSize . length )
157
+ export const binpack = ( boxSize : IBinSize , items : IBinItem [ ] ) : IBinResult => {
158
+ const box = generateEmptyBin ( boxSize . height , boxSize . width , boxSize . length )
161
159
items . sort ( ( a : IBinItem , b : IBinItem ) => a . length * a . width * a . height > b . length * b . width * b . height ? - 1 : 1 )
162
160
const results : any = [ ]
163
- items . forEach ( ( itm : IBinItem ) => results . push ( placeItem ( box , itm ) ) )
161
+ items . forEach ( ( item : IBinItem ) => results . push ( pack ( box , item ) ) )
164
162
const unfit : number = results . filter ( ( r : IBinItemResult ) => r . code === 'OVERSIZED' ) . length
165
163
const rotations : number = results . filter ( ( r : IBinItemResult ) => r . code === 'ROTATION' ) . length
166
164
return { box, results, rotations, unfit }
167
165
}
168
166
169
- let items : IBinItem [ ] = [
170
- { length : 2 , width : 2 , height : 2 , id : 'A' } ,
171
- { length : 1 , width : 2 , height : 3 , id : 'B' } ,
172
- { length : 3 , width : 1 , height : 1 , id : 'C' } ,
173
- { length : 2 , width : 2 , height : 2 , id : 'D' } ,
174
- { length : 2 , width : 4 , height : 4 , id : 'E' } ,
175
- { length : 1 , width : 5 , height : 1 , id : 'F' } ,
176
- { length : 1 , width : 2 , height : 2 , id : 'G' } ,
177
- { length : 1 , width : 1 , height : 3 , id : 'H' } ,
178
- { length : 1 , width : 3 , height : 1 , id : 'J' } ,
179
- { length : 1 , width : 1 , height : 1 , id : 'K' } ,
180
- { length : 1 , width : 3 , height : 1 , id : 'L' } ,
181
- { length : 1 , width : 1 , height : 3 , id : 'M' } ,
182
- { length : 1 , width : 1 , height : 3 , id : 'N' } ,
183
- { length : 1 , width : 3 , height : 1 , id : 'O' } ,
184
- { length : 1 , width : 2 , height : 1 , id : 'P' } ,
185
- { length : 1 , width : 1 , height : 2 , id : 'Q' } ,
186
- { length : 2 , width : 1 , height : 1 , id : 'R' } ,
187
- ]
188
-
189
- let bin = { width : 5 , height : 5 , length : 3 }
190
-
191
- console . time ( 'BinPack time' )
192
- const results = binpack ( bin , items )
193
- console . log ( results )
194
- console . timeEnd ( 'BinPack time' )
167
+ export default binpack
0 commit comments