我有一个读取图像并使用以下命令显示网格的管道VTK;在更改图像读取器的输入并更新管道后,网格不会更新,直到我与窗口交互。
我有一个目录,其中包含一系列分割文件(即,3D 图像体积,其中像素值对应于相应图像中的结构),这些文件显示结构如何随时间变化。我编写了一个实用程序VTK,允许我加载目录中的第一个图像,将标签可视化为网格,并通过更改输入图像的文件名和更新管道,使用箭头键向前或向后“步进” 。这几乎可以工作——唯一的问题是,更新管道后,网格体不会更新,直到我与窗口交互(例如,只需单击窗口中的任意位置即可更新网格体)。
呼吁Update()读者:
this->reader->Update();
Run Code Online (Sandbox Code Playgroud)呼吁Modified()演员们:
const auto actors = this->GetCurrentRenderer()->GetActors();
actors->InitTraversal();
for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i)
{
actors->GetNextActor()->Modified();
}
Run Code Online (Sandbox Code Playgroud)调用Render()当前渲染窗口:
this->GetCurrentRenderer()->Render();
Run Code Online (Sandbox Code Playgroud)注意:阅读器在 中更新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;
}
Run Code Online (Sandbox Code Playgroud)
您实际上没有尝试调用Render()当前渲染窗口,而是在当前渲染器上调用它,这是两个不同的事情。正如vtkRenderer::Renderer()的文档所述,
仅由 vtkRenderWindow 调用。
最终用户通过您的方式并调用 vtkRenderWindow::Render()。创建图像。这是一个超类方法,它将依次调用 vtkRenderer 子类的 DeviceRender 方法。
所以this->GetCurrentRenderer()->Render();改成this->GetCurrentRenderer()->GetRenderWindow()->Render();