VTK 管道更新

VTK pipeline update

我在 Linux 上使用 VTK-6.2、C++ (gcc-4.7.2) 并且我有以下 VTK 管道设置(请忽略实现、细节并关注管道:cone->filter- >映射器->演员):

// cone/initialize
vtkConeSource cone;

// add cone(s) to filter
vtkAppendFilter filter;
filter.AddInputData(cone.GetOutput());

// add filter to mapper
vtkDataSetMapper mapper;
mapper.SetInputData(filter->GetOutput());

// actor
vtkActor actor;
actor.SetMapper(mapper);

场景渲染良好。

问题

我想更新原始数据(即视锥细胞)和要正确渲染的演员。

请原谅,我不是 VTK 专家,管道很混乱。也许一种方法是简化我的管道。

谢谢

[更新]

根据 对类似 post 的回答,原始数据 (vtkConeSource) 被转换(添加到 vtkAppendFilter 时变为 vtkUnstructuredGrid ) 所以即使我保留了原始数据,更改它们也没用。

VTK 管道是需求驱动的管道。即使管道的其中一个元素被修改,它们也不会自动更新。我们需要在管道的最后一个 vtkAlgorithm(或其派生的 class 对象)上显式调用 Update() 函数来更新整个管道。设置管道的正确方法是当我们连接两个派生自 vtkAlgorithm 类型的对象时使用

currAlgoObj->SetInputConnection( prevAlgoObj->GetOutputPort() )

而不是

currAlgoObj->SetInputData( prevAlgo->GetOutput() )

然后我们可以使用指向 actor 对象的指针更新管道,方法是 actor->GetMapper()->Update() 如下例所示。

在这个例子中,我们将从一个圆锥源创建一个圆锥体,通过 vtkAppendFilter 然后改变原始圆锥源的高度并在另一个 window 中渲染它以查看更新的锥体。 (您必须关闭第一个渲染 window 才能在第二个 window 中看到更新的圆锥体。)

#include <vtkConeSource.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkAppendFilter.h>

int main(int, char *[])
{
    // Set up the data pipeline
    auto cone = vtkSmartPointer<vtkConeSource>::New();
    cone->SetHeight( 1.0 );

    auto appf = vtkSmartPointer<vtkAppendFilter>::New();
    appf->SetInputConnection( cone->GetOutputPort() );

    auto coneMapper = vtkSmartPointer<vtkDataSetMapper>::New();
    coneMapper->SetInputConnection( appf->GetOutputPort() );

    auto coneActor = vtkSmartPointer<vtkActor>::New();
    coneActor->SetMapper( coneMapper );

    // We need to update the pipeline otherwise nothing will be rendered
    coneActor->GetMapper()->Update();

    // Connect to the rendering portion of the pipeline
    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor( coneActor );
    renderer->SetBackground( 0.1, 0.2, 0.4 );

    auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize( 200, 200 );
    renderWindow->AddRenderer(renderer);

    auto renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindowInteractor->Start();

    // Change cone property
    cone->SetHeight( 10.0 );

    //Update the pipeline using the actor object
    coneActor->GetMapper()->Update();

    auto renderer2 = vtkSmartPointer<vtkRenderer>::New();
    renderer2->AddActor( coneActor );
    renderer2->SetBackground( 0.1, 0.2, 0.4 );

    auto renderWindow2 = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow2->SetSize( 200, 200 );
    renderWindow2->AddRenderer(renderer2);

    auto renderWindowInteractor2 =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor2->SetRenderWindow(renderWindow2);

    renderWindowInteractor2->Start();

    return EXIT_SUCCESS;
}