vs2013+qt5.51+vtk 实现的 双view,显示并联动操作

本文参考了https://blog.csdn.net/Jecklin_online/article/details/82187558的博客,此博客是用vtk实现的,此文是在此基础上增加了qt界面是利用了qt的QVTKWidget来实现的。

1.首先我们必须将QVTKWidget加入到Qt Designer,只需找到编译好的vtk的release版本下的QVTKWidgetPlugin.dll,QVTKWidgetPlugin.exp,QVTKWidgetPlugin.lib放入到自己QT下的designer下即可(我的目录是:F:\Qt\Qt5.5.1\5.5\msvc2013\plugins\designer)

2.写好CmakeLists.txt


CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
PROJECT( qt )

#----------------------------------------------------------------------------------
FIND_PACKAGE( VTK REQUIRED )
FIND_PACKAGE( Qt5Widgets )

INCLUDE( ${VTK_USE_FILE} )


#----------------------------------------------------------------------------------
#	定义变量PROJECT_SRCS,其值为所列表的文件列表,包含所有源文件
SET( PROJECT_SRCS
	main.cpp
	ProjectMainWindow.cpp
	ProjectMainWindow.h
	
    )
#	定义变量PROJECT_UIS,其值为所列表的文件列表,包含所有的ui文件
SET( PROJECT_UIS
    ProjectMainWindow.ui
)
#	定义变量PROJECT_MOC_HDRS,其值为所列表的文件列表,包含所有Q_OBJECT的头文件
SET( PROJECT_MOC_HDRS
  ProjectMainWindow.h
  
)

#----------------------------------------------------------------------------------
#	QT5_WRAP_UI通过QT的uic.exe生成UI文件对应的ui_xxx.h文件,
#	并将生成的moc_xxx.h文件列表存储于变量PROJECT_UIS_H 里
QT5_WRAP_UI( PROJECT_UIS_H 
             ${PROJECT_UIS}
           )
#	QT5_WRAP_CPP通过QT的moc.exe将包含的Q_OBJECT的文件生成对应的moc_xxx.cxx文件,
#	并将生成的moc_xxx.cxx文件列表存储于变量PROJECT_MOC_SRCS里	
QT5_WRAP_CPP( PROJECT_MOC_SRCS 
              ${PROJECT_MOC_HDRS}
            )

#----------------------------------------------------------------------------------
#	Qt的MOC和UIC程序生成的moc_xxx.cxx和ui.xxx.h等文件是存放在CMAKE的
#	where to bulid the binaries 里指定的目录,所以必须把这些路径包含进来
INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR} 
                     ${CMAKE_CURRENT_BINARY_DIR} 
                     ${VTK_DIR} 
                   )

#	Qt程序如果有资源文件(*.qrc),要包含资源文件,
#	qt5_add_resources 用的rcc.exe生成相应的qrc_xxx.cpp文件,
#	并将生成的文件列表存储于变量Project_RCC_SRCS里
#	set(Project_RCCS YourProjectName.qrc)
#	qt5_add_resources(Project_RCCS_SRCS ${Project_RCCS})



#	根据程序的cpp文件、头文件以及生成的ui_xxx.h、moc_xxx.cxx、qrc_xxx.cxx
#	等生成可执行文件,并链接Qt的动态库(Qt的动态都定义在QT_LIBRARIES变量里了)
ADD_EXECUTABLE( qt 
                ${PROJECT_SRCS} 
                ${PROJECT_UIS_H} 
                ${PROJECT_MOC_SRCS} 
              )

TARGET_LINK_LIBRARIES ( qt
  ${VTK_LIBRARIES}
  #QVTK 
  )

3.用cmake编译

4.main函数

#include "ProjectMainWindow.h"
#include 

//程序发布时,可以不要注释以下语句,编译的时候就不会带控制台输出窗口
//#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )

int main( int argc, char **argv ) 
{
	QApplication *app = new QApplication(argc, argv);
	ProjectMainWindow *window = new ProjectMainWindow();
	window->show();
	return app->exec();
};

5.ProjectMainwindow.h

#ifndef Project_MainWindow_H
#define Project_MainWindow_H

#include 
#include "ui_ProjectMainWindow.h"
#include 

class vtkImageViewer2;
class vtkRenderer;
class vtkEventQtSlotConnect;
class vtkObject;
class vtkCommand;

class ProjectMainWindow : public QMainWindow
{
	Q_OBJECT

public:
	ProjectMainWindow(QWidget *parent = 0);
	~ProjectMainWindow();

private:
	Ui::ProjectMainWindowClass ui;

private slots:
	//响应打开图像文件的槽函数
	void onOpenSlot();


private:

	vtkSmartPointer< vtkRenderer > m_pRenderder_left;
	vtkSmartPointer< vtkRenderer > m_pRenderder_right;

	vtkEventQtSlotConnect* m_Connections;
};

#endif

6.ProjectMainwindow.cpp

#include "ProjectMainWindow.h"  
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include 
#include 
#include   
#include 


#include "Header.h"
//#include "Header2.h"
#include "vtkEventQtSlotConnect.h"  
#include "vtkCommand.h"  

ProjectMainWindow::ProjectMainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	m_pRenderder_left = vtkSmartPointer< vtkRenderer >::New();
	m_pRenderder_right = vtkSmartPointer< vtkRenderer >::New();
	m_Connections = vtkEventQtSlotConnect::New();

	// 设置m_QVTKWidget的渲染器  
	ui.qvtkWidget_1->GetRenderWindow()->AddRenderer(m_pRenderder_left);
	ui.qvtkWidget_1->GetRenderWindow()->AddRenderer(m_pRenderder_right);

	//连接打开的信号与相应的槽    
	connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(onOpenSlot()));
	connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(close()));


	vtkSmartPointerinteractor = vtkSmartPointer::New();
	vtkSmartPointer style = vtkSmartPointer::New();
	style->SetLeftRender(m_pRenderder_left);
	style->SetRightRender(m_pRenderder_right);
	interactor->SetInteractorStyle(style);


	//vtkSmartPointer camera = vtkSmartPointer::New();
	//camera->SetPosition(4, 4, 4);
	//m_pRenderder_left->SetActiveCamera(camera);
	//m_pRenderder_right->SetActiveCamera(camera);

	//vtkSmartPointerinteractor = vtkSmartPointer::New();


	//vtkSmartPointer style = vtkSmartPointer::New();
	//style->SetLeftRender(m_pRenderder_left);
	//style->SetRightRender(m_pRenderder_right);
	//style->SetCamera(camera);

	//interactor->SetInteractorStyle(style);

	ui.qvtkWidget_1->GetRenderWindow()->SetInteractor(interactor);
	ui.qvtkWidget_1->GetRenderWindow()->Render();

}

ProjectMainWindow::~ProjectMainWindow()
{
	m_Connections->Delete();
}

void ProjectMainWindow::onOpenSlot()
{

	double leftViewStation[4] = { 0.0, 0.0, 0.5, 1.0 };
	double rightViewStation[4] = { 0.5, 0.0, 1.0, 1.0 };

	QString filter;
	filter = "stl file (*.stl)";
	QDir dir;

	QString fileName = QFileDialog::getOpenFileName(this,
		QString(tr("打开图像")), dir.absolutePath(), filter);
	if (fileName.isEmpty() == true) return;

	// 支持带中文路径的读取  
	QByteArray ba = fileName.toLocal8Bit();
	const char *fileName_str = ba.data();

	qDebug() << fileName_str << endl;
	// 用vtkSTLReader读取STL图像  
	vtkSmartPointer reader = vtkSmartPointer::New();
	reader->SetFileName(fileName_str);
	reader->Update();

	vtkSmartPointer mapper =
		vtkSmartPointer::New();
	mapper->SetInputConnection(reader->GetOutputPort());

	vtkSmartPointer actor =
		vtkSmartPointer::New();

	actor->SetMapper(mapper);

	m_pRenderder_left->AddActor(actor);
	m_pRenderder_left->ResetCamera();
	m_pRenderder_left->DrawOn();
	m_pRenderder_left->SetViewport(leftViewStation);

	
	
	m_pRenderder_right->AddActor(actor);
	m_pRenderder_right->ResetCamera();
	m_pRenderder_right->DrawOn();
	m_pRenderder_right->SetViewport(rightViewStation);
	

	
}

7.新建一个Header.h

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


class myInteractorStyle :public vtkInteractorStyleTrackballCamera
{
public:

	static myInteractorStyle* New()
	{
		return new myInteractorStyle;
	}

	void    SetLeftRender(vtkRenderer *render)
	{
		this->m_leftRender = render;
	}

	void    SetRightRender(vtkRenderer *render)
	{
		this->m_rightRender = render;
	}

	void    OnMouseMove()
	{
		vtkInteractorStyleTrackballCamera::OnMouseMove();
		this->CameraSynchronous();
	}

	void OnLeftButtonDown()
	{
		vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
		this->CameraSynchronous();
	}

	void OnLeftButtonUp()
	{
		vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
		this->CameraSynchronous();
	}

	void OnMiddleButtonDown()
	{
		vtkInteractorStyleTrackballCamera::OnMiddleButtonDown();
		this->CameraSynchronous();
	}

	void OnMiddleButtonUp()
	{
		vtkInteractorStyleTrackballCamera::OnMiddleButtonUp();
		this->CameraSynchronous();
	}

	void OnRightButtonDown()
	{
		vtkInteractorStyleTrackballCamera::OnRightButtonDown();
		this->CameraSynchronous();
	}

	void OnRightButtonUp()
	{
		vtkInteractorStyleTrackballCamera::OnRightButtonUp();
		this->CameraSynchronous();
	}

	void OnMouseWheelForward()
	{
		vtkInteractorStyleTrackballCamera::OnMouseWheelForward();
		this->CameraSynchronous();
	}

	void OnMouseWheelBackward()
	{
		vtkInteractorStyleTrackballCamera::OnMouseWheelBackward();
		this->CameraSynchronous();
	}



	void Rotate()
	{
		vtkInteractorStyleTrackballCamera::Rotate();
		this->CameraSynchronous();
	}

	void Spin()
	{
		vtkInteractorStyleTrackballCamera::Spin();
		this->CameraSynchronous();
	}

	void Pan()
	{
		vtkInteractorStyleTrackballCamera::Pan();
		this->CameraSynchronous();
	}

	void Dolly()
	{
		vtkInteractorStyleTrackballCamera::Dolly();
		this->CameraSynchronous();
	}




private:
	void    CameraSynchronous()
	{
		vtkCamera *leftCamera = m_leftRender->GetActiveCamera();
		vtkCamera *rightCamera = m_rightRender->GetActiveCamera();

		//获取窗口大小
		int *winSize = this->GetInteractor()->GetRenderWindow()->GetSize();

		//获取事件窗口位置
		int eventStation[3];
		this->GetInteractor()->GetEventPosition(eventStation);

		if (eventStation[0] < winSize[0] / 2)
		{
			rightCamera->SetPosition(leftCamera->GetPosition());
			rightCamera->SetFocalPoint(leftCamera->GetFocalPoint());
			rightCamera->SetViewUp(leftCamera->GetViewUp());
			m_rightRender->ResetCameraClippingRange();

		}
		else
		{
			leftCamera->SetPosition(rightCamera->GetPosition());
			leftCamera->SetViewUp(rightCamera->GetViewUp());
			leftCamera->SetFocalPoint(rightCamera->GetFocalPoint());
			m_leftRender->ResetCameraClippingRange();

		}

		this->GetInteractor()->GetRenderWindow()->Render();
	}

private:
	vtkRenderer     *m_leftRender;
	vtkRenderer     *m_rightRender;

};

8.ProjectMainwindow.ui



 ProjectMainWindowClass
 
  
   
    0
    0
    607
    583
   
  
  
   ProjectMainWindow
  
  
   
    
     
      10
      20
      571
      331
     
    
   
  
  
   
    
     0
     0
     607
     23
    
   
   
    
     文件
    
    
   
   
    
     退出
    
    
   
   
   
  
  
  
   
    Print
   
  
  
   
    false
   
   
    Help
   
  
  
   
    Save
   
  
  
   
    open
   
  
  
   
    exit
   
  
 
 
  
   QVTKWidget
   QWidget
   
QVTKWidget.h

显示效果图

vs2013+qt5.51+vtk 实现的 双view,显示并联动操作_第1张图片

 

无论点那个都可以使图像联动

你可能感兴趣的:(vtk)