Vtk.js with vtk.wasm usage

Hi!

I faced a lot of vtk.js restrictions (mostly lack of necessary methods, filters etc) and decided to expand application with vtk wasm but only as computation power without any rendering. Before that the flask backend with python-vtk was used, but because of speed limits and too long processing time it does not always fit.

First I tried to process (“process” – send vtp from vtk.js to vtk wasm for applying e.g. any filter that is missing in vtk.js and return this updated vtp to vtk.js back) some lightweight polydata through wasm and it work really much faster then it was through backend.

But what I noticed is when I am trying to process large vti file through wasm (100+mb) it process much longer than through the backend.

Since I can’t transfer readers and other vtk things from vtk.js to c++ vtk directly, I have to transfer it as files (strings) and quite a lot of time is spent on read and write these heavyweight files on both vtk.js and vtk wasm sides.

Are there any problems with this approach and is there a better way to optimize data flow between vtk.js and vtk wasm?

JS:

const core = await wasmModuleInstance
const vti = new TextDecoder('utf-8').decode(new Uint8Array(fileData))
const vtiStringPtr = core.allocateUTF8(vti)

const resultPtr = core.ccall(
  'wasmFunctionName',
  'number',
  ['number'],
  [vtiStringPtr]
)

const resultStr = core.UTF8ToString(resultPtr)
const resultVolume = new TextEncoder().encode(resultStr).buffer

core._free(vti)
core._free(vtiStringPtr)
core._free(resultStr)

// regular vtk.js rendering using resultVolume variable as file

C++ WASM:

char* wasmFunctionName(const char* volume) {

  // this operation takes a lot of time if volume is heavyweight
  auto reader = vtkSmartPointer<vtkXMLImageDataReader>::New();
  reader->ReadFromInputStringOn();
  reader->SetInputString(volume);
  reader->Update();

  // ...
  // some operations
  // ...

  // this operation also takes a lot of time if volume is heavyweight
  auto volumeWriter = vtkSmartPointer<vtkXMLImageDataWriter>::New();
  volumeWriter->SetInputData(updatedVolume);
  volumeWriter->WriteToOutputStringOn();
  volumeWriter->Update();
  
  std::string outputStr = volumeWriter->GetOutputString();
  char* resultStr = (char*)malloc(outputStr.size() + 1);
  strcpy(resultStr, outputStr.c_str());
  return resultStr;
1 Like