serialize a vtk object

How is a good way to serialize objects like vtkPolyData to transmit between client and server?

actually I create a point cloud in javascript and want to send it to python script on server to do some processing and send back to javascript for visualization.

Update: in this link on github, @Sebastien_Jourdain has mentioned a better way than using JSON is to use vtp format to send a single binary blob over network.

does it mean to save the polydata as vtp file in javascript, send that file to python server, read the vtp file in python script and do the processing, again save it as a vtp or something file and send it back to client javascript?

If you are just dealing with point clouds, I would just send the binary arrays of xyz and fields and just rebuild the polydata on both client/server based on what you need.

1 Like

actually I’m producing point cloud in javascript, then I’m sending that point cloud to python flask process to use surfaceReconstructionFilter and create a surface for that and at the end I need to send the surface mesh back to javascript client to visualize. so it’s not just point clouds.

If the points don’t changes, you could just send back the connectivity array to convert your existing point cloud into a bunch of triangles/polys. That will only require only 1 binary transfer which you know implicitly what to do with it…

thank you, neat idea.
yet in any time I send the point cloud from javascript web to python server, the user has manipulated the location of most of points in point cloud on the web page interface.
so point cloud locations is not fixed and every time changes. so I guess I need to make the surface on server, save the mesh file, and give the address to client to download and visualize it.is there a more efficient way I can use?

I’m not sure, but I think you have all the keys to solve your problem.

So I did it, now on every move of the user in javascript, I create the point cloud, send it to python server, there I create the surface out of it and save it on the server file system as stl.
now my question is how can I bring that surface back to client and visualize it?
actually I tried to do that using this example on vtk.js that reads and visualizes an stl file. javascript fileReader needs me to use an input html tag and seems like I can’t read the stl file from server just by knowing it’s path.
any suggestions?

call the method setUrl(...) on the reader. That’s the way we do it when the data is not local…

1 Like

great, then I’m not sure if this way of reading and visualizing an online stl is right?

const reader = vtkSTLReader.newInstance();
reader.setUrl("https://files.fm/down.php?cf&i=4m4h9trdg&n=resultingMesh.stl").then(() => {
    const sur = reader.getOutputData(0);
    mapper.setInputData(sur);
    actor.setMapper(mapper);  
    renderer.addActor(actor);
    resetCamera();
    render();
})

Seems reasonable. Does it work?

It’s not working.
What can be the reason?

CORS issue? Do you see any network error?

no there’s no network error and even when I console.log the transmitted surface, it shows something like:


which I think means that the stl object is correctly read from server.

the only thing is that it doesn’t show the object in the scene.
any suggestions?

maybe the way that I’m setting mapper or it’s input data is not right?

const reader = vtkSTLReader.newInstance();
reader.setUrl("https://files.fm/down.php?cf&i=4m4h9trdg&n=resultingMesh.stl").then(() => {
    const sur = reader.getOutputData(0);
    console.log(sur);
    mapper.setInputData(sur);
    actor.setMapper(mapper);  
    renderer.addActor(actor);
    resetCamera();
    render();
})

Query how many points your geometry has… Maybe something wrong happened and you have 0 which could explain why you don’t see anything.

If need be you can purchase some support from Kitware which could help you debug your code or even develop some of the parts that you need. I’m only saying that as it seems that you are running into issues that could be related to your infrastructure and/or code base which we can’t easily debug via a discourse thread.

seems like that’s exactly the case:
console.log(sur.getNumberOfPoints());
returns 0.

Solved,
Just needed to change the line:

mapper.setInputData(reader.getOutputData(0));

to

mapper.setInputConnection(reader.getOutputPort());

and that works like a charm. :slight_smile: