如何使用 vtk 计算 3 个几何图形之间的布尔交集?

How to compute boolean intersection between 3 geometries using vtk?

我正在尝试使用 VTK 计算 3 个几何图形之间的交集。

我从 BooleanOperationPolyDataFilter example 开始并做了一些小的调整,例如:

添加第三个球体:

vtkNew<vtkSphereSource> sphereSource3;
    sphereSource3->SetCenter(0.25, 0, .5);
    sphereSource3->SetPhiResolution(21);
    sphereSource3->SetThetaResolution(21);
    sphereSource3->Update();
    
    input3 = sphereSource3->GetOutput();

尝试插入第一个布尔交集(在第一个两个球体之间)的结果,然后作为与第三个球体的联合测试:

booleanOperation->SetInputData(0, input1);
  booleanOperation->SetInputData(1, input2);
  
  vtkNew<vtkBooleanOperationPolyDataFilter> booleanOperation2;
  booleanOperation2->SetOperationToUnion();
  booleanOperation2->SetInputData(0, booleanOperation->GetOutput());
  booleanOperation2->SetInputData(1, input3);
 
  vtkNew<vtkPolyDataMapper> booleanOperationMapper;
  booleanOperationMapper->SetInputConnection(booleanOperation2->GetOutputPort());
  booleanOperationMapper->ScalarVisibilityOff();

完整代码清单:

#include <vtkActor.h>
#include <vtkBooleanOperationPolyDataFilter.h>
#include <vtkCleanPolyData.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkTriangleFilter.h>
 
#include <vtkBYUReader.h>
#include <vtkOBJReader.h>
#include <vtkPLYReader.h>
#include <vtkPolyDataReader.h>
#include <vtkSTLReader.h>
#include <vtkXMLPolyDataReader.h>
 
#include <vtkCylinderSource.h>
#include <vtkSphereSource.h>
#include <vtksys/SystemTools.hxx>
 
#include <chrono>
#include <ctime>
 
#include <vtkCamera.h>
namespace {
vtkSmartPointer<vtkPolyData> ReadPolyData(const char* fileName);
void PositionCamera(vtkRenderer* renderer, double* viewUp, double* position);
} // namespace
 
