marcomusy / vedo

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

volume operation crashes #1002

Closed smoothumut closed 5 months ago

smoothumut commented 6 months ago

Hi Marco, I hope you are doing well.

I have some questions regarding volume operations. this code below somehow crashes on vtkImageMathematics Update()

  from vedo import load, show, utils, Points, Volume,Plotter
  import vedo, vtk

  plotter = Plotter()

  box1 = vedo.Box(length=35,width=10,height=5).triangulate().clean().flat().wireframe(True).binarize()
  box2 = vedo.Box(length=5,width=10,height=35).triangulate().clean().flat().wireframe(True).binarize()

  plotter.add(box1,box2)
  plotter.show(interactive=True,axes=1)

  box_combined = box1.operation("+",box2)

  plotter.remove(box1,box2)
  plotter.add(box_combined)
  plotter.render()
  plotter.interactive()

here are my questions : ) 1- what do you think the problem can be for this basic boxes example ? 2- this question may look silly : ) why do box volumes are black? Am I doing something wrong while creating? (My other volumes that binarized from meshes were brown) 3- (this is general question) how can I solve or debug the problem when the problem occurs on vtk's end. Because especially on intersections, boolean operations and now this one manytimes crashes and close itself without giving any error.

Thanks in advance,

marcomusy commented 6 months ago

Hi Umut, the problem is that the "+" sign means summing the scalar data inside volumes that have the same geometry. If the volumes have different geometries (like voxel spacing or origin) some degree of interpolation is unavoidable.

I just extended operation() method to support logic operations, eg:

from vedo import Box, show
vol1 = Box(size=(35,10, 5)).binarize()
vol2 = Box(size=( 5,10,35)).binarize()

# vol = vol1.operation("+", vol2) # this will now give an error
vol = vol1.operation("xor", vol2)
print(vol)
show([[vol1, vol2], 
      ["vol1 xor vol2", vol]],
    N=2, axes=1, viewup="z",
)

Screenshot from 2024-01-04 17-27-27

smoothumut commented 5 months ago

Hi Marco, Sorry for my late reply. I was just able to return coding. Operation now it works great on logical operators also. Your interpolation under the hood saved my weeks. I have used your interpolation and also vtkImageMathematics in order to Substract from one volume to another. Without your interpolation, none of them was working THANK YOU VERY MUCH and have a great day 🙇