Qt&Vtk-013-Cube
Posted DreamLife.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt&Vtk-013-Cube相关的知识,希望对你有一定的参考价值。
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;
}
2 运行效果
★ 源码 ★
这里就要有人问了呀,这么优秀的代码,能不能分享下呀,当然可以呀,我不生产代码,我只是代码的搬运工,链接如下:
以上是关于Qt&Vtk-013-Cube的主要内容,如果未能解决你的问题,请参考以下文章
26.Qt Quick QML-RotationAnimationPathAnimationSmoothedAnimationBehaviorPauseAnimationSequential(代码片段
如何在 Javadoc 中使用 @ 和 符号格式化代码片段?