文章目录
Qt&Vtk-Cone2
1 代码搬运
1.1 cone2.h
1.2 cone2.cpp
1.3 官方代码
2 运行效果
3 知识点
3.1 观察者模式
★ 源码 ★
Qt&Vtk-Cone2
今天我们又来搬运代码了,看标题也知道,这个和昨天的那个应该是没有啥区别的,那么我看官方实例的描述的,就是下面的内容。
// This example shows how to add an observer to a C++ program. It extends
// the Step1/Cxx/Cone.cxx C++ example (see that example for information on
// the basic setup).
大致翻译:意思就是演示一下如何在C++中添加一个观察者,项目是扩展的了昨天的那个Cone
官方的效果呢,就是下面这个鬼样子,相比于昨天的样子,就是在cmd窗口中有了数据输出。
#ifndef CONE2_H #define CONE2_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 "vtkRenderWindow.h" #include "vtkCamera.h" #include "vtkActor.h" #include "vtkRenderer.h" #include "vtkCommand.h" namespace Ui { class Cone2; } class vtkMyCallback : public vtkCommand { public: static vtkMyCallback *New() { return new vtkMyCallback; } void Execute(vtkObject *caller,unsigned long,void*) override; QTextBrowser *textBrowser = nullptr; private: }; class Cone2 : public QWidget { Q_OBJECT public: explicit Cone2(QWidget *parent = nullptr); ~Cone2(); void outputMessage(double temp); private: Ui::Cone2 *ui; vtkConeSource *cone = nullptr; //声明椎体 vtkPolyDataMapper *coneMapper = nullptr; //声明映射器 vtkActor *coneActor = nullptr; //声明Actor vtkRenderer *coneRender = nullptr; //声明渲染器 QTimer *rotationTimer = nullptr; //声明定时器,用于旋转椎体 vtkMyCallback *myCallBack = nullptr; }; #endif // CONE2_H
1.2 cone2.cpp
#include "cone2.h" #include "ui_cone2.h" Cone2::Cone2(QWidget *parent) : QWidget(parent), ui(new Ui::Cone2) { ui->setupUi(this); cone = vtkConeSource::New(); //新建对象 cone->SetHeight(3.0); //设置高度 cone->SetRadius(1.0); //设置半径 cone->SetResolution(10000000); //翻译为设置分辨率,不过实际应该不是 coneMapper = vtkPolyDataMapper::New(); //新建映射器 coneMapper->SetInputConnection(cone->GetOutputPort()); //设置Cone输出coneMapper数据输入 //新建Actor,并设置映射器 coneActor = vtkActor::New(); coneActor->SetMapper(coneMapper); //新建渲染器并设置Actor和背景颜色 coneRender = vtkRenderer::New(); coneRender->AddActor(coneActor); coneRender->SetBackground(0.1,0.2,0.3); myCallBack = vtkMyCallback::New(); myCallBack->textBrowser = ui->textBrowser; coneRender->AddObserver(vtkCommand::StartEvent,myCallBack); //获取渲染窗口 ui->widget->GetRenderWindow()->AddRenderer(coneRender); // while (1) { // ui->widget->GetRenderWindow()->Render(); // coneRender->GetActiveCamera()->Azimuth(1); // } //旋转椎体 rotationTimer = new QTimer(); connect(rotationTimer,&QTimer::timeout,this,[=](){ coneRender->GetActiveCamera()->Azimuth(1); ui->widget->GetRenderWindow()->Render(); //注意这句话,要加上呀,不然人不给你转,惰性渲染。 }); rotationTimer->start(25); } Cone2::~Cone2() { rotationTimer->stop(); delete ui; } void vtkMyCallback::Execute(vtkObject *caller, unsigned long, void *) { vtkRenderer *renderer = reinterpret_cast<vtkRenderer*>(caller); textBrowser->append("Camera Position:"); textBrowser->append(QString::number((renderer->GetActiveCamera()->GetPosition()[0]),'g',6)); textBrowser->append(QString::number((renderer->GetActiveCamera()->GetPosition()[1]),'g',6)); textBrowser->append(QString::number((renderer->GetActiveCamera()->GetPosition()[2]),'g',6)); }
1.3 官方代码
#include "vtkConeSource.h" #include "vtkPolyDataMapper.h" #include "vtkRenderWindow.h" #include "vtkCommand.h" #include "vtkCamera.h" #include "vtkActor.h" #include "vtkRenderer.h" // Callback for the interaction class vtkMyCallback : public vtkCommand { public: static vtkMyCallback *New() { return new vtkMyCallback; } void Execute(vtkObject *caller, unsigned long, void*) override { vtkRenderer *renderer = reinterpret_cast<vtkRenderer*>(caller); cout << renderer->GetActiveCamera()->GetPosition()[0] << " " << renderer->GetActiveCamera()->GetPosition()[1] << " " << renderer->GetActiveCamera()->GetPosition()[2] << "\n"; } }; int main() { // // The pipeline creation is documented in Step1 // vtkConeSource *cone = vtkConeSource::New(); cone->SetHeight( 3.0 ); cone->SetRadius( 1.0 ); cone->SetResolution( 10 ); vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); coneMapper->SetInputConnection( cone->GetOutputPort() ); vtkActor *coneActor = vtkActor::New(); coneActor->SetMapper( coneMapper ); vtkRenderer *ren1= vtkRenderer::New(); ren1->AddActor( coneActor ); ren1->SetBackground( 0.1, 0.2, 0.4 ); ren1->ResetCamera(); vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer( ren1 ); renWin->SetSize( 300, 300 ); // Here is where we setup the observer, we do a new and ren1 will // eventually free the observer vtkMyCallback *mo1 = vtkMyCallback::New(); ren1->AddObserver(vtkCommand::StartEvent,mo1); mo1->Delete(); // // now we loop over 360 degrees and render the cone each time // int i; for (i = 0; i < 360; ++i) { // render the image renWin->Render(); // rotate the active camera by one degree ren1->GetActiveCamera()->Azimuth( 1 ); } // // Free up any objects we created // cone->Delete(); coneMapper->Delete(); coneActor->Delete(); ren1->Delete(); renWin->Delete(); return 0; }
3 知识点
3.1 观察者模式
参考资料:https://zhuanlan.zhihu.com/p/119308881
引用资料中的一句话观察者模式的核心思想就是一对多。
看到这里,是不是感觉和Qt 的信号槽好像呀,Qt的信号槽就是观察者模式的一种实现。
★ 源码 ★