]> Creatis software - clitk.git/blob - vv/vvSlicer.cxx
reloading the overlay works!
[clitk.git] / vv / vvSlicer.cxx
1 /*=========================================================================
2
3 Program:   vv
4 Language:  C++
5 Author :   Pierre Seroul (pierre.seroul@gmail.com)
6
7 Copyright (C) 2008
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 =========================================================================*/
24 #include "vvSlicer.h"
25
26 #include "vvImage.h"
27 #include "vvImage.h"
28 #include "vvSlicerManagerCommand.h"
29 #include "vvGlyphSource.h"
30 #include "vvGlyph2D.h"
31 #include "vvImageMapToWLColors.h"
32
33 #include <vtkTextProperty.h>
34 #include <vtkTextActor.h>
35 #include <vtkTextSource.h>
36 #include <vtkActor2D.h>
37 #include <vtkCursor2D.h>
38 #include <vtkPolyDataMapper2D.h>
39 #include <vtkProperty2D.h>
40 #include <vtkCornerAnnotation.h>
41 #include <vtkImageMapToWindowLevelColors.h>
42 #include <vtkImageData.h>
43 #include <vtkImageActor.h>
44 #include <vtkToolkits.h>
45 #include <vtkObjectFactory.h>
46 #include <vtkPointData.h>
47 #include <vtkDataArray.h>
48 #include <vtkFloatArray.h>
49 #include <vtkClipPolyData.h>
50 #include <vtkGlyph3D.h>
51 #include <vtkMath.h>
52 #include <vtkCursor3D.h>
53 #include <vtkProperty.h>
54 #include <vtkLight.h>
55 #include <vtkLightCollection.h>
56 #include <vtkScalarBarActor.h>
57 #include <vtkLookupTable.h>
58
59 #include <vtkRenderer.h>
60 #include <vtkRendererCollection.h>
61 #include <vtkRenderWindow.h>
62 #include <vtkRenderWindowInteractor.h>
63 #include <vtkCamera.h>
64 #include <vtkCallbackCommand.h>
65 #include <vtkCommand.h>
66 #include <vtkPolyDataMapper.h>
67 #include <vtkBox.h>
68
69 #include <vtkExtractVOI.h>
70 #include <vtkSphereSource.h>
71 #include <vtkCutter.h>
72 #include <vtkPlane.h>
73 #include <vtkAssignAttribute.h>
74
75 vtkCxxRevisionMacro(vvSlicer, "DummyRevision");
76 vtkStandardNewMacro(vvSlicer);
77
78 vvSlicer::vvSlicer()
79 {
80     mImage = NULL;
81     mCurrentTSlice = 0;
82
83     mCurrent[0] = -VTK_DOUBLE_MAX;
84     mCurrent[1] = -VTK_DOUBLE_MAX;
85     mCurrent[2] = -VTK_DOUBLE_MAX;
86
87     mCursor[0] = -VTK_DOUBLE_MAX;
88     mCursor[1] = -VTK_DOUBLE_MAX;
89     mCursor[2] = -VTK_DOUBLE_MAX;
90     mCursor[3] = -VTK_DOUBLE_MAX;
91
92     mSubSampling = 5;
93     mScale = 1;
94     mVFLog = 0;
95
96     std::string text = "F1 = sagital; F2 = coronal; F3 = axial\n";
97     text += "F5 = horizontal flip; F6 = vertical flip\n\n";
98     text += "0,1,2,3,4,5 : preset windowing\n";
99     text += "6,7,8,9 : preset colormap\n";
100     text += "r : reset view\n";
101     text += "l : reload image\n";
102     text += "f : fly to mouse position\n";
103     text += "g : go to cross hair position\n\n";
104     text += "Up,down : change slice\n";
105     text += "Left,right : change tenporal slice\n\n";
106     text += "Scrollbar (or w/x) : zoom in/out\n";
107     text += "left button : synchronize all views\n";
108     text += "middle button : grab image\n";
109     text += "right button : change windowing\n";
110
111     crossCursor = vtkCursor2D::New();
112     crossCursor->AllOff();
113     crossCursor->AxesOn();
114     crossCursor->SetTranslationMode(1);
115     crossCursor->SetRadius(2);
116
117     pdm = vtkPolyDataMapper2D::New();
118     pdm->SetInput(crossCursor->GetOutput());
119
120     pdmA = vtkActor2D::New();
121     pdmA->SetMapper(pdm);
122     pdmA->GetProperty()->SetColor(255,10,212);
123     pdmA->SetVisibility(0);
124     pdmA->SetPickable(0);
125
126     ca = vtkCornerAnnotation::New();
127     ca->GetTextProperty()->SetColor(255,10,212);
128     ca->SetVisibility(1);
129     mFileName = "";
130
131     mVF = NULL;
132     mOverlay = NULL;
133     mFusion = NULL;
134     mLandmarks = NULL;
135
136     legend = vtkSmartPointer<vtkScalarBarActor>::New();
137     //legend->SetTitle("test!");
138     legend->SetPosition(0.82,0.18);
139     legend->SetWidth(0.1);
140     legend->SetVisibility(0);
141     legend->SetLabelFormat("%.1f");
142     this->GetRenderer()->AddActor(legend);
143
144     this->WindowLevel->Delete();
145     this->WindowLevel = vvImageMapToWLColors::New();
146     this->InstallPipeline();
147 }
148
149 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper() {
150     return mOverlayMapper.GetPointer();
151 }
152 vtkImageActor* vvSlicer::GetOverlayActor() {
153     return mOverlayActor.GetPointer();
154 }
155 vtkImageMapToWindowLevelColors* vvSlicer::GetFusionMapper() {
156     return mFusionMapper.GetPointer();
157 }
158     
159 vtkImageActor* vvSlicer::GetFusionActor() {
160     return mFusionActor.GetPointer();
161 }
162 vtkActor* vvSlicer::GetVFActor() {
163     return mVFActor.GetPointer();
164 }
165 vtkCornerAnnotation* vvSlicer::GetAnnotation() {
166     return ca.GetPointer();
167 }
168
169 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
170 {
171
172     mSurfaceCutActors.push_back(new vvMeshActor());
173     if (propagate)
174         mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
175     else
176         mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
177     mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
178     this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
179
180     SetContourSlice();
181 }
182
183 void vvSlicer::ToggleContourSuperposition()
184 {
185     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
186             i!=mSurfaceCutActors.end();i++)
187         (*i)->ToggleSuperposition();
188 }
189
190 void vvSlicer::SetCursorColor(int r,int g, int b)
191 {
192     pdmA->GetProperty()->SetColor(r,g,b);
193 }
194 void vvSlicer::SetCursorVisibility(bool s)
195 {
196     pdmA->SetVisibility(s);
197 }
198 bool vvSlicer::GetCursorVisibility()
199 {
200     return pdmA->GetVisibility();
201 }
202
203 vvSlicer::~vvSlicer()
204 {
205     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
206             i!=mSurfaceCutActors.end();i++)
207         delete (*i);
208 }
209
210 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
211 {
212     mCurrent[0] = x;
213     mCurrent[1] = y;
214     mCurrent[2] = z;
215     mCurrentTSlice = t;
216 }
217
218 void vvSlicer::SetImage(vvImage::Pointer image)
219 {
220     if (image->GetVTKImages().size())
221     {
222         mImage = image;
223         this->Superclass::SetInput(image->GetVTKImages()[0]);
224         this->UpdateDisplayExtent();
225         mCurrentTSlice = 0;
226         ca->SetText(0,mFileName.c_str());
227     }
228 }
229
230 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
231 {
232     if (overlay->GetVTKImages().size())
233     {
234         mOverlay = overlay;
235
236         if (!mOverlayMapper)
237             mOverlayMapper = vtkImageMapToWindowLevelColors::New();
238         mOverlayMapper->SetInput(overlay->GetVTKImages()[0]);
239
240         if (!mOverlayActor)
241         {
242             mOverlayActor = vtkImageActor::New();
243             mOverlayActor->SetInput(mOverlayMapper->GetOutput());
244             mOverlayActor->SetPickable(0);
245             mOverlayActor->SetVisibility(false);
246             mOverlayActor->SetOpacity(0.5);
247             this->UpdateDisplayExtent();
248         }
249
250         //stupid but necessary : the Overlay need to be rendered before fusion
251         if (mFusionActor)
252         {
253             this->GetRenderer()->RemoveActor(mFusionActor);
254             this->GetRenderer()->AddActor(mOverlayActor);
255             this->GetRenderer()->AddActor(mFusionActor);
256         }
257         else
258             this->GetRenderer()->AddActor(mOverlayActor);
259
260         //Synchronize slice
261         SetTSlice(mCurrentTSlice);
262     }
263 }
264
265 void vvSlicer::SetFusion(vvImage::Pointer fusion)
266 {
267     if (fusion->GetVTKImages().size())
268     {
269         mFusion = fusion;
270
271         if (!mFusionMapper)
272             mFusionMapper = vtkImageMapToWindowLevelColors::New();
273         mFusionMapper->SetInput(fusion->GetVTKImages()[0]);
274
275         if (!mFusionActor)
276         {
277             mFusionActor = vtkImageActor::New();
278             mFusionActor->SetInput(mFusionMapper->GetOutput());
279             mFusionActor->SetPickable(0);
280             mFusionActor->SetVisibility(false);
281             mFusionActor->SetOpacity(0.7);
282             this->UpdateDisplayExtent();
283             this->GetRenderer()->AddActor(mFusionActor);
284         }
285
286         //Synchronize slice
287         SetTSlice(mCurrentTSlice);
288     }
289 }
290
291 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
292 {
293     if (actor_type == "vector")
294     {
295         this->mVFActor->SetVisibility(vis);
296     }
297     if (actor_type == "overlay")
298     {
299         this->mOverlayActor->SetVisibility(vis);
300     }
301     if (actor_type == "fusion")
302     {
303         this->mFusionActor->SetVisibility(vis);
304     }
305     if (actor_type == "contour")
306         this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
307     UpdateDisplayExtent();
308 }
309
310 void vvSlicer::SetVF(vvImage::Pointer vf)
311 {
312     if (vf->GetVTKImages().size())
313     {
314         mVF = vf;
315
316         if (!mAAFilter)
317         {
318             mAAFilter=vtkAssignAttribute::New();
319             mVOIFilter = vtkExtractVOI::New();
320             mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
321         }
322         mVOIFilter->SetInput(vf->GetVTKImages()[0]);
323         mAAFilter->SetInput(mVOIFilter->GetOutput());
324         ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
325         mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
326
327         if (!mArrow)
328             mArrow = vvGlyphSource::New();
329         mArrow->SetGlyphTypeToSpecificArrow();
330         mArrow->SetScale(mScale);
331         mArrow->FilledOff();
332
333         // Glyph the gradient vector (with arrows)
334         if (!mGlyphFilter)
335             mGlyphFilter = vvGlyph2D::New();
336         mGlyphFilter->SetInput(mAAFilter->GetOutput());
337         mGlyphFilter->SetSource(mArrow->GetOutput());
338         mGlyphFilter->ScalingOn();
339         mGlyphFilter->SetScaleModeToScaleByVector();
340         mGlyphFilter->OrientOn();
341         mGlyphFilter->SetVectorModeToUseVector();
342         mGlyphFilter->SetColorModeToColorByVector();
343
344         if (!mVFMapper)
345             mVFMapper = vtkPolyDataMapper::New();
346         //mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
347         mVFMapper->SetInput(mGlyphFilter->GetOutput());
348         mVFMapper->ImmediateModeRenderingOn();
349
350         if (!mVFActor)
351             mVFActor = vtkActor::New();
352         mVFActor->SetMapper(mVFMapper);
353         mVFActor->SetPickable(0);
354         this->UpdateDisplayExtent();
355         this->GetRenderer()->AddActor(mVFActor);
356
357         //Synchronize slice
358         SetTSlice(mCurrentTSlice);
359     }
360 }
361
362 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
363 {
364     mLandmarks = landmarks;
365     if (landmarks)
366     {
367
368         if (!mCross)
369             mCross = vtkCursor3D::New();
370         mCross->SetFocalPoint(0.0,0.0,0.0);
371         mCross->SetModelBounds(-10,10,-10,10,-10,10);
372         mCross->AllOff();
373         mCross->AxesOn();
374
375         if (!mLandGlyph)
376             mLandGlyph = vtkGlyph3D::New();
377         mLandGlyph->SetSource(mCross->GetOutput());
378         mLandGlyph->SetInput(landmarks->GetOutput());
379         //mLandGlyph->SetIndexModeToScalar();
380         mLandGlyph->SetRange(0,1);
381         mLandGlyph->ScalingOff();
382
383         mLandGlyph->SetColorModeToColorByScalar();
384
385         if (!mClipBox)
386             mClipBox = vtkBox::New();
387         if (!mLandClipper)
388             mLandClipper = vtkClipPolyData::New();
389         mLandClipper->InsideOutOn();
390         mLandClipper->SetInput(mLandGlyph->GetOutput());
391         mLandClipper->SetClipFunction(mClipBox);
392
393         if (!mLandMapper)
394             mLandMapper = vtkPolyDataMapper::New();
395         mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
396         //mLandMapper->ScalarVisibilityOff();
397
398         if (!mLandActor)
399             mLandActor = vtkActor::New();
400         mLandActor->SetMapper(mLandMapper);
401         mLandActor->GetProperty()->SetColor(255,10,212);
402         mLandActor->SetPickable(0);
403         mLandActor->SetVisibility(true);
404         this->UpdateDisplayExtent();
405         this->GetRenderer()->AddActor(mLandActor);
406     }
407 }
408
409 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
410 {
411     if (actor_type == "vector")
412     {
413         mGlyphFilter=NULL;
414         mVF = NULL;
415         mArrow = NULL;
416         mAAFilter=NULL;
417         mVOIFilter = NULL;
418         mVFMapper = NULL;
419         mVFActor = NULL;
420     }
421     if (actor_type == "overlay")
422     {
423         Renderer->RemoveActor(mOverlayActor);
424         mOverlay = NULL;
425         mOverlayActor = NULL;
426         mOverlayMapper = NULL;
427     }
428     if (actor_type == "fusion")
429     {
430         Renderer->RemoveActor(mFusionActor);
431         mFusion = NULL;
432         mFusionActor = NULL;
433         mFusionMapper = NULL;
434     }
435     if (actor_type == "contour")
436     {
437         Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
438         mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
439     }
440 }
441
442 void vvSlicer::SetVFSubSampling(int sub)
443 {
444     if (mVOIFilter)
445     {
446         mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
447         mSubSampling = sub;
448     }
449     UpdateDisplayExtent();
450     Render();
451 }
452
453 void vvSlicer::SetVFScale(int scale)
454 {
455     mScale = scale;
456     if (mArrow)
457         mArrow->SetScale(mScale);
458     UpdateDisplayExtent();
459     Render();
460 }
461
462 void vvSlicer::SetVFLog(int log)
463 {
464     mVFLog = log;
465     if (mGlyphFilter)
466     {
467         mGlyphFilter->SetUseLog(mVFLog);
468         mGlyphFilter->Modified();
469     }
470     UpdateDisplayExtent();
471     Render();
472 }
473
474 void vvSlicer::SetTSlice(int t)
475 {
476     if (t < 0)
477         t = 0;
478     else if ((unsigned int)t >= mImage->GetVTKImages().size())
479         t = mImage->GetVTKImages().size() -1;
480     mCurrentTSlice = t;
481     this->SetInput(mImage->GetVTKImages()[t]);
482     if (mVF && mVFActor->GetVisibility())
483     {
484         if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
485             mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
486     }
487     if (mOverlay && mOverlayActor->GetVisibility())
488     {
489         if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
490             mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
491     }
492     if (mFusion && mFusionActor->GetVisibility())
493     {
494         if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
495             mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
496     }
497     if (mSurfaceCutActors.size() > 0)
498         for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
499                 i!=mSurfaceCutActors.end();i++)
500             (*i)->SetTimeSlice(mCurrentTSlice);
501     UpdateDisplayExtent();
502 }
503
504 int vvSlicer::GetTSlice()
505 {
506     return mCurrentTSlice;
507 }
508
509 void vvSlicer::SetSliceOrientation(int orientation)
510 {
511     //if 2D image, force to watch in Axial View
512     int extent[6];
513     this->GetInput()->GetWholeExtent(extent);
514     if (extent[5]-extent[4] <= 2)
515         orientation=2;
516
517     if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
518             orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
519     {
520         vtkErrorMacro("Error - invalid slice orientation " << orientation);
521         return;
522     }
523
524     this->SliceOrientation = orientation;
525     
526     // Update the viewer
527     int *range = this->GetSliceRange();
528     if (range)
529         this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
530
531     this->UpdateOrientation();
532     this->UpdateDisplayExtent();
533
534     if (this->Renderer && this->GetInput())
535     {
536         double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
537         this->Renderer->ResetCamera();
538         this->Renderer->GetActiveCamera()->SetParallelScale(scale);
539     }
540
541     SetContourSlice();
542 }
543
544 //----------------------------------------------------------------------------
545 void vvSlicer::UpdateDisplayExtent()
546 {
547     vtkImageData *input = this->GetInput();
548     if (!input || !this->ImageActor)
549     {
550         return;
551     }
552     input->UpdateInformation();
553     int *w_ext = input->GetWholeExtent();
554
555     switch (this->SliceOrientation)
556     {
557         case vtkImageViewer2::SLICE_ORIENTATION_XY:
558             this->ImageActor->SetDisplayExtent(
559                     w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
560             if (mVF && mVFActor->GetVisibility())
561             {
562                 int vfExtent[6];
563                 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
564                 mVOIFilter->SetVOI(vfExtent);
565                 mGlyphFilter->SetOrientation(1,1,0);
566                 mVFMapper->Update();
567                 // put the vector field between the image and the camera
568                 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
569                     mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
570                 else
571                     mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
572             }
573             if (mOverlay && mOverlayActor->GetVisibility())
574             {
575                 int overExtent[6];
576                 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
577                 mOverlayActor->SetDisplayExtent(overExtent);
578                 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
579                     mOverlayActor->SetPosition(0,0,1);
580                 else
581                     mOverlayActor->SetPosition(0,0,-1);
582             }
583             if (mFusion && mFusionActor->GetVisibility())
584             {
585                 int fusExtent[6];
586                 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
587                 mFusionActor->SetDisplayExtent(fusExtent);
588                 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
589                     mFusionActor->SetPosition(0,0,1.5);
590                 else
591                     mFusionActor->SetPosition(0,0,-1.5);
592             }
593             if (mLandActor)
594             {
595                 if (mClipBox)
596                 {
597                     double bounds [6];
598                     bounds[0] = ImageActor->GetBounds()[0];
599                     bounds[1] = ImageActor->GetBounds()[1];
600                     bounds[2] = ImageActor->GetBounds()[2];
601                     bounds[3] = ImageActor->GetBounds()[3];
602                     bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
603                     bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
604                     mClipBox->SetBounds(bounds);
605                     UpdateLandmarks();
606                 }
607                 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
608                     mLandActor->SetPosition(0,0,1.5);
609                 else
610                     mLandActor->SetPosition(0,0,-1.5);
611             }
612             break;
613
614         case vtkImageViewer2::SLICE_ORIENTATION_XZ:
615             this->ImageActor->SetDisplayExtent(
616                     w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
617             if (mVF && mVFActor->GetVisibility())
618             {
619                 int vfExtent[6];
620                 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
621                 mVOIFilter->SetVOI(vfExtent);
622                 mGlyphFilter->SetOrientation(1,0,1);
623                 mVFMapper->Update();
624                 // put the vector field between the image aSpacingnd the camera
625                 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
626                     mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
627                 else
628                     mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
629             }
630             if (mOverlay && mOverlayActor->GetVisibility())
631             {
632                 int overExtent[6];
633                 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
634                 mOverlayActor->SetDisplayExtent(overExtent);
635                 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
636                     mOverlayActor->SetPosition(0,1,0);
637                 else
638                     mOverlayActor->SetPosition(0,-1,0);
639             }
640             if (mFusion && mFusionActor->GetVisibility())
641             {
642                 int fusExtent[6];
643                 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
644                 mFusionActor->SetDisplayExtent(fusExtent);
645                 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
646                     mFusionActor->SetPosition(0,1.5,0);
647                 else
648                     mFusionActor->SetPosition(0,-1.5,0);
649             }
650             if (mLandActor)
651             {
652                 if (mClipBox)
653                 {
654                     double bounds [6];
655                     bounds[0] = ImageActor->GetBounds()[0];
656                     bounds[1] = ImageActor->GetBounds()[1];
657                     bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
658                     bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
659                     bounds[4] = ImageActor->GetBounds()[4];
660                     bounds[5] = ImageActor->GetBounds()[5];
661                     mClipBox->SetBounds(bounds);
662                     UpdateLandmarks();
663                 }
664                 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
665                     mLandActor->SetPosition(0,1.5,0);
666                 else
667                     mLandActor->SetPosition(0,-1.5,0);
668             }
669             break;
670
671         case vtkImageViewer2::SLICE_ORIENTATION_YZ:
672             this->ImageActor->SetDisplayExtent(
673                     this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
674             if (mVF && mVFActor->GetVisibility())
675             {
676                 int vfExtent[6];
677                 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
678                 mVOIFilter->SetVOI(vfExtent);
679                 mGlyphFilter->SetOrientation(0,1,1);
680                 mVFMapper->Update();
681                 // put the vector field between the image and the camera
682                 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
683                     mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
684                 else
685                     mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
686             }
687             if (mOverlay && mOverlayActor->GetVisibility())
688             {
689                 int overExtent[6];
690                 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
691                 mOverlayActor->SetDisplayExtent(overExtent);
692                 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
693                     mOverlayActor->SetPosition(1,0,0);
694                 else
695                     mOverlayActor->SetPosition(-1,0,0);
696             }
697             if (mFusion && mFusionActor->GetVisibility())
698             {
699                 int fusExtent[6];
700                 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
701                 mFusionActor->SetDisplayExtent(fusExtent);
702                 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
703                     mFusionActor->SetPosition(1.5,0,0);
704                 else
705                     mFusionActor->SetPosition(-1.5,0,0);
706             }
707             if (mLandActor)
708             {
709                 if (mClipBox)
710                 {
711                     double bounds [6];
712                     bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
713                     bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
714                     bounds[2] = ImageActor->GetBounds()[2];
715                     bounds[3] = ImageActor->GetBounds()[3];
716                     bounds[4] = ImageActor->GetBounds()[4];
717                     bounds[5] = ImageActor->GetBounds()[5];
718                     mClipBox->SetBounds(bounds);
719                     UpdateLandmarks();
720                 }
721                 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
722                     mLandActor->SetPosition(1.5,0,0);
723                 else
724                     mLandActor->SetPosition(-1.5,0,0);
725             }
726             break;
727     }
728
729     // Figure out the correct clipping range
730
731     if (this->Renderer)
732     {
733         if (this->InteractorStyle &&
734                 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
735         {
736             this->Renderer->ResetCameraClippingRange();
737         }
738         else
739         {
740             vtkCamera *cam = this->Renderer->GetActiveCamera();
741             if (cam)
742             {
743                 double bounds[6];
744                 this->ImageActor->GetBounds(bounds);
745                 double spos = (double)bounds[this->SliceOrientation * 2];
746                 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
747                 double range = fabs(spos - cpos);
748                 double *spacing = input->GetSpacing();
749                 double avg_spacing =
750                     ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
751                 cam->SetClippingRange(
752                         range - avg_spacing * 3.0, range + avg_spacing * 3.0);
753             }
754         }
755     }
756 }
757
758 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
759 {
760     vtkImageData* image=this->GetInput();
761     vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
762                 mVF->GetSpacing()[0];
763     vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
764                 mVF->GetSpacing()[0];
765     vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
766                 mVF->GetSpacing()[1];
767     vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
768                 mVF->GetSpacing()[1];
769     vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
770                 mVF->GetSpacing()[2];
771     vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
772                 mVF->GetSpacing()[2];
773
774     ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
775 }
776
777 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
778 {
779     vtkImageData* image=this->GetInput();
780     overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
781                 mOverlay->GetSpacing()[0];
782     overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
783                 mOverlay->GetSpacing()[0];
784     overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
785                 mOverlay->GetSpacing()[1];
786     overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
787                 mOverlay->GetSpacing()[1];
788     overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
789                 mOverlay->GetSpacing()[2];
790     overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
791                 mOverlay->GetSpacing()[2];
792     ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
793 }
794
795 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
796 {
797     vtkImageData* image=this->GetInput();
798     fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
799                 mFusion->GetSpacing()[0];
800     fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
801                 mFusion->GetSpacing()[0];
802     fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
803                 mFusion->GetSpacing()[1];
804     fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
805                 mFusion->GetSpacing()[1];
806     fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
807                 mFusion->GetSpacing()[2];
808     fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
809                 mFusion->GetSpacing()[2];
810     ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
811 }
812
813 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
814 {
815     bool out = false;
816     int maxBound = 6;
817
818     //2D overlay on 3D image specific case
819     if (refExtent[4] == refExtent[5])
820     {
821         maxBound = 4;
822         extent[4] = refExtent[4];
823         extent[5] = refExtent[5];
824     }
825
826     for (int i = 0; i < maxBound; i = i+2)
827     {
828         //if we are totally outside the image
829         if ( extent[i] > refExtent[i+1] or extent[i+1] < refExtent[i] )
830         {
831             out = true;
832             break;
833         }
834         //crop to the limit of the image
835         extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
836         extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
837         extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
838         extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
839     }
840     if (out)
841         for (int i = 0; i < maxBound; i = i+2)
842         {
843             extent[i] = refExtent[i];
844             extent[i+1] = refExtent[i];
845         }
846 }
847
848 void vvSlicer::UpdateOrientation()
849 {
850     // Set the camera position
851     vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
852     if (cam)
853     {
854         switch (this->SliceOrientation)
855         {
856             case vtkImageViewer2::SLICE_ORIENTATION_XY:
857                 cam->SetFocalPoint(0,0,0);
858                 cam->SetPosition(0,0,-1); // -1 if medical ?
859                 cam->SetViewUp(0,-1,0);
860                 break;
861
862             case vtkImageViewer2::SLICE_ORIENTATION_XZ:
863                 cam->SetFocalPoint(0,0,0);
864                 cam->SetPosition(0,-1,0); // 1 if medical ?
865                 cam->SetViewUp(0,0,1);
866                 break;
867
868             case vtkImageViewer2::SLICE_ORIENTATION_YZ:
869                 cam->SetFocalPoint(0,0,0);
870                 cam->SetPosition(-1,0,0); // -1 if medical ?
871                 cam->SetViewUp(0,0,1);
872                 break;
873         }
874     }
875 }
876
877 void vvSlicer::SetOpacity(double s)
878 {
879     this->GetImageActor()->SetOpacity(s);
880 }
881
882 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
883 {
884     this->Superclass::SetRenderWindow(rw);
885     this->SetupInteractor(rw->GetInteractor());
886     ca->SetImageActor(this->GetImageActor());
887     ca->SetWindowLevel(this->GetWindowLevel());
888     ca->SetText(2, "<slice>");
889     ca->SetText(3, "<window>\n<level>");
890
891     double bounds[6];
892     double max = 65000;
893
894     bounds[0] = -max;
895     bounds[1] = max;
896     bounds[2] = -max;
897     bounds[3] = max;
898     bounds[4] = -max;
899     bounds[5] = max;
900
901     crossCursor->SetModelBounds(bounds);
902     this->GetRenderer()->AddActor(pdmA);
903     this->GetRenderer()->AddActor(ca);
904     this->GetRenderer()->ResetCamera();
905
906     //this is just a mapping between the labeling of the orientations presented to the user and
907     //the one used by vtk
908     SetSliceOrientation(2-(orientation%3));
909     ResetCamera();
910 }
911
912 void vvSlicer::ResetCamera()
913 {
914     if (this->GetInput())
915     {
916         double* input_bounds=this->GetInput()->GetBounds();
917         double bmax=input_bounds[1]-input_bounds[0];
918         if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
919         if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
920         this->GetRenderer()->ResetCamera();
921         this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
922     }
923 }
924
925 void vvSlicer::SetDisplayMode(bool i)
926 {
927     this->GetImageActor()->SetVisibility(i);
928     this->GetAnnotation()->SetVisibility(i);
929     this->GetRenderer()->SetDraw(i);
930     if (mLandActor)
931         mLandActor->SetVisibility(i);
932     pdmA->SetVisibility(i);
933     if (i)
934         UpdateDisplayExtent();
935 }
936
937 void vvSlicer::FlipHorizontalView()
938 {
939     vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
940     if (cam)
941     {
942         double *position = cam->GetPosition();
943         switch (this->SliceOrientation)
944         {
945             case vtkImageViewer2::SLICE_ORIENTATION_XY:
946                 cam->SetPosition(position[0],position[1],-position[2]);
947                 break;
948
949             case vtkImageViewer2::SLICE_ORIENTATION_XZ:
950                 cam->SetPosition(position[0],-position[1],position[2]);
951                 break;
952
953             case vtkImageViewer2::SLICE_ORIENTATION_YZ:
954                 cam->SetPosition(-position[0],position[1],position[2]);
955                 break;
956         }
957         this->Renderer->ResetCameraClippingRange();
958         this->UpdateDisplayExtent();
959     }
960 }
961
962 void vvSlicer::FlipVerticalView()
963 {
964     vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
965     if (cam)
966     {
967         FlipHorizontalView();
968         double *viewup = cam->GetViewUp();
969         cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
970         this->UpdateDisplayExtent();
971     }
972 }
973
974 void vvSlicer::Render()
975 {
976     if (this->GetWindowLevel()->GetLookupTable() and not this->mOverlay and not
977             this->mFusion)
978     {
979         legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
980         legend->SetVisibility(1);
981     }
982     else legend->SetVisibility(0);
983
984     if (ca->GetVisibility())
985     {
986         std::string worldPos = "";
987         std::stringstream world1;
988         std::stringstream world2;
989         std::stringstream world3;
990         world1 << (int)mCurrent[0];
991         world2 << (int)mCurrent[1];
992         world3 << (int)mCurrent[2];
993         double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
994         double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
995         double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
996
997         if (pdmA->GetVisibility())
998         {
999             double x = mCursor[0];
1000             double y = mCursor[1];
1001             double z = mCursor[2];
1002             double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1003             double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1004             double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1005
1006             if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1007                     xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1008                     yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1009                     yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1010                     zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1011                     zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1012             {
1013                 vtkRenderer * renderer = this->Renderer;
1014
1015                 renderer->WorldToView(x,y,z);
1016                 renderer->ViewToNormalizedViewport(x,y,z);
1017                 renderer->NormalizedViewportToViewport(x,y);
1018                 renderer->ViewportToNormalizedDisplay(x,y);
1019                 renderer->NormalizedDisplayToDisplay(x,y);
1020                 crossCursor->SetFocalPoint(x,y,z);
1021             }
1022             else
1023                 crossCursor->SetFocalPoint(-1,-1,z);
1024         }
1025
1026         if (X >= this->GetInput()->GetWholeExtent()[0] &&
1027                 X <= this->GetInput()->GetWholeExtent()[1] &&
1028                 Y >= this->GetInput()->GetWholeExtent()[2] &&
1029                 Y <= this->GetInput()->GetWholeExtent()[3] &&
1030                 Z >= this->GetInput()->GetWholeExtent()[4] &&
1031                 Z <= this->GetInput()->GetWholeExtent()[5])
1032         {
1033             std::stringstream pixel1;
1034             std::stringstream pixel2;
1035             std::stringstream pixel3;
1036             std::stringstream temps;
1037             pixel1 << (int)X;
1038             pixel2 << (int)Y;
1039             pixel3 << (int)Z;
1040             temps << mCurrentTSlice;
1041             double value = this->GetInput()->GetScalarComponentAsDouble(
1042                     (int)X,
1043                     (int)Y,
1044                     (int)Z,0);
1045
1046             std::stringstream val;
1047             val << value;
1048             worldPos += "data value : " + val.str();
1049             worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " + world3.str() + " " + temps.str();
1050             worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " + pixel3.str() + " " + temps.str();
1051         }
1052         ca->SetText(1,worldPos.c_str());
1053     }
1054     if (mOverlay && mOverlayActor->GetVisibility())
1055     {
1056         mOverlayMapper->SetWindow(this->GetColorWindow());
1057         mOverlayMapper->SetLevel(this->GetColorLevel());
1058         mOverlayMapper->Update();
1059     }
1060     if (mLandMapper)
1061         UpdateLandmarks();
1062     //this->Superclass::Render();
1063     this->GetRenderWindow()->Render();
1064 }
1065
1066 void vvSlicer::UpdateCursorPosition()
1067 {
1068     if (this->GetImageActor()->GetVisibility())
1069     {
1070         pdmA->SetVisibility(true);
1071         mCursor[0] = mCurrent[0];
1072         mCursor[1] = mCurrent[1];
1073         mCursor[2] = mCurrent[2];
1074         mCursor[3] = mCurrentTSlice;
1075     }
1076 }
1077
1078 void vvSlicer::UpdateLandmarks()
1079 {
1080     vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1081     if (pd->GetPoints())
1082     {
1083         mLandGlyph->SetRange(0,1);
1084         mLandGlyph->Modified();
1085         mLandGlyph->Update();
1086
1087         mClipBox->Modified();
1088         mLandClipper->Update();
1089         mLandMapper->Update();
1090     }
1091
1092 }
1093
1094 //----------------------------------------------------------------------------
1095 void vvSlicer::SetSlice(int slice)
1096 {
1097     int *range = this->GetSliceRange();
1098     if (range)
1099     {
1100         if (slice < range[0])
1101         {
1102             slice = range[0];
1103         }
1104         else if (slice > range[1])
1105         {
1106             slice = range[1];
1107         }
1108     }
1109
1110     if (this->Slice == slice)
1111     {
1112         return;
1113     }
1114
1115     this->Slice = slice;
1116     SetContourSlice();
1117     this->Modified();
1118     this->UpdateDisplayExtent();
1119     this->Render();
1120 }
1121
1122 void vvSlicer::SetContourSlice()
1123 {
1124     if (mSurfaceCutActors.size() > 0)
1125         for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1126                 i!=mSurfaceCutActors.end();i++)
1127             (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1128                     this->GetImage()->GetOrigin()[this->SliceOrientation]);
1129 }
1130
1131 void vvSlicer::ForceUpdateDisplayExtent()
1132 {
1133     this->UpdateDisplayExtent();
1134 }
1135
1136 int* vvSlicer::GetDisplayExtent()
1137 {
1138     return this->GetImageActor()->GetDisplayExtent();
1139 }
1140
1141 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1142 {
1143     this->Superclass::PrintSelf(os, indent);
1144 }