]> Creatis software - creaContours.git/blob - lib/Interface_ManagerContour_NDimensions/wxContourEventHandler.cxx__
Feature #1772 Add licence terms for all files.
[creaContours.git] / lib / Interface_ManagerContour_NDimensions / wxContourEventHandler.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 /*  Diagrama de Secuaencia de cambiar un instante
29 wxContourEventHandler :: changeInstant()
30         OutlineModelManager :: ->setInstant( _actualInstant );
31                 OutlineModelManager :: updateToActualInstant()
32                   OutlineModelManager :: getOutlinesAtInstant(Instant * anInstant, bool ifAnnotate)
33                     OutlineModelManager :: annotateOutlinesWrap(std::vector<std::string> kNamesVector, std::vector<ContourThing **> theOutlinesVector)
34         wxContourEventHandler :: updateInstantOutlines()
35                 OutlineModelManager :: getActualInstantOutlines ()
36                 wxVtkBaseView_SceneManager :: addToScene( std::vector< std::string> theKeyNameVector, bool append, bool visualization, bool control, bool ifActive, bool ifShowCtrlPoints )
37                         wxVtkBaseView_SceneManager :: addToScene( std::string theKeyName, bool append, bool visualization, bool control, bool ifActive, bool ifShowCtrlPoints )
38                                 wxVtkBaseView_SceneManager :: getContourWrap_ViewControlOf
39                                         MAP  find
40                            void wxVtkBaseView_SceneManager :: addToScene( std::string theKeyName, ContourWrap_ViewControl * contourWRP, bool append, bool visualization, bool control, bool ifActive, bool ifShowCtrlPoints )
41
42 */
43
44
45 /*!
46  \mainpage NDimension with wxWidgets and maracas
47
48  \section intro Introduction
49
50  This is the introduction. 
51
52
53
54   \dot
55
56
57 digraph example_0 {
58         graph [ ratio=fill];
59     node [shape=record, fontname=Helvetica, fontsize=10];
60
61         _wxContourGUIExample                    [ label="wxContourGUIExample"                   URL="\ref wxContourGUIExample"];
62         _wxContourEventHandler                  [ label="wxContourEventHandler"                 URL="\ref wxContourEventHandler"];
63
64         _OutlineModelManager                    [ label="OutlineModelManager"                   URL="\ref OutlineModelManager"];
65         _wxInstantChooserPanel                  [ label="wxInstantChooserPanel"                 URL="\ref wxInstantChooserPanel"];
66         _wxContourViewPanel                             [ label="wxContourViewPanel"                    URL="\ref wxContourViewPanel"];
67         _wxContour_ButtonsBar                   [ label="wxContour_ButtonsBar"                  URL="\ref wxContour_ButtonsBar"];       
68         _OutlineModelManager                    [ label="OutlineModelManager"                   URL="\ref OutlineModelManager"];
69         _wxVtkBaseView_SceneManager             [ label="wxVtkBaseView_SceneManager"    URL="\ref wxVtkBaseView_SceneManager"];
70         _SomeEnvironment                                [ label="SomeEnvironment"                               URL="\ref SomeEnvironment"];
71                 
72
73         _wxContourGUIExample                    ->      _wxContourEventHandler                  [ arrowhead="open", style="dashed" ];
74         _wxContourGUIExample                    ->      _OutlineModelManager                    [ arrowhead="open", style="dashed" ];
75         _wxContourGUIExample                    ->      _wxInstantChooserPanel                  [ arrowhead="open", style="dashed" ];
76         _wxContourGUIExample                    ->      _wxContourViewPanel                             [ arrowhead="open", style="dashed" ];
77         _wxContourGUIExample                    ->      _wxContour_ButtonsBar                   [ arrowhead="open", style="dashed" ];
78         _wxContourViewPanel                             ->      wxMaracas_N_ViewersWidget               [ arrowhead="open", style="dashed" ];
79         _wxContourEventHandler                  ->      _OutlineModelManager                    [ arrowhead="open", style="dashed" ];
80         _wxContourEventHandler                  ->      _wxVtkBaseView_SceneManager             [ arrowhead="open", style="dashed" ];
81         _OutlineModelManager                    ->      _SomeEnvironment                                [ arrowhead="open", style="dashed" ];
82         
83 }
84
85 \enddot
86
87
88
89 */
90
91
92
93 //----------------------------------------------------------------------------------------------------------------
94 // Class definition include
95 //----------------------------------------------------------------------------------------------------------------
96 #include "wxContourEventHandler.h"
97 #include <wx/filedlg.h>
98 #include <wx/spinctrl.h>
99 #include <iostream>
100 #include <fstream>
101
102 //----------------------------------------------------------------------------------------------------------------
103 // Class implementation
104 //----------------------------------------------------------------------------------------------------------------
105 /** @file wxContourEventHandler.cxx */
106
107
108 //------------------------------------------------------------------------------------------------------------
109 // Includes
110 //------------------------------------------------------------------------------------------------------------
111
112 #include "wxContour_ActionCommandsID.h"
113 #include "NameWrapper.h"
114 #include "ConceptDataWrap.h"
115 #include "OutlineGroup.h"
116
117 #include <manualContour.h>
118 #include "Contour/ContourExtractData.h"
119 #include "ImageSourceThing.h"
120 #include "PanelBullEyeOptions.h"
121
122
123 #include <vtkContourFilter.h>
124 #include <vtkCleanPolyData.h>
125 #include <vtkPolyDataConnectivityFilter.h>         
126 #include <vtkStripper.h>
127 #include <vtkImageReslice.h>
128 #include <vtkMetaImageWriter.h>
129 #include <vtkImageMapToWindowLevelColors.h>
130
131
132
133 //------------------------------------------------------------------------------------------------------------
134 //------------------------------------------------------------------------------------------------------------
135 //------------------------------------------------------------------------------------------------------------
136 /*
137 PanelBullEyeOptions::PanelBullEyeOptions (wxWindow * parent, wxSize size) :
138                  wxPanel(parent, -1, wxDefaultPosition, size)
139 {
140
141         _maxLevels              = 5;
142         _maxSections    = 10;
143         wxSize sizepanel(190,300);
144         wxPanel *panel = this;
145
146
147         wxString lstOptions[2];
148         lstOptions[0]= wxString("General options",wxConvUTF8);
149         lstOptions[1]= wxString("Detail options",wxConvUTF8);
150         _radioboxBullEyeGenOpt  = new wxRadioBox(panel, -1, wxString("General/Detail options",wxConvUTF8), wxDefaultPosition, wxSize(200,45), 2 , lstOptions,  2, wxRA_SPECIFY_COLS);
151         _radioboxBullEyeGenOpt->SetSelection(0);
152
153         _spinctrlBullEyeNumOfCrowns = new wxSpinCtrl( panel , -1,_T("B"),wxDefaultPosition, wxSize(40,20) );
154         _spinctrlBullEyeNumOfCrowns->SetRange(1,_maxLevels);
155         _spinctrlBullEyeNumOfCrowns->SetValue(3);
156
157         _spinctrlBullEyeNumOfSec = new wxSpinCtrl( panel , -1,_T("A"),wxDefaultPosition, wxSize(40,20) );
158         _spinctrlBullEyeNumOfSec->SetRange(1,_maxSections);
159         _spinctrlBullEyeNumOfSec->SetValue(5);
160
161
162         _sliderBullEyeAngle = new wxSlider( panel , -1 ,0,0,180, wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
163
164         wxFlexGridSizer * sizerGeneralOptions                           = new wxFlexGridSizer(10);
165
166         wxFlexGridSizer * sizerGeneralOptionsA                          = new wxFlexGridSizer(1);
167         wxFlexGridSizer * sizerGeneralOptionsB                          = new wxFlexGridSizer(1);
168         wxFlexGridSizer * sizerGeneralOptionsC                          = new wxFlexGridSizer(1);
169         sizerGeneralOptionsA -> Add( new wxStaticText(panel,-1,_T("Crowns")) , 1, wxGROW );
170         sizerGeneralOptionsA -> Add( _spinctrlBullEyeNumOfCrowns , 1/*, wxGROW );
171         sizerGeneralOptionsB -> Add( new wxStaticText(panel,-1,_T("Sections")) , 1, wxGROW );
172         sizerGeneralOptionsB -> Add( _spinctrlBullEyeNumOfSec , 1/*, wxGROW );
173         sizerGeneralOptionsC -> Add( new wxStaticText(panel,-1,_T("      Angle")) , 1, wxGROW );
174         sizerGeneralOptionsC -> Add( _sliderBullEyeAngle , 1/*, wxGROW );
175
176         sizerGeneralOptions -> Add( sizerGeneralOptionsA  , 1/*, wxGROW );
177         sizerGeneralOptions -> Add( new wxStaticText(panel,-1,_T("   ")) , 1, wxGROW );
178         sizerGeneralOptions -> Add( sizerGeneralOptionsB  , 1/*, wxGROW );
179         sizerGeneralOptions -> Add( new wxStaticText(panel,-1,_T("   ")) , 1, wxGROW );
180         sizerGeneralOptions -> Add( sizerGeneralOptionsC  , 1/*, wxGROW );
181
182
183         wxSpinCtrl      *tmpSpinSection;
184         wxSlider        *tmpSliderRadio;
185         wxSlider        *tmpSliderAng;
186
187         wxFlexGridSizer * sizerDetailOptions                            = new wxFlexGridSizer(6);
188
189         sizerDetailOptions -> Add( new wxStaticText(panel,-1,_T("Crown ")) , 1, wxGROW );
190         sizerDetailOptions -> Add( new wxStaticText(panel,-1,_T("  Sections  ")) , 1, wxGROW );
191         sizerDetailOptions -> Add( new wxStaticText(panel,-1,_T("  ")) , 1, wxGROW );
192         sizerDetailOptions -> Add( new wxStaticText(panel,-1,_T("      Radio %")) , 1, wxGROW );
193         sizerDetailOptions -> Add( new wxStaticText(panel,-1,_T("  ")) , 1, wxGROW );
194         sizerDetailOptions -> Add( new wxStaticText(panel,-1,_T("      Angle")) , 1, wxGROW );
195
196         wxString tmpwxstring;
197         int i;
198         for (i=0;i<_maxLevels;i++){
199                 tmpSpinSection = new wxSpinCtrl( panel  , -1,_T(""),wxDefaultPosition, wxSize(40,20) );
200                 tmpSliderRadio = new wxSlider( panel    , -1 ,0,0,100, wxDefaultPosition, wxSize(110,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
201                 tmpSliderAng   = new wxSlider( panel    , -1 ,0,0,180, wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
202
203                 tmpSpinSection ->SetRange(1,_maxSections);
204
205                 tmpwxstring.Printf(_T("  %d.  "),i+1);
206                 sizerDetailOptions -> Add( new wxStaticText(panel,-1,tmpwxstring) , 1, wxGROW );
207                 sizerDetailOptions -> Add( tmpSpinSection, 1 );
208                 sizerDetailOptions -> Add( new wxStaticText(panel,-1,_T("  ")) , 1, wxGROW );
209                 sizerDetailOptions -> Add( tmpSliderRadio, 1, wxGROW );
210                 sizerDetailOptions -> Add( new wxStaticText(panel,-1,_T("  ")) , 1, wxGROW );
211                 sizerDetailOptions -> Add( tmpSliderAng, 1, wxGROW );
212
213                 tmpSpinSection  -> SetValue(5);
214                 tmpSliderRadio  -> SetValue( 100-(double)((i)*100.0/_maxLevels) );
215                 tmpSliderAng    -> SetValue(0);
216                 
217                 Connect( tmpSliderRadio->GetId(),       wxEVT_SCROLL_THUMBRELEASE  , (wxObjectEventFunction) &PanelBullEyeOptions::onRefreshPanel );
218                 Connect( tmpSliderRadio->GetId(),       wxEVT_SCROLL_CHANGED  , (wxObjectEventFunction) &PanelBullEyeOptions::onRefreshPanel );
219
220                 _lstBullEyeDetailNumOfSec.push_back( tmpSpinSection );
221                 _lstBullEyeDetailRadio.push_back( tmpSliderRadio );
222                 _lstBullEyeDetailAngle.push_back( tmpSliderAng );
223
224         } //for
225
226         wxFlexGridSizer * sizer                         = new wxFlexGridSizer(1);
227         sizer -> Add( new wxStaticText(panel,-1,_T("")) , 1, wxGROW );
228         sizer -> Add( new wxStaticText(panel,-1,_T("")) , 1, wxGROW );
229         sizer -> Add( _radioboxBullEyeGenOpt , 1, wxGROW );
230         sizer -> Add( new wxStaticText(panel,-1,_T("")) , 1, wxGROW );
231         sizer -> Add( new wxStaticText(panel,-1,_T("      -- Bull Eye general options --")) , 1, wxGROW );
232         sizer -> Add( new wxStaticText(panel,-1,_T("")) , 1, wxGROW );
233         sizer -> Add(  sizerGeneralOptions , 1, wxGROW );
234         sizer -> Add( new wxStaticText(panel,-1,_T("")) , 1, wxGROW );
235         sizer -> Add( new wxStaticText(panel,-1,_T("")) , 1, wxGROW );
236         sizer -> Add( new wxStaticText(panel,-1,_T("      -- Bull Eye detail options --")) , 1, wxGROW );
237         sizer -> Add( new wxStaticText(panel,-1,_T("")) , 1, wxGROW );
238         sizer -> Add(  sizerDetailOptions  , 1, wxGROW );
239
240         Connect( _radioboxBullEyeGenOpt->GetId(),       wxEVT_COMMAND_RADIOBOX_SELECTED , (wxObjectEventFunction) &PanelBullEyeOptions::onRefreshPanel ); 
241         Connect( _spinctrlBullEyeNumOfCrowns->GetId(),  wxEVT_COMMAND_TEXT_UPDATED              , (wxObjectEventFunction) &PanelBullEyeOptions::onRefreshPanel );
242         
243         panel->SetSizer( sizer );
244         panel->SetSize( sizepanel );
245         panel->SetAutoLayout( true );
246         panel->Layout();
247
248         RefreshPanel();
249
250 }
251
252 //------------------------------------------------------------------------------------------------------------
253 PanelBullEyeOptions::~PanelBullEyeOptions() 
254 {
255 }
256
257 //------------------------------------------------------------------------------------------------------------
258 void PanelBullEyeOptions::onRefreshPanel( wxCommandEvent& event )
259 {
260         RefreshPanel();
261 }
262
263 //------------------------------------------------------------------------------------------------------------
264 void PanelBullEyeOptions::RefreshPanel()
265 {
266         int i;
267         bool ok;
268         if (this->_radioboxBullEyeGenOpt->GetSelection()==0){
269                 for (i=0; i<_maxLevels; i++)
270                 {
271                         _lstBullEyeDetailNumOfSec[i]    -> Enable(false);
272                         _lstBullEyeDetailRadio[i]               -> Enable(false);
273                         _lstBullEyeDetailAngle[i]               -> Enable(false);
274                 } // for
275         } else {
276                 int spinSelec = _spinctrlBullEyeNumOfCrowns->GetValue(); 
277                 for (i=0; i<_maxLevels; i++)
278                 {
279                         ok = (i < spinSelec);
280                         _lstBullEyeDetailNumOfSec[i]    -> Enable(ok);
281                         _lstBullEyeDetailRadio[i]               -> Enable(ok);
282                         _lstBullEyeDetailAngle[i]               -> Enable(ok);
283
284                         if (i>0){
285                                 if (_lstBullEyeDetailRadio[i]->GetValue() > _lstBullEyeDetailRadio[i-1]->GetValue())
286                                 {
287                                         _lstBullEyeDetailRadio[i]->SetValue( _lstBullEyeDetailRadio[i-1]->GetValue() );
288                                 } 
289                         }// if i>0
290                 } // for
291         } // if
292 }
293
294 //------------------------------------------------------------------------------------------------------------
295 int PanelBullEyeOptions::GetNumberOfCrowns()
296 {
297         return this->_spinctrlBullEyeNumOfCrowns->GetValue();
298 }
299
300 //------------------------------------------------------------------------------------------------------------
301 int PanelBullEyeOptions::GetNumberOfSections(int nCrown)
302 {
303         int result;
304         if (this->_radioboxBullEyeGenOpt->GetSelection()==0)
305         { 
306                 result = this->_spinctrlBullEyeNumOfSec->GetValue();
307         } else {
308                 result = this->_lstBullEyeDetailNumOfSec[nCrown]->GetValue();
309         }
310         return result;
311 }
312
313 //------------------------------------------------------------------------------------------------------------
314 int PanelBullEyeOptions::GetRadioOfCrown(int nCrown)
315 {
316         int result;
317         double sizeCrowns;
318         if (this->_radioboxBullEyeGenOpt->GetSelection()==0)
319         { 
320                 sizeCrowns = (double)( this->_spinctrlBullEyeNumOfCrowns->GetValue() );
321                 result = 100.0 * (nCrown+1)/sizeCrowns ;
322         } else {
323                 result = this->_lstBullEyeDetailRadio[nCrown]->GetValue();
324         }
325         return result;
326 }
327
328 //------------------------------------------------------------------------------------------------------------
329 double PanelBullEyeOptions::GetAngOfCrownSection(int nCrown,int section)
330 {
331         double angle;
332         if (this->_radioboxBullEyeGenOpt->GetSelection()==0)
333         { 
334                 angle = this->_sliderBullEyeAngle->GetValue();
335         } else {
336                 angle = this->_lstBullEyeDetailAngle[nCrown]->GetValue();
337         }
338         double numOfSec = (double)GetNumberOfSections(nCrown);
339         double deltaSec = GetAngDeltaOfCrownSection(nCrown);
340         return angle + section*deltaSec ;
341 }
342
343 //------------------------------------------------------------------------------------------------------------
344 double PanelBullEyeOptions::GetAngDeltaOfCrownSection(int nCrown)
345 {
346         double numOfSec = (double)GetNumberOfSections(nCrown);
347         return 360.0/numOfSec;
348 }
349
350 */
351
352 //------------------------------------------------------------------------------------------------------------
353 //------------------------------------------------------------------------------------------------------------
354 //------------------------------------------------------------------------------------------------------------
355
356
357         //------------------------------------------------------------------------------------------------------------
358         // Constructors & Destructors
359         //------------------------------------------------------------------------------------------------------------
360
361         wxContourEventHandler ::wxContourEventHandler()
362                 :wxEvtHandler()
363         {
364         _creatingContoursActive                 = false;
365                 _theViewPanel                                   = NULL;
366                 _modelManager                                   = NULL;
367                 _instantPanel                                   = NULL;
368                 _buttonsBar                                             = NULL;
369                 _gridPanel                                              = NULL; 
370                 _drawToolsPanel                                 = NULL;
371                 _operationsToolsPanel                   = NULL;
372                 _autoFormsPanel                                 = NULL; 
373                 _standardToolsPanel                             = NULL;
374                 _editionToolsPanel                              = NULL;
375                 _listViewPanel                                  = NULL;
376                 _sceneManager                                   = NULL;
377                 _actualInstant                                  = NULL;
378                 _performingOperation                    = new PerformingOperation();
379
380                 // EED Interface and Spread
381                 _spreadFrame                                    = NULL;
382                 _segmentationFrame                              = NULL;
383                 _contourPropagation                             = NULL;
384                 _createContourFrame                             = NULL;
385                 _deleteFrame                                    = NULL;
386                 _InformationContourFrame                = NULL;
387                 _interfaceConfigurationFrame    = NULL;
388                 _TestFrame                                              = NULL;
389                 _wxtextctrlTest                                 = NULL;
390                 _panelBullEyeOptions                    = NULL;
391
392                 fileNameContourROI                              = "";
393                 _numberOfVariablesStatistics=6+1;
394
395                 inundo = 0;
396                 inredo = 0;
397                 stundoredo = "data/temp";
398 #if(WIN32)
399                 mkdir(stundoredo.c_str());
400 #else
401                 mkdir(stundoredo.c_str(),755);
402 #endif
403                 stundoredo += "/cont";
404         }
405         //------------------------------------------------------------------------------------------------------------
406         wxContourEventHandler :: ~ wxContourEventHandler()
407         {
408                 delete _actualInstant;
409                 /*delete _theViewPanel;         
410                 delete _instantPanel;
411                 delete _buttonsBar;
412                 delete _actualInstant;  
413                 */       
414                 delete _sceneManager;
415                 delete _performingOperation;
416
417                 
418         }
419         //------------------------------------------------------------------------------------------------------------
420         // Configuration methods
421         //------------------------------------------------------------------------------------------------------------
422         
423         bool wxContourEventHandler :: configureEventsHandling()
424         {
425                 bool configured = true;
426
427                 return configured;
428         }
429
430         //------------------------------------------------------------------------------------------------------------
431         // Methods for capturing events 
432         //------------------------------------------------------------------------------------------------------------
433         void wxContourEventHandler :: onProcessMessage( wxCommandEvent& event )
434         {
435                 
436         }
437         //------------------------------------------------------------------------------------------------------------
438
439         void wxContourEventHandler :: onActionButtonPressed( wxCommandEvent& event )
440         {
441                 const char buttonTool = *(char *)event.GetClientData();
442                 executeActionCommand( buttonTool );             
443         }
444         //------------------------------------------------------------------------------------------------------------          
445         void wxContourEventHandler :: onChangedDeep ( wxCommandEvent& event )
446         {
447                 double val = _theViewPanel->getCurrentDeep();
448                 _instantPanel->setConceptValue( "Axe Depth", (int)val );
449                 _theViewPanel->setActualVertical( (int)val );
450                 changeInstant();
451         }
452
453         //------------------------------------------------------------------------------------------------------------
454         void wxContourEventHandler :: onChangeInstant( wxCommandEvent& event )
455         {               
456                 ConceptDataWrap * dataConcept = NULL; 
457                 if ( _theViewPanel->GetId() == event.GetId() )
458                 {
459                         dataConcept = (ConceptDataWrap *)event.GetClientData();
460                         int value = dataConcept->getActualValue();
461                         _instantPanel->setConceptValue( dataConcept->getName(), value );
462                 }
463                 else
464                 {
465                         dataConcept = _instantPanel->getLastConceptData();
466                         if( dataConcept->getName().compare( _theViewPanel->getVerticalConceptName() ) == 0)
467                         {
468                                 _theViewPanel->setActualVertical( dataConcept->getActualValue());
469                                 //_theViewPanel->setVerticalConcept( dataConcept->getName(), dataConcept->getMinShowedValue()/*dataConcept->getMinValue()*/, /*dataConcept->getMaxValue()*/ dataConcept->getMaxShowedValue(), dataConcept->getMinShowedValue(), dataConcept->getMaxShowedValue(), dataConcept->getActualValue());
470                         }
471                         else if( dataConcept->getName().compare( _theViewPanel->getHorizontalConceptName() ) == 0|| _instantPanel->getIfConceptCheckedAt( dataConcept->getName(), 0 ) )
472                         {
473                                 _theViewPanel->setHorizontalConcept( dataConcept->getName(), dataConcept->getMinShowedValue()/*dataConcept->getMinValue()*/, /*dataConcept->getMaxValue()*/ dataConcept->getMaxShowedValue(), dataConcept->getMinShowedValue(), dataConcept->getMaxShowedValue(), dataConcept->getActualValue());
474                         }
475                 }       
476                 changeInstant();
477         }
478         //------------------------------------------------------------------------------------------------------------
479                 
480         void wxContourEventHandler :: onCreateMultipleROI( wxCommandEvent& event )
481         {
482                 _creatingContoursActive = !_creatingContoursActive;
483                 if( _creatingContoursActive )
484                 {                       
485                         createContour();
486                 }
487         }
488         //------------------------------------------------------------------------------------------------------------
489         void wxContourEventHandler :: onCreateROI( wxCommandEvent& event )
490         {
491                 if( _creatingContoursActive )
492                 {                       
493                         createContour();
494                 }
495         }
496
497         //------------------------------------------------------------------------------------------------------------
498         void wxContourEventHandler :: onStopCreateROI( wxCommandEvent& event )
499         {
500                 
501         }
502
503         //------------------------------------------------------------------------------------------------------------
504         void wxContourEventHandler :: onDeleterROI( wxCommandEvent& event )
505         {
506
507         }
508         //------------------------------------------------------------------------------------------------------------
509         void wxContourEventHandler :: onSelectROI( wxCommandEvent& event )
510         {
511                 
512         }
513         //------------------------------------------------------------------------------------------------------------
514
515
516         void wxContourEventHandler :: onUnSelectROI( wxCommandEvent& event )
517         {
518
519         }
520         //------------------------------------------------------------------------------------------------------------
521
522         void wxContourEventHandler :: onManualHidedROI( wxCommandEvent& event )
523         {
524
525         }
526         //------------------------------------------------------------------------------------------------------------
527
528         void wxContourEventHandler :: onManualShowedROI( wxCommandEvent& event )
529         {
530
531         }
532         //------------------------------------------------------------------------------------------------------------
533
534         void wxContourEventHandler :: onEditingROI( wxCommandEvent& event )
535         {
536
537         }
538         //------------------------------------------------------------------------------------------------------------
539
540         void wxContourEventHandler :: onMovingROI( wxCommandEvent& event )
541         {
542
543         }
544
545         //------------------------------------------------------------------------------------------------------------
546         //  Model management methods
547         //------------------------------------------------------------------------------------------------------------
548
549         void wxContourEventHandler::onSpreadReset( wxCommandEvent& event )
550         {
551                 _wxtextctrlSpread->SetValue(wxString("",wxConvUTF8));
552                 _contourPropagation->resetAppend();
553         }
554
555         //------------------------------------------------------------------------------------------------------------
556         void wxContourEventHandler::onSpreadAdd( wxCommandEvent& event )
557         {
558                 std::vector<double> vecX; 
559                 std::vector<double> vecY; 
560                 std::vector<double> vecZ; 
561                 _theViewPanel->getSceneManager()->GetPointsOfActualContour( &vecX , &vecY , &vecZ );    
562
563                 if (vecX.size()!=0){
564                         std::vector<int> tempVector;
565                         _instantPanel->getInstant( tempVector );
566                         int i,size=vecZ.size();
567                         int actualSlice = tempVector[1];
568                         for ( i=0 ; i<size ; i++ )
569                         {
570                                 vecZ[i] = actualSlice;
571                         } // for
572
573                         _contourPropagation->appendContour(&vecX , &vecY , &vecZ);
574                         wxString newstring;
575                         //newstring.Printf("%s %d -",_wxtextctrlSpread->GetValue(), actualSlice );
576                         newstring.append(wxString(_wxtextctrlSpread->GetValue(),wxConvUTF8));
577                         newstring.append(wxString(" ",wxConvUTF8));
578                         newstring.append(wxString(intToString(actualSlice).c_str(),wxConvUTF8));
579                         _wxtextctrlSpread->SetValue(newstring);
580                 } // if 
581         }
582
583         
584
585         //------------------------------------------------------------------------------------------------------------
586         void wxContourEventHandler::onSpreadGo( wxCommandEvent& event )
587         {
588
589                 //JCP 20-10-08 Undo redo implementation
590                 saveState();
591                 //JCP 20-10-08 Undo redo implementation
592
593                 wxBusyCursor wait;
594                 int typeMethodFindCtrlPoints=_spreadMethodRadiobox->GetSelection();
595                 int typeofcontour = 1;
596                 manualContourModel *manModelContour;
597                 std::vector<double> vecCtrlPointX;
598                 std::vector<double> vecCtrlPointY;
599                 std::vector<double> vecCtrlPointZ;
600                 double  minZ,maxZ;
601                 int z,j,sizeCtrPt;
602 //              double idTmp;
603                 int idTmp;
604
605                 std::vector<int> tempVector;
606                 _instantPanel->getInstant( tempVector );
607 //              tempVector[1];
608
609                 _contourPropagation->getMaxMinZ(&minZ,&maxZ);
610 //JSTG_16-07-08_----------------------------------------------------------------
611                 //_contourPropagation->setInterpolationNumber(maxZ-minZ+1);
612                 _contourPropagation->setInterpolationNumber(100);
613                 _contourPropagation->CalculeSplinePropagation();
614
615                 double          totalZ = maxZ-minZ+1;
616                 double          porcent;
617                 wxString        tmpString;
618 //--------------------------------------------------------------------
619                 for ( z=(int)minZ ; z<=(int)maxZ ; z++ )
620                 {
621                         porcent = 100.0* (z-minZ)/totalZ;
622                         int numero = (int)(z-minZ+1)/(int)totalZ;
623                         std::string stringtemp = "  "+intToString(porcent)+"%            "+intToString(numero)+"            "+intToString(z);
624                         tmpString.Append(wxString(stringtemp.c_str(),wxConvUTF8));
625
626                         //tmpString.Printf("  %d %c            %d/%d            %d", (int)porcent , 37 , (int)(z-minZ+1), (int)totalZ,z );
627                         _staticTextSpread->SetLabel(tmpString);
628
629                         if (_contourPropagation->ifSliceKeyContourExist(z)==false)
630                         {
631                                 manModelContour = factoryManualContourModel( typeofcontour );
632                                 idTmp = _contourPropagation->FindIdWithZ(z);
633                                 if (typeMethodFindCtrlPoints==0) // Initial Points
634                                 {   
635                                         _contourPropagation->GetInitialControlPoints( idTmp , &vecCtrlPointX,&vecCtrlPointY,&vecCtrlPointZ);
636                                 } 
637                                 if (typeMethodFindCtrlPoints==1)  // Automatique Method
638                                 {
639                                         _contourPropagation->GetControlPoints( idTmp  ,&vecCtrlPointX,&vecCtrlPointY,&vecCtrlPointZ);
640                                 }
641                                 if (typeMethodFindCtrlPoints==2)  // sampling
642                                 {
643                                         _contourPropagation->GetControlPoints( idTmp , 20.0 ,&vecCtrlPointX,&vecCtrlPointY,&vecCtrlPointZ);
644                                 }
645         //--------------------------------------------------------------------
646                                 sizeCtrPt = vecCtrlPointX.size();
647                                 for (j=0 ; j<sizeCtrPt ; j++)
648                                 {
649         //JSTG_16-07-08_----------------------------------------------------------------
650                                         manModelContour->AddPoint( vecCtrlPointX[j] , vecCtrlPointY[j] , -900  );
651         //--------------------------------------------------------------------
652                                 } // for j
653
654                                 tempVector[1]=z;
655                                 std::string theName;
656                                 theName = _modelManager->createOutline( manModelContour, tempVector );
657                                 bool addedModel = theName.compare("") != 0;
658                                 if( addedModel )
659                                 {
660                                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
661                                         this->_theViewPanel->getSpacing(spc);                                   
662                                         //Adding the manualContourControler to interface objects structure
663                                         //Adding the manualViewContour to interface objects structure           
664                                         //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
665                                         _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc,typeofcontour ) ;
666                                 } // if addedModel
667                         }// ifSliceKeyContourExist
668                 } // for z
669                 _staticTextSpread->SetLabel(wxString("   ",wxConvUTF8));
670                 //RefreshInterface();
671
672
673                 
674         }
675
676         //------------------------------------------------------------------------------------------------------------
677         void wxContourEventHandler :: onSpread( )
678         {
679                 if (_spreadFrame==NULL)
680                 {
681                         
682                         _spreadFrame                            = new wxFrame (_theViewPanel, -1,_T(" Spread "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
683                         
684                         wxSize sizepanel(250,400);
685                         _spreadFrame->SetSize( sizepanel );
686                         _spreadFrame->SetEventHandler( this );
687                         wxPanel *panel                          = getSpreadPanel(_spreadFrame);
688                         panel->SetSize(sizepanel);
689                         /**new wxPanel(_spreadFrame,-1);
690
691                         wxButton *spreadResetBtn        = new wxButton(panel,-1,_T("Reset"),wxDefaultPosition, wxSize(80,35) );
692                         wxButton *spreadAddBtn          = new wxButton(panel,-1,_T("Add"),wxDefaultPosition, wxSize(80,35));
693
694                         wxString lstOptions[3];
695                         lstOptions[0]="A";
696                         lstOptions[1]="B";
697                         lstOptions[2]="C";
698                         _spreadMethodRadiobox                                           = new wxRadioBox(panel, -1, "Method (find ctrl. Points)", wxDefaultPosition, wxSize(200,45), 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
699                         _spreadMethodRadiobox->SetSelection(2);
700
701                         wxButton *spreadGoBtn           = new wxButton(panel,-1,_T("Go"),wxDefaultPosition, wxSize(80,35));
702                         _staticTextSpread                       = new wxStaticText(panel,-1,_T("    "));
703
704                         _wxtextctrlSpread           = new wxTextCtrl(panel,-1, "",wxDefaultPosition, wxSize(200,150), wxTE_MULTILINE );
705
706                         _spreadFrame->SetEventHandler( this );
707                         Connect( spreadResetBtn->GetId(),       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSpreadReset ); 
708                         Connect( spreadAddBtn->GetId(),         wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSpreadAdd ); 
709                         Connect( spreadGoBtn->GetId(),          wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSpreadGo ); 
710
711                         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
712                         sizer -> Add( spreadResetBtn            , 1, wxGROW );
713                         sizer -> Add( spreadAddBtn                      , 1, wxGROW );
714                         sizer -> Add( _spreadMethodRadiobox     , 1, wxGROW );
715                         sizer -> Add( spreadGoBtn                       , 1, wxGROW );
716                         sizer -> Add( _staticTextSpread         , 1, wxGROW );
717                         sizer -> Add( _wxtextctrlSpread         , 1, wxGROW );
718
719
720                         panel->SetSizer( sizer );
721                         panel->SetSize( sizepanel );
722                         panel->SetAutoLayout( true );
723                         panel->Layout();**/
724                         _spreadFrame->Show();
725                 } else {
726                         if (_spreadFrame->IsShown()==true)
727                         {
728                                 _spreadFrame->Show(false);
729                         } else  {
730                                 _spreadFrame->Show(true);
731                         }
732                 }
733         }
734
735         wxPanel* wxContourEventHandler::getSpreadPanel(wxWindow* parent){
736                 //wxPanel *panel                                = new wxPanel(parent,-1);
737
738                 _contourPropagation                     = new ContourPropagation();
739
740                 wxPanel *panel                                                  = new wxPanel(parent,-1,wxDefaultPosition, wxDefaultSize,wxNO_BORDER, wxString("",wxConvUTF8));
741
742                 wxButton *spreadResetBtn        = new wxButton(panel,-1,_T("Reset"),wxDefaultPosition, wxDefaultSize );
743                 wxButton *spreadAddBtn          = new wxButton(panel,-1,_T("Add"),wxDefaultPosition, wxDefaultSize);
744
745                 wxString lstOptions[3];
746                 lstOptions[0]=_T("A");
747                 lstOptions[1]=_T("B");
748                 lstOptions[2]=_T("C");
749                 _spreadMethodRadiobox                                           = new wxRadioBox(panel, -1, _T("Method (find ctrl. Points)"), wxDefaultPosition, wxDefaultSize, 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
750                 _spreadMethodRadiobox->SetSelection(2);
751
752                 wxButton *spreadGoBtn           = new wxButton(panel,-1,_T("Go"),wxDefaultPosition, wxDefaultSize);
753                 _staticTextSpread                       = new wxStaticText(panel,-1,_T("    "));
754
755                 _wxtextctrlSpread           = new wxTextCtrl(panel,-1, _T(""),wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
756
757                 
758                 Connect( spreadResetBtn->GetId(),       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSpreadReset ); 
759                 Connect( spreadAddBtn->GetId(),         wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSpreadAdd ); 
760                 Connect( spreadGoBtn->GetId(),          wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSpreadGo ); 
761
762                 wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
763                 sizer -> Add( spreadResetBtn            , 1, wxGROW );
764                 sizer -> Add( spreadAddBtn                      , 1, wxGROW );
765                 sizer -> Add( _spreadMethodRadiobox     , 1, wxGROW );
766                 sizer -> Add( spreadGoBtn                       , 1, wxGROW );
767                 sizer -> Add( _staticTextSpread         , 1, wxGROW );
768                 sizer -> Add( _wxtextctrlSpread         , 1, wxGROW );
769
770
771                 panel->SetSizer( sizer );
772                 //panel->SetSize( wxDefaultSize );
773                 panel->SetAutoLayout( true );
774                 panel->Layout();
775                 return panel;
776         }
777
778         //------------------------------------------------------------------------------------------------------------
779         void wxContourEventHandler::onSegmentationAllSlice( wxCommandEvent& event )
780         {
781                 //JCP 20-10-08 Undo redo implementation
782                 saveState();
783                 //JCP 20-10-08 Undo redo implementation
784                 
785                 wxBusyCursor wait;
786                 int                                     x                                       = this->_theViewPanel->GetX();
787                 int                                     y                                       = this->_theViewPanel->GetY();
788                 int z,minZ,maxZ;
789                 double porcent; 
790                 wxString tmpString;
791                 minZ=_mbarrange->GetStart();
792                 maxZ=_mbarrange->GetEnd();
793                 double totalZ = maxZ-minZ+1;
794                 for( z=minZ ; z<=maxZ ; z++ )
795                 {
796                         porcent = 100.0* (z-minZ)/totalZ;
797                         tmpString.Printf(_T("  %d %c            %d/%d            %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ,z );
798                         _staticTextSegmentation->SetLabel(tmpString);
799                         SegmentationOneSlice( x,y,z );
800                 }
801                 _staticTextSegmentation->SetLabel(_T("   "));
802                 RefreshInterface();
803
804                 
805         }
806
807         //------------------------------------------------------------------------------------------------------------
808         void wxContourEventHandler::onSegmentationOneSlice( wxCommandEvent& event )
809         {
810                 //JCP 20-10-08 Undo redo implementation
811                 saveState();
812                 //JCP 20-10-08 Undo redo implementation
813                 
814                 wxBusyCursor wait;
815                 int                                     x                                       = this->_theViewPanel->GetX();
816                 int                                     y                                       = this->_theViewPanel->GetY();
817                 int                                     z                                       = this->_theViewPanel->GetZ();
818                 SegmentationOneSlice( x,y,z );
819                 RefreshInterface();
820
821                 
822         }
823         //------------------------------------------------------------------------------------------------------------
824         void wxContourEventHandler::SegmentationOneSlice( int x, int y, int z )
825         {               
826                 int typeofcontour = 1;
827                 //--Extracting Contour
828                 vtkImageData    *imagedata      = _theViewPanel->getSceneManager()->GetImageData();
829 //              double                  *range          = imagedata->GetScalarRange();  
830 //              double                  thr                     = 1;
831                 int                             isovalue        = _isovalue->GetValue();
832                 int                             sampling        = _sampling->GetValue();
833
834                 vtkImageReslice *imageReslice = vtkImageReslice::New();
835 //EED
836 //              double spc[3];
837 //              imagedata->GetSpacing(spc);
838 //              x = x*spc[0];
839 //              y = y*spc[1];
840 //              z = z*spc[3];
841
842                 imageReslice->SetInput( imagedata );
843                 imageReslice->SetInformationInput(imagedata);
844                 imageReslice->SetResliceAxesDirectionCosines(1,0,0, 0,1,0 ,0,0,1);
845                 imageReslice->SetResliceAxesOrigin(0,0,z);
846                 imageReslice->SetOutputDimensionality(2);
847                 imageReslice->SetInterpolationModeToLinear();
848
849                 imagedata = imageReslice->GetOutput();
850                 imagedata->Update();
851                 imagedata->UpdateInformation();
852
853                 vtkContourFilter* cntVTK = vtkContourFilter::New( );
854                 cntVTK->SetInput( imagedata );
855
856                 cntVTK->SetNumberOfContours( 1 );
857                 //cntVTK->SetValue( 0, vmin );
858 //              cntVTK->SetValue( 0, (range[1]*thr/100) );
859                 cntVTK->SetValue( 1, isovalue );
860         //      cntVTK->SetValue( 1, vmax );
861                 cntVTK->Update( );
862                 cntVTK->UpdateInformation();
863                         
864                 vtkCleanPolyData* cpd = vtkCleanPolyData::New( );
865                 cpd->SetInput( cntVTK->GetOutput( ) );
866                 cpd->ConvertLinesToPointsOff( );
867                 cpd->Update( );
868                 cpd->UpdateInformation();
869
870                 vtkPolyDataConnectivityFilter* conn = vtkPolyDataConnectivityFilter::New( );
871                 conn->SetExtractionModeToClosestPointRegion( );
872                 //conn->SetMaxRecursionDepth( 3000 );
873                         
874                 conn->SetInput( cpd->GetOutput( ) );
875                         
876                 conn->SetClosestPoint( x, y, 0 );
877                 conn->Update( );
878                 conn->UpdateInformation();
879                         
880                 vtkCleanPolyData* cpd2 = vtkCleanPolyData::New( );
881                 cpd2->SetInput( conn->GetOutput( ) );
882                 cpd2->Update();
883                 cpd2->UpdateInformation();
884
885                 vtkStripper* vtkstripper = vtkStripper::New( );
886                 vtkstripper->SetInput( cpd2->GetOutput() );
887                 vtkstripper->Update();
888                 vtkstripper->UpdateInformation();
889
890
891                 vtkPolyData* polyDataResult =  vtkstripper->GetOutput();
892
893                 polyDataResult->Update( );
894                 polyDataResult->UpdateInformation();
895
896 /* EED
897 ofstream myfile;
898 myfile.open ("c:/temp/example.txt");
899 myfile << "\n";
900 polyDataResult->Print(myfile);
901 myfile << "-------------------------------------\n";
902 polyDataResult->GetLines()->Print(myfile);
903 myfile.close();
904 */
905
906                 cntVTK          -> Delete();
907                 cpd2            -> Delete();
908                 cpd                     -> Delete();
909                 conn            -> Delete();
910
911
912 //--Calculating control points
913
914                 std::vector<double> vecX;
915                 std::vector<double> vecY;
916                 std::vector<double> vecZ;
917
918                 std::vector<double> vecCtrlPointX;
919                 std::vector<double> vecCtrlPointY;
920                 std::vector<double> vecCtrlPointZ;
921
922
923                 double *p;
924                 int ii,size=polyDataResult->GetNumberOfPoints();
925                 int id;
926                 for (ii=1;ii<=size;ii++)
927                 {
928                         id      = polyDataResult->GetLines()->GetData()->GetValue(ii);
929                         p       = polyDataResult->GetPoint(id);
930                         double x=p[0];
931                         double y=p[1];
932                         vecX.push_back( p[0] );
933                         vecY.push_back( p[1] );
934                         vecZ.push_back( -900 );
935 //                      vecZ.push_back( p[2] );
936                 }
937
938
939                 ExtractControlPoints2D *extractcontrolpoints2d = new ExtractControlPoints2D();
940                 extractcontrolpoints2d->SetContour( &vecX , &vecY , &vecZ );
941
942 //PROOFS
943                 if (methodRadiobox->GetSelection()==0){
944                         extractcontrolpoints2d->GetInitialControlPoints( &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
945                 }
946                 if (methodRadiobox->GetSelection()==1){
947                         extractcontrolpoints2d->GetControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
948                 }
949                 if (methodRadiobox->GetSelection()==2){
950                         extractcontrolpoints2d->SetSamplingControlPoints( sampling );
951                         extractcontrolpoints2d->GetSamplingControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
952                 }
953
954                 //--Adding contour to the system
955
956                 std::vector<int> actualInstantVector;
957                 _instantPanel->getInstant( actualInstantVector );
958                 actualInstantVector[1]=z;
959
960                 int j,sizeCtrPt = vecCtrlPointX.size();
961                 manualContourModel *manModelContour =  factoryManualContourModel( typeofcontour );
962                 manModelContour->SetNumberOfPointsSpline( ((sizeCtrPt/100)+1)*100 );
963                 if (sizeCtrPt>=3){
964                         for (j=0 ; j<sizeCtrPt ; j++)
965                         {
966                                 manModelContour->AddPoint( vecCtrlPointX[j] , vecCtrlPointY[j] , vecCtrlPointZ[j]  );
967                         } // for
968                         std::string theName;
969                         theName = _modelManager->createOutline( manModelContour, actualInstantVector );
970                         bool addedModel = theName.compare("") != 0;
971                         if( addedModel )
972                         {
973                                 double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
974                                 this->_theViewPanel->getSpacing(spc);                                   
975                                 //Adding the manualContourControler to interface objects structure
976                                 //Adding the manualViewContour to interface objects structure           
977                                 //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
978                                 _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc, typeofcontour ) ;
979                         }       // if addedModel
980                 } // if sizeCtrPt
981         }
982
983         //------------------------------------------------------------------------------------------------------------
984         void wxContourEventHandler :: onAutomatiqueSegmentation( )
985         {
986                 if (_segmentationFrame==NULL)
987                 {
988                         //JCP 13-10-09
989                         /*int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
990                         double range[2];
991                         _theViewPanel->getSceneManager()->GetImageDataRange(range);*/
992                         //JCP 13-10-09
993
994                          wxSize sizePanel( 300, 470);
995                         _segmentationFrame                                      =  new wxFrame (_theViewPanel, -1,_T("  Segmentation  "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
996                         _segmentationFrame->SetSize( sizePanel );
997                         wxPanel *panel                                          = getSegmentationPanel(_segmentationFrame);
998                                 /*new wxPanel(_segmentationFrame,-1);
999                         wxButton *segmentationOneSliceBtn       = new wxButton(panel,-1,_T("Actual slice"), wxDefaultPosition, wxSize(200,35) );
1000                         wxButton *segmentationAllSliceBtn       = new wxButton(panel,-1,_T("All slices") ,wxDefaultPosition, wxSize(200,35) );
1001                         _isovalue                                                       = new wxSlider(panel, -1, 40 , (int)(range[0]), (int)(range[1]), wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
1002                         _sampling                                                       = new wxSlider(panel, -1, 20 , 4, 50, wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
1003                         wxString lstOptions[3];
1004                         lstOptions[0]="A";
1005                         lstOptions[1]="B";
1006                         lstOptions[2]="C";
1007                         methodRadiobox                                          = new wxRadioBox(panel, -1, "Method (find ctrl. Points)", wxDefaultPosition, wxSize(200,45), 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
1008                         methodRadiobox->SetSelection(2);
1009                         _staticTextSegmentation                         = new wxStaticText(panel,-1,_T("    "));
1010
1011
1012                         _mbarrange                                      =  new mBarRange(panel,70, 65);
1013                         _mbarrange->SetMin(0);
1014                         _mbarrange->SetStart(0);
1015
1016                         _mbarrange-> SetOrientation( true );
1017                         _mbarrange-> setActiveStateTo(true);
1018                         _mbarrange-> setVisibleLabels( true );
1019                         _mbarrange-> setDeviceEndMargin(10);
1020                         _mbarrange-> setRepresentedValues( 0 , sizeZ );
1021                         _mbarrange-> setDeviceBlitStart(10,10); 
1022                         _mbarrange-> setIfWithActualDrawed( false );
1023                         _mbarrange-> SetStart( 0 );
1024                         _mbarrange-> SetEnd( sizeZ );  
1025
1026
1027 //                      _segmentationFrame->SetEventHandler( this );
1028                         segmentationOneSliceBtn->SetEventHandler( this );
1029                         segmentationAllSliceBtn->SetEventHandler( this );
1030                         Connect( segmentationOneSliceBtn->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSegmentationOneSlice ); 
1031                         Connect( segmentationAllSliceBtn->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSegmentationAllSlice ); 
1032
1033                         wxFlexGridSizer * sizer                         = new wxFlexGridSizer(1);
1034                         sizer -> Add( new wxStaticText(panel,-1,_T("Isovalue (Gray level)"))  , 1, wxGROW );
1035                         sizer -> Add( _isovalue , 1, wxGROW );
1036                         sizer -> Add( methodRadiobox , 1, wxGROW );
1037                         sizer -> Add( new wxStaticText(panel,-1,_T("Sampling (%)"))  , 1, wxGROW );
1038                         sizer -> Add( _sampling , 1, wxGROW );
1039                         sizer -> Add( segmentationOneSliceBtn , 1, wxGROW );
1040                         sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
1041                         sizer -> Add( _staticTextSegmentation  , 1, wxGROW );
1042                         sizer -> Add( segmentationAllSliceBtn , 1, wxGROW );
1043                         sizer -> Add( _mbarrange  , 1, wxGROW );
1044
1045                         panel->SetSizer( sizer );
1046                         panel->SetSize( sizePanel );
1047                         panel->SetAutoLayout( true );
1048                         panel->Layout();*/
1049
1050                         this->_theViewPanel->SetVisibleAxis(true);
1051                         this->_theViewPanel->Refresh();
1052                         _segmentationFrame->Show(true);
1053 //JCP 13-10-09 View the method getSegmentationPanel(), this two methods where moved to getSegmentationPanel
1054                         //this->_theViewPanel->SetVisibleAxis(true);
1055                         //this->_theViewPanel->Refresh();
1056                 } else {
1057                         if (_segmentationFrame->IsShown()==true)
1058                         {
1059                                 _segmentationFrame->Show(false);
1060                                 this->_theViewPanel->SetVisibleAxis(false);
1061                                 this->_theViewPanel->Refresh();
1062                         } else  {
1063                                 _segmentationFrame->Show(true);
1064                                 this->_theViewPanel->SetVisibleAxis(true);
1065                                 this->_theViewPanel->Refresh();
1066                         }
1067                 }
1068         }
1069
1070         void wxContourEventHandler::showAxis(bool show){
1071                 this->_theViewPanel->SetVisibleAxis(show);
1072                 this->_theViewPanel->Refresh();
1073         }
1074         wxPanel* wxContourEventHandler::getSegmentationPanel(wxWindow* parent){
1075                 int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
1076                 double range[2];
1077                 _theViewPanel->getSceneManager()->GetImageDataRange(range);
1078
1079                 wxPanel *panel                                          = new wxPanel(parent,-1);
1080                 wxButton *segmentationOneSliceBtn       = new wxButton(panel,-1,_T("Actual slice"), wxDefaultPosition, wxSize(200,35) );
1081                 wxButton *segmentationAllSliceBtn       = new wxButton(panel,-1,_T("All slices") ,wxDefaultPosition, wxSize(200,35) );
1082                 _isovalue                                                       = new wxSlider(panel, -1, 40 , (int)(range[0]), (int)(range[1]), wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
1083                 _sampling                                                       = new wxSlider(panel, -1, 20 , 4, 50, wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
1084                 wxString lstOptions[3];
1085                 lstOptions[0]=_T("A");
1086                 lstOptions[1]=_T("B");
1087                 lstOptions[2]=_T("C");
1088                 methodRadiobox                                          = new wxRadioBox(panel, -1, _T("Method (find ctrl. Points)"), wxDefaultPosition, wxSize(200,45), 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
1089                 methodRadiobox->SetSelection(2);
1090                 _staticTextSegmentation                         = new wxStaticText(panel,-1,_T("    "));
1091
1092
1093                 _mbarrange                                      =  new mBarRange(panel,70, 65);
1094                 _mbarrange->SetMin(0);
1095                 _mbarrange->SetStart(0);
1096
1097                 _mbarrange-> SetOrientation( true );
1098                 _mbarrange-> setActiveStateTo(true);
1099                 _mbarrange-> setVisibleLabels( true );
1100                 _mbarrange-> setDeviceEndMargin(10);
1101                 _mbarrange-> setRepresentedValues( 0 , sizeZ );
1102                 _mbarrange-> setDeviceBlitStart(10,10); 
1103                 _mbarrange-> setIfWithActualDrawed( false );
1104                 _mbarrange-> SetStart( 0 );
1105                 _mbarrange-> SetEnd( sizeZ );  
1106
1107
1108 //                      _segmentationFrame->SetEventHandler( this );
1109                 segmentationOneSliceBtn->SetEventHandler( this );
1110                 segmentationAllSliceBtn->SetEventHandler( this );
1111                 Connect( segmentationOneSliceBtn->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSegmentationOneSlice ); 
1112                 Connect( segmentationAllSliceBtn->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSegmentationAllSlice ); 
1113
1114                 wxFlexGridSizer * sizer                         = new wxFlexGridSizer(1);
1115                 sizer -> Add( new wxStaticText(panel,-1,_T("Isovalue (Gray level)"))  , 1, wxGROW );
1116                 sizer -> Add( _isovalue , 1, wxGROW );
1117                 sizer -> Add( methodRadiobox , 1, wxGROW );
1118                 sizer -> Add( new wxStaticText(panel,-1,_T("Sampling (%)"))  , 1, wxGROW );
1119                 sizer -> Add( _sampling , 1, wxGROW );
1120                 sizer -> Add( segmentationOneSliceBtn , 1, wxGROW );
1121                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
1122                 sizer -> Add( _staticTextSegmentation  , 1, wxGROW );
1123                 sizer -> Add( segmentationAllSliceBtn , 1, wxGROW );
1124                 sizer -> Add( _mbarrange  , 1, wxGROW );
1125
1126                 panel->SetSizer( sizer );
1127 //              panel->SetSize( sizePanel );
1128                 panel->SetSize( wxDefaultSize );
1129                 panel->SetAutoLayout( true );
1130                 panel->Layout();
1131                 
1132                 return panel;
1133         }
1134         void wxContourEventHandler :: onInterfaceTestFrame( )
1135         {
1136                 if (_TestFrame==NULL)
1137                 {
1138                         _contourPropagation                     = new ContourPropagation();
1139                         _TestFrame                                      = new wxFrame (_theViewPanel, -1,_T("  Test Segmentation  "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
1140                         wxSize sizepanel(200,300);
1141                         _TestFrame->SetSize( sizepanel );
1142                         wxPanel *panel                          = new wxPanel(_TestFrame,-1);
1143                         wxButton *spreadResetBtn        = new wxButton(panel,-1,_T("Reset"),wxDefaultPosition, wxSize(80,35) );
1144                         wxButton *spreadAddBtn          = new wxButton(panel,-1,_T("Add"),wxDefaultPosition, wxSize(80,35));
1145                         wxButton *spreadGoBtn           = new wxButton(panel,-1,_T("GoA"),wxDefaultPosition, wxSize(80,35));
1146                         _wxtextctrlTest                         = new wxTextCtrl(panel,-1, _T(""),wxDefaultPosition, wxSize(200,150), wxTE_MULTILINE );
1147
1148                         _TestFrame->SetEventHandler( this );
1149                         Connect( spreadResetBtn->GetId(),       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onTestReset ); 
1150                         Connect( spreadAddBtn->GetId(),         wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onTestAdd ); 
1151                         Connect( spreadGoBtn->GetId(),          wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onTestGo ); 
1152
1153                         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
1154                         sizer -> Add( spreadResetBtn    , 1, wxGROW );
1155                         sizer -> Add( spreadAddBtn              , 1, wxGROW );
1156                         sizer -> Add( spreadGoBtn               , 1, wxGROW );
1157                         sizer -> Add( _wxtextctrlTest , 1, wxGROW );
1158
1159                         panel->SetSizer( sizer );
1160                         panel->SetSize( sizepanel );
1161                         panel->SetAutoLayout( true );
1162                         panel->Layout();
1163                         _TestFrame->Show();
1164                 } else {
1165                         if (_TestFrame->IsShown()==true)
1166                         {
1167                                 _TestFrame->Show(false);
1168                         } else  {
1169                                 _TestFrame->Show(true);
1170                         }
1171                 }
1172         }
1173
1174         void wxContourEventHandler::onTestReset( wxCommandEvent& event )
1175         {
1176                 _wxtextctrlTest->SetValue(_T(""));
1177                 _contourPropagation->resetAppend();
1178         }
1179
1180         //------------------------------------------------------------------------------------------------------------
1181         void wxContourEventHandler::onTestAdd( wxCommandEvent& event )
1182         {
1183                 std::vector<double> vecX; 
1184                 std::vector<double> vecY; 
1185                 std::vector<double> vecZ; 
1186                 _theViewPanel->getSceneManager()->GetPointsOfActualContour( &vecX , &vecY , &vecZ );    
1187
1188                 if (vecX.size()!=0){
1189                         std::vector<int> tempVector;
1190                         _instantPanel->getInstant( tempVector );
1191                         int i,size=vecZ.size();
1192                         int actualSlice = tempVector[1];
1193                         for ( i=0 ; i<size ; i++ )
1194                         {
1195                                 vecZ[i] = actualSlice;
1196                         } // for
1197
1198                         _contourPropagation->appendContour(&vecX , &vecY , &vecZ);
1199                         wxString newstring;
1200                         newstring.append(_wxtextctrlTest->GetValue());
1201                         newstring.append(_T(" "));
1202                         newstring.append(wxString(intToString(actualSlice).c_str(),wxConvUTF8));                        
1203                         //newstring.Printf(_T("%s %d -"),,  );
1204                         newstring.append(_T(" -"));
1205                         _wxtextctrlTest->SetValue(newstring);
1206                 } // if 
1207         }
1208
1209         //------------------------------------------------------------------------------------------------------------
1210         void wxContourEventHandler::onTestGo( wxCommandEvent& event )
1211         {
1212                 std::vector<double> vecX; 
1213                 std::vector<double> vecY; 
1214                 std::vector<double> vecZ; 
1215                 std::vector<int> size; 
1216
1217                 std::vector<double> vecCtrlPointX;
1218                 std::vector<double> vecCtrlPointY;
1219                 std::vector<double> vecCtrlPointZ;
1220
1221                 //Getting the points of the actual contour      
1222
1223                 _contourPropagation->GetKeyContours(&vecX , &vecY , &vecZ, &size);
1224                 for(int i = 0; i < vecX.size();i++){
1225                         vecZ[i] = -900;
1226                 }
1227
1228                 /*std::vector<double> vecX1; 
1229                 std::vector<double> vecY1; 
1230                 std::vector<double> vecZ1;
1231                 _theViewPanel->getSceneManager()->GetPointsOfActualContour( &vecX1 , &vecY1 , &vecZ1 );
1232
1233                 std::ofstream file1;
1234         file1.open( "Temp.txt" );       
1235                 if(file1.is_open())
1236                 {
1237                         for(int i = 0; i < vecX.size(); i++){
1238                                 file1<<vecX[i] <<" X1 "<< vecX1[i] <<" Y "<<vecY[i] <<" Y1 "<< vecY1[i]  <<" Z "<<vecZ[i] <<" Z1 "<< vecZ1[i]<<std::endl;
1239                                 
1240                         }
1241                         file1.close();
1242                 }*/     
1243                         
1244                 
1245
1246                 //Setting the points of the contour 
1247
1248                 ExtractControlPoints2D *extractcontrolpoints2d = new ExtractControlPoints2D();
1249                 extractcontrolpoints2d->SetContour( &vecX , &vecY , &vecZ );
1250
1251                 //Getting the control points of the contour by method A
1252
1253                 extractcontrolpoints2d->GetInitialControlPoints( &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
1254                 
1255                 std::vector<int> actualInstantVector;
1256                 _instantPanel->getInstant( actualInstantVector );
1257                 actualInstantVector[1]= this->_theViewPanel->GetZ();
1258
1259
1260
1261
1262
1263                 //Adding the Contour to the scene
1264
1265                 manualContourModel *manModelContour = new manualContourModel();
1266                 int j,sizeCtrPt = vecCtrlPointX.size();
1267                 if (sizeCtrPt>=3){
1268                         for (j=0 ; j<sizeCtrPt ; j++)
1269                         {
1270                                 manModelContour->AddPoint( vecCtrlPointX[j] , vecCtrlPointY[j] , vecCtrlPointZ[j]  );
1271                         } // for
1272                         std::string theName;
1273                         theName = _modelManager->createOutline( manModelContour, actualInstantVector );
1274                         bool addedModel = theName.compare("") != 0;
1275                         if( addedModel )
1276                         {
1277                                 double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
1278                                 this->_theViewPanel->getSpacing(spc);                                   
1279                                 //Adding the manualContourControler to interface objects structure
1280                                 //Adding the manualViewContour to interface objects structure           
1281                                 //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
1282                                 _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc,1 ) ;
1283                         }       // if addedModel
1284                 } // if sizeCtrPt
1285         }
1286
1287         //------------------------------------------------------------------------------------------------------------
1288         void wxContourEventHandler::FillGridWithContoursInformation()
1289         {
1290                 wxString tempString;
1291                 _grid->ClearGrid();
1292                 _grid->SetColLabelValue(0, _T("A") );
1293                 _grid->SetColLabelValue(1, _T("B") );
1294                 _grid->SetColLabelValue(2, _T("C") );
1295                 _grid->SetColLabelValue(3, _T("D") );
1296                 _grid->SetColLabelValue(4, _T("E") );
1297                 _grid->SetColLabelValue(5, _T("F") );
1298
1299                 std::vector<int> tempVector;
1300                 _instantPanel->getInstant( tempVector );
1301
1302                 int z,sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
1303                 int ii,sizeLstContourThings;
1304                 for ( z=0 ; z<sizeZ ; z++)
1305                 {
1306                         tempVector[1]=z;
1307                         Instant instant(&tempVector);
1308                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
1309
1310                         sizeLstContourThings = lstContourThings.size();
1311                         tempString.Printf(_T("%d - %d"),z, sizeLstContourThings);
1312                         _grid->SetRowLabelValue(z, tempString );
1313
1314                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
1315                         {
1316                                 ContourThing **contourthing = lstContourThings[ii];
1317                                 tempString = wxString((*contourthing)->getName().c_str(),wxConvUTF8) ;
1318                                 _grid->SetCellValue( z, ii, tempString );
1319                         }
1320                 }
1321
1322
1323         }
1324
1325         //------------------------------------------------------------------------------------------------------------
1326         std::vector<manualContourModel*> wxContourEventHandler::ExploseEachModel( std::vector<manualContourModel*> lstManConMod )
1327         {
1328                 std::vector<manualContourModel*> lstTmp;
1329                 std::vector<manualContourModel*> lstResult;
1330                 int j,jSize;
1331                 int i,iSize=lstManConMod.size();
1332                 for (i=0;i<iSize;i++)
1333                 {
1334                         lstTmp = lstManConMod[i]->ExploseModel();
1335                         jSize=lstTmp.size();
1336                         for (j=0;j<jSize;j++)
1337                         {
1338                                 lstResult.push_back( lstTmp[j] );
1339                         }
1340                 }
1341                 return lstResult;
1342         }
1343
1344         //------------------------------------------------------------------------------------------------------------
1345         void wxContourEventHandler::onExtractInformation( wxCommandEvent& event )
1346         {
1347                 wxBusyCursor wait;
1348                 int maxContourGroup     =       0;
1349
1350                 _grid->ClearGrid();
1351
1352                 ContourExtractData      *contourextractdata = new ContourExtractData();
1353                 int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1354                 contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1355
1356                 std::vector<manualContourModel*> lstManConMod;
1357                 std::vector<manualContourModel*> lstManConModTmp;
1358                 std::vector<manualContourModel*> lstManConModExp;
1359                 std::vector<double> pLstValue;
1360                 std::vector<double> pLstValuePosX;
1361                 std::vector<double> pLstValuePosY;
1362                 std::vector<double> pLstValuePosZ;
1363
1364                 int             resultSize; 
1365                 int             resultGrayRangeCount;
1366                 double  resultMin; 
1367                 double  resultMax;
1368                 double  resultAverage;
1369                 double  resultStandardeviation;
1370
1371                 std::vector<int> tempVector;
1372                 _instantPanel->getInstant( tempVector );
1373
1374                 vtkImageData *imagedata = _theViewPanel->getSceneManager()->GetImageData();
1375                 int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
1376                 int z;
1377                 int ii,sizeLstContourThings;
1378
1379
1380                 int minZ,maxZ;
1381                 double totalZ;
1382                 double porcent; 
1383                 wxString tmpString;
1384
1385
1386                 if (_informationRadiobox->GetSelection()==0 ) // actual slice
1387                 {
1388                         std::vector<int> tempVector;
1389                         _instantPanel->getInstant( tempVector );
1390                         int actualSlice = tempVector[1];
1391                         minZ    = actualSlice;
1392                         maxZ    = actualSlice;
1393                 }
1394                 if (_informationRadiobox->GetSelection()==1 ) // slice range
1395                 {
1396                         minZ    = _mbarrangeSliceInformation->GetStart();
1397                         maxZ    = _mbarrangeSliceInformation->GetEnd();
1398                 }
1399                 if (_informationRadiobox->GetSelection()==2 ) // All slices
1400                 {
1401                         minZ    = 0;
1402                         maxZ    = sizeZ-1;
1403                 }
1404
1405                 totalZ  = maxZ-minZ+1;
1406                 contourextractdata->SetImage( imagedata);
1407
1408         // For each slice..
1409                 for ( z=minZ ; z<=maxZ ; z++ )
1410                 {
1411                         porcent = 100.0* (z-minZ)/totalZ;
1412                         tmpString.Printf(_T("  %d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1413                         _staticTextInformation->SetLabel(tmpString);
1414
1415                         //Extraction data from contours of each slice
1416                         contourextractdata->SetZtoBeAnalys( z);
1417
1418                         tempVector[1]=z;
1419                         Instant instant(&tempVector);
1420                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
1421                         sizeLstContourThings = lstContourThings.size();
1422
1423                         lstManConMod.clear();
1424                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
1425                         {
1426                                 ContourThing **contourthing = lstContourThings[ii];
1427                                 lstManConMod.push_back( (*contourthing)->getModel() );
1428                         }
1429                         lstManConModExp =  ExploseEachModel( lstManConMod );
1430
1431
1432                         wxString tempString;
1433                         tempString.Printf(_T("%d - %d"),z, sizeLstContourThings);
1434                         _grid->SetRowLabelValue(z, tempString );
1435 //EED004
1436                         int iContourGroup,sizeContourGroup;
1437                         if (typeContourGroup==3) // contour separete
1438                         {
1439                                 sizeContourGroup=lstManConModExp.size();
1440                                 if ( maxContourGroup<sizeContourGroup ) 
1441                                 {
1442                                         maxContourGroup=sizeContourGroup;
1443                                 }
1444                         } else {  // contour AND OR XOR
1445                                 sizeContourGroup=1;
1446                                 maxContourGroup=1;
1447                         }
1448
1449                         int tmpIntA;
1450
1451
1452
1453                         for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1454                                 lstManConModTmp.clear();
1455                                 if (typeContourGroup==3) // contour separete
1456                                 {
1457                                         lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1458                                 } else {  // contour AND OR XOR
1459                                         lstManConModTmp = lstManConModExp;
1460                                 }
1461
1462                                 contourextractdata->SetLstManualContourModel( lstManConModTmp );
1463
1464                                 pLstValue.clear();
1465                                 pLstValuePosX.clear();
1466                                 pLstValuePosY.clear();
1467                                 pLstValuePosZ.clear();
1468                                 contourextractdata->GetValuesInsideCrown(       &pLstValue,
1469                                                                                                                         &pLstValuePosX,
1470                                                                                                                         &pLstValuePosY,
1471                                                                                                                         &pLstValuePosZ);
1472                                 // Statistics of each slice.
1473                                 contourextractdata->Statistics( &pLstValue,
1474                                                                                                 _mbarrangeRangeInformation->GetStart(),
1475                                                                                                 _mbarrangeRangeInformation->GetEnd(),
1476                                                                                                 &resultGrayRangeCount, 
1477                                                                                                 &resultSize, 
1478                                                                                                 &resultMin, 
1479                                                                                                 &resultMax,
1480                                                                                                 &resultAverage,
1481                                                                                                 &resultStandardeviation);
1482                                 if (_grid->GetNumberCols()<_numberOfVariablesStatistics*(iContourGroup+1)  )
1483                                 {
1484                                         _grid->AppendCols(_numberOfVariablesStatistics);
1485                                 }
1486
1487                                 tmpIntA=_numberOfVariablesStatistics*iContourGroup ;
1488
1489                                 tempString.Printf(_T("%d"),resultSize);
1490                                 _grid->SetCellValue( z, tmpIntA + 0, tempString );
1491                                 tempString.Printf(_T("%d"),resultGrayRangeCount);
1492                                 _grid->SetCellValue( z, tmpIntA + 1, tempString );
1493                                 tempString.Printf(_T("%f"),resultMin);
1494                                 _grid->SetCellValue( z, tmpIntA + 2, tempString );
1495                                 tempString.Printf(_T("%f"),resultMax);
1496                                 _grid->SetCellValue( z, tmpIntA + 3, tempString );
1497                                 tempString.Printf(_T("%f"),resultAverage);
1498                                 _grid->SetCellValue( z, tmpIntA + 4, tempString );
1499                                 tempString.Printf(_T("%f"),resultStandardeviation);
1500                                 _grid->SetCellValue( z, tmpIntA + 5, tempString );
1501
1502                         } // for iContourGroup
1503                 } // for z
1504
1505                 int iTitleGroup;
1506                 wxString tmpTitleString;
1507                 int tmpIntB;
1508                 for ( iTitleGroup=0 ; iTitleGroup<maxContourGroup ; iTitleGroup++ )
1509                 {
1510                         tmpIntB =_numberOfVariablesStatistics*iTitleGroup;
1511                         tmpTitleString.Printf(_T("%d-Size Data"),iTitleGroup);
1512                         _grid->SetColLabelValue( tmpIntB + 0, tmpTitleString );
1513                         _grid->SetColLabelValue( tmpIntB + 1, _T("Size Range") );
1514                         _grid->SetColLabelValue( tmpIntB + 2, _T("Min") );
1515                         _grid->SetColLabelValue( tmpIntB + 3, _T("Max") );
1516                         _grid->SetColLabelValue( tmpIntB + 4, _T("Average") );
1517                         _grid->SetColLabelValue( tmpIntB + 5, _T("St.Dev.") );
1518                         _grid->SetColLabelValue( tmpIntB + 6, _T(" ") );
1519                 }
1520
1521                 _staticTextInformation->SetLabel( _T("") );
1522                 delete contourextractdata;
1523         }
1524
1525
1526         //------------------------------------------------------------------------------------------------------------
1527         void wxContourEventHandler::SaveValuesXYZ(std::string directory,std::string namefile)
1528         {
1529                 wxBusyCursor wait;
1530                 ContourExtractData      *contourextractdata = new ContourExtractData();
1531                 int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1532                 contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1533
1534                 std::vector<manualContourModel*> lstManConMod;
1535                 std::vector<manualContourModel*> lstManConModTmp;
1536                 std::vector<manualContourModel*> lstManConModExp;
1537                 std::vector<double> pLstValue;
1538                 std::vector<double> pLstValuePosX;
1539                 std::vector<double> pLstValuePosY;
1540                 std::vector<double> pLstValuePosZ;
1541
1542                 std::vector<int> tempVector;
1543                 _instantPanel->getInstant( tempVector );
1544
1545                 vtkImageData *imagedata = _theViewPanel->getSceneManager()->GetImageData();
1546                 int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
1547                 int z;
1548                 int ii,sizeLstContourThings;
1549
1550
1551                 int minZ,maxZ;
1552                 double totalZ;
1553                 double porcent; 
1554                 wxString tmpString;
1555                 minZ    = 0;// _mbarrange->GetStart();
1556                 maxZ    = sizeZ;//_mbarrange->GetEnd();
1557                 totalZ  = maxZ-minZ+1;
1558
1559                 contourextractdata->SetImage( imagedata);
1560
1561         // For each slice..
1562                 for ( z=0 ; z<sizeZ ; z++)
1563                 {
1564
1565                         porcent = 100.0* (z-minZ)/totalZ;
1566                         
1567                         tmpString.Printf(_T("Saving Values"));
1568                         tmpString.Printf(_T("  %d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1569                         _staticTextInformation->SetLabel(tmpString);
1570
1571
1572                         //Extraction data from contours of each slice
1573                         contourextractdata->SetZtoBeAnalys( z);
1574
1575                         tempVector[1]=z;
1576                         Instant instant(&tempVector);
1577                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
1578                         sizeLstContourThings = lstContourThings.size();
1579
1580                         lstManConMod.clear();
1581                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
1582                         {
1583                                 ContourThing **contourthing = lstContourThings[ii];
1584                                 lstManConMod.push_back( (*contourthing)->getModel() );
1585                         }
1586                         lstManConModExp =  ExploseEachModel( lstManConMod );
1587
1588
1589 //EED004
1590
1591                         int iContourGroup,sizeContourGroup;
1592                         if (typeContourGroup==3) // contour separete
1593                         {
1594                                 sizeContourGroup=lstManConModExp.size();
1595                         } else {  // contour AND OR XOR
1596                                 sizeContourGroup=1;
1597                         }
1598
1599                         for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1600                                 lstManConModTmp.clear();
1601                                 if (typeContourGroup==3) // contour separete
1602                                 {
1603                                         lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1604                                 } else {  // contour AND OR XOR
1605                                         lstManConModTmp = lstManConModExp;
1606                                 }
1607
1608                                 contourextractdata->SetLstManualContourModel( lstManConModTmp );
1609
1610                                 pLstValue.clear();
1611                                 pLstValuePosX.clear();
1612                                 pLstValuePosY.clear();
1613                                 pLstValuePosZ.clear();
1614                                 contourextractdata->GetValuesInsideCrown(       &pLstValue,
1615                                                                                                                         &pLstValuePosX,
1616                                                                                                                         &pLstValuePosY,
1617                                                                                                                         &pLstValuePosZ);
1618
1619                                 wxString filename;
1620                                 filename.Printf(_T("%s"),directory.c_str());
1621                                 filename.Printf(_T("\\"));
1622                                 filename.Printf(_T("%s"),namefile.c_str());
1623                                 filename.Printf(_T("-slice"));
1624                                 filename.Printf(_T("%d"),z);
1625                                 filename.Printf(_T("-cont"));
1626                                 filename.Printf(_T("%d"),iContourGroup);
1627                                 filename.Printf(_T(".txt"));
1628                                 FILE *pFile=fopen((const char *)filename.mb_str(),"w+");
1629                                 fprintf(pFile,"value \t x \t y \t z\n"  );
1630                                 int iLstValue,sizeLstValue=pLstValue.size();
1631                                 for (iLstValue=0 ; iLstValue<sizeLstValue ; iLstValue++ )
1632                                 {
1633                                         fprintf(pFile,"%f\t %f\t %f\t %f\n", (float)pLstValue[iLstValue] , (float)pLstValuePosX[iLstValue], (float)pLstValuePosY[iLstValue], (float)z );
1634                                 }
1635                                 fclose(pFile);
1636                         } // for  iContourGroup
1637                 } // for z
1638                 _staticTextInformation->SetLabel( _T("") );
1639                 delete contourextractdata;
1640         }
1641
1642
1643         //------------------------------------------------------------------------------------------------------------
1644         void wxContourEventHandler::SaveImageResult(std::string directory,std::string namefile)
1645         {
1646                 wxBusyCursor wait;
1647                 ContourExtractData      *contourextractdata = new ContourExtractData( true );
1648                 int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1649                 if (typeContourGroup==3)
1650                 {
1651                         typeContourGroup=1;
1652                 }
1653                 contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1654
1655                 std::vector<manualContourModel*> lstManConMod;
1656                 std::vector<manualContourModel*> lstManConModTmp;
1657                 std::vector<manualContourModel*> lstManConModExp;
1658
1659                 std::vector<int> tempVector;
1660                 _instantPanel->getInstant( tempVector );
1661
1662                 vtkImageData *imagedata = _theViewPanel->getSceneManager()->GetImageData();
1663                 int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
1664                 int z;
1665                 int ii,sizeLstContourThings;
1666
1667
1668                 int minZ,maxZ;
1669                 double totalZ;
1670                 double porcent; 
1671
1672                 wxString tmpString;
1673
1674                 minZ    = 0;// _mbarrange->GetStart();
1675                 maxZ    = sizeZ;//_mbarrange->GetEnd();
1676                 totalZ  = maxZ-minZ+1;
1677
1678                 contourextractdata->SetImage( imagedata);
1679
1680         // For each slice..
1681                 for ( z=0 ; z<sizeZ ; z++)
1682                 {
1683
1684                         porcent = 100.0* (z-minZ)/totalZ;
1685                         tmpString.Printf(_T("Saving Values"));
1686                         tmpString.Printf(_T("%d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1687                         _staticTextInformation->SetLabel(tmpString);
1688
1689                         //Extraction data from contours of each slice
1690                         contourextractdata->SetZtoBeAnalys( z);
1691
1692                         tempVector[1]=z;
1693                         Instant instant(&tempVector);
1694                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
1695                         sizeLstContourThings = lstContourThings.size();
1696
1697                         lstManConMod.clear();
1698                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
1699                         {
1700                                 ContourThing **contourthing = lstContourThings[ii];
1701                                 lstManConMod.push_back( (*contourthing)->getModel() );
1702                         }
1703                         lstManConModExp =  ExploseEachModel( lstManConMod );
1704
1705
1706                         int iContourGroup,sizeContourGroup;
1707                         if (typeContourGroup==3) // contour separete
1708                         {
1709                                 sizeContourGroup=lstManConModExp.size();
1710                         } else {  // contour AND OR XOR
1711                                 sizeContourGroup=1;
1712                         }
1713
1714                         for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1715                                 lstManConModTmp.clear();
1716                                 if (typeContourGroup==3) // contour separete
1717                                 {
1718                                         lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1719                                 } else {  // contour AND OR XOR
1720                                         lstManConModTmp = lstManConModExp;
1721                                 }
1722
1723                                 contourextractdata->SetLstManualContourModel( lstManConModTmp );
1724
1725
1726 //                              for (ii=0 ; ii<sizeLstContourThings ; ii++)
1727 //                              {
1728 //                                      ContourThing **contourthing = lstContourThings[ii];
1729 //                                      lstManConMod.push_back( (*contourthing)->getModel() );
1730 //                              }
1731 //                              contourextractdata->SetLstManualContourModel( lstManConMod );
1732
1733                                 contourextractdata->CalculateImageResult(); // with actual Z
1734
1735                         } // for  iContourGroup
1736                 } // for z
1737
1738
1739                 wxString filename;
1740                 //filename.Printf(_T("%s\\%s-Value.mhd",directory.c_str(),namefile.c_str(),z);
1741                 filename.Printf(_T("%s"),directory.c_str());
1742                 filename.Printf(_T("\\"));
1743                 filename.Printf(_T("%s"),namefile.c_str());
1744                 filename.Printf(_T("-Value.mhd"));
1745
1746 // Image Value
1747                 vtkMetaImageWriter *writerValueImage = vtkMetaImageWriter::New( );
1748                 writerValueImage->SetInput( contourextractdata->GetVtkImageValueResult() );
1749                 writerValueImage->SetFileName( (const char *)filename.mb_str() );
1750                 writerValueImage->SetFileDimensionality( 3 );
1751                 writerValueImage->Write( );
1752
1753 // Image Mask
1754                 //              filename.Printf("%s\\%s-Mask.mhd",directory.c_str(),namefile.c_str(),z);
1755                 filename.Printf(_T("%s"),directory.c_str());
1756                 filename.Printf(_T("\\"));
1757                 filename.Printf(_T("%s"),namefile.c_str());
1758                 filename.Printf(_T("-Mask.mhd"));
1759                 vtkMetaImageWriter *writerMaskImage = vtkMetaImageWriter::New( );
1760                 writerMaskImage->SetInput( contourextractdata->GetVtkImageMaskResult() );
1761                 writerMaskImage->SetFileName( (const char *)filename.mb_str() );
1762                 writerMaskImage->SetFileDimensionality( 3 );
1763                 writerMaskImage->Write( );
1764
1765                 _staticTextInformation->SetLabel( _T("") );
1766                 delete contourextractdata;
1767         }
1768
1769
1770         //------------------------------------------------------------------------------------------------------------
1771         void wxContourEventHandler::onInformationContourLabels( wxCommandEvent& event )
1772         {
1773                 wxBusyCursor wait;
1774                 FillGridWithContoursInformation();
1775         }
1776
1777
1778         //------------------------------------------------------------------------------------------------------------
1779         void wxContourEventHandler::onSaveResults( wxCommandEvent& event )
1780         {
1781                 wxFileDialog dialog(_InformationContourFrame, _T("Choose a file"), _T(""), _T(""), _T("*.txt"), wxSAVE );
1782                 if (dialog.ShowModal() == wxID_OK)
1783                 {
1784                         onExtractInformation( event );
1785                         std::string directory=(const char *)(dialog.GetDirectory().mb_str());
1786                         std::string namefile=(const char *)(dialog.GetFilename().mb_str());
1787
1788                         SaveValuesXYZ( directory , namefile );
1789                         SaveImageResult( directory , namefile );
1790
1791                         std::string filename = (const char *)(dialog.GetPath().mb_str() );
1792                         FILE *pFile=fopen(filename.c_str(),"w+");
1793
1794                         int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
1795
1796                         wxString tmpString;
1797                         int i,j,maxX,maxY=sizeZ;
1798                         maxX=this->_grid->GetNumberCols();
1799
1800                         int iTitle,sizeTitle = (maxX / _numberOfVariablesStatistics);
1801                         for ( iTitle=0; iTitle<sizeTitle ; iTitle++)
1802                         {
1803                                 fprintf(pFile,"-- \t %d-Size \t SizeRange \t Min \t Max \t Ave \t StDv \t" , iTitle,tmpString.c_str() );
1804                         }
1805                         fprintf(pFile,"\n" );
1806
1807
1808
1809                         for ( j=0; j<maxY ; j++)
1810                         {
1811                                 fprintf(pFile,"%d\t" , j );
1812                                 for (i=0 ; i<maxX ; i++){
1813                                         tmpString = _grid->GetCellValue( j , i );
1814                                         fprintf(pFile,"%s\t" , (const char *)(tmpString.mb_str()) );
1815                                 } // for i
1816                                 fprintf(pFile,"\n"  );
1817                         } // for j
1818
1819                         fclose(pFile);
1820                 } // if  ShowModal
1821
1822
1823         }
1824
1825
1826         //------------------------------------------------------------------------------------------------------------
1827         void wxContourEventHandler::onInformationContourFrame()
1828         {
1829                 if (_InformationContourFrame==NULL)
1830                 {
1831                         _InformationContourFrame                                                = new wxFrame (_theViewPanel, -1,_T("  Information  "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT | wxRESIZE_BORDER  );
1832                         _InformationContourFrame->SetSize( wxSize(500,450) );
1833                         wxSize sizepanel(200,300);
1834                         wxPanel *panel                                                                  = getInformationPanel(_InformationContourFrame);
1835 //JCP 13-10-09  Grouping the panel in a method
1836                         /*new wxPanel(_InformationContourFrame,-1,wxDefaultPosition, wxDefaultSize,wxTAB_TRAVERSAL);
1837
1838                         wxString lstOptions[4];
1839                         lstOptions[0]=_T("Actual Slice");
1840                         lstOptions[1]=_T("Range Slices");
1841                         lstOptions[2]=_T("All Slices");
1842                         _informationRadiobox                                                    = new wxRadioBox(panel, -1, _T("Slice analysis"), wxDefaultPosition, wxSize(270,45), 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
1843
1844                         wxString lstOptContOperation[5];
1845                         lstOptContOperation[0]=_T("AND");
1846                         lstOptContOperation[1]=_T("OR");
1847                         lstOptContOperation[2]=_T("XOR");
1848                         lstOptContOperation[3]=_T("ALL");
1849                         _radiolstboxContourGroup                                                = new wxRadioBox(panel, -1, _T("Contour group"), wxDefaultPosition, wxSize(270,45), 4 , lstOptContOperation,  4, wxRA_SPECIFY_COLS);
1850
1851                         wxButton *informationContourLabelsBtn                   = new wxButton(panel,-1,_T("Contour labels"),wxDefaultPosition, wxSize(140,35) );
1852                         informationContourLabelsBtn->SetEventHandler( this );
1853                         Connect( informationContourLabelsBtn->GetId(),          wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onInformationContourLabels ); 
1854
1855                         wxButton *statisticsContourBtn                                  = new wxButton(panel,-1,_T("Contour statistics"),wxDefaultPosition, wxSize(140,35) );
1856                         statisticsContourBtn->SetEventHandler( this );
1857                         Connect( statisticsContourBtn->GetId(),         wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onExtractInformation ); 
1858
1859                         wxButton *saveResultsBtn                                                = new wxButton(panel,-1,_T("Save statistics results"),wxDefaultPosition, wxSize(140,35) );
1860                         saveResultsBtn->SetEventHandler( this );
1861                         Connect( saveResultsBtn->GetId(),               wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSaveResults ); 
1862
1863
1864                         int sizeZ = this->_theViewPanel->getSceneManager()->GetImageDataSizeZ();                        
1865                         _mbarrangeSliceInformation                                      =  new mBarRange(panel,65,65);
1866                         _mbarrangeSliceInformation->SetMin(0);
1867                         _mbarrangeSliceInformation->SetStart(0);
1868                         _mbarrangeSliceInformation-> SetOrientation( true );
1869                         _mbarrangeSliceInformation-> setActiveStateTo(true);
1870                         _mbarrangeSliceInformation-> setVisibleLabels( true );
1871                         _mbarrangeSliceInformation-> setDeviceEndMargin(10);
1872                         _mbarrangeSliceInformation-> setRepresentedValues( 0 , sizeZ-1 );
1873                         _mbarrangeSliceInformation-> setDeviceBlitStart(10,10); 
1874                         _mbarrangeSliceInformation-> setIfWithActualDrawed( false );
1875                         _mbarrangeSliceInformation-> SetStart( 0 );
1876                         _mbarrangeSliceInformation-> SetEnd( sizeZ-1 );  
1877
1878
1879                         double range[2];
1880                         this->_theViewPanel->getSceneManager()->GetImageDataRange(range);
1881                         _mbarrangeRangeInformation                                      =  new mBarRange(panel,65,65);
1882                         _mbarrangeRangeInformation->SetMin(0);
1883                         _mbarrangeRangeInformation->SetStart(0);
1884                         _mbarrangeRangeInformation-> SetOrientation( true );
1885                         _mbarrangeRangeInformation-> setActiveStateTo(true);
1886                         _mbarrangeRangeInformation-> setVisibleLabels( true );
1887                         _mbarrangeRangeInformation-> setDeviceEndMargin(10);
1888                         _mbarrangeRangeInformation-> setRepresentedValues( range[0] , range[1] );
1889                         _mbarrangeRangeInformation-> setDeviceBlitStart(10,10); 
1890                         _mbarrangeRangeInformation-> setIfWithActualDrawed( false );
1891                         _mbarrangeRangeInformation-> SetStart( range[0] );
1892                         _mbarrangeRangeInformation-> SetEnd( range[1] );  
1893
1894
1895                         _staticTextInformation                                                  = new wxStaticText(panel,-1,_T("    "));
1896
1897
1898 //                      wxButton *XXXXXXXBtn                    = new wxButton(panel,-1,_T("Statistics"),wxDefaultPosition, wxSize(180,35) );
1899 //                      XXXXXXXBtn->SetEventHandler( this );
1900 //                      Connect( XXXXXXXBtn->GetId(),           wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onXXXXX ); 
1901
1902
1903                         _grid = new wxGrid( panel,
1904                        wxID_ANY,
1905                        wxPoint( 0, 0 ),
1906                        wxSize( 100, 100 ) );
1907
1908                         int i,gridCol=10,gridRow=sizeZ;
1909                         _grid->CreateGrid( 0, 0 );
1910                         _grid->AppendRows(gridRow);
1911                         _grid->AppendCols(gridCol);
1912
1913                         for (i=0;i<gridRow;i++)
1914                         {
1915                                 _grid->SetRowLabelValue(i, _T(" ") );
1916                         }
1917 //                      _grid->SetColLabelSize(0);
1918
1919
1920                         FillGridWithContoursInformation();
1921
1922                         wxFlexGridSizer * sizerA                = new wxFlexGridSizer(10);
1923                         sizerA->Add( _informationRadiobox                       , 1, wxALL ,2 );
1924                         sizerA->Add( _radiolstboxContourGroup           , 1, wxALL ,2 );
1925
1926                         wxFlexGridSizer * sizerB                = new wxFlexGridSizer(10);
1927                         sizerB->Add( informationContourLabelsBtn        , 1, wxALL ,2 );
1928                         sizerB->Add( statisticsContourBtn                       , 1, wxALL ,2 );
1929                         sizerB->Add( saveResultsBtn                                     , 1, wxALL ,2 );
1930
1931                         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
1932                         sizer->AddGrowableCol(0);
1933                         sizer->AddGrowableRow(7);
1934                         sizer->Add( sizerA                                              , 1, wxALL              , 0 );
1935                         sizer->Add( new wxStaticText(panel              ,-1,_T("Slice Range"))  , 1, wxALL              , 0 );
1936                         sizer->Add( _mbarrangeSliceInformation  , 1, wxALL|wxGROW               , 2 );                  
1937                         sizer->Add( new wxStaticText(panel              ,-1,_T("Gray Range"))   , 1, wxALL              , 0 );
1938                         sizer->Add( _mbarrangeRangeInformation  , 1, wxALL|wxGROW               , 2 );                  
1939                         sizer->Add( sizerB                                              , 1, wxALL              , 0 );
1940                         sizer->Add( _staticTextInformation              , 1, wxALL              , 0 );
1941                         sizer->Add( _grid                                               , 1, wxEXPAND   , 0 );
1942
1943
1944
1945                         panel->SetSizer( sizer );
1946                         panel->SetSize( wxSize(1500,1500) );
1947                         //panel->SetBackgroundColour( wxColour(100,100,100) );
1948                         panel->SetAutoLayout( true );
1949                         panel->Layout();*/
1950
1951                         wxBoxSizer * sizerPanel         = new wxBoxSizer(wxHORIZONTAL);
1952
1953 //                      sizerPanel->AddGrowableCol(0);
1954 //                      sizerPanel->AddGrowableRow(0);
1955
1956 //JCP 14-10-09
1957                         //sizerPanel -> Add( sizer              , 1, wxEXPAND ,0);
1958                         sizerPanel -> Add( panel->GetSizer()            , 1, wxEXPAND ,0);
1959 //JCP 14-10-09
1960
1961
1962
1963                         _InformationContourFrame->SetSizer( sizerPanel );
1964                         _InformationContourFrame->SetAutoLayout( true );
1965                         _InformationContourFrame->Layout();
1966
1967                         _InformationContourFrame->Show();
1968                 } else {
1969                         if (_InformationContourFrame->IsShown()==true)
1970                         {
1971                                 _InformationContourFrame->Show(false);
1972                         } else  {
1973                                 FillGridWithContoursInformation();
1974                                 _InformationContourFrame->Show(true);
1975                         }
1976                 }
1977         }
1978
1979         wxPanel* wxContourEventHandler::getInformationPanel(wxWindow* parent){
1980         wxPanel *panel                                                                  = new wxPanel(parent,-1,wxDefaultPosition, wxDefaultSize,wxTAB_TRAVERSAL);
1981
1982                 wxString lstOptions[4];
1983                 lstOptions[0]=_T("Actual Slice");
1984                 lstOptions[1]=_T("Range Slices");
1985                 lstOptions[2]=_T("All Slices");
1986                 _informationRadiobox                                                    = new wxRadioBox(panel, -1, _T("Slice analysis"), wxDefaultPosition, wxSize(270,45), 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
1987
1988                 wxString lstOptContOperation[5];
1989                 lstOptContOperation[0]=_T("AND");
1990                 lstOptContOperation[1]=_T("OR");
1991                 lstOptContOperation[2]=_T("XOR");
1992                 lstOptContOperation[3]=_T("ALL");
1993                 _radiolstboxContourGroup                                                = new wxRadioBox(panel, -1, _T("Contour group"), wxDefaultPosition, wxSize(270,45), 4 , lstOptContOperation,  4, wxRA_SPECIFY_COLS);
1994
1995                 wxButton *informationContourLabelsBtn                   = new wxButton(panel,-1,_T("Contour labels"),wxDefaultPosition, wxSize(140,35) );
1996                 informationContourLabelsBtn->SetEventHandler( this );
1997                 Connect( informationContourLabelsBtn->GetId(),          wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onInformationContourLabels ); 
1998
1999                 wxButton *statisticsContourBtn                                  = new wxButton(panel,-1,_T("Contour statistics"),wxDefaultPosition, wxSize(140,35) );
2000                 statisticsContourBtn->SetEventHandler( this );
2001                 Connect( statisticsContourBtn->GetId(),         wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onExtractInformation ); 
2002
2003                 wxButton *saveResultsBtn                                                = new wxButton(panel,-1,_T("Save statistics results"),wxDefaultPosition, wxSize(140,35) );
2004                 saveResultsBtn->SetEventHandler( this );
2005                 Connect( saveResultsBtn->GetId(),               wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSaveResults ); 
2006
2007
2008                 int sizeZ = this->_theViewPanel->getSceneManager()->GetImageDataSizeZ();                        
2009                 _mbarrangeSliceInformation                                      =  new mBarRange(panel,65,65);
2010                 _mbarrangeSliceInformation->SetMin(0);
2011                 _mbarrangeSliceInformation->SetStart(0);
2012                 _mbarrangeSliceInformation-> SetOrientation( true );
2013                 _mbarrangeSliceInformation-> setActiveStateTo(true);
2014                 _mbarrangeSliceInformation-> setVisibleLabels( true );
2015                 _mbarrangeSliceInformation-> setDeviceEndMargin(10);
2016                 _mbarrangeSliceInformation-> setRepresentedValues( 0 , sizeZ-1 );
2017                 _mbarrangeSliceInformation-> setDeviceBlitStart(10,10); 
2018                 _mbarrangeSliceInformation-> setIfWithActualDrawed( false );
2019                 _mbarrangeSliceInformation-> SetStart( 0 );
2020                 _mbarrangeSliceInformation-> SetEnd( sizeZ-1 );  
2021
2022
2023                 double range[2];
2024                 this->_theViewPanel->getSceneManager()->GetImageDataRange(range);
2025                 _mbarrangeRangeInformation                                      =  new mBarRange(panel,65,65);
2026                 _mbarrangeRangeInformation->SetMin(0);
2027                 _mbarrangeRangeInformation->SetStart(0);
2028                 _mbarrangeRangeInformation-> SetOrientation( true );
2029                 _mbarrangeRangeInformation-> setActiveStateTo(true);
2030                 _mbarrangeRangeInformation-> setVisibleLabels( true );
2031                 _mbarrangeRangeInformation-> setDeviceEndMargin(10);
2032                 _mbarrangeRangeInformation-> setRepresentedValues( range[0] , range[1] );
2033                 _mbarrangeRangeInformation-> setDeviceBlitStart(10,10); 
2034                 _mbarrangeRangeInformation-> setIfWithActualDrawed( false );
2035                 _mbarrangeRangeInformation-> SetStart( range[0] );
2036                 _mbarrangeRangeInformation-> SetEnd( range[1] );  
2037
2038
2039                 _staticTextInformation                                                  = new wxStaticText(panel,-1,_T("    "));
2040
2041
2042 //                      wxButton *XXXXXXXBtn                    = new wxButton(panel,-1,_T("Statistics"),wxDefaultPosition, wxSize(180,35) );
2043 //                      XXXXXXXBtn->SetEventHandler( this );
2044 //                      Connect( XXXXXXXBtn->GetId(),           wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onXXXXX ); 
2045
2046
2047                 _grid = new wxGrid( panel,
2048                     wxID_ANY,
2049                     wxPoint( 0, 0 ),
2050                     wxSize( 100, 100 ) );
2051
2052                 int i,gridCol=10,gridRow=sizeZ;
2053                 _grid->CreateGrid( 0, 0 );
2054                 _grid->AppendRows(gridRow);
2055                 _grid->AppendCols(gridCol);
2056
2057                 for (i=0;i<gridRow;i++)
2058                 {
2059                         _grid->SetRowLabelValue(i, _T(" ") );
2060                 }
2061 //                      _grid->SetColLabelSize(0);
2062
2063
2064                 FillGridWithContoursInformation();
2065
2066                 wxFlexGridSizer * sizerA                = new wxFlexGridSizer(10);
2067                 sizerA->Add( _informationRadiobox                       , 1, wxALL ,2 );
2068                 sizerA->Add( _radiolstboxContourGroup           , 1, wxALL ,2 );
2069
2070                 wxFlexGridSizer * sizerB                = new wxFlexGridSizer(10);
2071                 sizerB->Add( informationContourLabelsBtn        , 1, wxALL ,2 );
2072                 sizerB->Add( statisticsContourBtn                       , 1, wxALL ,2 );
2073                 sizerB->Add( saveResultsBtn                                     , 1, wxALL ,2 );
2074
2075                 wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
2076                 sizer->AddGrowableCol(0);
2077                 sizer->AddGrowableRow(7);
2078                 sizer->Add( sizerA                                              , 1, wxALL              , 0 );
2079                 sizer->Add( new wxStaticText(panel              ,-1,_T("Slice Range"))  , 1, wxALL              , 0 );
2080                 sizer->Add( _mbarrangeSliceInformation  , 1, wxALL|wxGROW               , 2 );                  
2081                 sizer->Add( new wxStaticText(panel              ,-1,_T("Gray Range"))   , 1, wxALL              , 0 );
2082                 sizer->Add( _mbarrangeRangeInformation  , 1, wxALL|wxGROW               , 2 );                  
2083                 sizer->Add( sizerB                                              , 1, wxALL              , 0 );
2084                 sizer->Add( _staticTextInformation              , 1, wxALL              , 0 );
2085                 sizer->Add( _grid                                               , 1, wxEXPAND   , 0 );
2086
2087
2088
2089                 panel->SetSizer( sizer );
2090                 panel->SetSize( wxSize(1500,1500) );
2091                 //panel->SetBackgroundColour( wxColour(100,100,100) );
2092                 panel->SetAutoLayout( true );
2093                 panel->Layout();
2094                 return panel;
2095         }
2096         //------------------------------------------------------------------------------------------------------------
2097         void wxContourEventHandler :: onSpreadInDepth( std::vector<std::string> & keyNamesVector )
2098         {
2099                 
2100         }
2101         //------------------------------------------------------------------------------------------------------------
2102         void wxContourEventHandler :: onOutline_Union_Of( std::vector<std::string> & keyNamesVector )
2103         {
2104                 
2105         }
2106         //------------------------------------------------------------------------------------------------------------
2107         void wxContourEventHandler :: onOutline_Intersection_Of( std::vector<std::string> & keyNamesVector )
2108         {
2109                 
2110         }
2111         //------------------------------------------------------------------------------------------------------------
2112         void wxContourEventHandler :: onOutline_Combination_Of( std::vector<std::string> & keyNamesVector )
2113         {
2114                 
2115         }
2116         //------------------------------------------------------------------------------------------------------------
2117         void wxContourEventHandler :: onOutline_Fragmentation_Of( std::vector<std::string> & keyNamesVector )
2118         {
2119                 
2120         }
2121         //------------------------------------------------------------------------------------------------------------
2122         void wxContourEventHandler :: onOutline_Agrupation_Of( std::vector<std::string> & keyNamesVector )
2123         {
2124                 
2125         }
2126         //------------------------------------------------------------------------------------------------------------
2127         void wxContourEventHandler :: onCopyOutlines( std::vector<std::string> & keyNamesVector )
2128         {
2129                 std::vector<int> tempVector;
2130                 _instantPanel->getInstant( tempVector );
2131         _performingOperation->reset();
2132                 _performingOperation->setStartCommand( (char)wxContour_ActionCommandsID::COPY_TOOL );
2133                 _performingOperation->setStartOperationInstantVector( tempVector );
2134                 _performingOperation->setKeyNamesOperationElems( keyNamesVector );
2135
2136         }
2137         //------------------------------------------------------------------------------------------------------------
2138         void wxContourEventHandler :: onPasteOutlines( )
2139         {
2140                 char theStartCommand = _performingOperation->getStartCommand();
2141                 if (  theStartCommand == wxContour_ActionCommandsID::COPY_TOOL )
2142                 {
2143                         //JCP 20-10-08 Undo redo implementation
2144                         saveState();
2145                         //JCP 20-10-08 Undo redo implementation
2146
2147
2148                         std::vector<int> tempVector;
2149                         _instantPanel->getInstant( tempVector );
2150                         _performingOperation->setEndOperationInstantVector ( tempVector );
2151                         std::vector<std::string> elems = _performingOperation->getKeyNamesOperationElems();
2152                         int i,size = elems.size();                      
2153                         for( i=0; i<size; i++ )
2154                         {                               
2155                                 createCopyContourOf( elems[i], tempVector, i>0 );
2156                         }
2157
2158                         
2159                 }
2160                 int fin = 0;
2161         }
2162         //------------------------------------------------------------------------------------------------------------
2163         void wxContourEventHandler :: onShowOutlines( std::vector<std::string> & keyNamesVector )
2164         {
2165                 
2166         }
2167         //------------------------------------------------------------------------------------------------------------
2168         void wxContourEventHandler :: onHideOutlines( std::vector<std::string> & keyNamesVector )
2169         {
2170                 
2171         }
2172         //------------------------------------------------------------------------------------------------------------
2173         void wxContourEventHandler :: onSelectOutlines( std::vector<std::string> & keyNamesVector )
2174         {
2175                 //if( keyNamesVector.empty() )
2176                 {
2177                         _theViewPanel->getSceneManager()->drawSelectionROI( );
2178                 }
2179                 /*else
2180                 {
2181                         int ok = keyNamesVector.size();
2182                         _theViewPanel->getSceneManager()->selectObjects( keyNamesVector );
2183                         std::string name = keyNamesVector[0];                   
2184                 }*/
2185                 /*
2186                 _theViewPanel->getSceneManager()->drawSelectionROI( );
2187                 std::vector<std::string> currentSelection = _theViewPanel->getSceneManager()->getSelectedObjects();
2188                 int ok = currentSelection.size();
2189                 _theViewPanel->getSceneManager()->selectObjects( currentSelection );*/
2190         }
2191         //------------------------------------------------------------------------------------------------------------
2192         void wxContourEventHandler :: onEditOutlines( std::vector<std::string> & keyNamesVector )
2193         {
2194                 
2195         }
2196         //------------------------------------------------------------------------------------------------------------
2197         
2198         //------------------------------------------------------------------------------------------------------------
2199         void wxContourEventHandler :: deleteContour( std::string theKeyName )
2200         {
2201                 manualContourModel                      * cModel;
2202                 manualViewBaseContour           * cViewer;
2203                 manualContourBaseControler      * cControler;
2204
2205                 ContourWrap_ViewControl *conwrapviewControl =  _theViewPanel->getSceneManager()->getContourWrap_ViewControlOf( theKeyName );
2206                 cControler      = conwrapviewControl->getControler();
2207                 int ispartofstaticlst= _modelManager->IsPartOfStaticList(theKeyName);
2208                 if ((cControler!=NULL) && (cControler->IsEditable()==false)  && (ispartofstaticlst==-1) )
2209                 {
2210                         _theViewPanel->getSceneManager()->removeFromScene( theKeyName );
2211                         cModel          = _modelManager->getOutlineByKeyName(theKeyName)->getModel();
2212                         _modelManager->removeOutline( theKeyName );
2213                         cViewer         = conwrapviewControl->getViewer();
2214                         _theViewPanel->getSceneManager()->removeWrap( theKeyName );
2215 //EED Borrame
2216 //FILE *ff = fopen("c:/temp/wxVtkBaseView_SceneManagerStadistics.txt","a+");
2217 //fprintf(ff,"EED wxContourEventHandler::deleteContours() \n" );
2218 //fprintf(ff,"    %s %p\n",keyNamesVector[i].c_str(),  cControler );
2219 //fclose(ff);
2220                         delete cModel;
2221                         delete cViewer;
2222                         delete cControler;
2223                 } // if editable
2224         }
2225
2226         //------------------------------------------------------------------------------------------------------------
2227         void wxContourEventHandler :: deleteContours( std::vector<std::string>  keyNamesVector )
2228         {
2229                 int i,size=keyNamesVector.size();
2230                 for (i=0;i<size;i++)
2231                 {
2232                         deleteContour( keyNamesVector[i] );
2233                 }
2234         }
2235
2236         //------------------------------------------------------------------------------------------------------------
2237         void wxContourEventHandler :: deleteAllContours(  )
2238         {               
2239                 wxBusyCursor wait;
2240                 std::vector<int> tempVector;
2241                 _instantPanel->getInstant( tempVector );
2242
2243 //JCP --08-09-2008 When using a diferent interface the _mbarrangeDeleteAll might not be initialize
2244 //              the values in GetStart and GetEnd will then not be initialize also.
2245 //              We use instead the values given when initializing the _deletepanel.
2246
2247                 //int minZ = _mbarrangeDeleteAll->GetStart();
2248                 //int maxZ = _mbarrangeDeleteAll->GetEnd();
2249                 int minZ, maxZ;
2250                 
2251                 minZ = 0;
2252                 maxZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
2253                 
2254 //JCP --08-09-2008
2255                 
2256                 _theViewPanel->getSceneManager()->removeSceneContours( );
2257                         _modelManager->removeAllOutlines();
2258                         _theViewPanel->getSceneManager()->removeAllOutlines();
2259 //JCP --08-09-2008
2260                 /*
2261                 if ( (minZ==0) && (maxZ==_mbarrangeDeleteAll->GetMax() ))
2262                 {
2263                         _theViewPanel->getSceneManager()->removeSceneContours( );
2264                         _modelManager->removeAllOutlines();
2265                         _theViewPanel->getSceneManager()->removeAllOutlines();
2266
2267                 } else {
2268                         for ( z=minZ ; z<=maxZ ; z++)
2269                         {
2270                                 tempVector[1]=z;
2271                                 Instant instant(&tempVector);
2272                                 std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
2273
2274                                 sizeLstContourThings = lstContourThings.size();
2275                                 for (ii=0 ; ii<sizeLstContourThings ; ii++)
2276                                 {
2277                                         ContourThing **contourthing = lstContourThings[ii];
2278                                         deleteContour( (*contourthing)->getName() );
2279                                 } //for ii
2280                         }// for z
2281                 } // if 
2282         JCP --08-09-2008 */
2283         }
2284
2285         //------------------------------------------------------------------------------------------------------------
2286         void wxContourEventHandler::openContours( FILE *pFile, bool staticContour )
2287         {
2288                 char tmp[255];
2289                 fscanf(pFile,"%s",tmp); // NumberOfContours 
2290                 fscanf(pFile,"%s",tmp); // ##
2291                 int numberOfContours = atoi(tmp);  
2292
2293                 std::vector<int> instantVector;
2294                 int typeContourModel;
2295                 manualContourModel *manModelContour;
2296                 int typeView;
2297
2298                 int i;
2299                 for (i=0;i<numberOfContours;i++)
2300                 {
2301                         instantVector.clear();
2302                         fscanf(pFile,"%s",tmp); // Instant
2303
2304                         fscanf(pFile,"%s",tmp); // 1
2305                         instantVector.push_back( atoi(tmp) );
2306                         fscanf(pFile,"%s",tmp); // 2
2307                         instantVector.push_back( atoi(tmp) );
2308                         fscanf(pFile,"%s",tmp); // 3
2309                         instantVector.push_back( atoi(tmp) );
2310                         fscanf(pFile,"%s",tmp); // 4
2311                         instantVector.push_back( atoi(tmp) );
2312                         fscanf(pFile,"%s",tmp); // 5
2313                         instantVector.push_back( atoi(tmp) );
2314                         fscanf(pFile,"%s",tmp); // 6
2315                         instantVector.push_back( atoi(tmp) );
2316
2317
2318                         fscanf(pFile,"%s",tmp); // TypeContourModel
2319                         fscanf(pFile,"%s",tmp); // ##
2320                         typeContourModel = atoi(tmp);
2321
2322                         manModelContour =  factoryManualContourModel(typeContourModel);
2323                         manModelContour->Open(pFile);
2324
2325                         fscanf(pFile,"%s",tmp); // TypeView
2326                         fscanf(pFile,"%s",tmp); // ##
2327                         typeView = atoi(tmp);  
2328
2329
2330 //                      if (typeView==1) 
2331 //                      {
2332 //                      }
2333
2334
2335                         std::string theName;
2336                         theName = _modelManager->createOutline( manModelContour, instantVector );
2337                         bool addedModel = theName.compare("") != 0;
2338                         if( addedModel )
2339                         {
2340                                 double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
2341                                 this->_theViewPanel->getSpacing(spc);                                   
2342                                 //Adding the manualContourControler to interface objects structure
2343                                 //Adding the manualViewContour to interface objects structure           
2344                                 //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
2345                                 _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc , typeView) ;
2346                         }       
2347
2348                         if (staticContour==true)
2349                         {
2350                                 Instant instant(&instantVector);
2351                                 changeContourOfManager( theName , &instant );
2352                         }
2353
2354                 }// for  numberOfContours
2355
2356
2357
2358         }
2359
2360         //------------------------------------------------------------------------------------------------------------
2361         void wxContourEventHandler::openFileWithContours()
2362         {
2363                 char tmp[255];
2364                 wxFileDialog dialog(NULL, _T("Choose a file"), _T(""), _T(""), _T("*.roi"), wxOPEN );
2365                 if (dialog.ShowModal() == wxID_OK)
2366                 {
2367                         fileNameContourROI = (const char *)(dialog.GetPath().mb_str());
2368                         FILE *pFile=fopen(fileNameContourROI.c_str(),"r+");
2369
2370                         fscanf(pFile,"%s",tmp); // --CreaContour--
2371
2372                         fscanf(pFile,"%s",tmp); // Version
2373                         fscanf(pFile,"%s",tmp); // 1.0.1
2374                         std::string version(tmp);
2375
2376                         openContours(pFile,false);
2377                         if (version!="1.0.0"){
2378                                 openContours(pFile,true);
2379                         }
2380 //                        _theViewPanel->getSceneManager()->openFileWithContours(ff);
2381
2382                         fclose(pFile);
2383                 }
2384                 _theViewPanel->getSceneManager()->removeSceneContours();
2385                 changeInstant();
2386                 //updateInstantOutlines();
2387         }
2388
2389
2390         //------------------------------------------------------------------------------------------------------------
2391         void wxContourEventHandler::saveFileWithContours( std::string filename )
2392         {
2393                 FILE *pFile=fopen(filename.c_str(),"w+");
2394                 std::vector< std::string > lstNameThings;
2395                 int i,sizeLstNameThings; 
2396
2397                 fprintf(pFile,"--CreaContour--\n");
2398                 fprintf(pFile,"Version %s\n", "1.0.1" );
2399
2400                 // Normal Contours
2401                 lstNameThings           = _modelManager->GetLstNameThings();
2402                 sizeLstNameThings       = lstNameThings.size(); 
2403                 fprintf(pFile,"NumberOfContours %d\n", sizeLstNameThings );
2404                 for (i=0 ; i<sizeLstNameThings ; i++) 
2405                 {
2406                         _modelManager->SaveThingName( pFile, lstNameThings[i] );
2407                         _theViewPanel->getSceneManager()->SaveThingName( pFile, lstNameThings[i] );
2408                 }// for i
2409
2410                 //-- Contours Statics 
2411                 lstNameThings           = _modelManager->GetLstNameThingsStatic();
2412                 sizeLstNameThings       = lstNameThings.size(); 
2413                 fprintf(pFile,"NumberOfContoursStatic %d\n", sizeLstNameThings );
2414                 for (i=0 ; i<sizeLstNameThings ; i++) 
2415                 {
2416                         _modelManager->SaveThingName( pFile, lstNameThings[i] );
2417                         _theViewPanel->getSceneManager()->SaveThingName( pFile, lstNameThings[i] );
2418                 }// for i
2419                 fclose(pFile);
2420         }
2421
2422
2423         //----------------------------------------------------------
2424
2425         void wxContourEventHandler::saveFileWithContours()
2426         {
2427                 wxFileDialog dialog(NULL, _T("Choose a file"), _T(""), _T(""), _T("*.roi"), wxSAVE );
2428                 if (dialog.ShowModal() == wxID_OK)
2429                 {
2430                         fileNameContourROI = (const char *)(dialog.GetPath().mb_str());
2431                         saveFileWithContours( fileNameContourROI );
2432                 } // if  ShowModal
2433         } 
2434
2435         //------------------------------------------------------------------------------------------------------------
2436         void wxContourEventHandler :: saveFileWithContoursAutomatique()
2437         {
2438                 if (fileNameContourROI=="")
2439                 {
2440                         saveFileWithContours();
2441                 } else {
2442                         saveFileWithContours(fileNameContourROI);
2443                 }
2444         }
2445
2446
2447         //------------------------------------------------------------------------------------------------------------
2448         void wxContourEventHandler :: createCopyContourOf ( std::string anExistingKName, std::vector<int> &instantNoTouchData, bool append )
2449         {
2450                 std::string cloneName = _modelManager->createCopyContourOf( anExistingKName, instantNoTouchData );
2451                 manualContourModel * manualModel = _modelManager->getOutlineByKeyName( cloneName )->getModel();
2452                 _theViewPanel->getSceneManager()->createCopyContourOf( anExistingKName, cloneName, manualModel , append );
2453         }
2454
2455
2456         //------------------------------------------------------------------------------------------------------------
2457         manualContourModel * wxContourEventHandler::factoryManualContourModel(int typeContour)
2458         {       
2459                 manualContourModel *manModelContour=NULL;
2460
2461                 // spline
2462                 if (typeContour==0)
2463                 {
2464                         manModelContour = new manualContourModel();
2465                 }
2466
2467                 // spline
2468                 if (typeContour==1)
2469                 {
2470                         manModelContour = new manualContourModel();
2471                 }
2472
2473                 // rectangle
2474                 if (typeContour==2)
2475                 {
2476                         manModelContour = new manualContourModelRoi();
2477                 }
2478
2479                 // circle
2480                 if (typeContour==3)
2481                 {
2482                         manModelContour = new manualContourModelCircle();
2483                 }
2484
2485                 // BullEye / star
2486                 if (typeContour==4)
2487                 {
2488                         manualContourModelBullEye *manModelContourBullEye = new manualContourModelBullEye();
2489                         manModelContour = manModelContourBullEye;
2490                         if (_panelBullEyeOptions!=NULL){
2491                                 int iCrown,sizeCrowns,iSector,sizeSectors;
2492                                 double radioA,radioB,ang,angDelta ;
2493                                 sizeCrowns = _panelBullEyeOptions->GetNumberOfCrowns();
2494                                 for ( iCrown=0 ; iCrown<sizeCrowns ; iCrown++ )
2495                                 {
2496                                         sizeSectors = _panelBullEyeOptions->GetNumberOfSections(iCrown);
2497                                         radioB  = _panelBullEyeOptions->GetRadioOfCrown(iCrown);
2498                                         if (iCrown==sizeCrowns-1)
2499                                         {
2500                                                 radioA  = 0;
2501                                         } else {
2502                                                 radioA  = _panelBullEyeOptions->GetRadioOfCrown(iCrown+1);
2503                                         }
2504                                         radioA=radioA/100.0;
2505                                         radioB=radioB/100.0;
2506                                         for ( iSector=0 ; iSector<sizeSectors  ; iSector++ )
2507                                         {
2508                                                 ang             = _panelBullEyeOptions->GetAngOfCrownSection(iCrown,iSector);
2509                                                 angDelta= _panelBullEyeOptions->GetAngDeltaOfCrownSection(iCrown);                                      
2510                                                 manModelContourBullEye->AddSector(radioA,radioB,ang,angDelta);
2511                                         } // for iSector
2512                                 } // for iCrown
2513                         } // if _panelBullEyeOptions
2514                 }// if typeContour==4
2515
2516                 return manModelContour;
2517         }
2518
2519         //------------------------------------------------------------------------------------------------------------
2520         void wxContourEventHandler :: createContour( int typeContour )
2521         {
2522                 //Creating the manualContourModel and including in the model
2523                 manualContourModel * manModelContour = factoryManualContourModel(typeContour);
2524                 std::vector<int> instantVector;
2525                 _instantPanel->getInstant( instantVector );
2526                 std::string theName;
2527                 theName= _modelManager->createOutline( manModelContour, instantVector );
2528                 bool addedModel = theName.compare("") != 0;//??
2529                 if( addedModel )
2530                 {
2531                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
2532                         this->_theViewPanel->getSpacing(spc);
2533                         //Adding the manualContourControler to interface objects structure
2534                         //Adding the manualViewContour to interface objects structure           
2535                         //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
2536                         _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc , typeContour) ;
2537                 }       
2538
2539 //EED Borrame
2540 //FILE *ff = fopen("c:/temp/wxVtkBaseView_SceneManagerStadistics.txt","a+");
2541 //fprintf(ff,"EED wxContourEventHandler::createContour() \n" );
2542 //fprintf(ff,"    %s\n %p ",theName.c_str() ,  );
2543 //fclose(ff);
2544
2545
2546         }
2547         manualContourModel * wxContourEventHandler::factoryManualContourModel(PanelBullEyeOptions* panel){
2548
2549                 manualContourModelBullEye *manModelContourBullEye = new manualContourModelBullEye();
2550                 manualContourModel *manModelContour=NULL;
2551                 manModelContour = manModelContourBullEye;
2552                 if (panel!=NULL){
2553                         int iCrown,sizeCrowns,iSector,sizeSectors;
2554                         double radioA,radioB,ang,angDelta ;
2555                         sizeCrowns = panel->GetNumberOfCrowns();
2556                         for ( iCrown=0 ; iCrown<sizeCrowns ; iCrown++ )
2557                         {
2558                                 sizeSectors = panel->GetNumberOfSections(iCrown);
2559                                 radioB  = panel->GetRadioOfCrown(iCrown);
2560                                 if (iCrown==sizeCrowns-1)
2561                                 {
2562                                         radioA  = 0;
2563                                 } else {
2564                                         radioA  = panel->GetRadioOfCrown(iCrown+1);
2565                                 }
2566                                 radioA=radioA/100.0;
2567                                 radioB=radioB/100.0;
2568                                 for ( iSector=0 ; iSector<sizeSectors  ; iSector++ )
2569                                 {
2570                                         ang             = panel->GetAngOfCrownSection(iCrown,iSector);
2571                                         angDelta= panel->GetAngDeltaOfCrownSection(iCrown);                                     
2572                                         manModelContourBullEye->AddSector(radioA,radioB,ang,angDelta);
2573                                 } // for iSector
2574                         } // for iCrown
2575                 } // if _panelBullEyeOptions
2576                 
2577                 return manModelContour;
2578         }
2579
2580         void wxContourEventHandler :: createContourBullsEye(PanelBullEyeOptions* panel )
2581         {
2582                 //Creating the manualContourModel and including in the model
2583                 manualContourModel * manModelContour = factoryManualContourModel(panel );
2584                 std::vector<int> instantVector;
2585                 _instantPanel->getInstant( instantVector );
2586                 std::string theName;
2587                 theName= _modelManager->createOutline( manModelContour, instantVector );
2588                 bool addedModel = theName.compare("") != 0;
2589                 if( addedModel )
2590                 {
2591                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
2592                         this->_theViewPanel->getSpacing(spc);                                   
2593                         //Adding the manualContourControler to interface objects structure
2594                         //Adding the manualViewContour to interface objects structure           
2595                         //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
2596                         _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc , 4) ;
2597                 }       
2598
2599 //EED Borrame
2600 //FILE *ff = fopen("c:/temp/wxVtkBaseView_SceneManagerStadistics.txt","a+");
2601 //fprintf(ff,"EED wxContourEventHandler::createContour() \n" );
2602 //fprintf(ff,"    %s\n %p ",theName.c_str() ,  );
2603 //fclose(ff);
2604
2605
2606         }
2607
2608         //------------------------------------------------------------------------------------------------------------
2609         //  Attributes getters and setters
2610         //------------------------------------------------------------------------------------------------------------
2611
2612         void wxContourEventHandler :: setModelManager( OutlineModelManager * theModelManager )
2613         {
2614                 _modelManager = theModelManager;
2615                 _workSpace = _modelManager->getContourWorkspace();
2616         }
2617         //------------------------------------------------------------------------------------------------------------  
2618         void wxContourEventHandler :: setViewPanel(  wxContourViewPanel * theViewPanel )
2619         {
2620                 _theViewPanel = theViewPanel;
2621                 //_theViewPanel->setWxEventHandler( this );
2622
2623                 /*double spc[3];
2624                 this->_theViewPanel->getSpacing(spc);
2625                 _sceneManager           = new wxVtkBaseView_SceneManager ( this->_theViewPanel->getWxVtkBaseView(), this, spc  );
2626                 
2627                 Connect( _theViewPanel->GetId(), wxINSTANT_CHANGE, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onChangeInstant );
2628         Connect( wxID_ANY, wxEVT_START_CREATE_MULT_ROI, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onCreateMultipleROI );
2629                 Connect( wxID_ANY, wxEVT_START_CREATE_ROI, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onCreateROI );
2630                 Connect( wxID_ANY, wxEVT_STOP_CREATE_ROI, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onStopCreateROI );
2631                 Connect( wxID_ANY, wxEVT_CHANGED_DEEP, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onChangedDeep );
2632                 Connect( wxID_ANY,  wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler :: onActionButtonPressed ); */
2633
2634                 
2635         }
2636
2637         //------------------------------------------------------------------------------------------------------------
2638         void wxContourEventHandler :: setInstantChooserPanel(  wxInstantChooserPanel * theInstantChooserPanel )
2639         {
2640                 _instantPanel = theInstantChooserPanel;
2641                 //_instantPanel->setWxEventHandler( this );
2642
2643                 //Connect( _instantPanel->GetId(), wxINSTANT_CHOOSER_CHANGE, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onChangeInstant );
2644
2645                 std::vector<int> vect;
2646                 _instantPanel->getInstant( vect );
2647                 _actualInstant = new Instant( &vect );
2648
2649                 if( _modelManager!=NULL )
2650                 {               
2651                         std::vector<std::string> conceptNameVect;
2652                         std::vector<int> conceptSizeVect;
2653                         _modelManager-> getConceptsInformation(conceptNameVect, conceptSizeVect);
2654                         int i=0;
2655                         int max = conceptNameVect.size();                       
2656                         for( ; i<max; i++)
2657                         {
2658                                 if( conceptNameVect[i].compare("Axe Depth") == 0 )
2659                                 {
2660                                         _instantPanel->addConcept( conceptNameVect[i], 0, conceptSizeVect[i], 1);                                       
2661                                 } else {
2662                                         _instantPanel->addConcept( conceptNameVect[i], 1, conceptSizeVect[i], 1);
2663                                 } // if
2664                         } // for                
2665                 } else {
2666                         //Should be configured later, or abort program, because Model -NEEDS- to be added before chooserPanel
2667                 }
2668                 if( _theViewPanel!=NULL )
2669                 {
2670                         double val = _theViewPanel->getCurrentDeep();
2671                         _instantPanel->setConceptValue( "Axe Depth", (int)val );
2672                         //JCP _instantPanel->setConceptValue("time", num de imagenes);
2673                         ConceptDataWrap * data = _instantPanel->getConceptDataOf( "Axe Depth" );
2674                         changeInstant();                        
2675                         _theViewPanel->setVerticalConcept( "Axe Depth", data->getMinValue(), data->getMaxValue(), data->getMinShowedValue(),  data->getMaxShowedValue(), data->getActualValue() );                      
2676                 } else {
2677                         //Should be configured later, or abort program, because ViewPanel -NEEDS- to be added before chooserPanel
2678                 }// if
2679         } 
2680
2681
2682         //------------------------------------------------------------------------------------------------------------
2683         void wxContourEventHandler :: setButtonsBar(  wxContour_ButtonsBar * theButtonsBar )
2684         {
2685                 _buttonsBar = theButtonsBar;
2686                 _buttonsBar->setWxEventHandler( this );
2687                 Connect( _buttonsBar->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler:: onActionButtonPressed );               
2688         }
2689         //------------------------------------------------------------------------------------------------------------
2690         void wxContourEventHandler :: setGrid(  wxContour_Grid * theGridPanel )
2691         {
2692                 _gridPanel = theGridPanel;
2693         }
2694         //------------------------------------------------------------------------------------------------------------
2695         void wxContourEventHandler :: setDrawToolsPanel(  wxContour_DrawToolsPanel * theDrawToolsPanel )
2696         {
2697                 _drawToolsPanel = theDrawToolsPanel;
2698         }
2699         //------------------------------------------------------------------------------------------------------------
2700         void wxContourEventHandler :: setOperationsToolsPanel(  wxContour_OperationsToolsPanel * theOperationsToolsPanel )
2701         {       
2702                 _operationsToolsPanel = theOperationsToolsPanel;
2703         }
2704         //------------------------------------------------------------------------------------------------------------
2705         void wxContourEventHandler :: setAutomaticFormsPanel(  wxContour_AutomaticFormsToolsPanel * theAutoFormsPanel )
2706         {
2707                 _autoFormsPanel = theAutoFormsPanel;
2708         }
2709
2710         void wxContourEventHandler :: setStandardToolsPanel(  wxContour_StandardToolsPanel * theStandardToolsPanel )
2711         {
2712                 _standardToolsPanel= theStandardToolsPanel;
2713         }
2714         //------------------------------------------------------------------------------------------------------------
2715         void wxContourEventHandler :: setEditionToolsPanel(  wxContour_EdtionToolsPanel * theEditionToolsPanel )
2716         {
2717                 _editionToolsPanel =  theEditionToolsPanel;
2718         }
2719
2720         //------------------------------------------------------------------------------------------------------------
2721         void wxContourEventHandler :: setListViewPanel(  wxContour_ListViewPanel * theListViewPanel )
2722         {
2723                 _listViewPanel = theListViewPanel;
2724         }
2725         //------------------------------------------------------------------------------------------------------------
2726         //  Other functional methods
2727         //------------------------------------------------------------------------------------------------------------
2728
2729
2730         //------------------------------------------------------------------------------------------------------------
2731         void wxContourEventHandler::onDeleteContour( wxCommandEvent& event )
2732         {
2733                 
2734                 //JCP 20-10-08 Undo redo implementation
2735                 saveState();
2736                 //JCP 20-10-08 Undo redo implementation
2737
2738
2739                 int i,size=_theViewPanel->getSceneManager()->getSelectedObjects().size();
2740                 for(i=0;i<size;i++)
2741                 {
2742                         std::string keyName             = _theViewPanel->getSceneManager()->getSelectedObjects()[i];
2743                         int ispartofstaticlist  = this->_modelManager->IsPartOfStaticList( keyName );
2744                         if ( ispartofstaticlist>=0 )
2745                         {
2746                                 std::vector<int> tempVector;
2747                                 _instantPanel->getInstant( tempVector );
2748                                 Instant instant(&tempVector);
2749                                 this->_modelManager->ChangeContourOfList(keyName, &instant);
2750                         }
2751                 }
2752
2753                 std::vector<std::string> lstKeyName;
2754                 std::vector<std::string> lstKeyNameActualSlice;
2755                 std::vector<std::string> lstKeyNameToBeErase;
2756
2757                 lstKeyNameToBeErase             = _theViewPanel->getSceneManager()->getSelectedObjects();
2758                 lstKeyNameActualSlice   = _theViewPanel->getSceneManager()->GetlstContoursNameActualSlice();
2759                 int k,kSize=lstKeyNameToBeErase.size();
2760                 int j,jSize=lstKeyNameActualSlice.size();
2761                 bool ok;
2762                 for (k=0;k<kSize; k++)
2763                 {
2764                         ok=false;
2765                         for (j=0;j<jSize; j++)
2766                         {
2767                                 if (lstKeyNameToBeErase[k]==lstKeyNameActualSlice[j])
2768                                 { 
2769                                         ok=true;
2770                                 }
2771                         } // for j
2772                         if (ok==true)
2773                         {
2774                                 lstKeyName.push_back( lstKeyNameToBeErase[k] );
2775                         } // if ok
2776                 } // for k
2777
2778                 deleteContours( lstKeyName );
2779
2780                 
2781         }
2782
2783         //------------------------------------------------------------------------------------------------------------
2784         void wxContourEventHandler::onDeleteContoursActSlice( wxCommandEvent& event )
2785         {
2786                 //JCP 20-10-08 Undo redo implementation
2787                 saveState();
2788                 //JCP 20-10-08 Undo redo implementation
2789                 deleteContours( (std::vector<std::string>)_theViewPanel->getSceneManager()->GetlstContoursNameActualSlice() );
2790
2791                 
2792         }
2793
2794         //------------------------------------------------------------------------------------------------------------
2795         void wxContourEventHandler::onDeleteAllContours( wxCommandEvent& event )        
2796         {
2797                 //JCP 20-10-08 Undo redo implementation
2798                 saveState();
2799                 //JCP 20-10-08 Undo redo implementation
2800                 deleteAllContours(  );
2801
2802                 
2803         }
2804
2805                 //------------------------------------------------------------------------------------------------------------
2806         void wxContourEventHandler :: onDeleteFrame( )
2807         {
2808                 if (_deleteFrame==NULL)
2809                 {
2810                         _deleteFrame                                            = new wxFrame (_theViewPanel, -1,_T("  Delete Contour "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
2811                         int width=300;
2812                         wxSize sizepanel(width,230);
2813                         _deleteFrame->SetSize( sizepanel );
2814                         wxPanel *panel                                          = new wxPanel(_deleteFrame,-1);
2815                         wxButton *deleteContourBtn                      = new wxButton(panel,-1,_T("Delete contour selected"),wxDefaultPosition, wxSize(width-10,35) );
2816                         wxButton *deleteContoursActSliceBtn     = new wxButton(panel,-1,_T("Delete contours of actual slice"),wxDefaultPosition, wxSize(width-10,35));
2817                         wxButton *deleteAllContoursBtn          = new wxButton(panel,-1,_T("Delete all contours"),wxDefaultPosition, wxSize(width-10,35));
2818
2819                         int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
2820                         _mbarrangeDeleteAll                                     =  new mBarRange(panel,70, 65);
2821                         _mbarrangeDeleteAll->SetMin(0);
2822                         _mbarrangeDeleteAll->SetStart(0);
2823
2824                         _mbarrangeDeleteAll-> SetOrientation( true );
2825                         _mbarrangeDeleteAll-> setActiveStateTo(true);
2826                         _mbarrangeDeleteAll-> setVisibleLabels( true );
2827                         _mbarrangeDeleteAll-> setDeviceEndMargin(10);
2828                         _mbarrangeDeleteAll-> setRepresentedValues( 0 , sizeZ );
2829                         _mbarrangeDeleteAll-> setDeviceBlitStart(10,10); 
2830                         _mbarrangeDeleteAll-> setIfWithActualDrawed( false );
2831                         _mbarrangeDeleteAll-> SetStart( 0 );
2832                         _mbarrangeDeleteAll-> SetEnd( sizeZ );  
2833
2834
2835                         _deleteFrame->SetEventHandler( this );
2836                         Connect( deleteContourBtn->GetId()                      ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onDeleteContour ); 
2837                         Connect( deleteContoursActSliceBtn->GetId()     ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onDeleteContoursActSlice ); 
2838                         Connect( deleteAllContoursBtn->GetId()          ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onDeleteAllContours ); 
2839
2840                         wxFlexGridSizer * sizer                         = new wxFlexGridSizer(1);
2841                         sizer -> Add( deleteContourBtn                          , 1, wxGROW );
2842                         sizer -> Add( deleteContoursActSliceBtn         , 1, wxGROW );
2843                         sizer -> Add( deleteAllContoursBtn                      , 1, wxGROW );
2844                         sizer -> Add( _mbarrangeDeleteAll                       , 1, wxGROW );
2845
2846                         panel->SetSizer( sizer );
2847                         panel->SetSize( sizepanel );
2848                         panel->SetAutoLayout( true );
2849                         panel->Layout();
2850                         _deleteFrame->Show(true);
2851                 } else {
2852                         if (_deleteFrame->IsShown()==true)
2853                         {
2854                                 _deleteFrame->Show(false);
2855                         } else  {
2856                                 _deleteFrame->Show(true);
2857                         }
2858                 }
2859         }
2860
2861
2862 //------------------------------------------------------------------------------------------------------------
2863 void wxContourEventHandler::onCreateContourSpline( wxCommandEvent& event )
2864 {
2865         
2866         //JCP 20-10-08 Undo redo implementation
2867         saveState();
2868         //JCP 20-10-08 Undo redo implementation
2869         createContour( 1 );
2870
2871         
2872
2873 }
2874
2875 //------------------------------------------------------------------------------------------------------------
2876 void wxContourEventHandler::onCreateContourRectangle( wxCommandEvent& event )
2877 {
2878         
2879         //JCP 20-10-08 Undo redo implementation
2880         saveState();
2881         //JCP 20-10-08 Undo redo implementation 
2882         createContour( 2 );
2883
2884         
2885 }
2886
2887 //------------------------------------------------------------------------------------------------------------
2888 void wxContourEventHandler::onCreateContourCircle( wxCommandEvent& event )
2889 {
2890         //JCP 20-10-08 Undo redo implementation
2891         saveState();
2892         //JCP 20-10-08 Undo redo implementation 
2893         createContour( 3 );
2894
2895         
2896 }
2897 //------------------------------------------------------------------------------------------------------------
2898 void wxContourEventHandler::onCreateContourBullEye( wxCommandEvent& event )
2899 {
2900         //JCP 20-10-08 Undo redo implementation
2901         saveState();
2902         //JCP 20-10-08 Undo redo implementation
2903         createContour( 4 );
2904         
2905 }
2906
2907
2908
2909 //------------------------------------------------------------------------------------------------------------
2910         void wxContourEventHandler :: onCreateContourFrame( )
2911         {
2912                 if (_createContourFrame==NULL)
2913                 {
2914                         _createContourFrame                                             = new wxFrame (_theViewPanel, -1,_T("  New Contour  "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT | wxRESIZE_BORDER );
2915                         wxSize sizepanel(490,600);
2916                         _createContourFrame->SetSize( sizepanel );
2917                         wxPanel *panel                                          = new wxPanel(_createContourFrame,-1);
2918                         wxButton *newContourSplineBtn           = new wxButton(panel,-1,_T("Interpolation B-spline (close)"),wxDefaultPosition, wxSize(180,35) );
2919                         newContourSplineBtn->SetToolTip(_T("CTRL-N"));
2920                         wxButton *newContourRectangleBtn        = new wxButton(panel,-1,_T("Rectangle"),wxDefaultPosition, wxSize(180,35));
2921                         wxButton *newContourCircleBtn           = new wxButton(panel,-1,_T("Circle"),wxDefaultPosition, wxSize(180,35));
2922                         wxButton *newContourStarBtn                     = new wxButton(panel,-1,_T("Bull eye"),wxDefaultPosition, wxSize(180,35));
2923
2924                         _panelBullEyeOptions = new PanelBullEyeOptions(panel, wxSize(100,200));
2925
2926                         _createContourFrame->SetEventHandler( this );
2927                         Connect( newContourSplineBtn->GetId()           ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onCreateContourSpline    ); 
2928                         Connect( newContourRectangleBtn->GetId()        ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onCreateContourRectangle ); 
2929                         Connect( newContourCircleBtn->GetId()           ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onCreateContourCircle    ); 
2930                         Connect( newContourStarBtn->GetId()                     ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onCreateContourBullEye   ); 
2931
2932                         wxFlexGridSizer * sizer                         = new wxFlexGridSizer(1);
2933                         sizer -> Add( newContourSplineBtn                       , 1, wxGROW );
2934                         sizer -> Add( newContourRectangleBtn            , 1, wxGROW );
2935                         sizer -> Add( newContourCircleBtn                       , 1, wxGROW );
2936                         sizer -> Add( newContourStarBtn                         , 1, wxGROW );
2937                         sizer -> Add( _panelBullEyeOptions                      , 1, wxGROW );
2938
2939                         panel->SetSizer( sizer );
2940                         panel->SetSize( sizepanel );
2941                         panel->SetAutoLayout( true );
2942                         panel->Layout();
2943                         _createContourFrame->Show(true);
2944                 } else {
2945                         if (_createContourFrame->IsShown()==true)
2946                         {
2947                                 _createContourFrame->Show(false);
2948                         } else  {
2949                                 _createContourFrame->Show(true);
2950                         }
2951                 }
2952         }
2953
2954
2955 //------------------------------------------------------------------------
2956 void wxContourEventHandler::RefreshInterface()
2957 {
2958         changeInstant();
2959         wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
2960         wxvtk2dbaseview->Refresh();
2961 }
2962
2963 //------------------------------------------------------------------------
2964 void wxContourEventHandler::onWidthOfContour(wxScrollEvent& event){
2965         _theViewPanel->getSceneManager()->removeSceneContours();
2966         double width = (double)_withOfContourLine->GetValue() / 2.0;
2967         _theViewPanel->getSceneManager()->SetWidthContour( width );
2968
2969         RefreshInterface();
2970 }
2971
2972 //------------------------------------------------------------------------
2973
2974 void wxContourEventHandler::onBrigthnessColorWindowLevel(wxScrollEvent& event){
2975         int colorwindow                         = _brithtnessColorLevel->GetValue();
2976         int windowlevel                         = _brithtnessWindowLevel->GetValue();
2977         wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
2978         vtkImageViewer2 *imageviewer = wxvtk2dbaseview->_imageViewer2XYZ->GetVtkImageViewer2();
2979         imageviewer->SetColorWindow(colorwindow);
2980         imageviewer->SetColorLevel(windowlevel);        
2981 //      wxvtk2dbaseview->Refresh();
2982         RefreshInterface();
2983 }
2984
2985 void wxContourEventHandler::OnInterpolation(wxCommandEvent& event)
2986 {
2987         wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
2988         vtkImageViewer2 *imageviewer = wxvtk2dbaseview->_imageViewer2XYZ->GetVtkImageViewer2();
2989         if (_interpolationCheckBox->GetValue()==true)
2990         {
2991                 imageviewer->GetImageActor()->InterpolateOn();
2992         } else {
2993                 imageviewer->GetImageActor()->InterpolateOff();
2994         }
2995         RefreshInterface();
2996 }
2997
2998
2999 //------------------------------------------------------------------------------------------------------------
3000         void wxContourEventHandler :: onInterfaceConfigurationFrame( )
3001         {
3002                 if (_interfaceConfigurationFrame==NULL)
3003                 {
3004                         _interfaceConfigurationFrame                    = new wxFrame (_theViewPanel, -1,_T(" Interface Configuration "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
3005                         wxSize sizepanel(190,400);
3006                         _interfaceConfigurationFrame->SetSize( sizepanel );
3007                         _interfaceConfigurationFrame->SetEventHandler( this );
3008                         wxPanel* panel = getConfigurationPanel(_interfaceConfigurationFrame);
3009                         panel->SetSize(sizepanel);
3010                         _interfaceConfigurationFrame->Show(true);
3011                 } else {
3012                         if (_interfaceConfigurationFrame->IsShown()==true)
3013                         {
3014                                 _interfaceConfigurationFrame->Show(false);
3015                         } else  {
3016                                 _interfaceConfigurationFrame->Show(true);
3017                         }
3018                 }
3019         }
3020
3021
3022         //**JCP 13-10-08
3023
3024         wxPanel* wxContourEventHandler :: getConfigurationPanel(wxWindow* parent){
3025                 
3026                 /*wxPanel *panel                                                        = new wxPanel(parent,-1);
3027                 _withOfContourLine                                              = new wxSlider(panel, -1, 1 , 1, 10, wxDefaultPosition, wxSize(180,40), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
3028
3029                 double range[2];
3030                 this->_theViewPanel->getSceneManager()->GetImageDataRange(range);
3031
3032                 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
3033                 vtkImageViewer2 *imageviewer = wxvtk2dbaseview->_imageViewer2XYZ->GetVtkImageViewer2();
3034                 int colorwindow = (int)imageviewer->GetWindowLevel()->GetLevel();
3035                 int windowlevel = (int)imageviewer->GetWindowLevel()->GetWindow();      
3036
3037                 _brithtnessWindowLevel                                  = new wxSlider(panel, -1, windowlevel , 1, range[1], wxDefaultPosition, wxSize(180,40), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
3038                 _brithtnessColorLevel                                   = new wxSlider(panel, -1, colorwindow , 1, range[1], wxDefaultPosition, wxSize(180,40), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
3039
3040                 _interpolationCheckBox = new wxCheckBox(panel, -1, _T("Image interpolation") );
3041                 _interpolationCheckBox->SetValue(true);
3042
3043                 _interfaceConfigurationFrame->SetEventHandler( this );
3044
3045                 Connect( _withOfContourLine->GetId()            ,       wxEVT_SCROLL_CHANGED    , (wxObjectEventFunction) &wxContourEventHandler::onWidthOfContour      ); 
3046                 Connect( _withOfContourLine->GetId()            ,       wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxContourEventHandler::onWidthOfContour      ); 
3047
3048                 Connect( _brithtnessWindowLevel->GetId()        ,       wxEVT_SCROLL_CHANGED    , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
3049                 Connect( _brithtnessWindowLevel->GetId()        ,       wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
3050                 Connect( _brithtnessColorLevel->GetId()         ,       wxEVT_SCROLL_CHANGED    , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
3051                 Connect( _brithtnessColorLevel->GetId()         ,       wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
3052                 Connect( _interpolationCheckBox->GetId()        ,       wxEVT_COMMAND_CHECKBOX_CLICKED    , (wxObjectEventFunction) &wxContourEventHandler::OnInterpolation             );
3053
3054                 wxFlexGridSizer * sizer                                 = new wxFlexGridSizer(1);
3055                 sizer -> Add( new wxStaticText(panel,-1,_T("Contour width"))  , 1, wxGROW );
3056                 sizer -> Add( _withOfContourLine                , 1, wxGROW );
3057                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
3058                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
3059                 sizer -> Add( new wxStaticText(panel,-1,_T("Brightness of the image"))  , 1, wxGROW );
3060                 sizer -> Add( new wxStaticText(panel,-1,_T("--Window level--"))  , 1, wxGROW );
3061                 sizer -> Add( _brithtnessWindowLevel            , 1, wxGROW );
3062                 sizer -> Add( new wxStaticText(panel,-1,_T("--Color level--"))  , 1, wxGROW );
3063                 sizer -> Add( _brithtnessColorLevel, 1, wxGROW );
3064                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
3065                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
3066                 sizer -> Add( _interpolationCheckBox            , 1, wxGROW );
3067
3068                 panel->SetSizer( sizer );
3069                 //JCP 13-10-08
3070                 //panel->SetSize( sizepanel );
3071                 //13-10-08
3072                 panel->SetAutoLayout( true );
3073                 panel->Layout();
3074
3075                 return panel;*/
3076
3077                 wxPanel *panel                                                  = new wxPanel(parent,-1,wxDefaultPosition, wxDefaultSize,wxNO_BORDER, wxString(_T("")));
3078                 _withOfContourLine                                              = new wxSlider(panel, -1, 1 , 1, 10, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
3079
3080                 double range[2];
3081                 this->_theViewPanel->getSceneManager()->GetImageDataRange(range);
3082
3083                 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
3084                 vtkImageViewer2 *imageviewer = wxvtk2dbaseview->_imageViewer2XYZ->GetVtkImageViewer2();
3085                 int colorwindow = (int)imageviewer->GetWindowLevel()->GetLevel();
3086                 int windowlevel = (int)imageviewer->GetWindowLevel()->GetWindow();      
3087
3088                 _brithtnessWindowLevel                                  = new wxSlider(panel, -1, windowlevel , 1, range[1], wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
3089                 _brithtnessColorLevel                                   = new wxSlider(panel, -1, colorwindow , 1, range[1], wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
3090
3091                 _interpolationCheckBox = new wxCheckBox(panel, -1, _T("Image interpolation") );
3092                 _interpolationCheckBox->SetValue(true);         
3093
3094                 Connect( _withOfContourLine->GetId()            ,       wxEVT_SCROLL_CHANGED    , (wxObjectEventFunction) &wxContourEventHandler::onWidthOfContour      ); 
3095                 Connect( _withOfContourLine->GetId()            ,       wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxContourEventHandler::onWidthOfContour      ); 
3096
3097                 Connect( _brithtnessWindowLevel->GetId()        ,       wxEVT_SCROLL_CHANGED    , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
3098                 Connect( _brithtnessWindowLevel->GetId()        ,       wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
3099                 Connect( _brithtnessColorLevel->GetId()         ,       wxEVT_SCROLL_CHANGED    , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
3100                 Connect( _brithtnessColorLevel->GetId()         ,       wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
3101                 Connect( _interpolationCheckBox->GetId()        ,       wxEVT_COMMAND_CHECKBOX_CLICKED    , (wxObjectEventFunction) &wxContourEventHandler::OnInterpolation             );
3102
3103                 wxFlexGridSizer * sizer                                 = new wxFlexGridSizer(1);
3104                 sizer -> Add( new wxStaticText(panel,-1,_T("Contour width"))  , 1, wxGROW );
3105                 sizer -> Add( _withOfContourLine                , 1, wxGROW );
3106                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
3107                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
3108                 sizer -> Add( new wxStaticText(panel,-1,_T("Brightness of the image"))  , 1, wxGROW );
3109                 sizer -> Add( new wxStaticText(panel,-1,_T("--Window level--"))  , 1, wxGROW );
3110                 sizer -> Add( _brithtnessWindowLevel            , 1, wxGROW );
3111                 sizer -> Add( new wxStaticText(panel,-1,_T("--Color level--"))  , 1, wxGROW );
3112                 sizer -> Add( _brithtnessColorLevel, 1, wxGROW );
3113                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
3114                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
3115                 sizer -> Add( _interpolationCheckBox            , 1, wxGROW );
3116
3117                 panel->SetSizer( sizer );
3118                 //JCP 13-10-08
3119                 //panel->SetSize( sizepanel );
3120                 //13-10-08
3121                 panel->SetAutoLayout( true );
3122                 panel->Layout();
3123
3124                 return panel;
3125         }
3126
3127 //**JCP 13-10-08
3128
3129         
3130
3131 //------------------------------------------------------------------
3132         void wxContourEventHandler :: changeContourOfManager(std::string keyName, Instant *instant)
3133         {
3134                 this->_modelManager->ChangeContourOfList(keyName, instant);
3135         }
3136
3137 //------------------------------------------------------------------
3138         void wxContourEventHandler :: changeContoursOfManager(  std::vector<std::string> & keyNamesVector  )
3139         {
3140
3141                 //JCP 20-10-08 Undo redo implementation
3142                 saveState();
3143                 //JCP 20-10-08 Undo redo implementation
3144
3145                 std::vector<int> tempVector;
3146                 _instantPanel->getInstant( tempVector );
3147                 Instant instant(&tempVector);
3148
3149                 int i , size=keyNamesVector.size();
3150                 for ( i=0 ; i<size ; i++ )
3151                 {
3152                         changeContourOfManager(keyNamesVector[i], &instant);
3153                 } // i
3154
3155                 
3156         }
3157
3158
3159 //------------------------------------------------------------------
3160         void wxContourEventHandler :: executeActionCommand( const char  toolCommand )
3161         {
3162                 std::vector<std::string> currentSelection = _theViewPanel->getSceneManager()->getSelectedObjects();
3163                 int elementsSelected = currentSelection.size();
3164                 if( toolCommand == (wxContour_ActionCommandsID::CREATE_TOOL) )
3165                 {
3166                         onCreateContourFrame();
3167                 }
3168                 if( toolCommand == (wxContour_ActionCommandsID::CREATE_CONTOUR_KEY) )
3169                 {
3170                         createContour();
3171                 }
3172                 else if( toolCommand == wxContour_ActionCommandsID::DELETE_TOOL )
3173                 {
3174                         onDeleteFrame( );
3175                 }
3176                 else if( toolCommand == wxContour_ActionCommandsID::DELETE_KEY )
3177                 {
3178                         deleteContours( currentSelection );
3179                 }
3180                 else if( toolCommand == wxContour_ActionCommandsID::SAVE_TOOL )
3181                 {
3182                         saveFileWithContours(  );
3183                 }
3184                 else if( toolCommand == wxContour_ActionCommandsID::SAVE_KEY )
3185                 {
3186                         saveFileWithContoursAutomatique(  );
3187                 }
3188                 else if( toolCommand == wxContour_ActionCommandsID::OPEN_TOOL )
3189                 {
3190                         openFileWithContours(  );
3191                 }
3192                 else if( toolCommand == wxContour_ActionCommandsID::CHANGE_TOOL )
3193                 {
3194                         if ( elementsSelected >= 1 )
3195                         {
3196                                 changeContoursOfManager( currentSelection );
3197                         }
3198
3199                 }
3200                 else if( toolCommand == wxContour_ActionCommandsID::HIDE_TOOL )
3201                 {
3202                         if ( elementsSelected >= 1 )
3203                         {
3204                                 onHideOutlines( currentSelection );
3205                         }                       
3206                 }
3207                 else if( toolCommand == wxContour_ActionCommandsID::SHOW_TOOL )
3208                 {
3209                         if ( elementsSelected >= 1 )
3210                         {
3211                                 onShowOutlines( currentSelection );
3212                         }
3213                 }
3214                 else if( toolCommand == wxContour_ActionCommandsID::COPY_TOOL )
3215                 {
3216                         if ( elementsSelected >= 1 )
3217                         {
3218                                 onCopyOutlines( currentSelection );
3219                         }
3220                 }
3221                 else if( toolCommand == wxContour_ActionCommandsID::PASTE_TOOL )
3222                 {
3223                         onPasteOutlines(  );                    
3224                 }
3225                 else if( toolCommand == wxContour_ActionCommandsID::SPREAD_TOOL )
3226                 {
3227                                 onSpread();
3228                 }
3229                 else if( toolCommand == wxContour_ActionCommandsID::AUTOMATIQUESEGMENTATION_TOOL )
3230                 {
3231                         onAutomatiqueSegmentation(  );                  
3232                 }
3233
3234                 else if( toolCommand == wxContour_ActionCommandsID::SELECT_TOOL )
3235                 {
3236                         onSelectOutlines( currentSelection );
3237                         
3238                 }
3239                 else if( toolCommand == wxContour_ActionCommandsID::EDIT_TOOL )
3240                 {
3241                         if ( elementsSelected >= 1 )
3242                         {
3243                                 onEditOutlines( currentSelection );
3244                         }
3245                 }
3246                 else if( toolCommand == wxContour_ActionCommandsID::UNDO_TOOL )
3247                 {
3248                         onUNDO();
3249                 }
3250                 else if( toolCommand == wxContour_ActionCommandsID::REDO_TOOL )
3251                 {
3252                         onREDO();
3253                 }               
3254                 else if( toolCommand == wxContour_ActionCommandsID::INFORMATIONCONTOUR_TOOL )
3255                 {
3256                         onInformationContourFrame();
3257                 }               
3258                 else if( toolCommand == wxContour_ActionCommandsID::INTERFACECONFIGURATION_TOOL )
3259                 {
3260                         onInterfaceConfigurationFrame();
3261                 }
3262                 else if( toolCommand == wxContour_ActionCommandsID::TEST_TOOL )
3263                 {
3264                         onInterfaceTestFrame();
3265                 }               
3266
3267
3268         }
3269
3270         
3271         //------------------------------------------------------------------------------------------------------------
3272         //  Getting information methods
3273         //------------------------------------------------------------------------------------------------------------
3274
3275
3276
3277         //------------------------------------------------------------------------------------------------------------
3278         //  Private methods
3279         //------------------------------------------------------------------------------------------------------------
3280    
3281         void wxContourEventHandler :: updateInstantOutlines()
3282         {
3283                 std::vector<NameWrapper *> namesWrapping = _modelManager->getActualInstantOutlines();
3284                 int size = namesWrapping.size();
3285                 _theViewPanel->getSceneManager()->removeSceneContours();
3286                 for( int i=0; i<size; i++)
3287                 {                       
3288                         _theViewPanel->getSceneManager()->addToScene(namesWrapping[i]->getKeyName(), true, true, true, false, false );
3289                 }
3290         }
3291         //------------------------------------------------------------------------------------------------------------
3292         void wxContourEventHandler :: updateInstantImageData()
3293         {
3294                 wxContourMainFrame * instance = wxContourMainFrame::getInstance();
3295                 if(instance != NULL){
3296                         int val;
3297                         std::string conceptname;
3298                         val = _instantPanel->getConceptValue(conceptname);
3299
3300                         if(conceptname.compare("time")==0){
3301                                 std::vector<vtkImageData*> imgvect = instance->getVectImages();
3302                                 for(int i = 0; i < imgvect.size(); i++){
3303                                         //_instantPanel->getI
3304                                 }
3305                                 //ImageSourceThing* imgsoth = new ImageSourceThing();
3306                                 //_modelManager->addImageSource(
3307                         }
3308                 }               
3309         }
3310         //------------------------------------------------------------------------------------------------------------
3311
3312         void wxContourEventHandler :: updateInstantAxes()
3313         {
3314                 
3315         }
3316         //------------------------------------------------------------------------------------------------------------  
3317         void wxContourEventHandler :: changeInstant()
3318         {
3319                 std::vector<int> instantVect;
3320                 _instantPanel->getInstant( instantVect );
3321                 Instant * theInstant = new Instant ( &instantVect );
3322                 //if( !theInstant->isEquals( _actualInstant ))
3323                 {       
3324                         //Setting the actual instant
3325                         _actualInstant = theInstant;
3326                         _modelManager->setInstant( _actualInstant );
3327                         
3328                         updateInstantOutlines();
3329                         updateInstantImageData();
3330                         updateInstantAxes();
3331
3332                         
3333                         //Searching the elements at the specified instant
3334
3335                         /*
3336                         std::map<std::string, ContourWrap_ViewControl *> :: iterator iter;
3337                         iter = contours_ViewControl->find("Outline 0");
3338                         (iter->second)->getViewer()->RemoveCompleteContourActor();*/
3339                 }
3340         }
3341
3342         void wxContourEventHandler :: saveState(){
3343                 
3344                         
3345                 inredo = 0;
3346
3347                 //char str[9000];
3348                 //itoa(inundo, str, 10);
3349                 std::string str = intToString(inundo);
3350
3351
3352                 std::string temp = stundoredo + str + ".roi";
3353                 saveFileWithContours(temp);
3354
3355                 inundo++;
3356         }
3357
3358         void wxContourEventHandler ::loadState(std::string filename){
3359
3360                 char tmp[255];
3361                 FILE *pFile=fopen(filename.c_str(),"r+");
3362
3363                 fscanf(pFile,"%s",tmp); // --CreaContour--
3364
3365                 fscanf(pFile,"%s",tmp); // Version
3366                 fscanf(pFile,"%s",tmp); // 1.0.1
3367                 std::string version(tmp);
3368
3369                 deleteAllContours();
3370
3371                 openContours(pFile,false);
3372                 if (version!="1.0.0"){
3373                         openContours(pFile,true);
3374                 }
3375
3376                 fclose(pFile);
3377                 
3378                 //_theViewPanel->getSceneManager()->removeSceneContours();
3379                 //changeInstant();
3380
3381         }
3382 void wxContourEventHandler :: onREDO()
3383         {
3384                 if(inredo > 0){
3385                         inredo--;
3386                         inundo++;
3387
3388                         //char str[9000];
3389                         //itoa(inundo, str, 10);
3390                         std::string str = intToString(inundo);
3391
3392             
3393                         std::string temp = stundoredo + str + ".roi";
3394                 
3395                         loadState(temp);
3396
3397                         
3398                 }
3399
3400         }
3401         //------------------------------------------------------------------------------------------------------------
3402         void wxContourEventHandler :: onUNDO()
3403         {
3404                 if(inundo>0){
3405                         if(inredo==0){
3406
3407                                 //char str[9000];
3408                                 //itoa(inundo, str, 10);
3409                                 std::string str = intToString(inundo);
3410
3411                                 std::string temp = stundoredo + str + ".roi";
3412                                 saveFileWithContours(temp);                             
3413
3414                         }
3415                         inredo++;
3416                         inundo--;
3417
3418                         //char str[9000];
3419                         //itoa(inundo, str, 10);
3420                         std::string str = intToString(inundo);
3421
3422             
3423                         std::string temp = stundoredo + str + ".roi";
3424                 
3425                         loadState(temp);
3426                 }
3427         }
3428 //JCP 21 - 10 - 09
3429         void  wxContourEventHandler :: getValuesXYZ(double **vectx, double **vecty, double **vectz, std::vector<int>* size){
3430
3431
3432                 wxBusyCursor wait;
3433                 ContourExtractData      *contourextractdata = new ContourExtractData();
3434                 int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
3435                 contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
3436
3437                 std::vector<manualContourModel*> lstManConMod;
3438                 std::vector<manualContourModel*> lstManConModTmp;
3439                 std::vector<manualContourModel*> lstManConModExp;
3440                 std::vector<double> pLstValue;
3441                 std::vector<double> pLstValuePosX;
3442                 std::vector<double> pLstValuePosY;
3443                 std::vector<double> pLstValuePosZ;
3444
3445                 std::vector<int> tempVector;
3446                 _instantPanel->getInstant( tempVector );
3447
3448                 vtkImageData *imagedata = _theViewPanel->getSceneManager()->GetImageData();
3449                 int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
3450                 int z;
3451                 int ii,sizeLstContourThings;
3452
3453
3454                 int minZ,maxZ;
3455                 double totalZ;
3456                 double porcent; 
3457                 wxString tmpString;
3458                 minZ    = 0;// _mbarrange->GetStart();
3459                 maxZ    = sizeZ;//_mbarrange->GetEnd();
3460                 totalZ  = maxZ-minZ+1;
3461
3462                 contourextractdata->SetImage( imagedata);
3463
3464         // For each slice..
3465                 vectx = new double *[sizeZ];
3466                 vecty = new double *[sizeZ];
3467                 vectz = new double *[sizeZ];
3468                 for ( z=0 ; z<sizeZ ; z++)
3469                 {
3470
3471                         porcent = 100.0* (z-minZ)/totalZ;
3472                         
3473                         //Extraction data from contours of each slice
3474                         contourextractdata->SetZtoBeAnalys( z);
3475
3476                         tempVector[1]=z;
3477                         Instant instant(&tempVector);
3478                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
3479                         sizeLstContourThings = lstContourThings.size();
3480
3481                         lstManConMod.clear();
3482                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
3483                         {
3484                                 ContourThing **contourthing = lstContourThings[ii];
3485                                 lstManConMod.push_back( (*contourthing)->getModel() );
3486                         }
3487                         lstManConModExp =  ExploseEachModel( lstManConMod );
3488
3489
3490 //EED004
3491
3492                         int iContourGroup,sizeContourGroup;
3493                         if (typeContourGroup==3) // contour separete
3494                         {
3495                                 sizeContourGroup=lstManConModExp.size();
3496                         } else {  // contour AND OR XOR
3497                                 sizeContourGroup=1;
3498                         }
3499
3500                         for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
3501                                 lstManConModTmp.clear();
3502                                 if (typeContourGroup==3) // contour separete
3503                                 {
3504                                         lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
3505                                 } else {  // contour AND OR XOR
3506                                         lstManConModTmp = lstManConModExp;
3507                                 }
3508
3509                                 contourextractdata->SetLstManualContourModel( lstManConModTmp );
3510
3511                                 pLstValue.clear();
3512                                 pLstValuePosX.clear();
3513                                 pLstValuePosY.clear();
3514                                 pLstValuePosZ.clear();
3515                                 contourextractdata->GetValuesInsideCrown(       &pLstValue,
3516                                                                                                                         &pLstValuePosX,
3517                                                                                                                         &pLstValuePosY,
3518                                                                                                                         &pLstValuePosZ);
3519
3520                                 
3521                                 int iLstValue,sizeLstValue=pLstValue.size();
3522
3523                                 //We initialize the pointers
3524                                 size->push_back(sizeLstValue);
3525                                 vectx[sizeZ] = new double[sizeLstValue];
3526                                 vecty[sizeZ] = new double[sizeLstValue];
3527                                 vectz[sizeZ] = new double[sizeLstValue];
3528                                 
3529                                 for (iLstValue=0 ; iLstValue<sizeLstValue ; iLstValue++ )
3530                                 {
3531                                         
3532                                         vectx[sizeZ][iLstValue] = pLstValuePosX[iLstValue];
3533                                         vecty[sizeZ][iLstValue] = pLstValuePosY[iLstValue];
3534                                         vectz[sizeZ][iLstValue] = pLstValuePosZ[iLstValue];
3535                                         
3536                                 }
3537                                 //The size of the contour is saved
3538                                 
3539                                 
3540                                 
3541                                 //fclose(pFile);
3542                         } // for  iContourGroup
3543                 } // for z
3544                 //_staticTextInformation->SetLabel( _T("") );
3545                 delete contourextractdata;
3546
3547         }
3548
3549
3550         void  wxContourEventHandler :: getMaskValue(vtkImageData* mask, vtkImageData* value){
3551
3552
3553                 wxBusyCursor wait;
3554                 ContourExtractData      *contourextractdata = new ContourExtractData( true );
3555                 int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
3556                 if (typeContourGroup==3)
3557                 {
3558                         typeContourGroup=1;
3559                 }
3560                 contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
3561
3562                 std::vector<manualContourModel*> lstManConMod;
3563                 std::vector<manualContourModel*> lstManConModTmp;
3564                 std::vector<manualContourModel*> lstManConModExp;
3565
3566                 std::vector<int> tempVector;
3567                 _instantPanel->getInstant( tempVector );
3568
3569                 vtkImageData *imagedata = _theViewPanel->getSceneManager()->GetImageData();
3570                 int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
3571                 int z;
3572                 int ii,sizeLstContourThings;
3573
3574
3575                 int minZ,maxZ;
3576                 double totalZ;
3577                 double porcent; 
3578
3579                 wxString tmpString;
3580
3581                 minZ    = 0;// _mbarrange->GetStart();
3582                 maxZ    = sizeZ;//_mbarrange->GetEnd();
3583                 totalZ  = maxZ-minZ+1;
3584
3585                 contourextractdata->SetImage( imagedata);
3586
3587         // For each slice..
3588                 for ( z=0 ; z<sizeZ ; z++)
3589                 {
3590
3591                         porcent = 100.0* (z-minZ)/totalZ;
3592                         tmpString.Printf(_T("Saving Values"));
3593                         tmpString.Printf(_T("  %d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
3594                         _staticTextInformation->SetLabel(tmpString);
3595
3596                         //Extraction data from contours of each slice
3597                         contourextractdata->SetZtoBeAnalys( z);
3598
3599                         tempVector[1]=z;
3600                         Instant instant(&tempVector);
3601                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
3602                         sizeLstContourThings = lstContourThings.size();
3603
3604                         lstManConMod.clear();
3605                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
3606                         {
3607                                 ContourThing **contourthing = lstContourThings[ii];
3608                                 lstManConMod.push_back( (*contourthing)->getModel() );
3609                         }
3610                         lstManConModExp =  ExploseEachModel( lstManConMod );
3611
3612
3613                         int iContourGroup,sizeContourGroup;
3614                         if (typeContourGroup==3) // contour separete
3615                         {
3616                                 sizeContourGroup=lstManConModExp.size();
3617                         } else {  // contour AND OR XOR
3618                                 sizeContourGroup=1;
3619                         }
3620
3621                         for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
3622                                 lstManConModTmp.clear();
3623                                 if (typeContourGroup==3) // contour separete
3624                                 {
3625                                         lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
3626                                 } else {  // contour AND OR XOR
3627                                         lstManConModTmp = lstManConModExp;
3628                                 }
3629
3630                                 contourextractdata->SetLstManualContourModel( lstManConModTmp );
3631
3632
3633                                 contourextractdata->CalculateImageResult(); // with actual Z
3634
3635                         } // for  iContourGroup
3636                 } // for z
3637
3638
3639
3640 // Image Value
3641                 value = contourextractdata->GetVtkImageValueResult();
3642                 
3643 // Image Mask
3644                 mask = contourextractdata->GetVtkImageMaskResult();
3645                 
3646                 delete contourextractdata;
3647
3648
3649         }
3650         wxPanel* wxContourEventHandler::getSegmentationPanelITK(wxWindow* parent){
3651
3652                 int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
3653                 double range[2];
3654                 _theViewPanel->getSceneManager()->GetImageDataRange(range);
3655
3656                 wxPanel *panel                                          = new wxPanel(parent,-1);
3657                 wxButton *segmentationOneSliceBtn       = new wxButton(panel,-1,_T("Actual slice"), wxDefaultPosition, wxSize(200,35) );
3658                 wxButton *segmentationAllSliceBtn       = new wxButton(panel,-1,_T("All slices") ,wxDefaultPosition, wxSize(200,35) );
3659                 _isovalue                                                       = new wxSlider(panel, -1, 40 , (int)(range[0]), (int)(range[1]), wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
3660                 _sampling                                                       = new wxSlider(panel, -1, 20 , 4, 50, wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
3661                 wxString lstOptions[3];
3662                 lstOptions[0]=_T("A");
3663                 lstOptions[1]=_T("B");
3664                 lstOptions[2]=_T("C");
3665                 methodRadiobox                                          = new wxRadioBox(panel, -1, _T("Method (find ctrl. Points)"), wxDefaultPosition, wxSize(200,45), 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
3666                 methodRadiobox->SetSelection(2);
3667                 _staticTextSegmentation                         = new wxStaticText(panel,-1,_T("    "));
3668
3669
3670                 _mbarrange                                      =  new mBarRange(panel,70, 65);
3671                 _mbarrange->SetMin(0);
3672                 _mbarrange->SetStart(0);
3673
3674                 _mbarrange-> SetOrientation( true );
3675                 _mbarrange-> setActiveStateTo(true);
3676                 _mbarrange-> setVisibleLabels( true );
3677                 _mbarrange-> setDeviceEndMargin(10);
3678                 _mbarrange-> setRepresentedValues( 0 , sizeZ );
3679                 _mbarrange-> setDeviceBlitStart(10,10); 
3680                 _mbarrange-> setIfWithActualDrawed( false );
3681                 _mbarrange-> SetStart( 0 );
3682                 _mbarrange-> SetEnd( sizeZ );  
3683
3684
3685 //                      _segmentationFrame->SetEventHandler( this );
3686                 segmentationOneSliceBtn->SetEventHandler( this );
3687                 segmentationAllSliceBtn->SetEventHandler( this );
3688                 Connect( segmentationOneSliceBtn->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSegmentationOneSliceITK ); 
3689                 Connect( segmentationAllSliceBtn->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSegmentationAllSliceITK ); 
3690
3691                 wxFlexGridSizer * sizer                         = new wxFlexGridSizer(1);
3692                 sizer -> Add( new wxStaticText(panel,-1,_T("Isovalue (Gray level)"))  , 1, wxGROW );
3693                 sizer -> Add( _isovalue , 1, wxGROW );
3694                 sizer -> Add( methodRadiobox , 1, wxGROW );
3695                 sizer -> Add( new wxStaticText(panel,-1,_T("Sampling (%)"))  , 1, wxGROW );
3696                 sizer -> Add( _sampling , 1, wxGROW );
3697                 sizer -> Add( segmentationOneSliceBtn , 1, wxGROW );
3698                 sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
3699                 sizer -> Add( _staticTextSegmentation  , 1, wxGROW );
3700                 sizer -> Add( segmentationAllSliceBtn , 1, wxGROW );
3701                 sizer -> Add( _mbarrange  , 1, wxGROW );
3702
3703                 panel->SetSizer( sizer );
3704 //              panel->SetSize( sizePanel );
3705                 panel->SetSize( wxDefaultSize );
3706                 panel->SetAutoLayout( true );
3707                 panel->Layout();
3708                 
3709                 return panel;
3710                 
3711         }
3712
3713         void wxContourEventHandler::onSegmentationAllSliceITK( wxCommandEvent& event ){
3714                 //TODO implementar el m�todo para todos los slices ver ayuda en onSegmentationAllSlice
3715         }
3716         void wxContourEventHandler::onSegmentationOneSliceITK( wxCommandEvent& event ){
3717                 //TODO implementar el m�todo de segmentaci�n en ITK ver ayuda en onSegmentationOneSlice y el m�todo
3718                 // SegmentationOneSlice(x, y, z) que es la implementaci�n del m�todo en VTK
3719
3720                 //JCP 20-10-08 Undo redo implementation
3721                 saveState();
3722                 //JCP 20-10-08 Undo redo implementation
3723                 
3724                 wxBusyCursor wait;
3725                 int                                     x                                       = this->_theViewPanel->GetX();
3726                 int                                     y                                       = this->_theViewPanel->GetY();
3727                 int                                     z                                       = this->_theViewPanel->GetZ();
3728                 //SegmentationOneSlice( x,y,z );
3729                 RefreshInterface();
3730         }
3731         void wxContourEventHandler::onSnakePressed(wxWindow* parent){
3732
3733                 std::vector<double> vecX; 
3734                 std::vector<double> vecY; 
3735                 std::vector<double> vecZ; 
3736                 _theViewPanel->getSceneManager()->GetPointsOfActualContour( &vecX , &vecY , &vecZ );    
3737
3738                 if (vecX.size()!=0){
3739                         std::vector<int> tempVector;
3740                         _instantPanel->getInstant( tempVector );
3741                         int i,size=vecZ.size();
3742                         int actualSlice = tempVector[1];
3743                         for ( i=0 ; i<size ; i++ )
3744                         {
3745                                 vecZ[i] = actualSlice;
3746                         } // for
3747
3748                         wxDialog* dialog = new wxDialog(parent, -1, wxString(_T("Snake")));
3749                         wxPanel* panel = new wxPanel(dialog,-1); 
3750                         wxStaticText* sttext = new wxStaticText(panel, -1, wxString(_T("Panel para snake")));
3751                         dialog->ShowModal();
3752                 } // if 
3753
3754         }
3755
3756         void wxContourEventHandler::checkInformationPanel(){
3757                 if (_InformationContourFrame==NULL){
3758                         onInformationContourFrame();
3759                         _InformationContourFrame->Show(false);
3760                 }               
3761         }
3762
3763         std::string wxContourEventHandler::intToString(int num){
3764                 std::string result;
3765                 if(num == 0){
3766                         result = "0";
3767                 }else{
3768                         int k=num;
3769                         while (k > 0){
3770                                 char temp = k % 10 + 48;
3771                                 k = k / 10;
3772                                 result = temp + result; 
3773                         }
3774                 }
3775                 return result;
3776         }
3777 //JCP 21 - 10 - 09
3778
3779