marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
2.05k stars 266 forks source link

Cell data, threshold and created inner surface? #150

Closed rc closed 4 years ago

rc commented 4 years ago

Hi,

I would like to apply Mesh.threshold() to cell data, and simply passing useCells=True does not work. So I used Mesh.mapCellsToPoints() and it works (with useCells=False), but the resulting mesh has only the original surface - not the new (inner) surface that was created by removing points/cells with the threshold. Is it possible to display the surface of the hole created by the threshold application?

marcomusy commented 4 years ago

Try pip install -U git+https://github.com/marcomusy/vtkplotter.git then

from vtkplotter import *

man = load(datadir+"man_low.vtk").lw(0.1)

scals = man.cellCenters()[:, 0] + 37  
man.cellColors(scals, cmap="jet").addScalarBar()
printc(man.getArrayNames())

# make a copy and threshold the mesh
cutman = man.clone().threshold('CellScalars', 36.6, 37.5, useCells=True)

# distribute the meshes on 2 renderers
show(man, cutman, N=2, elevation=-30, axes=1)

image

Is it possible to display the surface of the hole created by the threshold application?

I 'm not sure ..it's not well defined (a contour does not define a unique surface)

rc commented 4 years ago

Thanks! useCells=True works fine now.

As for the other question, let me explain what I want to achieve. I have a 3D tetrahedral mesh with two materials - a solid matrix (blue) and channels with a fluid (green): screenshot1

When I apply the threshold, the resulting mesh is surface only: screenshot2

So, its silhouette is: screenshot3

After the channels material (green) is removed (without using vtkplotter) and a new mesh file is created and loaded: screenshot4

the silhouette is then: screenshot5

Is it possible to achieve the end result with vtkplotter/vtk only, without resorting to other tools to remove the green material elements, and without having to save the intermediate mesh to a file? It looks like the threshold filter might not be the right choice for this purpose, as its output is no longer a tetrahedral (volume) mesh. Newbie questions... :)

marcomusy commented 4 years ago

Is it possible to achieve the end result with vtkplotter/vtk only, without resorting to other tools to remove the green material elements, and without having to save the intermediate mesh to a file? It looks like the threshold filter might not be the right choice for this purpose, as its output is no longer a tetrahedral (volume) mesh.

Yes. although vtkplotter focuses on polygonal meshes and Volumes (uniform grids of voxels) you can still use the native VTK class vtkThreshold to fill the intermediate step, something like: [see next message]

marcomusy commented 4 years ago

...I actually had a dataset to test it:

from vtk import vtkThreshold
from vtkplotter import *

tetmesh = loadUnStructuredGrid(datadir+'limb_ugrid.vtk')

thres = vtkThreshold()
thres.SetInputData(tetmesh)
thres.SetInputArrayToProcess(0,0,0, 1, 'chem_0') # name of the array
thres.ThresholdByUpper(0.8)
thres.Update()
ugrid = thres.GetOutput()

# if False will only show the outer surface:
# settings.visibleGridEdges = True

msh = Mesh(ugrid).cellColors(cmap='nipy_spectral')
show(msh)

image

rc commented 4 years ago

Perfect, thank you! So the first trick was to use loadUnStructuredGrid() instead of just load()...

marcomusy commented 4 years ago

cool , i hope it work with your mesh too

Perfect, thank you! So the first trick was to use loadUnStructuredGrid() instead of just load()...

yes, because load already transforms it into a polygonal mesh, all this makes me think i should add a class to explicitly support tet meshes

thanks for your feedback, m.

rc commented 4 years ago

Yes, it works for my meshes!

marcomusy commented 4 years ago

@rc with the latest version pip install -U git+https://github.com/marcomusy/vtkplotter.git you can:

from vtkplotter import *

tetmesh = load(datadir+'limb_ugrid.vtk')
tetmesh.color('prism').alpha([0,1])#.printInfo()

# Threshold the tetrahedral mesh for values in the range:
tetmesh.threshold(above=0.9, below=1)
tetmesh.addScalarBar3D(title='chem_0  expression levels', c='k', italic=1)

show(tetmesh.toMesh(shrink=0.9), axes=1)

Run the built-in examples with: vtkplotter -ir tet_threshold

image

More examples here for isosurfacing cutting and slicing.

image image image image

rc commented 4 years ago

Very nice!

FYI: I have run pip install -U git+https://github.com/marcomusy/vtkplotter.git, but the new examples seem to be missing from the package:

vtkplotter -ir tet_threshold
No matching example found containing string: tet_threshold
marcomusy commented 4 years ago

I think I forgot to add the new dir to the pip manifesto.. this time should work.. pip install -U git+https://github.com/marcomusy/vtkplotter.git then vtkplotter -ir tet_threshold

Let me know if you find any problems using it (it's still a bit experimental!)

rc commented 4 years ago

No more problems, all the new examples work now.