具有 >50 个(移动)演员的 Vtk Qt 场景

Vtk Qt scene with >50 (moving) actors

我正在尝试实现(相当)简单的场景,其中我有大约 50 个正向特定方向移动的立方体。立方体的位置每秒变化 20 次。

我的第一次拍摄是在场景中添加和删除演员。这种方法无法扩展。整个场景卡顿,用户无法移动相机。

void draw(vtkRenderer *renderer)
{    
    renderer->RemoveAllViewProps(); 
    for(const Cube& cube : cubes_)
    {
        vtkSmartPointer<vtkCubeSource> cube_source = vtkSmartPointer<vtkCubeSource>::New();  
        cube_source->Update();
        cube_source->SetXLength(cube.lengt());
        cube_source->SetYLength(cube.width());
        cube_source->SetZLength(cube.height());

        vtkSmartPointer<vtkPolyDataMapper> poly_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        poly_mapper->SetInputConnection(cube_source->GetOutputPort());

        vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
        actor->SetMapper(poly_mapper);
        actor->SetPosition(cube.x(), cube.y(), cube.z());

        renderer->AddActor(actor);
    }
}

第二枪好一点。我创建了 "actor pool",我在其中重用了演员并隐藏了不需要的演员。 仍然,移动相机是滞后的,我的 UI 的其余部分(我在 Vtk 小部件中有一些额外的小部件)似乎是滞后的。

我找不到场景为 "dynamic" 的 Vtk 的任何相关来源。所有示例都预加载所有场景元素并进一步使用它们。谁能告诉我我做错了什么?

这是我的可视化 VTK 示例

.....
    vtkSmartPointer<vtkRenderer> m_vtkRenderer;
    vtkSmartPointer<MouseInteractor> m_mouseInteractor;
    QVector<vtkActor* > m_parcellationActors;
    QVector<vtkActor* > m_electrodesActors;
    QVector<vtkFollower* > m_textActors;
    QVector<vtkActor*> m_vectorFieldsActors;
    QVector<vtkActor*> m_streamlinesActors;
......
  void VisualizationWidget::create3DViewArea()
    {

    m_3DViewArea = new QStackedWidget(this);
    m_vtkWidget = new QVTKWidget(m_3DViewArea);
    m_vtkRenderer = vtkSmartPointer<vtkRenderer>::New();
    m_vtkRenderer->SetBackground(0.4, 0.4, 0.4);
    this->m_vtkWidget->GetRenderWindow()->AddRenderer(m_vtkRenderer);
    m_mouseInteractor = vtkSmartPointer<MouseInteractor>::New();
    m_mouseInteractor ->SetDefaultRenderer(m_vtkRenderer);
    this->m_vtkWidget->GetRenderWindow()->GetInteractor()->SetInteractorStyle( m_mouseInteractor);
    connect(m_mouseInteractor, &MouseInteractor::onActorSelected, this, &VisualizationWidget::onActorSelectedSlot);
    m_vtkLoadingWidget = new LoadingWidget(m_3DViewArea);
    m_vtkLoadingWidget->setIconPath(Icon::s_getTDCSLoadingGif);
    m_3DViewArea->addWidget(m_vtkLoadingWidget);
    m_3DViewArea->addWidget(m_vtkWidget);
}


void VisualizationWidget::setParcellationActors(QVector<vtkActor*> actors)
{
    m_parcellationActors = actors;
    for (int i = 0; i < m_parcellationActors.size(); ++i) {
        m_vtkRenderer->AddActor(m_parcellationActors.at(i));
    }

    m_vtkWidget->update();
}
void VisualizationWidget::setElectrodesActors(QVector<vtkActor*> actors)
{
    m_electrodesActors = actors;

    for (int i = 0; i < m_electrodesActors.size(); ++i) {
        m_vtkRenderer->AddActor(m_electrodesActors.at(i));
    }
    m_vtkWidget->update();
}

