VTK 使用 Procedural Source Object 来展示一些固定的三维物体,比如 多边形柱体、多边球体、棱锥等等,在三维画布下添加 Prop 的示例代码如下:
auto sphere = vtkSmartPointer<vtkSphereSource>::New();
sphere->SetRadius(24);
auto cylinder = vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetResolution(3);
cylinder->SetHeight(48);
cylinder->SetRadius(48);
auto mapperSphere = vtkSmartPointer<vtkPolyDataMapper>::New();
mapperSphere->SetInputConnection(sphere->GetOutputPort());
auto actorSphere = vtkSmartPointer<vtkActor>::New();
actorSphere->SetMapper(mapperSphere);
actorSphere->SetPosition(0, 0, 0);
auto mapperCylinder = vtkSmartPointer<vtkPolyDataMapper>::New();
mapperCylinder->SetInputConnection(cylinder->GetOutputPort());
auto actorCylinder = vtkSmartPointer<vtkActor>::New();
actorCylinder->SetMapper(mapperCylinder);
actorCylinder->SetPosition(144, 0, 0);
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actorSphere);
renderer->AddActor(actorCylinder);
renderer->SetBackground(0.6, 0.6, 0.6);
auto actorAxes = vtkSmartPointer<vtkAxesActor>::New();
actorAxes->SetPosition(0, 0, 0);
actorAxes->SetTotalLength(120, 120, 120);
actorAxes->SetShaftType(0);
actorAxes->SetAxisLabels(0);
actorAxes->SetCylinderRadius(0.02);
renderer->AddActor(actorAxes);
auto win = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
win->AddRenderer(renderer);
ui->widgetVtk->setRenderWindow(win);
需要注意的地方是,在给 Actor 调用 SetPosition 的时候,这个位置算的是三维物体的重心位置到原点的相对距离,而原点的位置默认是(0, 0, 0);
可以通过 Source 的 SetCenter 来重置原点的位置。