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