Qt&Vtk-Cone6

234 阅读1分钟

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

头图

Qt&Vtk-Cone61 代码搬运1.1 cone6.h1.2 cone6.cpp2 运行效果3 知识点3.1 vtkBoxWidget★ 源码 ★

Qt&Vtk-Cone6

今天我们又来搬运代码了,今天还是搞Cone,哈哈哈,已经是第六个Cone,不过这是最后一个了,还有点意思,先看看官方的例子。

动画

1 代码搬运

1.1 cone6.h

 #ifndef CONE6_H
 #define CONE6_H
 ​
 #include <QWidget>
 #include <QTimer>
 #include <QDebug>
 #include <QString>
 #include <QTextBrowser>
 #include "QVTKOpenGLWidget.h"               //新版本,旧版QVTKWidget
 #include "vtkAutoInit.h"
 ​
 #include "vtkConeSource.h"
 #include "vtkPolyDataMapper.h"
 #include "vtkRenderer.h"
 #include "vtkRenderWindow.h"
 #include "vtkRenderWindowInteractor.h"
 #include "vtkCamera.h"
 #include "vtkActor.h"
 #include "vtkCommand.h"
 #include "vtkBoxWidget.h"
 #include "vtkTransform.h"
 #include "vtkInteractorStyleTrackballCamera.h"
 ​
 ​
 namespace Ui {
 class Cone6;
 }
 ​
 ​
 ​
 ​
 class vtkMyCallBack : public vtkCommand
 {
 public:
     static vtkMyCallBack *New()
     {
         return new vtkMyCallBack;
     }
     void Execute(vtkObject *caller, unsigned long eventId, void *callData) override;
 };
 ​
 ​
 class Cone6 : public QWidget
 {
     Q_OBJECT
 ​
 public:
     explicit Cone6(QWidget *parent = 0);
     ~Cone6();
 ​
     void startInteractor();
 ​
 private:
     Ui::Cone6 *ui;
 ​
 ​
     vtkConeSource *cone = nullptr;
 ​
     vtkPolyDataMapper *mapper = nullptr;
 ​
     vtkActor *actor = nullptr;
 ​
     vtkRenderer *render = nullptr;
 ​
     vtkRenderWindowInteractor *iren = nullptr;
 ​
     vtkInteractorStyleTrackballCamera * style = nullptr;
 ​
     vtkBoxWidget *boxWidget = nullptr;
 ​
     vtkMyCallBack *callback = nullptr;
 ​
 };
 ​
 #endif // CONE6_H
 ​

1.2 cone6.cpp

 #include "cone6.h"
 #include "ui_cone6.h"
 ​
 Cone6::Cone6(QWidget *parent) :
     QWidget(parent),
     ui(new Ui::Cone6)
 {
     ui->setupUi(this);
 ​
 ​
     cone = vtkConeSource::New();
     cone->SetRadius(1);
     cone->SetResolution(20);
     cone->SetHeight(3);
 ​
 ​
 ​
     mapper = vtkPolyDataMapper::New();
     mapper->SetInputConnection(cone->GetOutputPort());
 ​
 ​
 ​
     actor = vtkActor::New();
     actor->SetMapper(mapper);
 ​
 ​
     render = vtkRenderer::New();
     render->AddActor(actor);
     render->SetBackground(0,0,1);
 ​
 ​
     ui->widget->GetRenderWindow()->AddRenderer(render);
 ​
 ​
     iren = vtkRenderWindowInteractor::New();
     iren->SetRenderWindow(ui->widget->GetRenderWindow());
 ​
 ​
 ​
     style = vtkInteractorStyleTrackballCamera::New();
     iren->SetInteractorStyle(style);
 ​
 ​
     boxWidget = vtkBoxWidget::New();
     boxWidget->SetInteractor(iren);
     boxWidget->SetPlaceFactor(1.25);
 ​
     boxWidget->SetProp3D(actor);
     boxWidget->PlaceWidget();
 ​
 ​
     callback = vtkMyCallBack::New();
     boxWidget->AddObserver(vtkCommand::InteractionEvent,callback);
     boxWidget->On();
 ​
     iren->Initialize();
 ​
 ​
 }
 ​
 Cone6::~Cone6()
 {
     delete ui;
 }
 ​
 void Cone6::startInteractor()
 {
     iren->Start();
 }
 ​
 void vtkMyCallBack::Execute(vtkObject *caller, unsigned long eventId, void *callData)
 {
     vtkTransform *t = vtkTransform::New();
     vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller);
     widget->GetTransform(t);
     widget->GetProp3D()->SetUserTransform(t);
     t->Delete();
 }
 ​

2 运行效果

动画

3 知识点

3.1 vtkBoxWidget

参考:blog.csdn.net/minmindianz…

vtk.org/doc/nightly…

image-20210625183626908

image-20210625183707515

这个Cone更新完了,暂时就不用和Cone玩了。

★ 源码 ★

在这里插入图片描述

这里就要有人问了呀,这么优秀的代码,能不能分享下呀,当然可以呀,我不生产代码,我只是代码的搬运工,链接如下:

自取:github.com/DreamLife-J…

在这里插入图片描述