70 #include "vtkSmartPointer.h"
72 #include "vtkCamera.h"
73 #include "vtkCommand.h"
74 #include "vtkImageActor.h"
75 #include "vtkImageData.h"
76 #include "vtkImageMapToWindowLevelColors.h"
77 #include "vtkInteractorStyleImage.h"
78 #include "vtkObjectFactory.h"
79 #include "vtkRenderWindow.h"
80 #include "vtkRenderWindowInteractor.h"
81 #include "vtkRenderer.h"
82 #include "vtkMatrix4x4.h"
83 #include "vtkTransform.h"
84 #include "vtkScalarBarActor.h"
86 #include "vtkCornerAnnotation.h"
87 #include "vtkTextProperty.h"
88 #include "vtkLookupTable.h"
94 #include "vtkPolyDataMapper.h"
95 #include "vtkPoints.h"
96 #include "vtkIdList.h"
97 #include "vtkOutlineSource.h"
98 #include "vtkMatrixToLinearTransform.h"
99 #include "vtkPointData.h"
100 #include "vtkUnsignedCharArray.h"
101 #include "vtkIntArray.h"
102 #include "vtkImageAccumulate.h"
105 #include "vtkProperty.h"
108 #include <vtkProperty2D.h>
109 #include <vtkAxisActor2D.h>
110 #include <vtkCursor2D.h>
111 #include <vtkPointHandleRepresentation2D.h>
113 #include <vtkExtractPolyDataGeometry.h>
159 this->
Cursor->ActiveRepresentationOff();
161 this->
Cursor->GetProperty()->SetColor (0.9, 0.9, 0.1);
162 this->
Cursor->SetVisibility (0);
164 this->Renderer->AddViewProp(this->
Cursor);
166 this->ShowAnnotationsOn();
201 int x_watcher, y_watcher, z_watcher;
203 switch ( convention )
254 unsigned int sliceorientation = 2;
256 for (
unsigned int i = 0; i < 3; i++ )
258 if ( dot < fabs( this->GetOrientationMatrix()->GetElement(orientation, i) ) )
260 dot = fabs( this->GetOrientationMatrix()->GetElement(orientation, i) );
261 sliceorientation = i;
264 this->SetSliceOrientation(sliceorientation);
281 points->InsertNextPoint(0, 0, 0);
282 points->InsertNextPoint(1, 0, 0);
283 points->InsertNextPoint(0, 1, 0);
284 points->InsertNextPoint(1, 1, 0);
288 pts[0] = 0; pts[1] = 1; pts[2] = 3; pts[3] = 2;
289 this->
SlicePlane->InsertNextCell(VTK_QUAD, 4, pts);
290 pts[0] = 0; pts[1] = 2; pts[2] = 3; pts[3] = 1;
291 this->
SlicePlane->InsertNextCell(VTK_QUAD, 4, pts);
293 vtkSmartPointer< vtkUnsignedCharArray > array =
295 array->SetName(
"Colors");
296 array->SetNumberOfComponents(3);
297 unsigned char vals[3];
298 vals[0] = 255; vals[1] = 0; vals[2] = 0;
299 array->InsertNextTupleValue(vals);
300 array->InsertNextTupleValue(vals);
301 array->InsertNextTupleValue(vals);
302 array->InsertNextTupleValue(vals);
304 this->
SlicePlane->GetPointData()->SetScalars(array);
326 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
333 double position[4], focalpoint[4], viewup[4];
335 double conventionposition[4];
336 conventionposition[0] = 0.;
337 conventionposition[1] = 0.;
338 conventionposition[2] = 0.;
339 conventionposition[3] = 0.;
341 double conventionview[4];
342 double focaltoposition[3];
343 double abs_focaltoposition[3];
344 std::vector< double * > viewupchoices;
345 double first[3], second[3], third[3], fourth[3];
350 for ( i = 0; i < 3; i++ )
352 position[i] = cam->GetPosition()[i];
353 focalpoint[i] = cam->GetFocalPoint()[i];
356 this->SliceOrientation);
361 conventionview[3] = 0;
365 this->GetOrientationMatrix()->MultiplyPoint(position, position);
366 this->GetOrientationMatrix()->MultiplyPoint(focalpoint, focalpoint);
367 this->GetOrientationMatrix()->MultiplyPoint(conventionview, conventionview);
368 this->GetOrientationMatrix()->MultiplyPoint(conventionposition,
372 for ( i = 0; i < 3; i++ )
374 focaltoposition[i] = position[i] - focalpoint[i];
375 abs_focaltoposition[i] = fabs(focaltoposition[i]);
380 if ( vtkMath::Dot(focaltoposition, conventionposition) < 0 )
382 for ( i = 0; i < 3; i++ )
384 position[i] -= 2 * focaltoposition[i];
389 for ( i = 0; i < 3; i++ )
391 first[i] = conventionview[i];
392 second[i] = -conventionview[i];
395 vtkMath::Cross(first, focaltoposition, third);
396 vtkMath::Cross(second, focaltoposition, fourth);
397 vtkMath::Normalize(third);
398 vtkMath::Normalize(fourth);
400 viewupchoices.push_back(first);
401 viewupchoices.push_back(second);
402 viewupchoices.push_back(third);
403 viewupchoices.push_back(fourth);
409 for ( i = 0; i < 3; i++ )
411 if ( dot < fabs(focaltoposition[i]) )
413 dot = fabs(focaltoposition[i]);
420 for ( i = 0; i < 3; i++ )
427 unsigned int id2 = 0;
429 for ( i = 0; i < viewupchoices.size(); i++ )
431 if ( dot2 < vtkMath::Dot(viewupchoices[i], conventionview) )
433 dot2 = vtkMath::Dot(viewupchoices[i], conventionview);
439 for ( i = 0; i < 3; i++ )
441 viewup[i] = viewupchoices[id2][i];
444 cam->SetPosition(position[0], position[1], position[2]);
445 cam->SetFocalPoint(focalpoint[0], focalpoint[1], focalpoint[2]);
446 cam->SetViewUp(viewup[0], viewup[1], viewup[2]);
450 double view_plane_normal[3];
451 cam->GetViewPlaneNormal(view_plane_normal);
466 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
473 std::string solution[4];
477 double *viewup = cam->GetViewUp();
478 double *normal = cam->GetViewPlaneNormal();
479 double rightvector[3];
480 vtkMath::Cross(normal, viewup, rightvector);
482 unsigned int id1 = 0;
483 unsigned int id2 = 0;
489 for (
unsigned int i = 0; i < 3; i++ )
491 if ( dot1 <= fabs(viewup[i]) )
493 dot1 = fabs(viewup[i]);
496 if ( dot2 <= fabs(rightvector[i]) )
498 dot2 = fabs(rightvector[i]);
501 if ( dot3 <= fabs(normal[i]) )
503 dot3 = fabs(normal[i]);
508 if ( viewup[id1] > 0 )
518 if ( rightvector[id2] > 0 )
529 for (
unsigned int i = 0; i < 4; i++ )
539 unsigned char vals[3] = { 0, 0, 0 };
543 vtkUnsignedCharArray *array = vtkUnsignedCharArray::SafeDownCast(
544 this->
SlicePlane->GetPointData()->GetScalars() );
551 array->SetTupleValue(0, vals);
552 array->SetTupleValue(1, vals);
553 array->SetTupleValue(2, vals);
554 array->SetTupleValue(3, vals);
561 if ( slice != this->Slice )
563 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
582 double * bounds = this->ImageActor->GetDisplayBounds();
586 for (
unsigned int i = 0; i < 4; i++ )
588 added1 = ( !( i % 2 ) ) ? 1 : 0;
589 added2 = ( i < 2 ) ? 1 : 0;
590 x[( this->SliceOrientation + 1 ) % 3] =
591 bounds[2 * ( ( this->SliceOrientation + 1 ) % 3 ) + added1];
592 x[( this->SliceOrientation + 2 ) % 3] =
593 bounds[2 * ( ( this->SliceOrientation + 2 ) % 3 ) + added2];
594 x[this->SliceOrientation] = bounds[2 * this->SliceOrientation];
595 oldpoints->InsertPoint(i, x);
601 points->GetPoint(0, x);
609 int xy[2] = { x, y };
619 int slice = indices[this->SliceOrientation];
630 int indices[3] = { slice, slice, slice };
641 if ( !this->GetInput() )
646 int *range = this->GetSliceRange();
649 this->
SetSlice( ( range[1] + range[0] ) / 2 );
660 this->GetInput()->GetBounds(bounds);
663 focal[0] = 0.5 * ( bounds[0] + bounds[1] );
664 focal[1] = 0.5 * ( bounds[2] + bounds[3] );
665 focal[2] = 0.5 * ( bounds[4] + bounds[5] );
673 length[0] = 0.5 * ( bounds[1] - bounds[0] );
674 length[1] = 0.5 * ( bounds[3] - bounds[2] );
675 length[2] = 0.5 * ( bounds[5] - bounds[4] );
677 double radius = 0.75 * sqrt(length[0] * length[0]
678 + length[1] * length[1]
679 + length[2] * length[2]);
681 vtkCamera *camera = this->GetRenderer()->GetActiveCamera();
683 radius / sin(camera->GetViewAngle() * vtkMath::Pi() / 360.0);
685 double view_plane_normal[3];
686 camera->GetViewPlaneNormal(view_plane_normal);
687 double *vup = camera->GetViewUp();
689 if ( fabs( vtkMath::Dot(vup, view_plane_normal) ) > 0.999 )
691 vtkWarningMacro(<<
"Resetting view-up since view plane normal is parallel");
692 camera->SetViewUp(-vup[2], vup[0], vup[1]);
695 pos[0] += distance * view_plane_normal[0];
696 pos[1] += distance * view_plane_normal[1];
697 pos[2] += distance * view_plane_normal[2];
701 double max_length = std::max( length[0], std::max(length[1], length[2]) );
702 bounds[0] = focal[0] - 50. * max_length;
703 bounds[1] = focal[0] + 50. * max_length;
704 bounds[2] = focal[1] - 50. * max_length;
705 bounds[3] = focal[1] + 50. * max_length;
706 bounds[4] = focal[2] - 50. * max_length;
707 bounds[5] = focal[2] + 50. * max_length;
708 this->GetRenderer()->ResetCameraClippingRange(bounds);
710 camera->SetParallelScale(radius);
726 vtkRenderer *ren = this->GetRenderer();
732 vtkCamera *camera = ren->GetActiveCamera();
739 camera->SetFocalPoint(focal[0], focal[1], focal[2]);
740 camera->SetPosition(pos[0], pos[1], pos[2]);
747 vtkRenderer *ren = this->GetRenderer();
753 vtkCamera *camera = ren->GetActiveCamera();
760 camera->GetPosition(pos);
761 camera->GetFocalPoint(focal);
772 if ( !this->GetInput() || !this->GetRenderer() )
775 double *nullpos =
new double[3];
776 nullpos[0] = 0; nullpos[1] = 0; nullpos[2] = 0;
781 this->GetRenderer()->SetWorldPoint(slicepos[0], slicepos[1], slicepos[2],
783 this->GetRenderer()->WorldToDisplay();
784 this->GetRenderer()->SetDisplayPoint(xy[0], xy[1],
785 this->GetRenderer()->GetDisplayPoint()[2]);
786 this->GetRenderer()->DisplayToWorld();
791 return this->GetRenderer()->GetWorldPoint();
798 if ( this->RenderWindow && this->Renderer )
800 if ( !this->RenderWindow->HasRenderer(this->Renderer) )
802 this->RenderWindow->AddRenderer(this->Renderer);
806 if ( this->Interactor )
808 if ( !this->InteractorStyle )
811 this->Interactor->SetInteractorStyle(this->InteractorStyle);
812 this->InteractorStyle->AddObserver(
813 vtkCommand::KeyPressEvent, this->
Command);
814 this->InteractorStyle->AddObserver(
816 this->InteractorStyle->AddObserver(
818 this->InteractorStyle->AddObserver(
820 this->InteractorStyle->AddObserver(
822 this->InteractorStyle->AddObserver(
824 this->InteractorStyle->AddObserver(
826 this->InteractorStyle->AddObserver(
827 vtkCommand::InteractionEvent, this->
Command);
828 this->InteractorStyle->AddObserver(
830 this->InteractorStyle->AddObserver(
832 this->InteractorStyle->AddObserver(
835 this->InteractorStyle->AddObserver(
836 vtkCommand::StartWindowLevelEvent, this->
Command);
837 this->InteractorStyle->AddObserver(
838 vtkCommand::WindowLevelEvent, this->
Command);
841 this->Interactor->SetInteractorStyle(this->InteractorStyle);
842 this->Interactor->SetRenderWindow(this->RenderWindow);
845 if ( this->Renderer && this->ImageActor )
847 this->ImageActor->SetInterpolate(
false);
850 if ( this->Renderer && this->ImageActor )
852 if ( !this->Renderer->GetActors()->IsItemPresent(this->ImageActor) )
854 this->Renderer->AddViewProp(this->ImageActor);
858 if ( this->ImageActor && this->WindowLevel )
860 this->ImageActor->SetInput( this->WindowLevel->GetOutput() );
868 if ( this->ImageActor )
870 this->ImageActor->SetInterpolate(val);
878 if ( this->ImageActor )
880 return this->ImageActor->GetInterpolate();
892 vtkProperty *property,
893 const bool & intersection,
894 const bool & iDataVisibility)
898 if ( !this->Renderer )
912 if ( dataset->GetNumberOfPoints() <= 0 )
917 vtkSmartPointer<vtkPolyDataMapper> mapper =
919 mapper->SetScalarVisibility(iDataVisibility);
920 mapper->ImmediateModeRenderingOn();
923 double *bounds = dataset->GetBounds();
928 if ( ( ( bounds[0] == bounds[1] ) && ( normal[1] == 0 ) && ( normal[2] == 0 ) )
929 || ( ( bounds[2] == bounds[3] ) && ( normal[2] == 0 ) && ( normal[0] == 0 ) )
930 || ( ( bounds[4] == bounds[5] ) && ( normal[0] == 0 ) && ( normal[1] == 0 ) ) )
932 vtkSmartPointer<vtkExtractPolyDataGeometry> extracter =
934 extracter->SetInput(dataset);
937 mapper->SetInput( extracter->GetOutput() );
944 vtkSmartPointer<vtkPlaneCutter> cutter =
946 cutter->SetInput(dataset);
948 mapper->SetInput( cutter->GetOutput() );
952 mapper->SetInput(dataset);
957 actor->SetMapper(mapper);
961 actor->SetProperty(property);
963 actor->GetProperty()->BackfaceCullingOn();
966 this->Renderer->AddViewProp(actor);
973 std::map<double, vtkActor *>
977 std::map<double, vtkActor*> contours;
980 double normal[3] = {0., 0., 0.};
982 switch (iOrientation)
1005 double origin[3] = {0., 0., 0.};
1006 origin[0] = iDataSet->GetCenter()[0];
1007 origin[1] = iDataSet->GetCenter()[1];
1008 origin[2] = iDataSet->GetCenter()[2];
1010 double position = iDataSet->GetBounds()[4 - 2*iOrientation];
1011 double maxPosition = iDataSet->GetBounds()[5 - 2*iOrientation];
1018 double spacing = this->GetInput()->GetSpacing()[2-iOrientation];
1020 while( position < maxPosition)
1022 origin[2-iOrientation] = position;
1037 plane->SetNormal(normal);
1038 plane->SetOrigin(origin);
1042 cutter->SetInput(iDataSet);
1043 cutter->SetCutFunction(plane);
1048 mapper->SetInput( cutter->GetOutput() );
1052 actor->SetMapper(mapper);
1053 actor->VisibilityOn();
1056 contours[position] = actor;
1059 position += spacing;
1070 if ( !this->GetInput() )
1076 if ( !this->Renderer )
1084 int *dimensions = this->GetInput()->GetDimensions();
1086 int indices[3] = { 0, 0, 0 };
1087 for (
unsigned int i = 0; i < 3; i++ )
1089 indices[i] = (int)( (
double)dimensions[i] / 2.0 );
1091 indices[this->SliceOrientation] = this->GetSlice();
1095 for (
unsigned int i = 0; i < 3; i++ )
1124 unsigned char vals[3] = { 0, 0, 0 };
1127 vtkUnsignedCharArray *array = vtkUnsignedCharArray::SafeDownCast ( this->
SlicePlane->GetPointData()->GetScalars() );
1128 if ( !array ) {
return; }
1129 array->SetTupleValue (0, vals);
1130 array->SetTupleValue (1, vals);
1131 array->SetTupleValue (2, vals);
1132 array->SetTupleValue (3, vals);
1145 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1147 if ( !cam ) {
return -1; }
1149 double position[4], focalpoint[4], viewup[4];
1150 double conventionposition[4];
1151 double conventionview[4];
1152 double focaltoposition[3];
1153 std::vector< double * > viewupchoices;
1154 double first[3], second[3], third[3], fourth[3];
1155 bool inverseposition;
1159 for (
unsigned int i = 0; i < 3; i++ )
1161 position[i] = cam->GetPosition()[i];
1162 focalpoint[i] = cam->GetFocalPoint()[i];
1164 conventionview[i] = this->
ConventionMatrix->GetElement (i, this->SliceOrientation);
1169 conventionview[3] = 0;
1173 if ( this->GetOrientationMatrix() )
1175 this->GetOrientationMatrix()->MultiplyPoint (position, position);
1176 this->GetOrientationMatrix()->MultiplyPoint (focalpoint, focalpoint);
1177 this->GetOrientationMatrix()->MultiplyPoint (conventionview, conventionview);
1178 this->GetOrientationMatrix()->MultiplyPoint (conventionposition, conventionposition);
1182 for (
unsigned int i = 0; i < 3; i++ )
1184 focaltoposition[i] = position[i] - focalpoint[i];
1189 inverseposition = ( vtkMath::Dot (focaltoposition, conventionposition) < 0 );
1190 if ( inverseposition )
1192 for (
unsigned int i = 0; i < 3; i++ )
1194 position[i] -= 2 * focaltoposition[i];
1199 for (
unsigned int i = 0; i < 3; i++ )
1201 first[i] = conventionview[i];
1202 second[i] = -conventionview[i];
1205 vtkMath::Cross (first, focaltoposition, third);
1206 vtkMath::Cross (second, focaltoposition, fourth);
1207 vtkMath::Normalize (third);
1208 vtkMath::Normalize (fourth);
1210 viewupchoices.push_back (first);
1211 viewupchoices.push_back (second);
1212 viewupchoices.push_back (third);
1213 viewupchoices.push_back (fourth);
1217 unsigned int id = 0;
1219 for (
unsigned int i = 0; i < 3; i++ )
1221 if ( dot < std::abs (focaltoposition[i]) )
1223 dot = std::abs (focaltoposition[i]);
1230 for (
unsigned int i = 0; i < 3; i++ )
1237 unsigned int id2 = 0;
1239 for (
unsigned int i = 0; i < viewupchoices.size(); i++ )
1241 if ( dot2 < vtkMath::Dot (viewupchoices[i], conventionview) )
1243 dot2 = vtkMath::Dot (viewupchoices[i], conventionview);
1249 for (
unsigned int i = 0; i < 3; i++ )
1251 viewup[i] = viewupchoices[id2][i];
1254 cam->SetPosition(position[0], position[1], position[2]);
1255 cam->SetFocalPoint(focalpoint[0], focalpoint[1], focalpoint[2]);
1256 cam->SetViewUp(viewup[0], viewup[1], viewup[2]);
1270 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1272 if ( !cam ) {
return; }
1274 std::string matrix[3][2];
1275 matrix[0][0] =
"R"; matrix[0][1] =
"L";
1276 matrix[1][0] =
"A"; matrix[1][1] =
"P";
1277 matrix[2][0] =
"I"; matrix[2][1] =
"S";
1279 std::string solution[4];
1283 double *viewup = cam->GetViewUp();
1284 double *normal = cam->GetViewPlaneNormal();
1285 double rightvector[3];
1286 vtkMath::Cross (normal, viewup, rightvector);
1288 unsigned int id1 = 0;
1289 unsigned int id2 = 0;
1295 for (
unsigned int i = 0; i < 3; i++ )
1297 if ( dot1 <= std::abs (viewup[i]) )
1299 dot1 = std::abs (viewup[i]);
1302 if ( dot2 <= std::abs (rightvector[i]) )
1304 dot2 = std::abs (rightvector[i]);
1307 if ( dot3 <= std::abs (normal[i]) )
1309 dot3 = std::abs (normal[i]);
1314 if ( viewup[id1] > 0 )
1316 solution[3] = matrix[id1][0];
1317 solution[1] = matrix[id1][1];
1321 solution[3] = matrix[id1][1];
1322 solution[1] = matrix[id1][0];
1324 if ( rightvector[id2] > 0 )
1326 solution[0] = matrix[id2][0];
1327 solution[2] = matrix[id2][1];
1331 solution[0] = matrix[id2][1];
1332 solution[2] = matrix[id2][0];
1335 for (
unsigned int i = 0; i < 4; i++ )
1340 if ( this->GetInput() )
1347 double Xaxis[4] = { 0, 0, 0, 0 };
1348 double Yaxis[4] = { 0, 0, 0, 0 };
1349 for (
unsigned int i = 0; i < 3; i++ )
1351 Xaxis[i] = rightvector[i];
1352 Yaxis[i] = viewup[i];
1356 inverse->Identity();
1357 if ( this->GetOrientationMatrix() )
1359 vtkMatrix4x4::Invert (this->GetOrientationMatrix(), inverse);
1362 inverse->MultiplyPoint (Xaxis, Xaxis);
1363 inverse->MultiplyPoint (Yaxis, Yaxis);
1371 for (
unsigned int i = 0; i < 3; i++ )
1373 if ( dotX <= std::abs (Xaxis[i]) )
1375 dotX = std::abs (Xaxis[i]);
1378 if ( dotY <= std::abs (Yaxis[i]) )
1380 dotY = std::abs (Yaxis[i]);
1385 int * dimensions = this->GetInput()->GetDimensions();
1386 double *spacing = this->GetInput()->GetSpacing();
1389 "Image Size: %i x %i\nVoxel Size: %g x %g mm",
1390 dimensions[idX], dimensions[idY],
1391 spacing[idX], spacing[idY]);
1393 "<slice_and_max>\n<window>\n<level>");
1404 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1406 if ( !cam ) {
return; }
1408 double *position = cam->GetPosition();
1409 double *focalpoint = cam->GetFocalPoint();
1411 double focaltoposition[3];
1416 for ( i = 0; i < 3; i++ )
1418 focaltoposition[i] = fabs(position[i] - focalpoint[i]);
1429 this->GetRenderer()->WorldToDisplay();
1431 double *xy = this->GetRenderer()->GetDisplayPoint();
1432 this->GetCursor()->SetDisplayPosition (xy);
1436 "%s\nXYZ: %4.2f, %4.2f, %4.2f mm\nValue: %g",