[vtkusers] wrong result while using vtkBooleanOperationPolyDataFilter

NsPx nspx.roronoa at gmail.com
Fri Aug 24 10:38:45 EDT 2012


Hi all,

I'm trying to use vtkBooleanOperationPolyDataFilter but it seems I'm 
missing something. I get unexpected result when computing the difference 
between a simple sphere and a poledron.

Can someone explain me what is wrong in the following example ?

Thanks by advance.

##############

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyData .h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkIdType.h>
#include <vtkTriangle.h>
#include <vtkBooleanOperationPolyDataFilter.h>
#include <vtkRenderer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

#include <vector>

int main(int argc, char *argv[])
{
     // sphere
     vtkSmartPointer<vtkSphereSource> sphereSource1 = 
vtkSmartPointer<vtkSphereSource>::New();
     sphereSource1->SetCenter(.25, 0, 0);
     sphereSource1->Update();

     vtkPolyData * sphere = sphereSource1->GetOutput();

     // design a bloc which intersects the sphere
     double bounds[6];
     sphere->GetBounds(bounds);

     double x1,x2,y1,y2,z1,z2;
     x1=bounds[0];
     x2=bounds[1];
     y1=bounds[2]-0.5;
     y2=bounds[3]+0.5;
     z1=bounds[4]-0.5;
     z2=bounds[5]+0.5;

     double lx = x2-x1;

     vtkSmartPointer<vtkPolyData> bloc = 
vtkSmartPointer<vtkPolyData>::New();
     vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
     vtkSmartPointer<vtkCellArray> triangles = 
vtkSmartPointer<vtkCellArray>::New();
     bloc->SetPoints(points);
     bloc->SetPolys(triangles);

     vector<vtkIdType> ids;
     ids.push_back( points->InsertNextPoint( x1+0.3*lx, y1, z2));
     ids.push_back( points->InsertNextPoint( x1+0.6*lx, y1, z2));
     ids.push_back( points->InsertNextPoint( x1+0.6*lx, y2, z2));
     ids.push_back( points->InsertNextPoint( x1+0.3*lx, y2, z2));
     ids.push_back( points->InsertNextPoint( x1+0.3*lx, y1, z1));
     ids.push_back( points->InsertNextPoint( x1+0.6*lx, y1, z1));
     ids.push_back( points->InsertNextPoint( x1+0.6*lx, y2, z1));
     ids.push_back( points->InsertNextPoint( x1+0.3*lx, y2, z1));

     vtkSmartPointer<vtkTriangle> t0 = vtkSmartPointer<vtkTriangle>::New();
     t0->GetPointIds()->SetId ( 0, ids[0]);
     t0->GetPointIds()->SetId ( 1, ids[1]);
     t0->GetPointIds()->SetId ( 2, ids[2]);
     triangles->InsertNextCell ( t0 );
     vtkSmartPointer<vtkTriangle> t1 = vtkSmartPointer<vtkTriangle>::New();
     t1->GetPointIds()->SetId ( 0, ids[0]);
     t1->GetPointIds()->SetId ( 1, ids[2]);
     t1->GetPointIds()->SetId ( 2, ids[3]);
     triangles->InsertNextCell ( t1 );
     vtkSmartPointer<vtkTriangle> t2 = vtkSmartPointer<vtkTriangle>::New();
     t2->GetPointIds()->SetId ( 0, ids[1]);
     t2->GetPointIds()->SetId ( 1, ids[5]);
     t2->GetPointIds()->SetId ( 2, ids[2]);
     triangles->InsertNextCell ( t2 );
     vtkSmartPointer<vtkTriangle> t3 = vtkSmartPointer<vtkTriangle>::New();
     t3->GetPointIds()->SetId ( 0, ids[2]);
     t3->GetPointIds()->SetId ( 1, ids[5]);
     t3->GetPointIds()->SetId ( 2, ids[6]);
     triangles->InsertNextCell ( t3 );
     vtkSmartPointer<vtkTriangle> t4 = vtkSmartPointer<vtkTriangle>::New();
     t4->GetPointIds()->SetId ( 0, ids[2]);
     t4->GetPointIds()->SetId ( 1, ids[6]);
     t4->GetPointIds()->SetId ( 2, ids[7]);
     triangles->InsertNextCell ( t4 );
     vtkSmartPointer<vtkTriangle> t5 = vtkSmartPointer<vtkTriangle>::New();
     t5->GetPointIds()->SetId ( 0, ids[2]);
     t5->GetPointIds()->SetId ( 1, ids[7]);
     t5->GetPointIds()->SetId ( 2, ids[3]);
     triangles->InsertNextCell ( t5 );
     vtkSmartPointer<vtkTriangle> t6 = vtkSmartPointer<vtkTriangle>::New();
     t6->GetPointIds()->SetId ( 0, ids[3]);
     t6->GetPointIds()->SetId ( 1, ids[7]);
     t6->GetPointIds()->SetId ( 2, ids[4]);
     triangles->InsertNextCell ( t6 );
     vtkSmartPointer<vtkTriangle> t7 = vtkSmartPointer<vtkTriangle>::New();
     t7->GetPointIds()->SetId ( 0, ids[4]);
     t7->GetPointIds()->SetId ( 1, ids[3]);
     t7->GetPointIds()->SetId ( 2, ids[0]);
     triangles->InsertNextCell ( t7 );
     vtkSmartPointer<vtkTriangle> t8 = vtkSmartPointer<vtkTriangle>::New();
     t8->GetPointIds()->SetId ( 0, ids[0]);
     t8->GetPointIds()->SetId ( 1, ids[4]);
     t8->GetPointIds()->SetId ( 2, ids[5]);
     triangles->InsertNextCell ( t8 );
     vtkSmartPointer<vtkTriangle> t9 = vtkSmartPointer<vtkTriangle>::New();
     t9->GetPointIds()->SetId ( 0, ids[0]);
     t9->GetPointIds()->SetId ( 1, ids[1]);
     t9->GetPointIds()->SetId ( 2, ids[5]);
     triangles->InsertNextCell ( t9);
     vtkSmartPointer<vtkTriangle> t10 = vtkSmartPointer<vtkTriangle>::New();
     t10->GetPointIds()->SetId ( 0, ids[6]);
     t10->GetPointIds()->SetId ( 1, ids[5]);
     t10->GetPointIds()->SetId ( 2, ids[4]);
     triangles->InsertNextCell ( t10);
     vtkSmartPointer<vtkTriangle> t11 = vtkSmartPointer<vtkTriangle>::New();
     t11->GetPointIds()->SetId ( 0, ids[7]); t11->GetPointIds()->SetId ( 
1, ids[6]); t11->GetPointIds()->SetId ( 2, ids[4]);
     triangles->InsertNextCell ( t11);

     bloc->BuildLinks();

     // boolean filter
     vtkSmartPointer<vtkBooleanOperationPolyDataFilter> booleanOperation 
= vtkSmartPointer<vtkBooleanOperationPolyDataFilter>::New();
     booleanOperation->SetOperationToDifference();
     booleanOperation->SetInputConnection( 1, sphere->GetProducerPort());
     booleanOperation->SetInputConnection( 0, bloc->GetProducerPort());
     booleanOperation->Update();

     //display result
     vtkSmartPointer<vtkRenderer> renderer = 
vtkSmartPointer<vtkRenderer>::New();
     vtkSmartPointer<vtkPolyDataMapper> mapper = 
vtkSmartPointer<vtkPolyDataMapper>::New();
     mapper->SetInput(booleanOperation->GetOutput());
     vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
     actor->SetMapper(mapper);

     renderer->AddActor( actor);

     vtkSmartPointer<vtkRenderWindow> rendWindow = 
vtkSmartPointer<vtkRenderWindow>::New();
     rendWindow->AddRenderer(renderer);

     vtkSmartPointer<vtkRenderWindowInteractor> iren = 
vtkSmartPointer<vtkRenderWindowInteractor>::New();
     iren->SetRenderWindow( rendWindow);

     renderer->ResetCamera();
     rendWindow->Render();
     iren->Start();

     return 0;
}

##############
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120824/73e10554/attachment.htm>


More information about the vtkusers mailing list