]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkImagePlanes.cxx
92476f5e19b8a01a00719c3c671d8a3063169404
[bbtk.git] / packages / vtk / src / bbvtkImagePlanes.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbvtkImagePlanes.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:51:58 $
33   Version:   $Revision: 1.38 $
34 =========================================================================*/
35
36 /**
37  *  \file 
38  *  \brief 
39  */
40
41 #ifdef _USE_VTK_
42 #include "bbvtkImagePlanes.h"
43 #include "bbvtkPackage.h"
44 #include "vtkCellPicker.h"
45 #include "vtkProperty.h"
46 #include "vtkPolyData.h"
47
48
49 #include "vtkMetaImageWriter.h"
50 #include "vtkPNGWriter.h"
51
52 #include "bbstdCast.h"
53 #include <vtkCommand.h>
54
55 #include "vtkImageData.h"
56 //#include "vtkOutlineFilter.h"
57 //#include "vtkPolyDataMapper.h"
58 //#include "vtkActor.h"
59 #include "vtkImagePlaneWidget.h"
60 #include "vtkCellPicker.h"
61 //#include "vtkProperty.h"
62
63 //#include "vtkRenderer.h"
64 //#include "vtkCamera.h"
65
66 #include "vtkPlaneWidget.h"
67
68 #include <vtkImplicitPlaneWidget.h>
69
70 #include "bbstdRelay.h"
71
72 #include "vtkObjectFactory.h"
73 #include "vtkImageFlip.h"
74
75 #include "vtkImageReslice.h"
76 #include "vtkImageChangeInformation.h"
77
78 namespace bbstd
79 {
80
81   //====================================================================
82   BBTK_BLACK_BOX_TEMPLATE2_IMPLEMENTATION(Cast,
83                                           bbtk::AtomicBlackBox);
84   //====================================================================
85   //====================================================================
86 //  BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION(Relay,
87 //                                       bbtk::AtomicBlackBox);
88   //====================================================================
89
90 }
91 using namespace bbstd;
92 /*
93 namespace bbtk
94 {
95         typedef vtkImageData::Pointer vtkImageDataPointer;
96   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(vtkImageDataPointer,
97                                        "vtkImageDataPointer");
98 }
99 */
100 namespace bbvtk
101 {
102
103   //====================================================================
104   // Add the specialized adaptors to the package
105   typedef vtkImagePlaneWidget* I;
106   typedef vtkInteractorObserver* O;
107
108   BBTK_ADD_TEMPLATE2_BLACK_BOX_TO_PACKAGE(vtk,Cast,I,O);
109
110   BBTK_DEFINE_RELAY_BLACK_BOX(vtkImageDataPointer,vtk,vtkImageDataPointerRelay);
111   BBTK_BLACK_BOX_IMPLEMENTATION(vtkImageDataPointerRelay,bbtk::AtomicBlackBox);
112
113   BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,vtkImageDataPointerRelay);
114  // BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(vtk,Relay,vtkImageDataPointer);
115   //Pointer);
116
117 }
118
119 namespace bbvtk
120 {
121
122   //================================================================
123  class ImagePlanes::VtkCallbackType : public vtkCommand
124  {
125  public:
126    static VtkCallbackType *New()
127    {
128      return new VtkCallbackType;
129    }
130    //vtkTypeRevisionMacro(VtkCallbackType,vtkCommand);
131
132    virtual void Execute(vtkObject *caller, unsigned long, void*)
133    {
134        mBlackBox->Process();
135        mBlackBox->bbSignalOutputModification();
136    } 
137    void SetBlackBox(ImagePlanes *BB) { mBlackBox = BB;};    
138    //   void SetVtkPlaneWidget( vtkImagePlaneWidget *planeWidget );
139    VtkCallbackType() {};
140
141  private:
142    //   vtkPlaneWidget *planeWidget;
143    ImagePlanes *mBlackBox;
144  };
145   //================================================================
146
147   //vtkCxxRevisionMacro(ImagePlanes::VtkCallbackType, "$Revision: 1.38 $");
148
149   //================================================================
150
151    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,ImagePlanes)
152    BBTK_BLACK_BOX_IMPLEMENTATION(ImagePlanes,bbtk::AtomicBlackBox);
153
154    void ImagePlanes::bbUserSetDefaultValues() 
155    { 
156      bbSetOutputPlaneX(0);
157      bbSetOutputPlaneY(0);
158      bbSetOutputPlaneZ(0);
159      bbSetOutputImageX(0);
160      bbSetOutputImageY(0);
161      bbSetOutputImageZ(0);
162      bbSetInputIn(0);
163      std::vector<double> vect;
164      vect.push_back(0);
165      vect.push_back(0);
166      bbSetInputWindowLevel (vect);  
167      mVtkCallback = 0;
168
169      std::vector<int> vectpoints;
170
171      bbSetOutputPlane3Pts(0);
172      bbSetOutputImage3Pts(0);
173      bbSetInputPointsX(vectpoints);
174      bbSetInputPointsY(vectpoints);
175      bbSetInputPointsZ(vectpoints);
176
177      _imageReslicer = NULL;
178      image          = NULL;
179      _transform     = NULL;
180      _matrix        = NULL;
181            
182            bbSetInputInterpolation(1);     
183    }
184
185         
186    void ImagePlanes::bbUserInitializeProcessing() 
187    {  
188      /// CREATION DES WIDGETS
189      if (bbGetOutputPlaneX() != 0) return;
190        
191      // The shared picker enables us to use 3 planes at one time
192      // and gets the picking order right
193      vtkCellPicker* picker = vtkCellPicker::New();
194      picker->SetTolerance(0.005);
195   
196      // The 3 image plane widgets 
197      vtkImagePlaneWidget* planeWidgetX = GetPlaneWidget('x', 1, 0, 0, picker);
198      vtkImagePlaneWidget* planeWidgetY = GetPlaneWidget('y', 1, 1, 0, picker);
199      planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
200
201      vtkImagePlaneWidget* planeWidgetZ = GetPlaneWidget('z', 0, 0, 1, picker);     
202      planeWidgetZ->SetLookupTable(planeWidgetX->GetLookupTable());
203
204      vtkImagePlaneWidget* planeWidget3Pts = GetPlaneWidget('3', 0, 1, 1, picker);        
205      planeWidget3Pts->SetLookupTable(planeWidgetX->GetLookupTable());
206
207            // EED MPR view orientation correction..
208      vtkImageFlip  *flipYFilter = vtkImageFlip::New();
209      flipYFilter->SetFilteredAxis(1); // flip y axis
210      flipYFilter->SetInput( planeWidgetX->GetResliceOutput() );
211      flipYFilter->Update();
212
213            vtkImageChangeInformation *image = vtkImageChangeInformation::New();
214            image->SetInput(  planeWidgetY->GetResliceOutput()  );
215            image->SetOutputSpacing( 1,1,1 );       
216            image->CenterImageOn();
217            image->Update();
218            _imageTransform = vtkTransform::New();
219            vtkImageReslice *slicer =vtkImageReslice::New();
220            slicer->SetInput( image->GetOutput() );
221            slicer->SetInformationInput( image->GetOutput() );
222            slicer->SetResliceTransform( _imageTransform );
223            slicer->SetOutputOrigin(0 , 0 , 0 );
224            slicer->SetInterpolationModeToNearestNeighbor();
225            slicer->Update();       
226            vtkImageChangeInformation *imageResult = vtkImageChangeInformation::New();
227            imageResult->SetInput( slicer->GetOutput() );
228            double spc[3];
229             planeWidgetY->GetResliceOutput()->GetSpacing(spc);
230            imageResult->SetOutputSpacing( spc[1], spc[0], spc[2] ); 
231            imageResult->SetOutputOrigin( 0,0,0 ); 
232                    
233            
234      bbSetOutputPlaneX(planeWidgetX);
235      bbSetOutputPlaneY(planeWidgetY);
236      bbSetOutputPlaneZ(planeWidgetZ);
237      bbSetOutputPlane3Pts(planeWidget3Pts);      
238      bbSetOutputImageX( flipYFilter->GetOutput() );             // EED MPR view orientation correction..
239      bbSetOutputImageY( imageResult->GetOutput() );                     // EED MPR view orientation correction..
240      bbSetOutputImageZ(planeWidgetZ->GetResliceOutput());
241      bbSetInputInteractor(0);
242      //bbSetOutputImage3Pts(planeWidget3Pts->GetResliceOutput());
243
244          if(picker != 0)
245                 picker->UnRegister(NULL);
246      
247      mVtkCallback = VtkCallbackType::New();
248      mVtkCallback->SetBlackBox(this);
249      planeWidgetX->AddObserver(vtkCommand::InteractionEvent,mVtkCallback);
250      planeWidgetY->AddObserver(vtkCommand::InteractionEvent,mVtkCallback);
251      planeWidgetZ->AddObserver(vtkCommand::InteractionEvent,mVtkCallback);       
252
253    }
254
255 //---------------------------------------------------------------------
256   void ImagePlanes::bbUserFinalizeProcessing()
257   {
258
259     if (bbGetOutputPlaneX()) 
260       {
261
262         /*
263           bbGetOutputPlaneX()->RemoveObserver(mVtkCallback);
264           bbGetOutputPlaneY()->RemoveObserver(mVtkCallback);
265           bbGetOutputPlaneZ()->RemoveObserver(mVtkCallback);
266
267         bbGetOutputPlaneX()->Delete();
268         bbGetOutputPlaneY()->Delete();
269         bbGetOutputPlaneZ()->Delete();
270         mVtkCallback->Delete();
271         */
272         //bbGetOutputPlaneX()->SetInput(NULL);
273         //bbGetOutputPlaneY()->SetInput(NULL);
274         //bbGetOutputPlaneZ()->SetInput(NULL);
275
276       }
277   }
278   
279 //---------------------------------------------------------------------  
280   void ImagePlanes::Process()
281   {
282           
283         if (bbGetInputIn()!=0)
284         {
285                 int dim[3];
286                 int ext[6];
287                 bbGetOutputPlaneX()->GetResliceOutput()->GetWholeExtent(ext);
288                 dim[0] = ext[1]-ext[0]+1;
289                 dim[1] = ext[3]-ext[2]+1;
290                 dim[2] = ext[5]-ext[4]+1;
291                 _imageTransform->Identity();
292                 _imageTransform->PostMultiply();
293                 _imageTransform->Translate( (int)(-(dim[0]/2)) , (int)(-(dim[1]/2)) ,0);
294                 _imageTransform->RotateZ(90);
295                 
296                 
297                 if ( image != bbGetInputIn()){//bbGetInputStatus("In") != bbtk::UPTODATE ){
298                         // Input image has changed : reinitialize planes
299                         image = bbGetInputIn();                 
300
301                         // Initial values : center of the volume (in real world, not in pixels!)
302                         int xMin, xMax, yMin, yMax, zMin, zMax;
303                         bbGetInputIn()->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
304                         double xSpacing, ySpacing, zSpacing;
305                         bbGetInputIn()->GetSpacing(xSpacing, ySpacing, zSpacing);
306
307                         bbGetOutputPlaneX()->SetInput(bbGetInputIn());
308                         bbGetOutputPlaneX()->SetPlaneOrientationToXAxes();       
309                         bbGetOutputPlaneX()->SetSlicePosition((xMax+xMin)/2.*xSpacing);
310
311                         //                 bbGetOutputPlaneX()->SetOrigin( 58*xSpacing , 80*ySpacing , 82*zSpacing );
312                         //                 bbGetOutputPlaneX()->SetPoint1( 0*xSpacing, 146*ySpacing, 186*zSpacing);
313                         //                 bbGetOutputPlaneX()->SetPoint2( 126*xSpacing, 146*ySpacing, 0*zSpacing);
314
315                         bbGetOutputPlaneY()->SetInput(bbGetInputIn());
316                         bbGetOutputPlaneY()->SetPlaneOrientationToYAxes();
317                         bbGetOutputPlaneY()->SetSlicePosition((yMax+yMin)/2.*ySpacing);
318
319                         bbGetOutputPlaneZ()->SetInput(bbGetInputIn());
320                         bbGetOutputPlaneZ()->SetPlaneOrientationToZAxes();
321                         bbGetOutputPlaneZ()->SetSlicePosition((zMax+zMin)/2.*zSpacing);
322
323                         if (bbGetInputWindowLevel()[0]!=0)
324                         {
325                                 bbGetOutputPlaneZ()->SetWindowLevel(bbGetInputWindowLevel()[0],
326                                                                 bbGetInputWindowLevel()[1]);
327                         } else {
328                                 double *range = image->GetScalarRange();
329                                 bbGetOutputPlaneZ()->SetWindowLevel(range[1]-range[0],
330                                                                0.5*(range[1]+range[0]));
331                         } // windowlevel
332                         updateInteractor();
333                         
334                         dim[0] = xMax-xMin+1;
335                         dim[1] = yMax-yMin+1;
336                         dim[2] = zMax-zMin+1;
337                         _imageTransform->Identity();
338                         _imageTransform->PostMultiply();
339                         _imageTransform->Translate( (int)(-(dim[0]/2)*(1/xSpacing)) , (int)(-(dim[2]/2)*(1/zSpacing)) ,0);
340                         _imageTransform->RotateZ(90);
341                         
342            } // image
343                 
344                         // UPDATE DES SORTIES 
345                 bbGetOutputPlaneX()->SetResliceInterpolate( bbGetInputInterpolation() );
346                 bbGetOutputPlaneY()->SetResliceInterpolate( bbGetInputInterpolation() );
347                 bbGetOutputPlaneZ()->SetResliceInterpolate( bbGetInputInterpolation() );
348
349                 
350                 bbGetOutputPlaneX()->GetResliceOutput()->Update();
351                 bbGetOutputPlaneY()->GetResliceOutput()->Update(); 
352                 bbGetOutputPlaneZ()->GetResliceOutput()->Update();               
353                 
354                 std::vector<int> pointsx = bbGetInputPointsX();
355                 std::vector<int> pointsy = bbGetInputPointsY();
356                 std::vector<int> pointsz = bbGetInputPointsZ();
357
358                 //std::cout<<pointsx.size()<<pointsy.size()<<pointsz.size()<<std::endl;
359
360                 if (pointsx.size()==pointsy.size() && pointsx.size()==pointsz.size()&&pointsx.size()>=3)
361                 {
362
363                         //Get the corresponding three points out of the vectors
364                         double origin[3];
365                         origin[0] = pointsx[0];
366                         origin[1] = pointsy[0];
367                         origin[2] = pointsz[0];
368
369                         double point1[3];
370                         point1[0] = pointsx[1];
371                         point1[1] = pointsy[1];
372                         point1[2] = pointsz[1];
373                         double point2[3];
374                         point2[0]= pointsx[2];
375                         point2[1]= pointsy[2];
376                         point2[2]= pointsz[2];  
377
378                         //With the three points we create the corresponding X, Y and Z vectors all orthogonal to each other
379                         double* vect1= getNormal(makeVector(origin, point1));
380                         double* vect2= getNormal(makeVector(origin, point2));                           
381                         double* crossp = getCrossProduct(vect1, vect2);
382
383                         double *newx = getCrossProduct(vect2, crossp);
384
385                         int ext[6],factor=0;
386                         bbGetInputIn()->GetExtent(ext);
387
388                         factor = ext[0]<ext[3]? ext[3] : ext[0];
389                         factor = factor<ext[5]? ext[5] : factor;
390
391         //for the plane widgets
392                         vtkImagePlaneWidget* plane3pts = (vtkImagePlaneWidget*)bbGetOutputPlane3Pts();
393                         plane3pts->SetInput(bbGetInputIn());                    
394                         double xSpacing, ySpacing, zSpacing;
395                         bbGetInputIn()->GetSpacing(xSpacing, ySpacing, zSpacing);
396                         plane3pts->SetOrigin(pointsx[0]*xSpacing,pointsy[0]*ySpacing,pointsz[0]*zSpacing);      
397                         plane3pts->SetPoint1((origin[0]+newx[0]*factor)*xSpacing,
398                                                                         (origin[1]+newx[1]*factor)*ySpacing,
399                                                                         (origin[2]+newx[2]*factor)*zSpacing);
400                         plane3pts->SetPoint2((origin[0]+vect2[0]*factor)*xSpacing,
401                                                                         (origin[1]+vect2[1]*factor)*ySpacing,
402                                                                         (origin[2]+vect2[2]*factor)*zSpacing);
403                         plane3pts->GetResliceOutput()->Update();
404 //To get the slice of image out of the selected volume
405                         if (_imageReslicer==NULL){
406                                 _imageReslicer = vtkImageReslice::New();                                        
407                                 _imageReslicer->SetOutputDimensionality(2);
408                                 _transform = vtkTransform::New();
409                                 _matrix = vtkMatrix4x4::New();  
410                         }
411                         _imageReslicer->SetInterpolationMode( bbGetInputInterpolation() );
412                         _imageReslicer->SetInput( bbGetInputIn() );
413                         _imageReslicer->SetInformationInput(bbGetInputIn());    
414                         //fill out the information with the created vectors and using the spacing of the image
415                         _imageReslicer->SetResliceAxesDirectionCosines(newx[0]*xSpacing,newx[1]*xSpacing,newx[2]*xSpacing,
416                                                                         vect2[0]*ySpacing,vect2[1]*ySpacing,vect2[2]*ySpacing,
417                                                                         crossp[0]*zSpacing,crossp[1]*zSpacing,crossp[2]*zSpacing);                      
418                         _imageReslicer->SetResliceAxesOrigin(origin[0]*xSpacing,origin[1]*ySpacing,origin[2]*zSpacing);
419                         _imageReslicer->GetOutput()->Update();
420                         _imageReslicer->GetOutput()->UpdateInformation();
421
422                         bbSetOutputImage3Pts(_imageReslicer->GetOutput());
423
424                         _matrix->Identity();    
425
426                         _matrix->SetElement(0,0,newx[0]*xSpacing);
427                         _matrix->SetElement(1,0,newx[1]*xSpacing);
428                         _matrix->SetElement(2,0,newx[2]*xSpacing);
429                         _matrix->SetElement(0,1,vect2[0]*ySpacing);
430                         _matrix->SetElement(1,1,vect2[1]*ySpacing);
431                         _matrix->SetElement(2,1,vect2[2]*ySpacing);
432                         _matrix->SetElement(0,2,crossp[0]*zSpacing);
433                         _matrix->SetElement(1,2,crossp[1]*zSpacing);
434                         _matrix->SetElement(2,2,crossp[2]*zSpacing);
435                         _matrix->SetElement(0,3,origin[0]*xSpacing);
436                         _matrix->SetElement(1,3,origin[1]*ySpacing);
437                         _matrix->SetElement(2,3,origin[2]*zSpacing);
438
439                         _transform->SetMatrix(_matrix);
440
441                         //set the transformation out to be used by other bbBoxes
442                         bbSetOutputTransform3Pts((vtkLinearTransform*)_transform);                      
443                 }       // pointsx pointsy  pointsz
444         } // bbGetInputIn
445   }
446         
447   void ImagePlanes::updateInteractor(){
448
449         vtkRenderWindowInteractor* interactor = bbGetInputInteractor();
450
451         if(interactor){
452                 bbGetOutputPlaneX()->SetInteractor(interactor);
453                 bbGetOutputPlaneX()->EnabledOn();
454                 bbGetOutputPlaneY()->SetInteractor(interactor);
455                 bbGetOutputPlaneY()->EnabledOn();
456                 bbGetOutputPlaneZ()->SetInteractor(interactor);
457                 bbGetOutputPlaneZ()->EnabledOn();
458                 bbGetOutputPlane3Pts()->SetInteractor(interactor);
459                 bbGetOutputPlane3Pts()->EnabledOn();
460         }
461   }
462         //-----------------------------------------------------------------     
463   void vtkImageDataPointerRelay::bbUserSetDefaultValues()
464         {
465                 
466         }
467         
468         //-----------------------------------------------------------------     
469   void vtkImageDataPointerRelay::bbUserInitializeProcessing()
470         {
471         }
472         
473         //-----------------------------------------------------------------     
474   void vtkImageDataPointerRelay::bbUserFinalizeProcessing()
475         {
476         }
477         
478   vtkImagePlaneWidget* ImagePlanes::GetPlaneWidget(unsigned char activationkey, double r, double g, double b, vtkCellPicker* picker)
479   {
480                 vtkProperty* prop1 = 0;         
481                 vtkImagePlaneWidget* planeWidget = 0;
482
483                 planeWidget = vtkImagePlaneWidget::New();
484                 planeWidget->DisplayTextOn();
485                 planeWidget->SetPicker(picker);
486                 planeWidget->SetKeyPressActivationValue(activationkey);
487                 prop1 = planeWidget->GetPlaneProperty();
488                 prop1->SetColor(r, g, b);
489
490                 return planeWidget;
491   }
492
493   double* ImagePlanes::getCrossProduct(double* vect0,double* vect1){
494         double* vectCross;
495         vectCross = new double[3];
496         vectCross[0] = vect0[1]*vect1[2]-(vect0[2]*vect1[1]);
497         vectCross[1] = -(vect0[0]*vect1[2]-(vect0[2]*vect1[0]));
498         vectCross[2] = vect0[0]*vect1[1]-(vect0[1]*vect1[0]);
499
500         return vectCross;
501   }
502 /**
503 **      Returns the magnitud of the given vector
504 **/
505   double ImagePlanes::getMagnitud(double* vect){
506
507         double mag;
508         mag = sqrt(pow(vect[0],2) + pow(vect[1],2) + pow(vect[2],2));
509         //std::cout<<"mag "<<mag <<std::endl;
510         return mag;
511   }
512 /**
513 **      returns the unitary vector of the given vector
514 **      u = 1/|vect| . vect
515 **/
516   double* ImagePlanes::getNormal(double* vect){
517
518         double* vectnorm;
519         double mag = getMagnitud(vect);
520
521         vectnorm = new double[3];
522
523         if(mag!=0){
524                 vectnorm[0] = vect[0]/mag;
525                 vectnorm[1] = vect[1]/mag;
526                 vectnorm[2] = vect[2]/mag;
527         }else{
528                 vectnorm[0] = 0;
529                 vectnorm[1] = 0;
530                 vectnorm[2] = 0;
531         }
532
533         return vectnorm;
534   }
535
536   double* ImagePlanes::makeVector(double podouble0[3], double podouble1[3]){
537         double *vect;
538         vect = new double[3];
539
540         vect[0]= podouble1[0]-podouble0[0];
541         vect[1]= podouble1[1]-podouble0[1];
542         vect[2]= podouble1[2]-podouble0[2];
543
544         return vect;
545   }
546         
547 }//namespace bbtk
548
549 #endif // _USE_VTK_