vtk中的vtkOpenFOAMReader可以读取openfoam的计算计算结果,需要设置的参数主要是计算结果路径和需要读取时刻。
标量条采用hsv配色,从蓝到红。
openfoam算例采用的是open foam所给的后台阶算例。
以下是读取open foam计算的结果的源程序代码如下:
在这里插入代码片
```#include "stdafx.h"
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkInteractionStyle)
#include <vtkOpenFOAMReader.h>
#include <vtkSmartPointer.h>
#include <vtkAppendPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkUnstructuredGrid.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkPointData.h>
#include <vtkDataSetMapper.h>
#include <vtkScalarBarActor.h>
#include <vtkLookupTable.h>
#include <vtkTextProperty.h>
int main()
{
vtkSmartPointer<vtkOpenFOAMReader> openFOAMReader = vtkSmartPointer<vtkOpenFOAMReader>::New();
openFOAMReader->SetFileName("D://openfoam//pitzDaily//case.foam");//设置读取文件路径
openFOAMReader->SetCreateCellToPoint(1);
openFOAMReader->SetSkipZeroTime(1);//开启跳过0时刻
openFOAMReader->SetTimeValue(298.0);//设置需要读取的时刻
openFOAMReader->Update();
//openFOAMReader->Update();
vtkUnstructuredGrid *block0 = vtkUnstructuredGrid::SafeDownCast(openFOAMReader->GetOutput()->GetBlock(0));
block0->GetPointData()->SetActiveScalars("p");//设置读取参数为有压力
//获取压力变化范围
double scalarRange[2];
scalarRange[0] = block0->GetPointData()->GetScalars()->GetRange()[0];
scalarRange[1] = block0->GetPointData()->GetScalars()->GetRange()[1];
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(block0);
mapper->SetScalarRange(scalarRange);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
//标量条设置
vtkSmartPointer<vtkScalarBarActor> scalarBar =
vtkSmartPointer<vtkScalarBarActor>::New();
vtkSmartPointer<vtkLookupTable> pColorTable =
vtkSmartPointer<vtkLookupTable>::New();
pColorTable->SetNumberOfTableValues(31);
pColorTable->SetHueRange(0.67, 0);//标量条颜色范围,从蓝到红
pColorTable->SetAlphaRange(1.0, 1.0);
pColorTable->SetValueRange(1, 1);
pColorTable->SetSaturationRange(1, 1);
pColorTable->SetRange(scalarRange);
pColorTable->Build();
mapper->SetLookupTable(pColorTable);
scalarBar->SetTitle("p (Pa)");
scalarBar->GetTitleTextProperty()->SetColor(0, 0, 0);
scalarBar->GetTitleTextProperty()->SetFontFamilyToArial();
scalarBar->GetTitleTextProperty()->SetFontSize(20);
scalarBar->GetLabelTextProperty()->SetColor(0, 0, 0);
scalarBar->SetLabelFormat("%5.3f");
scalarBar->GetLabelTextProperty()->SetFontFamilyToArial();
scalarBar->GetLabelTextProperty()->SetFontSize(20);
scalarBar->SetNumberOfLabels(7);
scalarBar->SetUnconstrainedFontSize(1);
scalarBar->SetLookupTable(pColorTable);
vtkSmartPointer<vtkRenderer> ren =
vtkSmartPointer<vtkRenderer>::New();
ren->SetBackground(1.0, 1.0, 1.0);
ren->AddActor(actor);
ren->AddActor(scalarBar);
ren->ResetCamera();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
vtkSmartPointer<vtkInteractorStyleTrackballCamera> TrackballCamera =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
iren->SetInteractorStyle(TrackballCamera);
renWin->AddRenderer(ren.GetPointer());
renWin->SetSize(500, 500);
renWin->Render();
iren->Initialize();
iren->Start();
}
代码运行之后得到云图如下: