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