labelmap-polydata
v1.2.1
Published
Convert vtk.js labelmap images to polygon meshes
Readme
labelmap-polydata
Convert vtk.js labelmap ImageData to PolyData meshes using marching cubes.
Installation
npm install labelmap-polydataRequires @kitware/vtk.js as a peer dependency.
Usage
import { labelmapToPolyDatas } from "labelmap-polydata/labelmapToPolyDatas";
// Create or load a labelmap ImageData where voxel values
// represent segment IDs (0 = background)
const labelmap: vtkImageData = /* your labelmap */;
// Convert to polydata meshes (runs on main thread)
const polyDatas = await labelmapToPolyDatas(labelmap);
// Result is a record mapping segment values to vtkPolyData
for (const [segmentValue, polyData] of Object.entries(polyDatas)) {
console.log(`Segment ${segmentValue}: ${polyData.getNumberOfPoints()} points`);
}Web Worker Support
For non-blocking conversion, create a worker file in your project. This lets your bundler resolve vtk.js, so you control the version.
1. Create a worker file in your project:
// src/workers/labelmapWorker.ts
import { handleLabelmapMessage } from "labelmap-polydata/workerHandler";
self.onmessage = (e) => {
const { result, transferables } = handleLabelmapMessage(e.data);
self.postMessage({ result }, transferables);
};2. Use the worker:
import { labelmapToPolyDatas } from "labelmap-polydata/labelmapToPolyDatas";
import LabelmapWorker from "./workers/labelmapWorker?worker";
const worker = new LabelmapWorker();
const polyDatas = await labelmapToPolyDatas(labelmap, { worker });
worker.terminate();Options
import { labelmapToPolyDatas } from "labelmap-polydata/labelmapToPolyDatas";
const polyDatas = await labelmapToPolyDatas(labelmap, {
worker: myWorker, // Worker instance for non-blocking execution
segments: [1, 2, 3], // Process specific segment values (default: all non-zero)
});World Space Alignment
The marching cubes algorithm outputs polydata in scaled index space (origin + index × spacing) but does not apply the image's direction matrix. For images with non-identity direction (oblique/rotated scans), use buildDirectionMatrix to align the mesh with world space:
import { buildDirectionMatrix } from "labelmap-polydata/directionMatrix";
const directionMatrix = buildDirectionMatrix(labelmap);
actor.setUserMatrix(directionMatrix);License
MIT
