如何使用VTK和C ++绘制流线图?

时间:2015-12-07 13:20:15

标签: c++ vtk

我想用VTK在C ++中可视化流线。我编写了一个代码,但它只显示了多维数据集的边缘,并且流线不可见。这是我的代码:

vtkPoints *p=vtkPoints::New();
vtkFloatArray *vectors=vtkFloatArray::New();
float vector[3];
vectors->SetNumberOfComponents(3);
int sum=0;
for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
        for(int k=0;k<4;k++)
        {
                p->InsertPoint(sum,k+5.0,j+5.0,i+5.0);
                vector[0]=0.5*k+0.1;
                vector[1]=0.2*j+0.2;
                vector[2]=0.3*+0.3;
                vectors->InsertNextTuple(vector);
                sum++;
        }

vtkCellArray *line=vtkCellArray::New();
line->InsertNextCell(2);
line->InsertCellPoint(63);
line->InsertCellPoint(15);

line->InsertNextCell(2);
line->InsertCellPoint(0);
line->InsertCellPoint(3);

line->InsertNextCell(2);
line->InsertCellPoint(0);
line->InsertCellPoint(12);

line->InsertNextCell(2);
line->InsertCellPoint(12);
line->InsertCellPoint(15);

line->InsertNextCell(2);
line->InsertCellPoint(3);
line->InsertCellPoint(15);

line->InsertNextCell(2);
line->InsertCellPoint(0);
line->InsertCellPoint(48);

line->InsertNextCell(2);
line->InsertCellPoint(48);
line->InsertCellPoint(51);

line->InsertNextCell(2);
line->InsertCellPoint(48);
line->InsertCellPoint(60);

line->InsertNextCell(2);
line->InsertCellPoint(63);
line->InsertCellPoint(60);

line->InsertNextCell(2);
line->InsertCellPoint(12);
line->InsertCellPoint(60);

line->InsertNextCell(2);
line->InsertCellPoint(63);
line->InsertCellPoint(51);

line->InsertNextCell(2);
line->InsertCellPoint(51);
line->InsertCellPoint(3);


vtkPolyData *poly=vtkPolyData::New();
poly->SetPoints(p);
poly->SetLines(line);


vtkStructuredGrid *grid=vtkStructuredGrid::New();
grid->SetPoints(p);
grid->GetPointData()->SetVectors(vectors);
vtkRungeKutta4 *integ=vtkRungeKutta4::New();
vtkStreamLine *streamer=vtkStreamLine::New();

streamer->SetInput(grid);
streamer->SetIntegrator(integ);
streamer->SetIntegrationStepLength(0.15);
streamer->SetMaximumPropagationTime(500);
streamer->SetNumberOfThreads(2);
streamer->SetStartPosition(5.0,0,0);
streamer->SetStepLength(2.5);
streamer->SetTerminalSpeed(0);
streamer->SetSpeedScalars(0);
streamer->SetVorticity(0);

vtkTubeFilter *streamTube=vtkTubeFilter::New();
streamTube->SetInput((vtkPolyData *) streamer->GetOutput());
streamTube->SetNumberOfSides(12);
streamTube->SetRadius(0.02);
streamTube->SetUseDefaultNormal(0);

vtkPolyDataMapper *pMap=vtkPolyDataMapper::New();
vtkPolyDataMapper *ppMap=vtkPolyDataMapper::New();

ppMap->SetInput(poly);

pMap->SetInput((vtkPolyData *) streamTube->GetOutput());


vtkActor *pActor=vtkActor::New();
pActor->SetMapper(pMap);

vtkActor *ppActor=vtkActor::New();
ppActor->SetMapper(ppMap);

ppActor->GetProperty()->SetColor(0.3800,0.7000,0.1600);

pActor->GetProperty()->SetColor(0.3800,0.7000,0.1600);

vtkSmartPointer<vtkRenderer>pRen=vtkRenderer::New();

pRen->AddActor(pActor);
pRen->AddActor(ppActor);
//pRen->AddActor(ppA);

vtkSmartPointer<vtkRenderWindow>pRenWin=vtkRenderWindow::New();
pRenWin->AddRenderer(pRen);
vtkRenderWindowInteractor *pIren=vtkRenderWindowInteractor::New();
pIren->SetRenderWindow(pRenWin);
pRen->SetBackground(1,1,1);
pRenWin->SetSize(300,300);
pRenWin->Render();
pRen->ResetCamera();
pIren->Initialize();
pIren->Start();
return 0;
}

我不明白哪里出错了。如果有人能提出一些建议,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下是我发现的一些事情:

  1. streamer的起始位置超出了grid
  2. 的范围
  3. 您可能需要致电

    streamer->Update()
    

    streamTube->SetInput( streamer->GetOutput() )
    

    和类似的

    streamTube->Update()
    

    pMap->SetInput( streamTube->GetOutput() )
    

    请注意(在VTK 6.3中,至少)streamer->GetOutput()streamTube->GetOutput()都会返回vtkPolyData,因此您不需要进行投射。

  4. 您可能需要设置grid的尺寸,即

    grid->SetDimensions(4, 4, 4);
    
  5. 考虑使用vtkOutlineFilter,而不是手动创建poly并向其添加lines

    vtkSmartPointer<vtkOutlineFilter> edges = vtkSmartPointer<vtkOutlineFilter>::New();
    edges->SetInputData(grid);
    ppMap->SetInputConnection(edges->GetOutputPort());
    
  6. 如果您以后没有删除对象,可能需要在任何地方使用vtkSmartPointers,例如:

    vtkSmartPointer<vtkPoints> p = vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkFloatArray> vectors = vtkSmartPointer<vtkFloatArray>::New();
    vtkSmartPointer<vtkRungeKutta4> integ = vtkSmartPointer<vtkRungeKutta4>::New();
    vtkSmartPointer<vtkStreamLine> streamer = vtkSmartPointer<vtkStreamLine>::New();
    

    有关详情,请参阅http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers