]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/ColorLayerImageViewPanel.cxx
#2651 creaMaracasVisu Feature New Normal - Color Layer: Transparent Base, Refresh
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / ThresholdImageView / ColorLayerImageViewPanel.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 /**
27  *  \file
28  *  \brief ClassThresholdImageViewPanel .
29  */
30
31
32 #include "ColorLayerImageViewPanel.h"
33 #include "OpenImageDialog.h"
34
35
36 //---------------------------------------------------------
37 //---------------------------------------------------------
38 //---------------------------------------------------------
39
40 wxDlgTransformByDimensionBySpacingByPixel::wxDlgTransformByDimensionBySpacingByPixel()
41 {
42 }
43
44 wxDlgTransformByDimensionBySpacingByPixel::~wxDlgTransformByDimensionBySpacingByPixel()
45 {
46 }
47
48
49 void wxDlgTransformByDimensionBySpacingByPixel::GetTransformType(wxWindow *parent , wxString message,int &typeOfTransformation,bool &dlgWxOK)
50 {
51         wxDialog* dial                          = new wxDialog (parent,-1,_T("Tools"),wxDefaultPosition, wxSize(590,190));
52         wxSizer* buttonsSizer   = dial->CreateSeparatedButtonSizer(wxOK|wxCANCEL);
53         wxBoxSizer *dialSizer   = new wxBoxSizer(wxVERTICAL);
54
55         wxString lstOptOperation[3];
56         lstOptOperation[0]=_T("By Dimension");
57         lstOptOperation[1]=_T("By Spacing");
58         lstOptOperation[2]=_T("By Pixel");
59         wxRadioBox * radioOpts = new wxRadioBox(dial, -1, _T("Type of Transformation"), wxDefaultPosition, wxSize(270,45), 3 , lstOptOperation,  3, wxRA_SPECIFY_COLS);
60
61         dialSizer->Add( new wxStaticText(dial,-1,message)  , 0, wxGROW );
62         dialSizer->Add( new wxStaticText(dial,-1,_T(" "))  , 0, wxGROW );
63         dialSizer->Add(radioOpts,0,wxGROW);
64         dialSizer->Add( new wxStaticText(dial,-1,_T(" "))  , 0, wxGROW );
65         dialSizer->Add(buttonsSizer,0,wxGROW);
66         dial->SetSizer(dialSizer, true);
67         dial->Layout();
68         dial->ShowModal();
69
70         typeOfTransformation    = -1;
71         dlgWxOK                 = false;
72
73         if (dial->GetReturnCode() == wxID_OK)
74         {
75                 typeOfTransformation    = radioOpts->GetSelection() + 1;
76                 dlgWxOK                 = true;
77         }
78 }
79
80
81 //---------------------------------------------------------
82 //---------------------------------------------------------
83 //---------------------------------------------------------
84
85
86 /**
87  ** Begin of the threshold panel
88  **/
89 ColorLayerImageViewPanel::ColorLayerImageViewPanel(wxWindow* parent, int min, int max, int type)
90 : wxPanel(parent, -1, wxDefaultPosition, wxSize(600,100), wxBORDER_SUNKEN)
91 {
92
93         _spcBase[0] = _spcBase[1] = _spcBase[2] = 0;
94         _dimBase[0] = _dimBase[1] = _dimBase[2] = 0;
95
96         _colorLayerImageViewManager     = new ColorLayerImageViewManager();
97
98         _btn_ReadImage                  = new wxButton(this, wxID_ANY, _T("Read Image") );
99         _thresholdGo                    = true;
100         _cb_ShowHide                    = new wxCheckBox(this, wxID_ANY, _T("Show/Hide Layer") );
101         _cb_ShowHide->SetValue(_thresholdGo);
102
103         _cb_SliceFixDinamic = new wxCheckBox(this, wxID_ANY, _T("Fixed/Dynamic Slice") );
104         _cb_SliceFixDinamic->SetValue(true);
105
106         _sl_SliceImageX = new wxSlider(this, wxID_ANY, 0, 1000, 1000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
107         _sl_SliceImageY = new wxSlider(this, wxID_ANY, 0, 1000, 1000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
108         _sl_SliceImageZ = new wxSlider(this, wxID_ANY, 0, 1000, 1000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
109         _sl_SliceImageX->Enable(false);
110         _sl_SliceImageY->Enable(false);
111         _sl_SliceImageZ->Enable(false);
112         _interpolationCheckBox = new wxCheckBox(this, -1, _T("Image Interpolation") );
113         _interpolationCheckBox->SetValue(true);
114         _opacity = new wxSlider(this, wxID_ANY, 6, 1, 10, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
115
116         _histogram                                      = new Histogram(  this , -1, wxPoint(0,0), wxSize(400,400), /*wxNO_BORDER*/ wxBORDER_DEFAULT ); 
117         _histogramMinMaxLevel   = new HistogramMinMaxLevel( (HistogramBase*)_histogram );
118
119         if (type==0)
120         {
121         }
122
123         if (type==1)
124         {
125         }
126
127
128         Connect( _btn_ReadImage->GetId(), wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &ColorLayerImageViewPanel::onReadImage );
129         Connect( _cb_ShowHide->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED  , (wxObjectEventFunction) &ColorLayerImageViewPanel::onThresholdShow );
130         Connect( _interpolationCheckBox->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &ColorLayerImageViewPanel::onThresholdInterpolation );
131         Connect( _opacity->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction) &ColorLayerImageViewPanel::onChangeOpacity );
132         Connect( _cb_SliceFixDinamic->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceFixDinamic );
133
134         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_THUMBTRACK      , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
135         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_LINEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
136         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_LINEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
137         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_PAGEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
138         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_PAGEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
139
140         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_THUMBTRACK      , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
141         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_LINEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
142         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_LINEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
143         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_PAGEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
144         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_PAGEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
145
146         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_THUMBTRACK      , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
147         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_LINEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
148         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_LINEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
149         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_PAGEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
150         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_PAGEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
151
152         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
153         if (type==1)
154         {
155                 sizer -> Add( new wxStaticText(this,-1,_T("Color Image Layer"))         , 1, wxGROW );
156                 sizer -> Add( _btn_ReadImage                                                                                                    , 1, wxGROW );
157                 sizer -> Add( new wxStaticText(this,-1,_T(" "))                                                 , 1, wxGROW );
158         }
159         sizer -> Add( new wxStaticText(this,-1,_T("Opacity Level"))                     , 1, wxGROW );
160         sizer -> Add( _opacity                                                                                                                          , 1, wxGROW );
161         sizer -> Add( new wxStaticText(this,-1,_T(" "))                                                         , 1, wxGROW );
162         sizer -> Add( _cb_ShowHide                                                                                                                      , 1, wxGROW );
163         sizer -> Add( new wxStaticText(this,-1,_T(" "))                                                         , 1, wxGROW );
164         sizer -> Add( _interpolationCheckBox                                                                                    , 1, wxGROW );
165
166         sizer -> Add( new wxStaticText(this,-1,_T(" "))                                                         , 1, wxGROW );
167         sizer -> Add( new wxStaticText(this,-1,_T("Slice "))                                    , 1, wxGROW );
168         sizer -> Add( _sl_SliceImageX                                                                                                           , 1, wxGROW );
169         sizer -> Add( _sl_SliceImageY                                                                                                           , 1, wxGROW );
170         sizer -> Add( _sl_SliceImageZ                                                                                                           , 1, wxGROW );
171         sizer -> Add( _cb_SliceFixDinamic                                                                                               , 1, wxGROW );
172         sizer -> Add( _histogramMinMaxLevel->GetWindow()                                                        , 1, wxGROW );
173
174         this->SetSizer( sizer );
175         this->SetAutoLayout( true );
176         this->Layout();
177      
178         //CM Sets the default fitting mode to Pixel mode.
179         _fitting_mode = 3;
180         // EO CM
181 }
182
183 //----------------------------------------------------------------------------
184 ColorLayerImageViewPanel::~ColorLayerImageViewPanel()
185 {
186 }
187
188
189 //----------------------------------------------------------------------------
190 ColorLayerImageViewManager* ColorLayerImageViewPanel::GetColorLayerImageViewManager()
191 {
192    return _colorLayerImageViewManager;
193 }
194
195
196 //----------------------------------------------------------------------------
197 // This is the new spacing of the background image  (1,1,1)
198 void ColorLayerImageViewPanel::SetBaseSpacing(double spc[3])
199 {
200         _spcBase[0] = spc[0];
201         _spcBase[1] = spc[1];
202         _spcBase[2] = spc[2];
203
204         _spcBase[0] = 1;
205         _spcBase[1] = 1;
206         _spcBase[2] = 1;
207 }
208
209 //----------------------------------------------------------------------------
210 // This is the Original spacing of the background image
211 void ColorLayerImageViewPanel::SetOriginalSpacing(double spc[3])
212 {
213         _spcOrg[0] = spc[0];
214         _spcOrg[1] = spc[1];
215         _spcOrg[2] = spc[2];
216 }
217
218 //----------------------------------------------------------------------------
219 // This is the size in pixels of the background image
220 void ColorLayerImageViewPanel::SetBaseDimension(int dim[3])
221 {
222         _dimBase[0] = dim[0];
223         _dimBase[1] = dim[1];
224         _dimBase[2] = dim[2];
225 }
226
227 //----------------------------------------------------------------------------
228 void ColorLayerImageViewPanel::SetImage(vtkImageData *img)
229 {
230     if (img!=NULL)
231     {
232                         _histogramMinMaxLevel->Configure( img );
233         _colorLayerImageViewManager->SetImage( img );
234
235         double spc[3];
236         img->GetSpacing(spc);
237
238         int dim[3];
239         int ext[6];
240         img->GetWholeExtent(ext);
241         dim[0] = ext[1]-ext[0];
242         dim[1] = ext[3]-ext[2];
243         dim[2] = ext[5]-ext[4];
244         _sl_SliceImageX->SetRange( 0 , dim[0] );
245         _sl_SliceImageY->SetRange( 0 , dim[1] );
246         _sl_SliceImageZ->SetRange( 0 , dim[2] );
247
248         if ( (spc[0]!=_spcBase[0]) || (spc[1]!=_spcBase[1]) || (spc[2]!=_spcBase[2]) ||
249              (dim[0]!=_dimBase[0]) || (spc[1]!=_spcBase[1]) || (spc[2]!=_spcBase[2])
250             )
251         {
252           // CM
253           int typeOfTransformation;     
254           // In case of the option is set to create a dialog box.
255           if (_fitting_mode == -1)
256             {
257               bool transformOkDlg;
258               wxString msg = _T("The image resolution of both images are not compatible. Do you want to tranform it? ");
259               wxDlgTransformByDimensionBySpacingByPixel dlg;
260               dlg.GetTransformType(this,msg,typeOfTransformation,transformOkDlg);
261               // CM Reset the default value (Pixel i.e. 3) if the dialog box returned an inappropriate value.
262               if (transformOkDlg == false)
263                 typeOfTransformation = 3;
264             }
265           else
266             typeOfTransformation = _fitting_mode;
267           //EO CM
268             if (typeOfTransformation==1)  // make dimensions equals
269             {
270                 printf("EED ColorLayerImageViewPanel::onReadImage ...WARNNING.... dif size image spc*dim ofnew image ...???\n");
271                 spc[0]=_spcBase[0]*_dimBase[0]/dim[0];
272                 spc[1]=_spcBase[1]*_dimBase[1]/dim[1];
273                 spc[2] = 1;
274             }
275
276             if (typeOfTransformation==2)  // make spacing equals
277             {
278                 spc[0] = ( _spcBase[0]/spc[0] ) * _spcOrg[0];
279                 spc[1] = ( _spcBase[1]/spc[1] ) * _spcOrg[1];
280                 spc[2] = ( _spcBase[2]/spc[2] ) * _spcOrg[2];
281             }
282
283             if (typeOfTransformation==3)  // make spacing = 1
284             {
285                 spc[0] = 1;
286                 spc[1] = 1;
287                 spc[2] = 1;
288             }
289
290             img->SetSpacing(spc);
291         } // spc !_spcBase   dim!__dimBase
292     } // _firsttime_mode
293
294 printf("EED ColorLayerImageViewPanel::SetImage\n");
295         if (_active==true)
296         {
297 printf("EED ColorLayerImageViewPanel::SetImage _active=true \n");
298             _colorLayerImageViewManager->onThreshold();
299                 RefreshView();
300         } // _ative 
301
302 }
303
304
305 void ColorLayerImageViewPanel::RefreshView()
306 {
307 //EED01         _colorLayerImageView->Refresh();
308         _colorLayerImageViewManager->Refresh();
309 }
310
311 //----------------------------------------------------------------------------
312 void ColorLayerImageViewPanel::SetFittingMode(int fitting_mode)
313 {
314   // CM Sets the fitting mode if an appropriate value is provided.
315   if (fitting_mode == -1 || fitting_mode == 1 || fitting_mode == 2 || fitting_mode == 3)
316     {
317       _fitting_mode = fitting_mode;
318     }
319   //Otherwise, an exception should be thrown (value 0 is fine, it corresponds to an empty field i.e. the default value is kept).
320   else if (fitting_mode != 0)
321     std::cout << "CM ERROR!!! The fitting mode provided has an inappropriate value. It should be an int between -1 ; 1 ; 2 ; 3, but its value is :" << fitting_mode << ". The default value will be set instead, i.e. Pixel transformation (3)." <<  std::endl;
322   //EO CM
323 }
324
325 //----------------------------------------------------------------------------
326 int ColorLayerImageViewPanel::GetFittingMode()
327 {
328   return _fitting_mode;
329 }
330
331 //----------------------------------------------------------------------------
332 void ColorLayerImageViewPanel::onReadImage(wxCommandEvent& event)
333 {
334 // EED ???
335 //      creaImageIO::WxSimpleDlg w(0,_T("Select your image"),"creaContours_Descriptor.dscp","creatisContours DB");
336 //      w.ShowModal();
337         printf("EED ColorLayerImageViewPanel::onReadImage ....WARNING... Read dlg. all images creaImageIO ... ???\n");
338
339         creaMaracasVisuKernel::OpenImageDialog diag = creaMaracasVisuKernel::OpenImageDialog();
340         SetImage( diag.getImageData() );
341 }
342
343 //----------------------------------------------------------------------------
344 void ColorLayerImageViewPanel::onThresholdChange(wxCommandEvent& event)
345 {
346                 if (_thresholdGo)
347                 {
348                  _colorLayerImageViewManager->onThresholdChange();
349                  RefreshView();
350                 //std::cout<< "Valor Min: " << minVal << " & Valor Max: " << maxVal  << std::endl;
351           } // _thresholdGo
352 }
353
354 //----------------------------------------------------------------------------
355 void ColorLayerImageViewPanel::onThresholdShow(wxCommandEvent& event)
356 {
357         _thresholdGo = _cb_ShowHide->GetValue();
358                         _active = _thresholdGo;
359         if (_thresholdGo)
360         {
361                     _colorLayerImageViewManager->onThreshold();
362         }else{
363                     _colorLayerImageViewManager->onThresholdRemove( );
364         }
365              RefreshView();
366 }
367
368 //----------------------------------------------------------------------------
369 void ColorLayerImageViewPanel::onThresholdStop()
370 {
371                 if (_thresholdGo)
372                 {
373                     _colorLayerImageViewManager->onThresholdRemove( );
374                     _thresholdGo=false;
375                 }
376 }
377
378 //----------------------------------------------------------------------------
379 void ColorLayerImageViewPanel::onThresholdInterpolation(wxCommandEvent& event)
380 {
381            _colorLayerImageViewManager->onThresholdInterpolation(_interpolationCheckBox->GetValue());
382                 RefreshView();
383 }
384
385 //----------------------------------------------------------------------------
386 void ColorLayerImageViewPanel::onSliceFixDinamic(wxCommandEvent& event)
387 {
388         bool fixdin = _cb_SliceFixDinamic->GetValue();
389         _colorLayerImageViewManager->SetSliceFixDynamic( fixdin );
390         if (fixdin==false)
391         {
392             _colorLayerImageViewManager->SetX2( _sl_SliceImageX->GetValue() );
393             _colorLayerImageViewManager->SetY2( _sl_SliceImageY->GetValue() );
394             _colorLayerImageViewManager->SetZ2( _sl_SliceImageZ->GetValue() );
395         }
396         _sl_SliceImageX->Enable(!fixdin);
397         _sl_SliceImageY->Enable(!fixdin);
398         _sl_SliceImageZ->Enable(!fixdin);
399         _colorLayerImageViewManager->onThreshold();
400         RefreshView();
401 }
402
403 //----------------------------------------------------------------------------
404 void ColorLayerImageViewPanel::onSliceImage(wxScrollEvent& event)
405 {
406    _colorLayerImageViewManager->SetX2( _sl_SliceImageX->GetValue() );
407    _colorLayerImageViewManager->SetY2( _sl_SliceImageY->GetValue() );
408    _colorLayerImageViewManager->SetZ2( _sl_SliceImageZ->GetValue() );
409    _colorLayerImageViewManager->onThreshold();
410         RefreshView();
411 }
412
413 //----------------------------------------------------------------------------
414 void ColorLayerImageViewPanel::onChangeOpacity(wxScrollEvent& event)
415 {
416         int opacity = _opacity->GetValue();
417         _colorLayerImageViewManager->onThresholdChangeOpacity(opacity);
418         RefreshView();
419 }
420
421
422 //----------------------------------------------------------------------------
423 bool ColorLayerImageViewPanel::IsVisible()
424 {
425         return _thresholdGo;
426 }
427
428 //----------------------------------------------------------------------------
429 void ColorLayerImageViewPanel::SetActive(bool active)
430 {
431       _active=active;
432       _cb_ShowHide->SetValue( _active );
433 }
434
435
436 // EOF
437