文章目录
Qt&Vtk-Cube
今天搬运的代码前面已经搞过了,为了整体性,在做一次搬运,官方示例演示如下。开搞
1 代码搬运
1.1 cube.h
#ifndef CUBE_H #define CUBE_H #include <QWidget> #include "QVTKOpenGLWidget.h" //新版本,旧版QVTKWidget #include "vtkAutoInit.h" #include "vtkActor.h" #include "vtkCamera.h" #include "vtkCellArray.h" #include "vtkFloatArray.h" #include "vtkNamedColors.h" #include "vtkNew.h" #include "vtkPointData.h" #include "vtkPolyData.h" #include "vtkPolyDataMapper.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "array" namespace Ui { class Cube; } class Cube : public QWidget { Q_OBJECT public: explicit Cube(QWidget *parent = 0); ~Cube(); private: Ui::Cube *ui; std::array<std::array<double,3>,8> pts; std::array<std::array<vtkIdType,4>,6> ordering; vtkPolyData *cube = nullptr; vtkPoints *points = nullptr; vtkCellArray *polys = nullptr; vtkFloatArray *scalars = nullptr; vtkNamedColors *colors = nullptr; vtkPolyDataMapper *mapper = nullptr; vtkActor *actor = nullptr; vtkRenderer *render = nullptr; }; #endif // CUBE_H
1.2 cube.cpp
#include "cube.h" #include "ui_cube.h" Cube::Cube(QWidget *parent) : QWidget(parent), ui(new Ui::Cube) { ui->setupUi(this); pts = {{{{0, 0, 0}}, {{1, 0, 0}}, {{1, 1, 0}}, {{0, 1, 0}}, {{0, 0, 1}}, {{1, 0, 1}}, {{1, 1, 1}}, {{0, 1, 1}}}}; ordering = {{{{0, 1, 2, 3}}, {{4, 5, 6, 7}}, {{0, 1, 5, 4}}, {{1, 2, 6, 5}}, {{2, 3, 7, 6}}, {{3, 0, 4, 7}}}}; cube = vtkPolyData::New(); points = vtkPoints::New(); polys = vtkCellArray::New(); scalars = vtkFloatArray::New(); for(auto i =0;i<pts.size();++i) { points->InsertPoint(i,pts[i].data()); scalars->InsertTuple1(i,i); } for(auto && i : ordering) polys->InsertNextCell(vtkIdType(i.size()),i.data()); cube->SetPoints(points); cube->SetPolys(polys); cube->GetPointData()->SetScalars(scalars); mapper = vtkPolyDataMapper::New(); mapper->SetInputData(cube); mapper->SetScalarRange(cube->GetScalarRange()); actor = vtkActor::New(); actor->SetMapper(mapper); render = vtkRenderer::New(); render->AddActor(actor); colors = vtkNamedColors::New(); render->SetBackground(colors->GetColor3d("black").GetData()); ui->widget->GetRenderWindow()->AddRenderer(render); } Cube::~Cube() { delete ui; }