-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvtkSliceImageTool.cxx
320 lines (272 loc) · 9.1 KB
/
vtkSliceImageTool.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*=========================================================================
Program: ToolCursor
Module: vtkSliceImageTool.cxx
Copyright (c) 2010 David Gobbi
All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSliceImageTool.h"
#include "vtkObjectFactory.h"
#include "vtkToolCursor.h"
#include "vtkCamera.h"
#include "vtkRenderer.h"
#include "vtkImageMapper3D.h"
#include "vtkImageData.h"
#include "vtkMatrix4x4.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkVolumePicker.h"
vtkStandardNewMacro(vtkSliceImageTool);
//----------------------------------------------------------------------------
vtkSliceImageTool::vtkSliceImageTool()
{
this->JumpToNearestSlice = 0;
}
//----------------------------------------------------------------------------
vtkSliceImageTool::~vtkSliceImageTool()
{
}
//----------------------------------------------------------------------------
void vtkSliceImageTool::SetJumpToNearestSlice(int val)
{
if (val != this->JumpToNearestSlice)
{
this->JumpToNearestSlice = val;
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkSliceImageTool::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//----------------------------------------------------------------------------
void vtkSliceImageTool::StartAction()
{
this->Superclass::StartAction();
vtkToolCursor *cursor = this->GetToolCursor();
vtkCamera *camera = cursor->GetRenderer()->GetActiveCamera();
this->StartDistance = camera->GetDistance();
// code for handling the mouse wheel interaction
if ((cursor->GetModifier() & VTK_TOOL_WHEEL_MASK) != 0)
{
int delta = 0;
if ((cursor->GetModifier() & VTK_TOOL_WHEEL_BWD) != 0)
{
delta = -1;
}
else if ((cursor->GetModifier() & VTK_TOOL_WHEEL_FWD) != 0)
{
delta = 1;
}
this->AdvanceSlice(delta);
}
}
//----------------------------------------------------------------------------
void vtkSliceImageTool::StopAction()
{
this->Superclass::StopAction();
}
//----------------------------------------------------------------------------
void vtkSliceImageTool::DoAction()
{
this->Superclass::DoAction();
vtkToolCursor *cursor = this->GetToolCursor();
vtkCamera *camera = cursor->GetRenderer()->GetActiveCamera();
// Get the display position.
double x0, y0, x, y;
this->GetStartDisplayPosition(x0, y0);
this->GetDisplayPosition(x, y);
// Get viewport height at the current depth
double height = 1;
if (camera->GetParallelProjection())
{
height = camera->GetParallelScale();
}
else
{
double angle = vtkMath::RadiansFromDegrees(camera->GetViewAngle());
height = 2*this->StartDistance*sin(angle*0.5);
}
// Get the viewport size in pixels
vtkRenderer *renderer = cursor->GetRenderer();
int *size = renderer->GetSize();
// Get the vertical offset
double dy = y - y0;
double distance = this->StartDistance + dy*height/size[1];
double focalPoint[3], position[3];
camera->GetFocalPoint(focalPoint);
camera->GetPosition(position);
double vector[3];
vector[0] = focalPoint[0] - position[0];
vector[1] = focalPoint[1] - position[1];
vector[2] = focalPoint[2] - position[2];
vtkMath::Normalize(vector);
focalPoint[0] = position[0] + distance*vector[0];
focalPoint[1] = position[1] + distance*vector[1];
focalPoint[2] = position[2] + distance*vector[2];
vtkImageData *data = 0;
if (this->JumpToNearestSlice &&
this->CurrentImageMatrix &&
this->CurrentImageMapper &&
(data = vtkImageData::SafeDownCast(this->CurrentImageMapper->GetInput())))
{
double point[4];
point[0] = focalPoint[0];
point[1] = focalPoint[1];
point[2] = focalPoint[2];
point[3] = 1.0;
double normal[4];
normal[0] = vector[0];
normal[1] = vector[1];
normal[2] = vector[2];
normal[3] = -vtkMath::Dot(point, normal);
// convert normal to data coordinates
double worldToData[16];
vtkMatrix4x4 *dataToWorld = this->CurrentImageMatrix;
vtkMatrix4x4::Transpose(*dataToWorld->Element, worldToData);
vtkMatrix4x4::MultiplyPoint(worldToData, normal, normal);
// find the slice orientation from the normal
int k = 0;
double maxsq = 0;
double sumsq = 0;
for (int i = 0; i < 3; i++)
{
double tmpsq = normal[i]*normal[i];
sumsq += tmpsq;
if (tmpsq > maxsq)
{
maxsq = tmpsq;
k = i;
}
}
// if the slice is not oblique
if ((1.0 - maxsq/sumsq) < 1e-12)
{
// get the point in data coordinates
vtkMatrix4x4::Invert(*dataToWorld->Element, worldToData);
vtkMatrix4x4::MultiplyPoint(worldToData, point, point);
double *spacing = data->GetSpacing();
double *origin = data->GetOrigin();
// set the point to lie exactly on a slice
double z = (point[k] - origin[k])/spacing[k];
if (z > VTK_INT_MIN && z < VTK_INT_MAX)
{
int j = vtkMath::Floor(z + 0.5);
point[k] = j*spacing[k] + origin[k];
}
// convert back to world coordinates
dataToWorld->MultiplyPoint(point, point);
if (point[3] != 0)
{
focalPoint[0] = point[0]/point[3];
focalPoint[1] = point[1]/point[3];
focalPoint[2] = point[2]/point[3];
}
}
}
camera->SetFocalPoint(focalPoint);
}
//----------------------------------------------------------------------------
void vtkSliceImageTool::AdvanceSlice(int delta)
{
vtkToolCursor *cursor = this->GetToolCursor();
vtkCamera *camera = cursor->GetRenderer()->GetActiveCamera();
// Get the camera information
double point[4], normal[4], position[3];
camera->GetFocalPoint(point);
camera->GetPosition(position);
normal[0] = point[0] - position[0];
normal[1] = point[1] - position[1];
normal[2] = point[2] - position[2];
vtkMath::Normalize(normal);
normal[3] = -vtkMath::Dot(point, normal);
// Convert the normal to data coordinates
if (this->CurrentImageMatrix)
{
double matrix[16];
vtkMatrix4x4::Transpose(*this->CurrentImageMatrix->Element, matrix);
vtkMatrix4x4::MultiplyPoint(matrix, normal, normal);
}
// Get the image information
vtkImageData *data = 0;
if (this->CurrentImageMapper)
{
data = vtkImageData::SafeDownCast(this->CurrentImageMapper->GetInput());
}
if (data)
{
// Compute the data bounds
double origin[3], spacing[3], bounds[6];
int extent[6];
data->GetOrigin(origin);
data->GetSpacing(spacing);
#if VTK_MAJOR_VERSION >= 6
data->GetExtent(extent);
vtkInformation *info = this->CurrentImageMapper->GetInputInformation(0, 0);
if (info &&
info->Has(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()))
{
info->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), extent);
}
#else
data->GetWholeExtent(extent);
#endif
bounds[0] = origin[0] + spacing[0]*extent[0];
bounds[1] = origin[0] + spacing[0]*extent[1];
bounds[2] = origin[1] + spacing[1]*extent[2];
bounds[3] = origin[1] + spacing[1]*extent[3];
bounds[4] = origin[2] + spacing[2]*extent[4];
bounds[5] = origin[2] + spacing[2]*extent[5];
// Compute the distance to each of the 8 corners of the data cube
int maxc = 0;
double maxdist = VTK_DOUBLE_MIN;
for (int c = 0; c < 8; c++)
{
double dist = (normal[0]*bounds[c&1] +
normal[1]*bounds[2+((c&2)>>1)] +
normal[2]*bounds[4+((c&4)>>2)] + normal[3]);
if (dist > maxdist)
{
maxdist = dist;
maxc = c;
}
}
// Compute the distance to the opposite corner
int minc = (maxc ^ 7);
double mindist = (normal[0]*bounds[minc&1] +
normal[1]*bounds[2+((minc&2)>>1)] +
normal[2]*bounds[4+((minc&4)>>2)] + normal[3]);
// Compute the spacing to use
double wx = normal[0]*normal[0];
double wy = normal[1]*normal[1];
double wz = normal[2]*normal[2];
double s = fabs(spacing[0])*wx + fabs(spacing[1])*wy + fabs(spacing[2])*wz;
// Round to get the nearest slice index
int n = vtkMath::Floor(maxdist/s + 0.5);
int nmax = vtkMath::Floor((maxdist - mindist)/s + 0.5);
n += delta;
// Apply some limits
n = (n < 0 ? 0 : n);
n = (n > nmax ? nmax : n);
// Adjust the plane
normal[3] -= n*s - maxdist;
}
// Convert the plane back to world coordinates
if (this->CurrentImageMatrix)
{
double matrix[16];
vtkMatrix4x4::Invert(*this->CurrentImageMatrix->Element, matrix);
vtkMatrix4x4::Transpose(matrix, matrix);
vtkMatrix4x4::MultiplyPoint(matrix, normal, normal);
}
// project the focal point onto this new plane
double f = vtkMath::Dot(normal, point) + normal[3];
point[0] += f*normal[0];
point[1] += f*normal[1];
point[2] += f*normal[2];
camera->SetFocalPoint(point);
}