]> 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         //------------------------------------------------------------------------------------------------------------
370         wxContourEventHandler :: ~ wxContourEventHandler()
371         {
372                 delete _actualInstant;
373                 /*delete _theViewPanel;         
374                 delete _instantPanel;
375                 delete _buttonsBar;
376                 delete _actualInstant;  
377                 */       
378                 delete _sceneManager;
379                 delete _performingOperation;
380         }
381         //------------------------------------------------------------------------------------------------------------
382         // Configuration methods
383         //------------------------------------------------------------------------------------------------------------
384         
385         bool wxContourEventHandler :: configureEventsHandling()
386         {
387                 bool configured = true;
388
389                 return configured;
390         }
391
392         //------------------------------------------------------------------------------------------------------------
393         // Methods for capturing events 
394         //------------------------------------------------------------------------------------------------------------
395         void wxContourEventHandler :: onProcessMessage( wxCommandEvent& event )
396         {
397                 
398         }
399         //------------------------------------------------------------------------------------------------------------
400
401         void wxContourEventHandler :: onActionButtonPressed( wxCommandEvent& event )
402         {
403                 const char buttonTool = *(char *)event.GetClientData();
404                 executeActionCommand( buttonTool );             
405         }
406         //------------------------------------------------------------------------------------------------------------          
407         void wxContourEventHandler :: onChangedDeep ( wxCommandEvent& event )
408         {
409                 double val = _theViewPanel->getCurrentDeep();
410                 _instantPanel->setConceptValue( "Axe Depth", (int)val );
411                 _theViewPanel->setActualVertical( (int)val );
412                 changeInstant();
413         }
414
415         //------------------------------------------------------------------------------------------------------------
416         void wxContourEventHandler :: onChangeInstant( wxCommandEvent& event )
417         {               
418                 ConceptDataWrap * dataConcept = NULL; 
419                 if ( _theViewPanel->GetId() == event.GetId() )
420                 {
421                         dataConcept = (ConceptDataWrap *)event.GetClientData();
422                         int value = dataConcept->getActualValue();
423                         _instantPanel->setConceptValue( dataConcept->getName(), value );
424                 }
425                 else
426                 {
427                         dataConcept = _instantPanel->getLastConceptData();
428                         if( dataConcept->getName().compare( _theViewPanel->getVerticalConceptName() ) )
429                         {
430                                 _theViewPanel->setActualVertical( dataConcept->getActualValue());
431                                 //_theViewPanel->setVerticalConcept( dataConcept->getName(), dataConcept->getMinShowedValue()/*dataConcept->getMinValue()*/, /*dataConcept->getMaxValue()*/ dataConcept->getMaxShowedValue(), dataConcept->getMinShowedValue(), dataConcept->getMaxShowedValue(), dataConcept->getActualValue());
432                         }
433                         else if( dataConcept->getName().compare( _theViewPanel->getHorizontalConceptName() ) || _instantPanel->getIfConceptCheckedAt( dataConcept->getName(), 0 ) )
434                         {
435                                 _theViewPanel->setHorizontalConcept( dataConcept->getName(), dataConcept->getMinShowedValue()/*dataConcept->getMinValue()*/, /*dataConcept->getMaxValue()*/ dataConcept->getMaxShowedValue(), dataConcept->getMinShowedValue(), dataConcept->getMaxShowedValue(), dataConcept->getActualValue());
436                         }
437                 }       
438                 changeInstant();
439         }
440         //------------------------------------------------------------------------------------------------------------
441                 
442         void wxContourEventHandler :: onCreateMultipleROI( wxCommandEvent& event )
443         {
444                 _creatingContoursActive = !_creatingContoursActive;
445                 if( _creatingContoursActive )
446                 {                       
447                         createContour();
448                 }
449         }
450         //------------------------------------------------------------------------------------------------------------
451         void wxContourEventHandler :: onCreateROI( wxCommandEvent& event )
452         {
453                 if( _creatingContoursActive )
454                 {                       
455                         createContour();
456                 }
457         }
458
459         //------------------------------------------------------------------------------------------------------------
460         void wxContourEventHandler :: onStopCreateROI( wxCommandEvent& event )
461         {
462                 
463         }
464
465         //------------------------------------------------------------------------------------------------------------
466         void wxContourEventHandler :: onDeleterROI( wxCommandEvent& event )
467         {
468
469         }
470         //------------------------------------------------------------------------------------------------------------
471         void wxContourEventHandler :: onSelectROI( wxCommandEvent& event )
472         {
473                 
474         }
475         //------------------------------------------------------------------------------------------------------------
476
477
478         void wxContourEventHandler :: onUnSelectROI( wxCommandEvent& event )
479         {
480
481         }
482         //------------------------------------------------------------------------------------------------------------
483
484         void wxContourEventHandler :: onManualHidedROI( wxCommandEvent& event )
485         {
486
487         }
488         //------------------------------------------------------------------------------------------------------------
489
490         void wxContourEventHandler :: onManualShowedROI( wxCommandEvent& event )
491         {
492
493         }
494         //------------------------------------------------------------------------------------------------------------
495
496         void wxContourEventHandler :: onEditingROI( wxCommandEvent& event )
497         {
498
499         }
500         //------------------------------------------------------------------------------------------------------------
501
502         void wxContourEventHandler :: onMovingROI( wxCommandEvent& event )
503         {
504
505         }
506
507         //------------------------------------------------------------------------------------------------------------
508         //  Model management methods
509         //------------------------------------------------------------------------------------------------------------
510
511         void wxContourEventHandler::onSpreadReset( wxCommandEvent& event )
512         {
513                 _wxtextctrlSpread->SetValue("");
514                 _contourPropagation->resetAppend();
515         }
516
517         //------------------------------------------------------------------------------------------------------------
518         void wxContourEventHandler::onSpreadAdd( wxCommandEvent& event )
519         {
520                 std::vector<double> vecX; 
521                 std::vector<double> vecY; 
522                 std::vector<double> vecZ; 
523                 _sceneManager->GetPointsOfActualContour( &vecX , &vecY , &vecZ );       
524
525                 if (vecX.size()!=0){
526                         std::vector<int> tempVector;
527                         _instantPanel->getInstant( tempVector );
528                         int i,size=vecZ.size();
529                         int actualSlice = tempVector[1];
530                         for ( i=0 ; i<size ; i++ )
531                         {
532                                 vecZ[i] = actualSlice;
533                         } // for
534
535                         _contourPropagation->appendContour(&vecX , &vecY , &vecZ);
536                         wxString newstring;
537                         newstring.Printf("%s %d -",_wxtextctrlSpread->GetValue(), actualSlice );
538                         _wxtextctrlSpread->SetValue(newstring);
539                 } // if 
540         }
541
542         //------------------------------------------------------------------------------------------------------------
543         void wxContourEventHandler::onSpreadGo( wxCommandEvent& event )
544         {
545                 wxBusyCursor wait;
546                 int typeMethodFindCtrlPoints=_spreadMethodRadiobox->GetSelection();
547                 int typeofcontour = 1;
548                 manualContourModel *manModelContour;
549                 std::vector<double> vecCtrlPointX;
550                 std::vector<double> vecCtrlPointY;
551                 std::vector<double> vecCtrlPointZ;
552                 double  minZ,maxZ;
553                 int z,j,sizeCtrPt;
554 //              double idTmp;
555                 int idTmp;
556
557                 std::vector<int> tempVector;
558                 _instantPanel->getInstant( tempVector );
559 //              tempVector[1];
560
561                 _contourPropagation->getMaxMinZ(&minZ,&maxZ);
562 //JSTG_16-07-08_----------------------------------------------------------------
563                 //_contourPropagation->setInterpolationNumber(maxZ-minZ+1);
564                 _contourPropagation->setInterpolationNumber(100);
565                 _contourPropagation->CalculeSplinePropagation();
566
567                 double          totalZ = maxZ-minZ+1;
568                 double          porcent;
569                 wxString        tmpString;
570 //--------------------------------------------------------------------
571                 for ( z=minZ ; z<=maxZ ; z++ )
572                 {
573                         porcent = 100.0* (z-minZ)/totalZ;
574                         tmpString.Printf("  %d %c            %d/%d            %d", (int)porcent , 37 , (int)(z-minZ+1), (int)totalZ,z );
575                         _staticTextSpread->SetLabel(tmpString);
576
577                         if (_contourPropagation->ifSliceKeyContourExist(z)==false)
578                         {
579                                 manModelContour = factoryManualContourModel( typeofcontour );
580                                 idTmp = _contourPropagation->FindIdWithZ(z);
581                                 if (typeMethodFindCtrlPoints==0) // Initial Points
582                                 {   
583                                         _contourPropagation->GetInitialControlPoints( idTmp , &vecCtrlPointX,&vecCtrlPointY,&vecCtrlPointZ);
584                                 } 
585                                 if (typeMethodFindCtrlPoints==1)  // Automatique Method
586                                 {
587                                         _contourPropagation->GetControlPoints( idTmp  ,&vecCtrlPointX,&vecCtrlPointY,&vecCtrlPointZ);
588                                 }
589                                 if (typeMethodFindCtrlPoints==2)  // sampling
590                                 {
591                                         _contourPropagation->GetControlPoints( idTmp , 20.0 ,&vecCtrlPointX,&vecCtrlPointY,&vecCtrlPointZ);
592                                 }
593         //--------------------------------------------------------------------
594                                 sizeCtrPt = vecCtrlPointX.size();
595                                 for (j=0 ; j<sizeCtrPt ; j++)
596                                 {
597         //JSTG_16-07-08_----------------------------------------------------------------
598                                         manModelContour->AddPoint( vecCtrlPointX[j] , vecCtrlPointY[j] , -900  );
599         //--------------------------------------------------------------------
600                                 } // for j
601
602                                 tempVector[1]=z;
603                                 std::string theName;
604                                 theName = _modelManager->createOutline( manModelContour, tempVector );
605                                 bool addedModel = theName.compare("") != 0;
606                                 if( addedModel )
607                                 {
608                                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
609                                         this->_theViewPanel->getSpacing(spc);                                   
610                                         //Adding the manualContourControler to interface objects structure
611                                         //Adding the manualViewContour to interface objects structure           
612                                         //_sceneManager->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
613                                         _sceneManager->configureViewControlTo( theName, manModelContour,spc,typeofcontour ) ;
614                                 } // if addedModel
615                         }// ifSliceKeyContourExist
616                 } // for z
617                 _staticTextSpread->SetLabel("   ");
618                 RefreshInterface();
619         }
620
621         //------------------------------------------------------------------------------------------------------------
622         void wxContourEventHandler :: onSpread( )
623         {
624                 if (_spreadFrame==NULL)
625                 {
626                         _contourPropagation                     = new ContourPropagation();
627                         _spreadFrame                            = new wxFrame (_theViewPanel, -1,_T("  Spread  "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
628                         wxSize sizepanel(250,400);
629                         _spreadFrame->SetSize( sizepanel );
630                         wxPanel *panel                          = new wxPanel(_spreadFrame,-1);
631
632                         wxButton *spreadResetBtn        = new wxButton(panel,-1,_T("Reset"),wxDefaultPosition, wxSize(80,35) );
633                         wxButton *spreadAddBtn          = new wxButton(panel,-1,_T("Add"),wxDefaultPosition, wxSize(80,35));
634
635                         wxString lstOptions[3];
636                         lstOptions[0]="A";
637                         lstOptions[1]="B";
638                         lstOptions[2]="C";
639                         _spreadMethodRadiobox                                           = new wxRadioBox(panel, -1, "Method (find ctrl. Points)", wxDefaultPosition, wxSize(200,45), 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
640                         _spreadMethodRadiobox->SetSelection(2);
641
642                         wxButton *spreadGoBtn           = new wxButton(panel,-1,_T("Go"),wxDefaultPosition, wxSize(80,35));
643                         _staticTextSpread                       = new wxStaticText(panel,-1,_T("    "));
644
645                         _wxtextctrlSpread           = new wxTextCtrl(panel,-1, "",wxDefaultPosition, wxSize(200,150), wxTE_MULTILINE );
646
647                         _spreadFrame->SetEventHandler( this );
648                         Connect( spreadResetBtn->GetId(),       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSpreadReset ); 
649                         Connect( spreadAddBtn->GetId(),         wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSpreadAdd ); 
650                         Connect( spreadGoBtn->GetId(),          wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSpreadGo ); 
651
652                         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
653                         sizer -> Add( spreadResetBtn            , 1, wxGROW );
654                         sizer -> Add( spreadAddBtn                      , 1, wxGROW );
655                         sizer -> Add( _spreadMethodRadiobox     , 1, wxGROW );
656                         sizer -> Add( spreadGoBtn                       , 1, wxGROW );
657                         sizer -> Add( _staticTextSpread         , 1, wxGROW );
658                         sizer -> Add( _wxtextctrlSpread         , 1, wxGROW );
659
660
661                         panel->SetSizer( sizer );
662                         panel->SetSize( sizepanel );
663                         panel->SetAutoLayout( true );
664                         panel->Layout();
665                         _spreadFrame->Show();
666                 } else {
667                         if (_spreadFrame->IsShown()==true)
668                         {
669                                 _spreadFrame->Show(false);
670                         } else  {
671                                 _spreadFrame->Show(true);
672                         }
673                 }
674         }
675
676
677         //------------------------------------------------------------------------------------------------------------
678         void wxContourEventHandler::onSegmentationAllSlice( wxCommandEvent& event )
679         {
680                 wxBusyCursor wait;
681                 int                                     x                                       = this->_theViewPanel->GetX();
682                 int                                     y                                       = this->_theViewPanel->GetY();
683                 int z,minZ,maxZ;
684                 double porcent; 
685                 wxString tmpString;
686                 minZ=_mbarrange->GetStart();
687                 maxZ=_mbarrange->GetEnd();
688                 double totalZ = maxZ-minZ+1;
689                 for( z=minZ ; z<=maxZ ; z++ )
690                 {
691                         porcent = 100.0* (z-minZ)/totalZ;
692                         tmpString.Printf("  %d %c            %d/%d            %d", (int)porcent , 37 , z-minZ+1, (int)totalZ,z );
693                         _staticTextSegmentation->SetLabel(tmpString);
694                         SegmentationOneSlice( x,y,z );
695                 }
696                 _staticTextSegmentation->SetLabel("   ");
697                 RefreshInterface();
698         }
699
700         //------------------------------------------------------------------------------------------------------------
701         void wxContourEventHandler::onSegmentationOneSlice( wxCommandEvent& event )
702         {
703                 wxBusyCursor wait;
704                 int                                     x                                       = this->_theViewPanel->GetX();
705                 int                                     y                                       = this->_theViewPanel->GetY();
706                 int                                     z                                       = this->_theViewPanel->GetZ();
707                 SegmentationOneSlice( x,y,z );
708                 RefreshInterface();
709         }
710         //------------------------------------------------------------------------------------------------------------
711         void wxContourEventHandler::SegmentationOneSlice( int x, int y, int z )
712         {               
713                 int typeofcontour = 1;
714                 //--Extracting Contour
715                 vtkImageData    *imagedata      = _sceneManager->GetImageData();
716 //              double                  *range          = imagedata->GetScalarRange();  
717 //              double                  thr                     = 1;
718                 int                             isovalue        = _isovalue->GetValue();
719                 int                             sampling        = _sampling->GetValue();
720
721                 vtkImageReslice *imageReslice = vtkImageReslice::New();
722 //EED
723 //              double spc[3];
724 //              imagedata->GetSpacing(spc);
725 //              x = x*spc[0];
726 //              y = y*spc[1];
727 //              z = z*spc[3];
728
729                 imageReslice->SetInput( imagedata );
730                 imageReslice->SetInformationInput(imagedata);
731                 imageReslice->SetResliceAxesDirectionCosines(1,0,0, 0,1,0 ,0,0,1);
732                 imageReslice->SetResliceAxesOrigin(0,0,z);
733                 imageReslice->SetOutputDimensionality(2);
734                 imageReslice->SetInterpolationModeToLinear();
735
736                 imagedata = imageReslice->GetOutput();
737                 imagedata->Update();
738                 imagedata->UpdateInformation();
739
740                 vtkContourFilter* cntVTK = vtkContourFilter::New( );
741                 cntVTK->SetInput( imagedata );
742
743                 cntVTK->SetNumberOfContours( 1 );
744                 //cntVTK->SetValue( 0, vmin );
745 //              cntVTK->SetValue( 0, (range[1]*thr/100) );
746                 cntVTK->SetValue( 1, isovalue );
747         //      cntVTK->SetValue( 1, vmax );
748                 cntVTK->Update( );
749                 cntVTK->UpdateInformation();
750                         
751                 vtkCleanPolyData* cpd = vtkCleanPolyData::New( );
752                 cpd->SetInput( cntVTK->GetOutput( ) );
753                 cpd->ConvertLinesToPointsOff( );
754                 cpd->Update( );
755                 cpd->UpdateInformation();
756
757                 vtkPolyDataConnectivityFilter* conn = vtkPolyDataConnectivityFilter::New( );
758                 conn->SetExtractionModeToClosestPointRegion( );
759                 //conn->SetMaxRecursionDepth( 3000 );
760                         
761                 conn->SetInput( cpd->GetOutput( ) );
762                         
763                 conn->SetClosestPoint( x, y, 0 );
764                 conn->Update( );
765                 conn->UpdateInformation();
766                         
767                 vtkCleanPolyData* cpd2 = vtkCleanPolyData::New( );
768                 cpd2->SetInput( conn->GetOutput( ) );
769                 cpd2->Update();
770                 cpd2->UpdateInformation();
771
772                 vtkStripper* vtkstripper = vtkStripper::New( );
773                 vtkstripper->SetInput( cpd2->GetOutput() );
774                 vtkstripper->Update();
775                 vtkstripper->UpdateInformation();
776
777
778                 vtkPolyData* polyDataResult =  vtkstripper->GetOutput();
779
780                 polyDataResult->Update( );
781                 polyDataResult->UpdateInformation();
782
783 /* EED
784 ofstream myfile;
785 myfile.open ("c:/temp/example.txt");
786 myfile << "\n";
787 polyDataResult->Print(myfile);
788 myfile << "-------------------------------------\n";
789 polyDataResult->GetLines()->Print(myfile);
790 myfile.close();
791 */
792
793                 cntVTK          -> Delete();
794                 cpd2            -> Delete();
795                 cpd                     -> Delete();
796                 conn            -> Delete();
797
798
799 //--Calculating control points
800
801                 std::vector<double> vecX;
802                 std::vector<double> vecY;
803                 std::vector<double> vecZ;
804
805                 std::vector<double> vecCtrlPointX;
806                 std::vector<double> vecCtrlPointY;
807                 std::vector<double> vecCtrlPointZ;
808
809
810                 double *p;
811                 int ii,size=polyDataResult->GetNumberOfPoints();
812                 int id;
813                 for (ii=1;ii<=size;ii++)
814                 {
815                         id      = polyDataResult->GetLines()->GetData()->GetValue(ii);
816                         p       = polyDataResult->GetPoint(id);
817                         double x=p[0];
818                         double y=p[1];
819                         vecX.push_back( p[0] );
820                         vecY.push_back( p[1] );
821                         vecZ.push_back( -900 );
822 //                      vecZ.push_back( p[2] );
823                 }
824
825
826                 ExtractControlPoints2D *extractcontrolpoints2d = new ExtractControlPoints2D();
827                 extractcontrolpoints2d->SetContour( &vecX , &vecY , &vecZ );
828
829 //PROOFS
830                 if (methodRadiobox->GetSelection()==0){
831                         extractcontrolpoints2d->GetInitialControlPoints( &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
832                 }
833                 if (methodRadiobox->GetSelection()==1){
834                         extractcontrolpoints2d->GetControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
835                 }
836                 if (methodRadiobox->GetSelection()==2){
837                         extractcontrolpoints2d->SetSamplingControlPoints( sampling );
838                         extractcontrolpoints2d->GetSamplingControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
839                 }
840
841                 //--Adding contour to the system
842
843                 std::vector<int> actualInstantVector;
844                 _instantPanel->getInstant( actualInstantVector );
845                 actualInstantVector[1]=z;
846
847                 int j,sizeCtrPt = vecCtrlPointX.size();
848                 manualContourModel *manModelContour =  factoryManualContourModel( typeofcontour );
849                 manModelContour->SetNumberOfPointsSpline( ((sizeCtrPt/100)+1)*100 );
850                 if (sizeCtrPt>=3){
851                         for (j=0 ; j<sizeCtrPt ; j++)
852                         {
853                                 manModelContour->AddPoint( vecCtrlPointX[j] , vecCtrlPointY[j] , vecCtrlPointZ[j]  );
854                         } // for
855                         std::string theName;
856                         theName = _modelManager->createOutline( manModelContour, actualInstantVector );
857                         bool addedModel = theName.compare("") != 0;
858                         if( addedModel )
859                         {
860                                 double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
861                                 this->_theViewPanel->getSpacing(spc);                                   
862                                 //Adding the manualContourControler to interface objects structure
863                                 //Adding the manualViewContour to interface objects structure           
864                                 //_sceneManager->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
865                                 _sceneManager->configureViewControlTo( theName, manModelContour,spc, typeofcontour ) ;
866                         }       // if addedModel
867                 } // if sizeCtrPt
868         }
869
870         //------------------------------------------------------------------------------------------------------------
871         void wxContourEventHandler :: onAutomatiqueSegmentation( )
872         {
873                 if (_segmentationFrame==NULL)
874                 {
875                         int sizeZ = _sceneManager->GetImageDataSizeZ();
876                         double range[2];
877                         _sceneManager->GetImageDataRange(range);
878
879                          wxSize sizePanel( 300, 470);
880                         _segmentationFrame                                      =  new wxFrame (_theViewPanel, -1,_T("  Segmentation  "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
881                         _segmentationFrame->SetSize( sizePanel );
882                         wxPanel *panel                                          = new wxPanel(_segmentationFrame,-1);
883                         wxButton *segmentationOneSliceBtn       = new wxButton(panel,-1,_T("Actual slice"), wxDefaultPosition, wxSize(200,35) );
884                         wxButton *segmentationAllSliceBtn       = new wxButton(panel,-1,_T("All slices") ,wxDefaultPosition, wxSize(200,35) );
885                         _isovalue                                                       = new wxSlider(panel, -1, 40 , (int)(range[0]), (int)(range[1]), wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
886                         _sampling                                                       = new wxSlider(panel, -1, 20 , 4, 50, wxDefaultPosition, wxSize(200,35), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
887                         wxString lstOptions[3];
888                         lstOptions[0]="A";
889                         lstOptions[1]="B";
890                         lstOptions[2]="C";
891                         methodRadiobox                                          = new wxRadioBox(panel, -1, "Method (find ctrl. Points)", wxDefaultPosition, wxSize(200,45), 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
892                         methodRadiobox->SetSelection(2);
893                         _staticTextSegmentation                         = new wxStaticText(panel,-1,_T("    "));
894
895
896                         _mbarrange                                      =  new mBarRange(panel,70, 65);
897                         _mbarrange->SetMin(0);
898                         _mbarrange->SetStart(0);
899
900                         _mbarrange-> SetOrientation( true );
901                         _mbarrange-> setActiveStateTo(true);
902                         _mbarrange-> setVisibleLabels( true );
903                         _mbarrange-> setDeviceEndMargin(10);
904                         _mbarrange-> setRepresentedValues( 0 , sizeZ );
905                         _mbarrange-> setDeviceBlitStart(10,10); 
906                         _mbarrange-> setIfWithActualDrawed( false );
907                         _mbarrange-> SetStart( 0 );
908                         _mbarrange-> SetEnd( sizeZ );  
909
910
911 //                      _segmentationFrame->SetEventHandler( this );
912                         segmentationOneSliceBtn->SetEventHandler( this );
913                         segmentationAllSliceBtn->SetEventHandler( this );
914                         Connect( segmentationOneSliceBtn->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSegmentationOneSlice ); 
915                         Connect( segmentationAllSliceBtn->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSegmentationAllSlice ); 
916
917                         wxFlexGridSizer * sizer                         = new wxFlexGridSizer(1);
918                         sizer -> Add( new wxStaticText(panel,-1,_T("Isovalue (Gray level)"))  , 1, wxGROW );
919                         sizer -> Add( _isovalue , 1, wxGROW );
920                         sizer -> Add( methodRadiobox , 1, wxGROW );
921                         sizer -> Add( new wxStaticText(panel,-1,_T("Sampling (%)"))  , 1, wxGROW );
922                         sizer -> Add( _sampling , 1, wxGROW );
923                         sizer -> Add( segmentationOneSliceBtn , 1, wxGROW );
924                         sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
925                         sizer -> Add( _staticTextSegmentation  , 1, wxGROW );
926                         sizer -> Add( segmentationAllSliceBtn , 1, wxGROW );
927                         sizer -> Add( _mbarrange  , 1, wxGROW );
928
929                         panel->SetSizer( sizer );
930                         panel->SetSize( sizePanel );
931                         panel->SetAutoLayout( true );
932                         panel->Layout();
933                         _segmentationFrame->Show(true);
934                         this->_theViewPanel->SetVisibleAxis(true);
935                         this->_theViewPanel->Refresh();
936                 } else {
937                         if (_segmentationFrame->IsShown()==true)
938                         {
939                                 _segmentationFrame->Show(false);
940                                 this->_theViewPanel->SetVisibleAxis(false);
941                                 this->_theViewPanel->Refresh();
942                         } else  {
943                                 _segmentationFrame->Show(true);
944                                 this->_theViewPanel->SetVisibleAxis(true);
945                                 this->_theViewPanel->Refresh();
946                         }
947                 }
948         }
949
950         void wxContourEventHandler :: onInterfaceTestFrame( )
951         {
952                 if (_TestFrame==NULL)
953                 {
954                         _contourPropagation                     = new ContourPropagation();
955                         _TestFrame                                      = new wxFrame (_theViewPanel, -1,_T("  Test Segmentation  "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
956                         wxSize sizepanel(200,300);
957                         _TestFrame->SetSize( sizepanel );
958                         wxPanel *panel                          = new wxPanel(_TestFrame,-1);
959                         wxButton *spreadResetBtn        = new wxButton(panel,-1,_T("Reset"),wxDefaultPosition, wxSize(80,35) );
960                         wxButton *spreadAddBtn          = new wxButton(panel,-1,_T("Add"),wxDefaultPosition, wxSize(80,35));
961                         wxButton *spreadGoBtn           = new wxButton(panel,-1,_T("GoA"),wxDefaultPosition, wxSize(80,35));
962                         _wxtextctrlTest                         = new wxTextCtrl(panel,-1, "",wxDefaultPosition, wxSize(200,150), wxTE_MULTILINE );
963
964                         _TestFrame->SetEventHandler( this );
965                         Connect( spreadResetBtn->GetId(),       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onTestReset ); 
966                         Connect( spreadAddBtn->GetId(),         wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onTestAdd ); 
967                         Connect( spreadGoBtn->GetId(),          wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onTestGo ); 
968
969                         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
970                         sizer -> Add( spreadResetBtn    , 1, wxGROW );
971                         sizer -> Add( spreadAddBtn              , 1, wxGROW );
972                         sizer -> Add( spreadGoBtn               , 1, wxGROW );
973                         sizer -> Add( _wxtextctrlTest , 1, wxGROW );
974
975                         panel->SetSizer( sizer );
976                         panel->SetSize( sizepanel );
977                         panel->SetAutoLayout( true );
978                         panel->Layout();
979                         _TestFrame->Show();
980                 } else {
981                         if (_TestFrame->IsShown()==true)
982                         {
983                                 _TestFrame->Show(false);
984                         } else  {
985                                 _TestFrame->Show(true);
986                         }
987                 }
988         }
989
990         void wxContourEventHandler::onTestReset( wxCommandEvent& event )
991         {
992                 _wxtextctrlTest->SetValue("");
993                 _contourPropagation->resetAppend();
994         }
995
996         //------------------------------------------------------------------------------------------------------------
997         void wxContourEventHandler::onTestAdd( wxCommandEvent& event )
998         {
999                 std::vector<double> vecX; 
1000                 std::vector<double> vecY; 
1001                 std::vector<double> vecZ; 
1002                 _sceneManager->GetPointsOfActualContour( &vecX , &vecY , &vecZ );       
1003
1004                 if (vecX.size()!=0){
1005                         std::vector<int> tempVector;
1006                         _instantPanel->getInstant( tempVector );
1007                         int i,size=vecZ.size();
1008                         int actualSlice = tempVector[1];
1009                         for ( i=0 ; i<size ; i++ )
1010                         {
1011                                 vecZ[i] = actualSlice;
1012                         } // for
1013
1014                         _contourPropagation->appendContour(&vecX , &vecY , &vecZ);
1015                         wxString newstring;
1016                         newstring.Printf("%s %d -",_wxtextctrlTest->GetValue(), actualSlice );
1017                         _wxtextctrlTest->SetValue(newstring);
1018                 } // if 
1019         }
1020
1021         //------------------------------------------------------------------------------------------------------------
1022         void wxContourEventHandler::onTestGo( wxCommandEvent& event )
1023         {
1024                 std::vector<double> vecX; 
1025                 std::vector<double> vecY; 
1026                 std::vector<double> vecZ; 
1027                 std::vector<int> size; 
1028
1029                 std::vector<double> vecCtrlPointX;
1030                 std::vector<double> vecCtrlPointY;
1031                 std::vector<double> vecCtrlPointZ;
1032
1033                 //Getting the points of the actual contour      
1034
1035                 _contourPropagation->GetKeyContours(&vecX , &vecY , &vecZ, &size);
1036                 for(int i = 0; i < vecX.size();i++){
1037                         vecZ[i] = -900;
1038                 }
1039
1040                 /*std::vector<double> vecX1; 
1041                 std::vector<double> vecY1; 
1042                 std::vector<double> vecZ1;
1043                 _sceneManager->GetPointsOfActualContour( &vecX1 , &vecY1 , &vecZ1 );
1044
1045                 std::ofstream file1;
1046         file1.open( "Temp.txt" );       
1047                 if(file1.is_open())
1048                 {
1049                         for(int i = 0; i < vecX.size(); i++){
1050                                 file1<<vecX[i] <<" X1 "<< vecX1[i] <<" Y "<<vecY[i] <<" Y1 "<< vecY1[i]  <<" Z "<<vecZ[i] <<" Z1 "<< vecZ1[i]<<std::endl;
1051                                 
1052                         }
1053                         file1.close();
1054                 }*/     
1055                         
1056                 
1057
1058                 //Setting the points of the contour 
1059
1060                 ExtractControlPoints2D *extractcontrolpoints2d = new ExtractControlPoints2D();
1061                 extractcontrolpoints2d->SetContour( &vecX , &vecY , &vecZ );
1062
1063                 //Getting the control points of the contour by method A
1064
1065                 extractcontrolpoints2d->GetInitialControlPoints( &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
1066                 
1067                 std::vector<int> actualInstantVector;
1068                 _instantPanel->getInstant( actualInstantVector );
1069                 actualInstantVector[1]= this->_theViewPanel->GetZ();
1070
1071                 //Adding the Contour to the scene
1072
1073                 manualContourModel *manModelContour = new manualContourModel();
1074                 int j,sizeCtrPt = vecCtrlPointX.size();
1075                 if (sizeCtrPt>=3){
1076                         for (j=0 ; j<sizeCtrPt ; j++)
1077                         {
1078                                 manModelContour->AddPoint( vecCtrlPointX[j] , vecCtrlPointY[j] , vecCtrlPointZ[j]  );
1079                         } // for
1080                         std::string theName;
1081                         theName = _modelManager->createOutline( manModelContour, actualInstantVector );
1082                         bool addedModel = theName.compare("") != 0;
1083                         if( addedModel )
1084                         {
1085                                 double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
1086                                 this->_theViewPanel->getSpacing(spc);                                   
1087                                 //Adding the manualContourControler to interface objects structure
1088                                 //Adding the manualViewContour to interface objects structure           
1089                                 //_sceneManager->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
1090                                 _sceneManager->configureViewControlTo( theName, manModelContour,spc,1 ) ;
1091                         }       // if addedModel
1092                 } // if sizeCtrPt
1093         }
1094
1095         //------------------------------------------------------------------------------------------------------------
1096         void wxContourEventHandler::FillGridWithContoursInformation()
1097         {
1098                 wxString tempString;
1099                 _grid->ClearGrid();
1100                 _grid->SetColLabelValue(0, _T("A") );
1101                 _grid->SetColLabelValue(1, _T("B") );
1102                 _grid->SetColLabelValue(2, _T("C") );
1103                 _grid->SetColLabelValue(3, _T("D") );
1104                 _grid->SetColLabelValue(4, _T("E") );
1105                 _grid->SetColLabelValue(5, _T("F") );
1106
1107                 std::vector<int> tempVector;
1108                 _instantPanel->getInstant( tempVector );
1109
1110                 int z,sizeZ = _sceneManager->GetImageDataSizeZ();
1111                 int ii,sizeLstContourThings;
1112                 for ( z=0 ; z<sizeZ ; z++)
1113                 {
1114                         tempVector[1]=z;
1115                         Instant instant(&tempVector);
1116                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
1117
1118                         sizeLstContourThings = lstContourThings.size();
1119                         tempString.Printf("%d - %d",z, sizeLstContourThings);
1120                         _grid->SetRowLabelValue(z, tempString );
1121
1122                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
1123                         {
1124                                 ContourThing **contourthing = lstContourThings[ii];
1125                                 tempString = (*contourthing)->getName().c_str() ;
1126                                 _grid->SetCellValue( z, ii, tempString );
1127                         }
1128                 }
1129
1130
1131         }
1132
1133         //------------------------------------------------------------------------------------------------------------
1134         std::vector<manualContourModel*> wxContourEventHandler::ExploseEachModel( std::vector<manualContourModel*> lstManConMod )
1135         {
1136                 std::vector<manualContourModel*> lstTmp;
1137                 std::vector<manualContourModel*> lstResult;
1138                 int j,jSize;
1139                 int i,iSize=lstManConMod.size();
1140                 for (i=0;i<iSize;i++)
1141                 {
1142                         lstTmp = lstManConMod[i]->ExploseModel();
1143                         jSize=lstTmp.size();
1144                         for (j=0;j<jSize;j++)
1145                         {
1146                                 lstResult.push_back( lstTmp[j] );
1147                         }
1148                 }
1149                 return lstResult;
1150         }
1151
1152         //------------------------------------------------------------------------------------------------------------
1153         void wxContourEventHandler::onExtractInformation( wxCommandEvent& event )
1154         {
1155                 wxBusyCursor wait;
1156                 int maxContourGroup     =       0;
1157
1158                 _grid->ClearGrid();
1159
1160                 ContourExtractData      *contourextractdata = new ContourExtractData();
1161                 int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1162                 contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1163
1164                 std::vector<manualContourModel*> lstManConMod;
1165                 std::vector<manualContourModel*> lstManConModTmp;
1166                 std::vector<manualContourModel*> lstManConModExp;
1167                 std::vector<double> pLstValue;
1168                 std::vector<double> pLstValuePosX;
1169                 std::vector<double> pLstValuePosY;
1170                 std::vector<double> pLstValuePosZ;
1171
1172                 int             resultSize; 
1173                 int             resultGrayRangeCount;
1174                 double  resultMin; 
1175                 double  resultMax;
1176                 double  resultAverage;
1177                 double  resultStandardeviation;
1178
1179                 std::vector<int> tempVector;
1180                 _instantPanel->getInstant( tempVector );
1181
1182                 vtkImageData *imagedata = _sceneManager->GetImageData();
1183                 int sizeZ = _sceneManager->GetImageDataSizeZ();
1184                 int z;
1185                 int ii,sizeLstContourThings;
1186
1187
1188                 int minZ,maxZ;
1189                 double totalZ;
1190                 double porcent; 
1191                 wxString tmpString;
1192
1193
1194                 if (_informationRadiobox->GetSelection()==0 ) // actual slice
1195                 {
1196                         std::vector<int> tempVector;
1197                         _instantPanel->getInstant( tempVector );
1198                         int actualSlice = tempVector[1];
1199                         minZ    = actualSlice;
1200                         maxZ    = actualSlice;
1201                 }
1202                 if (_informationRadiobox->GetSelection()==1 ) // slice range
1203                 {
1204                         minZ    = _mbarrangeSliceInformation->GetStart();
1205                         maxZ    = _mbarrangeSliceInformation->GetEnd();
1206                 }
1207                 if (_informationRadiobox->GetSelection()==2 ) // All slices
1208                 {
1209                         minZ    = 0;
1210                         maxZ    = sizeZ-1;
1211                 }
1212
1213                 totalZ  = maxZ-minZ+1;
1214                 contourextractdata->SetImage( imagedata);
1215
1216         // For each slice..
1217                 for ( z=minZ ; z<=maxZ ; z++ )
1218                 {
1219                         porcent = 100.0* (z-minZ)/totalZ;
1220                         tmpString.Printf("  %d %c            %d/%d             %d", (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1221                         _staticTextInformation->SetLabel(tmpString);
1222
1223                         //Extraction data from contours of each slice
1224                         contourextractdata->SetZtoBeAnalys( z);
1225
1226                         tempVector[1]=z;
1227                         Instant instant(&tempVector);
1228                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
1229                         sizeLstContourThings = lstContourThings.size();
1230
1231                         lstManConMod.clear();
1232                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
1233                         {
1234                                 ContourThing **contourthing = lstContourThings[ii];
1235                                 lstManConMod.push_back( (*contourthing)->getModel() );
1236                         }
1237                         lstManConModExp =  ExploseEachModel( lstManConMod );
1238
1239
1240                         wxString tempString;
1241                         tempString.Printf("%d - %d",z, sizeLstContourThings);
1242                         _grid->SetRowLabelValue(z, tempString );
1243 //EED004
1244                         int iContourGroup,sizeContourGroup;
1245                         if (typeContourGroup==3) // contour separete
1246                         {
1247                                 sizeContourGroup=lstManConModExp.size();
1248                                 if ( maxContourGroup<sizeContourGroup ) 
1249                                 {
1250                                         maxContourGroup=sizeContourGroup;
1251                                 }
1252                         } else {  // contour AND OR XOR
1253                                 sizeContourGroup=1;
1254                                 maxContourGroup=1;
1255                         }
1256
1257                         int tmpIntA;
1258
1259
1260
1261                         for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1262                                 lstManConModTmp.clear();
1263                                 if (typeContourGroup==3) // contour separete
1264                                 {
1265                                         lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1266                                 } else {  // contour AND OR XOR
1267                                         lstManConModTmp = lstManConModExp;
1268                                 }
1269
1270                                 contourextractdata->SetLstManualContourModel( lstManConModTmp );
1271
1272                                 pLstValue.clear();
1273                                 pLstValuePosX.clear();
1274                                 pLstValuePosY.clear();
1275                                 pLstValuePosZ.clear();
1276                                 contourextractdata->GetValuesInsideCrown(       &pLstValue,
1277                                                                                                                         &pLstValuePosX,
1278                                                                                                                         &pLstValuePosY,
1279                                                                                                                         &pLstValuePosZ);
1280                                 // Statistics of each slice.
1281                                 contourextractdata->Statistics( &pLstValue,
1282                                                                                                 _mbarrangeRangeInformation->GetStart(),
1283                                                                                                 _mbarrangeRangeInformation->GetEnd(),
1284                                                                                                 &resultGrayRangeCount, 
1285                                                                                                 &resultSize, 
1286                                                                                                 &resultMin, 
1287                                                                                                 &resultMax,
1288                                                                                                 &resultAverage,
1289                                                                                                 &resultStandardeviation);
1290                                 if (_grid->GetNumberCols()<_numberOfVariablesStatistics*(iContourGroup+1)  )
1291                                 {
1292                                         _grid->AppendCols(_numberOfVariablesStatistics);
1293                                 }
1294
1295                                 tmpIntA=_numberOfVariablesStatistics*iContourGroup ;
1296
1297                                 tempString.Printf("%d",resultSize);
1298                                 _grid->SetCellValue( z, tmpIntA + 0, tempString );
1299                                 tempString.Printf("%d",resultGrayRangeCount);
1300                                 _grid->SetCellValue( z, tmpIntA + 1, tempString );
1301                                 tempString.Printf("%f",resultMin);
1302                                 _grid->SetCellValue( z, tmpIntA + 2, tempString );
1303                                 tempString.Printf("%f",resultMax);
1304                                 _grid->SetCellValue( z, tmpIntA + 3, tempString );
1305                                 tempString.Printf("%f",resultAverage);
1306                                 _grid->SetCellValue( z, tmpIntA + 4, tempString );
1307                                 tempString.Printf("%f",resultStandardeviation);
1308                                 _grid->SetCellValue( z, tmpIntA + 5, tempString );
1309
1310                         } // for iContourGroup
1311                 } // for z
1312
1313                 int iTitleGroup;
1314                 wxString tmpTitleString;
1315                 int tmpIntB;
1316                 for ( iTitleGroup=0 ; iTitleGroup<maxContourGroup ; iTitleGroup++ )
1317                 {
1318                         tmpIntB =_numberOfVariablesStatistics*iTitleGroup;
1319                         tmpTitleString.Printf(_T("%d-Size Data"),iTitleGroup);
1320                         _grid->SetColLabelValue( tmpIntB + 0, tmpTitleString );
1321                         _grid->SetColLabelValue( tmpIntB + 1, _T("Size Range") );
1322                         _grid->SetColLabelValue( tmpIntB + 2, _T("Min") );
1323                         _grid->SetColLabelValue( tmpIntB + 3, _T("Max") );
1324                         _grid->SetColLabelValue( tmpIntB + 4, _T("Average") );
1325                         _grid->SetColLabelValue( tmpIntB + 5, _T("St.Dev.") );
1326                         _grid->SetColLabelValue( tmpIntB + 6, _T(" ") );
1327                 }
1328
1329                 _staticTextInformation->SetLabel( _T("") );
1330                 delete contourextractdata;
1331         }
1332
1333
1334         //------------------------------------------------------------------------------------------------------------
1335         void wxContourEventHandler::SaveValuesXYZ(std::string directory,std::string namefile)
1336         {
1337                 wxBusyCursor wait;
1338                 ContourExtractData      *contourextractdata = new ContourExtractData();
1339                 int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1340                 contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1341
1342                 std::vector<manualContourModel*> lstManConMod;
1343                 std::vector<manualContourModel*> lstManConModTmp;
1344                 std::vector<manualContourModel*> lstManConModExp;
1345                 std::vector<double> pLstValue;
1346                 std::vector<double> pLstValuePosX;
1347                 std::vector<double> pLstValuePosY;
1348                 std::vector<double> pLstValuePosZ;
1349
1350                 std::vector<int> tempVector;
1351                 _instantPanel->getInstant( tempVector );
1352
1353                 vtkImageData *imagedata = _sceneManager->GetImageData();
1354                 int sizeZ = _sceneManager->GetImageDataSizeZ();
1355                 int z;
1356                 int ii,sizeLstContourThings;
1357
1358
1359                 int minZ,maxZ;
1360                 double totalZ;
1361                 double porcent; 
1362                 wxString tmpString;
1363                 minZ    = 0;// _mbarrange->GetStart();
1364                 maxZ    = sizeZ;//_mbarrange->GetEnd();
1365                 totalZ  = maxZ-minZ+1;
1366
1367                 contourextractdata->SetImage( imagedata);
1368
1369         // For each slice..
1370                 for ( z=0 ; z<sizeZ ; z++)
1371                 {
1372
1373                         porcent = 100.0* (z-minZ)/totalZ;
1374                         tmpString.Printf("Saving Values  %d %c            %d/%d             %d", (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1375                         _staticTextInformation->SetLabel(tmpString);
1376
1377
1378                         //Extraction data from contours of each slice
1379                         contourextractdata->SetZtoBeAnalys( z);
1380
1381                         tempVector[1]=z;
1382                         Instant instant(&tempVector);
1383                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
1384                         sizeLstContourThings = lstContourThings.size();
1385
1386                         lstManConMod.clear();
1387                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
1388                         {
1389                                 ContourThing **contourthing = lstContourThings[ii];
1390                                 lstManConMod.push_back( (*contourthing)->getModel() );
1391                         }
1392                         lstManConModExp =  ExploseEachModel( lstManConMod );
1393
1394
1395 //EED004
1396
1397                         int iContourGroup,sizeContourGroup;
1398                         if (typeContourGroup==3) // contour separete
1399                         {
1400                                 sizeContourGroup=lstManConModExp.size();
1401                         } else {  // contour AND OR XOR
1402                                 sizeContourGroup=1;
1403                         }
1404
1405                         for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1406                                 lstManConModTmp.clear();
1407                                 if (typeContourGroup==3) // contour separete
1408                                 {
1409                                         lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1410                                 } else {  // contour AND OR XOR
1411                                         lstManConModTmp = lstManConModExp;
1412                                 }
1413
1414                                 contourextractdata->SetLstManualContourModel( lstManConModTmp );
1415
1416                                 pLstValue.clear();
1417                                 pLstValuePosX.clear();
1418                                 pLstValuePosY.clear();
1419                                 pLstValuePosZ.clear();
1420                                 contourextractdata->GetValuesInsideCrown(       &pLstValue,
1421                                                                                                                         &pLstValuePosX,
1422                                                                                                                         &pLstValuePosY,
1423                                                                                                                         &pLstValuePosZ);
1424
1425                                 wxString filename;
1426                                 filename.Printf("%s\\%s-slice%d-cont%d.txt",directory.c_str(),namefile.c_str(),z,iContourGroup);
1427                                 FILE *pFile=fopen((const char *)filename.mb_str(),"w+");
1428                                 fprintf(pFile,"value \t x \t y \t z\n"  );
1429                                 int iLstValue,sizeLstValue=pLstValue.size();
1430                                 for (iLstValue=0 ; iLstValue<sizeLstValue ; iLstValue++ )
1431                                 {
1432                                         fprintf(pFile,"%f\t %f\t %f\t %f\n", (float)pLstValue[iLstValue] , (float)pLstValuePosX[iLstValue], (float)pLstValuePosY[iLstValue], (float)z );
1433                                 }
1434                                 fclose(pFile);
1435                         } // for  iContourGroup
1436                 } // for z
1437                 _staticTextInformation->SetLabel( _T("") );
1438                 delete contourextractdata;
1439         }
1440
1441
1442         //------------------------------------------------------------------------------------------------------------
1443         void wxContourEventHandler::SaveImageResult(std::string directory,std::string namefile)
1444         {
1445                 wxBusyCursor wait;
1446                 ContourExtractData      *contourextractdata = new ContourExtractData( true );
1447                 int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1448                 if (typeContourGroup==3)
1449                 {
1450                         typeContourGroup=1;
1451                 }
1452                 contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1453
1454                 std::vector<manualContourModel*> lstManConMod;
1455                 std::vector<manualContourModel*> lstManConModTmp;
1456                 std::vector<manualContourModel*> lstManConModExp;
1457
1458                 std::vector<int> tempVector;
1459                 _instantPanel->getInstant( tempVector );
1460
1461                 vtkImageData *imagedata = _sceneManager->GetImageData();
1462                 int sizeZ = _sceneManager->GetImageDataSizeZ();
1463                 int z;
1464                 int ii,sizeLstContourThings;
1465
1466
1467                 int minZ,maxZ;
1468                 double totalZ;
1469                 double porcent; 
1470
1471                 wxString tmpString;
1472
1473                 minZ    = 0;// _mbarrange->GetStart();
1474                 maxZ    = sizeZ;//_mbarrange->GetEnd();
1475                 totalZ  = maxZ-minZ+1;
1476
1477                 contourextractdata->SetImage( imagedata);
1478
1479         // For each slice..
1480                 for ( z=0 ; z<sizeZ ; z++)
1481                 {
1482
1483                         porcent = 100.0* (z-minZ)/totalZ;
1484                         tmpString.Printf("Saving Values  %d %c            %d/%d             %d", (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1485                         _staticTextInformation->SetLabel(tmpString);
1486
1487                         //Extraction data from contours of each slice
1488                         contourextractdata->SetZtoBeAnalys( z);
1489
1490                         tempVector[1]=z;
1491                         Instant instant(&tempVector);
1492                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
1493                         sizeLstContourThings = lstContourThings.size();
1494
1495                         lstManConMod.clear();
1496                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
1497                         {
1498                                 ContourThing **contourthing = lstContourThings[ii];
1499                                 lstManConMod.push_back( (*contourthing)->getModel() );
1500                         }
1501                         lstManConModExp =  ExploseEachModel( lstManConMod );
1502
1503
1504                         int iContourGroup,sizeContourGroup;
1505                         if (typeContourGroup==3) // contour separete
1506                         {
1507                                 sizeContourGroup=lstManConModExp.size();
1508                         } else {  // contour AND OR XOR
1509                                 sizeContourGroup=1;
1510                         }
1511
1512                         for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1513                                 lstManConModTmp.clear();
1514                                 if (typeContourGroup==3) // contour separete
1515                                 {
1516                                         lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1517                                 } else {  // contour AND OR XOR
1518                                         lstManConModTmp = lstManConModExp;
1519                                 }
1520
1521                                 contourextractdata->SetLstManualContourModel( lstManConModTmp );
1522
1523
1524 //                              for (ii=0 ; ii<sizeLstContourThings ; ii++)
1525 //                              {
1526 //                                      ContourThing **contourthing = lstContourThings[ii];
1527 //                                      lstManConMod.push_back( (*contourthing)->getModel() );
1528 //                              }
1529 //                              contourextractdata->SetLstManualContourModel( lstManConMod );
1530
1531                                 contourextractdata->CalculateImageResult(); // with actual Z
1532
1533                         } // for  iContourGroup
1534                 } // for z
1535
1536
1537                 wxString filename;
1538                 filename.Printf("%s\\%s-Value.mhd",directory.c_str(),namefile.c_str(),z);
1539
1540 // Image Value
1541                 vtkMetaImageWriter *writerValueImage = vtkMetaImageWriter::New( );
1542                 writerValueImage->SetInput( contourextractdata->GetVtkImageValueResult() );
1543                 writerValueImage->SetFileName( (const char *)filename.mb_str() );
1544                 writerValueImage->SetFileDimensionality( 3 );
1545                 writerValueImage->Write( );
1546
1547 // Image Mask
1548                 filename.Printf("%s\\%s-Mask.mhd",directory.c_str(),namefile.c_str(),z);
1549                 vtkMetaImageWriter *writerMaskImage = vtkMetaImageWriter::New( );
1550                 writerMaskImage->SetInput( contourextractdata->GetVtkImageMaskResult() );
1551                 writerMaskImage->SetFileName( (const char *)filename.mb_str() );
1552                 writerMaskImage->SetFileDimensionality( 3 );
1553                 writerMaskImage->Write( );
1554
1555                 _staticTextInformation->SetLabel( _T("") );
1556                 delete contourextractdata;
1557         }
1558
1559
1560         //------------------------------------------------------------------------------------------------------------
1561         void wxContourEventHandler::onInformationContourLabels( wxCommandEvent& event )
1562         {
1563                 wxBusyCursor wait;
1564                 FillGridWithContoursInformation();
1565         }
1566
1567
1568         //------------------------------------------------------------------------------------------------------------
1569         void wxContourEventHandler::onSaveResults( wxCommandEvent& event )
1570         {
1571                 wxFileDialog dialog(_InformationContourFrame, _T("Choose a file"), _T(""), _T(""), _T("*.txt"), wxSAVE );
1572                 if (dialog.ShowModal() == wxID_OK)
1573                 {
1574                         onExtractInformation( event );
1575                         std::string directory=(const char *)(dialog.GetDirectory().mb_str());
1576                         std::string namefile=(const char *)(dialog.GetFilename().mb_str());
1577
1578                         SaveValuesXYZ( directory , namefile );
1579                         SaveImageResult( directory , namefile );
1580
1581                         std::string filename = (const char *)(dialog.GetPath().mb_str() );
1582                         FILE *pFile=fopen(filename.c_str(),"w+");
1583
1584                         int sizeZ = _sceneManager->GetImageDataSizeZ();
1585
1586                         wxString tmpString;
1587                         int i,j,maxX,maxY=sizeZ;
1588                         maxX=this->_grid->GetNumberCols();
1589
1590                         int iTitle,sizeTitle = (maxX / _numberOfVariablesStatistics);
1591                         for ( iTitle=0; iTitle<sizeTitle ; iTitle++)
1592                         {
1593                                 fprintf(pFile,"-- \t %d-Size \t SizeRange \t Min \t Max \t Ave \t StDv \t" , iTitle,tmpString.char_str() );
1594                         }
1595                         fprintf(pFile,"\n" );
1596
1597
1598
1599                         for ( j=0; j<maxY ; j++)
1600                         {
1601                                 fprintf(pFile,"%d\t" , j );
1602                                 for (i=0 ; i<maxX ; i++){
1603                                         tmpString = _grid->GetCellValue( j , i );
1604                                         fprintf(pFile,"%s\t" , (const char *)(tmpString.mb_str()) );
1605                                 } // for i
1606                                 fprintf(pFile,"\n"  );
1607                         } // for j
1608
1609                         fclose(pFile);
1610                 } // if  ShowModal
1611
1612
1613         }
1614
1615
1616         //------------------------------------------------------------------------------------------------------------
1617         void wxContourEventHandler::onInformationContourFrame()
1618         {
1619                 if (_InformationContourFrame==NULL)
1620                 {
1621                         _InformationContourFrame                                                = new wxFrame (_theViewPanel, -1,_T("  Information  "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT | wxRESIZE_BORDER  );
1622                         _InformationContourFrame->SetSize( wxSize(500,450) );
1623                         wxSize sizepanel(200,300);
1624                         wxPanel *panel                                                                  = new wxPanel(_InformationContourFrame,-1,wxDefaultPosition, wxDefaultSize,wxTAB_TRAVERSAL);
1625
1626                         wxString lstOptions[4];
1627                         lstOptions[0]=_T("Actual Slice");
1628                         lstOptions[1]=_T("Range Slices");
1629                         lstOptions[2]=_T("All Slices");
1630                         _informationRadiobox                                                    = new wxRadioBox(panel, -1, _T("Slice analysis"), wxDefaultPosition, wxSize(270,45), 3 , lstOptions,  3, wxRA_SPECIFY_COLS);
1631
1632                         wxString lstOptContOperation[5];
1633                         lstOptContOperation[0]=_T("AND");
1634                         lstOptContOperation[1]=_T("OR");
1635                         lstOptContOperation[2]=_T("XOR");
1636                         lstOptContOperation[3]=_T("ALL");
1637                         _radiolstboxContourGroup                                                = new wxRadioBox(panel, -1, _T("Contour group"), wxDefaultPosition, wxSize(270,45), 4 , lstOptContOperation,  4, wxRA_SPECIFY_COLS);
1638
1639                         wxButton *informationContourLabelsBtn                   = new wxButton(panel,-1,_T("Contour labels"),wxDefaultPosition, wxSize(140,35) );
1640                         informationContourLabelsBtn->SetEventHandler( this );
1641                         Connect( informationContourLabelsBtn->GetId(),          wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onInformationContourLabels ); 
1642
1643                         wxButton *statisticsContourBtn                                  = new wxButton(panel,-1,_T("Contour statistics"),wxDefaultPosition, wxSize(140,35) );
1644                         statisticsContourBtn->SetEventHandler( this );
1645                         Connect( statisticsContourBtn->GetId(),         wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onExtractInformation ); 
1646
1647                         wxButton *saveResultsBtn                                                = new wxButton(panel,-1,_T("Save statistics results"),wxDefaultPosition, wxSize(140,35) );
1648                         saveResultsBtn->SetEventHandler( this );
1649                         Connect( saveResultsBtn->GetId(),               wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onSaveResults ); 
1650
1651
1652                         int sizeZ = this->_sceneManager->GetImageDataSizeZ();                   
1653                         _mbarrangeSliceInformation                                      =  new mBarRange(panel,65,65);
1654                         _mbarrangeSliceInformation->SetMin(0);
1655                         _mbarrangeSliceInformation->SetStart(0);
1656                         _mbarrangeSliceInformation-> SetOrientation( true );
1657                         _mbarrangeSliceInformation-> setActiveStateTo(true);
1658                         _mbarrangeSliceInformation-> setVisibleLabels( true );
1659                         _mbarrangeSliceInformation-> setDeviceEndMargin(10);
1660                         _mbarrangeSliceInformation-> setRepresentedValues( 0 , sizeZ-1 );
1661                         _mbarrangeSliceInformation-> setDeviceBlitStart(10,10); 
1662                         _mbarrangeSliceInformation-> setIfWithActualDrawed( false );
1663                         _mbarrangeSliceInformation-> SetStart( 0 );
1664                         _mbarrangeSliceInformation-> SetEnd( sizeZ-1 );  
1665
1666
1667                         double range[2];
1668                         this->_sceneManager->GetImageDataRange(range);
1669                         _mbarrangeRangeInformation                                      =  new mBarRange(panel,65,65);
1670                         _mbarrangeRangeInformation->SetMin(0);
1671                         _mbarrangeRangeInformation->SetStart(0);
1672                         _mbarrangeRangeInformation-> SetOrientation( true );
1673                         _mbarrangeRangeInformation-> setActiveStateTo(true);
1674                         _mbarrangeRangeInformation-> setVisibleLabels( true );
1675                         _mbarrangeRangeInformation-> setDeviceEndMargin(10);
1676                         _mbarrangeRangeInformation-> setRepresentedValues( range[0] , range[1] );
1677                         _mbarrangeRangeInformation-> setDeviceBlitStart(10,10); 
1678                         _mbarrangeRangeInformation-> setIfWithActualDrawed( false );
1679                         _mbarrangeRangeInformation-> SetStart( range[0] );
1680                         _mbarrangeRangeInformation-> SetEnd( range[1] );  
1681
1682
1683                         _staticTextInformation                                                  = new wxStaticText(panel,-1,_T("    "));
1684
1685
1686 //                      wxButton *XXXXXXXBtn                    = new wxButton(panel,-1,_T("Statistics"),wxDefaultPosition, wxSize(180,35) );
1687 //                      XXXXXXXBtn->SetEventHandler( this );
1688 //                      Connect( XXXXXXXBtn->GetId(),           wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onXXXXX ); 
1689
1690
1691                         _grid = new wxGrid( panel,
1692                        wxID_ANY,
1693                        wxPoint( 0, 0 ),
1694                        wxSize( 100, 100 ) );
1695
1696                         int i,gridCol=10,gridRow=sizeZ;
1697                         _grid->CreateGrid( 0, 0 );
1698                         _grid->AppendRows(gridRow);
1699                         _grid->AppendCols(gridCol);
1700
1701                         for (i=0;i<gridRow;i++)
1702                         {
1703                                 _grid->SetRowLabelValue(i, _T(" ") );
1704                         }
1705 //                      _grid->SetColLabelSize(0);
1706
1707
1708                         FillGridWithContoursInformation();
1709
1710                         wxFlexGridSizer * sizerA                = new wxFlexGridSizer(10);
1711                         sizerA->Add( _informationRadiobox                       , 1, wxALL ,2 );
1712                         sizerA->Add( _radiolstboxContourGroup           , 1, wxALL ,2 );
1713
1714                         wxFlexGridSizer * sizerB                = new wxFlexGridSizer(10);
1715                         sizerB->Add( informationContourLabelsBtn        , 1, wxALL ,2 );
1716                         sizerB->Add( statisticsContourBtn                       , 1, wxALL ,2 );
1717                         sizerB->Add( saveResultsBtn                                     , 1, wxALL ,2 );
1718
1719                         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
1720                         sizer->AddGrowableCol(0);
1721                         sizer->AddGrowableRow(7);
1722                         sizer->Add( sizerA                                              , 1, wxALL              , 0 );
1723                         sizer->Add( new wxStaticText(panel              ,-1,_T("Slice Range"))  , 1, wxALL              , 0 );
1724                         sizer->Add( _mbarrangeSliceInformation  , 1, wxALL|wxGROW               , 2 );                  
1725                         sizer->Add( new wxStaticText(panel              ,-1,_T("Gray Range"))   , 1, wxALL              , 0 );
1726                         sizer->Add( _mbarrangeRangeInformation  , 1, wxALL|wxGROW               , 2 );                  
1727                         sizer->Add( sizerB                                              , 1, wxALL              , 0 );
1728                         sizer->Add( _staticTextInformation              , 1, wxALL              , 0 );
1729                         sizer->Add( _grid                                               , 1, wxEXPAND   , 0 );
1730
1731
1732
1733                         panel->SetSizer( sizer );
1734                         panel->SetSize( wxSize(1500,1500) );
1735                         //panel->SetBackgroundColour( wxColour(100,100,100) );
1736                         panel->SetAutoLayout( true );
1737                         panel->Layout();
1738
1739                         wxBoxSizer * sizerPanel         = new wxBoxSizer(wxHORIZONTAL);
1740                         sizerPanel -> Add( panel                , 1, wxEXPAND ,0);
1741                         _InformationContourFrame->SetSizer( sizerPanel );
1742                         _InformationContourFrame->SetAutoLayout( true );
1743                         _InformationContourFrame->Layout();
1744
1745                         _InformationContourFrame->Show();
1746                 } else {
1747                         if (_InformationContourFrame->IsShown()==true)
1748                         {
1749                                 _InformationContourFrame->Show(false);
1750                         } else  {
1751                                 FillGridWithContoursInformation();
1752                                 _InformationContourFrame->Show(true);
1753                         }
1754                 }
1755         }
1756
1757
1758         //------------------------------------------------------------------------------------------------------------
1759         void wxContourEventHandler :: onSpreadInDepth( std::vector<std::string> & keyNamesVector )
1760         {
1761                 
1762         }
1763         //------------------------------------------------------------------------------------------------------------
1764         void wxContourEventHandler :: onOutline_Union_Of( std::vector<std::string> & keyNamesVector )
1765         {
1766                 
1767         }
1768         //------------------------------------------------------------------------------------------------------------
1769         void wxContourEventHandler :: onOutline_Intersection_Of( std::vector<std::string> & keyNamesVector )
1770         {
1771                 
1772         }
1773         //------------------------------------------------------------------------------------------------------------
1774         void wxContourEventHandler :: onOutline_Combination_Of( std::vector<std::string> & keyNamesVector )
1775         {
1776                 
1777         }
1778         //------------------------------------------------------------------------------------------------------------
1779         void wxContourEventHandler :: onOutline_Fragmentation_Of( std::vector<std::string> & keyNamesVector )
1780         {
1781                 
1782         }
1783         //------------------------------------------------------------------------------------------------------------
1784         void wxContourEventHandler :: onOutline_Agrupation_Of( std::vector<std::string> & keyNamesVector )
1785         {
1786                 
1787         }
1788         //------------------------------------------------------------------------------------------------------------
1789         void wxContourEventHandler :: onCopyOutlines( std::vector<std::string> & keyNamesVector )
1790         {
1791                 std::vector<int> tempVector;
1792                 _instantPanel->getInstant( tempVector );
1793         _performingOperation->reset();
1794                 _performingOperation->setStartCommand( (char)wxContour_ActionCommnadsID::COPY_TOOL );
1795                 _performingOperation->setStartOperationInstantVector( tempVector );
1796                 _performingOperation->setKeyNamesOperationElems( keyNamesVector );
1797
1798         }
1799         //------------------------------------------------------------------------------------------------------------
1800         void wxContourEventHandler :: onPasteOutlines( )
1801         {
1802                 char theStartCommand = _performingOperation->getStartCommand();
1803                 if (  theStartCommand == wxContour_ActionCommnadsID::COPY_TOOL )
1804                 {
1805                         std::vector<int> tempVector;
1806                         _instantPanel->getInstant( tempVector );
1807                         _performingOperation->setEndOperationInstantVector ( tempVector );
1808                         std::vector<std::string> elems = _performingOperation->getKeyNamesOperationElems();
1809                         int i,size = elems.size();                      
1810                         for( i=0; i<size; i++ )
1811                         {                               
1812                                 createCopyContourOf( elems[i], tempVector, i>0 );
1813                         }
1814                 }
1815                 int fin = 0;
1816         }
1817         //------------------------------------------------------------------------------------------------------------
1818         void wxContourEventHandler :: onShowOutlines( std::vector<std::string> & keyNamesVector )
1819         {
1820                 
1821         }
1822         //------------------------------------------------------------------------------------------------------------
1823         void wxContourEventHandler :: onHideOutlines( std::vector<std::string> & keyNamesVector )
1824         {
1825                 
1826         }
1827         //------------------------------------------------------------------------------------------------------------
1828         void wxContourEventHandler :: onSelectOutlines( std::vector<std::string> & keyNamesVector )
1829         {
1830                 //if( keyNamesVector.empty() )
1831                 {
1832                         _sceneManager->drawSelectionROI( );
1833                 }
1834                 /*else
1835                 {
1836                         int ok = keyNamesVector.size();
1837                         _sceneManager->selectObjects( keyNamesVector );
1838                         std::string name = keyNamesVector[0];                   
1839                 }*/
1840                 /*
1841                 _sceneManager->drawSelectionROI( );
1842                 std::vector<std::string> currentSelection = _sceneManager->getSelectedObjects();
1843                 int ok = currentSelection.size();
1844                 _sceneManager->selectObjects( currentSelection );*/
1845         }
1846         //------------------------------------------------------------------------------------------------------------
1847         void wxContourEventHandler :: onEditOutlines( std::vector<std::string> & keyNamesVector )
1848         {
1849                 
1850         }
1851         //------------------------------------------------------------------------------------------------------------
1852         void wxContourEventHandler :: onREDO()
1853         {
1854
1855         }
1856         //------------------------------------------------------------------------------------------------------------
1857         void wxContourEventHandler :: onUNDO()
1858         {
1859                 
1860         }
1861         //------------------------------------------------------------------------------------------------------------
1862         void wxContourEventHandler :: deleteContour( std::string theKeyName )
1863         {
1864                 manualContourModel                      * cModel;
1865                 manualViewBaseContour           * cViewer;
1866                 manualContourBaseControler      * cControler;
1867
1868                 ContourWrap_ViewControl *conwrapviewControl =  _sceneManager->getContourWrap_ViewControlOf( theKeyName );
1869                 cControler      = conwrapviewControl->getControler();
1870                 int ispartofstaticlst= _modelManager->IsPartOfStaticList(theKeyName);
1871                 if ((cControler!=NULL) && (cControler->IsEditable()==false)  && (ispartofstaticlst==-1) )
1872                 {
1873                         _sceneManager->removeFromScene( theKeyName );
1874                         cModel          = _modelManager->getOutlineByKeyName(theKeyName)->getModel();
1875                         _modelManager->removeOutline( theKeyName );
1876                         cViewer         = conwrapviewControl->getViewer();
1877                         _sceneManager->removeWrap( theKeyName );
1878 //EED Borrame
1879 //FILE *ff = fopen("c:/temp/wxVtkBaseView_SceneManagerStadistics.txt","a+");
1880 //fprintf(ff,"EED wxContourEventHandler::deleteContours() \n" );
1881 //fprintf(ff,"    %s %p\n",keyNamesVector[i].c_str(),  cControler );
1882 //fclose(ff);
1883                         delete cModel;
1884                         delete cViewer;
1885                         delete cControler;
1886                 } // if editable
1887         }
1888
1889         //------------------------------------------------------------------------------------------------------------
1890         void wxContourEventHandler :: deleteContours( std::vector<std::string> & keyNamesVector )
1891         {
1892                 int i,size=keyNamesVector.size();
1893                 for (i=0;i<size;i++)
1894                 {
1895                         deleteContour( keyNamesVector[i] );
1896                 }
1897         }
1898
1899         //------------------------------------------------------------------------------------------------------------
1900         void wxContourEventHandler :: deleteAllContours(  )
1901         {               
1902                 wxBusyCursor wait;
1903                 std::vector<int> tempVector;
1904                 _instantPanel->getInstant( tempVector );
1905                 int minZ = _mbarrangeDeleteAll->GetStart();
1906                 int maxZ = _mbarrangeDeleteAll->GetEnd();
1907                 int z;
1908                 int ii,sizeLstContourThings;
1909                 
1910                 if ( (minZ==0) && (maxZ==_mbarrangeDeleteAll->GetMax() ))
1911                 {
1912                         _sceneManager->removeSceneContours( );
1913                         _modelManager->removeAllOutlines();
1914                         _sceneManager->removeAllOutlines();
1915
1916                 } else {
1917                         for ( z=minZ ; z<=maxZ ; z++)
1918                         {
1919                                 tempVector[1]=z;
1920                                 Instant instant(&tempVector);
1921                                 std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
1922
1923                                 sizeLstContourThings = lstContourThings.size();
1924                                 for (ii=0 ; ii<sizeLstContourThings ; ii++)
1925                                 {
1926                                         ContourThing **contourthing = lstContourThings[ii];
1927                                         deleteContour( (*contourthing)->getName() );
1928                                 } //for ii
1929                         }// for z
1930                 } // if 
1931         }
1932
1933         //------------------------------------------------------------------------------------------------------------
1934         void wxContourEventHandler::openContours( FILE *pFile, bool staticContour )
1935         {
1936                 char tmp[255];
1937                 fscanf(pFile,"%s",tmp); // NumberOfContours 
1938                 fscanf(pFile,"%s",tmp); // ##
1939                 int numberOfContours = atoi(tmp);  
1940
1941                 std::vector<int> instantVector;
1942                 int typeContourModel;
1943                 manualContourModel *manModelContour;
1944                 int typeView;
1945
1946                 int i;
1947                 for (i=0;i<numberOfContours;i++)
1948                 {
1949                         instantVector.clear();
1950                         fscanf(pFile,"%s",tmp); // Instant
1951
1952                         fscanf(pFile,"%s",tmp); // 1
1953                         instantVector.push_back( atoi(tmp) );
1954                         fscanf(pFile,"%s",tmp); // 2
1955                         instantVector.push_back( atoi(tmp) );
1956                         fscanf(pFile,"%s",tmp); // 3
1957                         instantVector.push_back( atoi(tmp) );
1958                         fscanf(pFile,"%s",tmp); // 4
1959                         instantVector.push_back( atoi(tmp) );
1960                         fscanf(pFile,"%s",tmp); // 5
1961                         instantVector.push_back( atoi(tmp) );
1962                         fscanf(pFile,"%s",tmp); // 6
1963                         instantVector.push_back( atoi(tmp) );
1964
1965
1966                         fscanf(pFile,"%s",tmp); // TypeContourModel
1967                         fscanf(pFile,"%s",tmp); // ##
1968                         typeContourModel = atoi(tmp);
1969
1970                         manModelContour =  factoryManualContourModel(typeContourModel);
1971                         manModelContour->Open(pFile);
1972
1973                         fscanf(pFile,"%s",tmp); // TypeView
1974                         fscanf(pFile,"%s",tmp); // ##
1975                         typeView = atoi(tmp);  
1976
1977
1978 //                      if (typeView==1) 
1979 //                      {
1980 //                      }
1981
1982
1983                         std::string theName;
1984                         theName = _modelManager->createOutline( manModelContour, instantVector );
1985                         bool addedModel = theName.compare("") != 0;
1986                         if( addedModel )
1987                         {
1988                                 double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
1989                                 this->_theViewPanel->getSpacing(spc);                                   
1990                                 //Adding the manualContourControler to interface objects structure
1991                                 //Adding the manualViewContour to interface objects structure           
1992                                 //_sceneManager->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
1993                                 _sceneManager->configureViewControlTo( theName, manModelContour,spc , typeView) ;
1994                         }       
1995
1996                         if (staticContour==true)
1997                         {
1998                                 Instant instant(&instantVector);
1999                                 changeContourOfManager( theName , &instant );
2000                         }
2001
2002                 }// for  numberOfContours
2003
2004
2005
2006         }
2007
2008         //------------------------------------------------------------------------------------------------------------
2009         void wxContourEventHandler::openFileWithContours()
2010         {
2011                 char tmp[255];
2012                 wxFileDialog dialog(NULL, _T("Choose a file"), _T(""), _T(""), _T("*.roi"), wxOPEN );
2013                 if (dialog.ShowModal() == wxID_OK)
2014                 {
2015                         fileNameContourROI = (const char *)(dialog.GetPath().mb_str());
2016                         FILE *pFile=fopen(fileNameContourROI.c_str(),"r+");
2017
2018                         fscanf(pFile,"%s",tmp); // --CreaContour--
2019
2020                         fscanf(pFile,"%s",tmp); // Version
2021                         fscanf(pFile,"%s",tmp); // 1.0.1
2022                         std::string version(tmp);
2023
2024                         openContours(pFile,false);
2025                         if (version!="1.0.0"){
2026                                 openContours(pFile,true);
2027                         }
2028 //                        _sceneManager->openFileWithContours(ff);
2029
2030                         fclose(pFile);
2031                 }
2032                 _sceneManager->removeSceneContours();
2033                 changeInstant();
2034                 //updateInstantOutlines();
2035         }
2036
2037
2038         //------------------------------------------------------------------------------------------------------------
2039         void wxContourEventHandler::saveFileWithContours( std::string filename )
2040         {
2041                 FILE *pFile=fopen(filename.c_str(),"w+");
2042                 std::vector< std::string > lstNameThings;
2043                 int i,sizeLstNameThings; 
2044
2045                 fprintf(pFile,"--CreaContour--\n");
2046                 fprintf(pFile,"Version %s\n", "1.0.1" );
2047
2048                 // Normal Contours
2049                 lstNameThings           = _modelManager->GetLstNameThings();
2050                 sizeLstNameThings       = lstNameThings.size(); 
2051                 fprintf(pFile,"NumberOfContours %d\n", sizeLstNameThings );
2052                 for (i=0 ; i<sizeLstNameThings ; i++) 
2053                 {
2054                         _modelManager->SaveThingName( pFile, lstNameThings[i] );
2055                         _sceneManager->SaveThingName( pFile, lstNameThings[i] );
2056                 }// for i
2057
2058                 //-- Contours Statics 
2059                 lstNameThings           = _modelManager->GetLstNameThingsStatic();
2060                 sizeLstNameThings       = lstNameThings.size(); 
2061                 fprintf(pFile,"NumberOfContoursStatic %d\n", sizeLstNameThings );
2062                 for (i=0 ; i<sizeLstNameThings ; i++) 
2063                 {
2064                         _modelManager->SaveThingName( pFile, lstNameThings[i] );
2065                         _sceneManager->SaveThingName( pFile, lstNameThings[i] );
2066                 }// for i
2067                 fclose(pFile);
2068         }
2069
2070
2071         //----------------------------------------------------------
2072
2073         void wxContourEventHandler::saveFileWithContours()
2074         {
2075                 wxFileDialog dialog(NULL, _T("Choose a file"), _T(""), _T(""), _T("*.roi"), wxSAVE );
2076                 if (dialog.ShowModal() == wxID_OK)
2077                 {
2078                         fileNameContourROI = (const char *)(dialog.GetPath().mb_str());
2079                         saveFileWithContours( fileNameContourROI );
2080                 } // if  ShowModal
2081         } 
2082
2083         //------------------------------------------------------------------------------------------------------------
2084         void wxContourEventHandler :: saveFileWithContoursAutomatique()
2085         {
2086                 if (fileNameContourROI=="")
2087                 {
2088                         saveFileWithContours();
2089                 } else {
2090                         saveFileWithContours(fileNameContourROI);
2091                 }
2092         }
2093
2094
2095         //------------------------------------------------------------------------------------------------------------
2096         void wxContourEventHandler :: createCopyContourOf ( std::string anExistingKName, std::vector<int> &instantNoTouchData, bool append )
2097         {
2098                 std::string cloneName = _modelManager->createCopyContourOf( anExistingKName, instantNoTouchData );
2099                 manualContourModel * manualModel = _modelManager->getOutlineByKeyName( cloneName )->getModel();
2100                 _sceneManager->createCopyContourOf( anExistingKName, cloneName, manualModel , append );
2101         }
2102
2103
2104         //------------------------------------------------------------------------------------------------------------
2105         manualContourModel * wxContourEventHandler::factoryManualContourModel(int typeContour)
2106         {       
2107                 manualContourModel *manModelContour=NULL;
2108
2109                 // spline
2110                 if (typeContour==0)
2111                 {
2112                         manModelContour = new manualContourModel();
2113                 }
2114
2115                 // spline
2116                 if (typeContour==1)
2117                 {
2118                         manModelContour = new manualContourModel();
2119                 }
2120
2121                 // rectangle
2122                 if (typeContour==2)
2123                 {
2124                         manModelContour = new manualContourModelRoi();
2125                 }
2126
2127                 // circle
2128                 if (typeContour==3)
2129                 {
2130                         manModelContour = new manualContourModelCircle();
2131                 }
2132
2133                 // BullEye / star
2134                 if (typeContour==4)
2135                 {
2136                         manualContourModelBullEye *manModelContourBullEye = new manualContourModelBullEye();
2137                         manModelContour = manModelContourBullEye;
2138                         if (_panelBullEyeOptions!=NULL){
2139                                 int iCrown,sizeCrowns,iSector,sizeSectors;
2140                                 double radioA,radioB,ang,angDelta ;
2141                                 sizeCrowns = _panelBullEyeOptions->GetNumberOfCrowns();
2142                                 for ( iCrown=0 ; iCrown<sizeCrowns ; iCrown++ )
2143                                 {
2144                                         sizeSectors = _panelBullEyeOptions->GetNumberOfSections(iCrown);
2145                                         radioB  = _panelBullEyeOptions->GetRadioOfCrown(iCrown);
2146                                         if (iCrown==sizeCrowns-1)
2147                                         {
2148                                                 radioA  = 0;
2149                                         } else {
2150                                                 radioA  = _panelBullEyeOptions->GetRadioOfCrown(iCrown+1);
2151                                         }
2152                                         radioA=radioA/100.0;
2153                                         radioB=radioB/100.0;
2154                                         for ( iSector=0 ; iSector<sizeSectors  ; iSector++ )
2155                                         {
2156                                                 ang             = _panelBullEyeOptions->GetAngOfCrownSection(iCrown,iSector);
2157                                                 angDelta= _panelBullEyeOptions->GetAngDeltaOfCrownSection(iCrown);                                      
2158                                                 manModelContourBullEye->AddSector(radioA,radioB,ang,angDelta);
2159                                         } // for iSector
2160                                 } // for iCrown
2161                         } // if _panelBullEyeOptions
2162                 }// if typeContour==4
2163
2164                 return manModelContour;
2165         }
2166
2167         //------------------------------------------------------------------------------------------------------------
2168         void wxContourEventHandler :: createContour( int typeContour )
2169         {
2170                 //Creating the manualContourModel and including in the model
2171                 manualContourModel * manModelContour = factoryManualContourModel(typeContour);
2172                 std::vector<int> instantVector;
2173                 _instantPanel->getInstant( instantVector );
2174                 std::string theName;
2175                 theName= _modelManager->createOutline( manModelContour, instantVector );
2176                 bool addedModel = theName.compare("") != 0;
2177                 if( addedModel )
2178                 {
2179                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
2180                         this->_theViewPanel->getSpacing(spc);                                   
2181                         //Adding the manualContourControler to interface objects structure
2182                         //Adding the manualViewContour to interface objects structure           
2183                         //_sceneManager->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
2184                         _sceneManager->configureViewControlTo( theName, manModelContour,spc , typeContour) ;
2185                 }       
2186
2187 //EED Borrame
2188 //FILE *ff = fopen("c:/temp/wxVtkBaseView_SceneManagerStadistics.txt","a+");
2189 //fprintf(ff,"EED wxContourEventHandler::createContour() \n" );
2190 //fprintf(ff,"    %s\n %p ",theName.c_str() ,  );
2191 //fclose(ff);
2192
2193
2194         }
2195
2196         //------------------------------------------------------------------------------------------------------------
2197         //  Attributes getters and setters
2198         //------------------------------------------------------------------------------------------------------------
2199
2200         void wxContourEventHandler :: setModelManager( OutlineModelManager * theModelManager )
2201         {
2202                 _modelManager = theModelManager;
2203                 _workSpace = _modelManager->getContourWorkspace();
2204         }
2205         //------------------------------------------------------------------------------------------------------------  
2206         void wxContourEventHandler :: setViewPanel(  wxContourViewPanel * theViewPanel )
2207         {
2208                 _theViewPanel = theViewPanel;
2209                 _theViewPanel->setWxEventHandler( this );
2210
2211                 double spc[3];
2212                 this->_theViewPanel->getSpacing(spc);
2213                 _sceneManager           = new wxVtkBaseView_SceneManager ( this->_theViewPanel->getWxVtkBaseView(), this, spc  );
2214                 
2215                 Connect( _theViewPanel->GetId(), wxINSTANT_CHANGE, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onChangeInstant );
2216         Connect( wxID_ANY, wxEVT_START_CREATE_MULT_ROI, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onCreateMultipleROI );
2217                 Connect( wxID_ANY, wxEVT_START_CREATE_ROI, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onCreateROI );
2218                 Connect( wxID_ANY, wxEVT_STOP_CREATE_ROI, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onStopCreateROI );
2219                 Connect( wxID_ANY, wxEVT_CHANGED_DEEP, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onChangedDeep );
2220                 Connect( wxID_ANY,  wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler :: onActionButtonPressed ); 
2221
2222                 
2223         }
2224
2225         //------------------------------------------------------------------------------------------------------------
2226         void wxContourEventHandler :: setInstantChooserPanel(  wxInstantChooserPanel * theInstantChooserPanel )
2227         {
2228                 _instantPanel = theInstantChooserPanel;
2229                 _instantPanel->setWxEventHandler( this );
2230
2231                 Connect( _instantPanel->GetId(), wxINSTANT_CHOOSER_CHANGE, (wxObjectEventFunction) (wxCommandEventFunction)  &wxContourEventHandler::onChangeInstant );
2232
2233                 std::vector<int> vect;
2234                 _instantPanel->getInstant( vect );
2235                 _actualInstant = new Instant( &vect );
2236
2237                 if( _modelManager!=NULL )
2238                 {               
2239                         std::vector<std::string> conceptNameVect;
2240                         std::vector<int> conceptSizeVect;
2241                         _modelManager-> getConceptsInformation(conceptNameVect, conceptSizeVect);
2242                         int i=0;
2243                         int max = conceptNameVect.size();                       
2244                         for( ; i<max; i++)
2245                         {
2246                                 if( conceptNameVect[i].compare("Axe Depth") == 0 )
2247                                 {
2248                                         _instantPanel->addConcept( conceptNameVect[i], 0, conceptSizeVect[i], 1);
2249                                 } else {
2250                                         _instantPanel->addConcept( conceptNameVect[i], 1, conceptSizeVect[i], 1);
2251                                 } // if
2252                         } // for                
2253                 } else {
2254                         //Should be configured later, or abort program, because Model -NEEDS- to be added before chooserPanel
2255                 }
2256                 if( _theViewPanel!=NULL )
2257                 {
2258                         double val = _theViewPanel->getCurrentDeep();
2259                         _instantPanel->setConceptValue( "Axe Depth", (int)val );
2260                         ConceptDataWrap * data = _instantPanel->getConceptDataOf( "Axe Depth" );
2261                         changeInstant();                        
2262                         _theViewPanel->setVerticalConcept( "Axe Depth", data->getMinValue(), data->getMaxValue(), data->getMinShowedValue(),  data->getMaxShowedValue(), data->getActualValue() );                      
2263                 } else {
2264                         //Should be configured later, or abort program, because ViewPanel -NEEDS- to be added before chooserPanel
2265                 }// if
2266         } 
2267
2268
2269         //------------------------------------------------------------------------------------------------------------
2270         void wxContourEventHandler :: setButtonsBar(  wxContour_ButtonsBar * theButtonsBar )
2271         {
2272                 _buttonsBar = theButtonsBar;
2273                 _buttonsBar->setWxEventHandler( this );
2274                 Connect( _buttonsBar->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler:: onActionButtonPressed );               
2275         }
2276         //------------------------------------------------------------------------------------------------------------
2277         void wxContourEventHandler :: setGrid(  wxContour_Grid * theGridPanel )
2278         {
2279                 _gridPanel = theGridPanel;
2280         }
2281         //------------------------------------------------------------------------------------------------------------
2282         void wxContourEventHandler :: setDrawToolsPanel(  wxContour_DrawToolsPanel * theDrawToolsPanel )
2283         {
2284                 _drawToolsPanel = theDrawToolsPanel;
2285         }
2286         //------------------------------------------------------------------------------------------------------------
2287         void wxContourEventHandler :: setOperationsToolsPanel(  wxContour_OperationsToolsPanel * theOperationsToolsPanel )
2288         {       
2289                 _operationsToolsPanel = theOperationsToolsPanel;
2290         }
2291         //------------------------------------------------------------------------------------------------------------
2292         void wxContourEventHandler :: setAutomaticFormsPanel(  wxContour_AutomaticFormsToolsPanel * theAutoFormsPanel )
2293         {
2294                 _autoFormsPanel = theAutoFormsPanel;
2295         }
2296
2297         void wxContourEventHandler :: setStandardToolsPanel(  wxContour_StandardToolsPanel * theStandardToolsPanel )
2298         {
2299                 _standardToolsPanel= theStandardToolsPanel;
2300         }
2301         //------------------------------------------------------------------------------------------------------------
2302         void wxContourEventHandler :: setEditionToolsPanel(  wxContour_EdtionToolsPanel * theEditionToolsPanel )
2303         {
2304                 _editionToolsPanel =  theEditionToolsPanel;
2305         }
2306
2307         //------------------------------------------------------------------------------------------------------------
2308         void wxContourEventHandler :: setListViewPanel(  wxContour_ListViewPanel * theListViewPanel )
2309         {
2310                 _listViewPanel = theListViewPanel;
2311         }
2312         //------------------------------------------------------------------------------------------------------------
2313         //  Other functional methods
2314         //------------------------------------------------------------------------------------------------------------
2315
2316
2317         //------------------------------------------------------------------------------------------------------------
2318         void wxContourEventHandler::onDeleteContour( wxCommandEvent& event )
2319         {
2320                 int i,size=_sceneManager->getSelectedObjects().size();
2321                 for(i=0;i<size;i++)
2322                 {
2323                         std::string keyName             = _sceneManager->getSelectedObjects()[i];
2324                         int ispartofstaticlist  = this->_modelManager->IsPartOfStaticList( keyName );
2325                         if ( ispartofstaticlist>=0 )
2326                         {
2327                                 std::vector<int> tempVector;
2328                                 _instantPanel->getInstant( tempVector );
2329                                 Instant instant(&tempVector);
2330                                 this->_modelManager->ChangeContourOfList(keyName, &instant);
2331                         }
2332                 }
2333
2334                 std::vector<std::string> lstKeyName;
2335                 std::vector<std::string> lstKeyNameActualSlice;
2336                 std::vector<std::string> lstKeyNameToBeErase;
2337
2338                 lstKeyNameToBeErase             = _sceneManager->getSelectedObjects();
2339                 lstKeyNameActualSlice   = _sceneManager->GetlstContoursNameActualSlice();
2340                 int k,kSize=lstKeyNameToBeErase.size();
2341                 int j,jSize=lstKeyNameActualSlice.size();
2342                 bool ok;
2343                 for (k=0;k<kSize; k++)
2344                 {
2345                         ok=false;
2346                         for (j=0;j<jSize; j++)
2347                         {
2348                                 if (lstKeyNameToBeErase[k]==lstKeyNameActualSlice[j])
2349                                 { 
2350                                         ok=true;
2351                                 }
2352                         } // for j
2353                         if (ok==true)
2354                         {
2355                                 lstKeyName.push_back( lstKeyNameToBeErase[k] );
2356                         } // if ok
2357                 } // for k
2358
2359                 deleteContours( lstKeyName );
2360         }
2361
2362         //------------------------------------------------------------------------------------------------------------
2363         void wxContourEventHandler::onDeleteContoursActSlice( wxCommandEvent& event )
2364         {
2365                 deleteContours( _sceneManager->GetlstContoursNameActualSlice() );
2366         }
2367
2368         //------------------------------------------------------------------------------------------------------------
2369         void wxContourEventHandler::onDeleteAllContours( wxCommandEvent& event )        
2370         {
2371                 deleteAllContours(  );
2372         }
2373
2374                 //------------------------------------------------------------------------------------------------------------
2375         void wxContourEventHandler :: onDeleteFrame( )
2376         {
2377                 if (_deleteFrame==NULL)
2378                 {
2379                         _deleteFrame                                            = new wxFrame (_theViewPanel, -1,_T("  Delete Contour "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
2380                         int width=300;
2381                         wxSize sizepanel(width,230);
2382                         _deleteFrame->SetSize( sizepanel );
2383                         wxPanel *panel                                          = new wxPanel(_deleteFrame,-1);
2384                         wxButton *deleteContourBtn                      = new wxButton(panel,-1,_T("Delete contour selected"),wxDefaultPosition, wxSize(width-10,35) );
2385                         wxButton *deleteContoursActSliceBtn     = new wxButton(panel,-1,_T("Delete contours of actual slice"),wxDefaultPosition, wxSize(width-10,35));
2386                         wxButton *deleteAllContoursBtn          = new wxButton(panel,-1,_T("Delete all contours"),wxDefaultPosition, wxSize(width-10,35));
2387
2388                         int sizeZ = _sceneManager->GetImageDataSizeZ();
2389                         _mbarrangeDeleteAll                                     =  new mBarRange(panel,70, 65);
2390                         _mbarrangeDeleteAll->SetMin(0);
2391                         _mbarrangeDeleteAll->SetStart(0);
2392
2393                         _mbarrangeDeleteAll-> SetOrientation( true );
2394                         _mbarrangeDeleteAll-> setActiveStateTo(true);
2395                         _mbarrangeDeleteAll-> setVisibleLabels( true );
2396                         _mbarrangeDeleteAll-> setDeviceEndMargin(10);
2397                         _mbarrangeDeleteAll-> setRepresentedValues( 0 , sizeZ );
2398                         _mbarrangeDeleteAll-> setDeviceBlitStart(10,10); 
2399                         _mbarrangeDeleteAll-> setIfWithActualDrawed( false );
2400                         _mbarrangeDeleteAll-> SetStart( 0 );
2401                         _mbarrangeDeleteAll-> SetEnd( sizeZ );  
2402
2403
2404                         _deleteFrame->SetEventHandler( this );
2405                         Connect( deleteContourBtn->GetId()                      ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onDeleteContour ); 
2406                         Connect( deleteContoursActSliceBtn->GetId()     ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onDeleteContoursActSlice ); 
2407                         Connect( deleteAllContoursBtn->GetId()          ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onDeleteAllContours ); 
2408
2409                         wxFlexGridSizer * sizer                         = new wxFlexGridSizer(1);
2410                         sizer -> Add( deleteContourBtn                          , 1, wxGROW );
2411                         sizer -> Add( deleteContoursActSliceBtn         , 1, wxGROW );
2412                         sizer -> Add( deleteAllContoursBtn                      , 1, wxGROW );
2413                         sizer -> Add( _mbarrangeDeleteAll                       , 1, wxGROW );
2414
2415                         panel->SetSizer( sizer );
2416                         panel->SetSize( sizepanel );
2417                         panel->SetAutoLayout( true );
2418                         panel->Layout();
2419                         _deleteFrame->Show(true);
2420                 } else {
2421                         if (_deleteFrame->IsShown()==true)
2422                         {
2423                                 _deleteFrame->Show(false);
2424                         } else  {
2425                                 _deleteFrame->Show(true);
2426                         }
2427                 }
2428         }
2429
2430
2431 //------------------------------------------------------------------------------------------------------------
2432 void wxContourEventHandler::onCreateContourSpline( wxCommandEvent& event )
2433 {
2434         createContour( 1 );
2435 }
2436
2437 //------------------------------------------------------------------------------------------------------------
2438 void wxContourEventHandler::onCreateContourRectangle( wxCommandEvent& event )
2439 {
2440         createContour( 2 );
2441 }
2442
2443 //------------------------------------------------------------------------------------------------------------
2444 void wxContourEventHandler::onCreateContourCircle( wxCommandEvent& event )
2445 {
2446         createContour( 3 );
2447 }
2448 //------------------------------------------------------------------------------------------------------------
2449 void wxContourEventHandler::onCreateContourBullEye( wxCommandEvent& event )
2450 {
2451         createContour( 4 );
2452 }
2453
2454
2455
2456 //------------------------------------------------------------------------------------------------------------
2457         void wxContourEventHandler :: onCreateContourFrame( )
2458         {
2459                 if (_createContourFrame==NULL)
2460                 {
2461                         _createContourFrame                                             = new wxFrame (_theViewPanel, -1,_T("  New Contour  "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT | wxRESIZE_BORDER );
2462                         wxSize sizepanel(490,600);
2463                         _createContourFrame->SetSize( sizepanel );
2464                         wxPanel *panel                                          = new wxPanel(_createContourFrame,-1);
2465                         wxButton *newContourSplineBtn           = new wxButton(panel,-1,_T("Interpolation B-spline (close)"),wxDefaultPosition, wxSize(180,35) );
2466                         newContourSplineBtn->SetToolTip(_T("CTRL-N"));
2467                         wxButton *newContourRectangleBtn        = new wxButton(panel,-1,_T("Rectangle"),wxDefaultPosition, wxSize(180,35));
2468                         wxButton *newContourCircleBtn           = new wxButton(panel,-1,_T("Circle"),wxDefaultPosition, wxSize(180,35));
2469                         wxButton *newContourStarBtn                     = new wxButton(panel,-1,_T("Bull eye"),wxDefaultPosition, wxSize(180,35));
2470
2471                         _panelBullEyeOptions = new PanelBullEyeOptions(panel, wxSize(100,200));
2472
2473                         _createContourFrame->SetEventHandler( this );
2474                         Connect( newContourSplineBtn->GetId()           ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onCreateContourSpline    ); 
2475                         Connect( newContourRectangleBtn->GetId()        ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onCreateContourRectangle ); 
2476                         Connect( newContourCircleBtn->GetId()           ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onCreateContourCircle    ); 
2477                         Connect( newContourStarBtn->GetId()                     ,       wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxContourEventHandler::onCreateContourBullEye   ); 
2478
2479                         wxFlexGridSizer * sizer                         = new wxFlexGridSizer(1);
2480                         sizer -> Add( newContourSplineBtn                       , 1, wxGROW );
2481                         sizer -> Add( newContourRectangleBtn            , 1, wxGROW );
2482                         sizer -> Add( newContourCircleBtn                       , 1, wxGROW );
2483                         sizer -> Add( newContourStarBtn                         , 1, wxGROW );
2484                         sizer -> Add( _panelBullEyeOptions                      , 1, wxGROW );
2485
2486                         panel->SetSizer( sizer );
2487                         panel->SetSize( sizepanel );
2488                         panel->SetAutoLayout( true );
2489                         panel->Layout();
2490                         _createContourFrame->Show(true);
2491                 } else {
2492                         if (_createContourFrame->IsShown()==true)
2493                         {
2494                                 _createContourFrame->Show(false);
2495                         } else  {
2496                                 _createContourFrame->Show(true);
2497                         }
2498                 }
2499         }
2500
2501
2502 //------------------------------------------------------------------------
2503 void wxContourEventHandler::RefreshInterface()
2504 {
2505         changeInstant();
2506         wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
2507         wxvtk2dbaseview->Refresh();
2508 }
2509
2510 //------------------------------------------------------------------------
2511 void wxContourEventHandler::onWidthOfContour(wxScrollEvent& event){
2512         _sceneManager->removeSceneContours();
2513         double width = (double)_withOfContourLine->GetValue() / 2.0;
2514         _sceneManager->SetWidthContour( width );
2515
2516         RefreshInterface();
2517 }
2518
2519 //------------------------------------------------------------------------
2520
2521 void wxContourEventHandler::onBrigthnessColorWindowLevel(wxScrollEvent& event){
2522         int colorwindow                         = _brithtnessColorLevel->GetValue();
2523         int windowlevel                         = _brithtnessWindowLevel->GetValue();
2524         wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
2525         vtkImageViewer2 *imageviewer = wxvtk2dbaseview->_imageViewer2XYZ->GetVtkImageViewer2();
2526         imageviewer->SetColorWindow(colorwindow);
2527         imageviewer->SetColorLevel(windowlevel);        
2528 //      wxvtk2dbaseview->Refresh();
2529         RefreshInterface();
2530 }
2531
2532 void wxContourEventHandler::OnInterpolation(wxCommandEvent& event)
2533 {
2534         wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
2535         vtkImageViewer2 *imageviewer = wxvtk2dbaseview->_imageViewer2XYZ->GetVtkImageViewer2();
2536         if (_interpolationCheckBox->GetValue()==true)
2537         {
2538                 imageviewer->GetImageActor()->InterpolateOn();
2539         } else {
2540                 imageviewer->GetImageActor()->InterpolateOff();
2541         }
2542         RefreshInterface();
2543 }
2544
2545
2546 //------------------------------------------------------------------------------------------------------------
2547         void wxContourEventHandler :: onInterfaceConfigurationFrame( )
2548         {
2549                 if (_interfaceConfigurationFrame==NULL)
2550                 {
2551                         _interfaceConfigurationFrame                    = new wxFrame (_theViewPanel, -1,_T(" Interface Configuration "), wxDefaultPosition, wxDefaultSize, wxFRAME_TOOL_WINDOW|wxSYSTEM_MENU | wxCAPTION |  wxCLIP_CHILDREN |wxFRAME_FLOAT_ON_PARENT );
2552                         wxSize sizepanel(190,400);
2553                         _interfaceConfigurationFrame->SetSize( sizepanel );
2554                         wxPanel *panel                                                  = new wxPanel(_interfaceConfigurationFrame,-1);
2555                         _withOfContourLine                                              = new wxSlider(panel, -1, 1 , 1, 10, wxDefaultPosition, wxSize(180,40), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
2556
2557                         double range[2];
2558                         this->_sceneManager->GetImageDataRange(range);
2559
2560                         wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
2561                         vtkImageViewer2 *imageviewer = wxvtk2dbaseview->_imageViewer2XYZ->GetVtkImageViewer2();
2562                         int colorwindow = (int)imageviewer->GetWindowLevel()->GetLevel();
2563                         int windowlevel = (int)imageviewer->GetWindowLevel()->GetWindow();      
2564
2565                         _brithtnessWindowLevel                                  = new wxSlider(panel, -1, windowlevel , 1, range[1], wxDefaultPosition, wxSize(180,40), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
2566                         _brithtnessColorLevel                                   = new wxSlider(panel, -1, colorwindow , 1, range[1], wxDefaultPosition, wxSize(180,40), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
2567
2568                         _interpolationCheckBox = new wxCheckBox(panel, -1, _T("Image interpolation") );
2569                         _interpolationCheckBox->SetValue(true);
2570
2571                         _interfaceConfigurationFrame->SetEventHandler( this );
2572
2573                         Connect( _withOfContourLine->GetId()            ,       wxEVT_SCROLL_CHANGED    , (wxObjectEventFunction) &wxContourEventHandler::onWidthOfContour      ); 
2574                         Connect( _withOfContourLine->GetId()            ,       wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxContourEventHandler::onWidthOfContour      ); 
2575
2576                         Connect( _brithtnessWindowLevel->GetId()        ,       wxEVT_SCROLL_CHANGED    , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
2577                         Connect( _brithtnessWindowLevel->GetId()        ,       wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
2578                         Connect( _brithtnessColorLevel->GetId()         ,       wxEVT_SCROLL_CHANGED    , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
2579                         Connect( _brithtnessColorLevel->GetId()         ,       wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxContourEventHandler::onBrigthnessColorWindowLevel  ); 
2580                         Connect( _interpolationCheckBox->GetId()        ,       wxEVT_COMMAND_CHECKBOX_CLICKED    , (wxObjectEventFunction) &wxContourEventHandler::OnInterpolation             );
2581
2582                         wxFlexGridSizer * sizer                                 = new wxFlexGridSizer(1);
2583                         sizer -> Add( new wxStaticText(panel,-1,_T("Contour width"))  , 1, wxGROW );
2584                         sizer -> Add( _withOfContourLine                , 1, wxGROW );
2585                         sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
2586                         sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
2587                         sizer -> Add( new wxStaticText(panel,-1,_T("Brightness of the image"))  , 1, wxGROW );
2588                         sizer -> Add( new wxStaticText(panel,-1,_T("--Window level--"))  , 1, wxGROW );
2589                         sizer -> Add( _brithtnessWindowLevel            , 1, wxGROW );
2590                         sizer -> Add( new wxStaticText(panel,-1,_T("--Color level--"))  , 1, wxGROW );
2591                         sizer -> Add( _brithtnessColorLevel, 1, wxGROW );
2592                         sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
2593                         sizer -> Add( new wxStaticText(panel,-1,_T(" "))  , 1, wxGROW );
2594                         sizer -> Add( _interpolationCheckBox            , 1, wxGROW );
2595
2596                         panel->SetSizer( sizer );
2597                         panel->SetSize( sizepanel );
2598                         panel->SetAutoLayout( true );
2599                         panel->Layout();
2600                         _interfaceConfigurationFrame->Show(true);
2601                 } else {
2602                         if (_interfaceConfigurationFrame->IsShown()==true)
2603                         {
2604                                 _interfaceConfigurationFrame->Show(false);
2605                         } else  {
2606                                 _interfaceConfigurationFrame->Show(true);
2607                         }
2608                 }
2609         }
2610
2611
2612         
2613
2614 //------------------------------------------------------------------
2615         void wxContourEventHandler :: changeContourOfManager(std::string keyName, Instant *instant)
2616         {
2617                 this->_modelManager->ChangeContourOfList(keyName, instant);
2618         }
2619
2620 //------------------------------------------------------------------
2621         void wxContourEventHandler :: changeContoursOfManager(  std::vector<std::string> & keyNamesVector  )
2622         {
2623
2624                 std::vector<int> tempVector;
2625                 _instantPanel->getInstant( tempVector );
2626                 Instant instant(&tempVector);
2627
2628                 int i , size=keyNamesVector.size();
2629                 for ( i=0 ; i<size ; i++ )
2630                 {
2631                         changeContourOfManager(keyNamesVector[i], &instant);
2632                 } // i
2633         }
2634
2635
2636 //------------------------------------------------------------------
2637         void wxContourEventHandler :: executeActionCommand( const char  toolCommand )
2638         {
2639                 std::vector<std::string> currentSelection = _sceneManager->getSelectedObjects();
2640                 int elementsSelected = currentSelection.size();
2641                 if( toolCommand == (wxContour_ActionCommnadsID::CREATE_TOOL) )
2642                 {
2643                         onCreateContourFrame();
2644                 }
2645                 if( toolCommand == (wxContour_ActionCommnadsID::CREATE_CONTOUR_KEY) )
2646                 {
2647                         createContour();
2648                 }
2649                 else if( toolCommand == wxContour_ActionCommnadsID::DELETE_TOOL )
2650                 {
2651                         onDeleteFrame( );
2652                 }
2653                 else if( toolCommand == wxContour_ActionCommnadsID::DELETE_KEY )
2654                 {
2655                         deleteContours( currentSelection );
2656                 }
2657                 else if( toolCommand == wxContour_ActionCommnadsID::SAVE_TOOL )
2658                 {
2659                         saveFileWithContours(  );
2660                 }
2661                 else if( toolCommand == wxContour_ActionCommnadsID::SAVE_KEY )
2662                 {
2663                         saveFileWithContoursAutomatique(  );
2664                 }
2665                 else if( toolCommand == wxContour_ActionCommnadsID::OPEN_TOOL )
2666                 {
2667                         openFileWithContours(  );
2668                 }
2669                 else if( toolCommand == wxContour_ActionCommnadsID::CHANGE_TOOL )
2670                 {
2671                         if ( elementsSelected >= 1 )
2672                         {
2673                                 changeContoursOfManager( currentSelection );
2674                         }
2675
2676                 }
2677                 else if( toolCommand == wxContour_ActionCommnadsID::HIDE_TOOL )
2678                 {
2679                         if ( elementsSelected >= 1 )
2680                         {
2681                                 onHideOutlines( currentSelection );
2682                         }                       
2683                 }
2684                 else if( toolCommand == wxContour_ActionCommnadsID::SHOW_TOOL )
2685                 {
2686                         if ( elementsSelected >= 1 )
2687                         {
2688                                 onShowOutlines( currentSelection );
2689                         }
2690                 }
2691                 else if( toolCommand == wxContour_ActionCommnadsID::COPY_TOOL )
2692                 {
2693                         if ( elementsSelected >= 1 )
2694                         {
2695                                 onCopyOutlines( currentSelection );
2696                         }
2697                 }
2698                 else if( toolCommand == wxContour_ActionCommnadsID::PASTE_TOOL )
2699                 {
2700                         onPasteOutlines(  );                    
2701                 }
2702                 else if( toolCommand == wxContour_ActionCommnadsID::SPREAD_TOOL )
2703                 {
2704                                 onSpread();
2705                 }
2706                 else if( toolCommand == wxContour_ActionCommnadsID::AUTOMATIQUESEGMENTATION_TOOL )
2707                 {
2708                         onAutomatiqueSegmentation(  );                  
2709                 }
2710
2711                 else if( toolCommand == wxContour_ActionCommnadsID::SELECT_TOOL )
2712                 {
2713                         onSelectOutlines( currentSelection );
2714                         
2715                 }
2716                 else if( toolCommand == wxContour_ActionCommnadsID::EDIT_TOOL )
2717                 {
2718                         if ( elementsSelected >= 1 )
2719                         {
2720                                 onEditOutlines( currentSelection );
2721                         }
2722                 }
2723                 else if( toolCommand == wxContour_ActionCommnadsID::UNDO_TOOL )
2724                 {
2725                         onUNDO();
2726                 }
2727                 else if( toolCommand == wxContour_ActionCommnadsID::REDO_TOOL )
2728                 {
2729                         onREDO();
2730                 }               
2731                 else if( toolCommand == wxContour_ActionCommnadsID::INFORMATIONCONTOUR_TOOL )
2732                 {
2733                         onInformationContourFrame();
2734                 }               
2735                 else if( toolCommand == wxContour_ActionCommnadsID::INTERFACECONFIGURATION_TOOL )
2736                 {
2737                         onInterfaceConfigurationFrame();
2738                 }
2739                 else if( toolCommand == wxContour_ActionCommnadsID::TEST_TOOL )
2740                 {
2741                         onInterfaceTestFrame();
2742                 }               
2743
2744
2745         }
2746
2747         
2748         //------------------------------------------------------------------------------------------------------------
2749         //  Getting information methods
2750         //------------------------------------------------------------------------------------------------------------
2751
2752
2753
2754         //------------------------------------------------------------------------------------------------------------
2755         //  Private methods
2756         //------------------------------------------------------------------------------------------------------------
2757    
2758         void wxContourEventHandler :: updateInstantOutlines()
2759         {
2760                 std::vector<NameWrapper *> namesWrapping = _modelManager->getActualInstantOutlines();
2761                 int size = namesWrapping.size();
2762                 _sceneManager->removeSceneContours();
2763                 for( int i=0; i<size; i++)
2764                 {                       
2765                         _sceneManager->addToScene(namesWrapping[i]->getKeyName(), true, true, true, false, false );
2766                 }
2767         }
2768         //------------------------------------------------------------------------------------------------------------
2769         void wxContourEventHandler :: updateInstantImageData()
2770         {
2771                 
2772         }
2773         //------------------------------------------------------------------------------------------------------------
2774
2775         void wxContourEventHandler :: updateInstantAxes()
2776         {
2777                 
2778         }
2779         //------------------------------------------------------------------------------------------------------------  
2780         void wxContourEventHandler :: changeInstant()
2781         {
2782                 std::vector<int> instantVect;
2783                 _instantPanel->getInstant( instantVect );
2784                 Instant * theInstant = new Instant ( &instantVect );
2785                 //if( !theInstant->isEquals( _actualInstant ))
2786                 {       
2787                         //Setting the actual instant
2788                         _actualInstant = theInstant;
2789                         _modelManager->setInstant( _actualInstant );
2790                         
2791                         updateInstantOutlines();
2792                         updateInstantImageData();
2793                         updateInstantAxes();
2794
2795                         //Searching the elements at the specified instant
2796
2797                         /*
2798                         std::map<std::string, ContourWrap_ViewControl *> :: iterator iter;
2799                         iter = contours_ViewControl->find("Outline 0");
2800                         (iter->second)->getViewer()->RemoveCompleteContourActor();*/
2801                 }
2802         }
2803