forked from ProstateBRP/ProstateNav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkZFrameRobotToImageRegistration.cxx
223 lines (177 loc) · 7.48 KB
/
vtkZFrameRobotToImageRegistration.cxx
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*==========================================================================
Portions (c) Copyright 2008 Brigham and Women's Hospital (BWH) All Rights Reserved.
See Doc/copyright/copyright.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Program: 3D Slicer
Module: $HeadURL: http://svn.slicer.org/Slicer3/trunk/Modules/OpenIGTLinkIF/vtkRobotRegistration.cxx $
Date: $Date: 2009-01-05 13:28:20 -0500 (Mon, 05 Jan 2009) $
Version: $Revision: 8267 $
==========================================================================*/
/*
* ZFrame Fiducial Registration Algorithm
* The method and code is originally developoed by Simon DiMaio from BWH.
*/
#include "vtkObjectFactory.h"
#include "vtkZFrameRobotToImageRegistration.h"
#include "vtkMRMLModelNode.h"
#include "vtkMRMLModelDisplayNode.h"
#include "vtkMRMLLinearTransformNode.h"
#include "vtkMRMLVolumeNode.h"
#include "vtkMRMLScalarVolumeNode.h"
#include "vtkMRMLScalarVolumeDisplayNode.h"
#include "vtkMRMLVolumeHeaderlessStorageNode.h"
#include "vtkMRMLVolumeArchetypeStorageNode.h"
#include <string.h>
// Test
#include <fstream>
#include "ZFrameCalibration.h"
#define MEPSILON (1e-10)
vtkStandardNewMacro(vtkZFrameRobotToImageRegistration);
vtkCxxRevisionMacro(vtkZFrameRobotToImageRegistration, "$Revision: 8267 $");
//#define DEBUG_ZFRAME_REGISTRATION 1
//---------------------------------------------------------------------------
vtkZFrameRobotToImageRegistration::vtkZFrameRobotToImageRegistration()
{
this->SliceRangeLow = -1;
this->SliceRangeHigh = -1;
this->ZFrameBaseOrientation = vtkMatrix4x4::New();
this->ZFrameBaseOrientation->Identity();
}
//---------------------------------------------------------------------------
vtkZFrameRobotToImageRegistration::~vtkZFrameRobotToImageRegistration()
{
this->ZFrameBaseOrientation->Delete();
this->ZFrameBaseOrientation = NULL;
}
//---------------------------------------------------------------------------
void vtkZFrameRobotToImageRegistration::PrintSelf(ostream& os, vtkIndent indent)
{
}
//---------------------------------------------------------------------------
void vtkZFrameRobotToImageRegistration::SetSliceRange(int param1, int param2)
{
this->SliceRangeLow = param1;
this->SliceRangeHigh = param2;
}
//---------------------------------------------------------------------------
int vtkZFrameRobotToImageRegistration::DoRegistration()
{
if (this->FiducialVolume && this->RobotToImageTransform)
{
//simond debug frame delay
// Get the image size attributes from the event.
vtkImageData* image = this->FiducialVolume->GetImageData();
int dimensions[3];
image->GetDimensions(dimensions);
// Get image position and orientation
vtkMatrix4x4* rtimgTransform = vtkMatrix4x4::New();
this->FiducialVolume->GetIJKToRASMatrix(rtimgTransform);
// Convert image positiona and orientation to zf::Matrix4x4
zf::Matrix4x4 imageTransform;
imageTransform[0][0] = rtimgTransform->GetElement(0, 0);
imageTransform[1][0] = rtimgTransform->GetElement(1, 0);
imageTransform[2][0] = rtimgTransform->GetElement(2, 0);
imageTransform[0][1] = rtimgTransform->GetElement(0, 1);
imageTransform[1][1] = rtimgTransform->GetElement(1, 1);
imageTransform[2][1] = rtimgTransform->GetElement(2, 1);
imageTransform[0][2] = rtimgTransform->GetElement(0, 2);
imageTransform[1][2] = rtimgTransform->GetElement(1, 2);
imageTransform[2][2] = rtimgTransform->GetElement(2, 2);
imageTransform[0][3] = rtimgTransform->GetElement(0, 3);
imageTransform[1][3] = rtimgTransform->GetElement(1, 3);
imageTransform[2][3] = rtimgTransform->GetElement(2, 3);
// ZFrame base orientation
zf::Matrix4x4 ZmatrixBase;
ZmatrixBase[0][0] = (float) this->ZFrameBaseOrientation->GetElement(0, 0);
ZmatrixBase[1][0] = (float) this->ZFrameBaseOrientation->GetElement(1, 0);
ZmatrixBase[2][0] = (float) this->ZFrameBaseOrientation->GetElement(2, 0);
ZmatrixBase[0][1] = (float) this->ZFrameBaseOrientation->GetElement(0, 1);
ZmatrixBase[1][1] = (float) this->ZFrameBaseOrientation->GetElement(1, 1);
ZmatrixBase[2][1] = (float) this->ZFrameBaseOrientation->GetElement(2, 1);
ZmatrixBase[0][2] = (float) this->ZFrameBaseOrientation->GetElement(0, 2);
ZmatrixBase[1][2] = (float) this->ZFrameBaseOrientation->GetElement(1, 2);
ZmatrixBase[2][2] = (float) this->ZFrameBaseOrientation->GetElement(2, 2);
ZmatrixBase[0][3] = (float) this->ZFrameBaseOrientation->GetElement(0, 3);
ZmatrixBase[1][3] = (float) this->ZFrameBaseOrientation->GetElement(1, 3);
ZmatrixBase[2][3] = (float) this->ZFrameBaseOrientation->GetElement(2, 3);
// Convert Base Matrix to quaternion
float ZquaternionBase[4];
zf::MatrixToQuaternion(ZmatrixBase, ZquaternionBase);
// Set slice range
int range[2];
range[0] = this->SliceRangeLow;
range[1] = this->SliceRangeHigh;
// Get a pointer to the image array.
image->SetScalarTypeToShort();
image->Modified();
short* inputImage = (short*)image->GetScalarPointer();
float Zposition[3];
float Zorientation[4];
// Call Z-frame registration
zf::ZFrameCalibration * calibration;
calibration = new zf::ZFrameCalibration();
calibration->SetInputImage(inputImage, dimensions, imageTransform);
calibration->SetOrientationBase(ZquaternionBase);
int r = calibration->Register(range, Zposition, Zorientation);
delete calibration;
if (r)
{
// Convert quaternion to matrix
zf::Matrix4x4 matrix;
zf::QuaternionToMatrix(Zorientation, matrix);
matrix[0][3] = Zposition[0];
matrix[1][3] = Zposition[1];
matrix[2][3] = Zposition[2];
#ifdef DEBUG_ZFRAME_REGISTRATION
std::cerr << "Result matrix:" << std::endl;
zf::PrintMatrix(matrix);
#endif // DEBUG_ZFRAME_REGISTRATION
//// For evaluation
//std::ofstream fout;
//fout.open("zframe_output.csv", std::ios::out | std::ios::app);
//
//fout << volumeNode->GetName() << ", "
// << n << ", "
// << matrix[0][0] << ", " << matrix[0][1] << ", " << matrix[0][2] << ", " << matrix[0][3] << ", "
// << matrix[1][0] << ", " << matrix[1][1] << ", " << matrix[1][2] << ", " << matrix[1][3] << ", "
// << matrix[2][0] << ", " << matrix[2][1] << ", " << matrix[2][2] << ", " << matrix[2][3]
// << std::endl;
//fout.close();
vtkMatrix4x4* zMatrix = vtkMatrix4x4::New();
zMatrix->Identity();
zMatrix->SetElement(0, 0, matrix[0][0]);
zMatrix->SetElement(1, 0, matrix[1][0]);
zMatrix->SetElement(2, 0, matrix[2][0]);
zMatrix->SetElement(0, 1, matrix[0][1]);
zMatrix->SetElement(1, 1, matrix[1][1]);
zMatrix->SetElement(2, 1, matrix[2][1]);
zMatrix->SetElement(0, 2, matrix[0][2]);
zMatrix->SetElement(1, 2, matrix[1][2]);
zMatrix->SetElement(2, 2, matrix[2][2]);
zMatrix->SetElement(0, 3, matrix[0][3]);
zMatrix->SetElement(1, 3, matrix[1][3]);
zMatrix->SetElement(2, 3, matrix[2][3]);
if (this->RobotToImageTransform != NULL)
{
vtkMatrix4x4* transformToParent = this->RobotToImageTransform->GetMatrixTransformToParent();
transformToParent->DeepCopy(zMatrix);
zMatrix->Delete();
this->RobotToImageTransform->Modified();
return 1;
}
else
{
zMatrix->Delete();
return 0;
}
}
else
{
return 0;
}
}
else
{
return 0;
}
}