]> Creatis software - clitk.git/blob - vv/vvSlicer.cxx
Initial revision
[clitk.git] / vv / vvSlicer.cxx
1 /*=========================================================================
2
3 Program:   vv
4 Module:    $RCSfile: vvSlicer.cxx,v $
5 Language:  C++
6 Date:      $Date: 2010/01/06 13:31:58 $
7 Version:   $Revision: 1.1 $
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.1 $");
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() == 1)
484         {
485             mVOIFilter->SetInput(mVF->GetVTKImages()[0]);
486         }
487         else if (mVF->GetVTKImages().size() <= (unsigned int)mCurrentTSlice)
488         {
489             Renderer->RemoveActor(mVFActor);
490         }
491         else
492         {
493             if (!Renderer->HasViewProp(mVFActor))
494             {
495                 Renderer->AddActor(mVFActor);
496             }
497             mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
498         }
499     }
500     if (mOverlay && mOverlayActor->GetVisibility())
501     {
502         if (mOverlay->GetVTKImages().size() == 1)
503         {
504             mOverlayMapper->SetInput(mOverlay->GetVTKImages()[0]);
505         }
506         else if (mOverlay->GetVTKImages().size() <= (unsigned int)mCurrentTSlice)
507         {
508             Renderer->RemoveActor(mOverlayActor);
509         }
510         else
511         {
512             if (!Renderer->HasViewProp(mOverlayActor))
513             {
514                 Renderer->AddActor(mOverlayActor);
515             }
516             mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
517         }
518     }
519     if (mFusion && mFusionActor->GetVisibility())
520     {
521         if (mFusion->GetVTKImages().size() == 1)
522         {
523             mFusionMapper->SetInput(mFusion->GetVTKImages()[0]);
524         }
525         else if (mFusion->GetVTKImages().size() <= (unsigned int)mCurrentTSlice)
526         {
527             Renderer->RemoveActor(mFusionActor);
528         }
529         else
530         {
531             if (!Renderer->HasViewProp(mFusionActor))
532             {
533                 Renderer->AddActor(mFusionActor);
534             }
535             mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
536         }
537     }
538     if (mSurfaceCutActors.size() > 0)
539         for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
540                 i!=mSurfaceCutActors.end();i++)
541             (*i)->SetTimeSlice(mCurrentTSlice);
542     UpdateDisplayExtent();
543 }
544
545 int vvSlicer::GetTSlice()
546 {
547     return mCurrentTSlice;
548 }
549
550 void vvSlicer::SetSliceOrientation(int orientation)
551 {
552     //if 2D image, force to watch in Axial View
553     int extent[6];
554     this->GetInput()->GetWholeExtent(extent);
555     if (extent[5]-extent[4] <= 2)
556         orientation=2;
557
558     if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
559             orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
560     {
561         vtkErrorMacro("Error - invalid slice orientation " << orientation);
562         return;
563     }
564
565     this->SliceOrientation = orientation;
566     
567     // Update the viewer
568     int *range = this->GetSliceRange();
569     if (range)
570         this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
571
572     this->UpdateOrientation();
573     this->UpdateDisplayExtent();
574
575     if (this->Renderer && this->GetInput())
576     {
577         double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
578         this->Renderer->ResetCamera();
579         this->Renderer->GetActiveCamera()->SetParallelScale(scale);
580     }
581
582     SetContourSlice();
583 }
584
585 //----------------------------------------------------------------------------
586 void vvSlicer::UpdateDisplayExtent()
587 {
588     vtkImageData *input = this->GetInput();
589     if (!input || !this->ImageActor)
590     {
591         return;
592     }
593     input->UpdateInformation();
594     int *w_ext = input->GetWholeExtent();
595
596     switch (this->SliceOrientation)
597     {
598         case vtkImageViewer2::SLICE_ORIENTATION_XY:
599             this->ImageActor->SetDisplayExtent(
600                     w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
601             if (mVF && mVFActor->GetVisibility())
602             {
603                 int vfExtent[6];
604                 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
605                 mVOIFilter->SetVOI(vfExtent);
606                 mGlyphFilter->SetOrientation(1,1,0);
607                 mVFMapper->Update();
608                 // put the vector field between the image and the camera
609                 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
610                     mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
611                 else
612                     mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
613             }
614             if (mOverlay && mOverlayActor->GetVisibility())
615             {
616                 int overExtent[6];
617                 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
618                 mOverlayActor->SetDisplayExtent(overExtent);
619                 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
620                     mOverlayActor->SetPosition(0,0,1);
621                 else
622                     mOverlayActor->SetPosition(0,0,-1);
623             }
624             if (mFusion && mFusionActor->GetVisibility())
625             {
626                 int fusExtent[6];
627                 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
628                 mFusionActor->SetDisplayExtent(fusExtent);
629                 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
630                     mFusionActor->SetPosition(0,0,1.5);
631                 else
632                     mFusionActor->SetPosition(0,0,-1.5);
633             }
634             if (mLandActor)
635             {
636                 if (mClipBox)
637                 {
638                     double bounds [6];
639                     bounds[0] = ImageActor->GetBounds()[0];
640                     bounds[1] = ImageActor->GetBounds()[1];
641                     bounds[2] = ImageActor->GetBounds()[2];
642                     bounds[3] = ImageActor->GetBounds()[3];
643                     bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
644                     bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
645                     mClipBox->SetBounds(bounds);
646                     UpdateLandmarks();
647                 }
648                 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
649                     mLandActor->SetPosition(0,0,1.5);
650                 else
651                     mLandActor->SetPosition(0,0,-1.5);
652             }
653             break;
654
655         case vtkImageViewer2::SLICE_ORIENTATION_XZ:
656             this->ImageActor->SetDisplayExtent(
657                     w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
658             if (mVF && mVFActor->GetVisibility())
659             {
660                 int vfExtent[6];
661                 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
662                 mVOIFilter->SetVOI(vfExtent);
663                 mGlyphFilter->SetOrientation(1,0,1);
664                 mVFMapper->Update();
665                 // put the vector field between the image aSpacingnd the camera
666                 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
667                     mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
668                 else
669                     mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
670             }
671             if (mOverlay && mOverlayActor->GetVisibility())
672             {
673                 int overExtent[6];
674                 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
675                 mOverlayActor->SetDisplayExtent(overExtent);
676                 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
677                     mOverlayActor->SetPosition(0,1,0);
678                 else
679                     mOverlayActor->SetPosition(0,-1,0);
680             }
681             if (mFusion && mFusionActor->GetVisibility())
682             {
683                 int fusExtent[6];
684                 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
685                 mFusionActor->SetDisplayExtent(fusExtent);
686                 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
687                     mFusionActor->SetPosition(0,1.5,0);
688                 else
689                     mFusionActor->SetPosition(0,-1.5,0);
690             }
691             if (mLandActor)
692             {
693                 if (mClipBox)
694                 {
695                     double bounds [6];
696                     bounds[0] = ImageActor->GetBounds()[0];
697                     bounds[1] = ImageActor->GetBounds()[1];
698                     bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
699                     bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
700                     bounds[4] = ImageActor->GetBounds()[4];
701                     bounds[5] = ImageActor->GetBounds()[5];
702                     mClipBox->SetBounds(bounds);
703                     UpdateLandmarks();
704                 }
705                 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
706                     mLandActor->SetPosition(0,1.5,0);
707                 else
708                     mLandActor->SetPosition(0,-1.5,0);
709             }
710             break;
711
712         case vtkImageViewer2::SLICE_ORIENTATION_YZ:
713             this->ImageActor->SetDisplayExtent(
714                     this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
715             if (mVF && mVFActor->GetVisibility())
716             {
717                 int vfExtent[6];
718                 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
719                 mVOIFilter->SetVOI(vfExtent);
720                 mGlyphFilter->SetOrientation(0,1,1);
721                 mVFMapper->Update();
722                 // put the vector field between the image and the camera
723                 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
724                     mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
725                 else
726                     mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
727             }
728             if (mOverlay && mOverlayActor->GetVisibility())
729             {
730                 int overExtent[6];
731                 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
732                 mOverlayActor->SetDisplayExtent(overExtent);
733                 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
734                     mOverlayActor->SetPosition(1,0,0);
735                 else
736                     mOverlayActor->SetPosition(-1,0,0);
737             }
738             if (mFusion && mFusionActor->GetVisibility())
739             {
740                 int fusExtent[6];
741                 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
742                 mFusionActor->SetDisplayExtent(fusExtent);
743                 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
744                     mFusionActor->SetPosition(1.5,0,0);
745                 else
746                     mFusionActor->SetPosition(-1.5,0,0);
747             }
748             if (mLandActor)
749             {
750                 if (mClipBox)
751                 {
752                     double bounds [6];
753                     bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
754                     bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
755                     bounds[2] = ImageActor->GetBounds()[2];
756                     bounds[3] = ImageActor->GetBounds()[3];
757                     bounds[4] = ImageActor->GetBounds()[4];
758                     bounds[5] = ImageActor->GetBounds()[5];
759                     mClipBox->SetBounds(bounds);
760                     UpdateLandmarks();
761                 }
762                 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
763                     mLandActor->SetPosition(1.5,0,0);
764                 else
765                     mLandActor->SetPosition(-1.5,0,0);
766             }
767             break;
768     }
769
770     // Figure out the correct clipping range
771
772     if (this->Renderer)
773     {
774         if (this->InteractorStyle &&
775                 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
776         {
777             this->Renderer->ResetCameraClippingRange();
778         }
779         else
780         {
781             vtkCamera *cam = this->Renderer->GetActiveCamera();
782             if (cam)
783             {
784                 double bounds[6];
785                 this->ImageActor->GetBounds(bounds);
786                 double spos = (double)bounds[this->SliceOrientation * 2];
787                 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
788                 double range = fabs(spos - cpos);
789                 double *spacing = input->GetSpacing();
790                 double avg_spacing =
791                     ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
792                 cam->SetClippingRange(
793                         range - avg_spacing * 3.0, range + avg_spacing * 3.0);
794             }
795         }
796     }
797 }
798
799 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
800 {
801     vtkImageData* image=this->GetInput();
802     vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
803                 mVF->GetSpacing()[0];
804     vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
805                 mVF->GetSpacing()[0];
806     vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
807                 mVF->GetSpacing()[1];
808     vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
809                 mVF->GetSpacing()[1];
810     vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
811                 mVF->GetSpacing()[2];
812     vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
813                 mVF->GetSpacing()[2];
814
815     ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
816 }
817
818 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
819 {
820     vtkImageData* image=this->GetInput();
821     overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
822                 mOverlay->GetSpacing()[0];
823     overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
824                 mOverlay->GetSpacing()[0];
825     overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
826                 mOverlay->GetSpacing()[1];
827     overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
828                 mOverlay->GetSpacing()[1];
829     overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
830                 mOverlay->GetSpacing()[2];
831     overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
832                 mOverlay->GetSpacing()[2];
833     ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
834 }
835
836 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
837 {
838     vtkImageData* image=this->GetInput();
839     fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
840                 mFusion->GetSpacing()[0];
841     fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
842                 mFusion->GetSpacing()[0];
843     fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
844                 mFusion->GetSpacing()[1];
845     fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
846                 mFusion->GetSpacing()[1];
847     fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
848                 mFusion->GetSpacing()[2];
849     fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
850                 mFusion->GetSpacing()[2];
851     ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
852 }
853
854 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
855 {
856     bool out = false;
857     int maxBound = 6;
858
859     //2D overlay on 3D image specific case
860     if (refExtent[4] == refExtent[5])
861     {
862         maxBound = 4;
863         extent[4] = refExtent[4];
864         extent[5] = refExtent[5];
865     }
866
867     for (int i = 0; i < maxBound; i = i+2)
868     {
869         //if we are totally outside the image
870         if ( extent[i] > refExtent[i+1] or extent[i+1] < refExtent[i] )
871         {
872             out = true;
873             break;
874         }
875         //crop to the limit of the image
876         extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
877         extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
878         extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
879         extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
880     }
881     if (out)
882         for (int i = 0; i < maxBound; i = i+2)
883         {
884             extent[i] = refExtent[i];
885             extent[i+1] = refExtent[i];
886         }
887 }
888
889 void vvSlicer::UpdateOrientation()
890 {
891     // Set the camera position
892     vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
893     if (cam)
894     {
895         switch (this->SliceOrientation)
896         {
897             case vtkImageViewer2::SLICE_ORIENTATION_XY:
898                 cam->SetFocalPoint(0,0,0);
899                 cam->SetPosition(0,0,-1); // -1 if medical ?
900                 cam->SetViewUp(0,-1,0);
901                 break;
902
903             case vtkImageViewer2::SLICE_ORIENTATION_XZ:
904                 cam->SetFocalPoint(0,0,0);
905                 cam->SetPosition(0,-1,0); // 1 if medical ?
906                 cam->SetViewUp(0,0,1);
907                 break;
908
909             case vtkImageViewer2::SLICE_ORIENTATION_YZ:
910                 cam->SetFocalPoint(0,0,0);
911                 cam->SetPosition(-1,0,0); // -1 if medical ?
912                 cam->SetViewUp(0,0,1);
913                 break;
914         }
915     }
916 }
917
918 void vvSlicer::SetOpacity(double s)
919 {
920     this->GetImageActor()->SetOpacity(s);
921 }
922
923 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
924 {
925     this->Superclass::SetRenderWindow(rw);
926     this->SetupInteractor(rw->GetInteractor());
927     ca->SetImageActor(this->GetImageActor());
928     ca->SetWindowLevel(this->GetWindowLevel());
929     ca->SetText(2, "<slice>");
930     ca->SetText(3, "<window>\n<level>");
931
932     double bounds[6];
933     double max = 65000;
934
935     bounds[0] = -max;
936     bounds[1] = max;
937     bounds[2] = -max;
938     bounds[3] = max;
939     bounds[4] = -max;
940     bounds[5] = max;
941
942     crossCursor->SetModelBounds(bounds);
943     this->GetRenderer()->AddActor(pdmA);
944     this->GetRenderer()->AddActor(ca);
945     this->GetRenderer()->ResetCamera();
946
947     //this is just a mapping between the labeling of the orientations presented to the user and
948     //the one used by vtk
949     SetSliceOrientation(2-(orientation%3));
950     ResetCamera();
951 }
952
953 void vvSlicer::ResetCamera()
954 {
955     if (this->GetInput())
956     {
957         double* input_bounds=this->GetInput()->GetBounds();
958         double bmax=input_bounds[1]-input_bounds[0];
959         if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
960         if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
961         this->GetRenderer()->ResetCamera();
962         this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
963     }
964 }
965
966 void vvSlicer::SetDisplayMode(bool i)
967 {
968     this->GetImageActor()->SetVisibility(i);
969     this->GetAnnotation()->SetVisibility(i);
970     this->GetRenderer()->SetDraw(i);
971     if (mLandActor)
972         mLandActor->SetVisibility(i);
973     pdmA->SetVisibility(i);
974     if (i)
975         UpdateDisplayExtent();
976 }
977
978 void vvSlicer::FlipHorizontalView()
979 {
980     vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
981     if (cam)
982     {
983         double *position = cam->GetPosition();
984         switch (this->SliceOrientation)
985         {
986             case vtkImageViewer2::SLICE_ORIENTATION_XY:
987                 cam->SetPosition(position[0],position[1],-position[2]);
988                 break;
989
990             case vtkImageViewer2::SLICE_ORIENTATION_XZ:
991                 cam->SetPosition(position[0],-position[1],position[2]);
992                 break;
993
994             case vtkImageViewer2::SLICE_ORIENTATION_YZ:
995                 cam->SetPosition(-position[0],position[1],position[2]);
996                 break;
997         }
998         this->Renderer->ResetCameraClippingRange();
999         this->UpdateDisplayExtent();
1000     }
1001 }
1002
1003 void vvSlicer::FlipVerticalView()
1004 {
1005     vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1006     if (cam)
1007     {
1008         FlipHorizontalView();
1009         double *viewup = cam->GetViewUp();
1010         cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1011         this->UpdateDisplayExtent();
1012     }
1013 }
1014
1015 void vvSlicer::Render()
1016 {
1017     if (this->GetWindowLevel()->GetLookupTable() and not this->mOverlay and not
1018             this->mFusion)
1019     {
1020         legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1021         legend->SetVisibility(1);
1022     }
1023     else legend->SetVisibility(0);
1024
1025     if (ca->GetVisibility())
1026     {
1027         std::string worldPos = "";
1028         std::stringstream world1;
1029         std::stringstream world2;
1030         std::stringstream world3;
1031         world1 << (int)mCurrent[0];
1032         world2 << (int)mCurrent[1];
1033         world3 << (int)mCurrent[2];
1034         double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1035         double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1036         double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1037
1038         if (pdmA->GetVisibility())
1039         {
1040             double x = mCursor[0];
1041             double y = mCursor[1];
1042             double z = mCursor[2];
1043             double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1044             double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1045             double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1046
1047             if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1048                     xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1049                     yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1050                     yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1051                     zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1052                     zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1053             {
1054                 vtkRenderer * renderer = this->Renderer;
1055
1056                 renderer->WorldToView(x,y,z);
1057                 renderer->ViewToNormalizedViewport(x,y,z);
1058                 renderer->NormalizedViewportToViewport(x,y);
1059                 renderer->ViewportToNormalizedDisplay(x,y);
1060                 renderer->NormalizedDisplayToDisplay(x,y);
1061                 crossCursor->SetFocalPoint(x,y,z);
1062             }
1063             else
1064                 crossCursor->SetFocalPoint(-1,-1,z);
1065         }
1066
1067         if (X >= this->GetInput()->GetWholeExtent()[0] &&
1068                 X <= this->GetInput()->GetWholeExtent()[1] &&
1069                 Y >= this->GetInput()->GetWholeExtent()[2] &&
1070                 Y <= this->GetInput()->GetWholeExtent()[3] &&
1071                 Z >= this->GetInput()->GetWholeExtent()[4] &&
1072                 Z <= this->GetInput()->GetWholeExtent()[5])
1073         {
1074             std::stringstream pixel1;
1075             std::stringstream pixel2;
1076             std::stringstream pixel3;
1077             std::stringstream temps;
1078             pixel1 << (int)X;
1079             pixel2 << (int)Y;
1080             pixel3 << (int)Z;
1081             temps << mCurrentTSlice;
1082             double value = this->GetInput()->GetScalarComponentAsDouble(
1083                     (int)X,
1084                     (int)Y,
1085                     (int)Z,0);
1086
1087             std::stringstream val;
1088             val << value;
1089             worldPos += "data value : " + val.str();
1090             worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " + world3.str() + " " + temps.str();
1091             worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " + pixel3.str() + " " + temps.str();
1092         }
1093         ca->SetText(1,worldPos.c_str());
1094     }
1095     if (mOverlay && mOverlayActor->GetVisibility())
1096     {
1097         mOverlayMapper->SetWindow(this->GetColorWindow());
1098         mOverlayMapper->SetLevel(this->GetColorLevel());
1099         mOverlayMapper->Update();
1100     }
1101     if (mLandMapper)
1102         UpdateLandmarks();
1103     //this->Superclass::Render();
1104     this->GetRenderWindow()->Render();
1105 }
1106
1107 void vvSlicer::UpdateCursorPosition()
1108 {
1109     if (this->GetImageActor()->GetVisibility())
1110     {
1111         pdmA->SetVisibility(true);
1112         mCursor[0] = mCurrent[0];
1113         mCursor[1] = mCurrent[1];
1114         mCursor[2] = mCurrent[2];
1115         mCursor[3] = mCurrentTSlice;
1116     }
1117 }
1118
1119 void vvSlicer::UpdateLandmarks()
1120 {
1121     vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1122     if (pd->GetPoints())
1123     {
1124         mLandGlyph->SetRange(0,1);
1125         mLandGlyph->Modified();
1126         mLandGlyph->Update();
1127
1128         mClipBox->Modified();
1129         mLandClipper->Update();
1130         mLandMapper->Update();
1131     }
1132
1133 }
1134
1135 //----------------------------------------------------------------------------
1136 void vvSlicer::SetSlice(int slice)
1137 {
1138     int *range = this->GetSliceRange();
1139     if (range)
1140     {
1141         if (slice < range[0])
1142         {
1143             slice = range[0];
1144         }
1145         else if (slice > range[1])
1146         {
1147             slice = range[1];
1148         }
1149     }
1150
1151     if (this->Slice == slice)
1152     {
1153         return;
1154     }
1155
1156     this->Slice = slice;
1157     SetContourSlice();
1158     this->Modified();
1159     this->UpdateDisplayExtent();
1160     this->Render();
1161 }
1162
1163 void vvSlicer::SetContourSlice()
1164 {
1165     if (mSurfaceCutActors.size() > 0)
1166         for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1167                 i!=mSurfaceCutActors.end();i++)
1168             (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1169                     this->GetImage()->GetOrigin()[this->SliceOrientation]);
1170 }
1171
1172 void vvSlicer::ForceUpdateDisplayExtent()
1173 {
1174     this->UpdateDisplayExtent();
1175 }
1176
1177 int* vvSlicer::GetDisplayExtent()
1178 {
1179     return this->GetImageActor()->GetDisplayExtent();
1180 }
1181
1182 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1183 {
1184     this->Superclass::PrintSelf(os, indent);
1185 }