#include "nmWxPostprocessingWidget.h" #include VTK_MODULE_INIT(vtkRenderingFreeType) #include #include #include #include #include "nmWxPostprocessingWidget.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include nmWxPostprocessingWidget::nmWxPostprocessingWidget(QWidget *parent) : QWidget(parent) { this->initLayout(); } void nmWxPostprocessingWidget::initLayout() { m_mainLayout = new QVBoxLayout; this->setLayout(m_mainLayout); QFrame* frame = new QFrame; m_frameLaoyt = new QVBoxLayout; m_frameLaoyt->setMargin(0); frame->setLayout(m_frameLaoyt); m_mainLayout->addWidget(frame); m_mainLayout->setMargin(0); qDebug() << "in initLayout"; } void nmWxPostprocessingWidget::loadVTKFile(const QString &filePath) { QVTKWidget* vtkWidget = new QVTKWidget; // 创建新的渲染器 vtkSmartPointer newRenderer = vtkSmartPointer::New(); // 设置通用的 VTK 数据集读取器 vtkSmartPointer reader = vtkSmartPointer::New(); reader->SetFileName(filePath.toStdString().c_str()); reader->Update(); // 更新读取器,确保读取文件数据 // 获取读取到的数据集 vtkDataSet* dataSet = reader->GetOutput(); // 处理 UnstructuredGrid 类型的数据 vtkUnstructuredGrid* unstructuredGrid = vtkUnstructuredGrid::SafeDownCast(dataSet); if (!unstructuredGrid) { qDebug() << "Failed to load unstructured grid from file:" << filePath; // 添加失败信息 return; // 如果加载失败,直接返回 } double scalarRange[2]; unstructuredGrid->GetPointData()->GetScalars()->GetRange(scalarRange); // 计算标量范围 qDebug() << "Loading file:" << filePath << " Scalar Range:" << scalarRange[0] << " " << scalarRange[1]; vtkSmartPointer unstructuredMapper = vtkSmartPointer::New(); unstructuredMapper->SetInputData(unstructuredGrid); unstructuredMapper->SetScalarRange(scalarRange); unstructuredMapper->ScalarVisibilityOn(); // 创建演员(actor) vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(unstructuredMapper); actor->GetProperty()->SetEdgeVisibility(true); actor->GetProperty()->SetLineWidth(1.0); // 设置边缘宽度 // 添加新演员到新的渲染器 newRenderer->AddActor(actor); newRenderer->SetBackground(0.1, 0.2, 0.3); // 设置背景颜色 // 创建颜色条 vtkSmartPointer scalarBar = vtkSmartPointer::New(); vtkSmartPointer lut = vtkSmartPointer::New(); lut->SetNumberOfColors(256); lut->SetRange(scalarRange); // 设置查找表的范围为标量范围 lut->Build(); scalarBar->SetLookupTable(lut); scalarBar->SetTitle("data"); scalarBar->SetPosition(0.05, 0.1); // 设置位置(右侧) scalarBar->SetWidth(0.1); // 设置宽度 scalarBar->SetHeight(0.8); // 设置高度 newRenderer->AddActor(scalarBar); // 创建文本演员以显示文件名 vtkSmartPointer textActor = vtkSmartPointer::New(); textActor->SetInput(QString("File: %1").arg(filePath).toStdString().c_str()); // 设置文本内容 textActor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay(); textActor->SetPosition(0.1, 0.95); // 设置位置为右上角(稍微向内偏移) textActor->GetTextProperty()->SetFontSize(24); // 设置字体大小 textActor->GetTextProperty()->SetColor(1.0, 1.0, 1.0); // 设置字体颜色为白色 textActor->GetTextProperty()->SetFontFamilyToArial(); // 设置字体 textActor->GetTextProperty()->SetItalic(1); // 斜体 textActor->GetTextProperty()->SetBold(1); // 加粗 newRenderer->AddActor(textActor); // 将新的渲染器添加到渲染窗口 vtkWidget->GetRenderWindow()->AddRenderer(newRenderer); vtkWidget->GetRenderWindow()->Render(); // 渲染窗口 // 添加到frame上 m_frameLaoyt->addWidget(vtkWidget); }