]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkImagePlanes.cxx
Windows compilation
[bbtk.git] / packages / vtk / src / bbvtkImagePlanes.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbvtkImagePlanes.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/01/08 10:18:37 $
6   Version:   $Revision: 1.19 $
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 myCallbackPlane : public vtkCommand
112  {
113  public:
114    static myCallbackPlane *New()
115       {
116          return new myCallbackPlane;
117       }
118    virtual void Execute(vtkObject *caller, unsigned long, void*)
119    {
120        currentBlackBox->Process();
121        currentBlackBox->bbSignalOutputModification();
122    } 
123    void SetCurrentBlackBox(ImagePlanes *cBB) {currentBlackBox = cBB;};    
124    void SetVtkPlaneWidget( vtkImagePlaneWidget *planeWidget );
125    myCallbackPlane() {};
126
127  private:
128    vtkPlaneWidget *planeWidget;
129    ImagePlanes *currentBlackBox;
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      myCallbackPlane *_myCallback = myCallbackPlane::New();
210      _myCallback->SetCurrentBlackBox(this);
211      planeWidgetX->AddObserver(vtkCommand::InteractionEvent,_myCallback);
212      planeWidgetY->AddObserver(vtkCommand::InteractionEvent,_myCallback);
213      planeWidgetZ->AddObserver(vtkCommand::InteractionEvent,_myCallback);  
214    }
215
216 //---------------------------------------------------------------------
217   void ImagePlanes::bbUserDestructor()
218   {
219
220     if (bbGetOutputPlaneX()) bbGetOutputPlaneX()->UnRegister(NULL);
221     if (bbGetOutputPlaneY()) bbGetOutputPlaneY()->UnRegister(NULL);
222     if (bbGetOutputPlaneZ()) bbGetOutputPlaneZ()->UnRegister(NULL);
223   }
224   
225 //---------------------------------------------------------------------  
226    void ImagePlanes::Process()
227    {
228      if (bbGetInputIn()!=0)
229        {
230          // Create the widgets if not already done
231          Init();
232
233          if ( bbGetInputStatus("In") != bbtk::UPTODATE )
234            {
235              // Input image has changed : reinitialize planes
236              image = bbGetInputIn();
237
238              int xMin, xMax, yMin, yMax, zMin, zMax;
239              bbGetInputIn()->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
240              
241              // Initial values : center of the volume (in real world, not in pixels!)
242              double xSpacing, ySpacing, zSpacing;
243              bbGetInputIn()->GetSpacing(xSpacing, ySpacing, zSpacing);
244           
245
246              bbGetOutputPlaneX()->SetInput(bbGetInputIn());
247              bbGetOutputPlaneX()->SetPlaneOrientationToXAxes();  
248              bbGetOutputPlaneX()->SetSlicePosition((xMax+xMin)/2.*xSpacing);
249              
250              bbGetOutputPlaneY()->SetInput(bbGetInputIn());
251              bbGetOutputPlaneY()->SetPlaneOrientationToYAxes();
252              bbGetOutputPlaneY()->SetSlicePosition((yMax+yMin)/2.*ySpacing);
253              
254              bbGetOutputPlaneZ()->SetInput(bbGetInputIn());
255              bbGetOutputPlaneZ()->SetPlaneOrientationToZAxes();
256              bbGetOutputPlaneZ()->SetSlicePosition((zMax+zMin)/2.*zSpacing);
257              
258              if (bbGetInputWindowLevel()[0]!=0)
259                {
260                  bbGetOutputPlaneZ()->SetWindowLevel(bbGetInputWindowLevel()[0],
261                                                      bbGetInputWindowLevel()[1]);
262                }
263              else 
264                {
265                  double *range = image->GetScalarRange();
266                  bbGetOutputPlaneZ()->SetWindowLevel(range[1] - range[0],
267                                                      0.5*(range[1]+range[0]));
268                }
269            }
270          
271          
272          // UPDATE DES SORTIES 
273          bbGetOutputPlaneX()->GetResliceOutput()->Update();
274          bbGetOutputPlaneY()->GetResliceOutput()->Update(); 
275          bbGetOutputPlaneZ()->GetResliceOutput()->Update(); 
276          
277                  
278        }
279    }
280 }//namespace bbtk
281
282 #endif // _USE_VTK_
283