forked from alankzh/MNavigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyslicerwidget.cpp
More file actions
188 lines (168 loc) · 5.93 KB
/
Copy pathmyslicerwidget.cpp
File metadata and controls
188 lines (168 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "myslicerwidget.h"
#include "CoordinateConverter.h"
#include "actormanager.h"
#include "TextAnnotation.h"
#include <sstream>
mySlicerWidget::mySlicerWidget(QWidget *parent): QVTKWidget(parent)
{
//设置窗口背景为黑色,否则三个窗口背景会透明
imageViewer2=vtkSmartPointer<vtkImageViewer2>::New();
imageViewer2->SetRenderWindow(this->GetRenderWindow());
imageViewer2->SetupInteractor(this->GetRenderWindow()->GetInteractor());
imageViewer2->GetRenderer()->ResetCamera();
imageViewer2->GetRenderer()->SetBackground(0,0,0);
vtkConnections = vtkSmartPointer<vtkEventQtSlotConnect>::New();
// connect(this, SIGNAL(OnMarkClick(vtkVector3d)), parent, SIGNAL(Mark(vtkVector3d)));
// connect(parent, SIGNAL(Mark(vtkVector3d)), this, SLOT(MarkReact(vtkVector3d)));
// updateRender();
}
/**
*设置几何位置
*/
void mySlicerWidget::setLocation(int x,int y,int width,int height){
this->setGeometry(x,y,width,height);
}
/**
*设置imageViewer2中截图截取位置
*/
void mySlicerWidget::setSlicerValue(int shiftValue){
imageViewer2->SetSlice(shiftValue);
int min = imageViewer2->GetSliceMin();
int max = imageViewer2->GetSliceMax();
float ratio = (float)(shiftValue - min) / (max - min);
std::string value;
std::stringstream ss;
ss << ratio;
ss >> value;
depth_info->SetInput(((std::string)"Slice Depth: " + value).c_str());
updateRender();
}
/**
*设置imageViewer2中截图截取位置,根据比例
*/
void mySlicerWidget::setSlicerValueByRatio(double ratio) {
int min = imageViewer2->GetSliceMin();
int max = imageViewer2->GetSliceMax();
int target = (int)ratio * (max - min) + min;
setSlicerValue(target);
}
/**
* 设置截面截取的坐标平面
*/
void mySlicerWidget::setOrientation(mySlicerWidget::ORIENTATION o){
switch(o){
case XY:
imageViewer2->SetSliceOrientationToXY();
direction_info->SetInput(((std::string)"Slice Direction: " + (std::string)"sagital").c_str());
break;
case YZ:
imageViewer2->SetSliceOrientationToYZ();
direction_info->SetInput(((std::string)"Slice Direction: " + (std::string)"coronal").c_str());
break;
case XZ:
imageViewer2->SetSliceOrientationToXZ();
direction_info->SetInput(((std::string)"Slice Direction: " + (std::string)"axial").c_str());
break;
}
}
mySlicerWidget::ORIENTATION mySlicerWidget::GetOrientation() const {
if (imageViewer2 != NULL) {
int orientation = imageViewer2->GetSliceOrientation();
switch (orientation)
{
case 0:
return YZ;
case 1:
return XZ;
case 2:
return XY;
default:
return defalut;
break;
}
}
return defalut;
}
/**
* 获取截面截取最大值
*/
int mySlicerWidget::getSlicerMax(){
return imageViewer2->GetSliceMax();
}
/**
* 获取截面截取最小值
*/
int mySlicerWidget::getSlicerMin(){
return imageViewer2->GetSliceMin();
}
/**
* 获取截面截取值
*/
int mySlicerWidget::getSlicer(){
return imageViewer2->GetSlice();
}
/**
* 更新此窗口内的Render使窗口重新渲染
*/
void mySlicerWidget::updateRender(){
imageViewer2->GetRenderWindow()->Render();
}
/**
* 获取此窗口内的核心控件vtkImageViewer2
* 慎用
*/
vtkSmartPointer<vtkImageViewer2> mySlicerWidget::getImageViewer2(){
return imageViewer2;
}
void mySlicerWidget::ListenMarkClick() {
vtkConnections->Connect(imageViewer2->GetRenderWindow()->GetInteractor(), vtkCommand::RightButtonPressEvent, this, SLOT(Mark(vtkObject*, unsigned long, void*, void*)));
}
void mySlicerWidget::Mark(vtkObject* obj, unsigned long, void*, void*) {
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::SafeDownCast(obj);
int EventPointX = iren->GetEventPosition()[0];
int EventPointY = iren->GetEventPosition()[1];
vtkVector3d modelPosition = CoordinateConverter::EventToModel(imageViewer2, EventPointX, EventPointY);
emit this->OnMarkClick(modelPosition);
}
void mySlicerWidget::MarkReact(vtkVector3d ModelPosition) {
setSlicerValue(CoordinateConverter::ModelToSlice(imageViewer2, ModelPosition));
vtkVector3d worldPostion = CoordinateConverter::ModelToWorld(imageViewer2->GetImageActor(), ModelPosition);
actorManager* manager = new actorManager;
auto sphere = manager->getSphereActor(worldPostion[0], worldPostion[1], worldPostion[2]);
imageViewer2->GetRenderer()->AddActor(sphere);
updateRender();
}
/**
* @brief mySlicerWidget::loadSlicerData
* 读取体绘制数据,得到截面
* @param o
* 截面方向
*/
void mySlicerWidget::loadSlicerData(vtkImageData *data,mySlicerWidget::ORIENTATION o=mySlicerWidget::ORIENTATION::defalut){
imageViewer2=vtkSmartPointer<vtkImageViewer2>::New();
imageViewer2->SetInputData(data);
imageViewer2->SetRenderWindow(this->GetRenderWindow());
imageViewer2->SetupInteractor(this->GetRenderWindow()->GetInteractor());
direction_info = Text::CreateAnnotation("Slice Direction:", vtkVector3d(1, 1, 1));
direction_info->SetPosition(5, 340);
imageViewer2->GetRenderer()->AddViewProp(direction_info);
depth_info = Text::CreateAnnotation("Slice Depth:", vtkVector3d(0.3, 0.3, 1));
depth_info->SetPosition(5, 0);
imageViewer2->GetRenderer()->AddViewProp(depth_info);
ListenMarkClick();
if(o!=0){
setOrientation(o);
}
hasSlicerSourceData=true;
//默认移动到滑动条中间
setSlicerValue(getSlicerMax()/2);
updateRender();
}
/*new ui start---------------------------------------------------------------------------------------------------------------------------------------*/
void mySlicerWidget::onSliderValueChanged(int v){
if(hasSlicerSourceData){
this->setSlicerValue(v);
this->update();
}
}
/*new ui end-----------------------------------------------------------------------------------------------------------------------------------*/