Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update shapeworks.sw2vtkMesh to export mesh vertices, edges, and polys rather than read/write to disk #825

Closed
sheryjoe opened this issue Nov 25, 2020 · 16 comments · Fixed by #1473
Assignees

Comments

@sheryjoe
Copy link
Contributor

sheryjoe commented Nov 25, 2020

Similar to shapeworks.Image, we want to convert a shapeworks mesh to itk or vtk data structure to be visualized by itkwidgets, which supports both itk:Mesh and vtk::PolyData

This also will be very handy when visualizing multiple samples in a grid view using pyvista.

For now, a workaround would be saving the mesh/image and re-reading it for visualization.

@sheryjoe sheryjoe changed the title Add toItk / toVtk to shapeworks.Mesh Add toItk / toVtk to shapeworks.Mesh and ShapeWorks.Image Nov 26, 2020
@sheryjoe sheryjoe changed the title Add toItk / toVtk to shapeworks.Mesh and ShapeWorks.Image Add toItk / toVtk to shapeworks.Mesh and shapeworks.Image Nov 26, 2020
@sheryjoe
Copy link
Contributor Author

sheryjoe commented Nov 26, 2020

Update for sw.Image:

  • Converting to itk image can be done by extracting a numpy array from a sw image and use itk.GetImageFromArray
  • Converting to vtk image can be done by extracting a numpy array from a sw image and use pyvista.wrap

These helper functions will be soon demonstrated in our notebook tutorials. These helper functions can be included in shapeworks init.py

using pyvista to render volumes does not reflect image physical coordinates converting to itk/vtk images should also consider header information for subsequent visualization.

I will provide more updates on sw.Mesh later.

@sheryjoe
Copy link
Contributor Author

For sw.Mesh, we need access to mesh vertices and faces to construct itkMesh and vtkPolyData in python.

@sheryjoe sheryjoe added this to the 6.0 Release milestone Nov 27, 2020
@sheryjoe
Copy link
Contributor Author

sheryjoe commented Dec 2, 2020

pyvista wrap function can wrap vtkPolyData and trimesh (python) objects. If we can get the vertices and faces as arrays from sw.Mesh, we can construct vtkPolydata in python and use pv.wrap for visualization.

@sheryjoe
Copy link
Contributor Author

Please, update notebooks to replace sw2vtk sw2itk helper functions.

@archanasri archanasri modified the milestones: 6.0 Release, 6.0 Leftover Feb 11, 2021
@sheryjoe sheryjoe modified the milestones: 6.0 Leftover, 6.1 Release (May 2021) Feb 15, 2021
@cchriste cchriste changed the title Add toItk / toVtk to shapeworks.Mesh and shapeworks.Image Add toItk / toVtk to python sw.Mesh and shapeworks.Image May 19, 2021
@cchriste
Copy link
Contributor

cchriste commented May 19, 2021

This may simply not be possible from c++, but these helpers should be in the pymodule.
For now, they will be moved into pymodule (by @jadie1 and @iyerkrithika21 for #1173)

@sheryjoe
Copy link
Contributor Author

@jadie1 @iyerkrithika21 please address this issue as you resolve #1173 and #865.

@archanasri
Copy link
Contributor

I don't think we need toItk anymore since we are not going to use itkwidgets.

@sheryjoe
Copy link
Contributor Author

@archanasri is this still an issue?

@archanasri
Copy link
Contributor

archanasri commented Jun 16, 2021

pyvista wrap function can wrap vtkPolyData and trimesh (python) objects. If we can get the vertices and faces as arrays from sw.Mesh, we can construct vtkPolydata in python and use pv.wrap for visualization.

In order to construct the mesh after obtaining the two arrays, we need to import vtk or import trimesh. Is this something we want to do?
importing itk always leads to more issues

@cchriste
Copy link
Contributor

pyvista wrap function can wrap vtkPolyData and trimesh (python) objects. If we can get the vertices and faces as arrays from sw.Mesh, we can construct vtkPolydata in python and use pv.wrap for visualization.

In order to construct the mesh after obtaining the two arrays, we need to import vtk or import trimesh. Is this something we want to do?
importing itk always leads to more issues

Agreed. And we essentially already have these functions in our module (we can add sw2itk if we want, but since our "resolution" to so many itk issues is to simply not include it, this would go against that). Implementing them on the C++ side--they already are, just not provided as part of our module--would provide no benefit.

@archanasri
Copy link
Contributor

So should we just leave sw2vtk as it is? It currently writes out the sw mesh and loads the same mesh using pyvista.read

@cchriste cchriste changed the title Add toItk / toVtk to python sw.Mesh and shapeworks.Image Update shapeworks.sw2vtkMesh to export mesh vertices, edges, and polys rather than read/write to disk Jun 17, 2021
@cchriste
Copy link
Contributor

So should we just leave sw2vtk as it is? It currently writes out the sw mesh and loads the same mesh using pyvista.read

This works fine, but should probably not use the disk.
I changed the name of the issue to address this remaining item specifically (formerly "Add toItk / toVtk to python sw.Mesh and shapeworks.Image).

I don't think resolving this is necessary for 6.1, and possibly not at all, though similarly to #903, sharing memory will be more efficient than copying.

@cchriste
Copy link
Contributor

cchriste commented Aug 3, 2021

These functions exist in shapeworks python module as shapeworks.sw2VtkImage and sw2vtkMesh.
Their names should be changed to be simply toVtk (maybe toVtkImage/toVtkMesh, or use Python overloading module; we do utilize overloading for the C++ bindings, so it's a reasonable expectation).

#903 will automatically improve performance (note: this should be used as a test and performance comparison for that issue).

@cchriste
Copy link
Contributor

Their names should be changed to be simply toVtk (maybe toVtkImage/toVtkMesh

I no longer agree with myself about renaming: sw2vtk[Image|Mesh] seems fine.

#903 will automatically improve performance

This now works efficiently and even the internal data pointer in the wrapped image points directly back to the image.
No copies were made in the wrapping of the Image for pyvista visualization. 👍

@archanasri
Copy link
Contributor

@cchriste It looks like this issue can be closed. Do you want to discuss this offline?

@cchriste
Copy link
Contributor

From our discussion yesterday, the best way to convert a shapeworks.Mesh to a vtkImage (wrapped by pyvista) will be to retrieve the vertices, faces, [and any fields], using the example here: https://docs.pyvista.org/examples/00-load/create-poly.html#sphx-glr-examples-00-load-create-poly-py

All of these can be fetched without copying similar to Images. The quantity of data is significant and unlikely to be re-allocated with every filter. Therefore sharing without copying should also increase efficiency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants