-
Notifications
You must be signed in to change notification settings - Fork 9
/
split-strategies.ts
107 lines (98 loc) · 3.31 KB
/
split-strategies.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import debugLib from 'debug'
const debug = debugLib('guillotine-packer:split-strategies')
import { Rectangle, Item } from './types'
// implementations based off of http://pds25.egloos.com/pds/201504/21/98/RectangleBinPack.pdf
const createSplitRectangle = (rectangle: Rectangle) => ({ ...rectangle, splitFrom: rectangle.id })
abstract class Splitter {
constructor(public kerfSize: number) {}
abstract split(rectangle: Rectangle, item: Item): Rectangle[]
protected splitHorizontally(rectangle: Rectangle, item: Item): Rectangle[] {
debug(`splitting ${rectangle.id} horizontally`)
const rectangle1 = {
...createSplitRectangle(rectangle),
x: rectangle.x + item.width + this.kerfSize,
width: rectangle.width - item.width - this.kerfSize,
height: item.height,
id: 'sh-r1'
}
const rectangle2 = {
...createSplitRectangle(rectangle),
y: rectangle.y + item.height + this.kerfSize,
height: rectangle.height - item.height - this.kerfSize,
id: 'sh-r2'
}
return [rectangle1, rectangle2]
}
protected splitVertically(rectangle: Rectangle, item: Item): Rectangle[] {
debug(`splitting ${rectangle.id} vertically`)
const rectangle1 = {
...createSplitRectangle(rectangle),
y: rectangle.y + item.height + this.kerfSize,
width: item.width,
height: rectangle.height - item.height - this.kerfSize,
id: 'sh-r1'
}
const rectangle2 = {
...createSplitRectangle(rectangle),
x: rectangle.x + item.width + this.kerfSize,
y: rectangle.y,
width: rectangle.width - item.width - this.kerfSize,
id: 'sh-r2'
}
return [rectangle1, rectangle2]
}
}
class ShortAxisSplit extends Splitter {
split(rectangle: Rectangle, item: Item) {
if (rectangle.width < rectangle.height) {
return this.splitHorizontally(rectangle, item)
} else {
return this.splitVertically(rectangle, item)
}
}
}
class LongAxisSplit extends Splitter {
split(rectangle: Rectangle, item: Item) {
if (rectangle.width > rectangle.height) {
return this.splitHorizontally(rectangle, item)
} else {
return this.splitVertically(rectangle, item)
}
}
}
class ShorterLeftoverAxisSplit extends Splitter {
split(rectangle: Rectangle, item: Item) {
if (rectangle.width - item.width < rectangle.height - item.height) {
return this.splitHorizontally(rectangle, item)
} else {
return this.splitVertically(rectangle, item)
}
}
}
class LongerLeftoverAxisSplit extends Splitter {
split(rectangle: Rectangle, item: Item) {
if (rectangle.width - item.width >= rectangle.height - item.height) {
return this.splitHorizontally(rectangle, item)
} else {
return this.splitVertically(rectangle, item)
}
}
}
export enum SplitStrategy {
LongLeftoverAxisSplit,
ShortLeftoverAxisSplit,
LongAxisSplit,
ShortAxisSplit
}
export function GetSplitImplementation(strategy: SplitStrategy, kerfSize: number): Splitter {
switch (strategy) {
case SplitStrategy.LongAxisSplit:
return new LongAxisSplit(kerfSize)
case SplitStrategy.ShortAxisSplit:
return new ShortAxisSplit(kerfSize)
case SplitStrategy.LongLeftoverAxisSplit:
return new LongerLeftoverAxisSplit(kerfSize)
case SplitStrategy.ShortLeftoverAxisSplit:
return new ShorterLeftoverAxisSplit(kerfSize)
}
}