-
Notifications
You must be signed in to change notification settings - Fork 35
/
maxrects-packer.ts
377 lines (352 loc) · 13.7 KB
/
maxrects-packer.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { Rectangle, IRectangle } from "./geom/Rectangle";
import { MaxRectsBin } from "./maxrects-bin";
import { OversizedElementBin } from "./oversized-element-bin";
import { Bin, IBin } from "./abstract-bin";
export const EDGE_MAX_VALUE: number = 4096;
export const EDGE_MIN_VALUE: number = 128;
export enum PACKING_LOGIC {
MAX_AREA = 0,
MAX_EDGE = 1
}
/**
* Options for MaxRect Packer
* @property {boolean} options.smart Smart sizing packer (default is true)
* @property {boolean} options.pot use power of 2 sizing (default is true)
* @property {boolean} options.square use square size (default is false)
* @property {boolean} options.allowRotation allow rotation packing (default is false)
* @property {boolean} options.tag allow auto grouping based on `rect.tag` (default is false)
* @property {boolean} options.exclusiveTag tagged rects will have dependent bin, if set to `false`, packer will try to put tag rects into the same bin (default is true)
* @property {boolean} options.border atlas edge spacing (default is 0)
* @property {PACKING_LOGIC} options.logic MAX_AREA or MAX_EDGE based sorting logic (default is MAX_EDGE)
* @export
* @interface Option
*/
export interface IOption {
smart?: boolean;
pot?: boolean;
square?: boolean;
allowRotation?: boolean;
tag?: boolean;
exclusiveTag?: boolean;
border?: number;
logic?: PACKING_LOGIC;
}
export class MaxRectsPacker<T extends IRectangle = Rectangle> {
/**
* The Bin array added to the packer
*
* @type {Bin[]}
* @memberof MaxRectsPacker
*/
public bins: Bin<T>[];
/**
* Options for MaxRect Packer
* @property {boolean} options.smart Smart sizing packer (default is true)
* @property {boolean} options.pot use power of 2 sizing (default is true)
* @property {boolean} options.square use square size (default is false)
* @property {boolean} options.allowRotation allow rotation packing (default is false)
* @property {boolean} options.tag allow auto grouping based on `rect.tag` (default is false)
* @property {boolean} options.exclusiveTag tagged rects will have dependent bin, if set to `false`, packer will try to put tag rects into the same bin (default is true)
* @property {boolean} options.border atlas edge spacing (default is 0)
* @property {PACKING_LOGIC} options.logic MAX_AREA or MAX_EDGE based sorting logic (default is MAX_EDGE)
* @export
* @interface Option
*/
public options: IOption = {
smart: true,
pot: true,
square: false,
allowRotation: false,
tag: false,
exclusiveTag: true,
border: 0,
logic: PACKING_LOGIC.MAX_EDGE
}
/**
* Creates an instance of MaxRectsPacker.
* @param {number} width of the output atlas (default is 4096)
* @param {number} height of the output atlas (default is 4096)
* @param {number} padding between glyphs/images (default is 0)
* @param {IOption} [options={}] (Optional) packing options
* @memberof MaxRectsPacker
*/
constructor (
public width: number = EDGE_MAX_VALUE,
public height: number = EDGE_MAX_VALUE,
public padding: number = 0,
options: IOption = {}
) {
this.bins = [];
this.options = { ...this.options, ...options };
}
/**
* Add a bin/rectangle object with data to packer
* @param {number} width of the input bin/rectangle
* @param {number} height of the input bin/rectangle
* @param {*} data custom data object
* @memberof MaxRectsPacker
*/
public add (width: number, height: number, data: any): T;
/**
* Add a bin/rectangle object extends IRectangle to packer
* @template T Generic type extends IRectangle interface
* @param {T} rect the rect object add to the packer bin
* @memberof MaxRectsPacker
*/
public add (rect: T): T;
public add (...args: any[]): any {
if (args.length === 1) {
if (typeof args[0] !== 'object') throw new Error("MacrectsPacker.add(): Wrong parameters");
const rect = args[0] as T;
if (rect.width > this.width || rect.height > this.height) {
this.bins.push(new OversizedElementBin<T>(rect));
} else {
let added = this.bins.slice(this._currentBinIndex).find(bin => bin.add(rect) !== undefined);
if (!added) {
let bin = new MaxRectsBin<T>(this.width, this.height, this.padding, this.options);
let tag = (rect.data && rect.data.tag) ? rect.data.tag : rect.tag ? rect.tag : undefined;
if (this.options.tag && tag) bin.tag = tag;
bin.add(rect);
this.bins.push(bin);
}
}
return rect;
} else {
const rect: IRectangle = new Rectangle(args[0], args[1]);
if (args.length > 2) rect.data = args[2];
if (rect.width > this.width || rect.height > this.height) {
this.bins.push(new OversizedElementBin<T>(rect as T));
} else {
let added = this.bins.slice(this._currentBinIndex).find(bin => bin.add(rect as T) !== undefined);
if (!added) {
let bin = new MaxRectsBin<T>(this.width, this.height, this.padding, this.options);
if (this.options.tag && rect.data.tag) bin.tag = rect.data.tag;
bin.add(rect as T);
this.bins.push(bin);
}
}
return rect as T;
}
}
/**
* Add an Array of bins/rectangles to the packer.
*
* `Javascript`: Any object has property: { width, height, ... } is accepted.
*
* `Typescript`: object shall extends `MaxrectsPacker.IRectangle`.
*
* note: object has `hash` property will have more stable packing result
*
* @param {IRectangle[]} rects Array of bin/rectangles
* @memberof MaxRectsPacker
*/
public addArray(rects: T[]) {
if (!this.options.tag || this.options.exclusiveTag) {
// if not using tag or using exclusiveTag, old approach
this.sort(rects, this.options.logic).forEach(rect => this.add(rect));
} else {
// sort rects by tags first
rects.sort((a,b) => {
const aTag = (a.data && a.data.tag) ? a.data.tag : a.tag ? a.tag : undefined;
const bTag = (b.data && b.data.tag) ? b.data.tag : b.tag ? b.tag : undefined;
return bTag === undefined ? -1 : aTag === undefined ? 1 : bTag > aTag ? -1 : 1;
});
// iterate all bins to find the first bin which can place rects with same tag
//
let currentTag: any;
let currentIdx: number = 0;
let targetBin = this.bins.slice(this._currentBinIndex).find(bin => {
let testBin = bin.clone();
for (let i = currentIdx; i < rects.length; i++) {
const rect = rects[i];
const tag = (rect.data && rect.data.tag) ? rect.data.tag : rect.tag ? rect.tag : undefined;
// initialize currentTag
if (i === 0) currentTag = tag;
if (tag !== currentTag) {
// all current tag memeber tested successfully
currentTag = tag;
// do addArray()
this.sort(rects.slice(currentIdx, i), this.options.logic).forEach(r => bin.add(r));
currentIdx = i;
testBin = bin.clone();
}
// remaining untagged rect will use normal addArray()
if (tag === undefined) {
// do addArray()
this.sort(rects.slice(i), this.options.logic).forEach(r => this.add(r));
currentIdx = rects.length;
// end test
return true;
}
// still in the same tag group
if (testBin.add(rect) === undefined) {
// current bin cannot contain all tag members
// procceed to test next bin
return false;
}
}
// all rects tested
// do addArray() to the remaining tag group
this.sort(rects.slice(currentIdx), this.options.logic).forEach(r => bin.add(r));
return true;
});
// create a new bin if no current bin fit
if (!targetBin) {
const rect = rects[currentIdx];
const bin = new MaxRectsBin<T>(this.width, this.height, this.padding, this.options);
const tag = (rect.data && rect.data.tag) ? rect.data.tag : rect.tag ? rect.tag : undefined;
if (this.options.tag && this.options.exclusiveTag && tag) bin.tag = tag;
this.bins.push(bin);
this.addArray(rects.slice(currentIdx));
}
}
}
/**
* Reset entire packer to initial states, keep settings
*
* @memberof MaxRectsPacker
*/
public reset (): void {
this.bins = [];
this._currentBinIndex = 0;
}
/**
* Repack all elements inside bins
*
* @param {boolean} [quick=true] quick repack only dirty bins
* @returns {void}
* @memberof MaxRectsPacker
*/
public repack (quick: boolean = true): void {
if (quick) {
let unpack: T[] = [];
for (let bin of this.bins) {
if (bin.dirty) {
let up = bin.repack();
if (up) unpack.push(...up);
}
}
this.addArray(unpack);
return;
}
if (!this.dirty) return;
const allRects = this.rects;
this.reset();
this.addArray(allRects);
}
/**
* Stop adding new element to the current bin and return a new bin.
*
* note: After calling `next()` all elements will no longer added to previous bins.
*
* @returns {Bin}
* @memberof MaxRectsPacker
*/
public next (): number {
this._currentBinIndex = this.bins.length;
return this._currentBinIndex;
}
/**
* Load bins to the packer, overwrite exist bins
* @param {MaxRectsBin[]} bins MaxRectsBin objects
* @memberof MaxRectsPacker
*/
public load (bins: IBin[]) {
bins.forEach((bin, index) => {
if (bin.maxWidth > this.width || bin.maxHeight > this.height) {
this.bins.push(new OversizedElementBin(bin.width, bin.height, {}));
} else {
let newBin = new MaxRectsBin<T>(this.width, this.height, this.padding, bin.options);
newBin.freeRects.splice(0);
bin.freeRects.forEach((r, i) => {
newBin.freeRects.push(new Rectangle(r.width, r.height, r.x, r.y));
});
newBin.width = bin.width;
newBin.height = bin.height;
if (bin.tag) newBin.tag = bin.tag;
this.bins[index] = newBin;
}
}, this);
}
/**
* Output current bins to save
* @memberof MaxRectsPacker
*/
public save (): IBin[] {
let saveBins: IBin[] = [];
this.bins.forEach((bin => {
let saveBin: IBin = {
width: bin.width,
height: bin.height,
maxWidth: bin.maxWidth,
maxHeight: bin.maxHeight,
freeRects: [],
rects: [],
options: bin.options
};
if (bin.tag) saveBin = { ...saveBin, tag: bin.tag };
bin.freeRects.forEach(r => {
saveBin.freeRects.push({
x: r.x,
y: r.y,
width: r.width,
height: r.height
});
});
saveBins.push(saveBin);
}));
return saveBins;
}
/**
* Sort the given rects based on longest edge or surface area.
*
* If rects have the same sort value, will sort by second key `hash` if presented.
*
* @private
* @param {T[]} rects
* @param {PACKING_LOGIC} [logic=PACKING_LOGIC.MAX_EDGE] sorting logic, "area" or "edge"
* @returns
* @memberof MaxRectsPacker
*/
private sort (rects: T[], logic: IOption['logic'] = PACKING_LOGIC.MAX_EDGE) {
return rects.slice().sort((a, b) => {
const result = (logic === PACKING_LOGIC.MAX_EDGE) ?
Math.max(b.width, b.height) - Math.max(a.width, a.height) :
b.width * b.height - a.width * a.height;
if (result === 0 && a.hash && b.hash) {
return a.hash > b.hash ? -1 : 1;
} else return result;
});
}
private _currentBinIndex: number = 0;
/**
* Return current functioning bin index, perior to this wont accept any new elements
*
* @readonly
* @type {number}
* @memberof MaxRectsPacker
*/
get currentBinIndex (): number { return this._currentBinIndex; }
/**
* Returns dirty status of all child bins
*
* @readonly
* @type {boolean}
* @memberof MaxRectsPacker
*/
get dirty (): boolean { return this.bins.some(bin => bin.dirty); }
/**
* Return all rectangles in this packer
*
* @readonly
* @type {T[]}
* @memberof MaxRectsPacker
*/
get rects (): T[] {
let allRects: T[] = [];
for (let bin of this.bins) {
allRects.push(...bin.rects);
}
return allRects;
}
}