0

我正在尝试实现(相当)简单的场景,其中我有大约 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);
    }
}

第二个镜头好一点。我创建了“演员池”,我在其中重用演员并隐藏不需要的演员。尽管如此,移动相机还是很慢,我的 UI 的其余部分(我在 Vtk 小部件中还有一些额外的小部件)似乎很慢。

我找不到任何相关的 Vtk 资源,其中场景是“动态的”。所有示例都预加载所有场景元素并进一步使用它们。谁能告诉我我在这里做错了什么?

4

2 回答 2

1

这是我的可视化 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);
}
于 2017-07-25T16:13:17.257 回答
1

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

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);

上面的代码将添加尽可能多的立方体,并且您希望使用单个演员!(在我的情况下,这是惊人的性能提升)

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

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

(请注意,我没有在场景中移除/添加演员,这是另一个提升)

于 2017-07-27T08:45:55.767 回答