文章目录
Qt&Vtk-Cone6
1 代码搬运
1.1 cone6.h
1.2 cone6.cpp
2 运行效果
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(); }