bin-packing-core
Advanced tools
Comparing version
@@ -43,6 +43,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return i + v.width; | ||
}, 0); | ||
}, 1); // 防止刚好踩到临界点情况 | ||
this.maxHeight = this.rects.reduce(function (i, v) { | ||
return i + v.height; | ||
}, 0); | ||
}, 1); // 防止刚好踩到临界点情况 | ||
} | ||
@@ -69,3 +69,3 @@ Object.defineProperty(Search.prototype, "minHeight", { | ||
}; | ||
for (var searchWidth = this.minWidth; searchWidth < this.maxWidth; searchWidth += this.step) { | ||
for (var searchWidth = this.minWidth; searchWidth <= this.maxWidth; searchWidth += this.step) { | ||
var _a = this.bestHeight(searchWidth), height = _a[0], op = _a[1]; | ||
@@ -72,0 +72,0 @@ if (op > bestResult.op) { |
{ | ||
"name": "bin-packing-core", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "image packer based on genetic & max-rect algorithm", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,2 +5,10 @@ # 基于人工智能的雪碧图拼接算法 | ||
## 应用场景 | ||
前端资源中经常需要将所有的小图合成大图,如何将合成大图的图片压缩到最小,就是本算法要解决的问题。 | ||
使用本算法可以有效减小图片资源的`解析时长`和`内存占用`。 | ||
基于目前亿级PV项目验证,可以减小10%~30%的内存占用。 | ||
## 算法 | ||
@@ -11,3 +19,3 @@ | ||
1. 底层是基于 `max-rect-bin-pack` 的 `Rectangle Bin Pack` 算法 | ||
2. 上层是基于 遗传算法 的最优化的搜索算法。 | ||
2. 上层是基于 遗传算法 的最优化的搜索算法。 // 更新了二分法 | ||
@@ -14,0 +22,0 @@ **FYI:** 底层算法分为`在线`和`离线`两种模式: 离线算法有一定的优化方案,所以离线算法效果更好。上层的遗传算法强制调用了离线算法。 |
@@ -34,9 +34,3 @@ import MaxRectBinPack, { FindPosition } from './max-rect-bin-pack'; | ||
*/ | ||
constructor( | ||
rects: Rect[], | ||
allowRotate: boolean, | ||
step: number, | ||
findPosition: FindPosition, | ||
rate: number, | ||
) { | ||
constructor(rects: Rect[], allowRotate: boolean, step: number, findPosition: FindPosition, rate: number) { | ||
this.rects = rects; | ||
@@ -55,6 +49,6 @@ this.allowRotate = allowRotate === true; | ||
return i + v.width; | ||
}, 0); | ||
}, 1); // 防止刚好踩到临界点情况 | ||
this.maxHeight = this.rects.reduce((i, v) => { | ||
return i + v.height; | ||
}, 0); | ||
}, 1); // 防止刚好踩到临界点情况 | ||
} | ||
@@ -67,7 +61,3 @@ public search(): IPoint { | ||
}; | ||
for ( | ||
let searchWidth = this.minWidth; | ||
searchWidth < this.maxWidth; | ||
searchWidth += this.step | ||
) { | ||
for (let searchWidth = this.minWidth; searchWidth <= this.maxWidth; searchWidth += this.step) { | ||
const [height, op] = this.bestHeight(searchWidth); | ||
@@ -74,0 +64,0 @@ if (op > bestResult.op) { |
@@ -1,1 +0,1 @@ | ||
var data = [{"x":0,"y":0,"width":259,"height":165,"isRotated":false,"info":{"width":259,"height":165}},{"x":0,"y":165,"width":203,"height":115,"isRotated":false,"info":{"width":203,"height":115}},{"x":203,"y":165,"width":261,"height":106,"isRotated":false,"info":{"width":261,"height":106}},{"x":259,"y":0,"width":265,"height":162,"isRotated":false,"info":{"width":265,"height":162}},{"x":464,"y":162,"width":266,"height":122,"isRotated":false,"info":{"width":266,"height":122}},{"x":524,"y":0,"width":211,"height":137,"isRotated":false,"info":{"width":211,"height":137}}] | ||
var data = [{"x":0,"y":0,"width":485,"height":869,"isRotated":false},{"x":0,"y":869,"width":96,"height":104,"isRotated":false}] |
@@ -11,14 +11,24 @@ //tslint:disable | ||
const r = require('./rects.json'); | ||
// const r = require('./rects.json'); | ||
r.forEach(($: any) => { | ||
var rect = new Rect(); | ||
rect.width = $.width; | ||
rect.height = $.height; | ||
rect.info = {}; | ||
rect.info.width = $.width; | ||
rect.info.height = $.height; | ||
rects.push(rect); | ||
}); | ||
// r.forEach(($: any) => { | ||
// var rect = new Rect(); | ||
// rect.width = $.width; | ||
// rect.height = $.height; | ||
// rect.info = {}; | ||
// rect.info.width = $.width; | ||
// rect.info.height = $.height; | ||
// rects.push(rect); | ||
// }); | ||
var rect1 = new Rect(); | ||
rect1.width = 485; | ||
rect1.height = 869; | ||
rects.push(rect1); | ||
var rect2 = new Rect(); | ||
rect2.width = 96; | ||
rect2.height = 104; | ||
rects.push(rect2); | ||
// function getRects() { | ||
@@ -28,11 +38,11 @@ // return rects.map($ => $.clone()); | ||
// const maxWidth = rects.reduce(($, $$) => $ + $$.width, 0); | ||
// const maxHeight = rects.reduce(($, $$) => $ + $$.height, 0); | ||
// const step = 10; | ||
// let maxWidth = rects.reduce(($, $$) => $ + $$.width, 0); | ||
// let maxHeight = rects.reduce(($, $$) => $ + $$.height, 0); | ||
// debugger; | ||
// const step = 1; | ||
// const result = []; | ||
// for (let currentWidth = 0; currentWidth < maxWidth; currentWidth += step) { | ||
// for (let currentWidth = 0; currentWidth <= maxWidth; currentWidth += step) { | ||
// for ( | ||
// let currentHeight = 0; | ||
// currentHeight < maxHeight; | ||
// currentHeight <= maxHeight; | ||
// currentHeight += step | ||
@@ -42,3 +52,4 @@ // ) { | ||
// const inserts = packer.insertRects(getRects(), 0); | ||
// if (inserts.length === r.length) { | ||
// if (inserts.length === 2) { | ||
// console.log(currentWidth, currentHeight); | ||
// result.push({ | ||
@@ -59,3 +70,3 @@ // width: currentWidth, | ||
// ); | ||
// debugger; | ||
const serach = new Search(rects, false, 1, 0, Infinity); | ||
@@ -62,0 +73,0 @@ const bestNode = serach.search(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
903381
0.03%2791
0.04%145
5.84%