marcomusy / vedo

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

cut_with_plane seems not working on volumes #928

Closed smoothumut closed 1 year ago

smoothumut commented 1 year ago

Hi Marco, Hope everything is going great with you.

I am working on volumes and somehow I couldnt make the cut_with_plane work on volumes. It waits then nothing happens. what would be the problem here is my sample code;

  from vedo import Mesh, dataurl, Plotter
  from vedo import *
  import vtk

  sphere = Sphere()
  sp_vol = sphere.binarize(spacing=(0.01,0.01,0.01))

  cut_vol = sp_vol.cut_with_plane(origin=(0,0,0.5), normal=(0,0,1))

  plt = Plotter(N=2,axes=1)
  plt.at(0).show(sp_vol,  __doc__)
  plt.at(1).show("..cutted volume", cut_vol)
  plt.interactive().close()
marcomusy commented 1 year ago

Hi Umut, all good! The problem is that you cannot cut a Volume with a plane because the result would not be a Volume but rather a UGrid (unstructured grid, a mixture of different types of basic cells). Out of a UGrid you can create a TetMesh (tetrahedral mesh), but this is not probably what you want to do.. (?)

pip install -U git+https://github.com/marcomusy/vedo.git

then

from vedo import *

sphere = Sphere()
sp_vol = sphere.binarize(spacing=(0.1,0.1,0.1))

ugrid = sp_vol.cut_with_plane(origin=(0,0,0.5), normal=(1,1,1))
ugrid.print()

tmesh = TetMesh(ugrid)
tmesh.print()

plt = Plotter(N=2, axes=1)
plt.at(0).show(sp_vol)
plt.at(1).show("..cut Volume to generate a TetMesh", ugrid)
plt.interactive().close()

Screenshot from 2023-09-13 12-59-45

If you want to cut a Volume along X Y or Z then you can do it with Volume.crop()

smoothumut commented 1 year ago

Hi Marco, Thanks a lot for your quick return and explaination. Everyday I am learning a lot from your library.

but for this case, As you have said that is not what I am looking for but many many thanks anyway.

Also the crop function wont help me because I need to crop the volume with a plane with custom normal I need to find another way. May be binarized mesh is not a good solution for me. it is getting too heavy if I binarize it with (0.01,0.01,0.01) I want to create a cover for my mesh. My plan was to convert the mesh into volume, then dilate, then remove the first volume, then cut the top with cut_with_plane. May be I should go with TetMesh

marcomusy commented 1 year ago

What do you mean with "create a cover for my mesh" ? You can create a volume dilate it, then isosurface it back to a polygonal mesh.

smoothumut commented 1 year ago

I want to have a solid volume between dilated isosurface and the mesh'S surface ( or the volume's isosurface in the beginning). I call it "cover for the mesh" : )

marcomusy commented 1 year ago

OK, a Volume (aka 3D image) is always a "cube"-like shape defined by its bounding box in the Cartesian space. Think of it as a 2d image with a z dimension where "pixels" become "voxels". Then you have other data structures like polygonal meshes (which in general do not define a volume because they are made of 2d triangles of zero volume) and are very useful to represent surfaces in 2d and 3d space. Then you have tetrahedral mesh where the triangle become a tetrahedron (a cell) so it identifies a volume in 3d space.

Depending on your needs and final goal you may want to use one of these data structures.

smoothumut commented 1 year ago

I understand the Volume and Mesh but Tetrahedral Mesh was not clear and now it makes more sense. Thanks a lot for your time Marco, have a great day

smoothumut commented 1 year ago

Hi Marco, just a quick question. Is there any simple way to create a tetmesh from a mesh ( not from a sphere Mesh) or is there any simple way to create tetmesh from a binarized volume or vtkvolume I couldnt be sure if your library has a way to do that I miss thanks thanks in advance

marcomusy commented 1 year ago

You can create a tetmesh from a Mesh (one example would be the above, plus you can cut the tetmesh with a Mesh), or directly with examples/volumetric/tetralize_surface.py Also see examples/volumetric/tet_threshold.py

smoothumut commented 1 year ago

Thanks a lot Marco, I know I was missing something : ) Have a great day !!