]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkImagePlanes.cxx
Try to display planes (from Image Planes)
[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/03 09:34:39 $
6   Version:   $Revision: 1.14 $
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 #include "vtkMetaImageWriter.h"
42
43 #include "bbstdCast.h"
44 namespace bbstd
45 {
46   //====================================================================
47   BBTK_BLACK_BOX_TEMPLATE2_IMPLEMENTATION(Cast,
48                                           bbtk::AtomicBlackBox);
49   //====================================================================
50
51 }
52 using namespace bbstd;
53 namespace bbvtk
54 {
55   //====================================================================
56   // Add the specialized adaptors to the package
57   typedef vtkImagePlaneWidget* I;
58   typedef vtkInteractorObserver* O;
59
60   BBTK_ADD_TEMPLATE2_BLACK_BOX_TO_PACKAGE(vtk,Cast,I,O);
61
62 }
63
64 namespace bbvtk
65 {
66
67    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,ImagePlanes)
68    BBTK_BLACK_BOX_IMPLEMENTATION(ImagePlanes,bbtk::AtomicBlackBox);
69
70    void ImagePlanes::bbUserConstructor() 
71    { 
72      planeWidgetX = planeWidgetY = planeWidgetZ = 0;
73      imageX       = imageY       = imageZ       = 0;
74      bbSetInputIn(0);
75      std::vector<double> vect;
76      vect.push_back(512);
77      vect.push_back(256);
78      bbSetInputWindowLevel (vect);  
79
80      bbSetModifiedStatus();
81    }
82    
83    void ImagePlanes::bbUserCopyConstructor(bbtk::BlackBox::Pointer) 
84    { 
85      planeWidgetX = planeWidgetY = planeWidgetZ = 0;
86      imageX       = imageY       = imageZ       = 0;
87    }
88
89    void ImagePlanes::Init() 
90    {  
91      if (planeWidgetX != 0) return;
92        
93      // The shared picker enables us to use 3 planes at one time
94      // and gets the picking order right
95      vtkCellPicker* picker = vtkCellPicker::New();
96      picker->SetTolerance(0.005);
97   
98      // The 3 image plane widgets 
99      planeWidgetX = vtkImagePlaneWidget::New();
100      planeWidgetX->DisplayTextOn();
101      planeWidgetX->SetPicker(picker);
102      planeWidgetX->SetKeyPressActivationValue('x');
103      vtkProperty* prop1 = planeWidgetX->GetPlaneProperty();
104      prop1->SetColor(1, 0, 0);
105
106      planeWidgetY = vtkImagePlaneWidget::New();
107      planeWidgetY->DisplayTextOn();
108      planeWidgetY->SetPicker(picker);
109      planeWidgetY->SetKeyPressActivationValue('y');
110      vtkProperty* prop2 = planeWidgetY->GetPlaneProperty();
111      prop2->SetColor(1, 1, 0);
112      planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
113
114      planeWidgetZ = vtkImagePlaneWidget::New();
115      planeWidgetZ->DisplayTextOn();
116      planeWidgetZ->SetPicker(picker);
117      planeWidgetZ->SetKeyPressActivationValue('z');
118      vtkProperty* prop3 = planeWidgetZ->GetPlaneProperty();
119      prop3->SetColor(0, 0, 1);
120      planeWidgetZ->SetLookupTable(planeWidgetX->GetLookupTable());
121
122      bbSetOutputPlaneX(planeWidgetX);
123      bbSetOutputPlaneY(planeWidgetY);
124      bbSetOutputPlaneZ(planeWidgetZ);
125
126      picker->UnRegister(NULL);
127      
128      myCallbackPlane *_myCallback = myCallbackPlane::New();
129      _myCallback->SetCurrentBlackBox(this);
130      planeWidgetX->AddObserver(vtkCommand::InteractionEvent,_myCallback);
131      planeWidgetY->AddObserver(vtkCommand::InteractionEvent,_myCallback);
132      planeWidgetZ->AddObserver(vtkCommand::InteractionEvent,_myCallback);  
133    }
134
135 //---------------------------------------------------------------------
136   void ImagePlanes::bbUserDestructor()
137   {
138
139     if (planeWidgetX) planeWidgetX->UnRegister(NULL);
140     if (planeWidgetY) planeWidgetY->UnRegister(NULL);
141     if (planeWidgetZ) planeWidgetZ->UnRegister(NULL);
142
143   }
144   
145 //---------------------------------------------------------------------  
146    void ImagePlanes::Process()
147    {
148      if (bbGetInputIn()!=0)
149        {
150          Init();
151
152          int xMin, xMax, yMin, yMax, zMin, zMax;
153          bbGetInputIn()->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
154
155          // Initial values : center of the volume (in real world, not in pixels!)
156          double xSpacing, ySpacing, zSpacing;
157          bbGetInputIn()->GetSpacing(xSpacing, ySpacing, zSpacing);
158          
159          planeWidgetX->SetInput(bbGetInputIn());
160          planeWidgetX->SetPlaneOrientationToXAxes();     
161          planeWidgetX->SetSlicePosition((xMax+xMin)/2.*xSpacing);
162          planeWidgetX->GetResliceOutput()->Update();
163          bbSetOutput("ImageX", planeWidgetX->GetResliceOutput());
164          
165          
166          vtkMetaImageWriter *writer = vtkMetaImageWriter::New();
167          
168          std::string newFilenameX("newFilenameX.mhd");
169          writer->SetInput(planeWidgetX->GetResliceOutput());
170          writer->SetFileName(newFilenameX.c_str());
171          writer->Update();
172          
173          //planeWidgetX->GetResliceOutput()->Print(std::cout);
174
175          planeWidgetY->SetInput(bbGetInputIn());
176          planeWidgetY->SetPlaneOrientationToYAxes();
177          planeWidgetY->SetSlicePosition((yMax+yMin)/2.*ySpacing);
178          planeWidgetY->GetResliceOutput()->Update(); 
179          bbSetOutput("ImageY", planeWidgetY->GetResliceOutput());
180          //planeWidgetY->GetResliceOutput()->Print(std::cout);
181          
182          std::string newFilenameY("newFilenameY.mhd");
183          writer->SetInput(planeWidgetY->GetResliceOutput());
184          writer->SetFileName(newFilenameY.c_str());
185          writer->Update();
186                  
187          planeWidgetZ->SetInput(bbGetInputIn());
188          planeWidgetZ->SetPlaneOrientationToZAxes();
189          planeWidgetZ->SetSlicePosition((zMax+zMin)/2.*zSpacing);
190          planeWidgetZ->GetResliceOutput()->Update(); 
191          bbSetOutput("ImageZ", planeWidgetZ->GetResliceOutput());
192         // planeWidgetZ->GetResliceOutput()->Print(std::cout);
193         
194         // planeWidgetZ->SetWindowLevel(512,256);
195         
196          std::string newFilenameZ("newFilenameZ.mhd");
197          writer->SetInput(planeWidgetZ->GetResliceOutput());
198          writer->SetFileName(newFilenameZ.c_str());
199          writer->Update();
200         
201          planeWidgetZ->SetWindowLevel(bbGetInputWindowLevel()[0],bbGetInputWindowLevel()[1]);
202         
203         // writer->vtkMetaImageWriter::Delete();
204          
205        }
206    }
207 }//namespace bbtk
208
209 #endif // _USE_VTK_
210