You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWATI/Src/nmNum/nmSubWxs/nmWxPostprocessingWidget.cpp

113 lines
4.6 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "nmWxPostprocessingWidget.h"
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingFreeType)
#include <vtkPointData.h>
#include <vtkDataSetReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkUnstructuredGridReader.h>
#include "nmWxPostprocessingWidget.h"
#include <QVTKWidget.h>
#include <vtkSmartPointer.h>
#include <vtkDataSetReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDebug>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkScalarBarActor.h>
#include <vtkSmartPointer.h>
#include <vtkScalarBarActor.h>
#include <vtkLookupTable.h>
#include <vtkTextProperty.h>
#include <QFrame>
#include <vtkTextActor.h>
#include <QLabel>
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<vtkRenderer> newRenderer = vtkSmartPointer<vtkRenderer>::New();
// 设置通用的 VTK 数据集读取器
vtkSmartPointer<vtkDataSetReader> reader = vtkSmartPointer<vtkDataSetReader>::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<vtkDataSetMapper> unstructuredMapper = vtkSmartPointer<vtkDataSetMapper>::New();
unstructuredMapper->SetInputData(unstructuredGrid);
unstructuredMapper->SetScalarRange(scalarRange);
unstructuredMapper->ScalarVisibilityOn();
// 创建演员actor
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::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<vtkScalarBarActor> scalarBar = vtkSmartPointer<vtkScalarBarActor>::New();
vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::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<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::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);
}