@meshinspector/meshlib 3.1.3-337 → 3.1.4-297
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/README.md +64 -18
- package/bindings.d.mts +1696 -163
- package/index.d.mts +11 -174
- package/meshlib.mjs +123 -136
- package/meshlib.node.mjs +151 -160
- package/meshlib.node.wasm +0 -0
- package/meshlib.wasm +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,18 +23,25 @@ npm install @meshinspector/meshlib
|
|
|
23
23
|
In the browser you can skip npm entirely and import the module directly:
|
|
24
24
|
|
|
25
25
|
```js
|
|
26
|
-
// latest
|
|
26
|
+
// latest release
|
|
27
27
|
import createMeshLib from 'https://js.meshlib.io/meshlib/meshlib.mjs';
|
|
28
28
|
|
|
29
|
-
// or pin a
|
|
30
|
-
import createMeshLib from 'https://js.meshlib.io/meshlib@
|
|
29
|
+
// or pin a release — v1.2.3.456 is an example tag, not a real one
|
|
30
|
+
import createMeshLib from 'https://js.meshlib.io/meshlib@v1.2.3.456/meshlib.mjs';
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
+
To pin, take a real tag from the [releases page](https://github.com/MeshInspector/MeshLib/releases): it has a
|
|
34
|
+
leading `v` and four components, and is **not** the npm version — the tag `v1.2.3.456` would be npm `1.2.3-456`.
|
|
35
|
+
Pinned URLs are immutable, so a pin never changes under you; the unpinned path always serves the latest release.
|
|
36
|
+
|
|
33
37
|
## Usage
|
|
34
38
|
|
|
35
39
|
The default export is an async factory. Await it once to get the module instance, then
|
|
36
40
|
call MeshLib functions on it:
|
|
37
41
|
|
|
42
|
+
> `using` requires Node.js 24+ or a current browser. On older runtimes, call `.delete()`
|
|
43
|
+
> instead — see [Memory management](#memory-management).
|
|
44
|
+
|
|
38
45
|
```js
|
|
39
46
|
import createMeshLib from '@meshinspector/meshlib';
|
|
40
47
|
|
|
@@ -50,48 +57,87 @@ const indices = new Uint32Array([
|
|
|
50
57
|
3, 6, 2, 3, 7, 6, 0, 4, 7, 0, 7, 3, 1, 2, 6, 1, 6, 5,
|
|
51
58
|
]);
|
|
52
59
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
60
|
+
using coords = ml.VertCoords.fromArray(positions);
|
|
61
|
+
using tris = ml.Triangulation.fromArray(indices);
|
|
62
|
+
using mesh = ml.Mesh.fromTriangles(coords, tris);
|
|
56
63
|
|
|
57
64
|
console.log('volume =', mesh.volume()); // ~8
|
|
58
|
-
|
|
59
|
-
// Objects are backed by WebAssembly memory — free them explicitly.
|
|
60
|
-
coords.delete();
|
|
61
|
-
tris.delete();
|
|
62
|
-
mesh.delete();
|
|
65
|
+
// `using` frees these WebAssembly-backed objects automatically at the end of scope
|
|
63
66
|
```
|
|
64
67
|
|
|
65
68
|
## Using with bundlers
|
|
66
69
|
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
Vite 8 and webpack 5 resolve `meshlib.wasm` from the module and emit it as an asset, so a plain `import`
|
|
71
|
+
needs no configuration. For other bundlers, such as esbuild or Rollup, import the wasm as an asset URL
|
|
72
|
+
and hand it to the loader via `locateFile`:
|
|
69
73
|
|
|
70
74
|
```js
|
|
71
75
|
import createMeshLib from '@meshinspector/meshlib';
|
|
72
|
-
import wasmUrl from '@meshinspector/meshlib/meshlib.wasm
|
|
76
|
+
import wasmUrl from '@meshinspector/meshlib/meshlib.wasm';
|
|
73
77
|
|
|
74
78
|
const ml = await createMeshLib( { locateFile: () => wasmUrl } );
|
|
75
79
|
```
|
|
76
80
|
|
|
81
|
+
The bundler must treat `.wasm` files as static assets, so that the import resolves to the URL of the
|
|
82
|
+
emitted file; the option is usually called an asset or file loader. For example:
|
|
83
|
+
|
|
84
|
+
- **esbuild**: pass `--loader:.wasm=file`
|
|
85
|
+
- **Rollup**: add `@rollup/plugin-url` with `include: /\.wasm$/`
|
|
86
|
+
- **Parcel**: use the `url:` scheme on the import specifier: `import wasmUrl from 'url:@meshinspector/meshlib/meshlib.wasm';`
|
|
87
|
+
|
|
77
88
|
## TypeScript
|
|
78
89
|
|
|
79
90
|
The package ships type definitions, so `createMeshLib` and the whole module API are typed with
|
|
80
|
-
|
|
91
|
+
minimal setup:
|
|
81
92
|
|
|
82
93
|
```ts
|
|
83
94
|
import createMeshLib, { type Mesh } from '@meshinspector/meshlib';
|
|
84
95
|
|
|
85
96
|
const ml = await createMeshLib();
|
|
86
|
-
const mesh: Mesh = ml.Mesh.fromTriangles(coords, tris)
|
|
97
|
+
const mesh: Mesh = ml.Mesh.fromTriangles(coords, tris)!;
|
|
87
98
|
const { valid, distSq } = ml.findProjection(point, mesh);
|
|
99
|
+
mesh.delete();
|
|
88
100
|
```
|
|
89
101
|
|
|
102
|
+
Also make sure to add `"type": "module"` to your package.json.
|
|
103
|
+
|
|
90
104
|
## Memory management
|
|
91
105
|
|
|
92
106
|
Values returned from the API (meshes, bit sets, settings, result objects, …) hold
|
|
93
|
-
WebAssembly memory that the JavaScript garbage collector does not reclaim
|
|
94
|
-
|
|
107
|
+
WebAssembly memory that the JavaScript garbage collector does not reclaim, so each one
|
|
108
|
+
must be freed explicitly.
|
|
109
|
+
|
|
110
|
+
The preferred way is JavaScript's explicit resource management: declare a handle with
|
|
111
|
+
`using` and it is freed automatically when its scope ends — even if an exception is thrown.
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
using mesh = ml.Mesh.fromTriangles(coords, tris);
|
|
115
|
+
// ... use mesh; it is freed at the end of this scope
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
When the number of handles is dynamic (for example built in a loop), collect them in a
|
|
119
|
+
`DisposableStack`, which frees everything it holds, in reverse order, at the end of the scope:
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
using stack = new DisposableStack();
|
|
123
|
+
for (const path of inputPaths) {
|
|
124
|
+
const cloud = stack.use(ml.PointsLoad.fromAnySupportedFormat(path));
|
|
125
|
+
// ... use cloud
|
|
126
|
+
}
|
|
127
|
+
// every handle passed to stack.use(...) is freed here
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`using` and `DisposableStack` are part of JavaScript's Explicit Resource Management,
|
|
131
|
+
available in Node.js 24+ and current browsers. On older runtimes and browsers, call
|
|
132
|
+
`.delete()` on each object when you are done instead:
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
const mesh = ml.Mesh.fromTriangles(coords, tris);
|
|
136
|
+
// ... use mesh
|
|
137
|
+
mesh.delete();
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
See also, on MDN: [`using`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using) and [`DisposableStack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DisposableStack).
|
|
95
141
|
|
|
96
142
|
## License
|
|
97
143
|
|