使用vtk读取openfoam计算结果

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();

}

代码运行之后得到云图如下:
在这里插入图片描述

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值