[vtkusers] PolyData Boolean operation problem

Malsoaz James jmalsoaz at yahoo.fr
Thu Jul 26 04:11:57 EDT 2012


Hi Kenichiro,

We are several VTK users experiencing similar problems. The vtkBooleanOperationPolyDataFilter class works great in most cases. Unfortunately, you should be aware that in some other cases, the algorithm presents several limitations (especially with simple geometrical surfaces). 

In your case, some triangles of your input meshes are coplanar. Unfortunately, coplanar intersections are not currently handled. To solve this, I have implemented my own version of boolean operations for basic geometrical objects based on the Javascript source code found here: http://evanw.github.com/csg.js/. It uses the CSG algorithm and it works perfectly for cube, cylinder, sphere and cone. Another solution could be to rotate your cylinder to avoid coplanar surfaces.

Another bug, you should be aware of, produces holes in resulting mesh. You can see enclosed several images showing this problem. After some researches, I have been able to find that the error comes from the vtkImplicitPolyDataDistance class. Indeed, some points are classified inside the second mesh while they are not because of the sign found for the distance between the input mesh and the point. This induces errors in the vtkDistancePolyDataFilter. I'm still working on this to find a solution as soon as possible. One solution could be to use another algorithm to find whether a point is inside or outside a mesh (for example using ray casting) but I guess, it could increase the computation time. Thus, currently, I'm checking the current algorithm based on the angle weighted pseudo-normal.

Anyway, regarding my needs, these problems make the vtkBooleanOperationPolyDataFilter class (as it is now) unusable in my software (I need a high percent of positive result). A good news is that the main developer (Cory Quammen) plans to debug these classes as soon as he finds time to do it. Because I can't move forward without the boolean operations on mesh, I will pursue to work on these VTK classes for few days. If I fail, I will move to another implementation such as the Carve library.

For more reading, you should look at previous threads in the VTK mailing list, such as:
http://vtk.1045678.n5.nabble.com/Holes-in-mesh-after-vtkBooleanOperationPolyDataFilter-td5713284.html
http://vtk.1045678.n5.nabble.com/Non-manifold-triangles-after-running-vtkBooleanOperationPolydataFilter-td5713964.html

Best
James



________________________________
 De : kenichiro yoshimi <rccm.kyoshimi at gmail.com>
À : vtkusers at vtk.org 
Envoyé le : Jeudi 26 juillet 2012 3h34
Objet : [vtkusers] PolyData Boolean operation problem
 
Hi all,

I try Boolean Operations for a box and a cylinder which are Poly Data
Set referring to the example:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/BooleanOperationPolyDataFilter

But, I have some of results which are the lack of  polygons as they
should be. My source code is the following, and it's output
"union5.000000.vtp" has the problem that some of polygons in
cylinder's surface are eliminated.

---------------------------
#include <vtkBooleanOperationPolyDataFilter.h>
#include <vtkCubeSource.h>
#include <vtkCylinderSource.h>
#include <vtkSmartPointer.h>
#include <vtkTriangleFilter.h>
#include <vtkXMLPolyDataWriter.h>

enum
{
  UNION,
  INTERSECTION,
  DIFFERENCE
};

int ExportBooleanOperation(double cyl_x, int operation)
{
  std::string outfilename = "";

  vtkSmartPointer<vtkCubeSource> box =
    vtkSmartPointer<vtkCubeSource>::New();
 
 box->SetXLength(10.0);
  box->SetYLength(1.0);
  box->SetZLength(10.0);

  vtkSmartPointer<vtkTriangleFilter> boxTri =
    vtkSmartPointer<vtkTriangleFilter>::New();
  boxTri->SetInput(box->GetOutput());

  vtkSmartPointer<vtkCylinderSource> cyl =
    vtkSmartPointer<vtkCylinderSource>::New();
  cyl->SetResolution(50);
  cyl->SetHeight(5.0);
  cyl->SetRadius(0.5);
  cyl->SetCenter(cyl_x, 0, 0.0);

  vtkSmartPointer<vtkTriangleFilter> cylTri =
    vtkSmartPointer<vtkTriangleFilter>::New();
  cylTri->SetInput(cyl->GetOutput());

  vtkSmartPointer<vtkBooleanOperationPolyDataFilter> booleanOperation =
    vtkSmartPointer<vtkBooleanOperationPolyDataFilter>::New();
 
 booleanOperation->SetInputConnection(0,
boxTri->GetOutput()->GetProducerPort());
  booleanOperation->SetInputConnection(1,
cylTri->GetOutput()->GetProducerPort());

  if (operation == UNION)
    {
    booleanOperation->SetOperationToUnion();
    outfilename = "union";
    }
  else if (operation == INTERSECTION)
    {
    booleanOperation->SetOperationToIntersection();
    outfilename = "intersection";
    }
  else if (operation == DIFFERENCE)
    {
    booleanOperation->SetOperationToDifference();
    outfilename = "difference";
    }
  else
    {
    std::cout << "Unknown operation: " << operation << std::endl;
    return EXIT_FAILURE;
    }

 
 char extension[64];
  sprintf(extension, "%f.vtp", cyl_x);
  outfilename += extension;
  vtkSmartPointer<vtkXMLPolyDataWriter> writer =
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName(outfilename.c_str());
  writer->SetInput(booleanOperation->GetOutput(0));
  writer->Write();

  return EXIT_SUCCESS;
}

int main(int argc, char* argv[])
{
  ExportBooleanOperation(0.0, UNION);
  ExportBooleanOperation(0.0, DIFFERENCE);
  ExportBooleanOperation(5.0, UNION);
  ExportBooleanOperation(5.0, DIFFERENCE);

  return EXIT_SUCCESS;
}
---------------------------

Would anyone please let me know how to fix.

Thanks,
yoshimi
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120726/466e07c3/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: b.png
Type: image/png
Size: 43832 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120726/466e07c3/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: f.png
Type: image/png
Size: 84482 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120726/466e07c3/attachment-0001.png>


More information about the vtkusers mailing list