首页 > 其他分享 >Qt VTK加载openfoam计算结果

Qt VTK加载openfoam计算结果

时间:2024-11-28 20:10:37浏览次数:10  
标签:mapper Qt VTK openfoam vtkSmartPointer New include openFOAMReader scalarBar

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

 

 

 

 

 

 

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

标签:mapper,Qt,VTK,openfoam,vtkSmartPointer,New,include,openFOAMReader,scalarBar
From: https://www.cnblogs.com/herd/p/18575084

相关文章

  • 【Qt】QDateTimeEdit控件实现清空(不保留默认时间/最小时间)
    一、QDateTimeEdit控件 QDateTimeEdit提供了一个用于编辑日期和时间的控件。用户可以通过键盘或使用上下箭头键来增加或减少日期和时间值。日期和时间的显示格式根据设置的格式显示,可以通过setDisplayFormat()方法来设置。二、如何清空我在使用的时候,发现这个控件不管......
  • Qt从入门到入土(一) QtDesigner完成自定义水波纹和切换开关设计
    这篇文章中,我从0开始讲起,对于新手十分友好,简单完成自定义水波纹和切换开关控件的设计,并将这两个控件结合起来实现交互。项目创建生成一个继承自QWidget的类,并选择Generateform生成对应的ui文件。生成完成后,双击.ui文件打开设计师界面。设计师面板介绍组件面板:窗口左侧......
  • 2-STM32F103+ESP8266+EC800K(移远4G Cat1)--整体运行测试-Android和微信扫码绑定EC800
    <p><iframename="ifd"src="https://mnifdv.cn/resource/cnblogs/ZLIOTB/EC800K/my.html"frameborder="0"scrolling="auto"width="100%"height="1500"></iframe></p> 说明(这节使用最新......
  • Qt CDB 调试器 版本 10.0.18362 - 针对Win10 1909
    https://blog.csdn.net/gitblog_09702/article/details/143015214 QtCDB调试器版本10.0.18362-针对Win101909【下载地址】QtCDB调试器版本10.0.18362-针对Win101909QtCDB调试器版本10.0.18362-针对Win101909项目地址:https://gitcode.com/open-source-toolkit......
  • 【QT】使用Qxlsx读取Excel单元格中函数表达式的结果值
    【QT】使用Qxlsx读取Excel单元格中函数表达式的结果值零、起因是这样的,目前朋友托我写一款模板生成软件,任务是先把他写的程序文件复制一份出来,然后再根据Excel中对应位置的单元格的值,修改程序文件副本中的某些文件。对于读Excel的需求,经过测试,最终选择Qxlsx这款开源QT组件来读取......
  • QT入门(1)
    1.什么是QT*QT是一个跨平台的C++图像用户界面应用程序框架*QT在1991年由奇趣科技开发*QT的优点  +跨平台,几乎支持所有平台  +接口简单,容易上手  +一定程度上简化了内存回收机制  +有很好的社区氛围  +可以进行嵌入式开发 2.QWidge......
  • opensuse系统下QtCreator中文输入法失效解决,亲测有效
    首先安装QTCreator,这里不再详细赘述。克隆fcitx-qt5仓库,然后在该目录下新建build文件克隆命令如下gitclonehttps://gitcode.com/gh_mirrors/fc/fcitx-qt5.git安装cmakezypperinstallcmake进入到build目录运行命令cmake..报错如下解决办法运行如下指令su......
  • QtCRC校验
    1crc校验子程序思路:取出待校验字节的最后两个字节,然后与计算出的crc进行比较,如果相同,则crc校验正确。说明:这种方法必须确保自己计算的是正确的,不然哪怕人家发过来的是对的,也会校验失败注意:一定要注意crc校验码的高低字节!!!使用该子程序时传入的数据格式为可以使用QByteArray::......
  • Linux下打包Qt应用程序
    linux下打包应用程序非常复杂接下来一步一步实现第一步:下载linuxdeployqt程序我已经保存在了百度网盘,记住qt5用老一些的版本第二步:下载好后重命名为linuxdeployqt好用一点然后将其移动到/usr/local/bin目录下并且授权记住一定要授权检查是否成功sudolinuxdeployq......
  • Qt中字节与浮点数互转
    1十六进制字节数转浮点数注意:使用此函数时传入的参数格式例如QStringstr1="068400040002847D";字符串中间没有空格floatMainWindow::hexBytesToFloat(constQString&hexString){QByteArraybyteArray;//创建一个QByteArray对象byteArrayfor(inti=hexStr......