1 /*=========================================================================
4 Module: $RCSfile: bbwxvtkViewer2D.cxx,v $
6 Date: $Date: 2009/09/17 11:17:18 $
7 Version: $Revision: 1.38 $
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.
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.
17 =========================================================================*/
27 #include "bbwxvtkViewer2D.h"
28 #include "bbwxvtkPackage.h"
29 //#include <wx/dialog.h>
31 #include "vtkInteractorStyleImage.h"
32 #include "vtkCamera.h"
33 #include "vtkRenderer.h"
34 #include <vtkInteractorStyleImage.h>
35 #include <vtkCommand.h>
36 #include <vtkImageActor.h>
39 //--------------------------------------------------------------------------
40 class Viewer2DWidget : public wxPanel
43 Viewer2DWidget(Viewer2D* box, wxWindow *parent);
46 vtkRenderer *GetRenderer();
47 vtkImageActor *GetImageActor();
51 wxvtkImageViewer2 *imageViewer;
52 vtkImageData *backImageData;
53 vtkImageData *mDefaultImage;
54 wxVTKRenderWindowInteractor *wxvtkrenderwindowinteractor;
57 vtkImplicitPlaneWidget * maPlane1, * maPlane2, * mbPlane1, * mbPlane2;
60 Viewer2DWidget::Viewer2DWidget(Viewer2D* box,
63 wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
67 wxPanel *panel = this;
69 wxvtkrenderwindowinteractor = new wxVTKRenderWindowInteractor(panel,-1);
70 wxvtkrenderwindowinteractor->UseCaptureMouseOn();
72 imageViewer = wxvtkImageViewer2::New();
73 imageViewer->SetSlice( 1 );
74 imageViewer->SetupInteractor ( wxvtkrenderwindowinteractor );
76 mDefaultImage = vtkImageData::New();
78 dim[0] = dim[1] = 256 ;
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);
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);
92 for (int i=0;i<dim[0];i++)
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);
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);
110 backImageData = mDefaultImage;
111 imageViewer->SetInput( backImageData );
112 mUpdateCamera = true;
114 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
115 sizer->Add( wxvtkrenderwindowinteractor, 1, wxEXPAND, 0);
116 panel->SetSizer(sizer);
117 panel->SetAutoLayout(true);
121 // wxvtkrenderwindowinteractor->Refresh();
122 // wxvtkrenderwindowinteractor->Render();
123 // imageViewer->GetRenderer()->ResetCameraClippingRange();
125 imageViewer->GetRenderer()->SetBackground(0.1,0.1,0.2);
128 //-------------------------------------------------------------------------
130 Viewer2DWidget::~Viewer2DWidget()
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();
142 // backImageData->UnRegister(NULL);
146 imageViewer->Delete();
148 delete wxvtkrenderwindowinteractor;
149 // std::cout << "Viewer2DWidget::~Viewer2DWidget() OK" << std::endl;
152 //-------------------------------------------------------------------------
155 void Viewer2DWidget::UpdateView()
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 ) )
164 // std::cout << "** Viewer2DWidget::UpdateView() : NULL Input (reset)" <<std::endl;
165 backImageData = mDefaultImage;
166 mUpdateCamera = true;
168 else if ( ( mBox->bbGetInputIn() != NULL ) &&
169 (( mBox->bbGetInputStatus("In") != bbtk::UPTODATE ) ||
172 //(backImageData != mBox->bbGetInputIn()) )
174 // std::cout << "** Viewer2DWidget::UpdateView() : Input changed" <<std::endl;
176 if (backImageData != mBox->bbGetInputIn()) // Don't update if just Slice changed
178 backImageData = mBox->bbGetInputIn();
179 backImageData->Update();
180 imageViewer->SetInput( backImageData );
181 mUpdateCamera = true;
187 // std::cout << "** Viewer2DWidget::UpdateView() : Update Camera"
189 int x1,x2,y1,y2,z1,z2;
191 backImageData->GetSpacing(spx,spy,spz);
192 backImageData->GetExtent (x1,x2,y1,y2,z1,z2);
194 double *range = backImageData->GetScalarRange();
195 imageViewer->SetColorWindow(range[1] - range[0]);
196 imageViewer->SetColorLevel(0.5 * (range[1] + range[0]));
198 imageViewer->GetRenderer()->ResetCamera();
200 imageViewer->GetRenderer()->ComputeVisiblePropBounds(bounds);
201 imageViewer->GetRenderer()->ResetCameraClippingRange(bounds);
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;
207 vtkCamera *camera = imageViewer->GetRenderer()->GetActiveCamera();
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 );
213 camera->ComputeViewPlaneNormal();
214 camera->SetParallelScale( spx*(x2-x1)/2.0 );
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);
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)
230 // std::cout << "OW = "<< mBox->bbGetWindow() << std::endl;
231 // std::cout << "CW = "<< mBox->bbGetContainingWindow() << std::endl;
232 // std::cout << "Sh = "<< mBox->bbIsShown() << std::endl;
234 bool interp = mBox->bbGetInputInterpolate();
236 imageViewer->GetImageActor()->InterpolateOn();
238 imageViewer->GetImageActor()->InterpolateOff();
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?
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
251 (backImageData!=NULL)&&
254 int z = mBox->bbGetInputSlice();
256 backImageData->GetWholeExtent(ext);
258 std::cout << "ext = "<<ext[0]<<" - "<<ext[1]<<" ; "
259 <<ext[2]<<" - "<<ext[3]<<" ; "
260 <<ext[4]<<" - "<<ext[5]
265 if (z<ext[0]) { z=ext[0]; }
266 if (z>ext[1]) { z=ext[1]; }
271 if (z<ext[2]) { z=ext[2]; }
272 if (z>ext[3]) { z=ext[3]; }
276 if (z<ext[4]) { z=ext[4]; }
277 if (z>ext[5]) { z=ext[5]; }
281 bbtkMessage("Output", 2, "Viewer2D : Orientation was not 0< <2 (shouldn't arrive here!"<<std::endl);
282 z = ext[5]; // Why not?
284 wxvtkrenderwindowinteractor->Refresh();
285 wxvtkrenderwindowinteractor->Render();
286 bbtkDebugMessage("Output",3,"Viewer2D : slice = "<<z<<std::endl);
287 imageViewer->SetSliceOrientation (orientation);
288 imageViewer->SetSlice( z );
294 vtkRenderer * Viewer2DWidget::GetRenderer()
296 return imageViewer->GetRenderer();
301 //--------------------------------------------------------------------------
302 //-------------------------------------------------------------------------
303 //--------------------------------------------------------------------------
304 //--------------------------------------------------------------------------
307 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wxvtk,Viewer2D);
308 BBTK_BLACK_BOX_IMPLEMENTATION(Viewer2D,bbtk::WxBlackBox);
311 //-----------------------------------------------------------------
312 void Viewer2D::bbUserSetDefaultValues()
315 bbSetInputInterpolate(true); // JP
317 bbSetOutputWidget(0);
318 bbSetOutputRenderer(0);
319 bbSetInputOrientation(2);
322 //-----------------------------------------------------------------
323 void Viewer2D::bbUserInitializeProcessing()
327 //-----------------------------------------------------------------
328 void Viewer2D::bbUserFinalizeProcessing()
332 //--------------------------------------------------------------------------
333 void Viewer2D::Process()
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();
343 bbSetOutputOut( bbGetInputSlice() );
349 //--------------------------------------------------------------------------
350 void Viewer2D::CreateWidget(wxWindow* parent)
352 Viewer2DWidget* w = new Viewer2DWidget(this,parent);
354 bbSetOutputWidget(w);
355 bbSetOutputRenderer( ((Viewer2DWidget*)bbGetOutputWidget())->GetRenderer() );