vtk 6.x,Qt:3D(线,面,散点)绘图

时间:2014-01-27 16:34:03

标签: c++ qt vtk

我正在研究Qt(4.7.4)项目,需要在2D和3D坐标系中绘制数据。我一直在研究vtk 6.1,因为它看起来非常强大,我还需要在稍后的时间点可视化图像数据。我基本上得到了二维图,但我仍然无法用3D绘制数据。

这是我尝试的内容:我正在使用以下一段代码,我从vtk的一个测试(Charts / Core / Testing / Cxx / TestSurfacePlot.cxx)中获取。我添加的唯一内容是我在GUI中使用的QVTKWidget及其交互器:

QVTKWidget vtkWidget;
vtkNew<vtkChartXYZ> chart;
vtkNew<vtkPlotSurface> plot;
vtkNew<vtkContextView> view;
view->GetRenderWindow()->SetSize(400, 300);
vtkWidget.SetRenderWindow(view->GetRenderWindow());

view->GetScene()->AddItem(chart.GetPointer());

chart->SetGeometry(vtkRectf(75.0, 20.0, 250, 260));

// Create a surface
vtkNew<vtkTable> table;
float numPoints = 70;
float inc = 9.424778 / (numPoints - 1);
for (float i = 0; i < numPoints; ++i)
{
    vtkNew<vtkFloatArray> arr;
    table->AddColumn(arr.GetPointer());
}
table->SetNumberOfRows(numPoints);
for (float i = 0; i < numPoints; ++i)
{
    float x = i * inc;
    for (float j = 0; j < numPoints; ++j)
    {
        float y  = j * inc;
        table->SetValue(i, j, sin(sqrt(x*x + y*y)));
    }
}

// Set up the surface plot we wish to visualize and add it to the chart.
plot->SetXRange(0, 9.424778);
plot->SetYRange(0, 9.424778);
plot->SetInputData(table.GetPointer());
chart->AddPlot(plot.GetPointer());

view->GetRenderWindow()->SetMultiSamples(0);
view->SetInteractor(vtkWidget.GetInteractor());
view->GetInteractor()->Initialize();
view->GetRenderWindow()->Render();

现在,这会产生一个情节,但我既不能与它互动,也不会看起来像3D。我想做一些基本的东西,如缩放,平移或旋转一个枢轴。我想到的一些问题是:

  • 将QVTKWidget交互器分配给底部第三行的视图是否正确?
  • 在测试中,vtkChartXYZ被添加到vtkContextView。根据文档,vtkContextView用于显示2D场景,但此处与3D图表(XYZ)一起使用。这是如何组合在一起的?

2 个答案:

答案 0 :(得分:1)

以下代码对我有用。无需显式分配交互器,因为QVTKWidget已经处理了这个问题。

QVTKWidget vtkWidget;
vtkSmartPointer<vtkContextView> view  = vtkSmartPointer<vtkContextView>::New();
vtkSmartPointer<vtkChartXYZ>    chart = vtkSmartPointer<vtkChartXYZ>::New();

// Create a surface
vtkSmartPointer<vtkTable> table = vtkSmartPointer<vtkTable>::New();
float numPoints = 70;
float inc = 9.424778 / (numPoints - 1);
for (float i = 0; i < numPoints; ++i)
{
   vtkSmartPointer<vtkFloatArray> arr = vtkSmartPointer<vtkFloatArray>::New();
   table->AddColumn(arr.GetPointer());
}
table->SetNumberOfRows(numPoints);
for (float i = 0; i < numPoints; ++i)
{
    float x = i * inc;
    for (float j = 0; j < numPoints; ++j)
    {
        float y  = j * inc;
        table->SetValue(i, j, sin(sqrt(x*x + y*y)));
    }
}

view->SetRenderWindow(vtkWidget.GetRenderWindow());
chart->SetGeometry(vtkRectf(200.0, 200.0, 300, 300));
view->GetScene()->AddItem(chart.GetPointer());

vtkSmartPointer<vtkPlotSurface> plot = vtkSmartPointer<vtkPlotSurface>::New();

// Set up the surface plot we wish to visualize and add it to the chart.
plot->SetXRange(0, 10.0);
plot->SetYRange(0, 10.0);
plot->SetInputData(table.GetPointer());
chart->AddPlot(plot.GetPointer());

view->GetRenderWindow()->SetMultiSamples(0);
view->GetRenderWindow()->Render();

答案 1 :(得分:0)

您可能需要阅读vtkRenderViewBase

中的详细说明
QVTKWidget *widget = new QVTKWidget;
vtkContextView *view = vtkContextView::New();
view->SetInteractor(widget->GetInteractor());
widget->SetRenderWindow(view->GetRenderWindow());