@packvium/engine 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +55 -0
- package/contact-graph.js +61 -0
- package/fallback.js +1685 -0
- package/index.d.ts +53 -0
- package/index.js +30 -0
- package/package.json +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Packvium contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @packvium/engine
|
|
2
|
+
|
|
3
|
+
Deterministic 3D cartonization for Node.js. It uses the optional native engine when
|
|
4
|
+
available and automatically falls back to the bundled JavaScript implementation.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @packvium/engine
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Node.js 16 or later is required. Use a currently supported Node.js release in
|
|
13
|
+
production.
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { backend, pack } from '@packvium/engine';
|
|
19
|
+
|
|
20
|
+
const result = pack({
|
|
21
|
+
items: [{
|
|
22
|
+
id: 'book', quantity: 4,
|
|
23
|
+
dimensions: { length: '210', width: '140', height: '30' },
|
|
24
|
+
}],
|
|
25
|
+
containers: [{
|
|
26
|
+
id: 'carton',
|
|
27
|
+
inner_dimensions: { length: '400', width: '300', height: '250' },
|
|
28
|
+
}],
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
console.log(backend()); // "rust" or "javascript"
|
|
32
|
+
console.log(result.status); // "feasible"
|
|
33
|
+
console.log(result.containers);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- Exact, deterministic placement with no floating-point geometry decisions.
|
|
39
|
+
- Rotation, payload, stackability, support, clearance, obstacle and tag constraints.
|
|
40
|
+
- Multiple container types and clear explanations for unpacked items.
|
|
41
|
+
- JSON input/output through `pack()` or `packJson()`.
|
|
42
|
+
- Optional payload rebalancing with `rebalanceWeight()`.
|
|
43
|
+
- Loading and removal sequence helpers for already placed boxes.
|
|
44
|
+
|
|
45
|
+
The native addon is optional. `npm install` works on unsupported platforms too; call
|
|
46
|
+
`backend()` if your application needs to know which implementation handled a request.
|
|
47
|
+
|
|
48
|
+
## API and support
|
|
49
|
+
|
|
50
|
+
TypeScript declarations are included. See the package's `index.d.ts` for the complete
|
|
51
|
+
request and result types. Report security issues through [SECURITY.md](SECURITY.md).
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT. See [LICENSE](LICENSE).
|
package/contact-graph.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Direct vertical-contact graph with a deterministic XY broad phase.
|
|
3
|
+
*
|
|
4
|
+
* Construction is expected O(n + q + e): q broad-phase candidates plus e real
|
|
5
|
+
* contact edges. A physically dense graph can still have e=O(n²), which no exact
|
|
6
|
+
* representation can avoid. The exact overlap function remains authoritative.
|
|
7
|
+
* This module is package-internal: package.json exports only the root entry point.
|
|
8
|
+
*/
|
|
9
|
+
export function buildContactGraph(boxes, overlapXY) {
|
|
10
|
+
const supporters = boxes.map(() => []);
|
|
11
|
+
const children = boxes.map(() => []);
|
|
12
|
+
if (boxes.length === 0) return { supporters, children, candidateChecks: 0 };
|
|
13
|
+
|
|
14
|
+
const cell = Math.max(1, ...boxes.map(box => Math.max(box.d[0], box.d[1])));
|
|
15
|
+
const byTop = new Map();
|
|
16
|
+
const levels = new Map();
|
|
17
|
+
const cells = box => {
|
|
18
|
+
const x1 = Math.floor(box.x / cell);
|
|
19
|
+
const x2 = Math.floor((box.x + box.d[0] - 1) / cell);
|
|
20
|
+
const y1 = Math.floor(box.y / cell);
|
|
21
|
+
const y2 = Math.floor((box.y + box.d[1] - 1) / cell);
|
|
22
|
+
return [...new Set([`${x1}:${y1}`, `${x2}:${y1}`, `${x1}:${y2}`, `${x2}:${y2}`])];
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
boxes.forEach((box, index) => {
|
|
26
|
+
const top = box.z + box.d[2];
|
|
27
|
+
if (!byTop.has(top)) byTop.set(top, []);
|
|
28
|
+
byTop.get(top).push(index);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
let candidateChecks = 0;
|
|
32
|
+
boxes.forEach((upper, upperIndex) => {
|
|
33
|
+
const candidates = byTop.get(upper.z);
|
|
34
|
+
if (!candidates) return;
|
|
35
|
+
let level = levels.get(upper.z);
|
|
36
|
+
if (level == null) {
|
|
37
|
+
level = new Map();
|
|
38
|
+
for (const index of candidates) {
|
|
39
|
+
for (const key of cells(boxes[index])) {
|
|
40
|
+
if (!level.has(key)) level.set(key, []);
|
|
41
|
+
level.get(key).push(index);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
levels.set(upper.z, level);
|
|
45
|
+
}
|
|
46
|
+
const nearby = new Set();
|
|
47
|
+
for (const key of cells(upper)) {
|
|
48
|
+
for (const index of level.get(key) ?? []) nearby.add(index);
|
|
49
|
+
}
|
|
50
|
+
for (const lowerIndex of [...nearby].sort((left, right) => left - right)) {
|
|
51
|
+
if (lowerIndex === upperIndex) continue;
|
|
52
|
+
candidateChecks++;
|
|
53
|
+
const area = overlapXY(boxes[lowerIndex], upper);
|
|
54
|
+
if (area > 0) {
|
|
55
|
+
supporters[upperIndex].push([lowerIndex, area]);
|
|
56
|
+
children[lowerIndex].push(upperIndex);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return { supporters, children, candidateChecks };
|
|
61
|
+
}
|