vtkbool icon indicating copy to clipboard operation
vtkbool copied to clipboard

Crash when computing difference for certain meshes

Open lassoan opened this issue 4 years ago • 9 comments

First of all, thank you very much for this library. We are trying to use it for creating surgical guides for fibula flap reconstructions and it seems to work very well for most of the cases.

Unfortunately, sometimes the code crashes in this line:

https://github.com/zippy84/vtkbool/blob/c0de9f68b41e53f71624dcf9aa3e4efddabfad4f/libs/vp/RmTrivials.cxx#L85

because _s vector is empty.

If you want to reproduce it: subtract DrillHole-dec.vtk from Plate-dec.vtk (the files are available here).

Do you have any suggestion for at least how to avoid the crash? Should we just add a if (!_s.empty()) {} check so that p.dir remains Dir::UNDEFINED?

lassoan avatar Feb 18 '21 16:02 lassoan

After adding a _s.empty() check, I got an assert here:

https://github.com/zippy84/vtkbool/blob/c0de9f68b41e53f71624dcf9aa3e4efddabfad4f/libs/vp/RmTrivials.cxx#L355

because number of groups is 2. Replacing the assert with a if (grps.size() > 0) {} check "fixes" the crash but I'm not sure if this is the right thing to do.

lassoan avatar Feb 18 '21 16:02 lassoan

Hi Andras.

At first I have to say, that the master version is not crash safe. It mostly crashes at this line:

https://github.com/zippy84/vtkbool/blob/c0de9f68b41e53f71624dcf9aa3e4efddabfad4f/vtkPolyDataBooleanFilter.cxx#L2854

Currently I'm working on a new version that fixes that. The lib is also not type-safe. There are many warnings when -Wconversion is enabled. My first changes could be found in the refactoring-branch, but it is not ready yet. The holes-merger an the decomposer are widely commented out.

I will take look for a solution of your issue.

zippy84 avatar Feb 18 '21 18:02 zippy84

Thank you for the quick response, looking forward to the fix.

We have found some additional issues (such as invalid comparator assert here because A>B and A<B did not provide consistent result), but if you are still working on some improvements then we'll hold this back until those get integrated.

lassoan avatar Feb 18 '21 19:02 lassoan

Feel free to submit them now. Maybe it's already fixed.

zippy84 avatar Feb 18 '21 20:02 zippy84

https://github.com/zippy84/vtkbool/blob/c0de9f68b41e53f71624dcf9aa3e4efddabfad4f/vtkPolyDataBooleanFilter.cxx#L2854

Currently I'm working on a new version that fixes that.

Hi @zippy84, Thank you very much for this library. I am trying to use boolean difference of this filter on my meshes. Most of the time it is working fine but some cases my program is crashing at the above mentioned line(#L2854 of vtkPolyDataBooleanFilter.cxx). I am using this filter in a loop and some times at the edge of the Sphere mesh if I do boolean diff operation I get an error message saying "Stripes are invalid" with some part of the mesh is left behind( Can see small parts of the mesh spiked at the edges in the attached image) and if I try to do difference operation on this area, then my program is crashing. I see in this thread, you are already working on this issue. Do you have any suggestions to stop this crash until your fix is integrated into master branch? Screenshot from 2021-03-12 12-49-33

mallikarjun49 avatar Mar 15 '21 06:03 mallikarjun49

@mallikarjun49 Can you supply two meshes that does not work?

zippy84 avatar Mar 15 '21 15:03 zippy84

@mallikarjun49 Can you supply two meshes that does not work?

Thank you very much for the quick response. I am basically using vtkSphereSource and vtkCylinderSource to do the difference operation. I am doing this operation in a loop. Basically my cylinder source translates to new position on key press and try to do difference operation on the output of the boolean filter in the previous step. //loop

{ 
   auto booleanFilter = vtkSmartPointer<vtkPolyDataBooleanFilter>::New();
   booleanFilter->SetInputData(0,polydata_);
   booleanFilter->SetInputData(1,transformFilter_->GetOutput()); //cylinder translates to new position in each iteration
   booleanFilter->SetOperModeToDifference();
   booleanFilter->Update();

   if(booleanFilter->GetOutput()->GetNumberOfCells() > 0)
   {
       polydata_->Initialize();
       polydata_->DeepCopy(booleanFilter->GetOutput());
   }
 }

mallikarjun49 avatar Mar 15 '21 16:03 mallikarjun49

@mallikarjun49 export the meshes (polydata_ and transformFilter_->GetOutput()) to files, upload them somewhere, and post the download links here. It is not possible to reproduce the meshes exactly based on your approximate instructions.

Also note that vtkCylinderSource creates meshes that are very efficient for rendering but almost unusable for 3D modeling, because it creates extremely ill-shaped triangles (that span between the top and bottom planes of the cylinder). Use vtkLineSource and vtkTubeFilter instead, because with that you can create better shaped triangles.

lassoan avatar Mar 15 '21 16:03 lassoan

@lassoan Thank you for your suggestions. I have now used vtkLineSource with vtkTubeFilter instead of vtkCylinderSource, and exported the mesh files. @zippy84 , Please find the attached source code files and the Mesh files for your reference. Source code should work as it is or with minor tweaks to CMakelists.txt.

BoolDiff.zip MeshFiles.zip MeshFiles2.zip Screenshot from 2021-03-15 23-06-38

mallikarjun49 avatar Mar 15 '21 18:03 mallikarjun49

I'm closing this as the code this refers to no longer exists.

zippy84 avatar May 28 '23 13:05 zippy84

Great! Thank you for working on it.

lassoan avatar May 29 '23 02:05 lassoan

Thank you very much @zippy84 !!

mallikarjun49 avatar May 29 '23 02:05 mallikarjun49