[vtkusers] PolyData Boolean operation problem

kenichiro yoshimi rccm.kyoshimi at gmail.com
Wed Jul 25 21:34:11 EDT 2012


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



More information about the vtkusers mailing list