]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/include/wxSegmentationFM3DWidget.cxx
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / include / wxSegmentationFM3DWidget.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
28   Program:   wxMaracas
29   Module:    $RCSfile: wxSegmentationFM3DWidget.cxx,v $
30   Language:  C++
31   Date:      $Date: 2012/11/15 14:15:18 $
32   Version:   $Revision: 1.2 $
33
34   Copyright: (c) 2002, 2003
35   License:
36
37      This software is distributed WITHOUT ANY WARRANTY; without even
38      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
39      PURPOSE.  See the above copyright notice for more information.
40
41 =========================================================================*/
42
43
44
45 #include <wx/wx.h>
46 #include <wx/splitter.h>
47 #include <wx/notebook.h>
48 #include <wx/colordlg.h>
49
50 #include "vtkMarchingCubes.h"
51 #include "vtkPolyDataMapper.h"
52 #include "vtkActor.h"
53
54
55 #include "wxSegmentationFM3DWidget.h"
56
57
58 //-------------------------------------------------------------------
59 //-------------------------------------------------------------------
60 //-------------------------------------------------------------------
61
62 BEGIN_EVENT_TABLE( wxSegmentationFM3DWidget, wxPanel )
63         EVT_MENU( 12121, wxSegmentationFM3DWidget::OnRefreshView )
64 //      EVT_MENU( 12122, wxSegmentationFM3DWidget::OnDClickLeft  )
65 END_EVENT_TABLE( );
66
67
68 wxSegmentationFM3DWidget::wxSegmentationFM3DWidget( wxWindow* parent,
69                           marImageData *marimageData ,double voxelSize)
70 : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
71 {
72
73         _voxelSize                                                      = voxelSize;
74         _marimageData                                                   = marimageData;
75
76         _wxvtk3Dbaseview_Clipping3D_C           = NULL;
77         _wxvtkmpr3Dview_C                                       = NULL;
78         _wxvtkclipping3Dview_C                          = NULL;
79
80         wxSplitterWindow* mainSplitter = new wxSplitterWindow(this,-1);
81     wxSplitterWindow    *pnlSplitter    = new wxSplitterWindow( mainSplitter , -1);
82         
83         int ww,hh;
84         _MPRWidget2                                     = new wxMPRWidget2(pnlSplitter,_marimageData,voxelSize);
85         wxPanel *       contour3DView   = Create3DViewContour( pnlSplitter , _MPRWidget2->GetVtkMPRBaseData());
86         this->GetSize(&ww,&hh);
87
88     pnlSplitter -> SplitVertically( _MPRWidget2, contour3DView , 600);
89
90
91
92
93
94         //PANEL DE OPERACIONES
95         //------------------------------------------------------------------
96         
97         wxPanel* panelOperaciones          = new wxPanel(mainSplitter, -1);
98
99         wxFlexGridSizer *fgs = new wxFlexGridSizer(2);
100
101         fgs->Add( new wxStaticText(panelOperaciones, -1, _T("Fast Marching Controls")));
102         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("")));
103         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("")));
104         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("")));
105
106         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("Alpha")));
107         slAlpha = new wxSlider(panelOperaciones, -1, 50, 1, 400, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS);
108         fgs->Add(slAlpha);
109         
110         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("Beta")));
111         slBeta = new wxSlider(panelOperaciones, -1, 128, 0, 800, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS);
112         fgs->Add(slBeta);
113                 
114         btnSegment = new wxButton(panelOperaciones, -1, _T("Segment"));
115         fgs->Add(btnSegment);
116         btnUndo = new wxButton(panelOperaciones, -1, _T("Undo"));
117         fgs->Add(btnUndo);
118         
119         
120
121         
122         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("")));
123         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("")));
124
125
126         
127         fgs->Add( new wxStaticText(panelOperaciones, -1, _T("Mesh Controls")));
128         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("")));
129         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("")));
130         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("")));
131
132         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("Opacity")));
133         slVolumeOpacity = new wxSlider(panelOperaciones, -1,50,1,100, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS);
134         fgs->Add(slVolumeOpacity);
135
136         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("Sigma")));
137         slSigmaLevel = new wxSlider(panelOperaciones, -1,100,1,300, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS);
138         fgs->Add(slSigmaLevel);
139
140         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("Convergence")));
141         slLaplacianConvergence = new wxSlider(panelOperaciones, -1,3000,1,3000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS);
142         fgs->Add(slLaplacianConvergence);
143
144         fgs->Add(new wxStaticText(panelOperaciones, -1, _T("Iterations")));
145         slLaplacianIterations = new wxSlider(panelOperaciones, -1,100,1,2000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS);
146         fgs->Add(slLaplacianIterations);
147         
148         panelOperaciones->SetSizer(fgs);
149
150         btnUndo->Disable();
151         slSigmaLevel->Disable();
152         slVolumeOpacity->Disable();
153         slLaplacianConvergence->Disable();
154         slLaplacianIterations->Disable();
155
156         Connect(slAlpha->GetId(),                               wxEVT_SCROLL_THUMBRELEASE,              (wxObjectEventFunction) &wxSegmentationFM3DWidget::OnChangeAlpha);
157         Connect(slBeta->GetId(),                                wxEVT_SCROLL_THUMBRELEASE,              (wxObjectEventFunction) &wxSegmentationFM3DWidget::OnChangeBeta);
158         Connect(btnSegment->GetId(),                    wxEVT_COMMAND_BUTTON_CLICKED,   (wxObjectEventFunction) &wxSegmentationFM3DWidget::OnBtnSegment);
159         Connect(btnUndo->GetId(),                               wxEVT_COMMAND_BUTTON_CLICKED,   (wxObjectEventFunction) &wxSegmentationFM3DWidget::OnBtnUndo);
160         Connect(slVolumeOpacity->GetId(),               wxEVT_SCROLL_THUMBRELEASE,              (wxObjectEventFunction) &wxSegmentationFM3DWidget::OnChangeOpacity);
161         Connect(slSigmaLevel->GetId(),                  wxEVT_SCROLL_THUMBRELEASE,              (wxObjectEventFunction) &wxSegmentationFM3DWidget::OnChangeSigmaLevel);
162         Connect(slLaplacianConvergence->GetId(),wxEVT_SCROLL_THUMBRELEASE,              (wxObjectEventFunction) &wxSegmentationFM3DWidget::OnChangeLaplacianConvergence);
163         Connect(slLaplacianIterations->GetId(), wxEVT_SCROLL_THUMBRELEASE,              (wxObjectEventFunction) &wxSegmentationFM3DWidget::OnChangeLaplacianIteration);
164         //-------------------------------------------------------------------
165
166         mainSplitter->SplitVertically(pnlSplitter, panelOperaciones, 600);
167
168         wxBoxSizer      *sizer  = new wxBoxSizer(wxVERTICAL  );
169         sizer->Add(mainSplitter,1, wxGROW,0);
170         pnlSplitter     -> SetMinimumPaneSize( 50 );
171         this            -> SetSizer(sizer);
172
173
174         // MODELO
175         //------------------------------------------------------------------
176         alphaValue = 0.5;
177         betaValue = 128;
178         opacityValue = 0.5;
179         convergenceValue = 1;
180         iterationsValue = 100;
181         filtroSegmentacion = new itkFM3D();
182         //------------------------------------------------------------------
183
184
185         // RENDERIZACION VTK
186         //------------------------------------------------------------------
187         f_gauss = vtkImageGaussianSmooth::New();
188         f_laplace = vtkSmoothPolyDataFilter::New();
189         f_cubes = vtkMarchingCubes::New();
190         f_mapper = vtkPolyDataMapper::New();
191         f_cubes->SetInput(f_gauss->GetOutput());
192         f_cubes->SetValue(0,128);
193         f_laplace->SetInput(f_cubes->GetOutput());
194         f_mapper->SetInput(f_laplace->GetOutput());
195         f_mapper->ScalarVisibilityOff();
196         f_actor = vtkActor::New();
197         f_actor->SetMapper(f_mapper);
198         f_actor->GetProperty()->SetColor(0.9,0.5,0.5);
199         //------------------------------------------------------------------
200
201         
202 }
203
204 //----------------------------------------------------------------------------
205
206 wxSegmentationFM3DWidget::~wxSegmentationFM3DWidget( )
207 {
208         if (_wxvtk3Dbaseview_Clipping3D_C       != NULL) { delete       _wxvtk3Dbaseview_Clipping3D_C;  }
209         if (_wxvtkmpr3Dview_C                           != NULL) { delete       _wxvtkmpr3Dview_C;                              }
210         if (_wxvtkclipping3Dview_C                      != NULL) { delete       _wxvtkclipping3Dview_C;                 }
211 }
212
213
214 //----------------------------------------------------------------------------
215 wxPanel* wxSegmentationFM3DWidget::Create3DViewContour( wxWindow *parent, vtkMPRBaseData *vtkmprbasedata)
216 {
217         wxWindow *wxwindow;
218         wxPanel *panel=new wxPanel(parent,-1);
219
220         wxSplitterWindow        *panelClipping3D        = new wxSplitterWindow( panel , -1);
221         _wxvtk3Dbaseview_Clipping3D_C = new wxVtk3DBaseView( panelClipping3D );
222
223         _wxvtkclipping3Dview_C = new wxVtkClipping3DView(_wxvtk3Dbaseview_Clipping3D_C);
224         vtkClipping3DDataViewer *vtkclipping3Ddataviewer = new vtkClipping3DDataViewer(); 
225         vtkclipping3Ddataviewer->SetVtkMPRBaseData(vtkmprbasedata);
226         vtkclipping3Ddataviewer->Configure();
227         _wxvtkclipping3Dview_C->SetVtkClipping3DDataViewer(vtkclipping3Ddataviewer);
228
229         _wxvtkmpr3Dview_C = new wxVtkMPR3DView( _wxvtk3Dbaseview_Clipping3D_C );
230         vtkMPR3DDataViewer *vtkmpr3Ddataviewer = new vtkMPR3DDataViewer(); 
231         vtkmpr3Ddataviewer->SetVtkMPRBaseData(vtkmprbasedata);
232         vtkmpr3Ddataviewer->Configure();
233         _wxvtkmpr3Dview_C->SetVtkMPR3DDataViewer(vtkmpr3Ddataviewer);
234
235         wxWindow        *window3D                               = _wxvtk3Dbaseview_Clipping3D_C->GetWxVTKRenderWindowInteractor();
236
237         wxPanel         *panelControl                   = new wxPanel(panelClipping3D,-1);      
238         wxPanel         *controlPanelMPR3D              = _wxvtkmpr3Dview_C->CreateControlPanel(panelControl);
239         wxPanel         *controlPanelClipping3D = _wxvtkclipping3Dview_C->CreateControlPanel(panelControl);
240
241 //      wxBoxSizer  *sizerCtrol             = new wxBoxSizer(wxVERTICAL);
242         wxFlexGridSizer  *sizerCtrol        = new wxFlexGridSizer(1);
243
244         sizerCtrol->Add(controlPanelMPR3D , 1, wxALL|wxEXPAND, 2);
245         sizerCtrol->Add(controlPanelClipping3D , 1, wxALL|wxEXPAND, 2);
246         panelControl->SetAutoLayout(true);
247         panelControl->SetSizer(sizerCtrol);
248         panelControl->SetSize(400,350);
249         panelControl->Layout();
250
251         int ww,hh;
252         wxWindow *pp=this;
253         while (pp->GetParent()!=NULL) pp=pp->GetParent();
254         pp->GetSize(&ww,&hh);
255
256 //EEDxx2.4
257 //              panelClipping3D -> SetMinimumPaneSize( -50 );
258         panelClipping3D -> SplitHorizontally( panelControl,window3D, (int)(hh*0.20) );
259         wxwindow=panelClipping3D;
260
261     wxBoxSizer *sizerH1 = new wxBoxSizer(wxHORIZONTAL);
262         sizerH1->Add(wxwindow , 1, wxALL|wxEXPAND, 0);
263         panel->SetAutoLayout(true);
264         panel->SetSizer(sizerH1);
265         panel->SetSize(400,400);
266         panel->Layout();
267
268 //EEDxx2.4
269 //      panel->FitInside();
270 //      FitInside();
271
272         return panel;
273 }
274
275
276 //----------------------------------------------------------------------------
277 void wxSegmentationFM3DWidget::ConfigureVTK(){
278         _MPRWidget2->ConfigureVTK();
279         _wxvtk3Dbaseview_Clipping3D_C   ->  Configure();
280         _wxvtkmpr3Dview_C                               ->      Configure();
281         _wxvtkclipping3Dview_C                  ->      Configure();
282
283         vtkInteractorStyle3DView *vtkinteractorstyle3Dview      = new vtkInteractorStyle3DView();
284         vtkinteractorstyle3Dview->SetWxVtkMPR3DView(_wxvtkmpr3Dview_C);
285         vtkinteractorstyle3Dview->SetWxVtkClipping3DView(_wxvtkclipping3Dview_C);
286         _wxvtk3Dbaseview_Clipping3D_C->GetInteractorStyleBaseView()->AddInteractorStyleMaracas( vtkinteractorstyle3Dview );
287
288 }
289
290 //----------------------------------------------------------------------------
291 vtkMPRBaseData  *wxSegmentationFM3DWidget::GetVtkMPRBaseData(){
292         return _MPRWidget2->GetVtkMPRBaseData();
293 }
294 //----------------------------------------------------------------------------
295 vtkPlane2DView *wxSegmentationFM3DWidget::GetVtkPlane2DView()
296 {
297         return _MPRWidget2->GetVtkPlane2DView();
298 }
299 //----------------------------------------------------------------------------
300 void wxSegmentationFM3DWidget::OnRefreshView(wxCommandEvent & event)
301 {
302         RefreshView();
303 }
304 //----------------------------------------------------------------------------
305 void wxSegmentationFM3DWidget::RefreshView()
306 {
307         this->_MPRWidget2->RefreshView();
308         _wxvtkmpr3Dview_C                               -> RefreshView();
309         _wxvtkclipping3Dview_C                  -> Refresh();
310         _wxvtk3Dbaseview_Clipping3D_C   -> Refresh();
311 }
312
313
314
315
316
317 /**
318 * This method is invoked when the segmentation button is pressed
319
320 */
321 void wxSegmentationFM3DWidget::OnBtnSegment(wxCommandEvent& event){
322
323         wxBusyCursor wait;
324         
325
326         double x =_MPRWidget2->GetVtkMPRBaseData()->GetX();
327         double y =_MPRWidget2->GetVtkMPRBaseData()->GetY();
328         double z = _MPRWidget2->GetVtkMPRBaseData()->GetZ();
329
330         filtroSegmentacion->AddSeed(x,y,z);
331         filtroSegmentacion->SetAlpha(this->alphaValue);
332         filtroSegmentacion->SetBeta(this->betaValue);
333
334         vtkImageData* resultado = filtroSegmentacion->segment(_MPRWidget2->GetVtkMPRBaseData()->GetImageData() );
335
336                 
337         f_gauss->SetInput(resultado);
338         f_gauss->SetStandardDeviation(this->sigmaValue);
339         
340     f_laplace->SetNumberOfIterations(this->iterationsValue);
341         f_laplace->SetConvergence(this->convergenceValue);
342         f_laplace->Update();
343
344         f_actor->GetProperty()->SetOpacity(this->opacityValue);
345         
346         vtkRenderer *ren = _wxvtkmpr3Dview_C->GetWxvtk3Dbaseview()->GetRenderer();
347         
348         ren->AddActor(f_actor);
349         RefreshView();
350         
351         btnUndo->Enable();
352         slVolumeOpacity->Enable();
353         slSigmaLevel->Enable();
354         slLaplacianConvergence->Enable();
355         slLaplacianIterations->Enable();
356 }
357
358
359 /**
360 * This method is invoked when the slAlpha scroll change its value
361 *
362 */
363 void wxSegmentationFM3DWidget::OnChangeAlpha(wxScrollEvent& event){
364         alphaValue = ((double)slAlpha->GetValue());
365         alphaValue = alphaValue / 100;
366 }
367
368 /**
369 * This method is invoked when the slBeta scroll change its value
370 *
371 */
372 void wxSegmentationFM3DWidget::OnChangeBeta(wxScrollEvent& event){
373         betaValue = ((double)slBeta->GetValue());
374 }
375
376 /**
377 *       Carga la imagen resultado de la segmentacion FM ITK
378 *
379 */
380 void wxSegmentationFM3DWidget::OnBtnUndo(wxCommandEvent& event){
381
382         if (f_actor != NULL){
383                 vtkRenderer *ren = _wxvtkmpr3Dview_C->GetWxvtk3Dbaseview()->GetRenderer();
384                 ren->RemoveActor(f_actor);
385                 RefreshView();
386                 btnUndo->Disable();
387                 slSigmaLevel->Disable();
388                 slVolumeOpacity->Disable();
389                 slLaplacianConvergence->Disable();
390                 slLaplacianIterations->Disable();
391         }
392
393
394 }
395
396 /**
397 * Este metodo cambia la opacidad del actor que representa la segmentacion realizada
398 *
399 */
400 void wxSegmentationFM3DWidget::OnChangeOpacity(wxScrollEvent& event){
401         if (f_actor != NULL){
402                 opacityValue = ((double)slVolumeOpacity->GetValue())/100.0;
403                 f_actor->GetProperty()->SetOpacity(opacityValue);
404                 RefreshView();
405         }
406 }
407
408 /**
409 * Cambia el valor de la desviacion estandar del filtro gaussiano aplicado al 
410 * volumen resultado de la segmentacion
411 */
412 void wxSegmentationFM3DWidget::OnChangeSigmaLevel(wxScrollEvent& event){
413         if(f_actor != NULL){
414                 this->sigmaValue = ((double)slSigmaLevel->GetValue())/300.0;
415                 f_gauss->SetStandardDeviation(this->sigmaValue);
416                 f_laplace->Update();
417                 RefreshView();
418         }
419 }
420
421 /**
422 * Cambia el valor de la convergencia del filtro de suavizado laplaciano aplicado
423 * a la superficie que representa el volumen resultado de la segmentacion
424 */
425 void wxSegmentationFM3DWidget::OnChangeLaplacianConvergence(wxScrollEvent& event){
426         if(f_actor != NULL){
427                 this->convergenceValue = ((double)slLaplacianConvergence->GetValue())/3000.0;
428                 f_laplace->SetConvergence(this->convergenceValue);
429                 f_laplace->Update();
430                 RefreshView();
431         }
432 }
433
434 /**
435 * Cambia el numero de iteraciones del filtro de suavizado laplaciano aplicado a la superficie
436 * que representa el volumen resultado de la segmentacion
437 */
438 void wxSegmentationFM3DWidget::OnChangeLaplacianIteration(wxScrollEvent& event){
439         if(f_actor != NULL){
440                 this->iterationsValue = slLaplacianIterations->GetValue();
441                 f_laplace->SetNumberOfIterations(this->iterationsValue);
442                 f_laplace->Update();
443                 RefreshView();
444         }
445 }
446
447 // EOF - wxSegmentationFM3DWidget.cxx
448
449
450
451