]> Creatis software - creaContours.git/blob - lib/Interface_ManagerContour_NDimensions/wxContourMainFrame.cxx
Version with out wxEventHandler
[creaContours.git] / lib / Interface_ManagerContour_NDimensions / wxContourMainFrame.cxx
1 //----------------------------------------------------------------------------------------------------------------
2 // Class definition include
3 //----------------------------------------------------------------------------------------------------------------
4 #include "wxContourMainFrame.h"
5
6
7
8 //----------------------------------------------------------------------------------------------------------------
9 // Includes
10 //----------------------------------------------------------------------------------------------------------------
11
12
13 #include "NameWrapper.h"
14 #include "wx/artprov.h"
15 #include "ConceptDataWrap.h"
16
17 #include <manualContour.h>
18 //#include "ImageSourceThing.h"
19 //#include "wxContour_ActionCommandsID.h"
20 //#include "OutlineModelBuilder.h"
21 //#include "wxContourEventHandler.h"
22
23 //----------------------------------------------------------------------------------------------------------------
24 // Class implementation
25 //----------------------------------------------------------------------------------------------------------------
26 /** @file wxContourMainFrame.cxx */
27
28         //------------------------------------------------------------------------------------------------------------
29         // Constructors & Destructors
30         //------------------------------------------------------------------------------------------------------------
31
32 wxContourMainFrame* wxContourMainFrame:: instance = NULL;
33 char wxContourMainFrame::COPY = 'C';
34                 
35         wxContourMainFrame :: wxContourMainFrame(wxWindow* parent, wxWindowID id,const wxString& title,const wxPoint& pos,const wxSize& size,long style)
36                 //: wxPanel(parent, id, title, pos, size, style)
37                 //: wxWindow(parent, id, pos, size, style)
38                 : wxPanel(parent, id, pos, size, style)
39         {
40                 m_mgr.SetManagedWindow(this);
41                 _creatingContoursActive         = false;
42                 _theViewPanel                           = NULL;
43 //              _modelManager                           = NULL;
44                 _instantPanel                           = NULL;
45                 _buttonsBar                                     = NULL;
46                 _gridPanel                                      = NULL; 
47                 _drawToolsPanel                         = NULL;
48                 _operationsToolsPanel           = NULL;
49                 _autoFormsPanel                         = NULL; 
50                 _standardToolsPanel                     = NULL;
51                 _editionToolsPanel                      = NULL;
52                 _listViewPanel                          = NULL;
53                 _sceneManager                           = NULL;
54                 //_actualInstant                                = NULL;
55                 _numberOfVariablesStatistics = 6+1;
56
57                 // set up default notebook style
58                 m_notebook_style =wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER;
59                 m_notebook_theme = 0;   
60                 //wxContour_ActionCommandsID a;
61
62         }
63
64 wxContourMainFrame :: wxContourMainFrame(wxWindow* parent, wxWindowID id,const wxString& title,const wxPoint& pos,const wxSize& size,std::vector<vtkImageData*> images, long style)             
65                 : wxPanel(parent, id, pos, size, style)
66         {
67                 m_mgr.SetManagedWindow(this);
68                 _creatingContoursActive         = false;
69                 _theViewPanel                           = NULL;
70 //              _modelManager                           = NULL;
71                 _instantPanel                           = NULL;
72                 _buttonsBar                                     = NULL;
73                 _gridPanel                                      = NULL; 
74                 _drawToolsPanel                         = NULL;
75                 _operationsToolsPanel           = NULL;
76                 _autoFormsPanel                         = NULL; 
77                 _standardToolsPanel                     = NULL;
78                 _editionToolsPanel                      = NULL;
79                 _listViewPanel                          = NULL;
80                 _sceneManager                           = NULL;
81                 //_actualInstant                                = NULL;
82
83                 // set up default notebook style
84                 m_notebook_style =wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER;
85                 m_notebook_theme = 0;   
86                 //wxContour_ActionCommandsID a;
87                 _numberOfVariablesStatistics = 6+1;
88
89
90                 _performingOperation                    = new PerformingOperation();
91 //JCP 17 - 10 - 2008
92
93         inredo = 0;
94         inundo = 0;
95
96         kernelManager = new KernelManagerContour(images);
97         //kernelManager->setVectImages(images);
98         //kernelManager->initializeEnvironment();
99                         
100         //vtkImageData* selectedimage = kernelManager->getVectImages()[0];
101         
102 //-------------------------------------------------------------
103
104         /*frame = new wxContourMainFrame(_builder->getImSourceEnv(), _builder->getImSectionEnv(), _builder->getAxesEnv(),  _builder->getContourEnv(),NULL, wxID_ANY, wxT("ROI Application Sample"), wxPoint(50,50), wxSize(800, 600)); 
105         frame->Show(TRUE);*/
106
107         //Creating the evtHandler of the panels
108         //wxContourEventHandler * eventHandler = new wxContourEventHandler();
109
110         //Creating the window that will show the panels
111
112         //Getting the parent for the panels ( using aui )
113         wxAuiNotebook * notebook = this->createNotebook();
114
115         //JCP 17 - 11 - 08
116         //wxInstantChooserPanel * instantPanel                          = new wxInstantChooserPanel( notebook, "Instant Chooser", true);
117         //JCP 17 - 11 - 08
118         std::vector<std::string> conceptNameVect;
119         std::vector<int> conceptSizeVect;
120         
121
122
123         _instantPanel                           = new wxInstantChooserPanel( notebook, "Instant Chooser", true, false, "c" );
124         _theViewPanel                           = new wxContourViewPanel( kernelManager->getSourceImage(), notebook );  
125
126
127
128     kernelManager->getConceptsInformation(conceptNameVect, conceptSizeVect);
129         _instantPanel->addConcepts(conceptNameVect, conceptSizeVect);
130                 
131
132         double val = _theViewPanel->getCurrentDeep();
133         _instantPanel->setConceptValue( "Axe Depth", (int)val );
134         //JCP _instantPanel->setConceptValue("time", num de imagenes);
135         ConceptDataWrap * data = _instantPanel->getConceptDataOf( "Axe Depth" );
136         //*******************changeInstant();
137         _theViewPanel->setVerticalConcept( "Axe Depth", data->getMinValue(), data->getMaxValue(), data->getMinShowedValue(),  data->getMaxShowedValue(), data->getActualValue() );
138
139         //eventHandler->setModelManager( kernelManager->getOutlineModelManager() );
140         //eventHandler->setViewPanel( _theViewPanel );
141         //_theViewPanel->initializeScenceManager();
142         //eventHandler->setInstantChooserPanel( _instantPanel );
143         
144         //bool successConfiuration = eventHandler->configureEventsHandling();
145
146         //successConfiuration &= this->configurePanels( notebook );
147         this->configurePanels( notebook );
148
149         interfMainPanel* pannew = interfMainPanel::getInstance(parent);//, eventHandler);
150
151 //JCP 17 -10 - 2008
152
153
154         }
155
156
157         wxContourMainFrame* wxContourMainFrame :: getInstance(wxWindow* parent, wxWindowID id,const wxString& title,const wxPoint& pos,const wxSize& size,std::vector<vtkImageData*> images, long style ){
158                 if(instance == NULL){
159                         instance = new wxContourMainFrame(parent, id, title, pos, size, images, style);
160                 }
161                 return instance;
162         }
163
164         std::vector<vtkImageData*> wxContourMainFrame ::getVectImages(){
165                 return kernelManager->getVectImages();
166         }
167         wxContourMainFrame* wxContourMainFrame :: getInstance(){
168                 return instance;
169                 
170         }
171
172         wxContourMainFrame :: ~wxContourMainFrame()
173         {
174                   m_mgr.UnInit();
175                   //El problema al cerrar la aplicacion puede estar asociado 
176                   //a que  wxAUINotebook esta en la aplicacion 
177                   //principal (wxContourGUIExample)tambien
178 //EED????               delete _theViewPanel;             
179 //EED????               delete _instantPanel;
180 //EED????               delete _buttonsBar;
181 //EED????               delete _actualInstant;           
182 //EED????               delete _sceneManager;
183         }
184
185
186         //------------------------------------------------------------------------------------------------------------
187         // Creational and initialization methods using WxAui
188         //------------------------------------------------------------------------------------------------------------
189         //
190         wxAuiNotebook * wxContourMainFrame :: createNotebook()
191         {
192                 wxSize client_size = GetClientSize();
193                 wxAuiNotebook* noteBook = new wxAuiNotebook(this, -1, wxPoint(client_size.x, client_size.y), wxSize(430,200), m_notebook_style);
194                 wxBitmap page_bmp = wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_OTHER, wxSize(16,16));
195                 return noteBook;
196         }
197         
198
199         //------------------------------------------------------------------------------------------------------------
200         // Creational and initialization methods 
201         //------------------------------------------------------------------------------------------------------------
202         bool wxContourMainFrame :: configurePanels(wxAuiNotebook* theNoteBook)
203         {
204                 bool configured = _theViewPanel!=NULL;
205                                         
206                 configured &= _theViewPanel!=NULL;
207                 if( _theViewPanel!=NULL )
208                 {
209                         theNoteBook->AddPage( _theViewPanel, wxT("       View       ") );               
210                         m_mgr.Update();
211                 }
212
213
214                 if( configured )
215                 {
216                         theNoteBook->AddPage( _instantPanel, wxT("Instant Page") );
217                         m_mgr.Update();
218                 }
219
220                 m_mgr.AddPane(theNoteBook, wxAuiPaneInfo().Name(wxT("notebook_content")).CenterPane().PaneBorder(false));
221                 m_mgr.Update();
222                 configured &= _buttonsBar!=NULL;
223                 if( _buttonsBar!=NULL )
224                 {                       
225                         m_mgr.AddPane(_buttonsBar, wxAuiPaneInfo().
226                                         Name(wxT("TB")).Caption(wxT("Buttons Bar")).
227                                         ToolbarPane().Top().
228                                         LeftDockable(  ).RightDockable( false ).CloseButton(false));
229                         m_mgr.Update();
230                 }
231                 //CreateStatusBar();
232                          
233                 SetMinSize(wxSize(300,300));
234                 m_mgr.Update();
235                 return configured;
236         }
237
238         void wxContourMainFrame :: setNotebook( wxAuiNotebook * noteBook )
239         {
240
241         }
242
243         //------------------------------------------------------------------------------------------------------------
244         //  Attributes getters and setters
245         //------------------------------------------------------------------------------------------------------------
246         
247
248         
249         void wxContourMainFrame :: setInstantChooserPanel(  wxInstantChooserPanel * theInstantChooserPanel )
250         {
251                 _instantPanel = theInstantChooserPanel; 
252         }
253
254         void wxContourMainFrame :: setButtonsBar(  wxContour_ButtonsBar * theButtonsBar )
255         {
256                 _buttonsBar = theButtonsBar;
257         }
258
259         void wxContourMainFrame :: setGrid(  wxContour_Grid * theGridPanel )
260         {
261                 _gridPanel = theGridPanel;
262         }
263
264         void wxContourMainFrame :: setDrawToolsPanel(  wxContour_DrawToolsPanel * theDrawToolsPanel )
265         {
266                 _drawToolsPanel = theDrawToolsPanel;
267         }
268
269         void wxContourMainFrame :: setOperationsToolsPanel(  wxContour_OperationsToolsPanel * theOperationsToolsPanel )
270         {       
271                 _operationsToolsPanel = theOperationsToolsPanel;
272         }
273
274         void wxContourMainFrame :: setAutomaticFormsPanel(  wxContour_AutomaticFormsToolsPanel * theAutoFormsPanel )
275         {
276                 _autoFormsPanel = theAutoFormsPanel;
277         }
278
279         void wxContourMainFrame :: setStandardToolsPanel(  wxContour_StandardToolsPanel * theStandardToolsPanel )
280         {
281                 _standardToolsPanel= theStandardToolsPanel;
282         }
283
284         void wxContourMainFrame :: setEditionToolsPanel(  wxContour_EdtionToolsPanel * theEditionToolsPanel )
285         {
286                 _editionToolsPanel =  theEditionToolsPanel;
287         }
288
289         void wxContourMainFrame :: setListViewPanel(  wxContour_ListViewPanel * theListViewPanel )
290         {
291                 _listViewPanel = theListViewPanel;
292         }
293
294
295
296 void wxContourMainFrame::onCreateContourSpline( ){
297         
298         //JCP 20-10-08 Undo redo implementation
299         saveState();
300         //JCP 20-10-08 Undo redo implementation
301         createContour( 1 );
302         
303 }
304 void wxContourMainFrame::onCreateContourRectangle( ){
305     //JCP 20-10-08 Undo redo implementation
306         saveState();
307         //JCP 20-10-08 Undo redo implementation 
308         createContour( 2 );
309 }
310 void wxContourMainFrame::onCreateContourCircle( ){
311     //JCP 20-10-08 Undo redo implementation
312         saveState();
313         //JCP 20-10-08 Undo redo implementation 
314         createContour( 3 );
315 }
316 //------------------------------------------------------------------------------------------------------------
317
318 void wxContourMainFrame :: onCreateContourBullEye(wxPanel* panel )
319 {
320         //Creating the manualContourModel and including in the model
321         manualContourModel * manModelContour = factoryManualContourModel(panel );
322
323         std::vector<int> instantVector;
324         _instantPanel->getInstant( instantVector );
325         std::string theName = kernelManager->createOutline(manModelContour, instantVector);
326         
327         /*std::vector<int> instantVector;
328         _instantPanel->getInstant( instantVector );
329         std::string theName;
330         theName= _modelManager->createOutline( manModelContour, instantVector );*/
331         bool addedModel = theName.compare("") != 0;//??
332
333         if( addedModel )
334         {
335                 double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
336                 _theViewPanel->getSpacing(spc);                                 
337                 //Adding the manualContourControler to interface objects structure
338                 //Adding the manualViewContour to interface objects structure           
339                 //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
340                 _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc , 4) ;
341         }
342 }
343 void wxContourMainFrame :: createContour( int typeContour )
344 {
345         //Creating the manualContourModel and including in the model
346         manualContourModel * manModelContour = kernelManager->factoryManualContourModel(typeContour);
347
348
349         std::vector<int> instantVector;
350         _instantPanel->getInstant( instantVector );
351         std::string theName = kernelManager->createOutline(manModelContour, instantVector);
352         
353         /*std::vector<int> instantVector;
354         _instantPanel->getInstant( instantVector );
355         std::string theName;
356         theName= _modelManager->createOutline( manModelContour, instantVector );*/
357         bool addedModel = theName.compare("") != 0;//??
358         if( addedModel )
359         {
360                 double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
361                 _theViewPanel->getSpacing(spc);                                 
362                 //Adding the manualContourControler to interface objects structure
363                 //Adding the manualViewContour to interface objects structure           
364                 //_sceneManager->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
365                 _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc , typeContour) ;
366         }       
367
368 }
369 manualContourModel * wxContourMainFrame::factoryManualContourModel(wxPanel* panel){
370
371         manualContourModelBullEye *manModelContourBullEye = new manualContourModelBullEye();
372         manualContourModel *manModelContour=NULL;
373         manModelContour = manModelContourBullEye;
374         if (panel!=NULL){
375                 int iCrown,sizeCrowns,iSector,sizeSectors;
376                 double radioA,radioB,ang,angDelta ;
377                 sizeCrowns = ((PanelBullEyeOptions*)panel)->GetNumberOfCrowns();
378                 for ( iCrown=0 ; iCrown<sizeCrowns ; iCrown++ )
379                 {
380                         sizeSectors = ((PanelBullEyeOptions*)panel)->GetNumberOfSections(iCrown);
381                         radioB  = ((PanelBullEyeOptions*)panel)->GetRadioOfCrown(iCrown);
382                         if (iCrown==sizeCrowns-1)
383                         {
384                                 radioA  = 0;
385                         } else {
386                                 radioA  = ((PanelBullEyeOptions*)panel)->GetRadioOfCrown(iCrown+1);
387                         }
388                         radioA=radioA/100.0;
389                         radioB=radioB/100.0;
390                         for ( iSector=0 ; iSector<sizeSectors  ; iSector++ )
391                         {
392                                 ang             = ((PanelBullEyeOptions*)panel)->GetAngOfCrownSection(iCrown,iSector);
393                                 angDelta= ((PanelBullEyeOptions*)panel)->GetAngDeltaOfCrownSection(iCrown);                                     
394                                 manModelContourBullEye->AddSector(radioA,radioB,ang,angDelta);
395                         } // for iSector
396                 } // for iCrown
397         } // if _panelBullEyeOptions
398         
399         return manModelContour;
400 }
401 void wxContourMainFrame :: saveState(){ 
402                 
403         std::string temp = kernelManager->saveState();
404         saveFileWithContours(temp);     
405 }
406
407 void wxContourMainFrame::onDeleteContour(){
408         //JCP 20-10-08 Undo redo implementation
409                 saveState();
410                 //JCP 20-10-08 Undo redo implementation
411
412                 std::vector<std::string> lstKeyNameToBeErase;
413                 lstKeyNameToBeErase             = _theViewPanel->getSceneManager()->getSelectedObjects();
414                 deleteContours( lstKeyNameToBeErase );
415 /*JCP 19 - 11 - 08
416                 int i,size=_sceneManager->getSelectedObjects().size();
417                 for(i=0;i<size;i++)
418                 {
419                         std::string keyName             = _sceneManager->getSelectedObjects()[i];
420                         int ispartofstaticlist  = this->_modelManager->IsPartOfStaticList( keyName );
421                         if ( ispartofstaticlist>=0 )
422                         {
423                                 std::vector<int> tempVector;
424                                 _instantPanel->getInstant( tempVector );
425                                 Instant instant(&tempVector);
426                                 this->_modelManager->ChangeContourOfList(keyName, &instant);
427                         }
428                 }
429
430                 std::vector<std::string> lstKeyName;
431                 std::vector<std::string> lstKeyNameActualSlice;
432                 std::vector<std::string> lstKeyNameToBeErase;
433
434                 lstKeyNameToBeErase             = _sceneManager->getSelectedObjects();
435                 lstKeyNameActualSlice   = _sceneManager->GetlstContoursNameActualSlice();
436                 int k,kSize=lstKeyNameToBeErase.size();
437                 int j,jSize=lstKeyNameActualSlice.size();
438                 bool ok;
439                 for (k=0;k<kSize; k++)
440                 {
441                         ok=false;
442                         for (j=0;j<jSize; j++)
443                         {
444                                 if (lstKeyNameToBeErase[k]==lstKeyNameActualSlice[j])
445                                 { 
446                                         ok=true;
447                                 }
448                         } // for j
449                         if (ok==true)
450                         {
451                                 lstKeyName.push_back( lstKeyNameToBeErase[k] );
452                         } // if ok
453                 } // for k
454
455                 deleteContours( lstKeyName );
456 JCP 19 - 11 - 08*/
457 }
458
459 void wxContourMainFrame :: deleteContours( std::vector<std::string>  keyNamesVector )
460 {
461         int i,size=keyNamesVector.size();
462         for (i=0;i<size;i++)
463         {
464                 deleteContour( keyNamesVector[i] );
465         }
466 }
467
468 void wxContourMainFrame :: deleteContour( std::string theKeyName ){
469         /*manualContourModel                    * cModel;
470         manualViewBaseContour           * cViewer;
471         manualContourBaseControler      * cControler;
472
473         ContourWrap_ViewControl *conwrapviewControl =  _theViewPanel->getSceneManager()->getContourWrap_ViewControlOf( theKeyName );
474         cControler      = conwrapviewControl->getControler();*/
475
476         //JCP 21 - 11 - 2008
477         bool isedit = _theViewPanel->isEditableCControler(theKeyName);
478         bool ispartofstaticlst = kernelManager->IsPartOfStaticList(theKeyName);
479
480         if (isedit  && ispartofstaticlst)
481         {
482                 _theViewPanel->removeFromScene(theKeyName);
483
484                 kernelManager->deleteCModel(theKeyName);
485                 //_theViewPanel->getSceneManager()->removeFromScene( theKeyName );                      
486                 //cViewer               = conwrapviewControl->getViewer();
487                 //_theViewPanel->getSceneManager()->removeWrap( theKeyName );
488                 //cControler* conwrapviewControl->getControler();                       
489 //EED Borrame
490 //FILE *ff = fopen("c:/temp/wxVtkBaseView_SceneManagerStadistics.txt","a+");
491 //fprintf(ff,"EED wxContourEventHandler::deleteContours() \n" );
492 //fprintf(ff,"    %s %p\n",keyNamesVector[i].c_str(),  cControler );
493 //fclose(ff);
494         //      delete cModel;
495         //      delete cViewer;
496         //      delete cControler;
497         } // if editable
498 //JCP 21 - 11 - 08
499 }
500
501 void wxContourMainFrame::onDeleteContoursActSlice(){
502         //JCP 20-10-08 Undo redo implementation
503         saveState();
504         //JCP 20-10-08 Undo redo implementation
505         deleteContours( (std::vector<std::string>)_theViewPanel->getSceneManager()->GetlstContoursNameActualSlice() );
506 }
507 void wxContourMainFrame::onDeleteAllContours(){
508         //JCP 20-10-08 Undo redo implementation
509         saveState();
510         deleteAllContours();
511 }
512 void wxContourMainFrame::deleteAllContours(){
513         //JCP 20-10-08 Undo redo implementation
514
515         wxBusyCursor wait;
516         std::vector<int> tempVector;
517         _instantPanel->getInstant( tempVector );
518
519 //JCP --08-09-2008 When using a diferent interface the _mbarrangeDeleteAll might not be initialize
520 //              the values in GetStart and GetEnd will then not be initialize also.
521 //              We use instead the values given when initializing the _deletepanel.
522
523         //int minZ = _mbarrangeDeleteAll->GetStart();
524         //int maxZ = _mbarrangeDeleteAll->GetEnd();
525         int minZ, maxZ;
526         
527         minZ = 0;
528         maxZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
529         
530 //JCP --08-09-2008
531         
532         _theViewPanel->getSceneManager()->removeSceneContours( );       
533         _theViewPanel->getSceneManager()->removeAllOutlines();
534         kernelManager->removeAllOutlines();     
535 //JCP --08-09-2008
536         /*
537         if ( (minZ==0) && (maxZ==_mbarrangeDeleteAll->GetMax() ))
538         {
539                 _theViewPanel->getSceneManager()->removeSceneContours( );
540                 _modelManager->removeAllOutlines();
541                 _theViewPanel->getSceneManager()->removeAllOutlines();
542
543         } else {
544                 for ( z=minZ ; z<=maxZ ; z++)
545                 {
546                         tempVector[1]=z;
547                         Instant instant(&tempVector);
548                         std::vector<ContourThing**> lstContourThings = this->_modelManager->getOutlinesAtInstant( &instant );
549
550                         sizeLstContourThings = lstContourThings.size();
551                         for (ii=0 ; ii<sizeLstContourThings ; ii++)
552                         {
553                                 ContourThing **contourthing = lstContourThings[ii];
554                                 deleteContour( (*contourthing)->getName() );
555                         } //for ii
556                 }// for z
557         } // if 
558 JCP --08-09-2008 */
559 }
560
561 void wxContourMainFrame::setConceptValue( std::string name, int value ){
562         _instantPanel->setConceptValue(name, value);
563 }
564
565 ConceptDataWrap* wxContourMainFrame::getLastConceptData(){
566         return _instantPanel->getLastConceptData();
567 }
568 bool wxContourMainFrame::getIfConceptCheckedAt( std::string name, int pos ){
569         return _instantPanel->getIfConceptCheckedAt( name, pos );
570 }
571
572 void wxContourMainFrame::changeInstant(){
573                 std::vector<int> instantVect;
574                 _instantPanel->getInstant( instantVect );
575 //              Instant * theInstant = new Instant ( &instantVect );
576                 
577                 //Setting the actual instant
578                 //_actualInstant = theInstant;
579
580                 kernelManager->setInstant(instantVect);
581                 //_modelManager->setInstant( _actualInstant );
582                 
583                 updateInstantOutlines();
584                 updateInstantImageData();
585                 updateInstantAxes();
586
587                 
588 }
589 void wxContourMainFrame :: updateInstantOutlines(){
590         
591         _theViewPanel->removeSceneContours();
592         _theViewPanel->addNameWrapperToScene();
593
594         //for( int i=0; i<size; i++)
595         //{                     
596         //      _theViewPanel->getSceneManager()->addToScene(namesWrapping[i]->getKeyName(), true, true, true, false, false );
597         //}
598 }
599
600 int wxContourMainFrame::getNamesWrappingSize(){
601         return kernelManager->getNamesWrappingSize();
602 }
603 std::string wxContourMainFrame::getNameWrapping(int i){
604         return kernelManager->getNameWrapping(i);
605 }
606
607 void wxContourMainFrame::updateInstantImageData(){
608         std::vector<int> inst;
609         _instantPanel->getInstant(inst);
610
611         vtkImageData* img = kernelManager->getImageAtInstant(inst);
612         if(img!=NULL){
613                 _theViewPanel->changeImage(img);
614         }       
615 }
616 void wxContourMainFrame::updateInstantAxes(){
617 }
618
619 void wxContourMainFrame::onChangeDeep(int val){
620         _instantPanel->setConceptValue( "Axe Depth", (int)val );        
621         changeInstant();
622 }
623
624
625 void wxContourMainFrame::onCopy(){
626         std::vector<std::string> currentSelection = _theViewPanel->getSelectedObjects();
627
628         std::vector<int> tempVector;
629         _instantPanel->getInstant( tempVector );
630     _performingOperation->reset();
631         _performingOperation->setStartCommand( COPY );
632         _performingOperation->setStartOperationInstantVector( tempVector );
633         _performingOperation->setKeyNamesOperationElems( currentSelection );
634 }
635 void wxContourMainFrame::onPaste(){
636
637
638         char theStartCommand = _performingOperation->getStartCommand();
639         if (  theStartCommand == COPY )
640         {
641                 //JCP 20-10-08 Undo redo implementation
642                 saveState();
643                 //JCP 20-10-08 Undo redo implementation
644                 std::vector<int> tempVector;
645                 _instantPanel->getInstant( tempVector );
646                 _performingOperation->setEndOperationInstantVector ( tempVector );
647                 std::vector<std::string> elems = _performingOperation->getKeyNamesOperationElems();
648                 int i,size = elems.size();                      
649                 for( i=0; i<size; i++ )
650                 {                               
651                         createCopyContourOf( elems[i], tempVector, i>0 );
652                 }               
653         }
654 }
655 void wxContourMainFrame::onUndo(){
656         std::string filename;
657         if(kernelManager->onUndoSaveFile(filename)){
658                 saveFileWithContours(filename);
659         }
660         if(kernelManager->onUndo(filename)){
661                 loadState(filename);
662         }
663 }
664 void wxContourMainFrame::onRedo(){
665         std::string filename;
666         if(kernelManager->onRedo(filename)){
667                 loadState(filename);
668         }
669         
670 }
671 void wxContourMainFrame :: createCopyContourOf ( std::string anExistingKName, std::vector<int> &instantNoTouchData, bool append ){
672         std::string cloneName = kernelManager->createCopyContourOf( anExistingKName, instantNoTouchData );
673         manualContourModel * manualModel = kernelManager->getOutlineByKeyName(cloneName);//_modelManager->getOutlineByKeyName( cloneName )->getModel();
674         _theViewPanel->getSceneManager()->createCopyContourOf( anExistingKName, cloneName, manualModel , append );
675 }
676
677 void wxContourMainFrame ::loadState(std::string filename){
678         char tmp[255];
679         FILE *pFile=fopen(filename.c_str(),"r+");
680
681         fscanf(pFile,"%s",tmp); // --CreaContour--
682
683         fscanf(pFile,"%s",tmp); // Version
684         fscanf(pFile,"%s",tmp); // 1.0.1
685         std::string version(tmp);
686
687         deleteAllContours();
688
689         openContours(pFile,false);
690         if (version!="1.0.0"){
691                 openContours(pFile,true);
692         }
693
694         fclose(pFile);
695         
696         //_theViewPanel->getSceneManager()->removeSceneContours();
697         //changeInstant();
698
699 }
700
701 void wxContourMainFrame::onLoad(){
702         char tmp[255];
703         wxFileDialog dialog(NULL, _T("Choose a file"), _T(""), _T(""), _T("*.roi"), wxOPEN );
704         if (dialog.ShowModal() == wxID_OK)
705         {
706                 std::string fileNameContourROI = (const char *)(dialog.GetPath().mb_str());
707                 FILE *pFile=fopen(fileNameContourROI.c_str(),"r+");
708
709                 fscanf(pFile,"%s",tmp); // --CreaContour--
710
711                 fscanf(pFile,"%s",tmp); // Version
712                 fscanf(pFile,"%s",tmp); // 1.0.1
713                 std::string version(tmp);
714
715                 openContours(pFile,false);
716                 if (version!="1.0.0"){
717                         openContours(pFile,true);
718                 }
719 //                        _theViewPanel->getSceneManager()->openFileWithContours(ff);
720
721                 fclose(pFile);
722         }
723         _theViewPanel->getSceneManager()->removeSceneContours();
724         changeInstant();
725 }
726 void wxContourMainFrame::onSave(){
727         wxFileDialog dialog(NULL, _T("Choose a file"), _T(""), _T(""), _T("*.roi"), wxSAVE );
728         if (dialog.ShowModal() == wxID_OK)
729         {
730                 std::string fileNameContourROI = (const char *)(dialog.GetPath().mb_str());
731                 saveFileWithContours( fileNameContourROI );
732         }
733 }
734 void wxContourMainFrame::saveFileWithContours( std::string filename ){
735
736         FILE *pFile=fopen(filename.c_str(),"w+");
737         std::vector< std::string > lstNameThings;
738         int i,sizeLstNameThings; 
739
740         fprintf(pFile,"--CreaContour--\n");
741         fprintf(pFile,"Version %s\n", "1.0.1" );
742
743         // Normal Contours
744         lstNameThings           = kernelManager->GetLstNameThings();
745         sizeLstNameThings       = lstNameThings.size(); 
746         fprintf(pFile,"NumberOfContours %d\n", sizeLstNameThings );
747         for (i=0 ; i<sizeLstNameThings ; i++) 
748         {
749                 kernelManager->SaveThingName( pFile, lstNameThings[i] );
750                 _theViewPanel->getSceneManager()->SaveThingName( pFile, lstNameThings[i] );
751         }// for i
752
753         //-- Contours Statics 
754         lstNameThings           = kernelManager->GetLstNameThingsStatic();
755         sizeLstNameThings       = lstNameThings.size(); 
756         fprintf(pFile,"NumberOfContoursStatic %d\n", sizeLstNameThings );
757         for (i=0 ; i<sizeLstNameThings ; i++) 
758         {
759                 kernelManager->SaveThingName( pFile, lstNameThings[i] );
760                 _theViewPanel->getSceneManager()->SaveThingName( pFile, lstNameThings[i] );
761         }// for i
762         fclose(pFile);
763 }
764
765 void wxContourMainFrame::openContours( FILE *pFile, bool staticContour ){
766         char tmp[255];
767         fscanf(pFile,"%s",tmp); // NumberOfContours 
768         fscanf(pFile,"%s",tmp); // ##
769         int numberOfContours = atoi(tmp);  
770
771         std::vector<int> instantVector;
772         int typeContourModel;
773         manualContourModel *manModelContour;
774         int typeView;
775
776         int i;
777         for (i=0;i<numberOfContours;i++)
778         {
779                 instantVector.clear();
780                 fscanf(pFile,"%s",tmp); // Instant
781
782                 fscanf(pFile,"%s",tmp); // 1
783                 instantVector.push_back( atoi(tmp) );
784                 fscanf(pFile,"%s",tmp); // 2
785                 instantVector.push_back( atoi(tmp) );
786                 fscanf(pFile,"%s",tmp); // 3
787                 instantVector.push_back( atoi(tmp) );
788                 fscanf(pFile,"%s",tmp); // 4
789                 instantVector.push_back( atoi(tmp) );
790                 fscanf(pFile,"%s",tmp); // 5
791                 instantVector.push_back( atoi(tmp) );
792                 fscanf(pFile,"%s",tmp); // 6
793                 instantVector.push_back( atoi(tmp) );
794
795
796                 fscanf(pFile,"%s",tmp); // TypeContourModel
797                 fscanf(pFile,"%s",tmp); // ##
798                 typeContourModel = atoi(tmp);
799
800                 manModelContour =  kernelManager->factoryManualContourModel(typeContourModel);
801                 manModelContour->Open(pFile);
802
803                 fscanf(pFile,"%s",tmp); // TypeView
804                 fscanf(pFile,"%s",tmp); // ##
805                 typeView = atoi(tmp);  
806
807
808 //                      if (typeView==1) 
809 //                      {
810 //                      }
811
812
813                 std::string theName;
814                 theName = kernelManager->createOutline( manModelContour, instantVector );
815                 bool addedModel = theName.compare("") != 0;
816                 if( addedModel )
817                 {
818                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
819                         _theViewPanel->getSpacing(spc);                                 
820                         //Adding the manualContourControler to interface objects structure
821                         //Adding the manualViewContour to interface objects structure           
822                         //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
823                         _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc , typeView) ;
824                 }       
825
826                 if (staticContour==true)
827                 {
828                         Instant instant(&instantVector);
829                         kernelManager->changeContourOfManager( theName , &instant );
830                 }
831
832         }// for  numberOfContours
833
834 }
835
836 void wxContourMainFrame::RefreshInterface(){
837         changeInstant();
838         _theViewPanel->RefreshInterface();
839         //wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
840         //wxvtk2dbaseview->Refresh();
841 }
842
843 vtkImageData* wxContourMainFrame::getImageData(){
844         return _theViewPanel->getImageData();
845 }
846
847 void wxContourMainFrame::onSegmentationOneSlice(int isovalue,int sampling,int method){
848         
849         //JCP 20-10-08 Undo redo implementation
850         saveState();
851         //JCP 20-10-08 Undo redo implementation
852         
853         wxBusyCursor wait;
854         int                                     x                                       = _theViewPanel->GetX();
855         int                                     y                                       = _theViewPanel->GetY();
856         int                                     z                                       = _theViewPanel->GetZ();
857         SegmentationOneSlice( x,y,z,isovalue, sampling, method );
858         RefreshInterface();
859 }
860
861 void wxContourMainFrame::SegmentationOneSlice( int x, int y, int z, int isovalue, int sampling, int method )
862 {               
863         int typeofcontour = 1;
864         //--Extracting Contour
865         //vtkImageData  *imagedata      = _theViewPanel->getSceneManager()->GetImageData();
866         vtkImageData    *imagedata      = getImageData();
867 //              double                  *range          = imagedata->GetScalarRange();  
868 //              double                  thr                     = 1;
869
870         vtkImageReslice *imageReslice = vtkImageReslice::New();
871 //EED
872 //              double spc[3];
873 //              imagedata->GetSpacing(spc);
874 //              x = x*spc[0];
875 //              y = y*spc[1];
876 //              z = z*spc[3];
877
878         imageReslice->SetInput( imagedata );
879         imageReslice->SetInformationInput(imagedata);
880         imageReslice->SetResliceAxesDirectionCosines(1,0,0, 0,1,0 ,0,0,1);
881         imageReslice->SetResliceAxesOrigin(0,0,z);
882         imageReslice->SetOutputDimensionality(2);
883         imageReslice->SetInterpolationModeToLinear();
884
885         imagedata = imageReslice->GetOutput();
886         imagedata->Update();
887         imagedata->UpdateInformation();
888
889         vtkContourFilter* cntVTK = vtkContourFilter::New( );
890         cntVTK->SetInput( imagedata );
891
892         cntVTK->SetNumberOfContours( 1 );
893         //cntVTK->SetValue( 0, vmin );
894 //              cntVTK->SetValue( 0, (range[1]*thr/100) );
895         cntVTK->SetValue( 1, isovalue );
896 //      cntVTK->SetValue( 1, vmax );
897         cntVTK->Update( );
898         cntVTK->UpdateInformation();
899                 
900         vtkCleanPolyData* cpd = vtkCleanPolyData::New( );
901         cpd->SetInput( cntVTK->GetOutput( ) );
902         cpd->ConvertLinesToPointsOff( );
903         cpd->Update( );
904         cpd->UpdateInformation();
905
906         vtkPolyDataConnectivityFilter* conn = vtkPolyDataConnectivityFilter::New( );
907         conn->SetExtractionModeToClosestPointRegion( );
908         //conn->SetMaxRecursionDepth( 3000 );
909                 
910         conn->SetInput( cpd->GetOutput( ) );
911                 
912         conn->SetClosestPoint( x, y, 0 );
913         conn->Update( );
914         conn->UpdateInformation();
915                 
916         vtkCleanPolyData* cpd2 = vtkCleanPolyData::New( );
917         cpd2->SetInput( conn->GetOutput( ) );
918         cpd2->Update();
919         cpd2->UpdateInformation();
920
921         vtkStripper* vtkstripper = vtkStripper::New( );
922         vtkstripper->SetInput( cpd2->GetOutput() );
923         vtkstripper->Update();
924         vtkstripper->UpdateInformation();
925
926
927         vtkPolyData* polyDataResult =  vtkstripper->GetOutput();
928
929         polyDataResult->Update( );
930         polyDataResult->UpdateInformation();
931
932 /* EED
933 ofstream myfile;
934 myfile.open ("c:/temp/example.txt");
935 myfile << "\n";
936 polyDataResult->Print(myfile);
937 myfile << "-------------------------------------\n";
938 polyDataResult->GetLines()->Print(myfile);
939 myfile.close();
940 */
941
942         cntVTK          -> Delete();
943         cpd2            -> Delete();
944         cpd                     -> Delete();
945         conn            -> Delete();
946
947
948 //--Calculating control points
949
950         std::vector<double> vecX;
951         std::vector<double> vecY;
952         std::vector<double> vecZ;
953
954         std::vector<double> vecCtrlPointX;
955         std::vector<double> vecCtrlPointY;
956         std::vector<double> vecCtrlPointZ;
957
958
959         double *p;
960         int ii,size=polyDataResult->GetNumberOfPoints();
961         int id;
962         for (ii=1;ii<=size;ii++)
963         {
964                 id      = polyDataResult->GetLines()->GetData()->GetValue(ii);
965                 p       = polyDataResult->GetPoint(id);
966                 double x=p[0];
967                 double y=p[1];
968                 vecX.push_back( p[0] );
969                 vecY.push_back( p[1] );
970                 vecZ.push_back( -900 );
971 //                      vecZ.push_back( p[2] );
972         }
973
974
975         ExtractControlPoints2D *extractcontrolpoints2d = new ExtractControlPoints2D();
976         extractcontrolpoints2d->SetContour( &vecX , &vecY , &vecZ );
977
978 //PROOFS
979         /*if (methodRadiobox->GetSelection()==0){
980                 extractcontrolpoints2d->GetInitialControlPoints( &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
981         }
982         if (methodRadiobox->GetSelection()==1){
983                 extractcontrolpoints2d->GetControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
984         }
985         if (methodRadiobox->GetSelection()==2){
986                 extractcontrolpoints2d->SetSamplingControlPoints( sampling );
987                 extractcontrolpoints2d->GetSamplingControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
988         }*/
989
990         if (method==0){
991                 extractcontrolpoints2d->GetInitialControlPoints( &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
992         }
993         else if (method==1){
994                 extractcontrolpoints2d->GetControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
995         }
996         else if (method==2){
997                 extractcontrolpoints2d->SetSamplingControlPoints( sampling );
998                 extractcontrolpoints2d->GetSamplingControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
999         }
1000
1001         //--Adding contour to the system
1002
1003         std::vector<int> actualInstantVector;
1004         _instantPanel->getInstant( actualInstantVector );
1005         actualInstantVector[1]=z;
1006
1007         int j,sizeCtrPt = vecCtrlPointX.size();
1008         manualContourModel *manModelContour =  kernelManager->factoryManualContourModel( typeofcontour );
1009         manModelContour->SetNumberOfPointsSpline( ((sizeCtrPt/100)+1)*100 );
1010         if (sizeCtrPt>=3){
1011                 for (j=0 ; j<sizeCtrPt ; j++)
1012                 {
1013                         manModelContour->AddPoint( vecCtrlPointX[j] , vecCtrlPointY[j] , vecCtrlPointZ[j]  );
1014                 } // for
1015                 std::string theName;
1016                 //theName = _modelManager->createOutline( manModelContour, actualInstantVector );
1017                 theName = kernelManager->createOutline( manModelContour, actualInstantVector );
1018                 bool addedModel = theName.compare("") != 0;
1019                 if( addedModel )
1020                 {
1021                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
1022                         _theViewPanel->getSpacing(spc);                                 
1023                         //Adding the manualContourControler to interface objects structure
1024                         //Adding the manualViewContour to interface objects structure           
1025                         //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
1026                         _theViewPanel->configureViewControlTo(theName, manModelContour, spc, typeofcontour);
1027                         //_theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc, typeofcontour ) ;
1028                 }       // if addedModel
1029         } // if sizeCtrPt
1030 }
1031
1032 int wxContourMainFrame::GetImageDataSizeZ(){
1033         return _theViewPanel->GetImageDataSizeZ();
1034 }
1035
1036 void wxContourMainFrame::GetImageDataRange(double *range){
1037         _theViewPanel->GetImageDataRange(range);
1038 }
1039
1040 void wxContourMainFrame::onSegmentationAllSlice(int minZ,int maxZ,int isovalue,int sampling,int method){
1041         //JCP 20-10-08 Undo redo implementation
1042         saveState();
1043         //JCP 20-10-08 Undo redo implementation
1044         
1045         wxBusyCursor wait;
1046         int                                     x                                       = _theViewPanel->GetX();
1047         int                                     y                                       = _theViewPanel->GetY();
1048         int z;
1049         double porcent; 
1050         wxString tmpString;     
1051         double totalZ = maxZ-minZ+1;
1052         for( z=minZ ; z<=maxZ ; z++ )
1053         {
1054                 porcent = 100.0* (z-minZ)/totalZ;
1055                 tmpString.Printf(_T("  %d %c            %d/%d            %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ,z );
1056                 interfMainPanel::getInstance()->setLabelSegmentationPanelVTK(tmpString);
1057                 //_staticTextSegmentation->SetLabel(tmpString);
1058                 SegmentationOneSlice( x,y,z,isovalue, sampling,method );
1059         }
1060         interfMainPanel::getInstance()->setLabelSegmentationPanelVTK(_T("   "));
1061         RefreshInterface();
1062 }
1063
1064 void wxContourMainFrame::showAxis(bool show){
1065         _theViewPanel->SetVisibleAxis(show);
1066         _theViewPanel->Refresh();
1067 }
1068
1069
1070 void wxContourMainFrame::onRigidPressed(){
1071         std::vector<std::string> currentSelection = _theViewPanel->getSelectedObjects();
1072         int elementsSelected = currentSelection.size();
1073
1074         if ( elementsSelected >= 1 ){
1075                 //JCP 20-10-08 Undo redo implementation
1076                 saveState();
1077                 //JCP 20-10-08 Undo redo implementation
1078
1079                 std::vector<int> tempVector;
1080                 _instantPanel->getInstant( tempVector );
1081                 Instant instant(&tempVector);
1082
1083                 int i , size=currentSelection.size();
1084                 for ( i=0 ; i<size ; i++ )
1085                 {
1086                         kernelManager->changeContourOfManager(currentSelection[i], &instant);
1087                 } // i
1088         }
1089 }
1090
1091 void wxContourMainFrame::onWidthOfContour(double width){
1092
1093         _theViewPanel->removeSceneContours();
1094         _theViewPanel->SetWidthContour(width);
1095         
1096         //_theViewPanel->getSceneManager()->SetWidthContour( width );
1097
1098         RefreshInterface();
1099 }
1100 int wxContourMainFrame::getColorWindow(){
1101         return _theViewPanel->getColorWindow();
1102 }
1103 int wxContourMainFrame::getWindowLevel(){
1104         return _theViewPanel->getWindowLevel();
1105 }
1106
1107 void wxContourMainFrame::onBrigthnessColorWindowLevel(int colorwindow,int windowlevel){
1108         _theViewPanel->onBrigthnessColorWindowLevel(colorwindow, windowlevel);
1109
1110         
1111         RefreshInterface();
1112 }
1113
1114 void wxContourMainFrame::onInterpolation(bool interpolate){
1115         _theViewPanel->onInterpolation(interpolate);    
1116         RefreshInterface();
1117 }
1118
1119 void wxContourMainFrame::onChangeInstant(std::string name,int actual){
1120         _instantPanel->setConceptValue( name, actual );
1121 }
1122
1123 void wxContourMainFrame::resetAppend(){
1124         kernelManager->resetAppend();
1125         
1126 }
1127
1128 void wxContourMainFrame::onSpreadAdd(){
1129         std::vector<double> vecX; 
1130         std::vector<double> vecY; 
1131         std::vector<double> vecZ; 
1132         _theViewPanel->GetPointsOfActualContour(&vecX , &vecY , &vecZ );
1133         
1134         std::vector<int> tempVector;
1135         _instantPanel->getInstant( tempVector );
1136
1137         std::string val = kernelManager->onSpreadAdd(&vecX , &vecY , &vecZ, tempVector);
1138
1139         interfMainPanel::getInstance()->appendStringSpread(val);
1140         
1141 }
1142
1143 void wxContourMainFrame::onSpreadGo(int type){
1144
1145         //JCP 20-10-08 Undo redo implementation
1146         saveState();
1147         //JCP 20-10-08 Undo redo implementation
1148
1149         wxBusyCursor wait;      
1150         std::vector<double> vecCtrlPointX;
1151         std::vector<double> vecCtrlPointY;
1152         std::vector<double> vecCtrlPointZ;
1153         double  minZ,maxZ;
1154         int z;
1155
1156         std::vector<int> tempVector;
1157         _instantPanel->getInstant( tempVector );
1158 //              tempVector[1];
1159
1160         kernelManager->getMaxMinZ(&minZ,&maxZ);
1161 //JSTG_16-07-08_----------------------------------------------------------------
1162         //_contourPropagation->setInterpolationNumber(maxZ-minZ+1);
1163         kernelManager->CalculeSplinePropagation();
1164
1165         double          totalZ = maxZ-minZ+1;
1166         double          porcent;
1167         wxString        tmpString;
1168 //--------------------------------------------------------------------
1169         for ( z=(int)minZ ; z<=(int)maxZ ; z++ )
1170         {
1171                 porcent = 100.0* (z-minZ)/totalZ;
1172                 int numero = (int)(z-minZ+1)/(int)totalZ;
1173                 std::string stringtemp = "  "+kernelManager->intToString(porcent)+"%            "+kernelManager->intToString(numero)+"            "+kernelManager->intToString(z);
1174                 
1175                 interfMainPanel::getInstance()->setStringSpread(stringtemp);    
1176
1177                 int typeofcontour = 1;
1178                 std::string theName;
1179                 manualContourModel* manModelContour = kernelManager->GetPoints(z, type, &vecCtrlPointX,&vecCtrlPointY,&vecCtrlPointZ, theName,typeofcontour, tempVector);
1180
1181                 if( manModelContour!=NULL ){
1182                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
1183                         this->_theViewPanel->getSpacing(spc);                                   
1184                         //Adding the manualContourControler to interface objects structure
1185                         //Adding the manualViewContour to interface objects structure           
1186                         //_theViewPanel->getSceneManager()->setControlActiveStateOfALL( false );//This call is being done here because if the ROI is created underneath the previously created ROIS will still be active.
1187                         _theViewPanel->configureViewControlTo( theName, manModelContour,spc,typeofcontour ) ;
1188                 } // if addedModel
1189         } 
1190         if(z > maxZ){
1191                 porcent = 100.0;
1192                 int numero = (int)(z-minZ+1)/(int)totalZ;
1193                 std::string stringtemp = "  "+kernelManager->intToString(porcent)+"%            "+kernelManager->intToString(numero)+"            "+kernelManager->intToString(z);
1194                 
1195                 interfMainPanel::getInstance()->setStringSpread(stringtemp);
1196         }// for z
1197         //RefreshInterface();
1198 }
1199
1200 void wxContourMainFrame::getInstantVector(std::vector<int>& tempVector){
1201         
1202 }
1203
1204 std::vector<std::string> wxContourMainFrame::getOutlinesName(int slide){
1205         std::vector<int> tempVector;
1206         _instantPanel->getInstant(tempVector);
1207         //Asignation of slide number should be different ex by name
1208         tempVector[1]=slide;
1209         return kernelManager->getOutlinesNameAtInstant(tempVector);
1210 }
1211
1212 void wxContourMainFrame::onInformationContourLabels(int typeContourGroup,int selection,int minimumZ,int maximumZ){
1213         int maxContourGroup     =       0;
1214
1215         ContourExtractData      *contourextractdata = new ContourExtractData();
1216         
1217         contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1218
1219         std::vector<manualContourModel*> lstManConMod;
1220         std::vector<manualContourModel*> lstManConModTmp;
1221         std::vector<manualContourModel*> lstManConModExp;
1222         std::vector<double> pLstValue;
1223         std::vector<double> pLstValuePosX;
1224         std::vector<double> pLstValuePosY;
1225         std::vector<double> pLstValuePosZ;
1226
1227         int             resultSize; 
1228         int             resultGrayRangeCount;
1229         double  resultMin; 
1230         double  resultMax;
1231         double  resultAverage;
1232         double  resultStandardeviation;
1233
1234         std::vector<int> tempVector;
1235         _instantPanel->getInstant( tempVector );
1236
1237         vtkImageData *imagedata = _theViewPanel->getImageData();
1238         int sizeZ = _theViewPanel->GetImageDataSizeZ();
1239         int z;
1240         int ii,sizeLstContourThings;
1241
1242
1243         int minZ,maxZ;
1244         double totalZ;
1245         double porcent; 
1246         wxString tmpString;
1247
1248         
1249
1250         if (selection==0 ) // actual slice
1251         {
1252                 std::vector<int> tempVector;
1253                 _instantPanel->getInstant( tempVector );
1254                 int actualSlice = tempVector[1];
1255                 minZ    = actualSlice;
1256                 maxZ    = actualSlice;
1257         }
1258         else if (selection==2 ) // All slices
1259         {
1260                 minZ    = 0;
1261                 maxZ    = sizeZ-1;
1262         }
1263         else if (selection==1 ) // slice range
1264         {
1265                 minZ    = minimumZ;
1266                 maxZ    = maximumZ;
1267         }
1268         
1269
1270         totalZ  = maxZ-minZ+1;
1271         contourextractdata->SetImage( imagedata);
1272
1273 // For each slice..
1274         for ( z=minZ ; z<=maxZ ; z++ )
1275         {
1276                 
1277                 porcent = 100.0* (z-minZ)/totalZ;
1278                 tmpString.Printf(_T("  %d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1279
1280                 interfMainPanel::getInstance()->setStringInfoPanel(tmpString);          
1281                 //_staticTextInformation->SetLabel(tmpString);
1282
1283                 //Extraction data from contours of each slice
1284                 contourextractdata->SetZtoBeAnalys( z);
1285
1286                 tempVector[1]=z;
1287                 Instant instant(&tempVector);
1288                 std::vector<ContourThing**> lstContourThings = kernelManager->getOutlinesAtInstant( &instant );
1289                 sizeLstContourThings = lstContourThings.size();
1290
1291                 lstManConMod.clear();
1292                 for (ii=0 ; ii<sizeLstContourThings ; ii++)
1293                 {
1294                         ContourThing **contourthing = lstContourThings[ii];
1295                         lstManConMod.push_back( (*contourthing)->getModel() );
1296                 }
1297                 lstManConModExp =  kernelManager->ExploseEachModel( lstManConMod );
1298
1299
1300                 wxString tempString;
1301                 tempString.Printf(_T("%d - %d"),z, sizeLstContourThings);
1302                 interfMainPanel::getInstance()->setRowLabelInfoPanel(z, tempString);
1303                 //_grid->SetRowLabelValue(z, tempString );
1304 //EED004
1305                 int iContourGroup,sizeContourGroup;
1306                 if (typeContourGroup==3) // contour separete
1307                 {
1308                         sizeContourGroup=lstManConModExp.size();
1309                         if ( maxContourGroup<sizeContourGroup ) 
1310                         {
1311                                 maxContourGroup=sizeContourGroup;
1312                         }
1313                 } else {  // contour AND OR XOR
1314                         sizeContourGroup=1;
1315                         maxContourGroup=1;
1316                 }
1317
1318                 int tmpIntA;
1319
1320
1321
1322                 for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1323                         lstManConModTmp.clear();
1324                         if (typeContourGroup==3) // contour separete
1325                         {
1326                                 lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1327                         } else {  // contour AND OR XOR
1328                                 lstManConModTmp = lstManConModExp;
1329                         }
1330
1331                         contourextractdata->SetLstManualContourModel( lstManConModTmp );
1332
1333                         pLstValue.clear();
1334                         pLstValuePosX.clear();
1335                         pLstValuePosY.clear();
1336                         pLstValuePosZ.clear();
1337                         contourextractdata->GetValuesInsideCrown(       &pLstValue,
1338                                                                                                                 &pLstValuePosX,
1339                                                                                                                 &pLstValuePosY,
1340                                                                                                                 &pLstValuePosZ);
1341                         // Statistics of each slice.
1342                         contourextractdata->Statistics( &pLstValue,
1343                                                                                         minimumZ,
1344                                                                                         maximumZ,
1345                                                                                         &resultGrayRangeCount, 
1346                                                                                         &resultSize, 
1347                                                                                         &resultMin, 
1348                                                                                         &resultMax,
1349                                                                                         &resultAverage,
1350                                                                                         &resultStandardeviation);
1351                         
1352                         if (interfMainPanel::getInstance()->getNumberColsInformationPanel()<_numberOfVariablesStatistics*(iContourGroup+1)  )
1353                         {
1354                                 interfMainPanel::getInstance()->appendColsInformationPanel(_numberOfVariablesStatistics);
1355                         }
1356
1357                         tmpIntA=_numberOfVariablesStatistics*iContourGroup ;
1358
1359                         tempString.Printf(_T("%d"),resultSize);
1360                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA, tempString);  
1361
1362                         tempString.Printf(_T("%d"),resultGrayRangeCount);
1363                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+1, tempString);
1364                         
1365                         tempString.Printf(_T("%f"),resultMin);
1366                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+2, tempString);
1367                         
1368                         tempString.Printf(_T("%f"),resultMax);
1369                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+3, tempString);
1370                         
1371                         tempString.Printf(_T("%f"),resultAverage);
1372                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+4, tempString);
1373                         
1374                         tempString.Printf(_T("%f"),resultStandardeviation);
1375                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+5, tempString);
1376                         
1377
1378                 } // for iContourGroup
1379         } // for z
1380
1381         int iTitleGroup;
1382         wxString tmpTitleString;
1383         int tmpIntB;
1384         for ( iTitleGroup=0 ; iTitleGroup<maxContourGroup ; iTitleGroup++ )
1385         {
1386                 tmpIntB =_numberOfVariablesStatistics*iTitleGroup;
1387                 tmpTitleString.Printf(_T("%d-Size Data"),iTitleGroup);
1388                 interfMainPanel::getInstance()->setColLableInfoPanel(tmpIntB + 0, tmpTitleString);
1389
1390                 interfMainPanel::getInstance()->setColLableInfoPanel(tmpIntB + 0, tmpTitleString );
1391                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 1, _T("Size Range") );
1392                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 2, _T("Min") );
1393                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 3, _T("Max") );
1394                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 4, _T("Average") );
1395                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 5, _T("St.Dev.") );
1396                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 6, _T(" ") );
1397         }
1398
1399         interfMainPanel::getInstance()->setStringInfoPanel( _T("") );
1400         delete contourextractdata;
1401 }
1402
1403 void wxContourMainFrame::onSaveResults(std::string directory,std::string namefile, std::string filename, int typeContourGroup){
1404     SaveValuesXYZ( directory , namefile, typeContourGroup );
1405         SaveImageResult( directory , namefile, typeContourGroup);
1406
1407         
1408         FILE *pFile=fopen(filename.c_str(),"w+");
1409
1410         int sizeZ = _theViewPanel->GetImageDataSizeZ();
1411
1412         wxString tmpString;
1413         int i,j,maxX,maxY=sizeZ;
1414         maxX= interfMainPanel::getInstance()->getNumberColsInformationPanel();//this->_grid->GetNumberCols();
1415
1416         int iTitle,sizeTitle = (maxX / _numberOfVariablesStatistics);
1417         for ( iTitle=0; iTitle<sizeTitle ; iTitle++)
1418         {
1419                 fprintf(pFile,"-- \t %d-Size \t SizeRange \t Min \t Max \t Ave \t StDv \t" , iTitle,tmpString.c_str() );
1420         }
1421         fprintf(pFile,"\n" );
1422
1423
1424
1425         for ( j=0; j<maxY ; j++)
1426         {
1427                 fprintf(pFile,"%d\t" , j );
1428                 for (i=0 ; i<maxX ; i++){
1429                         tmpString = interfMainPanel::getInstance()->getCellValue(j, i);//_grid->GetCellValue( j , i );
1430                         fprintf(pFile,"%s\t" , (const char *)(tmpString.mb_str()) );
1431                 } // for i
1432                 fprintf(pFile,"\n"  );
1433         } // for j
1434
1435         fclose(pFile);
1436 }
1437
1438 void wxContourMainFrame::SaveValuesXYZ(std::string directory,std::string namefile,int typeContourGroup)
1439 {
1440         wxBusyCursor wait;
1441         ContourExtractData      *contourextractdata = new ContourExtractData();
1442         //int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1443         contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1444
1445         std::vector<manualContourModel*> lstManConMod;
1446         std::vector<manualContourModel*> lstManConModTmp;
1447         std::vector<manualContourModel*> lstManConModExp;
1448         std::vector<double> pLstValue;
1449         std::vector<double> pLstValuePosX;
1450         std::vector<double> pLstValuePosY;
1451         std::vector<double> pLstValuePosZ;
1452
1453         std::vector<int> tempVector;
1454         _instantPanel->getInstant( tempVector );
1455
1456         vtkImageData *imagedata = _theViewPanel->getSceneManager()->GetImageData();
1457         int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
1458         int z;
1459         int ii,sizeLstContourThings;
1460
1461
1462         int minZ,maxZ;
1463         double totalZ;
1464         double porcent; 
1465         wxString tmpString;
1466         minZ    = 0;// _mbarrange->GetStart();
1467         maxZ    = sizeZ;//_mbarrange->GetEnd();
1468         totalZ  = maxZ-minZ+1;
1469
1470         contourextractdata->SetImage( imagedata);
1471
1472 // For each slice..
1473         for ( z=0 ; z<sizeZ ; z++)
1474         {
1475
1476                 porcent = 100.0* (z-minZ)/totalZ;
1477                 
1478                 tmpString.Printf(_T("Saving Values"));
1479                 tmpString.Printf(_T("  %d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1480                 interfMainPanel::getInstance()->setStringInfoPanel(tmpString);
1481                 //_staticTextInformation->SetLabel(tmpString);
1482
1483
1484                 //Extraction data from contours of each slice
1485                 contourextractdata->SetZtoBeAnalys( z);
1486
1487                 tempVector[1]=z;
1488                 Instant instant(&tempVector);
1489                 std::vector<ContourThing**> lstContourThings = kernelManager->getOutlinesAtInstant(&instant);//this->_modelManager->getOutlinesAtInstant( &instant );
1490                 sizeLstContourThings = lstContourThings.size();
1491
1492                 lstManConMod.clear();
1493                 for (ii=0 ; ii<sizeLstContourThings ; ii++)
1494                 {
1495                         ContourThing **contourthing = lstContourThings[ii];
1496                         lstManConMod.push_back( (*contourthing)->getModel() );
1497                 }
1498                 lstManConModExp =  kernelManager->ExploseEachModel( lstManConMod );
1499
1500
1501 //EED004
1502
1503                 int iContourGroup,sizeContourGroup;
1504                 if (typeContourGroup==3) // contour separete
1505                 {
1506                         sizeContourGroup=lstManConModExp.size();
1507                 } else {  // contour AND OR XOR
1508                         sizeContourGroup=1;
1509                 }
1510
1511                 for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1512                         lstManConModTmp.clear();
1513                         if (typeContourGroup==3) // contour separete
1514                         {
1515                                 lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1516                         } else {  // contour AND OR XOR
1517                                 lstManConModTmp = lstManConModExp;
1518                         }
1519
1520                         contourextractdata->SetLstManualContourModel( lstManConModTmp );
1521
1522                         pLstValue.clear();
1523                         pLstValuePosX.clear();
1524                         pLstValuePosY.clear();
1525                         pLstValuePosZ.clear();
1526                         contourextractdata->GetValuesInsideCrown(       &pLstValue,
1527                                                                                                                 &pLstValuePosX,
1528                                                                                                                 &pLstValuePosY,
1529                                                                                                                 &pLstValuePosZ);
1530
1531                         std::string temp = directory + "/" + namefile + "-slice";
1532                         temp = temp + kernelManager->intToString(z) + "-cont" + kernelManager->intToString(iContourGroup);
1533                         temp = temp + ".txt";
1534
1535                         wxString filename;
1536                         /*filename.Printf(_T("%s"),directory.c_str());
1537                         filename.append(_T("\\"));
1538                         filename.append(_T("%s"),namefile.c_str());
1539                         filename.append(_T("-slice"));
1540                         filename.append(_T("%d"),z);
1541                         filename.append(_T("-cont"));
1542                         filename.append(_T("%d"),iContourGroup);
1543                         filename.append(_T(".txt"));*/
1544                         filename.Printf(temp.c_str());
1545                         FILE *pFile=fopen((const char *)filename.mb_str(),"w+");
1546                         fprintf(pFile,"value \t x \t y \t z\n"  );
1547                         int iLstValue,sizeLstValue=pLstValue.size();
1548                         for (iLstValue=0 ; iLstValue<sizeLstValue ; iLstValue++ )
1549                         {
1550                                 fprintf(pFile,"%f\t %f\t %f\t %f\n", (float)pLstValue[iLstValue] , (float)pLstValuePosX[iLstValue], (float)pLstValuePosY[iLstValue], (float)z );
1551                         }
1552                         fclose(pFile);
1553                 } // for  iContourGroup
1554         } // for z
1555         interfMainPanel::getInstance()->setStringInfoPanel(_T(""));
1556         //_staticTextInformation->SetLabel( _T("") );
1557         delete contourextractdata;
1558 }
1559
1560 void wxContourMainFrame::SaveImageResult(std::string directory,std::string namefile, int typeContourGroup)
1561 {
1562         wxBusyCursor wait;
1563         ContourExtractData      *contourextractdata = new ContourExtractData( true );
1564         //int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1565         if (typeContourGroup==3)
1566         {
1567                 typeContourGroup=1;
1568         }
1569         contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1570
1571         std::vector<manualContourModel*> lstManConMod;
1572         std::vector<manualContourModel*> lstManConModTmp;
1573         std::vector<manualContourModel*> lstManConModExp;
1574
1575         std::vector<int> tempVector;
1576         _instantPanel->getInstant( tempVector );
1577
1578         vtkImageData *imagedata = _theViewPanel->getImageData();
1579         int sizeZ = _theViewPanel->GetImageDataSizeZ();
1580         int z;
1581         int ii,sizeLstContourThings;
1582
1583
1584         int minZ,maxZ;
1585         double totalZ;
1586         double porcent; 
1587
1588         wxString tmpString;
1589
1590         minZ    = 0;// _mbarrange->GetStart();
1591         maxZ    = sizeZ;//_mbarrange->GetEnd();
1592         totalZ  = maxZ-minZ+1;
1593
1594         contourextractdata->SetImage( imagedata);
1595
1596 // For each slice..
1597         for ( z=0 ; z<sizeZ ; z++)
1598         {
1599
1600                 porcent = 100.0* (z-minZ)/totalZ;
1601                 tmpString.Printf(_T("Saving Values"));
1602                 tmpString.Printf(_T("%d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1603                 interfMainPanel::getInstance()->setStringInfoPanel(tmpString);
1604
1605                 //Extraction data from contours of each slice
1606                 contourextractdata->SetZtoBeAnalys( z);
1607
1608                 tempVector[1]=z;
1609                 Instant instant(&tempVector);
1610                 std::vector<ContourThing**> lstContourThings = kernelManager->getOutlinesAtInstant( &instant );
1611                 sizeLstContourThings = lstContourThings.size();
1612
1613                 lstManConMod.clear();
1614                 for (ii=0 ; ii<sizeLstContourThings ; ii++)
1615                 {
1616                         ContourThing **contourthing = lstContourThings[ii];
1617                         lstManConMod.push_back( (*contourthing)->getModel() );
1618                 }
1619                 lstManConModExp =  kernelManager->ExploseEachModel( lstManConMod );
1620
1621
1622                 int iContourGroup,sizeContourGroup;
1623                 if (typeContourGroup==3) // contour separete
1624                 {
1625                         sizeContourGroup=lstManConModExp.size();
1626                 } else {  // contour AND OR XOR
1627                         sizeContourGroup=1;
1628                 }
1629
1630                 for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1631                         lstManConModTmp.clear();
1632                         if (typeContourGroup==3) // contour separete
1633                         {
1634                                 lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1635                         } else {  // contour AND OR XOR
1636                                 lstManConModTmp = lstManConModExp;
1637                         }
1638
1639                         contourextractdata->SetLstManualContourModel( lstManConModTmp );
1640
1641
1642 //                              for (ii=0 ; ii<sizeLstContourThings ; ii++)
1643 //                              {
1644 //                                      ContourThing **contourthing = lstContourThings[ii];
1645 //                                      lstManConMod.push_back( (*contourthing)->getModel() );
1646 //                              }
1647 //                              contourextractdata->SetLstManualContourModel( lstManConMod );
1648
1649                         contourextractdata->CalculateImageResult(); // with actual Z
1650
1651                 } // for  iContourGroup
1652         } // for z
1653
1654         std::string temp = directory + "/" + namefile + "-Value.mhd";
1655
1656         wxString filename;
1657         filename.Printf(_T(temp.c_str()));
1658         //filename.Printf(_T("%s\\%s-Value.mhd",directory.c_str(),namefile.c_str(),z);
1659         /*filename.Printf(_T("%s"),directory.c_str());
1660         filename.append(_T("\\"));
1661         filename.append(_T("%s"),namefile.c_str());
1662         filename.append(_T("-Value.mhd"));*/
1663
1664 // Image Value
1665         vtkMetaImageWriter *writerValueImage = vtkMetaImageWriter::New( );
1666         writerValueImage->SetInput( contourextractdata->GetVtkImageValueResult() );
1667         writerValueImage->SetFileName( (const char *)filename.mb_str() );
1668         writerValueImage->SetFileDimensionality( 3 );
1669         writerValueImage->Write( );
1670
1671 // Image Mask
1672         //              filename.Printf("%s\\%s-Mask.mhd",directory.c_str(),namefile.c_str(),z);
1673         temp = directory + "/" + namefile + "-Mask.mhd";
1674
1675         filename.Printf(_T(temp.c_str()));
1676
1677         /*filename.Printf(_T("%s"),directory.c_str());
1678         filename.append(_T("\\"));
1679         filename.append(_T("%s"),namefile.c_str());
1680         filename.append(_T("-Mask.mhd"));*/
1681
1682         vtkMetaImageWriter *writerMaskImage = vtkMetaImageWriter::New( );
1683         writerMaskImage->SetInput( contourextractdata->GetVtkImageMaskResult() );
1684         writerMaskImage->SetFileName( (const char *)filename.mb_str() );
1685         writerMaskImage->SetFileDimensionality( 3 );
1686         writerMaskImage->Write( );
1687
1688         interfMainPanel::getInstance()->setStringInfoPanel( _T("") );
1689         delete contourextractdata;
1690 }
1691
1692 void wxContourMainFrame::onChangeInstantInstantPanel(std::string name,int value, int minshow, int maxshow){
1693         if( name.compare( _theViewPanel->getVerticalConceptName() ) == 0){
1694                 _theViewPanel->setActualVertical( value);
1695         }
1696         else if( name.compare( _theViewPanel->getHorizontalConceptName() ) == 0|| _instantPanel->getIfConceptCheckedAt( name, 0 ) )     {
1697                 _theViewPanel->setHorizontalConcept( name, minshow, maxshow, minshow, maxshow, value);
1698         }
1699
1700         changeInstant();
1701 }