为什么模型没有显示在屏幕中央?

Why the model is not displayed in the center of screen?

我想用 VTK 显示点云。我从QVTKOpenGLNativeWidget继承了一个名为VTKWidget的class,它的构造函数是:

VTKWidget::VTKWidget(QVTKOpenGLNativeWidget* parent)
    :QVTKOpenGLNativeWidget(parent)
{
    QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
    _renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
    _render = vtkSmartPointer<vtkRenderer>::New();
    _actor = vtkSmartPointer<vtkActor>::New();
    _points = vtkSmartPointer<vtkPoints>::New();
    _mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    _polyData = vtkSmartPointer<vtkPolyData>::New();    
    _verts = vtkSmartPointer<vtkCellArray>::New();

    _polyData->SetPoints(_points);
    _polyData->SetVerts(_verts);

    _mapper->SetInputData(_polyData);

    _actor->SetMapper(_mapper);
    _actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
    _actor->GetProperty()->SetPointSize(2.0f);

    _render->AddActor(_actor);
    double color[] = {0.5, 0.5, 0.5};
    _render->SetBackground(color);

    _renderWindow->AddRenderer(_render);
    //setRenderWindow(_renderWindow);
}

点将在updateData函数中修改,updateData函数是:

void VTKWidget::updateData(const QVector<QVector<QVector3D>>& data)
{
    setRenderWindow(_renderWindow);
    _points->Reset();
    long long w = data.size();
    long long h = data[0].size();
    _points->SetNumberOfPoints(w * h);
    _polyData->Reset();
    _verts->Reset();
    for (int i = 0; i < data.size(); ++i)
    {
        for (int j = 0; j < data[0].size(); ++j)
        {
            vtkIdType pid;
            pid = _points->InsertNextPoint(data[i][j].x(), data[i][j].y(), data[i][j].z());
            _verts->InsertNextCell(1, &pid);
        }
    }
    _polyData->Modified();
    _mapper->Update();
}

问题是,如果代码setRenderWindow (_renderWindow);在构造函数中,点云不会显示在这个小部件的中心,但如果代码在函数 updateData.

如果你想在updateData的末尾看到整个点云,你应该使用_renderer->ResetCamera()。 (https://vtk.org/doc/nightly/html/classvtkRenderer.html#ae8055043e676defbbacff6f1ea65ad1e)

setRenderWindow() 的位置影响渲染的事实是一个副作用。