Qt VTK加载openfoam计算结果

news/2024/11/28 20:10:12/文章来源:https://www.cnblogs.com/herd/p/18575084

Qt VTK加载openfoam计算结果.foam文件。

 

#include <QApplication>
#include <QDebug>
#include "qvtkopenglwidget.h"
#include <vtkSmartPointer.h>
#include <vtkGenericDataObjectReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkCellData.h>#include <vtkVersion.h>
#include <vtkPlaneSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkAutoInit.h>VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);#include <vtkUnstructuredGridWriter.h>
#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>
#include <vtkGenericOpenGLRenderWindow.h>#include <QString>int main(int argc, char *argv[]) {QApplication app(argc, argv);// 创建QVTKOpenGLWidget来显示VTK渲染的结果QVTKOpenGLWidget *widget = new QVTKOpenGLWidget();widget->setMinimumSize(400, 400);widget->setMaximumSize(16777215, 16777215);widget->show();//// 创建VTK的读取器来读取.foam文件//vtkSmartPointer<vtkGenericDataObjectReader> reader =//    vtkSmartPointer<vtkGenericDataObjectReader>::New();//reader->SetFileName("J://OpenFOAM//loop_cyclic(1)//loop_cyclic//f.foam");//reader->Update();//// 创建映射器和actor//vtkSmartPointer<vtkPolyDataMapper> mapper =//    vtkSmartPointer<vtkPolyDataMapper>::New();//mapper->SetInputConnection(reader->GetOutputPort());//vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();//actor->SetMapper(mapper);////////////////////////////////////////////////////////////////////////////////////
    vtkSmartPointer<vtkOpenFOAMReader> openFOAMReader = vtkSmartPointer<vtkOpenFOAMReader>::New();//openFOAMReader->SetFileName("J://OpenFOAM//singlePipe_totalPowerStay(1)//singlePipe_totalPowerStay//f.foam");//设置读取文件路径openFOAMReader->SetFileName("J://OpenFOAM//loop_cyclic(1)//loop_cyclic//f.foam");//设置读取文件路径//openFOAMReader->SetFileName("J://OpenFOAM//singlePipe_totalPowerStay(1)//singlePipe_totalPowerStay//VTK//singlePipe_totalPowerStay_8.vtm");//设置读取文件路径
openFOAMReader->SetCreateCellToPoint(1);openFOAMReader->SetSkipZeroTime(1);//开启跳过0时刻//openFOAMReader->SetTimeValue(289.0);//设置需要读取的时刻openFOAMReader->Update();//openFOAMReader->Update();//vtkMultiBlockDataSet* GetOutput();vtkMultiBlockDataSet* set1=openFOAMReader->GetOutput();int count1= openFOAMReader->GetNumberOfCellArrays();qDebug() << "" << count1;for (int k1 = 0; k1 < count1;k1++) {const char* indexName = openFOAMReader->GetCellArrayName(k1);//vtkDataObject* indexDataObj = set1->GetBlock(k1);//const char* indexName3 = indexDataObj->GetClassName();
QString  indexNameStr(indexName);qDebug() << k1 << "         " << indexNameStr;}qDebug() << "==================";int countPointArrays = openFOAMReader->GetNumberOfPointArrays();qDebug() << "" << countPointArrays;//openFOAMReader->GetReadZones();// 获取 vtkMultiBlockDataSet,这通常包含了网格和其他数据vtkMultiBlockDataSet* mbds = openFOAMReader->GetOutput();if (mbds) {// 遍历所有的块(blocks)unsigned int numBlocks = mbds->GetNumberOfBlocks();for (unsigned int i = 0; i < numBlocks; i++){vtkDataObject* dataObject = mbds->GetBlock(i);const char* indexClassName = dataObject->GetClassName();qDebug() << "--" << QString(indexClassName);// 动态类型检查,确保我们可以处理这个数据对象vtkDataSet* dataSet = vtkDataSet::SafeDownCast(dataObject);if (dataSet) {qDebug() << "Block " << i << " is a vtkDataSet of type: " << dataSet->GetClassName() ;// 检查是否有单元区域数据(这里假设单元区域数据以某种方式标识)vtkCellData* cellData = dataSet->GetCellData();if (cellData) {const char* indexCellName = cellData->GetClassName();qDebug() << "---------------------------------------------" << QString(indexCellName);// 遍历单元数据数组(这里需要知道具体的数组名,OpenFOAMReader可能会以某种方式提供这些信息)// 示例中我们假设有一个名为 "zoneID" 的数组if (cellData->HasArray("zoneID")) {vtkDataArray* zoneArray = cellData->GetArray("zoneID");// 遍历数组中的元组for (vtkIdType j = 0; j < zoneArray->GetNumberOfTuples(); j++) {int zoneID = zoneArray->GetTuple1(j);qDebug() << "Cell " << j << " in block " << i << " is in zoneID: " << zoneID ;}}else {std::cout << "No 'zoneID' array found in cell data of block " << i << std::endl;}}else {std::cout << "No cell data found in block " << i << std::endl;}}else {std::cout << "Block " << i << " is not a vtkDataSet." << std::endl;}}////////////////// Iterate over all blocks in the multi-block data set// 遍历所有的块////////////////////////
    }qDebug() << "==================";int count3 = set1->GetNumberOfBlocks();int count2 = set1->GetNumberOfCells();qDebug() << "" << count2 << "     " << count3;vtkUnstructuredGrid *block0 = vtkUnstructuredGrid::SafeDownCast(openFOAMReader->GetOutput()->GetBlock(0));block0->GetPointData()->SetActiveScalars("p");//设置读取参数为有压力int count4 = block0->GetNumberOfCells();const char * cellName = block0->GetClassName();qDebug() << "+=" << count4<<"       "<<QString(cellName);vtkSmartPointer<vtkUnstructuredGridWriter> writerObj= vtkSmartPointer<vtkUnstructuredGridWriter>::New();writerObj->SetInputData(block0);writerObj->SetFileName("test.vtk");writerObj->Update();//获取压力变化范围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<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);//
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();actor->SetMapper(mapper);/*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();
*////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //vtkSmartPointer<vtkPlaneSource> planeSource =vtkSmartPointer<vtkPlaneSource>::New();//planeSource->SetCenter(1.0, 0.0, 0.0);//planeSource->SetNormal(1.0, 0.0, 1.0);//planeSource->Update();//vtkPolyData* plane = planeSource->GetOutput();//// Create a mapper and actor//vtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();////#if VTK_MAJOR_VERSION <= 5
    ////    mapper->SetInput(plane);////#else//mapper->SetInputData(plane);//vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();//actor->SetMapper(mapper);///////////////////////////
