VTK boolean operation 使用方法

    在使用VTK的boolean operation时,仅仅接受从polydata数据结构中通过shallow copy或者直接获取数据中的polydata作为输入,才能够正确地进行布尔运算。这个类不同于其他的类,可以直接使用filter的流处理模式来进行,即通过SetInputConnection()和GetOutputPort()来获得。

    并且VTK的boolean operationa中,仅仅能够处理两个三角面片交点为2或者更小的情况。对于两个三角面片共面的情况,交点情况将会变得更加复杂。因此,在处理共面三角面片之间的布尔运算时,其运算将会出现问题。

    从VTK网站中获得的实例如下:

   

int main(int argc, char *argv[])
{
  vtkSmartPointer input1;
  vtkSmartPointer input2;

  vtkSmartPointer sphereSource1 = vtkSmartPointer::New();
  sphereSource1->SetCenter(.25, 0, 0);
  sphereSource1->Update();
  input1 = sphereSource1->GetOutput();// use the polydata as input
 
  vtkSmartPointer sphereSource2 = vtkSmartPointer::New();
  sphereSource2->Update();
  input2 = sphereSource2->GetOutput();

  vtkSmartPointer booleanOperation =
    vtkSmartPointer::New();
  if (operation == "union")
    {
      booleanOperation->SetOperationToUnion();
    }
  else if (operation == "intersection")
    {
      booleanOperation->SetOperationToIntersection();
    }
  else if (operation == "difference")
    {
      booleanOperation->SetOperationToDifference();
    }
  else
    {
    std::cout << "Unknown operation: " << operation << std::endl;
    return EXIT_FAILURE;
    }
#if VTK_MAJOR_VERSION <= 5
  booleanOperation->SetInputConnection( 0, input1->GetProducerPort() );
  booleanOperation->SetInputConnection( 1, input2->GetProducerPort() );
#else
  booleanOperation->SetInputData( 0, input1 ); // set the input data
  booleanOperation->SetInputData( 1, input2 );
#endif
  vtkSmartPointer booleanOperationMapper =
    vtkSmartPointer::New();
  booleanOperationMapper->SetInputConnection( booleanOperation->GetOutputPort() );
  booleanOperationMapper->ScalarVisibilityOff();
 
  vtkSmartPointer booleanOperationActor =
    vtkSmartPointer::New();
  booleanOperationActor->SetMapper( booleanOperationMapper );
 
  vtkSmartPointer renderer =
    vtkSmartPointer::New();
  renderer->AddViewProp(booleanOperationActor);
  renderer->SetBackground(.1, .2, .3);
  vtkSmartPointer renderWindow =
    vtkSmartPointer::New();
  renderWindow->AddRenderer( renderer );
 
  vtkSmartPointer renWinInteractor =
    vtkSmartPointer::New();
  renWinInteractor->SetRenderWindow( renderWindow );
 
  renderWindow->Render();
  renWinInteractor->Start();
 
  return EXIT_SUCCESS;
}


你可能感兴趣的:(计算机图形学,vtk)