【发布时间】:2020-02-11 10:51:33
【问题描述】:
我正在尝试使用 vtkBooleanOperationPolyDataFilter 类计算两个 vtkPolyData 之间的差异。我阅读了VTK examples website 中提供的唯一示例,并尝试使用它。
但是,我的问题有点不同,因为我有 .stl 文件。因此,首先我必须将 .stl 文件转换为 vtkPolyData。我正在使用函数convert_stl_to_polydata() 执行此操作。然后,我使用compute_difference() 计算差异并写入输出。
#include <string>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkSTLReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkBooleanOperationPolyDataFilter.h>
vtkSmartPointer<vtkPolyData> convert_stl_to_polydata (std::string input) {
auto stl_reader = vtkSmartPointer<vtkSTLReader>::New();
stl_reader->SetFileName (input.c_str());
stl_reader->Update();
auto poly_data = vtkSmartPointer<vtkPolyData>::New();
poly_data->ShallowCopy(stl_reader->GetOutput());
return poly_data;
}
void compute_difference (vtkSmartPointer<vtkPolyData> input1,
vtkSmartPointer<vtkPolyData> input2, std::string output) {
auto boolean_operation = vtkSmartPointer<vtkBooleanOperationPolyDataFilter>::New();
boolean_operation->SetInputData (0, input1);
boolean_operation->SetInputData (1, input2);
boolean_operation->SetOperationToDifference();
boolean_operation->Update();
// write the result as an Unstructured Grid
auto unstructured_grid = vtkSmartPointer<vtkUnstructuredGrid>::New();
unstructured_grid->ShallowCopy(boolean_operation->GetOutput());
auto writer = vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
writer->SetFileName(output.c_str());
writer->SetInputData(unstructured_grid);
writer->SetDataModeToAscii();
writer->Update();
writer->Write();
}
int main () {
std::string input1_filename {"Data/input_1.stl"};
std::string input2_filename {"Data/input_2.stl"};
std::string diff_filename {"Data/difference.vtu"};
auto input1 = vtkSmartPointer<vtkPolyData>::New();
auto input2 = vtkSmartPointer<vtkPolyData>::New();
input1 = convert_stl_to_polydata(input1_filename);
input2 = convert_stl_to_polydata(input2_filename);
compute_difference (input1, input2, diff_filename);
return 0;
}
第一个功能运行良好。但是,当我用 paraview 打开时,第二个函数的输出文件是空的。有关更多信息,我正在使用 VTK 8.2.0。
问题
- 我是否正确使用了 vtkBooleanOperationPolyDataFilter?
- 是否可以直接在 STL 网格上使用布尔运算?
感谢任何改进代码的建议。
【问题讨论】: