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