因为一般 VTK 自己的形状都是 polydata的,我们有时候需要将两个或者多个数据进行合并显示。
直接说,使用的类是 vtkAppendPolyData,输入多个数据,直接 update() 就可以得到想要的结果。之后可以加一个 vtkCleanPolyData 的对象进行基本的数据处理 - 去掉重复的点。
示例 创建了一个 圆锥和圆柱(圆管)。
//Create a cone
vtkSmartPointer<vtkConeSource> coneSource =
vtkSmartPointer<vtkConeSource>::New();
coneSource->SetCenter(0, 0, 0);
coneSource->SetRadius(2);
coneSource->SetHeight(10);
coneSource->SetResolution(50);
coneSource->Update();
vtkIntArray *scalars = vtkIntArray::New();
vtkPointData *conePointDataPointer = coneSource->GetOutput()->GetPointData();
vtkPolyData *conePolyDataPointer = coneSource->GetOutput();
scalars->SetNumberOfTuples(conePolyDataPointer->GetNumberOfPoints());
scalars->SetNumberOfComponents(1);
for (int i = 0; i < conePolyDataPointer->GetNumberOfPoints(); i++)
{
scalars->SetTuple1(i, 1);
}
conePointDataPointer->SetScalars(scalars);
// second
vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New();
line->SetPoint1(1.0, 0, 0);
line->SetPoint2(0, 1.0, 20);
vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
tube->SetInputConnection(line->GetOutputPort());
tube->SetRadius(2);
tube->SetNumberOfSides(50);
tube->SetCapping(1);
tube->Update();
vtkIntArray *scalarstube = vtkIntArray::New();
vtkPointData *tubePointDataPointer = tube->GetOutput()->GetPointData();
vtkPolyData *tubePolyDataPointer = tube->GetOutput();
scalarstube->SetNumberOfTuples(tubePolyDataPointer->GetNumberOfPoints());
scalarstube->SetNumberOfComponents(1);
for (int i = 0; i < tubePolyDataPointer->GetNumberOfPoints(); i++)
{
scalarstube->SetTuple1(i, 0);
}
tubePointDataPointer->SetScalars(scalarstube);
// combine two poly data
vtkSmartPointer<vtkAppendPolyData> appendFilter =
vtkSmartPointer<vtkAppendPolyData>::New();
appendFilter->AddInputData(tube->GetOutput());
appendFilter->AddInputData(coneSource->GetOutput());
appendFilter->Update();
// Remove any duplicate points.
vtkSmartPointer<vtkCleanPolyData> cleanFilter =
vtkSmartPointer<vtkCleanPolyData>::New();
cleanFilter->SetInputConnection(appendFilter->GetOutputPort());
cleanFilter->Update();
//Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> appendmapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
appendmapper->SetInputConnection(cleanFilter->GetOutputPort());
vtkSmartPointer<vtkActor> appendactor =
vtkSmartPointer<vtkActor>::New();
appendactor->SetMapper(appendmapper);
//Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
//Add the actors to the scene
renderer->AddActor(appendactor);
renderer->SetBackground(.3, .2, .1); // Background color dark red
//Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
结果