文章目錄
Qt&Vtk-Cone6
1 代碼搬運
1.1 cone6.h
1.2 cone6.cpp
2 運作效果
3 知識點
3.1 vtkBoxWidget
★ 源碼 ★
今天我們又來搬運代碼了,今天還是搞Cone,哈哈哈,已經是第六個Cone,不過這是最後一個了,還有點意思,先看看官方的例子。
#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
#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();
}