// 创建渲染器和设置渲染场景vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();renderer->AddActor(actor);renderer->SetBackground(0.0, 0.0, 0.0);//renderer->SetBackground(1.0, 1.0, 1.0);//renderer->AddActor(actor);//renderer->AddActor(scalarBar);renderer->ResetCamera();vtkSmartPointer<vtkGenericOpenGLRenderWindow> nWin = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();nWin->AddRenderer(renderer);// 设置QVTKOpenGLWidget的渲染器vtkRenderWindow* renderWindowObj = renderer->GetRenderWindow();if (renderWindowObj==nullptr) {std::cout << "null" << std::endl;}else {}widget->SetRenderWindow(renderer->GetRenderWindow());return app.exec();
}
View Code

 

 

 

 

 

 

###################################

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/843038.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

蓝桥3511飞机降落

样例输入 2 3 0 100 10 10 10 10 0 2 20 3 0 10 20 10 10 20 20 10 20 样例输出 YES NO思路: 具体来说,对于每架飞机,有起飞时间(t)、降落时间限制(d)和飞行时长(l)等信息,代码要判断能否按照一定规则安排这些飞机的起降顺序,使得所有飞机都能在其降落时间限制内完成…

多校A层冲刺NOIP2024模拟赛27终结篇

不知道是不是我打的最后一场模拟赛了,记录一下吧,总体来说还不错,虽然 \(T1\) 方案数求错爆零了,但 \(T3\) 场切了,暴力打满的话有265,希望 \(NOIP\) 时也可以不让自己遗憾吧。 A 【模板】分治FFT 考虑每加进来一个数的贡献 \(x_1*x_2+(x_1+x_2)*x_3+...=x_1*x_2+x_1*x_3…

elasticseach-head插件

git地址 https://github.com/mobz/elasticsearch-head 安装方式 浏览器插件 docker本地安装

支持超线程的numa架构

支持超线程的numa架构 物理硬件视角,将多个CPU封装在一起,这个封装被称为插槽Socket; Core是socket上独立的硬件单元; 通过intel的超线程HT技术进一步提升CPU的处理能力,OS看到的逻辑上的核Processor的数量。每个硬件线程都可以按逻辑cpu寻址,因此这个处理器看上去有八块…

schoolcms代码审计

sql注入 注入点:burp的数据包: POST /index.php?m=Admin&c=Article&a=Delete HTTP/1.1 Host: schoolcms Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6422.112 Safa…

Burp抓模拟器App HttpHttps数据包

Burp抓模拟器App Http/Https数据包Next抓模拟器中App Https数据包,本文模拟器环境为最新版本雷电模拟器9。首先仍需在模拟器内安装证书,Burp导出证书,导出步骤同上。雷电模拟器安装证书可能需要设置PIN码,依据提示设置安装即可。模拟器设置手动代理,将流量转发至自己PC。新…

Burp抓Web端应用HttpHttps数据包

Burp抓Web端应用Http/Https数据包抓Web端Https数据包,需提前在本机安装证书。打开Burp证书安装完成后,本机或浏览器代理插件设置代理,并将流量转发至Burp,尝试抓包。Web端Http/Https数据包抓包成功!

【论文精读】Lora

【论文精读】 Lora:Low-rank adaptation of large language models论文地址:Lora:Low-rank adaptation of large language models 年份:2021 引用量:8000+ 关键词:LLM的高效微调目录【论文精读】Lora:Low-rank adaptation of large language models1. 背景2. Lora方法3. 实…

2024-0xGame-WEB方向全题解

0xGame Round1 ez_rce 源码: from flask import Flask, request import subprocessapp = Flask(__name__)@app.route("/") def index():return open(__file__).read()@app.route("/calc", methods=[POST]) def calculator():expression = request.form.ge…

【开发】计算机延迟指标全解析:深入理解系统性能瓶颈

在计算机的世界里,“速度”一直是我们不懈追求的目标。从早期的计算机到如今的高性能设备,每一次技术进步都伴随着对速度的极致渴望。无论是处理器的运算速度,还是数据的传输与存储速度,都直接影响着我们使用计算机的体验。那你是否曾好奇,计算机中的“快”究竟是如何衡量…

SKILL脚本的加密与解密及使用

SKILL脚本一般是用.il 和 .ile 结尾的文件,一般设置为 .ile 结尾的文件是加密的,调用的时候需要密码。 SKILL脚本的加密: 用encrypt函数加密脚本,格式如下: encrypt("/apps/SC/skill-script/migrateDesign/MigrateDesign.il" "/apps/SC/skill-script/migr…