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