Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# PackingSolver in the browser

PackingSolver compiled to WebAssembly with [Emscripten](https://emscripten.org/), and a web page to solve instances in the browser. The computation runs on the user's machine.

## Build

```shell
emcmake cmake -S . -B build_wasm -DCMAKE_BUILD_TYPE=Release -DPACKINGSOLVER_BUILD_WASM=ON
cmake --build build_wasm --target PackingSolver_wasm
```

This builds `build_wasm/wasm/packingsolver.js` and `packingsolver.wasm`.

## Contents

- `src/packingsolver_wasm.cpp`: the JavaScript API of the module: `solve(problemType, instanceJson, parametersJson)`, which blocks, and `Session`, which doesn't. See the comment at the top of the file.
- `js/packingsolver_worker.js`: a Web Worker running sessions and posting the solutions to the page.
- `web/`: the web page.
- `visualize/`: JavaScript ports of the plotly visualizers of `python/packingsolver/visualize/`.
- `tests/`: the tests.

## Tests

```shell
node --test wasm/tests/
```

The visualizers are compared with the figures of the Python ones. These are stored in `tests/fixtures/visualize/`, and generated with plotly installed:

```shell
python3 wasm/tests/generate_visualize_fixtures.py [type ...]
```

## Web page

The threads of the module need `SharedArrayBuffer`. So the page must be served with the headers `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp`. The development server sets them:

```shell
python3 wasm/web/serve.py
```

Then open http://localhost:8000/.
6 changes: 5 additions & 1 deletion wasm/js/packingsolver_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ if (typeof importScripts === "function") {
importScripts("packingsolver.js");
postToPage = (message) => self.postMessage(message);
self.onmessage = (event) => onMessage(event.data);
modulePromise = PackingSolver();
// The threads of the module run in workers started from its own script:
// loaded with 'importScripts', it would take the URL of this one.
modulePromise = PackingSolver({
mainScriptUrlOrBlob: new URL("packingsolver.js", self.location.href).href,
});
} else {
// Node.
const {parentPort, workerData} = require("node:worker_threads");
Expand Down
10 changes: 10 additions & 0 deletions wasm/tests/fixtures/visualize/box/cases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{"name": "knapsack_default", "certificate": "data/box/tests/knapsack_merge_identical_items_requires_matching_profit/solution.csv", "options": {}, "function": "figure"},
{"name": "variable_sized_default", "certificate": "data/box/tests/variable_sized_bin_packing_mandatory_bins/solution.csv", "options": {}, "function": "figure"},
{"name": "variable_sized_options", "certificate": "data/box/tests/variable_sized_bin_packing_mandatory_bins/solution.csv", "options": {"item_color": "SAME", "columns": 1, "zoom": 2.0, "legend": false}, "function": "figure"},
{"name": "open_dimension_x_default", "certificate": "data/box/tests/open_dimension_x_4_different_items_xz/solution.csv", "options": {}, "function": "figure"},
{"name": "open_dimension_x_id", "certificate": "data/box/tests/open_dimension_x_4_different_items_xz/solution.csv", "options": {"item_color": "ID", "zoom": 0.5}, "function": "figure"},
{"name": "defects_default", "certificate": "wasm/tests/fixtures/visualize/box/defects.csv", "options": {}, "function": "figure"},
{"name": "defects_same_columns_2", "certificate": "wasm/tests/fixtures/visualize/box/defects.csv", "options": {"item_color": "SAME", "columns": 2, "legend": false}, "function": "figure"},
{"name": "defects_columns_5_zoom", "certificate": "wasm/tests/fixtures/visualize/box/defects.csv", "options": {"columns": 5, "zoom": 1.5, "legend": true}, "function": "figure"}
]
15 changes: 15 additions & 0 deletions wasm/tests/fixtures/visualize/box/defects.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
TYPE,ID,COPIES,BIN,X,Y,Z,LX,LY,LZ,ROTATION
BIN,0,1,0,0,0,0,20,10,10,
DEFECT,0,1,0,15,0,0,5,5,5,
DEFECT,1,1,0,15,5,5,5,5,5,
ITEM,3,1,0,0,0,0,7,10,5,XYZ
ITEM,12,1,0,7,0,0,8,5,3,YXZ
ITEM,3,1,0,0,0,5,7,10,5,XYZ
BIN,1,1,1,0,0,0,30,10,10,
ITEM,0,1,1,0,0,0,9,9,9,XYZ
BIN,0,1,2,0,0,0,20,10,10,
DEFECT,2,1,2,1,1,1,3,3,3,
BIN,1,1,3,0,0,0,30,10,10,
ITEM,5,1,3,10,0,0,11,10,10,ZYX
BIN,1,1,4,0,0,0,30,10,10,
ITEM,7,1,4,0,0,0,1,1,1,XYZ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"function": "figure", "certificate": "TYPE,ID,COPIES,BIN,X,Y,Z,LX,LY,LZ,ROTATION\nBIN,0,1,0,0,0,0,20,10,10,\nDEFECT,0,1,0,15,0,0,5,5,5,\nDEFECT,1,1,0,15,5,5,5,5,5,\nITEM,3,1,0,0,0,0,7,10,5,XYZ\nITEM,12,1,0,7,0,0,8,5,3,YXZ\nITEM,3,1,0,0,0,5,7,10,5,XYZ\nBIN,1,1,1,0,0,0,30,10,10,\nITEM,0,1,1,0,0,0,9,9,9,XYZ\nBIN,0,1,2,0,0,0,20,10,10,\nDEFECT,2,1,2,1,1,1,3,3,3,\nBIN,1,1,3,0,0,0,30,10,10,\nITEM,5,1,3,10,0,0,11,10,10,ZYX\nBIN,1,1,4,0,0,0,30,10,10,\nITEM,7,1,4,0,0,0,1,1,1,XYZ\n", "options": {"columns": 5, "zoom": 1.5, "legend": true}, "figure": {"data": [{"color": "grey", "flatshading": true, "i": [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2], "j": [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3], "k": [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6], "legendgroup": "bins", "name": "Bins", "opacity": 0.1, "showlegend": true, "x": [0, 0, 20, 20, 0, 0, 20, 20], "y": [0, 10, 10, 0, 0, 10, 10, 0], "z": [0, 0, 0, 0, 10, 10, 10, 10], "type": "mesh3d", "scene": "scene"}, {"color": "grey", "flatshading": true, "i": [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2, 15, 8, 8, 8, 12, 12, 14, 14, 12, 8, 11, 10], "j": [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3, 11, 12, 9, 10, 13, 14, 13, 10, 8, 9, 14, 11], "k": [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6, 8, 15, 10, 11, 14, 15, 9, 9, 13, 13, 15, 14], "legendgroup": "defects", "name": "Defects", "opacity": 0.2, "showlegend": true, "x": [15, 15, 20, 20, 15, 15, 20, 20, 15, 15, 20, 20, 15, 15, 20, 20], "y": [0, 5, 5, 0, 0, 5, 5, 0, 5, 10, 10, 5, 5, 10, 10, 5], "z": [0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10], "type": "mesh3d", "scene": "scene"}, {"color": "rgb(220, 176, 242)", "flatshading": true, "i": [0, 3, 4, 7, 0, 5, 2, 7, 0, 6, 1, 7], "j": [1, 1, 5, 5, 1, 1, 3, 3, 2, 2, 3, 3], "k": [2, 2, 6, 6, 4, 4, 6, 6, 4, 4, 5, 5], "legendgroup": "items", "name": "Items 3", "opacity": 1, "showlegend": true, "x": [0.1, 6.9, 0.1, 6.9, 0.1, 6.9, 0.1, 6.9], "y": [0.1, 0.1, 9.9, 9.9, 0.1, 0.1, 9.9, 9.9], "z": [0.1, 0.1, 0.1, 0.1, 4.9, 4.9, 4.9, 4.9], "type": "mesh3d", "scene": "scene"}, {"color": "rgb(246, 207, 113)", "flatshading": true, "i": [0, 3, 4, 7, 0, 5, 2, 7, 0, 6, 1, 7], "j": [1, 1, 5, 5, 1, 1, 3, 3, 2, 2, 3, 3], "k": [2, 2, 6, 6, 4, 4, 6, 6, 4, 4, 5, 5], "legendgroup": "items", "name": "Items 12", "opacity": 1, "showlegend": false, "x": [7.1, 14.9, 7.1, 14.9, 7.1, 14.9, 7.1, 14.9], "y": [0.1, 0.1, 4.9, 4.9, 0.1, 0.1, 4.9, 4.9], "z": [0.1, 0.1, 0.1, 0.1, 2.9, 2.9, 2.9, 2.9], "type": "mesh3d", "scene": "scene"}, {"color": "rgb(220, 176, 242)", "flatshading": true, "i": [0, 3, 4, 7, 0, 5, 2, 7, 0, 6, 1, 7], "j": [1, 1, 5, 5, 1, 1, 3, 3, 2, 2, 3, 3], "k": [2, 2, 6, 6, 4, 4, 6, 6, 4, 4, 5, 5], "legendgroup": "items", "name": "Items 3", "opacity": 1, "showlegend": false, "x": [0.1, 6.9, 0.1, 6.9, 0.1, 6.9, 0.1, 6.9], "y": [0.1, 0.1, 9.9, 9.9, 0.1, 0.1, 9.9, 9.9], "z": [5.1, 5.1, 5.1, 5.1, 9.9, 9.9, 9.9, 9.9], "type": "mesh3d", "scene": "scene"}, {"legendgroup": "items", "line": {"color": "black", "width": 1}, "mode": "lines", "name": "Item borders", "showlegend": false, "x": [0, 0, 7, 7, 7, 7, 0, 0, 0, null, 0, 0, 7, 7, 7, 7, 0, 0, 0, null, 7, 7, 15, 15, 15, 15, 7, 7, 7, null, 7, 7, 15, 15, 15, 15, 7, 7, 7, null, 0, 0, 7, 7, 7, 7, 0, 0, 0, null, 0, 0, 7, 7, 7, 7, 0, 0, 0, null], "y": [0, 0, 0, 0, 10, 10, 10, 10, 0, null, 0, 0, 0, 0, 10, 10, 10, 10, 0, null, 0, 0, 0, 0, 5, 5, 5, 5, 0, null, 0, 0, 0, 0, 5, 5, 5, 5, 0, null, 0, 0, 0, 0, 10, 10, 10, 10, 0, null, 0, 0, 0, 0, 10, 10, 10, 10, 0, null], "z": [0, 5, 5, 0, 0, 5, 5, 0, 0, null, 5, 0, 0, 5, 5, 0, 0, 5, 5, null, 0, 3, 3, 0, 0, 3, 3, 0, 0, null, 3, 0, 0, 3, 3, 0, 0, 3, 3, null, 5, 10, 10, 5, 5, 10, 10, 5, 5, null, 10, 5, 5, 10, 10, 5, 5, 10, 10, null], "type": "scatter3d", "scene": "scene"}, {"legendgroup": "items", "mode": "text", "name": "Item ids", "showlegend": false, "text": ["3", "12", "3"], "textfont": {"size": 8}, "textposition": "middle center", "x": [3.5, 11.0, 3.5], "y": [5.0, 2.5, 5.0], "z": [2.5, 1.5, 7.5], "type": "scatter3d", "scene": "scene"}, {"color": "grey", "flatshading": true, "i": [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2], "j": [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3], "k": [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6], "legendgroup": "bins", "name": "Bins", "opacity": 0.1, "showlegend": false, "x": [0, 0, 30, 30, 0, 0, 30, 30], "y": [0, 10, 10, 0, 0, 10, 10, 0], "z": [0, 0, 0, 0, 10, 10, 10, 10], "type": "mesh3d", "scene": "scene2"}, {"color": "grey", "flatshading": true, "i": [], "j": [], "k": [], "legendgroup": "defects", "name": "Defects", "opacity": 0.2, "showlegend": false, "x": [], "y": [], "z": [], "type": "mesh3d", "scene": "scene2"}, {"color": "rgb(102, 197, 204)", "flatshading": true, "i": [0, 3, 4, 7, 0, 5, 2, 7, 0, 6, 1, 7], "j": [1, 1, 5, 5, 1, 1, 3, 3, 2, 2, 3, 3], "k": [2, 2, 6, 6, 4, 4, 6, 6, 4, 4, 5, 5], "legendgroup": "items", "name": "Items 0", "opacity": 1, "showlegend": false, "x": [0.1, 8.9, 0.1, 8.9, 0.1, 8.9, 0.1, 8.9], "y": [0.1, 0.1, 8.9, 8.9, 0.1, 0.1, 8.9, 8.9], "z": [0.1, 0.1, 0.1, 0.1, 8.9, 8.9, 8.9, 8.9], "type": "mesh3d", "scene": "scene2"}, {"legendgroup": "items", "line": {"color": "black", "width": 1}, "mode": "lines", "name": "Item borders", "showlegend": false, "x": [0, 0, 9, 9, 9, 9, 0, 0, 0, null, 0, 0, 9, 9, 9, 9, 0, 0, 0, null], "y": [0, 0, 0, 0, 9, 9, 9, 9, 0, null, 0, 0, 0, 0, 9, 9, 9, 9, 0, null], "z": [0, 9, 9, 0, 0, 9, 9, 0, 0, null, 9, 0, 0, 9, 9, 0, 0, 9, 9, null], "type": "scatter3d", "scene": "scene2"}, {"legendgroup": "items", "mode": "text", "name": "Item ids", "showlegend": false, "text": ["0"], "textfont": {"size": 8}, "textposition": "middle center", "x": [4.5], "y": [4.5], "z": [4.5], "type": "scatter3d", "scene": "scene2"}, {"color": "grey", "flatshading": true, "i": [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2], "j": [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3], "k": [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6], "legendgroup": "bins", "name": "Bins", "opacity": 0.1, "showlegend": false, "x": [0, 0, 20, 20, 0, 0, 20, 20], "y": [0, 10, 10, 0, 0, 10, 10, 0], "z": [0, 0, 0, 0, 10, 10, 10, 10], "type": "mesh3d", "scene": "scene3"}, {"color": "grey", "flatshading": true, "i": [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2], "j": [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3], "k": [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6], "legendgroup": "defects", "name": "Defects", "opacity": 0.2, "showlegend": false, "x": [1, 1, 4, 4, 1, 1, 4, 4], "y": [1, 4, 4, 1, 1, 4, 4, 1], "z": [1, 1, 1, 1, 4, 4, 4, 4], "type": "mesh3d", "scene": "scene3"}, {"legendgroup": "items", "line": {"color": "black", "width": 1}, "mode": "lines", "name": "Item borders", "showlegend": false, "x": [], "y": [], "z": [], "type": "scatter3d", "scene": "scene3"}, {"legendgroup": "items", "mode": "text", "name": "Item ids", "showlegend": false, "text": [], "textfont": {"size": 8}, "textposition": "middle center", "x": [], "y": [], "z": [], "type": "scatter3d", "scene": "scene3"}, {"color": "grey", "flatshading": true, "i": [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2], "j": [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3], "k": [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6], "legendgroup": "bins", "name": "Bins", "opacity": 0.1, "showlegend": false, "x": [0, 0, 30, 30, 0, 0, 30, 30], "y": [0, 10, 10, 0, 0, 10, 10, 0], "z": [0, 0, 0, 0, 10, 10, 10, 10], "type": "mesh3d", "scene": "scene4"}, {"color": "grey", "flatshading": true, "i": [], "j": [], "k": [], "legendgroup": "defects", "name": "Defects", "opacity": 0.2, "showlegend": false, "x": [], "y": [], "z": [], "type": "mesh3d", "scene": "scene4"}, {"color": "rgb(158, 185, 243)", "flatshading": true, "i": [0, 3, 4, 7, 0, 5, 2, 7, 0, 6, 1, 7], "j": [1, 1, 5, 5, 1, 1, 3, 3, 2, 2, 3, 3], "k": [2, 2, 6, 6, 4, 4, 6, 6, 4, 4, 5, 5], "legendgroup": "items", "name": "Items 5", "opacity": 1, "showlegend": false, "x": [10.1, 20.9, 10.1, 20.9, 10.1, 20.9, 10.1, 20.9], "y": [0.1, 0.1, 9.9, 9.9, 0.1, 0.1, 9.9, 9.9], "z": [0.1, 0.1, 0.1, 0.1, 9.9, 9.9, 9.9, 9.9], "type": "mesh3d", "scene": "scene4"}, {"legendgroup": "items", "line": {"color": "black", "width": 1}, "mode": "lines", "name": "Item borders", "showlegend": false, "x": [10, 10, 21, 21, 21, 21, 10, 10, 10, null, 10, 10, 21, 21, 21, 21, 10, 10, 10, null], "y": [0, 0, 0, 0, 10, 10, 10, 10, 0, null, 0, 0, 0, 0, 10, 10, 10, 10, 0, null], "z": [0, 10, 10, 0, 0, 10, 10, 0, 0, null, 10, 0, 0, 10, 10, 0, 0, 10, 10, null], "type": "scatter3d", "scene": "scene4"}, {"legendgroup": "items", "mode": "text", "name": "Item ids", "showlegend": false, "text": ["5"], "textfont": {"size": 8}, "textposition": "middle center", "x": [15.5], "y": [5.0], "z": [5.0], "type": "scatter3d", "scene": "scene4"}, {"color": "grey", "flatshading": true, "i": [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2], "j": [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3], "k": [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6], "legendgroup": "bins", "name": "Bins", "opacity": 0.1, "showlegend": false, "x": [0, 0, 30, 30, 0, 0, 30, 30], "y": [0, 10, 10, 0, 0, 10, 10, 0], "z": [0, 0, 0, 0, 10, 10, 10, 10], "type": "mesh3d", "scene": "scene5"}, {"color": "grey", "flatshading": true, "i": [], "j": [], "k": [], "legendgroup": "defects", "name": "Defects", "opacity": 0.2, "showlegend": false, "x": [], "y": [], "z": [], "type": "mesh3d", "scene": "scene5"}, {"color": "rgb(201, 219, 116)", "flatshading": true, "i": [0, 3, 4, 7, 0, 5, 2, 7, 0, 6, 1, 7], "j": [1, 1, 5, 5, 1, 1, 3, 3, 2, 2, 3, 3], "k": [2, 2, 6, 6, 4, 4, 6, 6, 4, 4, 5, 5], "legendgroup": "items", "name": "Items 7", "opacity": 1, "showlegend": false, "x": [0.1, 0.9, 0.1, 0.9, 0.1, 0.9, 0.1, 0.9], "y": [0.1, 0.1, 0.9, 0.9, 0.1, 0.1, 0.9, 0.9], "z": [0.1, 0.1, 0.1, 0.1, 0.9, 0.9, 0.9, 0.9], "type": "mesh3d", "scene": "scene5"}, {"legendgroup": "items", "line": {"color": "black", "width": 1}, "mode": "lines", "name": "Item borders", "showlegend": false, "x": [0, 0, 1, 1, 1, 1, 0, 0, 0, null, 0, 0, 1, 1, 1, 1, 0, 0, 0, null], "y": [0, 0, 0, 0, 1, 1, 1, 1, 0, null, 0, 0, 0, 0, 1, 1, 1, 1, 0, null], "z": [0, 1, 1, 0, 0, 1, 1, 0, 0, null, 1, 0, 0, 1, 1, 0, 0, 1, 1, null], "type": "scatter3d", "scene": "scene5"}, {"legendgroup": "items", "mode": "text", "name": "Item ids", "showlegend": false, "text": ["7"], "textfont": {"size": 8}, "textposition": "middle center", "x": [0.5], "y": [0.5], "z": [0.5], "type": "scatter3d", "scene": "scene5"}], "layout": {"scene": {"domain": {"x": [0.0, 0.16799999999999998], "y": [0.0, 1.0]}, "camera": {"center": {"x": 0, "y": 0, "z": -0.25}, "eye": {"x": -1.1666666666666667, "y": -1.0, "z": 0.5}}, "aspectmode": "data"}, "scene2": {"domain": {"x": [0.208, 0.376], "y": [0.0, 1.0]}, "camera": {"center": {"x": 0, "y": 0, "z": -0.25}, "eye": {"x": -1.1666666666666667, "y": -1.0, "z": 0.5}}, "aspectmode": "data"}, "scene3": {"domain": {"x": [0.416, 0.584], "y": [0.0, 1.0]}, "camera": {"center": {"x": 0, "y": 0, "z": -0.25}, "eye": {"x": -1.1666666666666667, "y": -1.0, "z": 0.5}}, "aspectmode": "data"}, "scene4": {"domain": {"x": [0.624, 0.792], "y": [0.0, 1.0]}, "camera": {"center": {"x": 0, "y": 0, "z": -0.25}, "eye": {"x": -1.1666666666666667, "y": -1.0, "z": 0.5}}, "aspectmode": "data"}, "scene5": {"domain": {"x": [0.832, 1.0], "y": [0.0, 1.0]}, "camera": {"center": {"x": 0, "y": 0, "z": -0.25}, "eye": {"x": -1.1666666666666667, "y": -1.0, "z": 0.5}}, "aspectmode": "data"}, "font": {"size": 14}, "legend": {"font": {"size": 14}, "itemsizing": "constant"}, "autosize": true, "showlegend": true, "xaxis": {"rangeslider": {"visible": false}}, "yaxis": {"scaleanchor": "x", "scaleratio": 1}}}}
Loading
Loading