VTK:视图在用户交互后才会更新
VTK: View doesn't update until after user interaction
TL;博士
我有一个管道,它读取图像并使用VTK
显示网格;在更改图像阅读器的输入并更新管道时,网格不会更新,直到我与窗互。
长版本
我有一个目录,其中包含一系列分割文件(即像素值对应于相应图像中的结构的 3D 图像体积),这些文件显示了结构如何随时间变化。 我在VTK
中编写了一个实用程序,它允许我加载目录中的第一个图像,将标签可视化为网格,并通过更改输入图像的文件名和更新管道来使用箭头键向前或向后"前进"。 这几乎有效 - 唯一的问题是,在更新管道后,网格不会更新,直到我与窗互(例如,只需单击窗口中的任意位置即可更新网格)。
我尝试过什么:
-
在读卡器上调用
Update()
:this->reader->Update();
-
呼吁
Modified()
演员:const auto actors = this->GetCurrentRenderer()->GetActors(); actors->InitTraversal(); for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i) { actors->GetNextActor()->Modified(); }
-
在当前渲染窗口上调用
Render()
:this->GetCurrentRenderer()->Render();
MCVE:
注意:读卡器在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;
}
您实际上并没有尝试在当前渲染窗口上调用Render()
,而是在当前渲染器上调用它,这是两回事。正如 vtkRenderer::Renderer() 的文档所述,
仅由 vtkRenderWindow 调用。
最终用户通过您的方式并调用 vtkRenderWindow::Render()。创建一个 图像。这是一个超类方法,它将依次调用 vtkRenderer 子类的 DeviceRender 方法。
因此,将this->GetCurrentRenderer()->Render();
更改为this->GetCurrentRenderer()->GetRenderWindow()->Render();
相关文章:
- 从C++本机插件更新Vector3数组
- Qt VTK交互风格的信号到小部件
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- cmake更新缓存的变量
- 更新到莫哈韦后出现cmath错误
- Qt Quick-如何仅从c++代码与qml属性交互
- OpenMP:并行更新数组总是需要减少数组吗
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 指针没有更新它在void函数内部指向的值
- 如何在c++中获取要更新的值
- 已修改的LinkedList未在文本文件本身中更新
- Qt:当QListView获得新条目时,如何更新QStringList
- 更新的矢量元素不打印
- 如何使用按钮更新GTK3图像以使用C++从相机捕获图片
- 如何在ECS框架中更新组件数据和通知系统
- 从VS 2015更新3更新到VS2015更新3 d后浮点计算行为不同的原因
- 如果我想在没有更新编译器的情况下使用新功能,该怎么办?
- 复制和交换习惯用法与移动操作之间的交互
- VTK:视图在用户交互后才会更新
- VTK -更新vtkPolyData在renderWindow没有阻塞交互