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

How to plot streamline with VTK and C++?

我想用 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. 您的 streamer 的起始位置在您的 grid 的域之外。
  2. 您可能需要致电

    streamer->Update()
    

    之前

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

    类似地

    streamTube->Update()
    

    之前

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

    请注意(至少在 VTK 6.3 中)streamer->GetOutput()streamTube->GetOutput() 都是 return 一个 vtkPolyData,因此您不需要转换它。

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

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

    vtkSmartPointer<vtkOutlineFilter> edges = vtkSmartPointer<vtkOutlineFilter>::New();
    edges->SetInputData(grid);
    ppMap->SetInputConnection(edges->GetOutputPort());
    
  5. 如果您以后不删除对象,您可能希望在任何地方都使用 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