Qt&Vtk-Cylinder

131 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

头图

代码吗,能跑就不要动,能抄就不要自己造,所以今天接着更新代码,搞一个小柱体。

1 官方示例展示2 代码搬运2.1 cylinder.h2.2 cylinder.cpp3 运行效果★ 源码 ★

1 官方示例展示

今天又来搬运代码了,因为能力有限,有一个搞不定,所以这里我们暂时跳过了一个例子,那个例子有点复杂,这里先跳过吧,先找软柿子捏。

今天我们来绘制这个金箍棒。

动画

2 代码搬运

2.1 cylinder.h

#ifndef CYLINDER_H
#define CYLINDER_H#include <QWidget>
#include "QVTKOpenGLWidget.h"               //新版本,旧版QVTKWidget
#include "vtkAutoInit.h"
​
​
#include "vtkCylinderSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkNamedColors.h"
#include "vtkNew.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkActor.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"namespace Ui {
class Cylinder;
}
​
class Cylinder : public QWidget
{
    Q_OBJECT
​
public:
    explicit Cylinder(QWidget *parent = 0);
    ~Cylinder();
​
private:
    Ui::Cylinder *ui;
​
    vtkCylinderSource *cylinder = nullptr;
​
    vtkPolyDataMapper *mapper = nullptr;
​
    vtkActor *actor = nullptr;
​
    vtkRenderer *render = nullptr;
​
​
};
​
#endif // CYLINDER_H

2.2 cylinder.cpp

#include "cylinder.h"
#include "ui_cylinder.h"
#include <vtkAnnotationLink.h>
#include <vtkCommand.h>
#include <vtkDataObjectToTable.h>
#include <vtkDataRepresentation.h>
#include <vtkEventQtSlotConnect.h>
#include "vtkGenericOpenGLRenderWindow.h"
#include <vtkGraphLayoutView.h>
#include <vtkQtTableView.h>
#include <vtkQtTreeView.h>
#include <vtkRenderer.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkTable.h>
#include <vtkTableToGraph.h>
#include <vtkTreeLayoutStrategy.h>
#include <vtkViewTheme.h>
#include <vtkViewUpdater.h>
#include <vtkXMLTreeReader.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkTable.h>
#include <vtkTableToGraph.h>
#include <vtkTreeLayoutStrategy.h>
#include <vtkViewTheme.h>
#include <vtkViewUpdater.h>
#include <vtkXMLTreeReader.h>
Cylinder::Cylinder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Cylinder)
{
    ui->setupUi(this);
​
    cylinder = vtkCylinderSource::New();
    cylinder->SetResolution(80);
    cylinder->SetHeight(10);
​
​
    mapper = vtkPolyDataMapper::New();
    mapper->SetInputConnection(cylinder->GetOutputPort());
​
​
    actor = vtkActor::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(1.0,0.23,0.45);
    actor->RotateX(30.0);
    actor->RotateY(-45.0);
​
​
    render = vtkRenderer::New();
    render->AddActor(actor);
​
    ui->widget->GetRenderWindow()->AddRenderer(render);
​
}
​
Cylinder::~Cylinder()
{
    delete ui;
}
​

3 运行效果

我的这个是粉箍棒,哈哈哈。其他的,可以改参数直接使用,或者套用前面的内容,给他装一个盒子,就可以改变他的大小了。

动画

★ 源码 ★

源码分享一时爽,一直分享一直爽, 链接如下:

自取:github.com/DreamLife-J…

在这里插入图片描述