npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

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-polydata

Requires @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