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