3d-bin-packing-ts

中文文档
1.0.1发布于 2025/09/17MITTypeScriptESM + CJS近 30 天下载 1795

A TypeScript implementation of the EB AFIT algorithm for 3D container packing

原始使用文档
文档目录9 节
  1. Features
  2. Usage
  3. Installation
  4. Basic Usage
  5. Packing Multiple Containers
  6. Incremental Packing
  7. API Reference
  8. Classes
  9. Interfaces and Types

3D Container Packing in TypeScript

npm version

This is a TypeScript port of the 3DContainerPacking C# library by davidmchapman.

It contains an implementation of the EB-AFIT algorithm, a heuristic approach designed to solve the three-dimensional pallet-packing problem, also known as 3D bin packing. It was developed by Erhan Baltacıoğlu (EB) at the U.S. Air Force Institute of Technology (AFIT) as part of his master's thesis in 2001, and later described in a paper co-authored with James T. Moore and Raymond R. Hill Jr. in 2006. If you want to learn more about the algorithm and its' history, visit reference implementation repository: https://github.com/wknechtel/3d-bin-pack.

This TypeScript port includes all the original functionality plus a new packIncremental feature that automatically distributes items across multiple containers of the same dimensions.

Features

  • Pack items into containers with optimal space utilization.
  • Incremental packing to distribute items across multiple containers of the same size.
  • TypeScript support with full type definitions.

Check out the examples folder for runnable code samples.

Usage

Installation

npm install 3d-bin-packing-ts

Basic Usage

import { Container, Item, PackingService, PackingAlgorithmType } from '3d-bin-packing-ts';

// Create a container
const container = new Container('container1', 100, 100, 100);

// Create items to pack
const items = [
  new Item('item1', 30, 40, 50, 1),
  new Item('item2', 20, 30, 40, 2),
  new Item('item3', 10, 20, 30, 3)
];

// Pack items into the container
const result = PackingService.packSingle(container, items);

// Check if all items were packed
console.log(`All items packed: ${result.algorithmPackingResults[0].isCompletePacked}`);

// Get packed items with their positions
const packedItems = result.algorithmPackingResults[0].packedItems;
console.log('Packed items:', packedItems);

// Get unpacked items
const unpackedItems = result.algorithmPackingResults[0].unpackedItems;
console.log('Unpacked items:', unpackedItems);

Packing Multiple Containers

import { Container, Item, PackingService } from '3d-bin-packing-ts';

// Create multiple containers
const containers = [
  new Container('container1', 100, 100, 100),
  new Container('container2', 150, 150, 150)
];

// Create items to pack
const items = [
  new Item('item1', 30, 40, 50, 1),
  new Item('item2', 20, 30, 40, 2),
  new Item('item3', 10, 20, 30, 3)
];

// Pack items into containers
const results = PackingService.pack(containers, items);

// Process results for each container
results.forEach(result => {
  console.log(`Container ${result.containerId}:`);
  console.log(`- Packed items: ${result.algorithmPackingResults[0].packedItems.length}`);
  console.log(`- Unpacked items: ${result.algorithmPackingResults[0].unpackedItems.length}`);
  console.log(`- Volume utilization: ${result.algorithmPackingResults[0].percentContainerVolumePacked}%`);
});

Incremental Packing

import { Item, PackingService } from '3d-bin-packing-ts';

// Create items to pack
const items = [
  new Item('item1', 30, 40, 50, 1),
  new Item('item2', 20, 30, 40, 2),
  new Item('item3', 10, 20, 30, 3)
];

// Define container dimensions
const containerDimensions = { length: 100, width: 100, height: 100 };

// Pack items incrementally (creates as many containers as needed)
const result = PackingService.packIncremental(items, containerDimensions);

// Get information about containers and item distribution
console.log(`Number of containers needed: ${result.containers.length}`);

// Get aggregated results (which items went into which container)
result.aggregatedResults.forEach(aggResult => {
  console.log(`Container ${aggResult.containerId}:`);
  aggResult.itemQuantities.forEach(itemQty => {
    console.log(`- Item ${itemQty.itemId}: ${itemQty.quantity} units`);
  });
});

API Reference

Classes

Container

Represents a 3D container to pack items into.

new Container(id: string, length: number, width: number, height: number)

Item

Represents a 3D item to be packed.

new Item(id: string, dim1: number, dim2: number, dim3: number, quantity: number)

PackingService

Static service for packing operations.

Methods:

  • packSingle(container, items, algorithmTypeIDs?): Pack items into a single container
  • pack(containers, items, algorithmTypeIDs?): Pack items into multiple containers
  • packIncremental(items, containerDimensions, maxVolumePercentage?, idGenerator?): Pack items into as many containers as needed

Interfaces and Types

AlgorithmPackingResult

Contains the results of a packing operation.

Properties:

  • packedItems: Array of packed items with their positions
  • unpackedItems: Array of items that couldn't be packed
  • isCompletePacked: Boolean indicating if all items were packed
  • packTimeMilliseconds: Time taken for packing
  • percentContainerVolumePacked: Percentage of container volume utilized
  • percentItemVolumePacked: Percentage of total item volume packed

版本与发布记录

3d-bin-packing-ts 共发布 2 个版本,首次发布于 2025/09/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2025/09/171.1 年前
1.0.02025/09/171.1 年前

关于 3d-bin-packing-ts 的常见问题

3d-bin-packing-ts 是什么?

A TypeScript implementation of the EB AFIT algorithm for 3D container packing

如何安装 3d-bin-packing-ts?

在项目目录运行 npm install 3d-bin-packing-ts,或使用 pnpm add / yarn add / bun add。

3d-bin-packing-ts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

3d-bin-packing-ts 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

3d-bin-packing-ts 可以商用吗?

3d-bin-packing-ts 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

3d-bin-packing-ts 的下载量有多少?

根据 npm 官方下载统计,3d-bin-packing-ts 在 2026/10/04 – 2026/11/02 这 30 天内被下载 1,795 次,日均约 60 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

3d-bin-packing-ts 还在维护吗?

最新版本 1.0.1 发布于 2025/09/17(1.1 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/09/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。