1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #include "vvSlicerManager.h"
22 #include "vvSlicerManagerCommand.h"
23 #include "vvInteractorStyleNavigator.h"
24 #include "vvLandmarks.h"
25 #include "vvImageReader.h"
26 #include "vvImageReader.h"
28 #include "vvImageMapToWLColors.h"
29 #include "vvBlendImageActor.h"
31 #include <vtkImageActor.h>
32 #include <vtkImageData.h>
33 #include <vtkRenderWindow.h>
34 #include <vtkRendererCollection.h>
35 #include <vtkRenderWindowInteractor.h>
36 #include <vtkImageMapToWindowLevelColors.h>
37 #include <vtkWindowLevelLookupTable.h>
38 #include <vtkColorTransferFunction.h>
39 #include <vtkImageClip.h>
40 #include <vtkLODActor.h>
41 #include <vtkPointData.h>
42 #include <vtksys/SystemTools.hxx>
43 #include <vtkCamera.h>
45 //----------------------------------------------------------------------------
46 vvSlicerManager::vvSlicerManager(int numberOfSlicers)
55 mType = UNDEFINEDIMAGETYPE;
69 mOverlayReader = NULL;
74 for ( int i = 0; i < numberOfSlicers; i++) {
75 vvSlicer *slicer = vvSlicer::New();
76 mSlicers.push_back(slicer);
78 mPreviousSlice.resize(numberOfSlicers);
79 mPreviousTSlice.resize(numberOfSlicers);
81 //----------------------------------------------------------------------------
84 //----------------------------------------------------------------------------
85 vvSlicerManager::~vvSlicerManager()
87 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
88 if (mSlicers[i] != NULL)
89 mSlicers[i]->Delete();
98 delete mOverlayReader;
101 delete mFusionReader;
106 //----------------------------------------------------------------------------
109 //------------------------------------------------------------------------------
110 void vvSlicerManager::SetFilename(std::string filename, int number)
112 mFileName = filename;
113 mFileName = vtksys::SystemTools::GetFilenameName(mFileName);
114 mBaseFileName = vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(mFileName));
115 mBaseFileNameNumber = number;
117 for(unsigned int i=0; i<mSlicers.size(); i++) {
118 mSlicers[i]->SetFileName(vtksys::SystemTools::GetFilenameWithoutLastExtension(filename));
122 mFileName.append("_"+clitk::toString(number));
125 //------------------------------------------------------------------------------
128 //----------------------------------------------------------------------------
129 void vvSlicerManager::AddContour(vvMesh::Pointer contour,bool propagate)
131 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
132 mSlicers[i]->AddContour(contour,propagate);
135 //----------------------------------------------------------------------------
138 //----------------------------------------------------------------------------
139 void vvSlicerManager::ToggleContourSuperposition()
141 for ( unsigned int i = 0; i < mSlicers.size(); i++)
142 mSlicers[i]->ToggleContourSuperposition();
144 //----------------------------------------------------------------------------
147 //----------------------------------------------------------------------------
148 bool vvSlicerManager::SetImage(std::string filename, LoadedImageType type, int n, unsigned int slice)
152 mReader = new vvImageReader;
153 std::vector<std::string> filenames;
154 filenames.push_back(filename);
155 mReader->SetInputFilenames(filenames);
156 mReader->SetSlice(slice); // Only used for SLICED type
157 mReader->Update(type);
159 SetFilename(filename, n);
160 // mFileName = vtksys::SystemTools::GetFilenameName(mFileName);
161 //mBaseFileName = vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(mFileName));
162 //mBaseFileNameNumber = n;
164 if (mReader->GetLastError().size() == 0) {
165 mImage=mReader->GetOutput();
166 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
167 mSlicers[i]->SetFileName(vtksys::SystemTools::GetFilenameWithoutLastExtension(filename));
168 mSlicers[i]->SetImage(mReader->GetOutput());
171 mLastError = mReader->GetLastError();
175 // mFileName.append("_"+clitk::toString(n));
179 //----------------------------------------------------------------------------
182 //----------------------------------------------------------------------------
183 void vvSlicerManager::SetImage(vvImage::Pointer image)
186 for (unsigned int i = 0; i < mSlicers.size(); i++) {
187 mSlicers[i]->SetImage(image);
190 //----------------------------------------------------------------------------
193 //----------------------------------------------------------------------------
194 bool vvSlicerManager::SetImages(std::vector<std::string> filenames,LoadedImageType type, int n)
197 std::string fileWithoutExtension = vtksys::SystemTools::GetFilenameWithoutExtension(filenames[0]);
199 fileWithoutExtension += "_dicom";
200 else if (type == MERGED)
201 fileWithoutExtension += "_merged";
202 else if (type == MERGEDWITHTIME)
203 fileWithoutExtension += "_merged_wt";
205 mFileName = vtksys::SystemTools::GetFilenameName(mFileName);
206 mFileName = fileWithoutExtension + vtksys::SystemTools::GetFilenameExtension(filenames[0]);
208 mReader = new vvImageReader;
209 mReader->SetInputFilenames(filenames);
210 mReader->Update(type);
212 mBaseFileName = vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(mFileName));
213 mBaseFileNameNumber = n;
215 if (mReader->GetLastError().size() == 0) {
216 mImage=mReader->GetOutput();
217 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
218 mSlicers[i]->SetFileName(fileWithoutExtension);
219 mSlicers[i]->SetImage(mReader->GetOutput());
222 mLastError = mReader->GetLastError();
226 mFileName.append("_"+clitk::toString(n));
230 //----------------------------------------------------------------------------
233 //----------------------------------------------------------------------------
234 bool vvSlicerManager::SetOverlay(std::string filename,int dim, std::string component)
236 mOverlayName = filename;
237 mOverlayComponent = component;
238 if (dim > mImage->GetNumberOfDimensions()) {
239 mLastError = " Overlay dimension cannot be greater then reference image!";
242 if (mOverlayReader == NULL)
243 mOverlayReader = new vvImageReader;
244 std::vector<std::string> filenames;
245 filenames.push_back(filename);
246 mOverlayReader->SetInputFilenames(filenames);
247 mOverlayReader->Update(mImage->GetNumberOfDimensions(),component.c_str(),mType);
248 if (mOverlayReader->GetLastError().size() == 0) {
249 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
250 mSlicers[i]->SetOverlay(mOverlayReader->GetOutput());
253 mLastError = mOverlayReader->GetLastError();
258 //----------------------------------------------------------------------------
261 //----------------------------------------------------------------------------
262 bool vvSlicerManager::SetFusion(std::string filename,int dim, std::string component)
264 mFusionName = filename;
265 mFusionComponent = component;
266 if (dim > mImage->GetNumberOfDimensions()) {
267 mLastError = " Overlay dimension cannot be greater then reference image!";
270 if (mFusionReader == NULL)
271 mFusionReader = new vvImageReader;
272 std::vector<std::string> filenames;
273 filenames.push_back(filename);
274 mFusionReader->SetInputFilenames(filenames);
275 mFusionReader->Update(mImage->GetNumberOfDimensions(),component.c_str(),mType);
276 if (mFusionReader->GetLastError().size() == 0) {
277 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
278 mSlicers[i]->SetFusion(mFusionReader->GetOutput());
281 mLastError = mFusionReader->GetLastError();
284 double *fusRange = mFusionReader->GetOutput()->GetVTKImages()[0]->GetScalarRange();
285 mFusionLevel = (fusRange[0]+fusRange[1])/2;
286 mFusionWindow = fusRange[1]-fusRange[0];
289 //----------------------------------------------------------------------------
292 //----------------------------------------------------------------------------
293 bool vvSlicerManager::SetVF(std::string filename)
295 if (mVectorReader == NULL)
296 mVectorReader = new vvImageReader;
297 mVectorReader->SetInputFilename(filename);
298 mVectorReader->Update(VECTORFIELD);
299 if (mVectorReader->GetLastError().size() != 0) {
300 mLastError = mVectorReader->GetLastError();
303 return SetVF(mVectorReader->GetOutput(),filename);
305 //----------------------------------------------------------------------------
308 //----------------------------------------------------------------------------
309 bool vvSlicerManager::SetVF(vvImage::Pointer vf,std::string filename)
311 if (vf->GetNumberOfDimensions() > mImage->GetNumberOfDimensions()) {
312 mLastError = "Sorry, vector field dimension cannot be greater then reference image.";
315 if (vf->GetNumberOfDimensions() == 4) {
316 if (vf->GetSpacing()[3] != mImage->GetSpacing()[3]) {
317 mLastError = "Sorry, vector field time spacing cannot be different from time spacing of the reference image.";
320 if (vf->GetOrigin()[3] != mImage->GetOrigin()[3]) {
321 mLastError = "Sorry, vector field time origin cannot be different from time origin of the reference image.";
327 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
328 mSlicers[i]->SetVF(vf);
332 //----------------------------------------------------------------------------
335 //----------------------------------------------------------------------------
336 vvSlicer* vvSlicerManager::GetSlicer(int i)
340 //----------------------------------------------------------------------------
343 //----------------------------------------------------------------------------
344 void vvSlicerManager::UpdateSlicer(int num, bool state)
346 if (mSlicers[num]->GetImage())
347 mSlicers[num]->SetDisplayMode(state);
349 //----------------------------------------------------------------------------
352 //----------------------------------------------------------------------------
353 void vvSlicerManager::SetSlicerWindow(int i, vtkRenderWindow* RW)
355 mSlicers[i]->SetRenderWindow(i,RW);
357 //----------------------------------------------------------------------------
360 //----------------------------------------------------------------------------
361 void vvSlicerManager::SetInteractorStyleNavigator(int i, vtkInteractorStyle* style)
363 vvSlicerManagerCommand *smc = vvSlicerManagerCommand::New();
365 smc->SetSlicerNumber(i);
366 mSlicers[i]->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style);
368 mSlicers[i]->GetRenderWindow()->GetInteractor()->
369 GetInteractorStyle()->AddObserver(vtkCommand::KeyPressEvent, smc);
370 mSlicers[i]->GetRenderWindow()->GetInteractor()->
371 GetInteractorStyle()->AddObserver(vtkCommand::WindowLevelEvent, smc);
372 mSlicers[i]->GetRenderWindow()->GetInteractor()->
373 GetInteractorStyle()->AddObserver(vtkCommand::EndWindowLevelEvent, smc);
374 mSlicers[i]->GetRenderWindow()->GetInteractor()->
375 GetInteractorStyle()->AddObserver(vtkCommand::StartWindowLevelEvent, smc);
376 mSlicers[i]->GetRenderWindow()->GetInteractor()->
377 GetInteractorStyle()->AddObserver(vtkCommand::PickEvent, smc);
378 mSlicers[i]->GetRenderWindow()->GetInteractor()->
379 GetInteractorStyle()->AddObserver(vtkCommand::StartPickEvent, smc);
380 mSlicers[i]->GetRenderWindow()->GetInteractor()->
381 GetInteractorStyle()->AddObserver(vtkCommand::LeaveEvent, smc);
382 mSlicers[i]->GetRenderWindow()->GetInteractor()->
383 GetInteractorStyle()->AddObserver(vtkCommand::UserEvent, smc);
384 mSlicers[i]->GetRenderWindow()->GetInteractor()->
385 GetInteractorStyle()->AddObserver(vtkCommand::MouseWheelForwardEvent, smc);
386 mSlicers[i]->GetRenderWindow()->GetInteractor()->
387 GetInteractorStyle()->AddObserver(vtkCommand::MouseWheelBackwardEvent, smc);
388 // mSlicers[i]->GetRenderWindow()->GetInteractor()->
389 // GetInteractorStyle()->AddObserver(vtkCommand::LeftButtonReleaseEvent, smc);
390 mSlicers[i]->GetRenderWindow()->GetInteractor()->
391 GetInteractorStyle()->AddObserver(vtkCommand::EndPickEvent, smc);
392 mSlicers[i]->GetRenderWindow()->GetInteractor()->
393 GetInteractorStyle()->AddObserver(vtkCommand::EndInteractionEvent, smc);
396 //----------------------------------------------------------------------------
399 //----------------------------------------------------------------------------
400 void vvSlicerManager::LeftButtonReleaseEvent(int slicer)
402 emit LeftButtonReleaseSignal(slicer);
404 //----------------------------------------------------------------------------
407 //----------------------------------------------------------------------------
408 void vvSlicerManager::SetTSlice(int slice)
412 else if (slice > mSlicers[0]->GetTMax())
413 slice = mSlicers[0]->GetTMax();
415 mLandmarks->SetTime(slice);
416 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
417 if (slice != mSlicers[i]->GetTSlice()) {
418 mSlicers[i]->SetTSlice(slice);
419 if (mSlicers[i]->GetImageActor()->GetVisibility())
424 //----------------------------------------------------------------------------
427 //----------------------------------------------------------------------------
428 void vvSlicerManager::SetNextTSlice(int originating_slicer)
430 int t = mSlicers[0]->GetTSlice();
432 if (t > mSlicers[0]->GetTMax())
434 emit UpdateTSlice(originating_slicer,t);
436 //----------------------------------------------------------------------------
439 //----------------------------------------------------------------------------
440 void vvSlicerManager::SetPreviousTSlice(int originating_slicer)
442 int t = mSlicers[0]->GetTSlice();
445 t = mSlicers[0]->GetTMax();
446 emit UpdateTSlice(originating_slicer,t);
448 //----------------------------------------------------------------------------
451 //----------------------------------------------------------------------------
452 void vvSlicerManager::ToggleInterpolation()
454 bool interpolate=!(mSlicers[0]->GetImageActor()->GetInterpolate());
455 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
456 mSlicers[i]->GetImageActor()->SetInterpolate(interpolate);
457 if (mSlicers[i]->GetOverlayActor())
458 mSlicers[i]->GetOverlayActor()->SetInterpolate(interpolate);
459 if (mSlicers[i]->GetFusionActor())
460 mSlicers[i]->GetFusionActor()->SetInterpolate(interpolate);
463 //----------------------------------------------------------------------------
466 //----------------------------------------------------------------------------
467 void vvSlicerManager::SetTSliceInSlicer(int tslice, int slicer)
471 else if (tslice > mSlicers[slicer]->GetTMax())
472 tslice = mSlicers[slicer]->GetTMax();
474 mLandmarks->SetTime(tslice);
476 if (mSlicers[slicer]->GetTSlice() == tslice) return;
478 mSlicers[slicer]->SetTSlice(tslice);
479 if (mSlicers[slicer]->GetImageActor()->GetVisibility())
480 UpdateTSlice(slicer);
482 //----------------------------------------------------------------------------
485 //----------------------------------------------------------------------------
486 void vvSlicerManager::SetColorWindow(double s)
488 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
489 mSlicers[i]->SetColorWindow(s);
492 //----------------------------------------------------------------------------
495 //----------------------------------------------------------------------------
496 void vvSlicerManager::SetColorLevel(double s)
498 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
499 mSlicers[i]->SetColorLevel(s);
502 //----------------------------------------------------------------------------
504 //----------------------------------------------------------------------------
505 void vvSlicerManager::SetCursorVisibility(int s)
507 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
508 mSlicers[i]->SetCursorVisibility(s);
511 //----------------------------------------------------------------------------
514 //----------------------------------------------------------------------------
515 void vvSlicerManager::SetOpacity(int i, double factor)
517 mSlicers[i]->SetOpacity(1/factor);
519 //----------------------------------------------------------------------------
522 //----------------------------------------------------------------------------
523 void vvSlicerManager::UpdateViews(int current,int slicer)
525 double x = (mSlicers[slicer]->GetCurrentPosition()[0] - mSlicers[slicer]->GetInput()->GetOrigin()[0])
526 /mSlicers[slicer]->GetInput()->GetSpacing()[0];
527 double y = (mSlicers[slicer]->GetCurrentPosition()[1] - mSlicers[slicer]->GetInput()->GetOrigin()[1])
528 /mSlicers[slicer]->GetInput()->GetSpacing()[1];
529 double z = (mSlicers[slicer]->GetCurrentPosition()[2] - mSlicers[slicer]->GetInput()->GetOrigin()[2])
530 /mSlicers[slicer]->GetInput()->GetSpacing()[2];
532 if (x >= mSlicers[slicer]->GetInput()->GetWholeExtent()[0] &&
533 x <= mSlicers[slicer]->GetInput()->GetWholeExtent()[1] &&
534 y >= mSlicers[slicer]->GetInput()->GetWholeExtent()[2] &&
535 y <= mSlicers[slicer]->GetInput()->GetWholeExtent()[3] &&
536 z >= mSlicers[slicer]->GetInput()->GetWholeExtent()[4] &&
537 z <= mSlicers[slicer]->GetInput()->GetWholeExtent()[5]) {
538 mSlicers[slicer]->UpdateCursorPosition();
539 mSlicers[slicer]->SetCursorColor(10,212,255);
541 switch (mSlicers[slicer]->GetSliceOrientation()) {
542 case vtkImageViewer2::SLICE_ORIENTATION_XY:
543 if (mSlicers[slicer]->GetSlice() == (int)floor(z))
544 mSlicers[slicer]->Render();
546 mSlicers[slicer]->SetSlice((int)floor(z));
549 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
550 if (mSlicers[slicer]->GetSlice() == (int)floor(y))
551 mSlicers[slicer]->Render();
553 mSlicers[slicer]->SetSlice((int)floor(y));
556 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
557 if (mSlicers[slicer]->GetSlice() == (int)floor(x))
558 mSlicers[slicer]->Render();
560 mSlicers[slicer]->SetSlice((int)floor(x));
564 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
565 if (i != (unsigned int)slicer && mSlicers[i]->GetImageActor()->GetVisibility()
566 && mSlicers[i]->GetRenderWindow()->GetSize()[0] > 2
567 && mSlicers[i]->GetRenderWindow()->GetSize()[1] > 2) {
568 mSlicers[i]->SetCurrentPosition(mSlicers[slicer]->GetCurrentPosition()[0],
569 mSlicers[slicer]->GetCurrentPosition()[1],
570 mSlicers[slicer]->GetCurrentPosition()[2],
571 mSlicers[slicer]->GetTSlice());
572 mSlicers[i]->UpdateCursorPosition();
573 if (current) { //do not display corner annotation if image is the one picked
574 mSlicers[i]->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
575 -VTK_DOUBLE_MAX, mSlicers[slicer]->GetTSlice());
576 mSlicers[i]->SetCursorColor(255,10,212);
578 mSlicers[i]->SetCursorColor(150,10,282);
580 switch (mSlicers[i]->GetSliceOrientation()) {
581 case vtkImageViewer2::SLICE_ORIENTATION_XY:
582 if (mSlicers[i]->GetSlice() == (int)floor(z))
583 mSlicers[i]->Render();
585 mSlicers[i]->SetSlice((int)floor(z));
588 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
589 if (mSlicers[i]->GetSlice() == (int)floor(y))
590 mSlicers[i]->Render();
592 mSlicers[i]->SetSlice((int)floor(y));
595 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
596 if (mSlicers[i]->GetSlice() == (int)floor(x))
597 mSlicers[i]->Render();
599 mSlicers[i]->SetSlice((int)floor(x));
608 //----------------------------------------------------------------------------
611 //----------------------------------------------------------------------------
612 void vvSlicerManager::UpdateLinked(int slicer)
614 double x = (mSlicers[slicer]->GetCurrentPosition()[0] - mSlicers[slicer]->GetInput()->GetOrigin()[0])
615 /mSlicers[slicer]->GetInput()->GetSpacing()[0];
616 double y = (mSlicers[slicer]->GetCurrentPosition()[1] - mSlicers[slicer]->GetInput()->GetOrigin()[1])
617 /mSlicers[slicer]->GetInput()->GetSpacing()[1];
618 double z = (mSlicers[slicer]->GetCurrentPosition()[2] - mSlicers[slicer]->GetInput()->GetOrigin()[2])
619 /mSlicers[slicer]->GetInput()->GetSpacing()[2];
621 if (x >= mSlicers[slicer]->GetInput()->GetWholeExtent()[0] &&
622 x <= mSlicers[slicer]->GetInput()->GetWholeExtent()[1] &&
623 y >= mSlicers[slicer]->GetInput()->GetWholeExtent()[2] &&
624 y <= mSlicers[slicer]->GetInput()->GetWholeExtent()[3] &&
625 z >= mSlicers[slicer]->GetInput()->GetWholeExtent()[4] &&
626 z <= mSlicers[slicer]->GetInput()->GetWholeExtent()[5]) {
627 for (std::list<std::string>::const_iterator i = mLinkedId.begin(); i != mLinkedId.end(); i++) {
628 emit UpdateLinkManager(*i, slicer,mSlicers[slicer]->GetCurrentPosition()[0],
629 mSlicers[slicer]->GetCurrentPosition()[1],
630 mSlicers[slicer]->GetCurrentPosition()[2],mSlicers[slicer]->GetTSlice());
634 //----------------------------------------------------------------------------
636 //----------------------------------------------------------------------------
637 void vvSlicerManager::UpdateLinkedNavigation(vvSlicer *slicer, bool bPropagate)
639 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
640 vtkCamera *camera = mSlicers[i] ->GetRenderer()->GetActiveCamera();
641 vtkCamera *refCam = slicer->GetRenderer()->GetActiveCamera();
642 camera->SetParallelScale(refCam->GetParallelScale());
644 double position[3], focal[3];
645 camera->GetPosition(position);
646 camera->GetFocalPoint(focal);
648 double refPosition[3], refFocal[3];
649 refCam->GetPosition(refPosition);
650 refCam->GetFocalPoint(refFocal);
652 if(slicer->GetSliceOrientation()==mSlicers[i]->GetSliceOrientation()) {
653 for(int i=0; i<3; i++) {
654 position[i] = refPosition[i];
655 focal[i] = refFocal[i];
659 if(slicer->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XY) {
660 if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XZ) {
661 position[0] = refPosition[0];
662 focal[0] = refFocal[0];
664 if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_YZ) {
665 position[1] = refPosition[1];
666 focal[1] = refFocal[1];
670 if(slicer->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XZ) {
671 if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_YZ) {
672 position[2] = refPosition[2];
673 focal[2] = refFocal[2];
675 if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XY) {
676 position[0] = refPosition[0];
677 focal[0] = refFocal[0];
681 if(slicer->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_YZ) {
682 if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XY) {
683 position[1] = refPosition[1];
684 focal[1] = refFocal[1];
686 if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XZ) {
687 position[2] = refPosition[2];
688 focal[2] = refFocal[2];
692 camera->SetFocalPoint(focal);
693 camera->SetPosition(position);
696 for (std::list<std::string>::const_iterator i = mLinkedId.begin(); i != mLinkedId.end(); i++)
697 emit UpdateLinkedNavigation(*i, this);
701 //----------------------------------------------------------------------------
703 //----------------------------------------------------------------------------
704 double vvSlicerManager::GetColorWindow()
707 return mSlicers[0]->GetColorWindow();
710 //----------------------------------------------------------------------------
713 //----------------------------------------------------------------------------
714 double vvSlicerManager::GetColorLevel()
717 return mSlicers[0]->GetColorLevel();
720 //----------------------------------------------------------------------------
723 //----------------------------------------------------------------------------
724 void vvSlicerManager::Render()
726 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
727 mSlicers[i]->Render();
730 //----------------------------------------------------------------------------
733 //----------------------------------------------------------------------------
734 void vvSlicerManager::GenerateDefaultLookupTable()
737 SetColorMap(mColorMap);
739 //----------------------------------------------------------------------------
742 //----------------------------------------------------------------------------
743 void vvSlicerManager::Reload()
745 mReader->Update(mType);
746 mImage=mReader->GetOutput();
747 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
748 mSlicers[i]->SetImage(mImage);
751 //----------------------------------------------------------------------------
754 //----------------------------------------------------------------------------
755 void vvSlicerManager::ReloadFusion()
757 mFusionReader->Update();
758 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
759 mSlicers[i]->SetFusion(mFusionReader->GetOutput());
760 mSlicers[i]->Render();
763 //----------------------------------------------------------------------------
766 //----------------------------------------------------------------------------
767 void vvSlicerManager::ReloadOverlay()
769 mOverlayReader->Update();
770 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
771 mSlicers[i]->SetOverlay(mOverlayReader->GetOutput());
772 mSlicers[i]->Render();
775 //----------------------------------------------------------------------------
778 //----------------------------------------------------------------------------
779 void vvSlicerManager::ReloadVF()
781 mVectorReader->Update(VECTORFIELD); //deletes the old images through the VF::Init() function
782 mVF=mVectorReader->GetOutput();
783 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
784 mSlicers[i]->SetVF(mVF);
785 mSlicers[i]->Render();
788 //----------------------------------------------------------------------------
791 //----------------------------------------------------------------------------
792 void vvSlicerManager::RemoveActor(const std::string& actor_type, int overlay_index)
794 if (actor_type =="overlay") {
795 delete mOverlayReader;
796 mOverlayReader = NULL;
799 if (actor_type =="fusion") {
800 delete mFusionReader;
801 mFusionReader = NULL;
804 for (unsigned int i = 0; i < mSlicers.size(); i++) {
805 mSlicers[i]->RemoveActor(actor_type,overlay_index);
807 if (actor_type=="vector") {
810 delete mVectorReader;
815 //----------------------------------------------------------------------------
818 //----------------------------------------------------------------------------
819 void vvSlicerManager::RemoveActors()
821 ///This method leaks a few objects. See RemoveActor for what a
822 ///correct implementation would look like
823 //DS -> probably due to the reader (now released in the
824 //RemoveActor() function. (I hope)
825 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
826 mSlicers[i]->SetDisplayMode(0);
827 mSlicers[i]->GetRenderer()->RemoveActor(mSlicers[i]->GetImageActor());
830 //----------------------------------------------------------------------------
833 //----------------------------------------------------------------------------
834 void vvSlicerManager::UpdateInfoOnCursorPosition(int slicer)
836 // int view = mSlicers[slicer]->GetSliceOrientation();
837 // int slice = mSlicers[slicer]->GetSlice();
838 double x = mSlicers[slicer]->GetCursorPosition()[0];
839 double y = mSlicers[slicer]->GetCursorPosition()[1];
840 double z = mSlicers[slicer]->GetCursorPosition()[2];
841 double X = (x - mSlicers[slicer]->GetInput()->GetOrigin()[0])/
842 mSlicers[slicer]->GetInput()->GetSpacing()[0];
843 double Y = (y - mSlicers[slicer]->GetInput()->GetOrigin()[1])/
844 mSlicers[slicer]->GetInput()->GetSpacing()[1];
845 double Z = (z - mSlicers[slicer]->GetInput()->GetOrigin()[2])/
846 mSlicers[slicer]->GetInput()->GetSpacing()[2];
847 double value = -VTK_DOUBLE_MAX;
849 double xVec=0, yVec=0, zVec=0, valueVec=0;
852 double valueOver=0, valueFus=0;
853 if (X >= mSlicers[slicer]->GetInput()->GetWholeExtent()[0] &&
854 X <= mSlicers[slicer]->GetInput()->GetWholeExtent()[1] &&
855 Y >= mSlicers[slicer]->GetInput()->GetWholeExtent()[2] &&
856 Y <= mSlicers[slicer]->GetInput()->GetWholeExtent()[3] &&
857 Z >= mSlicers[slicer]->GetInput()->GetWholeExtent()[4] &&
858 Z <= mSlicers[slicer]->GetInput()->GetWholeExtent()[5]) {
859 value = this->GetScalarComponentAsDouble(mSlicers[slicer]->GetInput(), X, Y, Z);
861 if (mSlicers[slicer]->GetVFActor() && mSlicers[slicer]->GetVFActor()->GetVisibility()) {
863 unsigned int currentTime = mSlicers[slicer]->GetTSlice();
864 vtkImageData *vf = NULL;
866 if (mSlicers[slicer]->GetVF()->GetVTKImages().size() > currentTime)
867 vf = mSlicers[slicer]->GetVF()->GetVTKImages()[currentTime];
869 vf = mSlicers[slicer]->GetVF()->GetVTKImages()[0];
872 double Xvf = (x - vf->GetOrigin()[0])/ vf->GetSpacing()[0];
873 double Yvf = (y - vf->GetOrigin()[1])/ vf->GetSpacing()[1];
874 double Zvf = (z - vf->GetOrigin()[2])/ vf->GetSpacing()[2];
875 xVec = this->GetScalarComponentAsDouble( vf, Xvf, Yvf, Zvf, 0);
876 yVec = this->GetScalarComponentAsDouble( vf, Xvf, Yvf, Zvf, 1);
877 zVec = this->GetScalarComponentAsDouble( vf, Xvf, Yvf, Zvf, 2);
878 valueVec = sqrt(xVec*xVec + yVec*yVec + zVec*zVec);
881 if (mSlicers[slicer]->GetOverlayActor() && mSlicers[slicer]->GetOverlayActor()->GetVisibility()) {
883 double Xover = (x - mSlicers[slicer]->GetOverlay()->GetOrigin()[0])
884 /mSlicers[slicer]->GetOverlay()->GetSpacing()[0];
885 double Yover = (y - mSlicers[slicer]->GetOverlay()->GetOrigin()[1])
886 /mSlicers[slicer]->GetOverlay()->GetSpacing()[1];
887 double Zover = (z - mSlicers[slicer]->GetOverlay()->GetOrigin()[2])
888 /mSlicers[slicer]->GetOverlay()->GetSpacing()[2];
889 if (Xover >= mSlicers[slicer]->GetOverlayMapper()->GetInput()->GetWholeExtent()[0] &&
890 Xover <= mSlicers[slicer]->GetOverlayMapper()->GetInput()->GetWholeExtent()[1] &&
891 Yover >= mSlicers[slicer]->GetOverlayMapper()->GetInput()->GetWholeExtent()[2] &&
892 Yover <= mSlicers[slicer]->GetOverlayMapper()->GetInput()->GetWholeExtent()[3] &&
893 Zover >= mSlicers[slicer]->GetOverlayMapper()->GetInput()->GetWholeExtent()[4] &&
894 Zover <= mSlicers[slicer]->GetOverlayMapper()->GetInput()->GetWholeExtent()[5]) {
895 valueOver = this->GetScalarComponentAsDouble(static_cast<vtkImageData*>(mSlicers[slicer]->GetOverlayMapper()->GetInput()), Xover, Yover, Zover);
898 if (mSlicers[slicer]->GetFusionActor() && mSlicers[slicer]->GetFusionActor()->GetVisibility()) {
900 double Xfus = (x - mSlicers[slicer]->GetFusion()->GetOrigin()[0])
901 /mSlicers[slicer]->GetFusion()->GetSpacing()[0];
902 double Yfus = (y - mSlicers[slicer]->GetFusion()->GetOrigin()[1])
903 /mSlicers[slicer]->GetFusion()->GetSpacing()[1];
904 double Zfus = (z - mSlicers[slicer]->GetFusion()->GetOrigin()[2])
905 /mSlicers[slicer]->GetFusion()->GetSpacing()[2];
906 if (Xfus >= mSlicers[slicer]->GetFusionMapper()->GetInput()->GetWholeExtent()[0] &&
907 Xfus <= mSlicers[slicer]->GetFusionMapper()->GetInput()->GetWholeExtent()[1] &&
908 Yfus >= mSlicers[slicer]->GetFusionMapper()->GetInput()->GetWholeExtent()[2] &&
909 Yfus <= mSlicers[slicer]->GetFusionMapper()->GetInput()->GetWholeExtent()[3] &&
910 Zfus >= mSlicers[slicer]->GetFusionMapper()->GetInput()->GetWholeExtent()[4] &&
911 Zfus <= mSlicers[slicer]->GetFusionMapper()->GetInput()->GetWholeExtent()[5]) {
912 valueFus = this->GetScalarComponentAsDouble(static_cast<vtkImageData*>(mSlicers[slicer]->GetFusionMapper()->GetInput()), Xfus, Yfus, Zfus);
915 emit UpdatePosition(mSlicers[slicer]->GetCursorVisibility(),
917 emit UpdateVector(displayVec,xVec, yVec, zVec, valueVec);
918 emit UpdateOverlay(displayOver,valueOver,value);
919 emit UpdateFusion(displayFus,valueFus);
920 for (unsigned int i = 0; i < mSlicers.size(); i++) {
921 if (mSlicers[i]->GetImageActor()->GetVisibility() == 1)
922 emit UpdateWindows(i,mSlicers[i]->GetSliceOrientation(),mSlicers[i]->GetSlice());
924 emit UpdateWindows(i,-1,-1);
928 //----------------------------------------------------------------------------
931 //----------------------------------------------------------------------------
932 void vvSlicerManager::Activated()
934 emit currentImageChanged(mId);
936 //----------------------------------------------------------------------------
939 //----------------------------------------------------------------------------
940 void vvSlicerManager::UpdateWindowLevel()
942 emit WindowLevelChanged(mSlicers[0]->GetColorWindow(),mSlicers[0]->GetColorLevel(),mPreset,mColorMap);
944 //----------------------------------------------------------------------------
947 //----------------------------------------------------------------------------
948 void vvSlicerManager::UpdateSlice(int slicer)
950 if (mPreviousSlice[slicer] == mSlicers[slicer]->GetSlice()) {
951 //DD("============= NOTHING");
954 emit UpdateSlice(slicer, mSlicers[slicer]->GetSlice());
955 mSlicers[slicer]->Render(); // DS <-- I add this, this could/must be the only Render ...
956 mPreviousSlice[slicer] = mSlicers[slicer]->GetSlice();
958 //----------------------------------------------------------------------------
961 //----------------------------------------------------------------------------
962 void vvSlicerManager::UpdateTSlice(int slicer)
964 if (mPreviousSlice[slicer] == mSlicers[slicer]->GetSlice()) {
965 if (mPreviousTSlice[slicer] == mSlicers[slicer]->GetTSlice()) {
966 // DD("************** NOTHING ***********");
970 mPreviousSlice[slicer] = mSlicers[slicer]->GetSlice();
971 mPreviousTSlice[slicer] = mSlicers[slicer]->GetTSlice();
972 emit UpdateTSlice(slicer,mSlicers[slicer]->GetTSlice());
974 //----------------------------------------------------------------------------
977 //----------------------------------------------------------------------------
978 void vvSlicerManager::UpdateSliceRange(int slicer)
980 emit UpdateSliceRange(slicer,
981 mSlicers[slicer]->GetSliceRange()[0], mSlicers[slicer]->GetSliceRange()[1],
982 0,mSlicers[slicer]->GetTMax());
984 //----------------------------------------------------------------------------
987 //----------------------------------------------------------------------------
988 void vvSlicerManager::SetPreset(int preset)
990 //vtkLookupTable* LUT = static_cast<vtkLookupTable*>(mSlicers[0]->GetWindowLevel()->GetLookupTable());
991 double window = mSlicers[0]->GetColorWindow();
992 double level = mSlicers[0]->GetColorLevel();
994 std::string component_type=mImage->GetScalarTypeAsITKString();
998 mImage->GetScalarRange(range);
999 window = range[1] - range[0];
1000 level = (range[1] + range[0])* 0.5;
1030 this->SetColorWindow(window);
1031 this->SetColorLevel(level);
1038 //----------------------------------------------------------------------------
1041 //----------------------------------------------------------------------------
1042 void vvSlicerManager::SetLocalColorWindowing(const int slicer)
1045 this->mSlicers[slicer]->GetExtremasAroundMousePointer(min, max);
1046 this->SetColorWindow(max-min);
1047 this->SetColorLevel(0.5*(min+max));
1048 this->UpdateWindowLevel();
1051 //----------------------------------------------------------------------------
1054 //----------------------------------------------------------------------------
1055 void vvSlicerManager::SetColorMap()
1057 SetColorMap(mColorMap);
1059 //----------------------------------------------------------------------------
1062 //----------------------------------------------------------------------------
1063 void vvSlicerManager::SetColorMap(int colormap)
1066 range[0] = mSlicers[0]->GetInput()->GetScalarRange()[0];
1067 range[1] = mSlicers[0]->GetInput()->GetScalarRange()[1];
1069 double window = mSlicers[0]->GetWindowLevel()->GetWindow();
1070 double level = mSlicers[0]->GetWindowLevel()->GetLevel();
1072 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(mSlicers[0]->GetWindowLevel()->GetLookupTable());
1081 LUT = vtkLookupTable::New();
1082 LUT->SetValueRange(0,1);
1083 LUT->SetSaturationRange(1,1);
1084 LUT->SetHueRange(0,0.18);
1088 LUT = vtkLookupTable::New();
1089 LUT->SetValueRange(0,1);
1090 LUT->SetSaturationRange(1,1);
1091 LUT->SetHueRange(0.4,0.80);
1095 LUT = vtkLookupTable::New();
1096 LUT->SetValueRange(0,1);
1097 LUT->SetSaturationRange(1,1);
1098 LUT->SetHueRange(0,1);
1102 LUT = vtkLookupTable::New();
1103 LUT->SetValueRange(0.,1);
1104 LUT->SetSaturationRange(1,1);
1105 LUT->SetHueRange(1,0.1);
1106 //LUT->SetRampToLinear();
1110 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1113 vtkLookupTable* fusLUT = NULL;
1114 if (mSlicers[0]->GetFusion()) {
1115 fusLUT = vtkLookupTable::New();
1116 double fusRange [2];
1117 fusRange[0] = mFusionLevel - mFusionWindow/2;
1118 fusRange[1] = mFusionLevel + mFusionWindow/2;
1119 fusLUT->SetTableRange(fusRange[0],fusRange[1]);
1120 fusLUT->SetValueRange(1,1);
1121 fusLUT->SetSaturationRange(1,1);
1122 if (mFusionColorMap == 1)
1123 fusLUT->SetHueRange(0,0.18);
1124 else if (mFusionColorMap == 2)
1125 fusLUT->SetHueRange(0.4,0.80);
1126 else if (mFusionColorMap == 3)
1127 fusLUT->SetHueRange(0,1);
1129 if (mFusionColorMap == 0)
1132 for ( unsigned int i = 0; i < mSlicers.size(); i++) {
1133 if (mSlicers[i]->GetOverlay() && mSlicers[i]->GetOverlayActor()->GetVisibility()) {
1134 vtkLookupTable* supLUT = vtkLookupTable::New();
1135 supLUT->SetTableRange(range[0],range[1]);
1136 supLUT->SetValueRange(1,1);
1137 supLUT->SetSaturationRange(1,1);
1138 supLUT->SetHueRange(double(mOverlayColor)/360,double(mOverlayColor)/360);
1140 vtkLookupTable* invLUT = vtkLookupTable::New();
1141 invLUT->SetTableRange(range[0],range[1]);
1142 invLUT->SetValueRange(1,1);
1143 invLUT->SetSaturationRange(1,1);
1144 invLUT->SetHueRange(double((mOverlayColor+180)%360)/360,double((mOverlayColor+180)%360)/360);
1146 dynamic_cast<vvImageMapToWLColors*>(mSlicers[i]->GetWindowLevel())
1147 ->SetWindowLevelMode(true);
1148 mSlicers[i]->GetWindowLevel()->SetLookupTable(supLUT);
1149 mSlicers[i]->GetOverlayMapper()->SetLookupTable(invLUT);
1152 } else if (mSlicers[i]->GetOverlay()) {
1153 //dynamic_cast<vvImageMapToWLColors*>(mSlicers[i]->GetWindowLevel())
1154 //->SetWindowLevelMode(false);
1155 mSlicers[i]->GetWindowLevel()->SetLookupTable(LUT);
1157 mSlicers[i]->GetWindowLevel()->SetLookupTable(LUT);
1159 if (mSlicers[i]->GetFusion() && mSlicers[i]->GetFusionActor()->GetVisibility()) {
1160 mSlicers[i]->GetFusionActor()->SetOpacity(double(mFusionOpacity)/100);
1161 mSlicers[i]->GetFusionMapper()->SetLookupTable(fusLUT);
1162 mSlicers[i]->GetFusionMapper()->SetWindow(mFusionWindow);
1163 mSlicers[i]->GetFusionMapper()->SetLevel(mFusionLevel);
1169 mColorMap = colormap;
1171 //----------------------------------------------------------------------------
1174 //----------------------------------------------------------------------------
1175 vvLandmarks* vvSlicerManager::GetLandmarks()
1177 if (mLandmarks == NULL) {
1178 mLandmarks = new vvLandmarks(mSlicers[0]->GetTMax()+1);
1179 for (unsigned int i = 0; i < mSlicers.size(); i++)
1180 mSlicers[i]->SetLandmarks(mLandmarks);
1184 //----------------------------------------------------------------------------
1187 //----------------------------------------------------------------------------
1188 void vvSlicerManager::AddLandmark(float x,float y,float z,float t)
1190 double x_index = (x - mSlicers[0]->GetInput()->GetOrigin()[0])/mSlicers[0]->GetInput()->GetSpacing()[0];
1191 double y_index = (y - mSlicers[0]->GetInput()->GetOrigin()[1])/mSlicers[0]->GetInput()->GetSpacing()[1];
1192 double z_index = (z - mSlicers[0]->GetInput()->GetOrigin()[2])/mSlicers[0]->GetInput()->GetSpacing()[2];
1193 if (x_index >= mSlicers[0]->GetInput()->GetWholeExtent()[0] &&
1194 x_index <= mSlicers[0]->GetInput()->GetWholeExtent()[1] &&
1195 y_index >= mSlicers[0]->GetInput()->GetWholeExtent()[2] &&
1196 y_index <= mSlicers[0]->GetInput()->GetWholeExtent()[3] &&
1197 z_index >= mSlicers[0]->GetInput()->GetWholeExtent()[4] &&
1198 z_index <= mSlicers[0]->GetInput()->GetWholeExtent()[5]) {
1199 double value = this->GetScalarComponentAsDouble(mSlicers[0]->GetInput(), x_index, y_index, z_index);
1200 this->GetLandmarks()->AddLandmark(x,y,z,t,value);
1201 emit LandmarkAdded();
1204 //----------------------------------------------------------------------------
1206 //----------------------------------------------------------------------------
1207 void vvSlicerManager::PrevImage(int slicer)
1209 emit ChangeImageWithIndexOffset(this, slicer, -1);
1211 //----------------------------------------------------------------------------
1213 //----------------------------------------------------------------------------
1214 void vvSlicerManager::NextImage(int slicer)
1216 emit ChangeImageWithIndexOffset(this, slicer, 1);
1218 //----------------------------------------------------------------------------
1220 //----------------------------------------------------------------------------
1221 void vvSlicerManager::VerticalSliderHasChanged(int slicer, int slice)
1223 emit AVerticalSliderHasChanged(slicer, slice);
1226 //----------------------------------------------------------------------------
1228 //----------------------------------------------------------------------------
1229 double vvSlicerManager::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int component)
1232 return mSlicers[0]->GetScalarComponentAsDouble(image, X, Y, Z, ix, iy, iz, component);
1234 //----------------------------------------------------------------------------