Qt&Vtk-013-Cube

简介: Qt&Vtk-013-Cube

image.png

文章目录


Qt&Vtk-Cube


今天搬运的代码前面已经搞过了,为了整体性,在做一次搬运,官方示例演示如下。开搞image.png

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;
}

image.pngimage.png

目录
相关文章
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
156 0
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
Qt&Vtk-012-CreateTree
Qt&Vtk-012-CreateTree
155 0
Qt&Vtk-012-CreateTree
Qt&Vtk-011-Cone6
Qt&Vtk-011-Cone6
153 0
Qt&Vtk-011-Cone6
|
C++
Qt&Vtk-007-Cone2
Qt&Vtk-007-Cone2
136 0
Qt&Vtk-007-Cone2
Qt&Vtk-010-Cone5
Qt&Vtk-010-Cone5
169 0
Qt&Vtk-010-Cone5
Qt&Vtk-026-QScalarsToColors
Qt&Vtk-026-QScalarsToColors
213 0
Qt&Vtk-026-QScalarsToColors
Qt&Vtk-019-GPURenderDemo
Qt&Vtk-019-GPURenderDemo
160 0
Qt&Vtk-019-GPURenderDemo
Qt&Vtk-014-CustomLinkView
Qt&Vtk-014-CustomLinkView
143 0
Qt&Vtk-014-CustomLinkView
Qt&Vtk-008-Cone3
Qt&Vtk-008-Cone3
134 0
Qt&Vtk-008-Cone3
Qt&Vtk-024-PiecewiseItem
Qt&Vtk-024-PiecewiseItem
167 0
Qt&Vtk-024-PiecewiseItem