forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkResliceCursorLineRepresentation.cxx
638 lines (524 loc) · 19.3 KB
/
vtkResliceCursorLineRepresentation.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*=========================================================================
Program: Visualization Toolkit
Module: vtkResliceCursorLineRepresentation.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
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 "vtkResliceCursorLineRepresentation.h"
#include "vtkHandleRepresentation.h"
#include "vtkCoordinate.h"
#include "vtkRenderer.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkLine.h"
#include "vtkTextProperty.h"
#include "vtkWindow.h"
#include "vtkResliceCursorPicker.h"
#include "vtkPlaneSource.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkCamera.h"
#include "vtkPlane.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkActor2D.h"
#include "vtkTextMapper.h"
#include "vtkTextProperty.h"
#include "vtkProperty2D.h"
#include "vtkPointHandleRepresentation2D.h"
#include "vtkObjectFactory.h"
#include "vtkInteractorObserver.h"
#include "vtkResliceCursorActor.h"
#include "vtkResliceCursorPolyDataAlgorithm.h"
#include "vtkResliceCursor.h"
#include "vtkBoundingBox.h"
#include "vtkMatrix4x4.h"
#include "vtkImageActor.h"
#include "vtkTextActor.h"
#include "vtkImageReslice.h"
#include "vtkSmartPointer.h"
#include <sstream>
vtkStandardNewMacro(vtkResliceCursorLineRepresentation);
//----------------------------------------------------------------------
vtkResliceCursorLineRepresentation::vtkResliceCursorLineRepresentation()
{
this->ResliceCursorActor = vtkResliceCursorActor::New();
this->Picker = vtkResliceCursorPicker::New();
this->Picker->SetTolerance(0.025);
this->MatrixReslice = vtkMatrix4x4::New();
this->MatrixView = vtkMatrix4x4::New();
this->MatrixReslicedView = vtkMatrix4x4::New();
}
//----------------------------------------------------------------------
vtkResliceCursorLineRepresentation::~vtkResliceCursorLineRepresentation()
{
this->ResliceCursorActor->Delete();
this->Picker->Delete();
this->MatrixReslice->Delete();
this->MatrixView->Delete();
this->MatrixReslicedView->Delete();
}
//----------------------------------------------------------------------
vtkResliceCursor * vtkResliceCursorLineRepresentation::GetResliceCursor()
{
return this->ResliceCursorActor->GetCursorAlgorithm()->GetResliceCursor();
}
//----------------------------------------------------------------------
int vtkResliceCursorLineRepresentation::
ComputeInteractionState(int X, int Y, int modify)
{
this->InteractionState = vtkResliceCursorLineRepresentation::Outside;
if (!this->Renderer)
{
return this->InteractionState;
}
vtkResliceCursor * rc = this->GetResliceCursor();
if (!rc)
{
vtkErrorMacro( << "Reslice cursor not set!" );
return this->InteractionState;
}
this->Modifier = modify;
// Ensure that the axis is initialized..
const int axis1 = this->ResliceCursorActor->GetCursorAlgorithm()->GetAxis1();
double bounds[6];
this->ResliceCursorActor->GetCenterlineActor(axis1)->GetBounds(bounds);
if (bounds[1] < bounds[0])
{
return this->InteractionState;
}
// Pick
this->Picker->SetResliceCursorAlgorithm(
this->ResliceCursorActor->GetCursorAlgorithm() );
int picked = this->Picker->Pick(X, Y, 0, this->Renderer );
const bool pickedAxis1 = this->Picker->GetPickedAxis1() ? true : false;
const bool pickedAxis2 = this->Picker->GetPickedAxis2() ? true : false;
const bool pickedCenter = this->Picker->GetPickedCenter() ? true : false;
if (picked)
{
this->Picker->GetPickPosition(this->StartPickPosition);
}
// Now assign the interaction state
if (pickedCenter)
{
this->InteractionState = vtkResliceCursorLineRepresentation::OnCenter;
}
else if (pickedAxis1)
{
this->InteractionState = vtkResliceCursorLineRepresentation::OnAxis1;
}
else if (pickedAxis2)
{
this->InteractionState = vtkResliceCursorLineRepresentation::OnAxis2;
}
return this->InteractionState;
}
//----------------------------------------------------------------------
// Record the current event position, and the center position.
void vtkResliceCursorLineRepresentation
::StartWidgetInteraction(double startEventPos[2])
{
this->StartEventPosition[0] = startEventPos[0];
this->StartEventPosition[1] = startEventPos[1];
if (this->ManipulationMode == WindowLevelling)
{
this->InitialWindow = this->CurrentWindow;
this->InitialLevel = this->CurrentLevel;
}
else
{
if ( vtkResliceCursor *rc = this->GetResliceCursor() )
{
rc->GetCenter(this->StartCenterPosition);
}
}
this->LastEventPosition[0] = startEventPos[0];
this->LastEventPosition[1] = startEventPos[1];
}
//----------------------------------------------------------------------
void vtkResliceCursorLineRepresentation::WidgetInteraction(double e[2])
{
vtkResliceCursor *rc = this->GetResliceCursor();
if (this->ManipulationMode == WindowLevelling)
{
this->WindowLevel(e[0], e[1]);
this->LastEventPosition[0] = e[0];
this->LastEventPosition[1] = e[1];
return;
}
// Depending on the state, different motions are allowed.
if ( this->InteractionState == Outside || ! this->Renderer || !rc )
{
this->LastEventPosition[0] = e[0];
this->LastEventPosition[1] = e[1];
return;
}
if (rc->GetThickMode() &&
this->ManipulationMode ==
vtkResliceCursorRepresentation::ResizeThickness)
{
double sf = 1.0;
// Compute the scale factor
int *size = this->Renderer->GetSize();
double dPos = e[1]-this->LastEventPosition[1];
sf *= (1.0 + 2.0*(dPos / size[1])); //scale factor of 2.0 is arbitrary
double thickness[3];
rc->GetThickness(thickness);
rc->SetThickness( thickness[0] * sf,
thickness[1] * sf,
thickness[2] * sf );
this->LastEventPosition[0] = e[0];
this->LastEventPosition[1] = e[1];
return;
}
// depending on the state, perform different operations
//
// 1. Translation
if ( this->InteractionState == OnCenter && !this->Modifier )
{
// Intersect with the viewing vector. We will use this point and the
// start event point to compute an offset vector to translate the
// center by.
double intersectionPos[3], newCenter[3];
this->Picker->Pick( e, intersectionPos, this->Renderer );
// Offset the center by this vector.
for (int i = 0; i < 3; i++)
{
newCenter[i] = this->StartCenterPosition[i] +
intersectionPos[i] - this->StartPickPosition[i];
}
rc->SetCenter(newCenter);
}
// 2. Rotation of axis 1
if ( this->InteractionState == OnAxis1 && !this->Modifier )
{
this->RotateAxis( e,
this->ResliceCursorActor->GetCursorAlgorithm()->GetPlaneAxis1() );
}
// 3. Rotation of axis 2
if ( this->InteractionState == OnAxis2 && !this->Modifier )
{
this->RotateAxis( e,
this->ResliceCursorActor->GetCursorAlgorithm()->GetPlaneAxis2() );
}
// 4. Rotation of both axes
if ( (this->InteractionState == OnAxis2 ||
this->InteractionState == OnAxis1) && this->Modifier )
{
// Rotate both by the same angle
const double angle = this->RotateAxis( e,
this->ResliceCursorActor->GetCursorAlgorithm()->GetPlaneAxis1() );
this->RotateAxis(
this->ResliceCursorActor->GetCursorAlgorithm()->GetPlaneAxis2(), angle );
}
this->LastEventPosition[0] = e[0];
this->LastEventPosition[1] = e[1];
}
//----------------------------------------------------------------------
double vtkResliceCursorLineRepresentation
::RotateAxis( double e[2], int axis )
{
vtkResliceCursor *rc = this->GetResliceCursor();
double center[3];
rc->GetCenter(center);
// Intersect with the viewing vector. We will use this point and the
// start event point to compute the rotation angle
double currIntersectionPos[3], lastIntersectionPos[3];
this->DisplayToReslicePlaneIntersection( e, currIntersectionPos );
this->DisplayToReslicePlaneIntersection(
this->LastEventPosition, lastIntersectionPos );
if (lastIntersectionPos[0] == currIntersectionPos[0] &&
lastIntersectionPos[1] == currIntersectionPos[1] &&
lastIntersectionPos[2] == currIntersectionPos[2])
{
return 0;
}
double lastVector[3], currVector[3];
for (int i = 0; i < 3; i++)
{
lastVector[i] = lastIntersectionPos[i] - center[i];
currVector[i] = currIntersectionPos[i] - center[i];
}
vtkMath::Normalize(lastVector);
vtkMath::Normalize(currVector);
// compute the angle betweem both vectors. This is the amount to
// rotate by.
double angle = acos(vtkMath::Dot(lastVector, currVector));
double crossVector[3];
vtkMath::Cross(lastVector, currVector, crossVector);
double aboutAxis[3];
const int rcPlaneIdx = this->ResliceCursorActor->
GetCursorAlgorithm()->GetReslicePlaneNormal();
vtkPlane *normalPlane = rc->GetPlane(rcPlaneIdx);
normalPlane->GetNormal(aboutAxis);
const double align = vtkMath::Dot(aboutAxis, crossVector);
const double sign = align > 0 ? 1.0 : -1.0;
angle *= sign;
if (angle == 0)
{
return 0;
}
this->RotateAxis( axis, angle );
return angle;
}
//----------------------------------------------------------------------
// Get the plane normal to the viewing axis.
vtkResliceCursorPolyDataAlgorithm *
vtkResliceCursorLineRepresentation::GetCursorAlgorithm()
{
return this->ResliceCursorActor->GetCursorAlgorithm();
}
//----------------------------------------------------------------------
void vtkResliceCursorLineRepresentation
::RotateAxis( int axis, double angle )
{
vtkResliceCursor * rc = this->GetResliceCursor();
vtkPlane * planeToBeRotated = rc->GetPlane(axis);
const int rcPlaneIdx = this->ResliceCursorActor->
GetCursorAlgorithm()->GetReslicePlaneNormal();
vtkPlane *normalPlane = rc->GetPlane(rcPlaneIdx);
double vectorToBeRotated[3], aboutAxis[3], rotatedVector[3];
planeToBeRotated->GetNormal(vectorToBeRotated);
normalPlane->GetNormal(aboutAxis);
this->RotateVectorAboutVector( vectorToBeRotated,
aboutAxis, angle, rotatedVector );
planeToBeRotated->SetNormal(rotatedVector);
}
//----------------------------------------------------------------------
void vtkResliceCursorLineRepresentation
::RotateVectorAboutVector( double vectorToBeRotated[3],
double axis[3], // vector about which we rotate
double angle, // angle in radians
double o[3] )
{
// let
// [v] = [vx, vy, vz] the vector to be rotated.
// [l] = [lx, ly, lz] the vector about rotation
// | 1 0 0|
// [i] = | 0 1 0| the identity matrix
// | 0 0 1|
//
// | 0 lz -ly |
// [L] = | -lz 0 lx |
// | ly -lx 0 |
//
// d = sqrt(lx*lx + ly*ly + lz*lz)
// a the angle of rotation
//
// then
//
// matrix operations gives:
//
// [v] = [v]x{[i] + sin(a)/d*[L] + ((1 - cos(a))/(d*d)*([L]x[L]))}
// normalize the axis vector
double v[3] = { vectorToBeRotated[0],
vectorToBeRotated[1], vectorToBeRotated[2] };
double l[3] = { axis[0], axis[1], axis[2] };
vtkMath::Normalize(v);
vtkMath::Normalize(l);
const double u = sin(angle);
const double w = 1.0 - cos(angle);
o[0] = v[0] * (1 - w * (l[2]*l[2] + l[1]*l[1])) +
v[1] * (-u * l[2] + w * l[0] * l[1]) +
v[2] * (u * l[1] + w * l[0] * l[1]);
o[1] = v[0] * (u * l[2] + w * l[0] * l[1]) +
v[1] * (1 - w * (l[0]*l[0] + l[2]*l[2])) +
v[2] * (-u * l[0] + w * l[1] * l[2] );
o[2] = v[0] * (-u * l[1] + w * l[0] * l[2]) +
v[1] * (u * l[0] + w * l[1] * l[2]) +
v[2] * (1 - w * (l[1]*l[1] + l[0]*l[0]));
}
//----------------------------------------------------------------------
int vtkResliceCursorLineRepresentation
::DisplayToReslicePlaneIntersection(
double displayPos[2], double intersectionPos[3] )
{
// First compute the equivalent of this display point on the focal plane
double fp[4], tmp1[4], camPos[4], eventFPpos[4];
this->Renderer->GetActiveCamera()->GetFocalPoint(fp);
this->Renderer->GetActiveCamera()->GetPosition(camPos);
fp[3] = 1.0;
this->Renderer->SetWorldPoint(fp);
this->Renderer->WorldToDisplay();
this->Renderer->GetDisplayPoint(tmp1);
tmp1[0] = displayPos[0];
tmp1[1] = displayPos[1];
this->Renderer->SetDisplayPoint(tmp1);
this->Renderer->DisplayToWorld();
this->Renderer->GetWorldPoint(eventFPpos);
const int rcPlaneIdx = this->ResliceCursorActor->
GetCursorAlgorithm()->GetReslicePlaneNormal();
vtkPlane *normalPlane = this->GetResliceCursor()->GetPlane(rcPlaneIdx);
double t;
return normalPlane->IntersectWithLine(eventFPpos, camPos, t, intersectionPos);
}
//----------------------------------------------------------------------
void vtkResliceCursorLineRepresentation::BuildRepresentation()
{
if ( this->GetMTime() > this->BuildTime ||
this->GetResliceCursor()->GetMTime() > this->BuildTime ||
(this->Renderer && this->Renderer->GetVTKWindow() &&
this->Renderer->GetVTKWindow()->GetMTime() > this->BuildTime) )
{
this->BuildTime.Modified();
}
this->Superclass::BuildRepresentation();
}
//----------------------------------------------------------------------
void vtkResliceCursorLineRepresentation::ReleaseGraphicsResources(vtkWindow *w)
{
this->ResliceCursorActor->ReleaseGraphicsResources(w);
this->TexturePlaneActor->ReleaseGraphicsResources(w);
this->ImageActor->ReleaseGraphicsResources(w);
this->TextActor->ReleaseGraphicsResources(w);
}
//----------------------------------------------------------------------
int vtkResliceCursorLineRepresentation::RenderOverlay(vtkViewport *viewport)
{
int count = 0;
if (this->TexturePlaneActor->GetVisibility() && !this->UseImageActor)
{
count += this->TexturePlaneActor->RenderOverlay(viewport);
}
if (this->ImageActor->GetVisibility() && this->UseImageActor)
{
count += this->ImageActor->RenderOverlay(viewport);
}
if (this->DisplayText && this->TextActor->GetVisibility())
{
count += this->TextActor->RenderOverlay(viewport);
}
return count;
}
//----------------------------------------------------------------------
void vtkResliceCursorLineRepresentation::SetUserMatrix(vtkMatrix4x4 *m)
{
this->TexturePlaneActor->SetUserMatrix(m);
this->ResliceCursorActor->SetUserMatrix(m);
}
//----------------------------------------------------------------------
int vtkResliceCursorLineRepresentation
::RenderOpaqueGeometry(vtkViewport *viewport)
{
this->BuildRepresentation();
const int normalAxis = this->ResliceCursorActor->
GetCursorAlgorithm()->GetReslicePlaneNormal();
// When the reslice plane is changed, update the camera to look at the
// normal to the reslice plane always.
double fp[3], cp[3], n[3];
this->Renderer->GetActiveCamera()->GetFocalPoint(fp);
this->Renderer->GetActiveCamera()->GetPosition(cp);
this->GetResliceCursor()->GetPlane(normalAxis)->GetNormal(n);
const double d = sqrt(vtkMath::Distance2BetweenPoints(cp,fp));
double newCamPos[3] = { fp[0] + (d * n[0]),
fp[1] + (d * n[1]),
fp[2] + (d * n[2]) };
this->Renderer->GetActiveCamera()->SetPosition(newCamPos);
// intersect with the plane to get updated focal point
double intersectionPos[3], t;
this->GetResliceCursor()->GetPlane(normalAxis)->
IntersectWithLine(fp, newCamPos, t, intersectionPos);
this->Renderer->GetActiveCamera()->SetFocalPoint(intersectionPos);
// Don't clip away any part of the data.
this->Renderer->ResetCameraClippingRange();
// Now Render all the actors.
int count = 0;
if (this->TexturePlaneActor->GetVisibility() && !this->UseImageActor)
{
count += this->TexturePlaneActor->RenderOpaqueGeometry(viewport);
}
if (this->ImageActor->GetVisibility() && this->UseImageActor)
{
count += this->ImageActor->RenderOpaqueGeometry(viewport);
}
count += this->ResliceCursorActor->RenderOpaqueGeometry(viewport);
if (this->DisplayText && this->TextActor->GetVisibility())
{
count += this->TextActor->RenderOpaqueGeometry(viewport);
}
return count;
}
//-------------------------------------------------------------------------
// Get the bounds for this Actor as (Xmin,Xmax,Ymin,Ymax,Zmin,Zmax).
double *vtkResliceCursorLineRepresentation::GetBounds()
{
vtkMath::UninitializeBounds(this->InitialBounds);
if (vtkResliceCursor *r = this->GetResliceCursor())
{
r->GetImage()->GetBounds(this->InitialBounds);
}
//vtkBoundingBox *bb = new vtkBoundingBox();
//bb->AddBounds(this->ResliceCursorActor->GetBounds());
//bb->AddBounds(this->TexturePlaneActor->GetBounds());
//bb->GetBounds(bounds);
//delete bb;
return this->InitialBounds;
}
//-----------------------------------------------------------------------------
int vtkResliceCursorLineRepresentation::RenderTranslucentPolygonalGeometry(
vtkViewport *viewport)
{
int count = 0;
if (this->TexturePlaneActor->GetVisibility() && !this->UseImageActor)
{
count +=
this->TexturePlaneActor->RenderTranslucentPolygonalGeometry(viewport);
}
if (this->ImageActor->GetVisibility() && this->UseImageActor)
{
count +=
this->ImageActor->RenderTranslucentPolygonalGeometry(viewport);
}
count += this->ResliceCursorActor->
RenderTranslucentPolygonalGeometry(viewport);
return count;
}
//-----------------------------------------------------------------------------
vtkTypeBool vtkResliceCursorLineRepresentation::HasTranslucentPolygonalGeometry()
{
return (this->ResliceCursorActor->HasTranslucentPolygonalGeometry() ||
(this->ImageActor->HasTranslucentPolygonalGeometry()
&& this->UseImageActor) ||
(this->TexturePlaneActor->HasTranslucentPolygonalGeometry()
&& !this->UseImageActor)) ? 1 : 0;
}
//----------------------------------------------------------------------
void vtkResliceCursorLineRepresentation::Highlight(int )
{
}
//----------------------------------------------------------------------
void vtkResliceCursorLineRepresentation::PrintSelf(ostream& os, vtkIndent indent)
{
//Superclass typedef defined in vtkTypeMacro() found in vtkSetGet.h
this->Superclass::PrintSelf(os,indent);
os << indent << "ResliceCursorActor: " << this->ResliceCursorActor << "\n";
if (this->ResliceCursorActor)
{
this->ResliceCursorActor->PrintSelf(os, indent);
}
os << indent << "Picker: " << this->Picker << "\n";
if (this->Picker)
{
this->Picker->PrintSelf(os, indent);
}
os << indent << "MatrixReslicedView: " << this->MatrixReslicedView << "\n";
if (this->MatrixReslicedView)
{
this->MatrixReslicedView->PrintSelf(os, indent);
}
os << indent << "MatrixView: " << this->MatrixView << "\n";
if (this->MatrixView)
{
this->MatrixView->PrintSelf(os, indent);
}
os << indent << "MatrixReslice: " << this->MatrixReslice << "\n";
if (this->MatrixReslice)
{
this->MatrixReslice->PrintSelf(os, indent);
}
// this->StartPickPosition;
// this->StartCenterPosition;
}