int main(int argc, char* argv[])
{
 
  const clock_t begin_time = clock();
 
  vtkSmartPointer<vtkPolyData> input1;
  vtkSmartPointer<vtkPolyData> input2;
  vtkSmartPointer<vtkPolyData> input3;
 
  std::string operation("intersection");
 
  if (argc == 4)
  {
    auto poly1 = ReadPolyData(argv[1]);
    vtkNew<vtkTriangleFilter> tri1;
    tri1->SetInputData(poly1);
    vtkNew<vtkCleanPolyData> clean1;
    clean1->SetInputConnection(tri1->GetOutputPort());
    clean1->Update();
    input1 = clean1->GetOutput();
 
    auto poly2 = ReadPolyData(argv[3]);
    vtkNew<vtkTriangleFilter> tri2;
    tri2->SetInputData(poly2);
    tri2->Update();
    vtkNew<vtkCleanPolyData> clean2;
    clean2->SetInputConnection(tri2->GetOutputPort());
    clean2->Update();
    input2 = clean2->GetOutput();
    operation = argv[2];
  }
  else
  {
    vtkNew<vtkSphereSource> sphereSource1;
    sphereSource1->SetCenter(0.25, 0, 0);
    sphereSource1->SetPhiResolution(21);
    sphereSource1->SetThetaResolution(21);
    sphereSource1->Update();
    input1 = sphereSource1->GetOutput();
 
    vtkNew<vtkSphereSource> sphereSource2;
    sphereSource2->Update();
    input2 = sphereSource2->GetOutput();
 
    vtkNew<vtkSphereSource> sphereSource3;
    sphereSource3->SetCenter(0.25, 0, .5);
    sphereSource3->SetPhiResolution(21);
    sphereSource3->SetThetaResolution(21);
    sphereSource3->Update();
    
    input3 = sphereSource3->GetOutput();
 
 
    if (argc == 2)
    {
      operation = argv[1];
    }
  }
 
  vtkNew<vtkNamedColors> colors;
 
  vtkNew<vtkPolyDataMapper> input1Mapper;
  input1Mapper->SetInputData(input1);
  input1Mapper->ScalarVisibilityOff();
  vtkNew<vtkActor> input1Actor;
  input1Actor->SetMapper(input1Mapper);
  input1Actor->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("Tomato").GetData());
  input1Actor->GetProperty()->SetSpecular(0.6);
  input1Actor->GetProperty()->SetSpecularPower(20);
  input1Actor->SetPosition(input1->GetBounds()[1] - input1->GetBounds()[0], 0,
                           0);
  vtkNew<vtkPolyDataMapper> input2Mapper;
  input2Mapper->SetInputData(input2);
  input2Mapper->ScalarVisibilityOff();
  vtkNew<vtkActor> input2Actor;
  input2Actor->SetMapper(input2Mapper);
  input2Actor->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("Mint").GetData());
  input2Actor->GetProperty()->SetSpecular(0.6);
  input2Actor->GetProperty()->SetSpecularPower(20);
  input2Actor->SetPosition(-(input1->GetBounds()[1] - input1->GetBounds()[0]),
                           0, 0);
  vtkNew<vtkBooleanOperationPolyDataFilter> booleanOperation;
  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;
  }
  booleanOperation->SetInputData(0, input1);
  booleanOperation->SetInputData(1, input2);
  
  vtkNew<vtkBooleanOperationPolyDataFilter> booleanOperation2;
  booleanOperation2->SetOperationToUnion();
  booleanOperation2->SetInputData(0, booleanOperation->GetOutput());
  booleanOperation2->SetInputData(1, input3);
 
  vtkNew<vtkPolyDataMapper> booleanOperationMapper;
  booleanOperationMapper->SetInputConnection(booleanOperation2->GetOutputPort());
  booleanOperationMapper->ScalarVisibilityOff();
 
  vtkNew<vtkActor> booleanOperationActor;
  booleanOperationActor->SetMapper(booleanOperationMapper);
  booleanOperationActor->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("Banana").GetData());
  booleanOperationActor->GetProperty()->SetSpecular(.6);
  booleanOperationActor->GetProperty()->SetSpecularPower(20);
 
  //std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC;
 
  vtkNew<vtkRenderer> renderer;
  renderer->AddViewProp(input1Actor);
  renderer->AddViewProp(input2Actor);
  renderer->AddViewProp(booleanOperationActor);
  renderer->SetBackground(colors->GetColor3d("Silver").GetData());
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(640, 480);
  renderWindow->SetWindowName("BooleanOperationPolyDataFilter");
 
  double viewUp[3] = {0.0, 0.0, 1.0};
  double position[3] = {0.0, -1.0, 0.0};
  PositionCamera(renderer, viewUp, position);
  renderer->GetActiveCamera()->Dolly(1.4);
  renderer->ResetCameraClippingRange();
 
  vtkNew<vtkRenderWindowInteractor> renWinInteractor;
  renWinInteractor->SetRenderWindow(renderWindow);
 
  renderWindow->Render();
  renWinInteractor->Start();
 
  return EXIT_SUCCESS;
}
namespace {
vtkSmartPointer<vtkPolyData> ReadPolyData(const char* fileName)
{
  vtkSmartPointer<vtkPolyData> polyData;
  std::string extension =
      vtksys::SystemTools::GetFilenameExtension(std::string(fileName));
  if (extension == ".ply")
  {
    vtkNew<vtkPLYReader> reader;
    reader->SetFileName(fileName);
    reader->Update();
    polyData = reader->GetOutput();
  }
  else if (extension == ".vtp")
  {
    vtkNew<vtkXMLPolyDataReader> reader;
    reader->SetFileName(fileName);
    reader->Update();
    polyData = reader->GetOutput();
  }
  else if (extension == ".obj")
  {
    vtkNew<vtkOBJReader> reader;
    reader->SetFileName(fileName);
    reader->Update();
    polyData = reader->GetOutput();
  }
  else if (extension == ".stl")
  {
    vtkNew<vtkSTLReader> reader;
    reader->SetFileName(fileName);
    reader->Update();
    polyData = reader->GetOutput();
  }
  else if (extension == ".vtk")
  {
    vtkNew<vtkPolyDataReader> reader;
    reader->SetFileName(fileName);
    reader->Update();
    polyData = reader->GetOutput();
  }
  else if (extension == ".g")
  {
    vtkNew<vtkBYUReader> reader;
    reader->SetGeometryFileName(fileName);
    reader->Update();
    polyData = reader->GetOutput();
  }
  else
  {
    vtkNew<vtkSphereSource> source;
    source->Update();
    polyData = source->GetOutput();
  }
  return polyData;
}
 
void PositionCamera(vtkRenderer* renderer, double* viewUp, double* position)
{
  renderer->GetActiveCamera()->SetFocalPoint(0.0, 0.0, 0.0);
  renderer->GetActiveCamera()->SetViewUp(viewUp);
  renderer->GetActiveCamera()->SetPosition(position);
  renderer->ResetCamera();
  return;
}
} // namespace

问题是我在球体 1,2 结果和球体 3 之间执行布尔运算时遇到此错误:

vtkDistancePolyDataFilt:82     ERR| vtkDistancePolyDataFilter (000001DE8F936820): No points/cells to operate on

我是 VTK 的新手,所以任何 hints/tips 我能做的 test/debug 都会很棒!

更新:

Marco Musy 的回答是正确的(调用 Update())。 作为参考,在我的场景中,这将是:

  booleanOperation->SetInputData(0, input1);
  booleanOperation->SetInputData(1, input2);
  // update 1st boolean operation
  booleanOperation->Update();

  vtkNew<vtkBooleanOperationPolyDataFilter> booleanOperation2;
  booleanOperation2->SetOperationToUnion();
  booleanOperation2->SetInputData(0, booleanOperation->GetOutput());
  booleanOperation2->SetInputData(1, input3);
 

当我进行一次操作时,mapped 可能已经在幕后完成了 Update(),但我完全忽略了这样一个事实,即如果我需要将其结果插入到另一个操作中,我需要手动更新每个布尔操作。

顺便说一下,他的库 vedo 真的很酷:使用起来非常简单有趣!

您可能只是在第二步中错过了对 Update() 的调用。 vtkBooleanOperationPolyDataFilter 似乎工作正常,例如。 (python):

from vedo import Sphere
s1 = Sphere([0.25, 0, 0.0])
s2 = Sphere([0.00, 0, 0.0])
s3 = Sphere([0.25, 0, 0.5])
s1.boolean("+", s2).boolean("+", s3).show()