]> Creatis software - bbtk.git/blob - packages/wxvtk/src/bbwxvtkViewer2D.cxx
1.6
[bbtk.git] / packages / wxvtk / src / bbwxvtkViewer2D.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbwxvtkViewer2D.cxx,v $
5   Language:  C++
6   Date:      $Date: 2009/09/17 11:17:18 $
7   Version:   $Revision: 1.38 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 /**
19  *  \file 
20  *  \brief 
21  */
22
23
24 #ifdef USE_WXWIDGETS
25 #ifdef USE_VTK
26
27 #include "bbwxvtkViewer2D.h"
28 #include "bbwxvtkPackage.h"
29 //#include <wx/dialog.h>
30
31 #include "vtkInteractorStyleImage.h"
32 #include "vtkCamera.h"
33 #include "vtkRenderer.h"
34 #include <vtkInteractorStyleImage.h>
35 #include <vtkCommand.h>
36 #include <vtkImageActor.h>
37 namespace bbwxvtk
38 {
39   //--------------------------------------------------------------------------
40   class Viewer2DWidget : public wxPanel
41   {
42   public:
43     Viewer2DWidget(Viewer2D* box, wxWindow *parent);
44     ~Viewer2DWidget();
45     void UpdateView();
46     vtkRenderer         *GetRenderer();
47     vtkImageActor       *GetImageActor();
48   private:
49     bool                        mFirstTime;
50     Viewer2D                    *mBox;
51     wxvtkImageViewer2           *imageViewer;
52     vtkImageData                *backImageData;
53     vtkImageData                *mDefaultImage;
54     wxVTKRenderWindowInteractor *wxvtkrenderwindowinteractor;
55     bool mUpdateCamera;
56
57     vtkImplicitPlaneWidget * maPlane1, * maPlane2, * mbPlane1, * mbPlane2;
58   };
59   
60   Viewer2DWidget::Viewer2DWidget(Viewer2D* box,
61                                  wxWindow *parent)
62     :  
63     wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
64     mFirstTime(true),
65     mBox(box)
66   {
67     wxPanel *panel      = this;
68
69     wxvtkrenderwindowinteractor = new wxVTKRenderWindowInteractor(panel,-1);
70     wxvtkrenderwindowinteractor->UseCaptureMouseOn();   
71
72     imageViewer = wxvtkImageViewer2::New();
73     imageViewer->SetSlice( 1 );
74     imageViewer->SetupInteractor ( wxvtkrenderwindowinteractor );
75     
76     mDefaultImage = vtkImageData::New();
77     int dim[3];
78     dim[0] = dim[1] = 256 ; 
79     dim[2] = 2; 
80     mDefaultImage->SetDimensions ( dim );
81     mDefaultImage->SetSpacing(1,1,1);
82     mDefaultImage->SetExtent(0,dim[0]-1,0,dim[1]-1,0,dim[2]-1);
83     mDefaultImage->SetWholeExtent(0,dim[0]-1,0,dim[1]-1,0,dim[2]-1);
84
85     mDefaultImage->SetScalarTypeToUnsignedChar();
86     mDefaultImage->AllocateScalars();    
87     for (int i=0;i<dim[0];i++) 
88       for (int j=0;j<dim[1];j++) 
89         for (int k=0;k<dim[2];k++) 
90            mDefaultImage->SetScalarComponentFromFloat(i,j,k,0,0);
91
92     for (int i=0;i<dim[0];i++) 
93       {
94         /*
95         mDefaultImage->SetScalarComponentFromFloat(i,0,0,0,255);
96         mDefaultImage->SetScalarComponentFromFloat(i,dim[1]-1,0,0,255);
97         mDefaultImage->SetScalarComponentFromFloat(0,i,0,0,255);
98         mDefaultImage->SetScalarComponentFromFloat(dim[0]-1,i,0,0,255);
99         mDefaultImage->SetScalarComponentFromFloat(i,0,1,0,255);
100         mDefaultImage->SetScalarComponentFromFloat(i,dim[1]-1,1,0,255);
101         mDefaultImage->SetScalarComponentFromFloat(0,i,1,0,255);
102         mDefaultImage->SetScalarComponentFromFloat(dim[0]-1,i,1,0,255);
103         */
104         mDefaultImage->SetScalarComponentFromFloat(i,i,0,0,255);
105         mDefaultImage->SetScalarComponentFromFloat(i,dim[1]-i-1,0,0,255);
106         mDefaultImage->SetScalarComponentFromFloat(i,i,1,0,255);
107         mDefaultImage->SetScalarComponentFromFloat(i,dim[1]-i-1,1,0,255);
108       }
109
110     backImageData = mDefaultImage;
111     imageViewer->SetInput( backImageData );
112     mUpdateCamera = true;
113
114     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
115     sizer->Add( wxvtkrenderwindowinteractor, 1, wxEXPAND, 0);
116     panel->SetSizer(sizer);
117     panel->SetAutoLayout(true);
118     panel->Layout();
119
120
121     //  wxvtkrenderwindowinteractor->Refresh();
122     //  wxvtkrenderwindowinteractor->Render();
123     //  imageViewer->GetRenderer()->ResetCameraClippingRange();
124
125     imageViewer->GetRenderer()->SetBackground(0.1,0.1,0.2);
126
127   }
128   //-------------------------------------------------------------------------
129   
130   Viewer2DWidget::~Viewer2DWidget()
131   {
132     //    delete wxvtkrenderwindowinteractor;
133     // pourquoi pas wxvtkrenderwindowinteractor->Delete(); // JP
134     // LG : because it has been allocated with operator new not method ::New 
135     // we want to force its destruction without taking care of vtk ref counting
136     // on the contrary the following vtk objects are deleted with vtk Delete
137     //    std::cout << "Viewer2DWidget::~Viewer2DWidget()" << std::endl;
138     mDefaultImage->Delete();
139
140     if (backImageData)
141       {
142         //      backImageData->UnRegister(NULL);
143       }
144     if (imageViewer) 
145       {
146         imageViewer->Delete();
147       }
148     delete wxvtkrenderwindowinteractor;
149     //    std::cout << "Viewer2DWidget::~Viewer2DWidget() OK" << std::endl;
150   }
151
152   //-------------------------------------------------------------------------
153
154
155   void Viewer2DWidget::UpdateView()
156   {
157     //    std::cout << "Viewer2DWidget::UpdateView() " << std::endl;
158     //    std::cout << "slice="<<mBox->bbGetInputSlice()<<std::endl;
159     mUpdateCamera = false; //JPR  
160     if ( ( mBox->bbGetInputIn() == NULL ) &&
161          ( backImageData != mDefaultImage ) )
162       {
163         // 
164         //      std::cout << "** Viewer2DWidget::UpdateView() : NULL Input (reset)" <<std::endl;
165         backImageData = mDefaultImage;
166         mUpdateCamera = true;
167       }
168     else if ( ( mBox->bbGetInputIn() != NULL ) && 
169               (( mBox->bbGetInputStatus("In") != bbtk::UPTODATE  ) ||
170                ( mFirstTime))
171               )
172               //(backImageData != mBox->bbGetInputIn()) ) 
173       { 
174         //      std::cout << "** Viewer2DWidget::UpdateView() : Input changed"  <<std::endl;
175         mFirstTime = false;
176         if (backImageData != mBox->bbGetInputIn()) // Don't update if just Slice changed
177           {
178           backImageData = mBox->bbGetInputIn();
179           backImageData->Update();
180           imageViewer->SetInput( backImageData );
181           mUpdateCamera = true;
182           }
183       }
184
185     if (mUpdateCamera)
186       {
187         //      std::cout << "** Viewer2DWidget::UpdateView() : Update Camera"
188         //                <<std::endl;
189         int x1,x2,y1,y2,z1,z2;
190         double spx,spy,spz;
191         backImageData->GetSpacing(spx,spy,spz);
192         backImageData->GetExtent (x1,x2,y1,y2,z1,z2);
193         
194         double *range = backImageData->GetScalarRange();
195         imageViewer->SetColorWindow(range[1] - range[0]);
196         imageViewer->SetColorLevel(0.5 * (range[1] + range[0]));
197
198         imageViewer->GetRenderer()->ResetCamera();
199         double bounds[6];
200         imageViewer->GetRenderer()->ComputeVisiblePropBounds(bounds);
201         imageViewer->GetRenderer()->ResetCameraClippingRange(bounds);
202         
203         //      bbtkMessage("Output",2,"Viewer2D '"<<mBox->bbGetName()<<"' - Image :"<<std::endl);
204         //      std::cout << " extent  = "<< x1<<"-"<<x2<<" x "<<y1<<"-"<<y2<<" x "<<z1<<"-"<<z2<<std::endl;
205         //      std::cout << " spacing = "<<spx << " - "<< spy << " - "<<spz<<std::endl;
206         /*
207         vtkCamera *camera = imageViewer->GetRenderer()->GetActiveCamera();
208
209         camera->SetViewUp    ( spx*0,         -spy*1,        spz*0 );
210         camera->SetPosition  ( spx*(x1+x2)/2, spy*(y1+y2)/2, -spz*10000000 ); 
211         camera->SetFocalPoint( spx*(x1+x2)/2, spy*(y1+y2)/2, spz*0 ); 
212
213         camera->ComputeViewPlaneNormal();
214         camera->SetParallelScale(  spx*(x2-x1)/2.0 );
215         //   
216         //      imageViewer->GetRenderer()->ResetCamera(x1,x2,y1,y2,z1,z2);
217         mUpdateCamera = false;
218         //      std::cout << "Viewer2DWidget::UpdateView() passe par 3" << std::endl;
219         imageViewer->SetColorWindow(512);
220         imageViewer->SetColorLevel(256);
221         */
222          /*
223         for (int i=0;i<10;i++)
224           for (int j=0;j<10;j++)
225             std::cout << mBox->bbGetInputIn()->GetScalarComponentAsFloat(i,j,0,0)
226                       << std::endl;
227          */
228     }
229
230     //  std::cout << "OW = "<< mBox->bbGetWindow() << std::endl;
231     //  std::cout << "CW = "<< mBox->bbGetContainingWindow() << std::endl;
232     //  std::cout << "Sh = "<< mBox->bbIsShown() << std::endl;
233
234     bool interp = mBox->bbGetInputInterpolate();
235     if ( interp )
236         imageViewer->GetImageActor()->InterpolateOn();
237     else
238         imageViewer->GetImageActor()->InterpolateOff();
239         
240     
241     int orientation = mBox->bbGetInputOrientation();
242     if (orientation<0 || orientation>2) {
243        bbtkMessage("Output", 2, "Viewer2D : Orientation was not 0< <2 "<<std::endl);
244        orientation=2; // Why not?      
245     }
246
247     // mBox->bbIsShown() is necessary to be sure that the wxWindow is already 
248     // created and displayed. 
249     // Else when slice != 0 we get an X Window System error with wxGTK
250     if (
251         (backImageData!=NULL)&&
252         (mBox->bbIsShown()))
253     {
254                 int z = mBox->bbGetInputSlice();
255                 int ext[6];
256                 backImageData->GetWholeExtent(ext);
257                 /*
258                 std::cout << "ext = "<<ext[0]<<" - "<<ext[1]<<" ; "
259                           <<ext[2]<<" - "<<ext[3]<<" ; "
260                           <<ext[4]<<" - "<<ext[5]
261                           <<std::endl;
262                 */
263             switch(orientation){
264                case 0:
265                 if (z<ext[0]) { z=ext[0]; }
266                 if (z>ext[1]) { z=ext[1]; }
267                  
268                 break;
269                                    
270                case 1:
271                 if (z<ext[2]) { z=ext[2]; }
272                 if (z>ext[3]) { z=ext[3]; }
273                 break;
274                                                     
275                case 2:
276                 if (z<ext[4]) { z=ext[4]; }
277                 if (z>ext[5]) { z=ext[5]; }
278                 break;
279                 
280               default:
281                 bbtkMessage("Output", 2, "Viewer2D : Orientation was not 0< <2 (shouldn't arrive here!"<<std::endl);
282                 z = ext[5]; // Why not?
283            }
284             wxvtkrenderwindowinteractor->Refresh();
285             wxvtkrenderwindowinteractor->Render();
286             bbtkDebugMessage("Output",3,"Viewer2D : slice = "<<z<<std::endl);
287             imageViewer->SetSliceOrientation (orientation);
288             imageViewer->SetSlice( z );
289
290     }   
291   }
292
293
294   vtkRenderer * Viewer2DWidget::GetRenderer()
295   {
296           return imageViewer->GetRenderer();
297   }
298
299
300
301   //--------------------------------------------------------------------------
302   //-------------------------------------------------------------------------
303   //--------------------------------------------------------------------------
304   //--------------------------------------------------------------------------
305
306
307   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wxvtk,Viewer2D);
308   BBTK_BLACK_BOX_IMPLEMENTATION(Viewer2D,bbtk::WxBlackBox);
309
310         
311         //-----------------------------------------------------------------     
312         void Viewer2D::bbUserSetDefaultValues()
313         {
314                 bbSetInputIn(NULL);
315                 bbSetInputInterpolate(true);  // JP
316                 bbSetInputSlice(0);
317                 bbSetOutputWidget(0);
318                 bbSetOutputRenderer(0);
319                 bbSetInputOrientation(2);
320         }
321         
322         //-----------------------------------------------------------------     
323         void Viewer2D::bbUserInitializeProcessing()
324         {
325         }
326         
327         //-----------------------------------------------------------------     
328         void Viewer2D::bbUserFinalizeProcessing()
329         {
330         }
331         
332         //--------------------------------------------------------------------------
333   void Viewer2D::Process() 
334   { 
335     //    std::cout << "Viewer2D::Process" << std::endl;
336     //    std::cout << "In="<<bbtk::GetIOStatusString(bbGetInputStatus("In")) << std::endl;
337     //    std::cout << "Slice="<<bbtk::GetIOStatusString(bbGetInputStatus("Slice")) << std::endl;
338     Viewer2DWidget* w = (Viewer2DWidget*)bbGetOutputWidget(); 
339     if (w)
340       {      
341         w->UpdateView();
342       }
343     bbSetOutputOut( bbGetInputSlice() );  
344   }
345   
346
347
348
349         //--------------------------------------------------------------------------
350   void Viewer2D::CreateWidget(wxWindow* parent)
351   {
352     Viewer2DWidget* w = new Viewer2DWidget(this,parent);
353     w->UpdateView();
354     bbSetOutputWidget(w);
355     bbSetOutputRenderer( ((Viewer2DWidget*)bbGetOutputWidget())->GetRenderer() );
356   }
357
358
359
360 }//namespace bbtk
361
362 #endif
363 #endif
364