creaImageIO_lib
creaImageIOWxViewer.cpp
Go to the documentation of this file.
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
16 #
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
21 # liability.
22 #
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27 
28 #include <creaImageIOWxViewer.h>
29 
30 #include <fstream>
31 #include <vtkCamera.h>
32 #include <vtkRenderer.h>
33 #include <vtkImageData.h>
34 #include <creaMessageManager.h>
35 #include <stdio.h>
36 #include <time.h>
37 
38 
39 using namespace crea;
40 
41 namespace creaImageIO
42 {
43 
44  //=====================================================================
45 
46  //=====================================================================
47  class WxViewerPlayer: public wxThread
48  {
49  public:
51  mWxViewer(v)
52  {}
53 
54  void* Entry();
55  void OnExit();
56 
57  private:
58 
60  };
61 
62  //=====================================================================
63 
64 
65 
66 
67 
68  //=====================================================================
69  // CTor
70  WxViewer::WxViewer(wxWindow *parent,
71  wxWindowID id,
72  wxString title,
73  const wxPoint& pos,
74  const wxSize& size)
75  : wxPanel( parent,
76  id,
77  pos,
78  size)
79  {
80  wxMutexLocker lock(mMutex);
81  GimmickDebugMessage(6,"WxViewer::WxViewer"
82  <<std::endl);
83 
84  mNeedRefresh = false;
85  mLastImageShown = NULL;
86 
87  // previewer
88  mInteractor = new crea::creawxVTKRenderWindowInteractor(this,-1);
89  mInteractor->UseCaptureMouseOn();
90 
91  mViewer = vtkImageViewer2::New();
92  mViewer->SetupInteractor ( mInteractor );
93 
94  mCurrent = 0;
95  mPlayer = 0;
96 
97  // Grid to place checkbox and slider
98  mflexSizer = new wxFlexGridSizer(1,2,1,1);
99  //Slider
100  mslide = new wxSlider(this,-1,0,0,1, wxDefaultPosition, wxSize(400,40), wxSL_HORIZONTAL | wxSL_LABELS);
101  Connect( mslide->GetId(), wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WxViewer::OnSlide );
102  //CheckBox
103  mcheck = new wxCheckBox(this,5123,crea::std2wx("Cine Loop"));
104  Connect( mcheck->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED , (wxObjectEventFunction) &WxViewer::OnCineLoop );
105  mcheck->SetValue(false);
106  mflexSizer->Add(mcheck,0, wxFIXED_MINSIZE);
107  mflexSizer-> Add( mslide,1,wxALIGN_CENTER | wxFIXED_MINSIZE );
108 
109  // Sizer for Previewer and GridSizer
110  mtopSizer = new wxBoxSizer(wxVERTICAL);
111  mtopSizer->Add(mflexSizer,0);
112  mtopSizer-> Add( mInteractor ,1,wxGROW,0);
113  SetSizer(mtopSizer,true);
114 
115  Update();
116  Layout();
117  }
118  //=====================================================================
119 
120  //=====================================================================
123  {
124  wxMutexLocker lock(mMutex);
125  GimmickDebugMessage(6,"WxViewer::~WxViewer"
126  <<std::endl);
127  // TO DO : desallocate cleanly
128  if(mPlayer)
129  {
130  mPlayer->Pause();
131  mPlayer->Delete();
132  mPlayer = 0;
133  }
134  delete mInteractor;
135  //delete mslide;
136  //delete mflexSizer;
137  }
138  //=====================================================================
139 
140  //================================================================
141  void WxViewer::SetImageVector(std::vector<boost::shared_ptr<ImagePointerHolder> >& pointers)
142  {
143  wxMutexLocker lock(mMutex);
144  GimmickDebugMessage(6,"WxViewer::SetImageVector"<<std::endl);
145  imagePointers=pointers;
146 
147  mslide->SetMax((int)pointers.size());
148  mslide->SetMin(1);
149  // Refresh don't work, TO MODIFY
150  mslide->Refresh();
151  mslide->ClearTicks();
152  mslide->Hide();
153  mslide->Show();
154  StartPlayer();
155  }
156 
157  //================================================================
158 
160  {
161 
162 
163  mMutex.Unlock();
164  wxMutexLocker lock(mMutex);
165 
166  GimmickMessage(2,"WxViewer::ShowNextImage() "
167  <<mCurrent+1<<"/"
168  <<imagePointers.size()<<std::endl);
169 
170  if(imagePointers.size()>0)
171  {
172  if (mCurrent<imagePointers.size())
173  {
174  boost::shared_ptr<ImagePointerHolder> iph = imagePointers[mCurrent];
175  //ImagePointerHolder* iph= imagePointers[mCurrent];
176  vtkImageData* currIm=iph->Get();
177  ShowImage(currIm);
178  if ( currIm != mLastImageShown )
179  {
180  mNeedRefresh = true;
181  mLastImageShown = currIm;
182  }
183  mCurrent++;
184  }
185  else
186  {
187  mCurrent = 0;
188  //ImagePointerHolder* iph=imagePointers[mCurrent];
189  boost::shared_ptr<ImagePointerHolder> iph = imagePointers[mCurrent];
190  vtkImageData* currIm=iph->Get();
191  ShowImage(currIm);
192  if ( currIm != mLastImageShown )
193  {
194  mNeedRefresh = true;
195  mLastImageShown = currIm;
196  }
197  mCurrent++;
198  }
199  }
200  }
201  //================================================================
202 
203  //=====================================================================
204  void WxViewer::ShowImage(vtkImageData* im)
205  {
206  GimmickDebugMessage(6,"WxViewer::ShowImage"
207  <<std::endl);
208  if (im==0) return;
209 
210  mViewer->SetInput(im);
211 
212  mViewer->SetSlice( 0 );
213 
214  int x1,x2,y1,y2,z1,z2;
215  double spx,spy,spz;
216  im->Update();
217 
218 //std::cout << "in WxViewer::ShowImage PrintSelf() =";
219 //im->PrintSelf(std::cout, vtkIndent(2));
220 
221  im->GetSpacing(spx,spy,spz);
222  //im->GetExtent (x1,x2,y1,y2,z1,z2); // JPR
223  im->GetWholeExtent (x1,x2,y1,y2,z1,z2);
224 /*
225 std::cout << "in WxViewer::ShowImage GetWholeExtent ext =";
226  std::cout << " [x1]=" << x1;
227  std::cout << " [x2]=" << x2;
228  std::cout << " [y1]=" << y1;
229  std::cout << " [y2]=" << y2;
230  std::cout << " [z1]=" << z1;
231  std::cout << " [z2]=" << z2;
232 std::cout << std::endl;
233 */
234  if ((x1!=mx1) ||
235  (x2!=mx2) ||
236  (y1!=my1) ||
237  (y2!=my2) ||
238  (z1!=mz1) ||
239  (z2!=mz2) ||
240  (spx!=mspx) ||
241  (spy!=mspy) ||
242  (spz!=mspz)
243  )
244  {
245  mx1 = x1;
246  mx2 = x2;
247  my1 = y1;
248  my2 = y2;
249  mz1 = z1;
250  mz2 = z2;
251  mspx = spx;
252  mspy = spy;
253  mspz = spz;
254 
255  double *range = im->GetScalarRange();
256  mViewer->SetColorWindow(range[1] - range[0]);
257  mViewer->SetColorLevel(0.5 * (range[1] + range[0]));
258 
259  mViewer->GetRenderer()->ResetCamera();
260 
261  //EED 21 mars 2012 FLIP problem ..PLOP..
262  vtkCamera *camera =mViewer->GetRenderer()->GetActiveCamera();
263  camera->SetViewUp ( 0 , -1 , 0 );
264  camera->SetPosition ((x1+x2)/2 , (y1+y2)/2 , -10000);
265  camera->SetFocalPoint ((x1+x2)/2 , (y1+y2)/2 , 0 );
266  camera->SetParallelScale( (x2-x1)/3.0 );
267 
268 
269  double bounds[6];
270 
271  mViewer->GetRenderer()->ComputeVisiblePropBounds(bounds);
272 
273  mViewer->GetRenderer()->ResetCameraClippingRange(bounds);
274  mViewer->GetRenderer()->SetBackground(0.1,0.1,0.2);
275  }
276  }
277  //================================================================
278 
279  //================================================================
281  {
282  if (mNeedRefresh)
283  {
284  GimmickDebugMessage(10,"WxViewer : Refreshing"<<std::endl);
285 
286  mInteractor->Render();
287  mNeedRefresh = false;
288  return true;
289  }
290  return false;
291  }
292  //================================================================
293 
294  //==================================================
296  {
297  wxMutexLocker lock(mMutex);
298  if (mPlayer==0 ) return;
299  mPlayer->Delete();
300  mPlayer=0;
301  mMutex.Unlock();
302  }
303  //================================================================
304 
305  //==================================================
307  {
308  if(mcheck->IsChecked())
309  {
310  // wxMutexLocker lock(mMutex);
311  if (mPlayer != 0) return;
312  mPlayer = new WxViewerPlayer(this);
313  mPlayer->Create();
314  mPlayer->Run();
315  }
316  else
317  {
318  ShowNextImage();
319  }
320  }
321 
322  //================================================================
323 
324  //==================================================
325 
326  void WxViewer::OnCineLoop(wxCommandEvent &Event)
327  {
328  if(!mcheck->IsChecked())
329  {
330  mPlayer->Pause();
331  mPlayer->Delete();
332  mPlayer = 0;
333  }
334  StartPlayer();
335  }
336 
337  //================================================================
338 
339  //==================================================
340 
341  void WxViewer::OnSlide(wxCommandEvent &Event)
342  {
343  mCurrent = mslide->GetValue() -1;
344  StartPlayer();
345  }
346  //================================================================
347 
348  //==================================================
349 
351  {
352  mslide->SetValue(mCurrent);
353  }
354 
355  // BEGIN_EVENT_TABLE(WxGimmickFrame, wxDialog)
356  // END_EVENT_TABLE()
357  //================================================================
358 
359 //========================================================================
360 //========================================================================
361 //========================================================================
362 //========================================================================
363 //========================================================================
364 //========================================================================
365 //========================================================================
366 //========================================================================
367 
369  {
370 
371  GimmickDebugMessage(6,"WxViewerPlayer::Entry()"<<std::endl);
372 
373  while(!TestDestroy())
374  {
375 
377  mWxViewer->SetValue();
378  ::wxWakeUpIdle();
379  clock_t endwait;
380  endwait = clock () + 0.2 * CLOCKS_PER_SEC ;
381  while (clock() < endwait ) {}
382 
383  }
384  return 0;
385  }
386 
387  //=====================================================================
388 
389  //=====================================================================
391  {
392  GimmickDebugMessage(6,"WxViewerPlayer::OnExit() "<<std::endl);
393  }
394 
395 
396 } // EO namespace creaImageIO
397