VTK:直到用户交互后视图才会更新

VTK: View doesn't update until after user interaction

TL;DR

我有一个管道可以读取图像并使用 VTK 显示网格;在更改图像 reader 的输入并更新管道后,网格不会更新,直到我与 window.

交互

长版

我有一个包含一系列分割文件(即 3D 图像体积,其中像素值对应于相应图像中的结构)的目录,这些文件显示了结构如何随时间变化。我在 VTK 中编写了一个实用程序,它允许我加载目录中的第一张图像,将标签可视化为网格,并通过更改文件名使用箭头键向前或向后 "step"输入图像并更新管道。这几乎可以工作——唯一的问题是,在更新管道后,网格不会更新,直到我与 window 交互(只需单击 window 中的任意位置会导致网格更新,例如)。

我尝试过的:

  1. 在 reader 上调用 Update():

    this->reader->Update();
    
  2. 正在调用 Modified() 演员:

    const auto actors = this->GetCurrentRenderer()->GetActors();
    actors->InitTraversal();
    for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i)
      {
      actors->GetNextActor()->Modified();
      }
    
  3. 在当前渲染上调用 Render() window:

    this->GetCurrentRenderer()->Render();
    

MCVE:

注意:reader 在 KeyPressInteractorStyle::UpdateReader() 中更新。

// VTK
#include <vtkSmartPointer.h>
#include <vtkNIFTIImageReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include <vtkDiscreteMarchingCubes.h>
#include <vtkProperty.h>
#include <vtkInteractorStyleTrackballCamera.h>

// Define interaction style
class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
{
  public:
    static KeyPressInteractorStyle* New();
    vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);

    virtual void OnKeyPress() 
    {
      // Get the keypress
      vtkRenderWindowInteractor *rwi = this->Interactor;
      std::string key = rwi->GetKeySym();

      // Output the key that was pressed
      std::cout << "Pressed " << key << std::endl;

      // Handle an arrow key
      if(key == "Down" || key == "Right")
        {
        this->index += 1;
        this->UpdateReader();
        }

      // Handle an arrow key
      if(key == "Up" || key == "Left")
        {
        this->index -= 1;
        this->UpdateReader();
        }

      // Forward events
      vtkInteractorStyleTrackballCamera::OnKeyPress();
    }

  void UpdateReader()
    {
    std::cout << "Frame: " << this->index << std::endl;
    const auto fn = this->directory + std::to_string(this->index) + ".nii.gz";
    std::cout << fn << std::endl;
    this->reader->SetFileName( fn.c_str() );
    this->reader->Update();
    const auto actors = this->GetCurrentRenderer()->GetActors();
    actors->InitTraversal();
    for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i)
      {
      actors->GetNextActor()->Modified();
      }
    this->GetCurrentRenderer()->Render();
    }
  unsigned int index = 0;
  std::string directory;
  vtkSmartPointer<vtkNIFTIImageReader> reader;
};
vtkStandardNewMacro(KeyPressInteractorStyle);

int
main( int argc, char ** argv )
{

  std::string dn = argv[1];

  const auto renderer = vtkSmartPointer<vtkRenderer>::New();

  unsigned int frameid = 0;

  const auto reader = vtkSmartPointer<vtkNIFTIImageReader>::New();
  reader->SetFileName( (dn + std::to_string(frameid) + ".nii.gz").c_str() );

  const auto cubes = vtkSmartPointer<vtkDiscreteMarchingCubes>::New();
  cubes->SetInputConnection( reader->GetOutputPort() );

  cubes->SetValue( 0, 1 );

  const auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection( cubes->GetOutputPort() );
  mapper->ScalarVisibilityOff();
  const auto actor = vtkSmartPointer<vtkActor>::New();
  actor->SetMapper( mapper );
  renderer->AddActor( actor );

  const auto window = vtkSmartPointer<vtkRenderWindow>::New();
  window->AddRenderer( renderer );
  const auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();

  const auto style = vtkSmartPointer<KeyPressInteractorStyle>::New();
  style->reader = reader;
  style->directory = dn;
  interactor->SetInteractorStyle( style );
  style->SetCurrentRenderer( renderer );

  interactor->SetRenderWindow( window );

  window->Render();

  interactor->Start();

  return EXIT_SUCCESS;

}

你实际上没有试图在当前渲染window上调用Render(),你是在当前渲染器上调用它,这是两个不同的事物。正如 vtkRenderer::Renderer() 的文档所述,

CALLED BY vtkRenderWindow ONLY.

End-user pass your way and call vtkRenderWindow::Render(). Create an image. This is a superclass method which will in turn call the DeviceRender method of Subclasses of vtkRenderer.

所以把this->GetCurrentRenderer()->Render();改成this->GetCurrentRenderer()->GetRenderWindow()->Render();