void VisualizationWidget::setElectrodesLabelsActors(QVector<vtkFollower*> actors)
{
    m_textActors = actors;

    for (int i = 0; i < m_textActors.size(); ++i) {
        m_textActors.at(i)->SetCamera(m_vtkRenderer->GetActiveCamera());
        m_vtkRenderer->AddActor(m_textActors.at(i));
    }
    m_vtkRenderer->ResetCamera();
    m_vtkWidget->update();
}

void VisualizationWidget::setVectorFieldsActors(QVector<vtkActor*> actors)
{
    for (int i = 0; i < m_vectorFieldsActors.size(); ++i) {
        m_vtkRenderer->RemoveActor(m_vectorFieldsActors.at(i));
    }
    m_vectorFieldsActors = actors;

    for (int i = 0; i <  m_vectorFieldsActors.size(); ++i) {
        changeActorOpacity(m_vectorFieldsActors[i], double(m_postProcResOpacSliders.at(i)->value()) / m_postProcResOpacSliders.at(i)->maximum());
        m_vtkRenderer->AddActor(m_vectorFieldsActors.at(i));
    }

    m_vtkRenderer->ResetCamera();
    m_vtkWidget->update();
}

void VisualizationWidget::setStreamlinesActors(QVector<vtkActor*> actors)
{
    for (int i = 0; i < m_streamlinesActors.size(); ++i) {
        m_vtkRenderer->RemoveActor(m_streamlinesActors.at(i));
    }
    m_streamlinesActors = actors;

    for (int i = 0; i < m_streamlinesActors.size(); ++i) {
        changeActorOpacity(m_streamlinesActors[i], double(m_streamLinesSlider->value()) / m_streamLinesSlider->maximum());
        m_vtkRenderer->AddActor(m_streamlinesActors.at(i));
    }

    m_vtkRenderer->ResetCamera();
    m_vtkWidget->update();
}

void VisualizationWidget::changeActorOpacity(vtkActor* actor, double opac)
{ 
    actor->SetVisibility(opac > 0.05);
    actor->GetMapper()->Modified();
    actor->GetProperty()->SetOpacity(opac);
}

经过几天的研究,我设法破解了工作解决方案:

vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
// as many points as you like
points->InsertNextPoint(-25, 0, 0);
points->InsertNextPoint(-35, 0, 0);


vtkSmartPointer<vtkFloatArray> scales = vtkSmartPointer<vtkFloatArray>::New();
scales->SetNumberOfComponents(3);
// same as number of points
scales->InsertNextTuple3(1, 1., 8.);
scales->InsertNextTuple3(1., 1., 10.);


vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
polydata->GetPointData()->SetVectors(scales);

vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New();

vtkSmartPointer<vtkGlyph3D> glyph3D = vtkSmartPointer<vtkGlyph3D>::New();
glyph3D->OrientOff(); // disable orientation
glyph3D->SetScaleModeToScaleByVectorComponents(); // sacle along each axis
glyph3D->SetSourceConnection(cubeSource->GetOutputPort());
glyph3D->SetInputData(polydata);
glyph3D->Update();

vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(glyph3D->GetOutputPort());
mapper->ScalarVisibilityOff(); // use color from actor

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->GetProperty()->SetColor(0, 1, 0); // this will change color for whole glyph
actor->SetMapper(mapper);

mapper->Update();

vtk_renderer->AddActor(actor);

上面的代码将添加尽可能多的立方体,而您希望使用单个 actor! (这对我来说是惊人的性能提升)

此外,如果你想更新立方体的位置,你只需要执行以下操作:

points->Reset();
points->InsertNextPoint(-25, 0, 0);
points->InsertNextPoint(-35, 0, 0);

scales->Reset();
scales->InsertNextTuple3(1, 1., 8.);
scales->InsertNextTuple3(1., 1., 10.);

polydata_->Modified();
// call render

(注意我不是 removing/adding 现场的演员,这是另一个提升)