#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <vector>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#include <vtkSmartPointer.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
std::vector<vtkSmartPointer<vtkRenderer>> pRenderers;
std::vector<vtkSmartPointer<vtkImageActor>> pImageActors;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <vtkImageGaussianSource.h>
#include <vtkImageCast.h>
#include <vtkImageNoiseSource.h>
#include <vtkImageMandelbrotSource.h>
#include <vtkImageGridSource.h>
#include <vtkImageData.h>
#include <vtkImageChangeInformation.h>
// create the source data
static vtkImageData *CreateData(const std::string& sourceName, const int scalarType, const int size[3])
{
vtkImageData *output = nullptr;
if (sourceName == "gaussian")
{
vtkSmartPointer<vtkImageGaussianSource> source = vtkSmartPointer<vtkImageGaussianSource>::New();
source->SetWholeExtent(0, size[0]-1, 0, size[1]-1, 0, size[2]-1);
source->SetCenter(0.5*(size[0]-1), 0.5*(size[1]-1), 0.5*(size[2]-1)); // 图像中心
int maxdim = (size[0] > size[1] ? size[0] : size[1]);
maxdim = (maxdim > size[2] ? maxdim : size[2]);
source->SetStandardDeviation(0.25*(maxdim-1)); // 设置标准差
source->SetMaximum(255.0);
vtkSmartPointer<vtkImageCast> cast = vtkSmartPointer<vtkImageCast>::New();
cast->SetInputConnection(source->GetOutputPort());
cast->SetOutputScalarType(scalarType);
cast->Update();
output = cast->GetOutput();
output->Register(nullptr);
}
else if (sourceName == "noise")
{
vtkSmartPointer<vtkImageNoiseSource> source = vtkSmartPointer<vtkImageNoiseSource>::New();
source->SetMinimum(0.0);
source->SetMinimum(255.0);
source->SetWholeExtent(0, size[0]-1, 0, size[1]-1, 0, size[2]-1);
vtkSmartPointer<vtkImageCast> cast = vtkSmartPointer<vtkImageCast>::New();
cast->SetInputConnection(source->GetOutputPort());
cast->SetOutputScalarType(scalarType);
cast->Update();
output = cast->GetOutput();
output->Register(nullptr);
}
else if (sourceName == "mandelbrot")
{
vtkSmartPointer<vtkImageMandelbrotSource> source = vtkSmartPointer<vtkImageMandelbrotSource>::New();
source->SetWholeExtent(0, size[0]-1, 0, size[1]-1, 0, size[2]-1);
vtkSmartPointer<vtkImageCast> cast = vtkSmartPointer<vtkImageCast>::New();
cast->SetInputConnection(source->GetOutputPort());
cast->SetOutputScalarType(scalarType);
cast->Update();
output = cast->GetOutput();
output->Register(nullptr);
}
else if (sourceName == "grid")
{
vtkSmartPointer<vtkImageGridSource> source = vtkSmartPointer<vtkImageGridSource>::New();
source->SetDataExtent(0, size[0]-1, 0, size[1]-1, 0, size[2]-1);
source->SetLineValue(255.0);
source->SetFillValue(0.0);
source->SetDataScalarType(scalarType);
source->Update();
output = source->GetOutput();
output->Register(nullptr);
}
else
{
cerr << "unrecognized option for --source\n";
return nullptr;
}
// standardize the geometry of the output
vtkSmartPointer<vtkImageChangeInformation> change = vtkSmartPointer<vtkImageChangeInformation>::New();
change->SetInputData(output);
change->SetOutputSpacing(1.0, 1.0, 1.0);
change->CenterImageOn();
change->Update();
output->Delete();
output = change->GetOutput();
output->Register(nullptr);
return output;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
const std::vector<std::string> sourceTypes = {"gaussian",
"noise",
"mandelbrot",
"grid"};
// 初始化render
pImageActors.resize(sourceTypes.size());
pRenderers.resize(sourceTypes.size());
int imageSize[] = {256, 256};
float viewOffset = 1.0 / sourceTypes.size();
for(size_t i = 0; i < sourceTypes.size(); ++i)
{
pImageActors[i] = vtkSmartPointer<vtkImageActor>::New();
vtkImageData* pImageData = CreateData(sourceTypes[i], VTK_UNSIGNED_CHAR, imageSize);
if(pImageData == nullptr)
{
continue;
}
pImageActors[i]->SetInputData(pImageData);
pRenderers[i] = vtkSmartPointer<vtkRenderer>::New();
pRenderers[i]->SetViewport(i * viewOffset, 0, (i + 1) * viewOffset, 1.0);
pRenderers[i]->AddActor(pImageActors[i]);
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRenderers[i]);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QVTKWidget" name="qvtkWidget"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QVTKWidget</class>
<extends>QWidget</extends>
<header>QVTKWidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>