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