Vtk and Web workers

I’ll go straight to the point: has anybody ever tried to use Web workers with the vtkXMLImageDataReader from vtk.js? I have this .vti file that comes from a reconstruction on the server part of the application but when I send it to the client it will stop the main thread to parse the model file. I wanted to give the web workers a try but I see that internally vtk.js uses the window variable (that does not exist in workers) so also importing the library is impossible. Also, to send data from a worker to the main thread it is required to have a Transferable object or parse everything in a json maybe (I don’t think this is good for large objects) .Are there any ways to avoid this block that I have not found?

Thanks all!

Hi, I’ve considered the possibility of using the vtkXML*Readers in a web worker before, so here are my thoughts:

  • window-based objects: I remember this just being related to XML parsing. As those facilities are not available in the webworker, we would have to bring in an xml parsing library to allow the readers to work in web workers.
  • Sending via Transferable won’t work, but you can call imageData.getState() to get a JSON serialized copy. To deserialize, you call import vtk from 'vtk.js/Sources/vtk'; imageData = vtk(imageDataJson);

Mmmh, I’m not sure of the xml part of your comment. For instance, I’m using the unpkg version of the library and only importing it using the importScript() syntax will throw an error for the window thing. Am I doing something wrong

Can you provide a codesandbox or other simple proof of concept? That will help me in seeing what’s wrong.

You should send two things from the worker:

  1. The field as shared array
  2. The image data description (dimensions, spacing, orientation, origin…) as json

If you use itk.js all its readers use WebAssembly and do the processing in a web worker.


This is a little example of what is happening. The vtk.js file is simply the vtk.js from unpkg (version 17.1.2). As I said, only importing the library inside the worker throws an error saying that window is not defined (image 2). If I try to remove the import from the worker and import vtk.js inside the index.html, in the worker it will not be recognized, saying that vtk is not defined.

Is this answer related only to the vtkHttpDataSetReader with its index.json as image data description and the data folder as the field or am I missing something?

I’m not saying that vtkXMLImageDataReader is working in a worker. Because it obviously don’t based on what you are showing. But a worker can be used to process an imageData and pass it to vtk.js because itk.js does it. The main difference is that itk.js rely in web assembly for decoding the data. While vtk.js will use what browser Js has to offer (like xml parsing and maybe something else that could be fixed?).

That’s the only thing that I’m saying… You can use worker to build image data for vtk.js, not that vtk.js reader works in a worker (although that part might be fixable).