]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkImagePlanes.cxx
*** empty log message ***
[bbtk.git] / packages / vtk / src / bbvtkImagePlanes.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbvtkImagePlanes.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/12/18 11:55:00 $
6   Version:   $Revision: 1.18 $
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 namespace bbstd
64 {
65
66   //====================================================================
67   BBTK_BLACK_BOX_TEMPLATE2_IMPLEMENTATION(Cast,
68                                           bbtk::AtomicBlackBox);
69   //====================================================================
70   //====================================================================
71   BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION(Relay,
72                                          bbtk::AtomicBlackBox);
73   //====================================================================
74
75 }
76 using namespace bbstd;
77
78
79 namespace bbvtk
80 {
81
82
83   //====================================================================
84   // Add the specialized adaptors to the package
85   typedef vtkImagePlaneWidget* I;
86   typedef vtkInteractorObserver* O;
87
88   BBTK_ADD_TEMPLATE2_BLACK_BOX_TO_PACKAGE(vtk,Cast,I,O);
89
90   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(vtk,Relay,vtkImageDataPointer);
91
92 }
93
94 namespace bbvtk
95 {
96
97   //================================================================
98  class myCallbackPlane : public vtkCommand
99  {
100  public:
101    static myCallbackPlane *New()
102       {
103          return new myCallbackPlane;
104       }
105    virtual void Execute(vtkObject *caller, unsigned long, void*)
106    {
107        currentBlackBox->Process();
108        currentBlackBox->bbSignalOutputModification();
109    } 
110    void SetCurrentBlackBox(ImagePlanes *cBB) {currentBlackBox = cBB;};    
111    void SetVtkPlaneWidget( vtkImagePlaneWidget *planeWidget );
112    myCallbackPlane() {};
113
114  private:
115    vtkPlaneWidget *planeWidget;
116    ImagePlanes *currentBlackBox;
117  };
118   //================================================================
119
120
121
122   //================================================================
123
124    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,ImagePlanes)
125    BBTK_BLACK_BOX_IMPLEMENTATION(ImagePlanes,bbtk::AtomicBlackBox);
126
127    void ImagePlanes::bbUserConstructor() 
128    { 
129      bbSetOutputPlaneX(0);
130      bbSetOutputPlaneY(0);
131      bbSetOutputPlaneZ(0);
132      bbSetOutputImageX(0);
133      bbSetOutputImageY(0);
134      bbSetOutputImageZ(0);
135      bbSetInputIn(0);
136      std::vector<double> vect;
137      vect.push_back(0);
138      vect.push_back(0);
139      bbSetInputWindowLevel (vect);  
140    }
141    
142    void ImagePlanes::bbUserCopyConstructor(bbtk::BlackBox::Pointer) 
143    { 
144      bbSetOutputPlaneX(0);
145      bbSetOutputPlaneY(0);
146      bbSetOutputPlaneZ(0);
147      bbSetOutputImageX(0);
148      bbSetOutputImageY(0);
149      bbSetOutputImageZ(0);
150    }
151
152    void ImagePlanes::Init() 
153    {  
154
155      /// CREATION DES WIDGETS
156      if (bbGetOutputPlaneX() != 0) return;
157        
158      // The shared picker enables us to use 3 planes at one time
159      // and gets the picking order right
160      vtkCellPicker* picker = vtkCellPicker::New();
161      picker->SetTolerance(0.005);
162   
163      // The 3 image plane widgets 
164      vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
165      planeWidgetX->DisplayTextOn();
166      planeWidgetX->SetPicker(picker);
167      planeWidgetX->SetKeyPressActivationValue('x');
168      vtkProperty* prop1 = planeWidgetX->GetPlaneProperty();
169      prop1->SetColor(1, 0, 0);
170
171      vtkImagePlaneWidget* planeWidgetY = vtkImagePlaneWidget::New();
172      planeWidgetY->DisplayTextOn();
173      planeWidgetY->SetPicker(picker);
174      planeWidgetY->SetKeyPressActivationValue('y');
175      vtkProperty* prop2 = planeWidgetY->GetPlaneProperty();
176      prop2->SetColor(1, 1, 0);
177      planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
178
179      vtkImagePlaneWidget* planeWidgetZ = vtkImagePlaneWidget::New();
180      planeWidgetZ->DisplayTextOn();
181      planeWidgetZ->SetPicker(picker);
182      planeWidgetZ->SetKeyPressActivationValue('z');
183      vtkProperty* prop3 = planeWidgetZ->GetPlaneProperty();
184      prop3->SetColor(0, 0, 1);
185      planeWidgetZ->SetLookupTable(planeWidgetX->GetLookupTable());
186
187      bbSetOutputPlaneX(planeWidgetX);
188      bbSetOutputPlaneY(planeWidgetY);
189      bbSetOutputPlaneZ(planeWidgetZ);
190      bbSetOutputImageX(planeWidgetX->GetResliceOutput());
191      bbSetOutputImageY(planeWidgetY->GetResliceOutput());
192      bbSetOutputImageZ(planeWidgetZ->GetResliceOutput());
193
194      picker->UnRegister(NULL);
195      
196      myCallbackPlane *_myCallback = myCallbackPlane::New();
197      _myCallback->SetCurrentBlackBox(this);
198      planeWidgetX->AddObserver(vtkCommand::InteractionEvent,_myCallback);
199      planeWidgetY->AddObserver(vtkCommand::InteractionEvent,_myCallback);
200      planeWidgetZ->AddObserver(vtkCommand::InteractionEvent,_myCallback);  
201    }
202
203 //---------------------------------------------------------------------
204   void ImagePlanes::bbUserDestructor()
205   {
206
207     if (bbGetOutputPlaneX()) bbGetOutputPlaneX()->UnRegister(NULL);
208     if (bbGetOutputPlaneY()) bbGetOutputPlaneY()->UnRegister(NULL);
209     if (bbGetOutputPlaneZ()) bbGetOutputPlaneZ()->UnRegister(NULL);
210   }
211   
212 //---------------------------------------------------------------------  
213    void ImagePlanes::Process()
214    {
215      if (bbGetInputIn()!=0)
216        {
217          // Create the widgets if not already done
218          Init();
219
220          if ( bbGetInputStatus("In") != bbtk::UPTODATE )
221            {
222              // Input image has changed : reinitialize planes
223              image = bbGetInputIn();
224
225              int xMin, xMax, yMin, yMax, zMin, zMax;
226              bbGetInputIn()->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
227              
228              // Initial values : center of the volume (in real world, not in pixels!)
229              double xSpacing, ySpacing, zSpacing;
230              bbGetInputIn()->GetSpacing(xSpacing, ySpacing, zSpacing);
231           
232
233              bbGetOutputPlaneX()->SetInput(bbGetInputIn());
234              bbGetOutputPlaneX()->SetPlaneOrientationToXAxes();  
235              bbGetOutputPlaneX()->SetSlicePosition((xMax+xMin)/2.*xSpacing);
236              
237              bbGetOutputPlaneY()->SetInput(bbGetInputIn());
238              bbGetOutputPlaneY()->SetPlaneOrientationToYAxes();
239              bbGetOutputPlaneY()->SetSlicePosition((yMax+yMin)/2.*ySpacing);
240              
241              bbGetOutputPlaneZ()->SetInput(bbGetInputIn());
242              bbGetOutputPlaneZ()->SetPlaneOrientationToZAxes();
243              bbGetOutputPlaneZ()->SetSlicePosition((zMax+zMin)/2.*zSpacing);
244              
245              if (bbGetInputWindowLevel()[0]!=0)
246                {
247                  bbGetOutputPlaneZ()->SetWindowLevel(bbGetInputWindowLevel()[0],
248                                                      bbGetInputWindowLevel()[1]);
249                }
250              else 
251                {
252                  double *range = image->GetScalarRange();
253                  bbGetOutputPlaneZ()->SetWindowLevel(range[1] - range[0],
254                                                      0.5*(range[1]+range[0]));
255                }
256            }
257          
258          
259          // UPDATE DES SORTIES 
260          bbGetOutputPlaneX()->GetResliceOutput()->Update();
261          bbGetOutputPlaneY()->GetResliceOutput()->Update(); 
262          bbGetOutputPlaneZ()->GetResliceOutput()->Update(); 
263          
264                  
265        }
266    }
267 }//namespace bbtk
268
269 #endif // _USE_VTK_
270