]> Creatis software - clitk.git/blob - vv/vvSlicerManager.cxx
Merge branch 'master' of git://git.creatis.insa-lyon.fr/clitk
[clitk.git] / vv / vvSlicerManager.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ======================================================================-====*/
18
19 #include "vvSlicerManager.h"
20 #include "vvSlicer.h"
21 #include "vvImage.h"
22 #include "vvSlicerManagerCommand.h"
23 #include "vvInteractorStyleNavigator.h"
24 #include "vvLandmarks.h"
25 #include "vvMesh.h"
26 #include "vvImageMapToWLColors.h"
27 #include "vvBlendImageActor.h"
28
29 #include <vtkImageActor.h>
30 #include <vtkImageData.h>
31 #include <vtkRenderWindow.h>
32 #include <vtkRendererCollection.h>
33 #include <vtkRenderWindowInteractor.h>
34 #include <vtkImageMapToWindowLevelColors.h>
35 #include <vtkWindowLevelLookupTable.h>
36 #include <vtkColorTransferFunction.h>
37 #include <vtkImageClip.h>
38 #include <vtkLODActor.h>
39 #include <vtkPointData.h>
40 #include <vtksys/SystemTools.hxx>
41 #include <vtkCamera.h>
42
43 #include <qfileinfo.h>
44
45 //----------------------------------------------------------------------------
46 vvSlicerManager::vvSlicerManager(int numberOfSlicers)
47 {
48   mFileName = "";
49   mId = "";
50   mVFName = "";
51   mOverlayName = "";
52   mFusionName = "";
53   mVFId = "";
54   mLastError = "";
55   mType = vvImageReader::UNDEFINEDIMAGETYPE;
56   mColorMap = 0;
57   mPreset = 0;
58   mOverlayColor = 130;
59
60   mFusionOpacity = 30;
61   mFusionThresOpacity = 1;
62   mFusionColorMap = 3;
63   mFusionWindow = 1000;
64   mFusionLevel = 1000;
65   mFusionShowLegend = true;
66   
67   mLandmarks = NULL;
68   mLinkedId.resize(0);
69
70   for ( int i = 0; i < numberOfSlicers; i++)
71     mSlicers.push_back(vtkSmartPointer<vvSlicer>::New());
72
73   mPreviousSlice.resize(numberOfSlicers);
74   mPreviousTSlice.resize(numberOfSlicers);
75 }
76 //----------------------------------------------------------------------------
77
78
79 //----------------------------------------------------------------------------
80 vvSlicerManager::~vvSlicerManager()
81 {
82   if (mLandmarks)
83     delete mLandmarks;
84 }
85 //----------------------------------------------------------------------------
86
87
88 //------------------------------------------------------------------------------
89 void vvSlicerManager::SetFilename(std::string filename, int number)
90 {
91   mFileName = filename;
92   mFileName = vtksys::SystemTools::GetFilenameName(mFileName);
93   mBaseFileName = vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(mFileName));
94   mBaseFileNameNumber = number;
95
96   mFileName = mBaseFileName;
97   if (number != 0) {
98     mFileName.append("_"+clitk::toString(number));
99   }
100   mFileName.append(vtksys::SystemTools::GetFilenameLastExtension(filename));
101
102   for(unsigned int i=0; i<mSlicers.size(); i++) {
103     mSlicers[i]->SetFileName(mFileName);//vtksys::SystemTools::GetFilenameWithoutLastExtension(filename));
104   }
105   
106 }
107 //------------------------------------------------------------------------------
108
109
110 //----------------------------------------------------------------------------
111 void vvSlicerManager::AddContour(vvMesh::Pointer contour,bool propagate)
112 {
113   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
114     mSlicers[i]->AddContour(contour,propagate);
115   }
116 }
117 //----------------------------------------------------------------------------
118
119
120 //----------------------------------------------------------------------------
121 void vvSlicerManager::ToggleContourSuperposition()
122 {
123   for ( unsigned int i = 0; i < mSlicers.size(); i++)
124     mSlicers[i]->ToggleContourSuperposition();
125 }
126 //----------------------------------------------------------------------------
127
128 //----------------------------------------------------------------------------
129 std::string vvSlicerManager::GetListOfAbsoluteFilePathInOneString(const std::string &actorType)
130 {
131   vvImageReader *reader = NULL;
132
133   if(actorType=="image")
134     reader = mReader;
135   else if(actorType=="overlay")
136     reader = mOverlayReader;
137   else if(actorType=="fusion")
138     reader = mFusionReader;
139   else if(actorType=="vector")
140     reader = mVectorReader;
141
142   if(!reader)
143     return "";
144
145   std::string list;
146   for(unsigned int i=0; i<reader->GetInputFilenames().size(); i++){
147     QFileInfo fileinfo(reader->GetInputFilenames()[i].c_str()); //Do not show the path
148     if(i)
149       list += '\n';
150     list += fileinfo.absoluteFilePath().toStdString();
151   }
152   return list;
153 }
154 //----------------------------------------------------------------------------
155
156
157 //----------------------------------------------------------------------------
158 bool vvSlicerManager::SetImage(std::string filename, vvImageReader::LoadedImageType type, int n, unsigned int slice)
159 {
160   mType = type;
161   if (mReader.IsNull())
162     mReader = vvImageReader::New();
163   std::vector<std::string> filenames;
164   filenames.push_back(filename);
165   mReader->SetInputFilenames(filenames);
166   mReader->SetSlice(slice); // Only used for SLICED type
167   mReader->Update(type);
168
169   SetFilename(filename, n);
170   //  mFileName = vtksys::SystemTools::GetFilenameName(mFileName);
171   //mBaseFileName = vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(mFileName));
172   //mBaseFileNameNumber = n;
173
174   if (mReader->GetLastError().size() == 0) {
175     mImage=mReader->GetOutput();
176     for ( unsigned int i = 0; i < mSlicers.size(); i++) {
177       mSlicers[i]->SetFileName(vtksys::SystemTools::GetFilenameWithoutLastExtension(filename));
178       mSlicers[i]->SetImage(mReader->GetOutput());
179     }
180   } else {
181     mLastError = mReader->GetLastError();
182     return false;
183   }
184   // if (n!=0) {
185   //   mFileName.append("_"+clitk::toString(n));
186   // }
187   return true;
188 }
189 //----------------------------------------------------------------------------
190
191
192 //----------------------------------------------------------------------------
193 void vvSlicerManager::SetImage(vvImage::Pointer image)
194 {
195   mImage=image;
196   for (unsigned int i = 0; i < mSlicers.size(); i++) {
197     mSlicers[i]->SetImage(image);
198   }
199 }
200 //----------------------------------------------------------------------------
201
202
203 //----------------------------------------------------------------------------
204 bool vvSlicerManager::SetImages(std::vector<std::string> filenames, vvImageReader::LoadedImageType type, int n)
205 {
206   mType = type;
207   std::string fileWithoutExtension = vtksys::SystemTools::GetFilenameWithoutExtension(filenames[0]);
208   if (type == vvImageReader::DICOM)
209     fileWithoutExtension += "_dicom";
210   else if (type == vvImageReader::MERGED)
211     fileWithoutExtension += "_merged";
212   else if (type == vvImageReader::MERGEDWITHTIME)
213     fileWithoutExtension += "_merged_wt";
214
215   mFileName = vtksys::SystemTools::GetFilenameName(mFileName);
216   mFileName = fileWithoutExtension + vtksys::SystemTools::GetFilenameExtension(filenames[0]);
217   if (mReader.IsNull())
218     mReader = vvImageReader::New();
219   mReader->SetInputFilenames(filenames);
220   mReader->Update(type);
221
222   mBaseFileName = vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(mFileName));
223   mBaseFileNameNumber = n;
224
225   if (mReader->GetLastError().size() == 0) {
226     mImage=mReader->GetOutput();
227     for ( unsigned int i = 0; i < mSlicers.size(); i++) {
228       mSlicers[i]->SetFileName(fileWithoutExtension);
229       mSlicers[i]->SetImage(mReader->GetOutput());
230     }
231   } else {
232     mLastError = mReader->GetLastError();
233     return false;
234   }
235   if (n!=0) {
236     mFileName.append("_"+clitk::toString(n));
237   }
238   return true;
239 }
240 //----------------------------------------------------------------------------
241
242
243 //----------------------------------------------------------------------------
244 bool vvSlicerManager::SetOverlay(std::string filename,int dim, std::string component)
245 {
246   mOverlayName = filename;
247   mOverlayComponent = component;
248   if (dim > mImage->GetNumberOfDimensions()) {
249     mLastError = " Overlay dimension cannot be greater then reference image!";
250     return false;
251   }
252   if (mOverlayReader.IsNull())
253     mOverlayReader = vvImageReader::New();
254   std::vector<std::string> filenames;
255   filenames.push_back(filename);
256   mOverlayReader->SetInputFilenames(filenames);
257   mOverlayReader->Update(mImage->GetNumberOfDimensions(),component.c_str(),mType);
258   if (mOverlayReader->GetLastError().size() == 0) {
259     for ( unsigned int i = 0; i < mSlicers.size(); i++) {
260       mSlicers[i]->SetOverlay(mOverlayReader->GetOutput());
261     }
262   } else {
263     mLastError = mOverlayReader->GetLastError();
264     return false;
265   }
266   return true;
267 }
268 //----------------------------------------------------------------------------
269
270
271 //----------------------------------------------------------------------------
272 bool vvSlicerManager::SetFusion(std::string filename,int dim, std::string component)
273 {
274   mFusionName = filename;
275   mFusionComponent = component;
276   if (dim > mImage->GetNumberOfDimensions()) {
277     mLastError = " Overlay dimension cannot be greater then reference image!";
278     return false;
279   }
280   if (mFusionReader.IsNull())
281     mFusionReader = vvImageReader::New();
282   std::vector<std::string> filenames;
283   filenames.push_back(filename);
284   mFusionReader->SetInputFilenames(filenames);
285   mFusionReader->Update(mImage->GetNumberOfDimensions(),component.c_str(),mType);
286   if (mFusionReader->GetLastError().size() == 0) {
287     for ( unsigned int i = 0; i < mSlicers.size(); i++) {
288       mSlicers[i]->SetFusion(mFusionReader->GetOutput());
289     }
290   } else {
291     mLastError = mFusionReader->GetLastError();
292     return false;
293   }
294   double *fusRange = mFusionReader->GetOutput()->GetVTKImages()[0]->GetScalarRange();
295   mFusionLevel = (fusRange[0]+fusRange[1])/2;
296   mFusionWindow = fusRange[1]-fusRange[0];
297
298   return true;
299 }
300 //----------------------------------------------------------------------------
301
302
303 //----------------------------------------------------------------------------
304 bool vvSlicerManager::SetVF(std::string filename)
305 {
306   if (mVectorReader.IsNull())
307     mVectorReader = vvImageReader::New();
308   mVectorReader->SetInputFilename(filename);
309   
310   if (mType == vvImageReader::IMAGEWITHTIME)
311     mVectorReader->Update(vvImageReader::VECTORFIELDWITHTIME);
312   else
313     mVectorReader->Update(vvImageReader::VECTORFIELD);
314   if (mVectorReader->GetLastError().size() != 0) {
315     mLastError = mVectorReader->GetLastError();
316     return false;
317   } else
318     return SetVF(mVectorReader->GetOutput(),filename);
319 }
320 //----------------------------------------------------------------------------
321
322
323 //----------------------------------------------------------------------------
324 bool vvSlicerManager::SetVF(vvImage::Pointer vf,std::string filename)
325 {
326   if (vf->GetNumberOfDimensions() > mImage->GetNumberOfDimensions()) {
327     mLastError = "Sorry, vector field dimension cannot be greater then reference image.";
328     return false;
329   }
330   if (vf->GetNumberOfDimensions() == 4) {
331     if (vf->GetSpacing()[3] != mImage->GetSpacing()[3]) {
332       mLastError = "Sorry, vector field time spacing cannot be different from time spacing of the reference image.";
333       return false;
334     }
335     if (vf->GetOrigin()[3] != mImage->GetOrigin()[3]) {
336       mLastError = "Sorry, vector field time origin cannot be different from time origin of the reference image.";
337       return false;
338     }
339   }
340   mVF=vf;
341   mVFName = filename;
342   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
343     mSlicers[i]->SetVF(vf);
344   }
345   return true;
346 }
347 //----------------------------------------------------------------------------
348
349
350 //----------------------------------------------------------------------------
351 vvSlicer* vvSlicerManager::GetSlicer(int i)
352 {
353   return mSlicers[i];
354 }
355 //----------------------------------------------------------------------------
356
357
358 //----------------------------------------------------------------------------
359 void vvSlicerManager::UpdateSlicer(int num, bool state)
360 {
361   if (mSlicers[num]->GetImage())
362     mSlicers[num]->SetDisplayMode(state);
363 }
364 //----------------------------------------------------------------------------
365
366
367 //----------------------------------------------------------------------------
368 void vvSlicerManager::SetSlicerWindow(int i, vtkRenderWindow* RW)
369 {
370   mSlicers[i]->SetRenderWindow(i,RW);
371 }
372 //----------------------------------------------------------------------------
373
374
375 //----------------------------------------------------------------------------
376 void vvSlicerManager::SetInteractorStyleNavigator(int i, vtkInteractorStyle* style)
377 {
378   vvSlicerManagerCommand *smc = vvSlicerManagerCommand::New();
379   smc->SM = this;
380   smc->SetSlicerNumber(i);
381   mSlicers[i]->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style);
382
383   mSlicers[i]->GetRenderWindow()->GetInteractor()->
384     GetInteractorStyle()->AddObserver(vtkCommand::KeyPressEvent, smc);
385   mSlicers[i]->GetRenderWindow()->GetInteractor()->
386     GetInteractorStyle()->AddObserver(vtkCommand::WindowLevelEvent, smc);
387   mSlicers[i]->GetRenderWindow()->GetInteractor()->
388     GetInteractorStyle()->AddObserver(vtkCommand::EndWindowLevelEvent, smc);
389   mSlicers[i]->GetRenderWindow()->GetInteractor()->
390     GetInteractorStyle()->AddObserver(vtkCommand::StartWindowLevelEvent, smc);
391   mSlicers[i]->GetRenderWindow()->GetInteractor()->
392     GetInteractorStyle()->AddObserver(vtkCommand::PickEvent, smc);
393   mSlicers[i]->GetRenderWindow()->GetInteractor()->
394     GetInteractorStyle()->AddObserver(vtkCommand::StartPickEvent, smc);
395   mSlicers[i]->GetRenderWindow()->GetInteractor()->
396     GetInteractorStyle()->AddObserver(vtkCommand::LeaveEvent, smc);
397   mSlicers[i]->GetRenderWindow()->GetInteractor()->
398     GetInteractorStyle()->AddObserver(vtkCommand::UserEvent, smc);
399   mSlicers[i]->GetRenderWindow()->GetInteractor()->
400     GetInteractorStyle()->AddObserver(vtkCommand::MouseWheelForwardEvent, smc);
401   mSlicers[i]->GetRenderWindow()->GetInteractor()->
402     GetInteractorStyle()->AddObserver(vtkCommand::MouseWheelBackwardEvent, smc);
403   // mSlicers[i]->GetRenderWindow()->GetInteractor()->
404   //   GetInteractorStyle()->AddObserver(vtkCommand::LeftButtonReleaseEvent, smc);
405   mSlicers[i]->GetRenderWindow()->GetInteractor()->
406     GetInteractorStyle()->AddObserver(vtkCommand::EndPickEvent, smc);
407   mSlicers[i]->GetRenderWindow()->GetInteractor()->
408     GetInteractorStyle()->AddObserver(vtkCommand::EndInteractionEvent, smc);
409   smc->Delete();
410 }
411 //----------------------------------------------------------------------------
412
413
414 //----------------------------------------------------------------------------
415 void vvSlicerManager::LeftButtonReleaseEvent(int slicer)
416 {
417   emit LeftButtonReleaseSignal(slicer);
418 }
419 //----------------------------------------------------------------------------
420
421 //----------------------------------------------------------------------------
422 void vvSlicerManager::SetSliceOrientation(int slicer, int orientation)
423 {
424   mSlicers[slicer]->SetSliceOrientation(orientation);
425   emit UpdateOrientation(slicer, orientation);
426 }
427 //----------------------------------------------------------------------------
428
429 //----------------------------------------------------------------------------
430 void vvSlicerManager::SetTSlice(int slice)
431 {
432   if (slice < 0)
433     slice = 0;
434   else if (slice > mSlicers[0]->GetTMax())
435     slice = mSlicers[0]->GetTMax();
436   if (mLandmarks)
437     mLandmarks->SetTime(slice);
438   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
439     if (slice != mSlicers[i]->GetTSlice()) {
440       mSlicers[i]->SetTSlice(slice);
441         UpdateTSlice(i);
442     }
443   }
444 }
445 //----------------------------------------------------------------------------
446
447
448 //----------------------------------------------------------------------------
449 void vvSlicerManager::SetNextTSlice(int originating_slicer)
450 {
451   int t = mSlicers[0]->GetTSlice();
452   t++;
453   if (t > mSlicers[0]->GetTMax())
454     t = 0;
455   //std::cout << "vvSlicerManager::SetNextTSlice" << std::endl;
456   emit UpdateTSlice(originating_slicer,t);
457 }
458 //----------------------------------------------------------------------------
459
460
461 //----------------------------------------------------------------------------
462 void vvSlicerManager::SetPreviousTSlice(int originating_slicer)
463 {
464   int t = mSlicers[0]->GetTSlice();
465   t--;
466   if (t < 0)
467     t = mSlicers[0]->GetTMax();
468   //std::cout << "vvSlicerManager::SetPreviousTSlice" << std::endl;
469   emit UpdateTSlice(originating_slicer,t);
470 }
471 //----------------------------------------------------------------------------
472
473
474 //----------------------------------------------------------------------------
475 void vvSlicerManager::ToggleInterpolation()
476 {
477   bool interpolate=!(mSlicers[0]->GetImageActor()->GetInterpolate());
478   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
479     mSlicers[i]->GetImageActor()->SetInterpolate(interpolate);
480     if (mSlicers[i]->GetOverlayActor())
481       mSlicers[i]->GetOverlayActor()->SetInterpolate(interpolate);
482     if (mSlicers[i]->GetFusionActor())
483       mSlicers[i]->GetFusionActor()->SetInterpolate(interpolate);
484   }
485 }
486 //----------------------------------------------------------------------------
487
488
489 //----------------------------------------------------------------------------
490 void vvSlicerManager::SetTSliceInSlicer(int tslice, int slicer)
491 {
492   if (tslice < 0)
493     tslice = 0;
494   else if (tslice > mSlicers[slicer]->GetTMax())
495     tslice = mSlicers[slicer]->GetTMax();
496   if (mLandmarks)
497     mLandmarks->SetTime(tslice);
498
499   if (mSlicers[slicer]->GetTSlice() == tslice) return;
500
501   mSlicers[slicer]->SetTSlice(tslice);
502   UpdateTSlice(slicer);
503 }
504 //----------------------------------------------------------------------------
505
506
507 //----------------------------------------------------------------------------
508 void vvSlicerManager::SetColorWindow(double s)
509 {
510   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
511     mSlicers[i]->SetColorWindow(s);
512   }
513 }
514 //----------------------------------------------------------------------------
515
516 //----------------------------------------------------------------------------
517 void vvSlicerManager::SetColorLevel(double s)
518 {
519   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
520     mSlicers[i]->SetColorLevel(s);
521   }
522 }
523 //----------------------------------------------------------------------------
524
525 //----------------------------------------------------------------------------
526 void vvSlicerManager::SetOverlayColorWindow(double s)
527 {
528   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
529     mSlicers[i]->SetOverlayColorWindow(s);
530   }
531 }
532 //----------------------------------------------------------------------------
533
534 //----------------------------------------------------------------------------
535 void vvSlicerManager::SetOverlayColorLevel(double s)
536 {
537   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
538     mSlicers[i]->SetOverlayColorLevel(s);
539   }
540 }
541 //----------------------------------------------------------------------------
542
543 //----------------------------------------------------------------------------
544 void vvSlicerManager::SetLinkOverlayWindowLevel(bool b)
545 {
546   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
547     mSlicers[i]->SetLinkOverlayWindowLevel(b);
548   }
549 }
550 //----------------------------------------------------------------------------
551
552 //----------------------------------------------------------------------------
553 void vvSlicerManager::SetCursorAndCornerAnnotationVisibility(int s)
554 {
555   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
556     mSlicers[i]->SetCursorVisibility(s);
557     mSlicers[i]->SetCornerAnnotationVisibility(s);
558   }
559 }
560 //----------------------------------------------------------------------------
561
562 //----------------------------------------------------------------------------
563 void vvSlicerManager::SetOpacity(int i, double factor)
564 {
565   mSlicers[i]->SetOpacity(1/factor);
566 }
567 //----------------------------------------------------------------------------
568
569
570 //----------------------------------------------------------------------------
571 void vvSlicerManager::UpdateViews(int current,int slicer)
572 {
573   double x = (mSlicers[slicer]->GetCurrentPosition()[0] - mSlicers[slicer]->GetInput()->GetOrigin()[0])
574     /mSlicers[slicer]->GetInput()->GetSpacing()[0];
575   double y = (mSlicers[slicer]->GetCurrentPosition()[1] - mSlicers[slicer]->GetInput()->GetOrigin()[1])
576     /mSlicers[slicer]->GetInput()->GetSpacing()[1];
577   double z = (mSlicers[slicer]->GetCurrentPosition()[2] - mSlicers[slicer]->GetInput()->GetOrigin()[2])
578     /mSlicers[slicer]->GetInput()->GetSpacing()[2];
579
580   if (x >= mSlicers[slicer]->GetInput()->GetWholeExtent()[0] &&
581       x <= mSlicers[slicer]->GetInput()->GetWholeExtent()[1] &&
582       y >= mSlicers[slicer]->GetInput()->GetWholeExtent()[2] &&
583       y <= mSlicers[slicer]->GetInput()->GetWholeExtent()[3] &&
584       z >= mSlicers[slicer]->GetInput()->GetWholeExtent()[4] &&
585       z <= mSlicers[slicer]->GetInput()->GetWholeExtent()[5]) {
586     mSlicers[slicer]->UpdateCursorPosition();
587     mSlicers[slicer]->SetCursorColor(10,212,255);
588
589     switch (mSlicers[slicer]->GetSliceOrientation()) {
590     case vtkImageViewer2::SLICE_ORIENTATION_XY:
591       if (mSlicers[slicer]->GetSlice() != (int)floor(z))
592         mSlicers[slicer]->SetSlice((int)floor(z));
593       break;
594
595     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
596       if (mSlicers[slicer]->GetSlice() != (int)floor(y))
597         mSlicers[slicer]->SetSlice((int)floor(y));
598       break;
599
600     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
601       if (mSlicers[slicer]->GetSlice() != (int)floor(x))
602         mSlicers[slicer]->SetSlice((int)floor(x));
603       break;
604     }
605     mSlicers[slicer]->Render();
606
607     for ( unsigned int i = 0; i < mSlicers.size(); i++) {
608       if (i != (unsigned int)slicer
609           && mSlicers[i]->GetRenderer()->GetDraw()
610           && mSlicers[i]->GetRenderWindow()->GetSize()[0] > 2
611           && mSlicers[i]->GetRenderWindow()->GetSize()[1] > 2) {
612         mSlicers[i]->SetCurrentPosition(mSlicers[slicer]->GetCurrentPosition()[0],
613                                         mSlicers[slicer]->GetCurrentPosition()[1],
614                                         mSlicers[slicer]->GetCurrentPosition()[2],
615                                         mSlicers[slicer]->GetTSlice());
616         mSlicers[i]->UpdateCursorPosition();
617         if (current) { //do not display corner annotation if image is the one picked
618           mSlicers[i]->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
619                                           -VTK_DOUBLE_MAX, mSlicers[slicer]->GetTSlice());
620           mSlicers[i]->SetCursorColor(255,10,212);
621         } else {
622           mSlicers[i]->SetCursorColor(150,10,282);
623         }
624         switch (mSlicers[i]->GetSliceOrientation()) {
625         case vtkImageViewer2::SLICE_ORIENTATION_XY:
626           if (mSlicers[i]->GetSlice() != (int)floor(z))
627             mSlicers[i]->SetSlice((int)floor(z));
628           break;
629
630         case vtkImageViewer2::SLICE_ORIENTATION_XZ:
631           if (mSlicers[i]->GetSlice() != (int)floor(y))
632             mSlicers[i]->SetSlice((int)floor(y));
633           break;
634
635         case vtkImageViewer2::SLICE_ORIENTATION_YZ:
636           if (mSlicers[i]->GetSlice() != (int)floor(x))
637             mSlicers[i]->SetSlice((int)floor(x));
638           break;
639         }
640         
641         mSlicers[i]->Render();
642         
643         UpdateSlice(i);
644         UpdateTSlice(i);
645       }
646     }
647   }
648 }
649 //----------------------------------------------------------------------------
650
651
652 //----------------------------------------------------------------------------
653 void vvSlicerManager::UpdateLinked(int slicer)
654 {
655   double x = (mSlicers[slicer]->GetCurrentPosition()[0] - mSlicers[slicer]->GetInput()->GetOrigin()[0])
656     /mSlicers[slicer]->GetInput()->GetSpacing()[0];
657   double y = (mSlicers[slicer]->GetCurrentPosition()[1] - mSlicers[slicer]->GetInput()->GetOrigin()[1])
658     /mSlicers[slicer]->GetInput()->GetSpacing()[1];
659   double z = (mSlicers[slicer]->GetCurrentPosition()[2] - mSlicers[slicer]->GetInput()->GetOrigin()[2])
660     /mSlicers[slicer]->GetInput()->GetSpacing()[2];
661
662   if (x >= mSlicers[slicer]->GetInput()->GetWholeExtent()[0] &&
663       x <= mSlicers[slicer]->GetInput()->GetWholeExtent()[1] &&
664       y >= mSlicers[slicer]->GetInput()->GetWholeExtent()[2] &&
665       y <= mSlicers[slicer]->GetInput()->GetWholeExtent()[3] &&
666       z >= mSlicers[slicer]->GetInput()->GetWholeExtent()[4] &&
667       z <= mSlicers[slicer]->GetInput()->GetWholeExtent()[5]) {
668     for (std::list<std::string>::const_iterator i = mLinkedId.begin(); i != mLinkedId.end(); i++) {
669       emit UpdateLinkManager(*i, slicer,mSlicers[slicer]->GetCurrentPosition()[0],
670                              mSlicers[slicer]->GetCurrentPosition()[1],
671                              mSlicers[slicer]->GetCurrentPosition()[2],mSlicers[slicer]->GetTSlice());
672     }
673   }
674 }
675 //----------------------------------------------------------------------------
676
677 //----------------------------------------------------------------------------
678 void vvSlicerManager::UpdateLinkedNavigation(vvSlicer *refSlicer, bool bPropagate)
679 {
680   vtkCamera *refCam = refSlicer->GetRenderer()->GetActiveCamera();
681   double refPosition[3], refFocal[3];
682   refCam->GetPosition(refPosition);
683   refCam->GetFocalPoint(refFocal);
684   
685   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
686     vtkCamera *camera = mSlicers[i]->GetRenderer()->GetActiveCamera();
687     camera->SetParallelScale(refCam->GetParallelScale());
688
689     double position[3], focal[3];
690     camera->GetPosition(position);
691     camera->GetFocalPoint(focal);
692
693     if(refSlicer->GetSliceOrientation()==mSlicers[i]->GetSliceOrientation()) {
694       for(int i=0; i<3; i++) {
695         position[i] = refPosition[i];
696         focal[i]    = refFocal[i];
697       }
698     }
699
700     if(refSlicer->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XY) {
701       if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XZ) {
702         position[0] = refPosition[0];
703         focal[0]    = refFocal[0];
704       }
705       if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_YZ) {
706         position[1] = refPosition[1];
707         focal[1]    = refFocal[1];
708       }
709     }
710
711     if(refSlicer->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XZ) {
712       if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_YZ) {
713         position[2] = refPosition[2];
714         focal[2]    = refFocal[2];
715       }
716       if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XY) {
717         position[0] = refPosition[0];
718         focal[0]    = refFocal[0];
719       }
720     }
721
722     if(refSlicer->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_YZ) {
723       if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XY) {
724         position[1] = refPosition[1];
725         focal[1]    = refFocal[1];
726       }
727       if(mSlicers[i]->GetSliceOrientation()==vtkImageViewer2::SLICE_ORIENTATION_XZ) {
728         position[2] = refPosition[2];
729         focal[2]    = refFocal[2];
730       }
731     }
732
733     camera->SetFocalPoint(focal);
734     camera->SetPosition(position);
735   
736     //Fix for bug #243
737     mSlicers[i]->ForceUpdateDisplayExtent();
738   }
739   
740   Render();
741   if(bPropagate)
742     for (std::list<std::string>::const_iterator i = mLinkedId.begin(); i != mLinkedId.end(); i++)
743       emit UpdateLinkedNavigation(*i, this, refSlicer);
744 }
745 //----------------------------------------------------------------------------
746
747 //----------------------------------------------------------------------------
748 double vvSlicerManager::GetColorWindow()
749 {
750   if (mSlicers.size())
751     return mSlicers[0]->GetColorWindow();
752   return -1;
753 }
754 //----------------------------------------------------------------------------
755
756
757 //----------------------------------------------------------------------------
758 double vvSlicerManager::GetColorLevel()
759 {
760   if (mSlicers.size())
761     return mSlicers[0]->GetColorLevel();
762   return -1;
763 }
764 //----------------------------------------------------------------------------
765
766 //----------------------------------------------------------------------------
767 double vvSlicerManager::GetOverlayColorWindow() const
768 {
769   if (mSlicers.size())
770     return mSlicers[0]->GetOverlayColorWindow();
771   return -1;
772 }
773 //----------------------------------------------------------------------------
774
775 //----------------------------------------------------------------------------
776 double vvSlicerManager::GetOverlayColorLevel() const
777 {
778   if (mSlicers.size())
779     return mSlicers[0]->GetOverlayColorLevel();
780   return -1;
781 }
782 //----------------------------------------------------------------------------
783
784 //----------------------------------------------------------------------------
785 bool vvSlicerManager::GetLinkOverlayWindowLevel() const
786 {
787   if (mSlicers.size())
788     return mSlicers[0]->GetLinkOverlayWindowLevel();
789   return -1;
790 }
791 //----------------------------------------------------------------------------
792
793 //------------------------------------------------------------------------------
794 void vvSlicerManager::ResetTransformationToIdentity(const std::string actorType)
795 {
796   if(actorType == "image")
797     this->GetImage()->GetTransform()->Identity();
798   else if(actorType == "overlay")
799     this->GetSlicer(0)->GetOverlay()->GetTransform()->Identity();
800   else if(actorType == "fusion")
801     this->GetSlicer(0)->GetFusion()->GetTransform()->Identity();
802   else if(actorType == "vf")
803     this->GetVF()->GetTransform()->Identity();
804   else
805     return;
806
807   for(int i=0; i< this->GetNumberOfSlicers(); i++){
808     this->GetSlicer(i)->ForceUpdateDisplayExtent();
809     this->GetSlicer(i)->ResetCamera();
810     this->GetSlicer(i)->Render();
811   }
812 }
813 //------------------------------------------------------------------------------
814
815 //----------------------------------------------------------------------------
816 void vvSlicerManager::Render()
817 {
818   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
819     mSlicers[i]->Render();
820   }
821 }
822 //----------------------------------------------------------------------------
823
824
825 //----------------------------------------------------------------------------
826 void vvSlicerManager::GenerateDefaultLookupTable()
827 {
828   SetPreset(mPreset);
829   SetColorMap(mColorMap);
830 }
831 //----------------------------------------------------------------------------
832
833
834 //----------------------------------------------------------------------------
835 void vvSlicerManager::Reload()
836 {
837   mReader->Update(mType);
838   mImage=mReader->GetOutput();
839   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
840     mSlicers[i]->SetImage(mImage);
841   }
842 }
843 //----------------------------------------------------------------------------
844
845
846 //----------------------------------------------------------------------------
847 void vvSlicerManager::ReloadFusion()
848 {
849   mFusionReader->Update(mImage->GetNumberOfDimensions(),mFusionComponent.c_str(),mType);
850
851   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
852     mSlicers[i]->SetFusion(mFusionReader->GetOutput());
853     mSlicers[i]->Render();
854   }
855 }
856 //----------------------------------------------------------------------------
857
858
859 //----------------------------------------------------------------------------
860 void vvSlicerManager::ReloadOverlay()
861 {
862   mOverlayReader->Update(mImage->GetNumberOfDimensions(),mOverlayComponent.c_str(),mType);
863   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
864     mSlicers[i]->SetOverlay(mOverlayReader->GetOutput());
865     mSlicers[i]->Render();
866   }
867 }
868 //----------------------------------------------------------------------------
869
870
871 //----------------------------------------------------------------------------
872 void vvSlicerManager::ReloadVF()
873 {
874   mVectorReader->Update(vvImageReader::VECTORFIELD); //deletes the old images through the VF::Init() function
875   mVF=mVectorReader->GetOutput();
876   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
877     mSlicers[i]->SetVF(mVF);
878     mSlicers[i]->Render();
879   }
880 }
881 //----------------------------------------------------------------------------
882
883
884 //----------------------------------------------------------------------------
885 void vvSlicerManager::RemoveActor(const std::string& actor_type, int overlay_index)
886 {
887   if (actor_type =="overlay")
888     mOverlayReader = NULL;
889
890   if (actor_type =="fusion")
891     mFusionReader = NULL;
892
893   for (unsigned int i = 0; i < mSlicers.size(); i++)
894     mSlicers[i]->RemoveActor(actor_type,overlay_index);
895
896   if (actor_type=="vector") {
897     mVF=NULL;
898     mVectorReader=NULL;
899   }
900 }
901 //----------------------------------------------------------------------------
902
903
904 //----------------------------------------------------------------------------
905 void vvSlicerManager::RemoveActors()
906 {
907   ///This method leaks a few objects. See RemoveActor for what a
908   ///correct implementation would look like
909   //DS -> probably due to the reader (now released in the
910   //RemoveActor() function. (I hope)
911   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
912     mSlicers[i]->SetDisplayMode(0);
913     mSlicers[i]->GetRenderer()->RemoveActor(mSlicers[i]->GetImageActor());
914   }
915 }
916 //----------------------------------------------------------------------------
917
918
919 //----------------------------------------------------------------------------
920 void vvSlicerManager::UpdateInfoOnCursorPosition(int slicer)
921 {
922   //  int view = mSlicers[slicer]->GetSliceOrientation();
923   //  int slice = mSlicers[slicer]->GetSlice();
924   double x = mSlicers[slicer]->GetCursorPosition()[0];
925   double y = mSlicers[slicer]->GetCursorPosition()[1];
926   double z = mSlicers[slicer]->GetCursorPosition()[2];
927   double X = (x - mSlicers[slicer]->GetInput()->GetOrigin()[0])/
928     mSlicers[slicer]->GetInput()->GetSpacing()[0];
929   double Y = (y - mSlicers[slicer]->GetInput()->GetOrigin()[1])/
930     mSlicers[slicer]->GetInput()->GetSpacing()[1];
931   double Z = (z - mSlicers[slicer]->GetInput()->GetOrigin()[2])/
932     mSlicers[slicer]->GetInput()->GetSpacing()[2];
933   double value = -VTK_DOUBLE_MAX;
934   int displayVec = 0;
935   double xVec=0, yVec=0, zVec=0, valueVec=0;
936   int displayOver = 0;
937   int displayFus = 0;
938   double valueOver=0, valueFus=0;
939   if (X >= mSlicers[slicer]->GetInput()->GetWholeExtent()[0] &&
940       X <= mSlicers[slicer]->GetInput()->GetWholeExtent()[1] &&
941       Y >= mSlicers[slicer]->GetInput()->GetWholeExtent()[2] &&
942       Y <= mSlicers[slicer]->GetInput()->GetWholeExtent()[3] &&
943       Z >= mSlicers[slicer]->GetInput()->GetWholeExtent()[4] &&
944       Z <= mSlicers[slicer]->GetInput()->GetWholeExtent()[5]) {
945     value = this->GetScalarComponentAsDouble(mSlicers[slicer]->GetInput(), X, Y, Z);
946
947     if (mSlicers[slicer]->GetVFActor() ) {
948       displayVec = 1;
949       unsigned int currentTime = mSlicers[slicer]->GetTSlice();
950       vtkImageData *vf = NULL;
951
952       if (mSlicers[slicer]->GetVF()->GetVTKImages().size() > currentTime)
953         vf = mSlicers[slicer]->GetVF()->GetVTKImages()[currentTime];
954       else
955         vf = mSlicers[slicer]->GetVF()->GetVTKImages()[0];
956
957       if (vf) {
958         double Xvf = (x - vf->GetOrigin()[0])/ vf->GetSpacing()[0];
959         double Yvf = (y - vf->GetOrigin()[1])/ vf->GetSpacing()[1];
960         double Zvf = (z - vf->GetOrigin()[2])/ vf->GetSpacing()[2];
961         xVec = this->GetScalarComponentAsDouble( vf, Xvf, Yvf, Zvf, 0);
962         yVec = this->GetScalarComponentAsDouble( vf, Xvf, Yvf, Zvf, 1);
963         zVec = this->GetScalarComponentAsDouble( vf, Xvf, Yvf, Zvf, 2);
964         valueVec = sqrt(xVec*xVec + yVec*yVec + zVec*zVec);
965       }
966     }
967     if (mSlicers[slicer]->GetOverlayActor() ) {
968       displayOver = 1;
969       vtkImageData *overlay = dynamic_cast<vtkImageData*>(mSlicers[slicer]->GetOverlayMapper()->GetInput());
970       double Xover = (x - overlay->GetOrigin()[0]) / overlay->GetSpacing()[0];
971       double Yover = (y - overlay->GetOrigin()[1]) / overlay->GetSpacing()[1];
972       double Zover = (z - overlay->GetOrigin()[2]) / overlay->GetSpacing()[2];
973       valueOver = this->GetScalarComponentAsDouble(overlay, Xover, Yover, Zover);
974     }
975     if (mSlicers[slicer]->GetFusionActor() ) {
976       displayFus = 1;
977       vtkImageData *fusion = dynamic_cast<vtkImageData*>(mSlicers[slicer]->GetFusionMapper()->GetInput());
978       double Xover = (x - fusion->GetOrigin()[0]) / fusion->GetSpacing()[0];
979       double Yover = (y - fusion->GetOrigin()[1]) / fusion->GetSpacing()[1];
980       double Zover = (z - fusion->GetOrigin()[2]) / fusion->GetSpacing()[2];
981       valueFus = this->GetScalarComponentAsDouble(fusion, Xover, Yover, Zover);
982     }
983     emit UpdatePosition(mSlicers[slicer]->GetCursorVisibility(),
984                         x,y,z,X,Y,Z,value);
985     emit UpdateVector(displayVec,xVec, yVec, zVec, valueVec);
986     emit UpdateOverlay(displayOver,valueOver,value);
987     emit UpdateFusion(displayFus,valueFus);
988   }
989 }
990 //----------------------------------------------------------------------------
991
992
993 //----------------------------------------------------------------------------
994 void vvSlicerManager::Activated()
995 {
996   emit currentImageChanged(mId);
997 }
998 //----------------------------------------------------------------------------
999
1000
1001 //----------------------------------------------------------------------------
1002 void vvSlicerManager::Picked()
1003 {
1004   emit currentPickedImageChanged(mId);
1005 }
1006 //----------------------------------------------------------------------------
1007
1008
1009 //----------------------------------------------------------------------------
1010 void vvSlicerManager::UpdateWindowLevel()
1011 {
1012   emit WindowLevelChanged();
1013 }
1014 //----------------------------------------------------------------------------
1015
1016
1017 //----------------------------------------------------------------------------
1018 void vvSlicerManager::UpdateSlice(int slicer)
1019 {
1020   if (mPreviousSlice[slicer] == mSlicers[slicer]->GetSlice()) {
1021     //DD("============= NOTHING");
1022     return;
1023   }
1024   //std::cout << "vvSlicerManager::UpdateSlice " << slicer << " " << mSlicers[slicer]->GetSlice() << std::endl;
1025   emit UpdateSlice(slicer, mSlicers[slicer]->GetSlice());
1026   mSlicers[slicer]->Render(); // DS <-- I add this, this could/must be the only Render ...
1027   mPreviousSlice[slicer] = mSlicers[slicer]->GetSlice();
1028 }
1029 //----------------------------------------------------------------------------
1030
1031
1032 //----------------------------------------------------------------------------
1033 void vvSlicerManager::UpdateTSlice(int slicer)
1034 {
1035   int slice = mSlicers[slicer]->GetSlice();
1036   int tslice = mSlicers[slicer]->GetTSlice();
1037   if (mPreviousSlice[slicer] == slice) {
1038     if (mPreviousTSlice[slicer] == tslice) {
1039       //      DD("************** NOTHING ***********");
1040       return;
1041     }
1042   }
1043   mPreviousSlice[slicer] = slice;
1044   mPreviousTSlice[slicer] = tslice;
1045   //std::cout << "vvSlicerManager::UpdateTSlice " << slicer << " " << tslice << std::endl;
1046   emit UpdateTSlice(slicer, tslice);
1047 }
1048 //----------------------------------------------------------------------------
1049
1050
1051 //----------------------------------------------------------------------------
1052 void vvSlicerManager::UpdateSliceRange(int slicer)
1053 {
1054   emit UpdateSliceRange(slicer,
1055                         mSlicers[slicer]->GetSliceRange()[0], mSlicers[slicer]->GetSliceRange()[1],
1056                         0,mSlicers[slicer]->GetTMax());
1057 }
1058 //----------------------------------------------------------------------------
1059
1060
1061 //----------------------------------------------------------------------------
1062 void vvSlicerManager::SetPreset(int preset)
1063 {
1064   //vtkLookupTable* LUT = static_cast<vtkLookupTable*>(mSlicers[0]->GetWindowLevel()->GetLookupTable());
1065   double window = mSlicers[0]->GetColorWindow();
1066   double level = mSlicers[0]->GetColorLevel();
1067
1068   std::string component_type=mImage->GetScalarTypeAsITKString();
1069   switch (preset) {
1070   case 0:
1071     double range[2];
1072     mImage->GetScalarRange(range);
1073     window = range[1] - range[0];
1074     level = (range[1] + range[0])* 0.5;
1075     break;
1076   case 1:
1077     window = 2000;
1078     level = 0;
1079     break;
1080   case 2:
1081     window = 400;
1082     level = 20;
1083     break;
1084   case 3:
1085     window = 1500;
1086     level = -500;
1087     break;
1088   case 4:
1089     window = 1000;
1090     level = 500;
1091     break;
1092   case 5:
1093     window = 1;
1094     level = 0.5;
1095     break;
1096   case 6:
1097     break;
1098   case 7:
1099     window=1.;
1100     level=0.;
1101     break;
1102   }
1103   mPreset = preset;
1104   this->SetColorWindow(window);
1105   this->SetColorLevel(level);
1106
1107   //if (LUT)
1108   //{
1109   //    SetColorMap(-1);
1110   //}
1111 }
1112 //----------------------------------------------------------------------------
1113
1114
1115 //----------------------------------------------------------------------------
1116 void vvSlicerManager::SetLocalColorWindowing(const int slicer, const bool bCtrlKey)
1117 {
1118   double min, max;
1119   int t = this->mSlicers[slicer]->GetTSlice();
1120   if(bCtrlKey && this->mSlicers[slicer]->GetFusion()) {
1121     this->mSlicers[slicer]->GetExtremasAroundMousePointer(min, max,
1122                                                           this->mSlicers[slicer]->GetFusion()->GetVTKImages()[t],
1123                                                           this->mSlicers[slicer]->GetFusion()->GetTransform());
1124     this->SetFusionWindow(max-min);
1125     this->SetFusionLevel(0.5*(min+max));
1126     this->SetColorMap(mColorMap);
1127   }
1128   else if(bCtrlKey && this->mSlicers[slicer]->GetOverlay()) {
1129     this->mSlicers[slicer]->GetExtremasAroundMousePointer(min, max,
1130                                                           this->mSlicers[slicer]->GetOverlay()->GetVTKImages()[t],
1131                                                           this->mSlicers[slicer]->GetOverlay()->GetTransform());
1132     if(this->mSlicers[slicer]->GetLinkOverlayWindowLevel()){
1133       this->SetColorWindow(max-min);
1134       this->SetColorLevel(0.5*(min+max));
1135     } else {
1136       this->SetOverlayColorWindow(max-min);
1137       this->SetOverlayColorLevel(0.5*(min+max));
1138     }
1139   }
1140   else {
1141     this->mSlicers[slicer]->GetExtremasAroundMousePointer(min, max,
1142                                                           this->mSlicers[slicer]->GetImage()->GetVTKImages()[t],
1143                                                           this->mSlicers[slicer]->GetImage()->GetTransform());
1144     this->SetColorWindow(max-min);
1145     this->SetColorLevel(0.5*(min+max));
1146     this->SetPreset(6);
1147   }
1148   this->Render();
1149   this->UpdateWindowLevel();
1150 }
1151 //----------------------------------------------------------------------------
1152
1153
1154 //----------------------------------------------------------------------------
1155 void vvSlicerManager::SetColorMap(int colormap)
1156 {
1157   double range[2];
1158   range[0] = mSlicers[0]->GetInput()->GetScalarRange()[0];
1159   range[1] = mSlicers[0]->GetInput()->GetScalarRange()[1];
1160
1161   double window = mSlicers[0]->GetWindowLevel()->GetWindow();
1162   double level = mSlicers[0]->GetWindowLevel()->GetLevel();
1163
1164   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(mSlicers[0]->GetWindowLevel()->GetLookupTable());
1165   switch (colormap) {
1166   case -1:
1167     break;
1168   case 0:
1169     LUT = NULL;
1170     break;
1171   case 1:
1172     if (LUT == NULL)
1173       LUT = vtkLookupTable::New();
1174     LUT->SetValueRange(0,1);
1175     LUT->SetSaturationRange(1,1);
1176     LUT->SetHueRange(0,0.18);
1177     break;
1178   case 2:
1179     if (LUT == NULL)
1180       LUT = vtkLookupTable::New();
1181     LUT->SetValueRange(0,1);
1182     LUT->SetSaturationRange(1,1);
1183     LUT->SetHueRange(0.4,0.80);
1184     break;
1185   case 3:
1186     if (LUT == NULL)
1187       LUT = vtkLookupTable::New();
1188     LUT->SetValueRange(0.5,1);
1189     LUT->SetSaturationRange(1,1);
1190     LUT->SetHueRange(0.666,0);
1191     break;
1192   case 4:
1193     if (LUT == NULL)
1194       LUT = vtkLookupTable::New();
1195     LUT->SetValueRange(1,1);
1196     LUT->SetSaturationRange(1,1);
1197     LUT->SetHueRange(0,1);
1198     LUT->SetAlphaRange(1, 1);
1199     break;
1200   case 5:
1201     if (LUT == NULL)
1202       LUT = vtkLookupTable::New();
1203     LUT->SetValueRange(1,1);
1204     LUT->SetSaturationRange(1,1);
1205     LUT->SetHueRange(1,0.1);
1206     //LUT->SetRampToLinear();
1207     break;
1208   }
1209   if (LUT) {
1210     LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1211     LUT->Build();
1212   }
1213   vtkWindowLevelLookupTable* fusLUT = NULL;
1214   if (mSlicers[0]->GetFusion()) { // && mFusionColorMap >= 0) {
1215     fusLUT = vtkWindowLevelLookupTable::New();
1216     double fusRange [2];
1217     fusRange[0] = mFusionLevel - mFusionWindow/2;
1218     fusRange[1] = mFusionLevel + mFusionWindow/2;
1219     double* frange = mFusionReader->GetOutput()->GetVTKImages()[0]->GetScalarRange();
1220     fusLUT->SetTableRange(frange);
1221     fusLUT->SetValueRange(1,1);
1222     fusLUT->SetSaturationRange(1,1);
1223     fusLUT->SetAlphaRange(1, 1);
1224     fusLUT->SetWindow(mFusionWindow);
1225     fusLUT->SetLevel(mFusionLevel);
1226     if (mFusionColorMap == 1)
1227       fusLUT->SetHueRange(0,0.18);
1228     else if (mFusionColorMap == 2)
1229       fusLUT->SetHueRange(0.4,0.80);
1230     else if (mFusionColorMap == 3)
1231     {
1232       fusLUT->SetHueRange(0.666, 0);
1233       fusLUT->SetValueRange(0.5, 1);
1234     }
1235     else if (mFusionColorMap == 4)
1236       fusLUT->SetHueRange(0,1);
1237     else if (mFusionColorMap <= 0)
1238     {
1239       fusLUT->SetValueRange(0,1);
1240       fusLUT->SetSaturationRange(0,0);
1241     }
1242     
1243     fusLUT->ForceBuild();
1244     double v[4];
1245     
1246     // set color table transparency
1247     //double alpha_range=(double)mFusionThresOpacity/10;
1248     double range_end = fusRange[0] + (double)mFusionThresOpacity*(fusRange[1] - fusRange[0])/100;
1249     double curr_value = fusRange[0];
1250     int nvalues = fusLUT->GetNumberOfTableValues();
1251     for (double i = 0; curr_value < range_end; i++) {  
1252       fusLUT->GetTableValue(i, v);
1253       v[3] = 0;
1254       //if (curr_value >= -alpha_range && curr_value <= alpha_range) v[3] = pow(fabs(curr_value/alpha_range),2);
1255       //else v[3] = 1;
1256       fusLUT->SetTableValue(i, v);
1257       curr_value += (fusRange[1] - fusRange[0])/nvalues;
1258     }
1259   }
1260   for ( unsigned int i = 0; i < mSlicers.size(); i++) {
1261     
1262     if (mSlicers[i]->GetOverlay()) {
1263       vtkLookupTable* supLUT = vtkLookupTable::New();
1264       supLUT->SetTableRange(range[0],range[1]);
1265       supLUT->SetValueRange(1,1);
1266       supLUT->SetSaturationRange(1,1);
1267       supLUT->SetHueRange(double(mOverlayColor)/360,double(mOverlayColor)/360);
1268       supLUT->Build();
1269       vtkLookupTable* invLUT = vtkLookupTable::New();
1270       invLUT->SetTableRange(range[0],range[1]);
1271       invLUT->SetValueRange(1,1);
1272       invLUT->SetSaturationRange(1,1);
1273       invLUT->SetHueRange(double((mOverlayColor+180)%360)/360,double((mOverlayColor+180)%360)/360);
1274       invLUT->Build();
1275       dynamic_cast<vvImageMapToWLColors*>(mSlicers[i]->GetWindowLevel())
1276         ->SetWindowLevelMode(true);
1277       mSlicers[i]->GetWindowLevel()->SetLookupTable(supLUT);
1278       mSlicers[i]->GetOverlayMapper()->SetLookupTable(invLUT);
1279       invLUT->Delete();
1280       supLUT->Delete();
1281     } else if (mSlicers[i]->GetOverlay()) {
1282       //dynamic_cast<vvImageMapToWLColors*>(mSlicers[i]->GetWindowLevel())
1283       //->SetWindowLevelMode(false);
1284       mSlicers[i]->GetWindowLevel()->SetLookupTable(LUT);
1285     } else {
1286       mSlicers[i]->GetWindowLevel()->SetLookupTable(LUT);
1287     }
1288     
1289     if (mSlicers[i]->GetFusion()) {
1290       mSlicers[i]->ShowFusionLegend(mFusionShowLegend);
1291       mSlicers[i]->GetFusionMapper()->SetLookupTable(fusLUT);
1292       mSlicers[i]->GetFusionActor()->SetOpacity(double(mFusionOpacity)/100);
1293     }
1294   }
1295   if (fusLUT)
1296     fusLUT->Delete();
1297   if (colormap >= 0)
1298     mColorMap = colormap;
1299 }
1300 //----------------------------------------------------------------------------
1301
1302
1303 //----------------------------------------------------------------------------
1304 vvLandmarks* vvSlicerManager::GetLandmarks()
1305 {
1306   if (mLandmarks == NULL) {
1307     mLandmarks = new vvLandmarks(mSlicers[0]->GetTMax()+1);
1308     for (unsigned int i = 0; i < mSlicers.size(); i++)
1309       mSlicers[i]->SetLandmarks(mLandmarks);
1310   }
1311   return mLandmarks;
1312 }
1313 //----------------------------------------------------------------------------
1314
1315
1316 //----------------------------------------------------------------------------
1317 void vvSlicerManager::AddLandmark(float x,float y,float z,float t)
1318 {
1319   double x_index = (x - mSlicers[0]->GetInput()->GetOrigin()[0])/mSlicers[0]->GetInput()->GetSpacing()[0];
1320   double y_index = (y - mSlicers[0]->GetInput()->GetOrigin()[1])/mSlicers[0]->GetInput()->GetSpacing()[1];
1321   double z_index = (z - mSlicers[0]->GetInput()->GetOrigin()[2])/mSlicers[0]->GetInput()->GetSpacing()[2];
1322   if (x_index >= mSlicers[0]->GetInput()->GetWholeExtent()[0] &&
1323       x_index <= mSlicers[0]->GetInput()->GetWholeExtent()[1] &&
1324       y_index >= mSlicers[0]->GetInput()->GetWholeExtent()[2] &&
1325       y_index <= mSlicers[0]->GetInput()->GetWholeExtent()[3] &&
1326       z_index >= mSlicers[0]->GetInput()->GetWholeExtent()[4] &&
1327       z_index <= mSlicers[0]->GetInput()->GetWholeExtent()[5]) {
1328     double value = this->GetScalarComponentAsDouble(mSlicers[0]->GetInput(), x_index, y_index, z_index);
1329     this->GetLandmarks()->AddLandmark(x,y,z,t,value);
1330     emit LandmarkAdded();
1331   }
1332 }
1333 //----------------------------------------------------------------------------
1334
1335 //----------------------------------------------------------------------------
1336 void vvSlicerManager::PrevImage(int slicer)
1337 {
1338   emit ChangeImageWithIndexOffset(this, slicer, -1);
1339 }
1340 //----------------------------------------------------------------------------
1341
1342 //----------------------------------------------------------------------------
1343 void vvSlicerManager::NextImage(int slicer)
1344 {
1345   emit ChangeImageWithIndexOffset(this, slicer,  1);
1346 }
1347 //----------------------------------------------------------------------------
1348
1349 //----------------------------------------------------------------------------
1350 void vvSlicerManager::VerticalSliderHasChanged(int slicer, int slice)
1351 {
1352   emit AVerticalSliderHasChanged(slicer, slice);
1353 }
1354
1355 //----------------------------------------------------------------------------
1356
1357 //----------------------------------------------------------------------------
1358 double vvSlicerManager::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int component)
1359 {
1360   int ix, iy, iz;
1361   return mSlicers[0]->GetScalarComponentAsDouble(image, X, Y, Z, ix, iy, iz, component);
1362 }
1363 //----------------------------------------------------------------------------