]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkImagePlanes.cxx
Fixed (sure ? to test extensively) the problem with vtk-inherited black boxes desallo...
[bbtk.git] / packages / vtk / src / bbvtkImagePlanes.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbvtkImagePlanes.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/02/27 11:20:26 $
6   Version:   $Revision: 1.20 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *  \file 
33  *  \brief 
34  */
35
36 #ifdef _USE_VTK_
37 #include "bbvtkImagePlanes.h"
38 #include "bbvtkPackage.h"
39 #include "vtkCellPicker.h"
40 #include "vtkProperty.h"
41
42 #include "vtkMetaImageWriter.h"
43 #include "vtkPNGWriter.h"
44
45 #include "bbstdCast.h"
46 #include <vtkCommand.h>
47
48 #include "vtkImageData.h"
49 //#include "vtkOutlineFilter.h"
50 //#include "vtkPolyDataMapper.h"
51 //#include "vtkActor.h"
52 #include "vtkImagePlaneWidget.h"
53 #include "vtkCellPicker.h"
54 //#include "vtkProperty.h"
55
56 //#include "vtkRenderer.h"
57 //#include "vtkCamera.h"
58
59 #include "vtkPlaneWidget.h"
60
61 #include "bbstdRelay.h"
62
63
64
65 namespace bbstd
66 {
67
68   //====================================================================
69   BBTK_BLACK_BOX_TEMPLATE2_IMPLEMENTATION(Cast,
70                                           bbtk::AtomicBlackBox);
71   //====================================================================
72   //====================================================================
73 //  BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION(Relay,
74 //                                       bbtk::AtomicBlackBox);
75   //====================================================================
76
77 }
78 using namespace bbstd;
79 /*
80 namespace bbtk
81 {
82         typedef vtkImageData::Pointer vtkImageDataPointer;
83   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(vtkImageDataPointer,
84                                        "vtkImageDataPointer");
85 }
86 */
87 namespace bbvtk
88 {
89
90
91   //====================================================================
92   // Add the specialized adaptors to the package
93   typedef vtkImagePlaneWidget* I;
94   typedef vtkInteractorObserver* O;
95
96   BBTK_ADD_TEMPLATE2_BLACK_BOX_TO_PACKAGE(vtk,Cast,I,O);
97
98   BBTK_DEFINE_RELAY_BLACK_BOX(vtkImageDataPointer,vtk,vtkImageDataPointerRelay);
99   BBTK_BLACK_BOX_IMPLEMENTATION(vtkImageDataPointerRelay,bbtk::AtomicBlackBox);
100
101   BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,vtkImageDataPointerRelay);
102  // BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(vtk,Relay,vtkImageDataPointer);
103   //Pointer);
104
105 }
106
107 namespace bbvtk
108 {
109
110   //================================================================
111   class ImagePlanes::VtkCallbackType : public vtkCommand
112  {
113  public:
114    static VtkCallbackType *New()
115       {
116          return new VtkCallbackType;
117       }
118    virtual void Execute(vtkObject *caller, unsigned long, void*)
119    {
120        mBlackBox->Process();
121        mBlackBox->bbSignalOutputModification();
122    } 
123    void SetBlackBox(ImagePlanes *BB) { mBlackBox = BB;};    
124    //   void SetVtkPlaneWidget( vtkImagePlaneWidget *planeWidget );
125    VtkCallbackType() {};
126
127  private:
128    //   vtkPlaneWidget *planeWidget;
129    ImagePlanes *mBlackBox;
130  };
131   //================================================================
132
133
134
135   //================================================================
136
137    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,ImagePlanes)
138    BBTK_BLACK_BOX_IMPLEMENTATION(ImagePlanes,bbtk::AtomicBlackBox);
139
140    void ImagePlanes::bbUserConstructor() 
141    { 
142      bbSetOutputPlaneX(0);
143      bbSetOutputPlaneY(0);
144      bbSetOutputPlaneZ(0);
145      bbSetOutputImageX(0);
146      bbSetOutputImageY(0);
147      bbSetOutputImageZ(0);
148      bbSetInputIn(0);
149      std::vector<double> vect;
150      vect.push_back(0);
151      vect.push_back(0);
152      bbSetInputWindowLevel (vect);  
153    }
154    
155    void ImagePlanes::bbUserCopyConstructor(bbtk::BlackBox::Pointer) 
156    { 
157      bbSetOutputPlaneX(0);
158      bbSetOutputPlaneY(0);
159      bbSetOutputPlaneZ(0);
160      bbSetOutputImageX(0);
161      bbSetOutputImageY(0);
162      bbSetOutputImageZ(0);
163    }
164
165    void ImagePlanes::Init() 
166    {  
167
168      /// CREATION DES WIDGETS
169      if (bbGetOutputPlaneX() != 0) return;
170        
171      // The shared picker enables us to use 3 planes at one time
172      // and gets the picking order right
173      vtkCellPicker* picker = vtkCellPicker::New();
174      picker->SetTolerance(0.005);
175   
176      // The 3 image plane widgets 
177      vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
178      planeWidgetX->DisplayTextOn();
179      planeWidgetX->SetPicker(picker);
180      planeWidgetX->SetKeyPressActivationValue('x');
181      vtkProperty* prop1 = planeWidgetX->GetPlaneProperty();
182      prop1->SetColor(1, 0, 0);
183
184      vtkImagePlaneWidget* planeWidgetY = vtkImagePlaneWidget::New();
185      planeWidgetY->DisplayTextOn();
186      planeWidgetY->SetPicker(picker);
187      planeWidgetY->SetKeyPressActivationValue('y');
188      vtkProperty* prop2 = planeWidgetY->GetPlaneProperty();
189      prop2->SetColor(1, 1, 0);
190      planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
191
192      vtkImagePlaneWidget* planeWidgetZ = vtkImagePlaneWidget::New();
193      planeWidgetZ->DisplayTextOn();
194      planeWidgetZ->SetPicker(picker);
195      planeWidgetZ->SetKeyPressActivationValue('z');
196      vtkProperty* prop3 = planeWidgetZ->GetPlaneProperty();
197      prop3->SetColor(0, 0, 1);
198      planeWidgetZ->SetLookupTable(planeWidgetX->GetLookupTable());
199
200      bbSetOutputPlaneX(planeWidgetX);
201      bbSetOutputPlaneY(planeWidgetY);
202      bbSetOutputPlaneZ(planeWidgetZ);
203      bbSetOutputImageX(planeWidgetX->GetResliceOutput());
204      bbSetOutputImageY(planeWidgetY->GetResliceOutput());
205      bbSetOutputImageZ(planeWidgetZ->GetResliceOutput());
206
207      picker->UnRegister(NULL);
208      
209      mVtkCallback = VtkCallbackType::New();
210      mVtkCallback->SetBlackBox(this);
211      planeWidgetX->AddObserver(vtkCommand::InteractionEvent,mVtkCallback);
212      planeWidgetY->AddObserver(vtkCommand::InteractionEvent,mVtkCallback);
213      planeWidgetZ->AddObserver(vtkCommand::InteractionEvent,mVtkCallback);  
214    }
215
216 //---------------------------------------------------------------------
217   void ImagePlanes::bbUserDestructor()
218   {
219
220     if (bbGetOutputPlaneX()) 
221       {
222         bbGetOutputPlaneX()->SetInput(NULL);
223         bbGetOutputPlaneX()->UnRegister(NULL);
224       }
225     if (bbGetOutputPlaneY()) 
226       {
227         bbGetOutputPlaneY()->SetInput(NULL);
228         bbGetOutputPlaneY()->UnRegister(NULL);
229       }
230     if (bbGetOutputPlaneZ()) 
231       {
232         bbGetOutputPlaneZ()->SetInput(NULL);
233         bbGetOutputPlaneZ()->UnRegister(NULL);
234       }
235     mVtkCallback->Delete();
236   }
237   
238 //---------------------------------------------------------------------  
239    void ImagePlanes::Process()
240    {
241      if (bbGetInputIn()!=0)
242        {
243          // Create the widgets if not already done
244          Init();
245
246          if ( bbGetInputStatus("In") != bbtk::UPTODATE )
247            {
248              // Input image has changed : reinitialize planes
249              image = bbGetInputIn();
250
251              int xMin, xMax, yMin, yMax, zMin, zMax;
252              bbGetInputIn()->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
253              
254              // Initial values : center of the volume (in real world, not in pixels!)
255              double xSpacing, ySpacing, zSpacing;
256              bbGetInputIn()->GetSpacing(xSpacing, ySpacing, zSpacing);
257           
258
259              bbGetOutputPlaneX()->SetInput(bbGetInputIn());
260              bbGetOutputPlaneX()->SetPlaneOrientationToXAxes();  
261              bbGetOutputPlaneX()->SetSlicePosition((xMax+xMin)/2.*xSpacing);
262              
263              bbGetOutputPlaneY()->SetInput(bbGetInputIn());
264              bbGetOutputPlaneY()->SetPlaneOrientationToYAxes();
265              bbGetOutputPlaneY()->SetSlicePosition((yMax+yMin)/2.*ySpacing);
266              
267              bbGetOutputPlaneZ()->SetInput(bbGetInputIn());
268              bbGetOutputPlaneZ()->SetPlaneOrientationToZAxes();
269              bbGetOutputPlaneZ()->SetSlicePosition((zMax+zMin)/2.*zSpacing);
270              
271              if (bbGetInputWindowLevel()[0]!=0)
272                {
273                  bbGetOutputPlaneZ()->SetWindowLevel(bbGetInputWindowLevel()[0],
274                                                      bbGetInputWindowLevel()[1]);
275                }
276              else 
277                {
278                  double *range = image->GetScalarRange();
279                  bbGetOutputPlaneZ()->SetWindowLevel(range[1] - range[0],
280                                                      0.5*(range[1]+range[0]));
281                }
282            }
283          
284          
285          // UPDATE DES SORTIES 
286          bbGetOutputPlaneX()->GetResliceOutput()->Update();
287          bbGetOutputPlaneY()->GetResliceOutput()->Update(); 
288          bbGetOutputPlaneZ()->GetResliceOutput()->Update(); 
289          
290                  
291        }
292    }
293 }//namespace bbtk
294
295 #endif // _USE_VTK_
296