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
|
/*=========================================================================
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/VolViewCopyright.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 "vtkKWProbeImageWidget.h"
#include "vtkCamera.h"
#include "vtkCornerAnnotation.h"
#include "vtkImageActor.h"
#include "vtkImageData.h"
#include "vtkImageReslice.h"
#include "vtkKWCursorWidget.h"
#include "vtkKWEvent.h"
#include "vtkKWMenu.h"
#include "vtkKWIcon.h"
#include "vtkKWImageMapToWindowLevelColors.h"
#include "vtkKWInternationalization.h"
#include "vtkKWWindow.h"
#include "vtkKWEventMap.h"
#include "vtkMath.h"
#include "vtkMatrix4x4.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkPolyDataAlgorithm.h"
#include "vtkAlgorithmOutput.h"
#include "vtkCutter.h"
#include "vtkPlane.h"
#include "vtkTransform.h"
#include "vtkKWOrientationWidget.h"
#include "vtkKWInteractorStyleImageView.h"
#include <vtksys/stl/string>
vtkStandardNewMacro(vtkKWProbeImageWidget);
vtkCxxRevisionMacro(vtkKWProbeImageWidget, "$Revision: 1.60 $");
#ifdef KWCommonPro_USE_XML_RW
#include "XML/vtkXMLKWProbeImageWidgetReader.h"
#include "XML/vtkXMLKWProbeImageWidgetWriter.h"
#endif
vtkKWGetXMLReaderWriterObjectsImplementationMacro(vtkKWProbeImageWidget, vtkXMLKWProbeImageWidgetReader, vtkXMLKWProbeImageWidgetWriter);
//----------------------------------------------------------------------------
vtkKWProbeImageWidget::vtkKWProbeImageWidget()
{
this->CornerAnnotation->ShowSliceAndImageOff();
this->HasSliceControl = 0;
this->ProbeInputAlgorithm = 0;
this->ImageReslice = vtkImageReslice::New();
this->ImageReslice->TransformInputSamplingOff();
this->ImageReslice->SetInterpolationModeToLinear();
this->ImageReslice->SetOutputDimensionality(2);
this->ImageReslice->AutoCropOutputOn();
// ImplicitPlaneWdg default are changed, need to update ImageReslice for Axial view
this->ImageReslice->SetResliceAxesDirectionCosines(1,0,0,0,-1,0,0,0,1);
this->ImageReslice->OptimizationOn ();
this->Image->SetVisibility(1);
this->Transform = vtkTransform::New();
}
//----------------------------------------------------------------------------
vtkKWProbeImageWidget::~vtkKWProbeImageWidget()
{
this->SetProbeInputAlgorithm(NULL);
this->ImageReslice->Delete();
this->Transform->Delete();
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::SetProbeInputAlgorithm(vtkPolyDataAlgorithm *arg)
{
if (this->ProbeInputAlgorithm == arg)
{
return;
}
if (this->ProbeInputAlgorithm)
{
this->ProbeInputAlgorithm->UnRegister(this);
}
this->ProbeInputAlgorithm = arg;
if (this->ProbeInputAlgorithm)
{
this->ProbeInputAlgorithm->Register(this);
}
this->ConnectInternalPipeline();
this->Modified();
}
//----------------------------------------------------------------------------
int vtkKWProbeImageWidget::ConnectInternalPipeline()
{
// Have a look at the documentation for this method (see .h).
if (!this->Superclass::ConnectInternalPipeline())
{
return 0;
}
if (this->ProbeInputAlgorithm)
{
this->ImageReslice->SetInput(this->Input);
vtkKWImageMapToWindowLevelColors *map = this->GetImageMapToRGBA();
if (map)
{
map->SetInput(this->ImageReslice->GetOutput());
this->Image->SetInput(map->GetOutput());
}
}
return 1;
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::ConnectImageMapToRGBA()
{
if (!this->ProbeInputAlgorithm)
{
this->Superclass::ConnectImageMapToRGBA();
return;
}
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::CreateWidget()
{
if (this->IsCreated())
{
vtkErrorMacro("widget already created " << this->GetClassName());
return;
}
this->Superclass::CreateWidget();
// The axis labels don't make sense for off-axis slices through the volume.
this->SetSideAnnotationVisibility(0);
this->SetOrientationMarkerVisibility(1);
this->GetOrientationWidget()->SynchronizeRenderersOff();
const double xAxis[3] = {1.0, 0.0, 0.0};
const double yAxis[3] = {0.0, 1.0, 0.0};
this->GetOrientationWidget()->UpdateCamera(180.0, xAxis);
this->GetOrientationWidget()->UpdateCamera(0.0, yAxis);
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::ConfigureEventMap()
{
// Get the render-widget common event map settings
this->Superclass::ConfigureEventMap();
// Configure the 2d-widget specific settings
switch (this->InteractionMode)
{
case vtkKWProbeImageWidget::INTERACTION_MODE_ROLL:
this->EventMap->SetMouseEvent(
vtkKWEventMap::LeftButton,vtkKWEventMap::NoModifier, "Roll");
this->EventMap->SetMouseEvent(
vtkKWEventMap::LeftButton,vtkKWEventMap::ShiftModifier, "WindowLevel");
this->EventMap->SetMouseEvent(
vtkKWEventMap::LeftButton,vtkKWEventMap::ControlModifier, "Zoom");
/*
this->EventMap->SetMouseEvent(
vtkKWEventMap::MiddleButton, vtkKWEventMap::NoModifier, "Zoom");
*/
break;
case vtkKWProbeImageWidget::INTERACTION_MODE_RESLICE:
this->EventMap->SetMouseEvent(
vtkKWEventMap::LeftButton,vtkKWEventMap::NoModifier, "Reslice");
this->EventMap->SetMouseEvent(
vtkKWEventMap::LeftButton,vtkKWEventMap::ShiftModifier, "WindowLevel");
this->EventMap->SetMouseEvent(
vtkKWEventMap::LeftButton,vtkKWEventMap::ControlModifier, "Zoom");
/*
this->EventMap->SetMouseEvent(
vtkKWEventMap::MiddleButton, vtkKWEventMap::NoModifier, "Zoom");
*/
break;
case vtkKWProbeImageWidget::INTERACTION_MODE_TRANSLATE:
this->EventMap->SetMouseEvent(
vtkKWEventMap::LeftButton,vtkKWEventMap::NoModifier, "Translate");
this->EventMap->SetMouseEvent(
vtkKWEventMap::LeftButton,vtkKWEventMap::ShiftModifier, "WindowLevel");
this->EventMap->SetMouseEvent(
vtkKWEventMap::LeftButton,vtkKWEventMap::ControlModifier, "Zoom");
/*
this->EventMap->SetMouseEvent(
vtkKWEventMap::MiddleButton, vtkKWEventMap::NoModifier, "Zoom");
*/
break;
}
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::PopulateContextMenuWithInteractionEntries(vtkKWMenu *menu)
{
this->Superclass::PopulateContextMenuWithInteractionEntries(menu);
if (!menu)
{
return;
}
const char *group_name = "InteractionMode";
int tcl_major, tcl_minor, tcl_patch_level;
Tcl_GetVersion(&tcl_major, &tcl_minor, &tcl_patch_level, NULL);
int show_icons = (tcl_major > 8 || (tcl_major == 8 && tcl_minor >= 5));
int index;
index = menu->AddRadioButton(
ks_("Interaction Mode|Rotate"),
this, "SetInteractionModeToRoll");
menu->SetItemSelectedValueAsInt(
index, vtkKWProbeImageWidget::INTERACTION_MODE_ROLL);
menu->SetItemGroupName(index, group_name);
if (show_icons)
{
menu->SetItemImageToPredefinedIcon(
index, vtkKWIcon::IconCrystalProject16x16ActionsRotate);
menu->SetItemCompoundModeToLeft(index);
}
index = menu->AddRadioButton(
ks_("Interaction Mode|Reslice"),
this, "SetInteractionModeToReslice");
menu->SetItemSelectedValueAsInt(
index, vtkKWProbeImageWidget::INTERACTION_MODE_RESLICE);
menu->SetItemGroupName(index, group_name);
if (show_icons)
{
menu->SetItemImageToPredefinedIcon(index, vtkKWIcon::IconObliqueProbe);
menu->SetItemCompoundModeToLeft(index);
}
index = menu->AddRadioButton(
ks_("Interaction Mode|Translate"),
this, "SetInteractionModeToTranslate");
menu->SetItemSelectedValueAsInt(
index, vtkKWProbeImageWidget::INTERACTION_MODE_TRANSLATE);
menu->SetItemGroupName(index, group_name);
if (show_icons)
{
menu->SetItemImageToPredefinedIcon(index, vtkKWIcon::IconMoveV);
menu->SetItemCompoundModeToLeft(index);
}
menu->SelectItemInGroupWithSelectedValueAsInt(
"InteractionMode", this->InteractionMode);
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::UpdateDisplayExtent()
{
if (!this->Image || !this->Image->GetInput() || !this->ProbeInputAlgorithm)
{
this->Superclass::UpdateDisplayExtent();
return;
}
this->Image->GetInput()->UpdateInformation();
this->Image->SetDisplayExtent(this->Image->GetInput()->GetWholeExtent());
// Note the renderer won't add it twice it is already there
vtkRenderer *ren = this->GetRenderer();
ren->AddViewProp(this->Image);
double bounds[6];
this->Image->GetBounds(bounds);
// Adjust the 3D widgets
this->Cursor3DWidget->PlaceWidget(bounds);
this->Cursor3DWidget->SetSliceOrientation(this->SliceOrientation);
// Figure out the correct clipping range?
// first get it in world coords
// This is particularly important when origin is different from 0,0,0
double spos = (double)bounds[this->SliceOrientation * 2];
double cpos = (double)
ren->GetActiveCamera()->GetPosition()[this->SliceOrientation];
double range = fabs(spos - cpos);
double *spacing = this->Image->GetInput()->GetSpacing();
double avgSpacing =
((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
ren->GetActiveCamera()->SetClippingRange(
range - avgSpacing * 3.0, range + avgSpacing * 3.0);
this->UpdateImageMap();
}
//---------------------------------------------------------------------------
int vtkKWProbeImageWidget::ComputeWorldCoordinate(
int x, int y, double *result, int *id)
{
if (this->Image->GetVisibility() == 0)
{
return 0;
}
if (!this->ProbeInputAlgorithm)
{
return this->Superclass::ComputeWorldCoordinate(x,y,result,id);
}
// What is the focal depth of the origin?
double point[4];
point[0] = 0.0;
point[1] = 0.0;
point[2] = 0.0;
point[3] = 1.0;
// Convert 0,0,0 into display coordinates to get the depth
vtkRenderer *ren = this->GetRenderer();
ren->SetWorldPoint(point);
ren->WorldToDisplay();
double *DPoint = ren->GetDisplayPoint();
double focalDepth = DPoint[2];
// Convert the x,y probe location to world coordinates using the
// computed depth
ren->SetDisplayPoint(x, y, focalDepth);
ren->DisplayToWorld();
double *RPoint = ren->GetWorldPoint();
if (RPoint[3] != 0.0)
{
RPoint[0] = (double)((double)RPoint[0] / (double)RPoint[3]);
RPoint[1] = (double)((double)RPoint[1] / (double)RPoint[3]);
RPoint[2] = (double)((double)RPoint[2] / (double)RPoint[3]);
RPoint[3] = 1.0;
}
// Now we have the world coordinate, now we can multiply that point
// by the reslice matrix to get the real world coordinate
vtkImageReslice *reslice = this->ImageReslice;
double range[2];
this->Input->GetScalarRange(range);
this->ImageReslice->SetBackgroundLevel ( range[0] );
if (reslice)
{
vtkMatrix4x4 *resliceAxes = this->ImageReslice->GetResliceAxes();
resliceAxes->MultiplyPoint(RPoint,point);
if (point[3] != 0.0)
{
point[0] = (double)((double)point[0] / (double)point[3]);
point[1] = (double)((double)point[1] / (double)point[3]);
point[2] = (double)((double)point[2] / (double)point[3]);
point[3] = 1.0;
}
double *origin = this->Input->GetOrigin();
double *spacing = this->Input->GetSpacing();
int loc[3];
loc[0] = (int)
(floor(((double)point[0]-(double)origin[0]) / (double)spacing[0] +0.5));
loc[1] = (int)
(floor(((double)point[1]-(double)origin[1]) / (double)spacing[1] +0.5));
loc[2] = (int)
(floor(((double)point[2]-(double)origin[2]) / (double)spacing[2] +0.5));
int dim[3];
this->Input->GetDimensions(dim);
// If we are outside the volume return 0
if (loc[0] < 0 || loc[0] >= dim[0] ||
loc[1] < 0 || loc[1] >= dim[1] ||
loc[2] < 0 || loc[2] >= dim[2])
{
return 0;
}
result[0] = point[0];
result[1] = point[1];
result[2] = point[2];
}
if (id)
{
*id = this->GetRendererIndex(ren);
}
return 1;
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::SetImageVisibility(int state)
{
if (this->GetImageVisibility() == state)
{
return;
}
this->Image->SetVisibility(state);
this->Render();
}
//----------------------------------------------------------------------------
int vtkKWProbeImageWidget::GetImageVisibility()
{
return this->Image->GetVisibility();
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::TiltPlane(double rxf, double ryf)
{
vtkCutter *cutter = vtkCutter::SafeDownCast(this->ProbeInputAlgorithm);
vtkPlane * plane = vtkPlane::SafeDownCast(cutter->GetCutFunction ());
double z[3]; // Z axis
plane->GetNormal(z);
double origin[3];
plane->GetOrigin(origin);
double oldx[3], oldy[3], oldz[3];
this->ImageReslice->GetResliceAxesDirectionCosines(oldx,oldy,oldz);
this->Transform->Identity();
this->Transform->RotateWXYZ(rxf,oldy);
this->Transform->RotateWXYZ(ryf,oldx);
//Set the new x
double x[3];
this->Transform->TransformNormal(oldx,x);
//Set the new y
double y[3];
this->Transform->TransformNormal(oldy,y);
//Set the new y
this->Transform->TransformNormal(oldz,z);
this->GetOrientationWidget()->UpdateCamera(rxf, oldy);
this->GetOrientationWidget()->UpdateCamera(ryf, oldx);
this->ImageReslice->SetResliceAxesDirectionCosines(x,y,z);
this->ImageReslice->GetResliceAxesDirectionCosines(oldx,oldy,oldz);
this->ImageReslice->SetResliceAxesOrigin(origin);
plane->SetNormal(z);
cutter->Modified();
this->Render();
this->InvokeEvent(vtkKWEvent::ProbeImageTiltPlaneEvent, 0);
}
//----------------------------------------------------------------------------
// This method moves the relices plane along the normal by
// a factor passed to it by the interactor that is based on
// the y motion of the mouse
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::TranslatePlane(double factor)
{
vtkCutter *cutter = vtkCutter::SafeDownCast(this->ProbeInputAlgorithm);
vtkPlane * plane = vtkPlane::SafeDownCast(cutter->GetCutFunction ());
// This is the reslice normal
double z[3];
plane->GetNormal(z);
// This is the reslice origin
double origin[3];
plane->GetOrigin(origin);
// Find out the x, y bounds of the image -
// use this as a scale factor so that the
// factor input parameter is linked to the size
// of the data
double bounds[6];
this->Image->GetBounds(bounds);
double avg =
( (bounds[1] - bounds[0]) +
(bounds[3] - bounds[2]) ) * 0.5;
factor *= avg;
// Move the origin along the normal
vtkMath::Normalize(z);
origin[0] += factor * z[0];
origin[1] += factor * z[1];
origin[2] += factor * z[2];
// No need to check bounds before setting - the plane widget
// will do it. But that means we need to get it to find out what
// it really is after we set it.
//
// TODO: there are a couple of problems here - first, this
// doesn't really work in practice - there is some bug that
// causes the origin to move off the reslice plane as drawn
// by the widget. Shouldn't happen but it does. Second, we
// should really be keeping the origin in the center of the
// plane - it looks better that way. Perhaps when we fix that
// issue the first one will go away.
plane->SetOrigin(origin);
plane->GetOrigin(origin);
cutter->Modified();
this->ImageReslice->SetResliceAxesOrigin(origin);
this->Render();
this->InvokeEvent(vtkKWEvent::ProbeImageTranslatePlaneEvent, 0);
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::RollPlane(double theta)
{
vtkCutter *cutter = vtkCutter::SafeDownCast(this->ProbeInputAlgorithm);
vtkPlane * plane = vtkPlane::SafeDownCast(cutter->GetCutFunction ());
double z[3]; // Z axis
plane->GetNormal(z);
double origin[3];
plane->GetOrigin(origin);
double oldx[3], oldy[3], oldz[3];
this->ImageReslice->GetResliceAxesDirectionCosines(oldx,oldy,oldz);
double axis[3];
vtkMath::Cross(oldz,z,axis);
if ( vtkMath::Normalize(axis) != 0.0 )
{
abort();
return;
}
this->Transform->Identity();
//this->Transform->Translate(origin[0],origin[1],origin[2]);
this->Transform->RotateWXYZ(theta,z);
//this->Transform->Translate(-origin[0],-origin[1],-origin[2]);
//Set the new x
double x[3];
this->Transform->TransformNormal(oldx,x);
//Set the new y
double y[3];
this->Transform->TransformNormal(oldy,y);
this->GetOrientationWidget()->UpdateCamera( theta, z);
this->ImageReslice->SetResliceAxesDirectionCosines(x,y,z);
this->ImageReslice->SetResliceAxesOrigin(origin);
this->Render();
this->InvokeEvent(vtkKWEvent::ProbeImageRollPlaneEvent, 0);
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::UpdatePlane()
{
vtkCutter *cutter = vtkCutter::SafeDownCast(this->ProbeInputAlgorithm);
vtkPlane * plane = vtkPlane::SafeDownCast(cutter->GetCutFunction ());
double z[3]; // Z axis
plane->GetNormal(z);
double origin[3];
plane->GetOrigin(origin);
double oldx[3], oldy[3], oldz[3];
this->ImageReslice->GetResliceAxesDirectionCosines(oldx,oldy,oldz);
double axis[3];
vtkMath::Cross(oldz,z,axis);
if ( vtkMath::Normalize(axis) < 1.0e-15 )
{
this->ImageReslice->SetResliceAxesOrigin(origin);
return;
}
double cos_theta = vtkMath::Dot(oldz, z);
double theta = vtkMath::DegreesFromRadians( acos( cos_theta ) );
this->Transform->Identity();
this->Transform->Translate(origin[0],origin[1],origin[2]);
this->Transform->RotateWXYZ(theta,axis);
this->Transform->Translate(-origin[0],-origin[1],-origin[2]);
//Set the new x
double x[3];
this->Transform->TransformNormal(oldx,x);
//Set the new y
double y[3];
this->Transform->TransformNormal(oldy,y);
this->GetOrientationWidget()->UpdateCamera( theta, axis);
this->ImageReslice->SetResliceAxesDirectionCosines(x,y,z);
this->ImageReslice->SetResliceAxesOrigin(origin);
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::Render()
{
if (this->Image && this->Image->GetInput() && this->ProbeInputAlgorithm)
{
this->UpdatePlane();
this->UpdateDisplayExtent();
}
this->Superclass::Render();
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::ResetCamera()
{
this->vtkKWRenderWidget::ResetCamera();
}
//----------------------------------------------------------------------------
void vtkKWProbeImageWidget::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "ProbeInputAlgorithm: " << this->ProbeInputAlgorithm << endl;
os << indent << "ImageReslice: " << this->ImageReslice << endl;
}
|