我有一个在循环中调用的函数,每次通过它都会给出一个 3D 点的 (x,y,z)。我正在尝试使用 VTK 实时连接这些点,但我无法让渲染窗口在每一帧后刷新。如果我退出循环,我会看到一堆行。
基本上,这就是我正在做的事情:
// Create a vtkPoints object to store the points in it
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkPolyLine> polyLine = vtkSmartPointer<vtkPolyLine>::New();
// Create a cell array to store the lines in
vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
// Create a polydata to store everything in
vtkSmartPointer<vtkPolyData> linesPolyData = vtkSmartPointer<vtkPolyData>::New();
// Setup actor and mapper
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// Setup render window, renderer, and interactor
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderWindow->Render();
renderer->Render();
int numpoints = 0;
while(some_condition)
{
call_function(&x,&y,&z);
double point[3] = {0.0, 0.0, 0.0};
point[0] = (float)x;
point[1] = (float)y;
point[2] = (float)z;
numpoints++;
points->InsertNextPoint(point);
polyLine->GetPointIds()->SetNumberOfIds(numpoints);
for(unsigned int i = 0; i < num_points; i++)
{
polyLine->GetPointIds()->SetId(i,i);
}
lines->InsertNextCell(polyLine);
// Add the points to the dataset
linesPolyData->SetPoints(points);
// Add the lines to the dataset
linesPolyData->SetLines(lines);
#if VTK_MAJOR_VERSION <= 5
mapper->SetInput(linesPolyData);
#else
mapper->SetInputData(linesPolyData);
#endif
linesPolyData->Modified();
linesPolyData->Update();
renderWindow->Render();
renderer->Render();
renderWindowInteractor->Enable();
check_loop_exit_condition();
}
如果我renderer->Render()
在循环之外做,我会得到所有的线路(或者至少我认为我会这样做)。有人能弄清楚我哪里出错了吗?