bin-packing-core
Advanced tools
Comparing version 0.1.3 to 0.1.4
import genetic, { Genetic } from './genetic'; | ||
import MaxRectBinPack, { FindPosition } from './max-rect-bin-pack'; | ||
import Rect from './rect'; | ||
import { Search } from './search'; | ||
export { FindPosition, Genetic, // 遗传算法类 | ||
MaxRectBinPack, Rect, genetic, }; | ||
MaxRectBinPack, Rect, genetic, // 包装方法 | ||
Search, }; |
@@ -17,3 +17,3 @@ var __importStar = (this && this.__importStar) || function (mod) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./genetic", "./max-rect-bin-pack", "./rect"], factory); | ||
define(["require", "exports", "./genetic", "./max-rect-bin-pack", "./rect", "./search"], factory); | ||
} | ||
@@ -31,3 +31,5 @@ })(function (require, exports) { | ||
exports.Rect = rect_1.default; | ||
var search_1 = require("./search"); | ||
exports.Search = search_1.Search; | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "bin-packing-core", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "image packer based on genetic & max-rect algorithm", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -113,2 +113,21 @@ # 基于人工智能的雪碧图拼接算法 | ||
5. `搜索算法` | ||
```typescript | ||
/** | ||
* 初始化 | ||
* @param rects 要插入的矩形数组 | ||
* @param allowRotate 是否旋转 | ||
* @param step 搜索步长 建议10 | ||
* @param findPosition FindPostion 策略 | ||
* @param rate 大于一的比率 等于1不可以的 | ||
*/ | ||
const serach = new Search(rects, false, 10, 0, 1.1); | ||
const bestNode = serach.search(); | ||
console.log(bestNode); | ||
const packer = new MaxRectBinPack(bestNode.x, bestNode.y, false); | ||
const result = packer.insertRects(rects, FindPosition.AreaFit); | ||
``` | ||
## TODO | ||
@@ -118,2 +137,2 @@ | ||
- [ ] 返回结果优化 | ||
- [ ] 提升算法稳定性,回归点有多个的时候会出现回归到次点的情况 | ||
- [X] 提升算法稳定性,回归点有多个的时候会出现回归到次点的情况 |
import genetic, { Genetic, IGeneticOptions } from './genetic'; | ||
import MaxRectBinPack, { FindPosition } from './max-rect-bin-pack'; | ||
import Rect from './rect'; | ||
import { Search } from './search'; | ||
@@ -11,2 +12,3 @@ export { | ||
genetic, // 包装方法 | ||
Search, | ||
}; |
@@ -1,1 +0,1 @@ | ||
var data = [{"x":0,"y":0,"width":121,"height":385,"isRotated":true},{"x":121,"y":0,"width":121,"height":382,"isRotated":true},{"x":242,"y":0,"width":121,"height":379,"isRotated":true},{"x":363,"y":0,"width":121,"height":375,"isRotated":true},{"x":484,"y":0,"width":121,"height":356,"isRotated":true},{"x":605,"y":0,"width":128,"height":342,"isRotated":true},{"x":733,"y":0,"width":122,"height":339,"isRotated":true},{"x":855,"y":0,"width":121,"height":327,"isRotated":true},{"x":976,"y":0,"width":121,"height":224,"isRotated":true},{"x":976,"y":224,"width":121,"height":130,"isRotated":true},{"x":1097,"y":0,"width":428,"height":124,"isRotated":false},{"x":1525,"y":0,"width":121,"height":206,"isRotated":true},{"x":1097,"y":124,"width":422,"height":123,"isRotated":false},{"x":1097,"y":247,"width":387,"height":122,"isRotated":false},{"x":1484,"y":247,"width":190,"height":121,"isRotated":false}] | ||
var data = [{"x":0,"y":0,"width":428,"height":532,"isRotated":false,"info":{"name":"tileset-ornaments-sets-wild","width":428,"height":532}},{"x":0,"y":532,"width":422,"height":523,"isRotated":false,"info":{"name":"tileset-ornaments-sets-cook","width":422,"height":523}},{"x":0,"y":1055,"width":427,"height":517,"isRotated":false,"info":{"name":"tileset-ornaments-sets-spring-festival","width":427,"height":517}},{"x":422,"y":532,"width":428,"height":508,"isRotated":false,"info":{"name":"tileset-ornaments-sets-bear","width":428,"height":508}},{"x":428,"y":0,"width":423,"height":498,"isRotated":false,"info":{"name":"tileset-ornaments-sets-huabei","width":423,"height":498}},{"x":0,"y":1572,"width":422,"height":497,"isRotated":false,"info":{"name":"tileset-ornaments-sets-rich","width":422,"height":497}},{"x":0,"y":2069,"width":430,"height":493,"isRotated":false,"info":{"name":"tileset-ornaments-sets-lili","width":430,"height":493}},{"x":0,"y":2562,"width":232,"height":175,"isRotated":false,"info":{"name":"tileset-ornaments-sGlass-rich","width":232,"height":175}},{"x":422,"y":1572,"width":423,"height":490,"isRotated":false,"info":{"name":"tileset-ornaments-sets-mike","width":423,"height":490}},{"x":232,"y":2562,"width":252,"height":169,"isRotated":false,"info":{"name":"tileset-ornaments-fGlass-bear","width":252,"height":169}},{"x":430,"y":2062,"width":422,"height":489,"isRotated":false,"info":{"name":"tileset-ornaments-sets-lady","width":422,"height":489}},{"x":484,"y":2551,"width":298,"height":187,"isRotated":false,"info":{"name":"tileset-ornaments-fGlass-rich","width":298,"height":187}},{"x":782,"y":2551,"width":130,"height":131,"isRotated":false,"info":{"name":"tileset-ornaments-fSlag-apple","width":130,"height":131}},{"x":912,"y":2551,"width":130,"height":131,"isRotated":false,"info":{"name":"tileset-ornaments-fSlag-bunch","width":130,"height":131}},{"x":427,"y":1040,"width":339,"height":362,"isRotated":false,"info":{"name":"tileset-ornaments-sCap-bear","width":339,"height":362}},{"x":427,"y":1402,"width":130,"height":131,"isRotated":false,"info":{"name":"tileset-ornaments-fSlag-donut","width":130,"height":131}},{"x":557,"y":1402,"width":130,"height":131,"isRotated":false,"info":{"name":"tileset-ornaments-fSlag-grape","width":130,"height":131}},{"x":687,"y":1402,"width":130,"height":131,"isRotated":false,"info":{"name":"tileset-ornaments-fSlag-icecream","width":130,"height":131}},{"x":766,"y":1040,"width":387,"height":360,"isRotated":false,"info":{"name":"tileset-ornaments-fCap-bear","width":387,"height":360}},{"x":850,"y":498,"width":361,"height":296,"isRotated":false,"info":{"name":"tileset-ornaments-fNecklace-rich","width":361,"height":296}},{"x":851,"y":794,"width":386,"height":242,"isRotated":false,"info":{"name":"tileset-ornaments-fNecklace-cook","width":386,"height":242}},{"x":851,"y":0,"width":381,"height":293,"isRotated":false,"info":{"name":"tileset-ornaments-fCoat-lady","width":381,"height":293}},{"x":1211,"y":293,"width":356,"height":289,"isRotated":false,"info":{"name":"tileset-ornaments-sNecklace-rich","width":356,"height":289}},{"x":1211,"y":582,"width":397,"height":211,"isRotated":false,"info":{"name":"tileset-ornaments-sNecklace-spring-festival","width":397,"height":211}},{"x":1232,"y":0,"width":379,"height":285,"isRotated":false,"info":{"name":"tileset-ornaments-fCoat-cook","width":379,"height":285}},{"x":1567,"y":285,"width":385,"height":282,"isRotated":false,"info":{"name":"tileset-ornaments-sCoat-cook","width":385,"height":282}},{"x":1611,"y":0,"width":381,"height":277,"isRotated":false,"info":{"name":"tileset-ornaments-fCoat-huabei","width":381,"height":277}},{"x":1992,"y":0,"width":406,"height":255,"isRotated":false,"info":{"name":"tileset-ornaments-fNecklace-spring-festival","width":406,"height":255}},{"x":2398,"y":0,"width":331,"height":257,"isRotated":false,"info":{"name":"tileset-ornaments-fCap-cook","width":331,"height":257}},{"x":1992,"y":255,"width":400,"height":274,"isRotated":false,"info":{"name":"tileset-ornaments-fCoat-mike","width":400,"height":274}},{"x":2392,"y":257,"width":342,"height":209,"isRotated":false,"info":{"name":"tileset-ornaments-fGlass-wild","width":342,"height":209}},{"x":1992,"y":529,"width":397,"height":262,"isRotated":false,"info":{"name":"tileset-ornaments-sCoat-wild","width":397,"height":262}},{"x":1608,"y":567,"width":384,"height":252,"isRotated":false,"info":{"name":"tileset-ornaments-sCoat-huabei","width":384,"height":252}},{"x":1237,"y":793,"width":370,"height":252,"isRotated":false,"info":{"name":"tileset-ornaments-fCap-lili","width":370,"height":252}},{"x":1237,"y":1045,"width":369,"height":267,"isRotated":false,"info":{"name":"tileset-ornaments-fCoat-bear","width":369,"height":267}},{"x":2392,"y":466,"width":331,"height":227,"isRotated":false,"info":{"name":"tileset-ornaments-sCap-huabei","width":331,"height":227}},{"x":2389,"y":693,"width":327,"height":227,"isRotated":false,"info":{"name":"tileset-ornaments-sCap-mike","width":327,"height":227}},{"x":1992,"y":791,"width":394,"height":270,"isRotated":false,"info":{"name":"tileset-ornaments-sCoat-lady","width":394,"height":270}},{"x":1606,"y":1045,"width":386,"height":254,"isRotated":false,"info":{"name":"tileset-ornaments-sCoat-lili","width":386,"height":254}},{"x":1607,"y":819,"width":381,"height":226,"isRotated":false,"info":{"name":"tileset-ornaments-sNecklace-cook","width":381,"height":226}},{"x":2386,"y":920,"width":352,"height":214,"isRotated":false,"info":{"name":"tileset-ornaments-sCap-rich","width":352,"height":214}},{"x":2386,"y":1134,"width":352,"height":211,"isRotated":false,"info":{"name":"tileset-ornaments-fCap-rich","width":352,"height":211}},{"x":1992,"y":1061,"width":387,"height":266,"isRotated":false,"info":{"name":"tileset-ornaments-sCoat-bear","width":387,"height":266}},{"x":1606,"y":1299,"width":386,"height":250,"isRotated":false,"info":{"name":"tileset-ornaments-sCoat-mike","width":386,"height":250}},{"x":1992,"y":1327,"width":382,"height":264,"isRotated":false,"info":{"name":"tileset-ornaments-fCoat-lili","width":382,"height":264}},{"x":2374,"y":1345,"width":364,"height":272,"isRotated":false,"info":{"name":"tileset-ornaments-fNecklace-lili","width":364,"height":272}},{"x":1992,"y":1591,"width":382,"height":227,"isRotated":false,"info":{"name":"tileset-ornaments-sCoat-rich","width":382,"height":227}},{"x":1992,"y":1818,"width":381,"height":263,"isRotated":false,"info":{"name":"tileset-ornaments-fCoat-wild","width":381,"height":263}},{"x":2374,"y":1617,"width":360,"height":225,"isRotated":false,"info":{"name":"tileset-ornaments-fCap-lady","width":360,"height":225}},{"x":2373,"y":1842,"width":359,"height":227,"isRotated":false,"info":{"name":"tileset-ornaments-sCap-lady","width":359,"height":227}},{"x":2373,"y":2069,"width":356,"height":231,"isRotated":false,"info":{"name":"tileset-ornaments-fCap-mike","width":356,"height":231}},{"x":852,"y":2300,"width":190,"height":248,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-bunch1","width":190,"height":248}},{"x":852,"y":2081,"width":206,"height":217,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-grape1","width":206,"height":217}},{"x":1153,"y":1312,"width":381,"height":235,"isRotated":false,"info":{"name":"tileset-ornaments-sCap-lili","width":381,"height":235}},{"x":845,"y":1400,"width":307,"height":258,"isRotated":false,"info":{"name":"tileset-ornaments-fCap-wild","width":307,"height":258}},{"x":1152,"y":1547,"width":375,"height":229,"isRotated":false,"info":{"name":"tileset-ornaments-fCoat-rich","width":375,"height":229}},{"x":845,"y":1658,"width":305,"height":264,"isRotated":false,"info":{"name":"tileset-ornaments-sCap-wild","width":305,"height":264}},{"x":1527,"y":1549,"width":323,"height":235,"isRotated":false,"info":{"name":"tileset-ornaments-sCap-cook","width":323,"height":235}},{"x":1150,"y":1776,"width":315,"height":260,"isRotated":false,"info":{"name":"tileset-ornaments-fCap-spring-festival","width":315,"height":260}},{"x":1150,"y":2036,"width":315,"height":256,"isRotated":false,"info":{"name":"tileset-ornaments-sCap-spring-festival","width":315,"height":256}},{"x":1042,"y":2300,"width":190,"height":248,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-bunch2","width":190,"height":248}},{"x":1232,"y":2300,"width":190,"height":248,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-bunch3","width":190,"height":248}},{"x":1422,"y":2300,"width":206,"height":237,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-icecream1","width":206,"height":237}},{"x":1465,"y":2081,"width":206,"height":217,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-grape2","width":206,"height":217}},{"x":1671,"y":1784,"width":288,"height":222,"isRotated":false,"info":{"name":"tileset-ornaments-fCap-huabei","width":288,"height":222}},{"x":1465,"y":1784,"width":206,"height":237,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-icecream2","width":206,"height":237}},{"x":1671,"y":2006,"width":224,"height":222,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-donut1","width":224,"height":222}},{"x":1671,"y":2228,"width":224,"height":222,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-donut2","width":224,"height":222}},{"x":1671,"y":2450,"width":206,"height":237,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-icecream3","width":206,"height":237}},{"x":1877,"y":2450,"width":217,"height":235,"isRotated":false,"info":{"name":"tileset-ornaments-fGlass-mike","width":217,"height":235}},{"x":2094,"y":2081,"width":224,"height":222,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-donut3","width":224,"height":222}},{"x":2094,"y":2303,"width":206,"height":217,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-grape3","width":206,"height":217}},{"x":2094,"y":2520,"width":206,"height":214,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-apple1","width":206,"height":214}},{"x":2300,"y":2520,"width":206,"height":214,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-apple2","width":206,"height":214}},{"x":2300,"y":2303,"width":206,"height":214,"isRotated":false,"info":{"name":"tileset-ornaments-snacks-apple3","width":206,"height":214}}] |
@@ -7,9 +7,9 @@ //tslint:disable | ||
import Rect from './../src/rect'; | ||
import { Search } from './../src/search'; | ||
const rects: Rect[] = []; | ||
const r = require('./rects.json'); | ||
const r = [{"x":0,"y":0,"width":387,"height":122,"isRotated":false,"info":{"name":"tileset-ornaments-001"}},{"x":0,"y":0,"width":356,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-002"}},{"x":0,"y":0,"width":379,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-003"}},{"x":0,"y":0,"width":375,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-004"}},{"x":0,"y":0,"width":342,"height":128,"isRotated":false,"info":{"name":"tileset-ornaments-005"}},{"x":0,"y":0,"width":130,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-006"}},{"x":0,"y":0,"width":339,"height":122,"isRotated":false,"info":{"name":"tileset-ornaments-007"}},{"x":0,"y":0,"width":327,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-008"}},{"x":0,"y":0,"width":385,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-009"}},{"x":0,"y":0,"width":382,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-010"}},{"x":0,"y":0,"width":422,"height":123,"isRotated":false,"info":{"name":"tileset-ornaments-011"}},{"x":0,"y":0,"width":428,"height":124,"isRotated":false,"info":{"name":"tileset-ornaments-012"}},{"x":0,"y":0,"width":190,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-013"}},{"x":0,"y":0,"width":224,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-014"}},{"x":0,"y":0,"width":206,"height":121,"isRotated":false,"info":{"name":"tileset-ornaments-015"}}] | ||
r.forEach($=>{ | ||
r.forEach(($: any) => { | ||
var rect = new Rect(); | ||
@@ -22,22 +22,72 @@ rect.width = $.width; | ||
rects.push(rect); | ||
}) | ||
const bestNode = genetic(rects, { | ||
findPosition: FindPosition.AreaFit, | ||
lifeTimes: 50, | ||
liveRate: 0.5, | ||
size: 50, | ||
}); | ||
console.log(JSON.stringify(bestNode)); | ||
console.log(bestNode.x * bestNode.y); | ||
const width = bestNode.x; //Math.sqrt(size); | ||
const height = bestNode.y; //size / width; | ||
// const bestNode = genetic(rects, { | ||
// findPosition: FindPosition.AreaFit, | ||
// lifeTimes: 50, | ||
// liveRate: 0.5, | ||
// size: 50, | ||
// allowRotate: false, | ||
// }); | ||
// console.log(JSON.stringify(bestNode)); | ||
// console.log(bestNode.x * bestNode.y); | ||
// const width = bestNode.x; // Math.sqrt(size); | ||
// const height = bestNode.y; // size / width; | ||
const packer = new MaxRectBinPack(width, height, true); | ||
// const result: any[] = []; | ||
// for (const rect of rects) { | ||
// result.push(packer.insert(rect.width, rect.height, FindPosition.AreaFit)); | ||
// const packer = new MaxRectBinPack(width, height, false); | ||
// // const result: any[] = []; | ||
// // for (const rect of rects) { | ||
// // result.push(packer.insert(rect.width, rect.height, FindPosition.AreaFit)); | ||
// // } | ||
// let rectslength = rects.length; | ||
// console.log(rectslength); | ||
// const result = packer.insertRects(rects, FindPosition.AreaFit); | ||
// console.log(result.length); | ||
// console.log(result.length === rectslength); | ||
// function getRects() { | ||
// return rects.map($ => $.clone()); | ||
// } | ||
// const maxWidth = rects.reduce(($, $$) => $ + $$.width, 0); | ||
// const maxHeight = rects.reduce(($, $$) => $ + $$.height, 0); | ||
// const step = 10; | ||
// const result = []; | ||
// for (let currentWidth = 0; currentWidth < maxWidth; currentWidth += step) { | ||
// for ( | ||
// let currentHeight = 0; | ||
// currentHeight < maxHeight; | ||
// currentHeight += step | ||
// ) { | ||
// const packer = new MaxRectBinPack(currentWidth, currentHeight, true); | ||
// const inserts = packer.insertRects(getRects(), 0); | ||
// if (inserts.length === r.length) { | ||
// result.push({ | ||
// width: currentWidth, | ||
// height: currentHeight, | ||
// op: packer.occupancy(), | ||
// }); | ||
// } | ||
// } | ||
// } | ||
// fs.writeFileSync( | ||
// path.join(__dirname, 'data.js'), | ||
// `var data = ${JSON.stringify(result)}`, | ||
// { | ||
// flag: 'w+', | ||
// }, | ||
// ); | ||
const serach = new Search(rects, false, 10, 0, 1.1); | ||
const bestNode = serach.search(); | ||
console.log(bestNode); | ||
const packer = new MaxRectBinPack(bestNode.x, bestNode.y, false); | ||
let rectslength = rects.length; | ||
console.log(rectslength); | ||
const result = packer.insertRects(rects, FindPosition.AreaFit); | ||
console.log(packer.occupancy()); | ||
console.log(result.length); | ||
console.log(bestNode.x * bestNode.y); | ||
console.log(result.length === rectslength); | ||
@@ -44,0 +94,0 @@ fs.writeFileSync( |
@@ -6,2 +6,3 @@ { | ||
"rules": { | ||
"arrow-parens": false, | ||
"quotemark": [true, "single", "avoid-escape", "avoid-template"], | ||
@@ -8,0 +9,0 @@ "no-var-requires": false, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
913996
34
2813
137
2
33
3