]> Creatis software - creaContours.git/blob - lib/Interface_ManagerContour_NDimensions/wxContourMainFrame.cxx
e0947b072cc35f9aff7b0cf9d9efc5bdd64e87e1
[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         }else{
615                 int z = _instantPanel->getConceptDataOf("Axe Depth")->getActualValue();
616                 _theViewPanel->setImageSlice(z);
617         }
618 }
619 void wxContourMainFrame::updateInstantAxes(){
620 }
621
622 void wxContourMainFrame::onChangeDeep(int val){
623         _instantPanel->setConceptValue( "Axe Depth", (int)val );        
624         changeInstant();
625 }
626
627
628 void wxContourMainFrame::onCopy(){
629         std::vector<std::string> currentSelection = _theViewPanel->getSelectedObjects();
630
631         std::vector<int> tempVector;
632         _instantPanel->getInstant( tempVector );
633     _performingOperation->reset();
634         _performingOperation->setStartCommand( COPY );
635         _performingOperation->setStartOperationInstantVector( tempVector );
636         _performingOperation->setKeyNamesOperationElems( currentSelection );
637 }
638 void wxContourMainFrame::onPaste(){
639
640
641         char theStartCommand = _performingOperation->getStartCommand();
642         if (  theStartCommand == COPY )
643         {
644                 //JCP 20-10-08 Undo redo implementation
645                 saveState();
646                 //JCP 20-10-08 Undo redo implementation
647                 std::vector<int> tempVector;
648                 _instantPanel->getInstant( tempVector );
649                 _performingOperation->setEndOperationInstantVector ( tempVector );
650                 std::vector<std::string> elems = _performingOperation->getKeyNamesOperationElems();
651                 int i,size = elems.size();                      
652                 for( i=0; i<size; i++ )
653                 {                               
654                         createCopyContourOf( elems[i], tempVector, i>0 );
655                 }               
656         }
657 }
658 void wxContourMainFrame::onUndo(){
659         std::string filename;
660         if(kernelManager->onUndoSaveFile(filename)){
661                 saveFileWithContours(filename);
662         }
663         if(kernelManager->onUndo(filename)){
664                 loadState(filename);
665         }
666 }
667 void wxContourMainFrame::onRedo(){
668         std::string filename;
669         if(kernelManager->onRedo(filename)){
670                 loadState(filename);
671         }
672         
673 }
674 void wxContourMainFrame :: createCopyContourOf ( std::string anExistingKName, std::vector<int> &instantNoTouchData, bool append ){
675         std::string cloneName = kernelManager->createCopyContourOf( anExistingKName, instantNoTouchData );
676         manualContourModel * manualModel = kernelManager->getOutlineByKeyName(cloneName);//_modelManager->getOutlineByKeyName( cloneName )->getModel();
677         _theViewPanel->getSceneManager()->createCopyContourOf( anExistingKName, cloneName, manualModel , append );
678 }
679
680 void wxContourMainFrame ::loadState(std::string filename){
681         char tmp[255];
682         FILE *pFile=fopen(filename.c_str(),"r+");
683
684         fscanf(pFile,"%s",tmp); // --CreaContour--
685
686         fscanf(pFile,"%s",tmp); // Version
687         fscanf(pFile,"%s",tmp); // 1.0.1
688         std::string version(tmp);
689
690         deleteAllContours();
691
692         openContours(pFile,false);
693         if (version!="1.0.0"){
694                 openContours(pFile,true);
695         }
696
697         fclose(pFile);
698         
699         //_theViewPanel->getSceneManager()->removeSceneContours();
700         //changeInstant();
701
702 }
703
704 void wxContourMainFrame::onLoad(){
705         char tmp[255];
706         wxFileDialog dialog(NULL, _T("Choose a file"), _T(""), _T(""), _T("*.roi"), wxOPEN );
707         if (dialog.ShowModal() == wxID_OK)
708         {
709                 std::string fileNameContourROI = (const char *)(dialog.GetPath().mb_str());
710                 FILE *pFile=fopen(fileNameContourROI.c_str(),"r+");
711
712                 fscanf(pFile,"%s",tmp); // --CreaContour--
713
714                 fscanf(pFile,"%s",tmp); // Version
715                 fscanf(pFile,"%s",tmp); // 1.0.1
716                 std::string version(tmp);
717
718                 openContours(pFile,false);
719                 if (version!="1.0.0"){
720                         openContours(pFile,true);
721                 }
722 //                        _theViewPanel->getSceneManager()->openFileWithContours(ff);
723
724                 fclose(pFile);
725         }
726         _theViewPanel->getSceneManager()->removeSceneContours();
727         changeInstant();
728 }
729 void wxContourMainFrame::onSave(){
730         wxFileDialog dialog(NULL, _T("Choose a file"), _T(""), _T(""), _T("*.roi"), wxSAVE );
731         if (dialog.ShowModal() == wxID_OK)
732         {
733                 std::string fileNameContourROI = (const char *)(dialog.GetPath().mb_str());
734                 saveFileWithContours( fileNameContourROI );
735         }
736 }
737 void wxContourMainFrame::saveFileWithContours( std::string filename ){
738
739         FILE *pFile=fopen(filename.c_str(),"w+");
740         std::vector< std::string > lstNameThings;
741         int i,sizeLstNameThings; 
742
743         fprintf(pFile,"--CreaContour--\n");
744         fprintf(pFile,"Version %s\n", "1.0.1" );
745
746         // Normal Contours
747         lstNameThings           = kernelManager->GetLstNameThings();
748         sizeLstNameThings       = lstNameThings.size(); 
749         fprintf(pFile,"NumberOfContours %d\n", sizeLstNameThings );
750         for (i=0 ; i<sizeLstNameThings ; i++) 
751         {
752                 kernelManager->SaveThingName( pFile, lstNameThings[i] );
753                 _theViewPanel->getSceneManager()->SaveThingName( pFile, lstNameThings[i] );
754         }// for i
755
756         //-- Contours Statics 
757         lstNameThings           = kernelManager->GetLstNameThingsStatic();
758         sizeLstNameThings       = lstNameThings.size(); 
759         fprintf(pFile,"NumberOfContoursStatic %d\n", sizeLstNameThings );
760         for (i=0 ; i<sizeLstNameThings ; i++) 
761         {
762                 kernelManager->SaveThingName( pFile, lstNameThings[i] );
763                 _theViewPanel->getSceneManager()->SaveThingName( pFile, lstNameThings[i] );
764         }// for i
765         fclose(pFile);
766 }
767
768 void wxContourMainFrame::openContours( FILE *pFile, bool staticContour ){
769         char tmp[255];
770         fscanf(pFile,"%s",tmp); // NumberOfContours 
771         fscanf(pFile,"%s",tmp); // ##
772         int numberOfContours = atoi(tmp);  
773
774         std::vector<int> instantVector;
775         int typeContourModel;
776         manualContourModel *manModelContour;
777         int typeView;
778
779         int i;
780         for (i=0;i<numberOfContours;i++)
781         {
782                 instantVector.clear();
783                 fscanf(pFile,"%s",tmp); // Instant
784
785                 fscanf(pFile,"%s",tmp); // 1
786                 instantVector.push_back( atoi(tmp) );
787                 fscanf(pFile,"%s",tmp); // 2
788                 instantVector.push_back( atoi(tmp) );
789                 fscanf(pFile,"%s",tmp); // 3
790                 instantVector.push_back( atoi(tmp) );
791                 fscanf(pFile,"%s",tmp); // 4
792                 instantVector.push_back( atoi(tmp) );
793                 fscanf(pFile,"%s",tmp); // 5
794                 instantVector.push_back( atoi(tmp) );
795                 fscanf(pFile,"%s",tmp); // 6
796                 instantVector.push_back( atoi(tmp) );
797
798
799                 fscanf(pFile,"%s",tmp); // TypeContourModel
800                 fscanf(pFile,"%s",tmp); // ##
801                 typeContourModel = atoi(tmp);
802
803                 manModelContour =  kernelManager->factoryManualContourModel(typeContourModel);
804                 manModelContour->Open(pFile);
805
806                 fscanf(pFile,"%s",tmp); // TypeView
807                 fscanf(pFile,"%s",tmp); // ##
808                 typeView = atoi(tmp);  
809
810
811 //                      if (typeView==1) 
812 //                      {
813 //                      }
814
815
816                 std::string theName;
817                 theName = kernelManager->createOutline( manModelContour, instantVector );
818                 bool addedModel = theName.compare("") != 0;
819                 if( addedModel )
820                 {
821                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
822                         _theViewPanel->getSpacing(spc);                                 
823                         //Adding the manualContourControler to interface objects structure
824                         //Adding the manualViewContour to interface objects structure           
825                         //_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.
826                         _theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc , typeView) ;
827                 }       
828
829                 if (staticContour==true)
830                 {
831                         Instant instant(&instantVector);
832                         kernelManager->changeContourOfManager( theName , &instant );
833                 }
834
835         }// for  numberOfContours
836
837 }
838
839 void wxContourMainFrame::RefreshInterface(){
840         changeInstant();
841         _theViewPanel->RefreshInterface();
842         //wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)this->_theViewPanel->getWxVtkBaseView();
843         //wxvtk2dbaseview->Refresh();
844 }
845
846 vtkImageData* wxContourMainFrame::getImageData(){
847         return _theViewPanel->getImageData();
848 }
849
850 void wxContourMainFrame::onSegmentationOneSlice(int isovalue,int sampling,int method){
851         
852         //JCP 20-10-08 Undo redo implementation
853         saveState();
854         //JCP 20-10-08 Undo redo implementation
855         
856         wxBusyCursor wait;
857         int                                     x                                       = _theViewPanel->GetX();
858         int                                     y                                       = _theViewPanel->GetY();
859         int                                     z                                       = _theViewPanel->GetZ();
860         SegmentationOneSlice( x,y,z,isovalue, sampling, method );
861         RefreshInterface();
862 }
863
864 void wxContourMainFrame::SegmentationOneSlice( int x, int y, int z, int isovalue, int sampling, int method )
865 {               
866         int typeofcontour = 1;
867         //--Extracting Contour
868         //vtkImageData  *imagedata      = _theViewPanel->getSceneManager()->GetImageData();
869         vtkImageData    *imagedata      = getImageData();
870 //              double                  *range          = imagedata->GetScalarRange();  
871 //              double                  thr                     = 1;
872
873         vtkImageReslice *imageReslice = vtkImageReslice::New();
874 //EED
875 //              double spc[3];
876 //              imagedata->GetSpacing(spc);
877 //              x = x*spc[0];
878 //              y = y*spc[1];
879 //              z = z*spc[3];
880
881         imageReslice->SetInput( imagedata );
882         imageReslice->SetInformationInput(imagedata);
883         imageReslice->SetResliceAxesDirectionCosines(1,0,0, 0,1,0 ,0,0,1);
884         imageReslice->SetResliceAxesOrigin(0,0,z);
885         imageReslice->SetOutputDimensionality(2);
886         imageReslice->SetInterpolationModeToLinear();
887
888         imagedata = imageReslice->GetOutput();
889         imagedata->Update();
890         imagedata->UpdateInformation();
891
892         vtkContourFilter* cntVTK = vtkContourFilter::New( );
893         cntVTK->SetInput( imagedata );
894
895         cntVTK->SetNumberOfContours( 1 );
896         //cntVTK->SetValue( 0, vmin );
897 //              cntVTK->SetValue( 0, (range[1]*thr/100) );
898         cntVTK->SetValue( 1, isovalue );
899 //      cntVTK->SetValue( 1, vmax );
900         cntVTK->Update( );
901         cntVTK->UpdateInformation();
902                 
903         vtkCleanPolyData* cpd = vtkCleanPolyData::New( );
904         cpd->SetInput( cntVTK->GetOutput( ) );
905         cpd->ConvertLinesToPointsOff( );
906         cpd->Update( );
907         cpd->UpdateInformation();
908
909         vtkPolyDataConnectivityFilter* conn = vtkPolyDataConnectivityFilter::New( );
910         conn->SetExtractionModeToClosestPointRegion( );
911         //conn->SetMaxRecursionDepth( 3000 );
912                 
913         conn->SetInput( cpd->GetOutput( ) );
914                 
915         conn->SetClosestPoint( x, y, 0 );
916         conn->Update( );
917         conn->UpdateInformation();
918                 
919         vtkCleanPolyData* cpd2 = vtkCleanPolyData::New( );
920         cpd2->SetInput( conn->GetOutput( ) );
921         cpd2->Update();
922         cpd2->UpdateInformation();
923
924         vtkStripper* vtkstripper = vtkStripper::New( );
925         vtkstripper->SetInput( cpd2->GetOutput() );
926         vtkstripper->Update();
927         vtkstripper->UpdateInformation();
928
929
930         vtkPolyData* polyDataResult =  vtkstripper->GetOutput();
931
932         polyDataResult->Update( );
933         polyDataResult->UpdateInformation();
934
935 /* EED
936 ofstream myfile;
937 myfile.open ("c:/temp/example.txt");
938 myfile << "\n";
939 polyDataResult->Print(myfile);
940 myfile << "-------------------------------------\n";
941 polyDataResult->GetLines()->Print(myfile);
942 myfile.close();
943 */
944
945         cntVTK          -> Delete();
946         cpd2            -> Delete();
947         cpd                     -> Delete();
948         conn            -> Delete();
949
950
951 //--Calculating control points
952
953         std::vector<double> vecX;
954         std::vector<double> vecY;
955         std::vector<double> vecZ;
956
957         std::vector<double> vecCtrlPointX;
958         std::vector<double> vecCtrlPointY;
959         std::vector<double> vecCtrlPointZ;
960
961
962         double *p;
963         int ii,size=polyDataResult->GetNumberOfPoints();
964         int id;
965         for (ii=1;ii<=size;ii++)
966         {
967                 id      = polyDataResult->GetLines()->GetData()->GetValue(ii);
968                 p       = polyDataResult->GetPoint(id);
969                 double x=p[0];
970                 double y=p[1];
971                 vecX.push_back( p[0] );
972                 vecY.push_back( p[1] );
973                 vecZ.push_back( -900 );
974 //                      vecZ.push_back( p[2] );
975         }
976
977
978         ExtractControlPoints2D *extractcontrolpoints2d = new ExtractControlPoints2D();
979         extractcontrolpoints2d->SetContour( &vecX , &vecY , &vecZ );
980
981 //PROOFS
982         /*if (methodRadiobox->GetSelection()==0){
983                 extractcontrolpoints2d->GetInitialControlPoints( &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
984         }
985         if (methodRadiobox->GetSelection()==1){
986                 extractcontrolpoints2d->GetControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
987         }
988         if (methodRadiobox->GetSelection()==2){
989                 extractcontrolpoints2d->SetSamplingControlPoints( sampling );
990                 extractcontrolpoints2d->GetSamplingControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
991         }*/
992
993         if (method==0){
994                 extractcontrolpoints2d->GetInitialControlPoints( &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
995         }
996         else if (method==1){
997                 extractcontrolpoints2d->GetControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
998         }
999         else if (method==2){
1000                 extractcontrolpoints2d->SetSamplingControlPoints( sampling );
1001                 extractcontrolpoints2d->GetSamplingControlPoints(  &vecCtrlPointX , &vecCtrlPointY , &vecCtrlPointZ );
1002         }
1003
1004         //--Adding contour to the system
1005
1006         std::vector<int> actualInstantVector;
1007         _instantPanel->getInstant( actualInstantVector );
1008         actualInstantVector[1]=z;
1009
1010         int j,sizeCtrPt = vecCtrlPointX.size();
1011         manualContourModel *manModelContour =  kernelManager->factoryManualContourModel( typeofcontour );
1012         manModelContour->SetNumberOfPointsSpline( ((sizeCtrPt/100)+1)*100 );
1013         if (sizeCtrPt>=3){
1014                 for (j=0 ; j<sizeCtrPt ; j++)
1015                 {
1016                         manModelContour->AddPoint( vecCtrlPointX[j] , vecCtrlPointY[j] , vecCtrlPointZ[j]  );
1017                 } // for
1018                 std::string theName;
1019                 //theName = _modelManager->createOutline( manModelContour, actualInstantVector );
1020                 theName = kernelManager->createOutline( manModelContour, actualInstantVector );
1021                 bool addedModel = theName.compare("") != 0;
1022                 if( addedModel )
1023                 {
1024                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
1025                         _theViewPanel->getSpacing(spc);                                 
1026                         //Adding the manualContourControler to interface objects structure
1027                         //Adding the manualViewContour to interface objects structure           
1028                         //_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.
1029                         _theViewPanel->configureViewControlTo(theName, manModelContour, spc, typeofcontour);
1030                         //_theViewPanel->getSceneManager()->configureViewControlTo( theName, manModelContour,spc, typeofcontour ) ;
1031                 }       // if addedModel
1032         } // if sizeCtrPt
1033 }
1034
1035 int wxContourMainFrame::GetImageDataSizeZ(){
1036         return _theViewPanel->GetImageDataSizeZ();
1037 }
1038
1039 void wxContourMainFrame::GetImageDataRange(double *range){
1040         _theViewPanel->GetImageDataRange(range);
1041 }
1042
1043 void wxContourMainFrame::onSegmentationAllSlice(int minZ,int maxZ,int isovalue,int sampling,int method){
1044         //JCP 20-10-08 Undo redo implementation
1045         saveState();
1046         //JCP 20-10-08 Undo redo implementation
1047         
1048         wxBusyCursor wait;
1049         int                                     x                                       = _theViewPanel->GetX();
1050         int                                     y                                       = _theViewPanel->GetY();
1051         int z;
1052         double porcent; 
1053         wxString tmpString;     
1054         double totalZ = maxZ-minZ+1;
1055         for( z=minZ ; z<=maxZ ; z++ )
1056         {
1057                 porcent = 100.0* (z-minZ)/totalZ;
1058                 tmpString.Printf(_T("  %d %c            %d/%d            %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ,z );
1059                 interfMainPanel::getInstance()->setLabelSegmentationPanelVTK(tmpString);
1060                 //_staticTextSegmentation->SetLabel(tmpString);
1061                 SegmentationOneSlice( x,y,z,isovalue, sampling,method );
1062         }
1063         interfMainPanel::getInstance()->setLabelSegmentationPanelVTK(_T("   "));
1064         RefreshInterface();
1065 }
1066
1067 void wxContourMainFrame::showAxis(bool show){
1068         _theViewPanel->SetVisibleAxis(show);
1069         _theViewPanel->Refresh();
1070 }
1071
1072
1073 void wxContourMainFrame::onRigidPressed(){
1074         std::vector<std::string> currentSelection = _theViewPanel->getSelectedObjects();
1075         int elementsSelected = currentSelection.size();
1076
1077         if ( elementsSelected >= 1 ){
1078                 //JCP 20-10-08 Undo redo implementation
1079                 saveState();
1080                 //JCP 20-10-08 Undo redo implementation
1081
1082                 std::vector<int> tempVector;
1083                 _instantPanel->getInstant( tempVector );
1084                 Instant instant(&tempVector);
1085
1086                 int i , size=currentSelection.size();
1087                 for ( i=0 ; i<size ; i++ )
1088                 {
1089                         kernelManager->changeContourOfManager(currentSelection[i], &instant);
1090                 } // i
1091         }
1092 }
1093
1094 void wxContourMainFrame::onWidthOfContour(double width){
1095
1096         _theViewPanel->removeSceneContours();
1097         _theViewPanel->SetWidthContour(width);
1098         
1099         //_theViewPanel->getSceneManager()->SetWidthContour( width );
1100
1101         RefreshInterface();
1102 }
1103 int wxContourMainFrame::getColorWindow(){
1104         return _theViewPanel->getColorWindow();
1105 }
1106 int wxContourMainFrame::getWindowLevel(){
1107         return _theViewPanel->getWindowLevel();
1108 }
1109
1110 void wxContourMainFrame::onBrigthnessColorWindowLevel(int colorwindow,int windowlevel){
1111         _theViewPanel->onBrigthnessColorWindowLevel(colorwindow, windowlevel);
1112
1113         
1114         RefreshInterface();
1115 }
1116
1117 void wxContourMainFrame::onInterpolation(bool interpolate){
1118         _theViewPanel->onInterpolation(interpolate);    
1119         RefreshInterface();
1120 }
1121
1122 void wxContourMainFrame::onChangeInstant(std::string name,int actual){
1123         _instantPanel->setConceptValue( name, actual );
1124 }
1125
1126 void wxContourMainFrame::resetAppend(){
1127         kernelManager->resetAppend();
1128         
1129 }
1130
1131 void wxContourMainFrame::onSpreadAdd(){
1132         std::vector<double> vecX; 
1133         std::vector<double> vecY; 
1134         std::vector<double> vecZ; 
1135         _theViewPanel->GetPointsOfActualContour(&vecX , &vecY , &vecZ );
1136         
1137         std::vector<int> tempVector;
1138         _instantPanel->getInstant( tempVector );
1139
1140         std::string val = kernelManager->onSpreadAdd(&vecX , &vecY , &vecZ, tempVector);
1141
1142         interfMainPanel::getInstance()->appendStringSpread(val);
1143         
1144 }
1145
1146 void wxContourMainFrame::onSpreadGo(int type){
1147
1148         //JCP 20-10-08 Undo redo implementation
1149         saveState();
1150         //JCP 20-10-08 Undo redo implementation
1151
1152         wxBusyCursor wait;      
1153         std::vector<double> vecCtrlPointX;
1154         std::vector<double> vecCtrlPointY;
1155         std::vector<double> vecCtrlPointZ;
1156         double  minZ,maxZ;
1157         int z;
1158
1159         std::vector<int> tempVector;
1160         _instantPanel->getInstant( tempVector );
1161 //              tempVector[1];
1162
1163         kernelManager->getMaxMinZ(&minZ,&maxZ);
1164 //JSTG_16-07-08_----------------------------------------------------------------
1165         //_contourPropagation->setInterpolationNumber(maxZ-minZ+1);
1166         kernelManager->CalculeSplinePropagation();
1167
1168         double          totalZ = maxZ-minZ+1;
1169         double          porcent;
1170         wxString        tmpString;
1171 //--------------------------------------------------------------------
1172         for ( z=(int)minZ ; z<=(int)maxZ ; z++ )
1173         {
1174                 porcent = 100.0* (z-minZ)/totalZ;
1175                 int numero = (int)(z-minZ+1)/(int)totalZ;
1176                 std::string stringtemp = "  "+kernelManager->intToString(porcent)+"%            "+kernelManager->intToString(numero)+"            "+kernelManager->intToString(z);
1177                 
1178                 interfMainPanel::getInstance()->setStringSpread(stringtemp);    
1179
1180                 int typeofcontour = 1;
1181                 std::string theName;
1182                 manualContourModel* manModelContour = kernelManager->GetPoints(z, type, &vecCtrlPointX,&vecCtrlPointY,&vecCtrlPointZ, theName,typeofcontour, tempVector);
1183
1184                 if( manModelContour!=NULL ){
1185                         double spc[3];//Si no hay imagen pero hay contornos que spacing se pone por default
1186                         this->_theViewPanel->getSpacing(spc);                                   
1187                         //Adding the manualContourControler to interface objects structure
1188                         //Adding the manualViewContour to interface objects structure           
1189                         //_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.
1190                         _theViewPanel->configureViewControlTo( theName, manModelContour,spc,typeofcontour ) ;
1191                 } // if addedModel
1192         } 
1193         if(z > maxZ){
1194                 porcent = 100.0;
1195                 int numero = (int)(z-minZ+1)/(int)totalZ;
1196                 std::string stringtemp = "  "+kernelManager->intToString(porcent)+"%            "+kernelManager->intToString(numero)+"            "+kernelManager->intToString(z);
1197                 
1198                 interfMainPanel::getInstance()->setStringSpread(stringtemp);
1199         }// for z
1200         //RefreshInterface();
1201 }
1202
1203 void wxContourMainFrame::getInstantVector(std::vector<int>& tempVector){
1204         
1205 }
1206
1207 std::vector<std::string> wxContourMainFrame::getOutlinesName(int slide){
1208         std::vector<int> tempVector;
1209         _instantPanel->getInstant(tempVector);
1210         //Asignation of slide number should be different ex by name
1211         tempVector[1]=slide;
1212         return kernelManager->getOutlinesNameAtInstant(tempVector);
1213 }
1214
1215 void wxContourMainFrame::onInformationContourLabels(int typeContourGroup,int selection,int minimumZ,int maximumZ){
1216         int maxContourGroup     =       0;
1217
1218         ContourExtractData      *contourextractdata = new ContourExtractData();
1219         
1220         contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1221
1222         std::vector<manualContourModel*> lstManConMod;
1223         std::vector<manualContourModel*> lstManConModTmp;
1224         std::vector<manualContourModel*> lstManConModExp;
1225         std::vector<double> pLstValue;
1226         std::vector<double> pLstValuePosX;
1227         std::vector<double> pLstValuePosY;
1228         std::vector<double> pLstValuePosZ;
1229
1230         int             resultSize; 
1231         int             resultGrayRangeCount;
1232         double  resultMin; 
1233         double  resultMax;
1234         double  resultAverage;
1235         double  resultStandardeviation;
1236
1237         std::vector<int> tempVector;
1238         _instantPanel->getInstant( tempVector );
1239
1240         vtkImageData *imagedata = _theViewPanel->getImageData();
1241         int sizeZ = _theViewPanel->GetImageDataSizeZ();
1242         int z;
1243         int ii,sizeLstContourThings;
1244
1245
1246         int minZ,maxZ;
1247         double totalZ;
1248         double porcent; 
1249         wxString tmpString;
1250
1251         
1252
1253         if (selection==0 ) // actual slice
1254         {
1255                 std::vector<int> tempVector;
1256                 _instantPanel->getInstant( tempVector );
1257                 int actualSlice = tempVector[1];
1258                 minZ    = actualSlice;
1259                 maxZ    = actualSlice;
1260         }
1261         else if (selection==2 ) // All slices
1262         {
1263                 minZ    = 0;
1264                 maxZ    = sizeZ-1;
1265         }
1266         else if (selection==1 ) // slice range
1267         {
1268                 minZ    = minimumZ;
1269                 maxZ    = maximumZ;
1270         }
1271         
1272
1273         totalZ  = maxZ-minZ+1;
1274         contourextractdata->SetImage( imagedata);
1275
1276 // For each slice..
1277         for ( z=minZ ; z<=maxZ ; z++ )
1278         {
1279                 
1280                 porcent = 100.0* (z-minZ)/totalZ;
1281                 tmpString.Printf(_T("  %d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1282
1283                 interfMainPanel::getInstance()->setStringInfoPanel(tmpString);          
1284                 //_staticTextInformation->SetLabel(tmpString);
1285
1286                 //Extraction data from contours of each slice
1287                 contourextractdata->SetZtoBeAnalys( z);
1288
1289                 tempVector[1]=z;
1290                 Instant instant(&tempVector);
1291                 std::vector<ContourThing**> lstContourThings = kernelManager->getOutlinesAtInstant( &instant );
1292                 sizeLstContourThings = lstContourThings.size();
1293
1294                 lstManConMod.clear();
1295                 for (ii=0 ; ii<sizeLstContourThings ; ii++)
1296                 {
1297                         ContourThing **contourthing = lstContourThings[ii];
1298                         lstManConMod.push_back( (*contourthing)->getModel() );
1299                 }
1300                 lstManConModExp =  kernelManager->ExploseEachModel( lstManConMod );
1301
1302
1303                 wxString tempString;
1304                 tempString.Printf(_T("%d - %d"),z, sizeLstContourThings);
1305                 interfMainPanel::getInstance()->setRowLabelInfoPanel(z, tempString);
1306                 //_grid->SetRowLabelValue(z, tempString );
1307 //EED004
1308                 int iContourGroup,sizeContourGroup;
1309                 if (typeContourGroup==3) // contour separete
1310                 {
1311                         sizeContourGroup=lstManConModExp.size();
1312                         if ( maxContourGroup<sizeContourGroup ) 
1313                         {
1314                                 maxContourGroup=sizeContourGroup;
1315                         }
1316                 } else {  // contour AND OR XOR
1317                         sizeContourGroup=1;
1318                         maxContourGroup=1;
1319                 }
1320
1321                 int tmpIntA;
1322
1323
1324
1325                 for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1326                         lstManConModTmp.clear();
1327                         if (typeContourGroup==3) // contour separete
1328                         {
1329                                 lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1330                         } else {  // contour AND OR XOR
1331                                 lstManConModTmp = lstManConModExp;
1332                         }
1333
1334                         contourextractdata->SetLstManualContourModel( lstManConModTmp );
1335
1336                         pLstValue.clear();
1337                         pLstValuePosX.clear();
1338                         pLstValuePosY.clear();
1339                         pLstValuePosZ.clear();
1340                         contourextractdata->GetValuesInsideCrown(       &pLstValue,
1341                                                                                                                 &pLstValuePosX,
1342                                                                                                                 &pLstValuePosY,
1343                                                                                                                 &pLstValuePosZ);
1344                         // Statistics of each slice.
1345                         contourextractdata->Statistics( &pLstValue,
1346                                                                                         minimumZ,
1347                                                                                         maximumZ,
1348                                                                                         &resultGrayRangeCount, 
1349                                                                                         &resultSize, 
1350                                                                                         &resultMin, 
1351                                                                                         &resultMax,
1352                                                                                         &resultAverage,
1353                                                                                         &resultStandardeviation);
1354                         
1355                         if (interfMainPanel::getInstance()->getNumberColsInformationPanel()<_numberOfVariablesStatistics*(iContourGroup+1)  )
1356                         {
1357                                 interfMainPanel::getInstance()->appendColsInformationPanel(_numberOfVariablesStatistics);
1358                         }
1359
1360                         tmpIntA=_numberOfVariablesStatistics*iContourGroup ;
1361
1362                         tempString.Printf(_T("%d"),resultSize);
1363                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA, tempString);  
1364
1365                         tempString.Printf(_T("%d"),resultGrayRangeCount);
1366                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+1, tempString);
1367                         
1368                         tempString.Printf(_T("%f"),resultMin);
1369                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+2, tempString);
1370                         
1371                         tempString.Printf(_T("%f"),resultMax);
1372                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+3, tempString);
1373                         
1374                         tempString.Printf(_T("%f"),resultAverage);
1375                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+4, tempString);
1376                         
1377                         tempString.Printf(_T("%f"),resultStandardeviation);
1378                         interfMainPanel::getInstance()->setCellValueInfoPanel(z, tmpIntA+5, tempString);
1379                         
1380
1381                 } // for iContourGroup
1382         } // for z
1383
1384         int iTitleGroup;
1385         wxString tmpTitleString;
1386         int tmpIntB;
1387         for ( iTitleGroup=0 ; iTitleGroup<maxContourGroup ; iTitleGroup++ )
1388         {
1389                 tmpIntB =_numberOfVariablesStatistics*iTitleGroup;
1390                 tmpTitleString.Printf(_T("%d-Size Data"),iTitleGroup);
1391                 interfMainPanel::getInstance()->setColLableInfoPanel(tmpIntB + 0, tmpTitleString);
1392
1393                 interfMainPanel::getInstance()->setColLableInfoPanel(tmpIntB + 0, tmpTitleString );
1394                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 1, _T("Size Range") );
1395                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 2, _T("Min") );
1396                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 3, _T("Max") );
1397                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 4, _T("Average") );
1398                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 5, _T("St.Dev.") );
1399                 interfMainPanel::getInstance()->setColLableInfoPanel( tmpIntB + 6, _T(" ") );
1400         }
1401
1402         interfMainPanel::getInstance()->setStringInfoPanel( _T("") );
1403         delete contourextractdata;
1404 }
1405
1406 void wxContourMainFrame::onSaveResults(std::string directory,std::string namefile, std::string filename, int typeContourGroup){
1407     SaveValuesXYZ( directory , namefile, typeContourGroup );
1408         SaveImageResult( directory , namefile, typeContourGroup);
1409
1410         
1411         FILE *pFile=fopen(filename.c_str(),"w+");
1412
1413         int sizeZ = _theViewPanel->GetImageDataSizeZ();
1414
1415         wxString tmpString;
1416         int i,j,maxX,maxY=sizeZ;
1417         maxX= interfMainPanel::getInstance()->getNumberColsInformationPanel();//this->_grid->GetNumberCols();
1418
1419         int iTitle,sizeTitle = (maxX / _numberOfVariablesStatistics);
1420         for ( iTitle=0; iTitle<sizeTitle ; iTitle++)
1421         {
1422                 fprintf(pFile,"-- \t %d-Size \t SizeRange \t Min \t Max \t Ave \t StDv \t" , iTitle,tmpString.c_str() );
1423         }
1424         fprintf(pFile,"\n" );
1425
1426
1427
1428         for ( j=0; j<maxY ; j++)
1429         {
1430                 fprintf(pFile,"%d\t" , j );
1431                 for (i=0 ; i<maxX ; i++){
1432                         tmpString = interfMainPanel::getInstance()->getCellValue(j, i);//_grid->GetCellValue( j , i );
1433                         fprintf(pFile,"%s\t" , (const char *)(tmpString.mb_str()) );
1434                 } // for i
1435                 fprintf(pFile,"\n"  );
1436         } // for j
1437
1438         fclose(pFile);
1439 }
1440
1441 void wxContourMainFrame::SaveValuesXYZ(std::string directory,std::string namefile,int typeContourGroup)
1442 {
1443         wxBusyCursor wait;
1444         ContourExtractData      *contourextractdata = new ContourExtractData();
1445         //int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1446         contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1447
1448         std::vector<manualContourModel*> lstManConMod;
1449         std::vector<manualContourModel*> lstManConModTmp;
1450         std::vector<manualContourModel*> lstManConModExp;
1451         std::vector<double> pLstValue;
1452         std::vector<double> pLstValuePosX;
1453         std::vector<double> pLstValuePosY;
1454         std::vector<double> pLstValuePosZ;
1455
1456         std::vector<int> tempVector;
1457         _instantPanel->getInstant( tempVector );
1458
1459         vtkImageData *imagedata = _theViewPanel->getSceneManager()->GetImageData();
1460         int sizeZ = _theViewPanel->getSceneManager()->GetImageDataSizeZ();
1461         int z;
1462         int ii,sizeLstContourThings;
1463
1464
1465         int minZ,maxZ;
1466         double totalZ;
1467         double porcent; 
1468         wxString tmpString;
1469         minZ    = 0;// _mbarrange->GetStart();
1470         maxZ    = sizeZ;//_mbarrange->GetEnd();
1471         totalZ  = maxZ-minZ+1;
1472
1473         contourextractdata->SetImage( imagedata);
1474
1475 // For each slice..
1476         for ( z=0 ; z<sizeZ ; z++)
1477         {
1478
1479                 porcent = 100.0* (z-minZ)/totalZ;
1480                 
1481                 tmpString.Printf(_T("Saving Values"));
1482                 tmpString.Printf(_T("  %d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1483                 interfMainPanel::getInstance()->setStringInfoPanel(tmpString);
1484                 //_staticTextInformation->SetLabel(tmpString);
1485
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 = kernelManager->getOutlinesAtInstant(&instant);//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 =  kernelManager->ExploseEachModel( lstManConMod );
1502
1503
1504 //EED004
1505
1506                 int iContourGroup,sizeContourGroup;
1507                 if (typeContourGroup==3) // contour separete
1508                 {
1509                         sizeContourGroup=lstManConModExp.size();
1510                 } else {  // contour AND OR XOR
1511                         sizeContourGroup=1;
1512                 }
1513
1514                 for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1515                         lstManConModTmp.clear();
1516                         if (typeContourGroup==3) // contour separete
1517                         {
1518                                 lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1519                         } else {  // contour AND OR XOR
1520                                 lstManConModTmp = lstManConModExp;
1521                         }
1522
1523                         contourextractdata->SetLstManualContourModel( lstManConModTmp );
1524
1525                         pLstValue.clear();
1526                         pLstValuePosX.clear();
1527                         pLstValuePosY.clear();
1528                         pLstValuePosZ.clear();
1529                         contourextractdata->GetValuesInsideCrown(       &pLstValue,
1530                                                                                                                 &pLstValuePosX,
1531                                                                                                                 &pLstValuePosY,
1532                                                                                                                 &pLstValuePosZ);
1533
1534                         std::string temp = directory + "/" + namefile + "-slice";
1535                         temp = temp + kernelManager->intToString(z) + "-cont" + kernelManager->intToString(iContourGroup);
1536                         temp = temp + ".txt";
1537
1538                         wxString filename;
1539                         /*filename.Printf(_T("%s"),directory.c_str());
1540                         filename.append(_T("\\"));
1541                         filename.append(_T("%s"),namefile.c_str());
1542                         filename.append(_T("-slice"));
1543                         filename.append(_T("%d"),z);
1544                         filename.append(_T("-cont"));
1545                         filename.append(_T("%d"),iContourGroup);
1546                         filename.append(_T(".txt"));*/
1547                         filename.Printf(temp.c_str());
1548                         FILE *pFile=fopen((const char *)filename.mb_str(),"w+");
1549                         fprintf(pFile,"value \t x \t y \t z\n"  );
1550                         int iLstValue,sizeLstValue=pLstValue.size();
1551                         for (iLstValue=0 ; iLstValue<sizeLstValue ; iLstValue++ )
1552                         {
1553                                 fprintf(pFile,"%f\t %f\t %f\t %f\n", (float)pLstValue[iLstValue] , (float)pLstValuePosX[iLstValue], (float)pLstValuePosY[iLstValue], (float)z );
1554                         }
1555                         fclose(pFile);
1556                 } // for  iContourGroup
1557         } // for z
1558         interfMainPanel::getInstance()->setStringInfoPanel(_T(""));
1559         //_staticTextInformation->SetLabel( _T("") );
1560         delete contourextractdata;
1561 }
1562
1563 void wxContourMainFrame::SaveImageResult(std::string directory,std::string namefile, int typeContourGroup)
1564 {
1565         wxBusyCursor wait;
1566         ContourExtractData      *contourextractdata = new ContourExtractData( true );
1567         //int typeContourGroup = this->_radiolstboxContourGroup->GetSelection();
1568         if (typeContourGroup==3)
1569         {
1570                 typeContourGroup=1;
1571         }
1572         contourextractdata ->SetTypeOperation( typeContourGroup % 3 );
1573
1574         std::vector<manualContourModel*> lstManConMod;
1575         std::vector<manualContourModel*> lstManConModTmp;
1576         std::vector<manualContourModel*> lstManConModExp;
1577
1578         std::vector<int> tempVector;
1579         _instantPanel->getInstant( tempVector );
1580
1581         vtkImageData *imagedata = _theViewPanel->getImageData();
1582         int sizeZ = _theViewPanel->GetImageDataSizeZ();
1583         int z;
1584         int ii,sizeLstContourThings;
1585
1586
1587         int minZ,maxZ;
1588         double totalZ;
1589         double porcent; 
1590
1591         wxString tmpString;
1592
1593         minZ    = 0;// _mbarrange->GetStart();
1594         maxZ    = sizeZ;//_mbarrange->GetEnd();
1595         totalZ  = maxZ-minZ+1;
1596
1597         contourextractdata->SetImage( imagedata);
1598
1599 // For each slice..
1600         for ( z=0 ; z<sizeZ ; z++)
1601         {
1602
1603                 porcent = 100.0* (z-minZ)/totalZ;
1604                 tmpString.Printf(_T("Saving Values"));
1605                 tmpString.Printf(_T("%d %c            %d/%d             %d"), (int)porcent , 37 , z-minZ+1, (int)totalZ ,z);
1606                 interfMainPanel::getInstance()->setStringInfoPanel(tmpString);
1607
1608                 //Extraction data from contours of each slice
1609                 contourextractdata->SetZtoBeAnalys( z);
1610
1611                 tempVector[1]=z;
1612                 Instant instant(&tempVector);
1613                 std::vector<ContourThing**> lstContourThings = kernelManager->getOutlinesAtInstant( &instant );
1614                 sizeLstContourThings = lstContourThings.size();
1615
1616                 lstManConMod.clear();
1617                 for (ii=0 ; ii<sizeLstContourThings ; ii++)
1618                 {
1619                         ContourThing **contourthing = lstContourThings[ii];
1620                         lstManConMod.push_back( (*contourthing)->getModel() );
1621                 }
1622                 lstManConModExp =  kernelManager->ExploseEachModel( lstManConMod );
1623
1624
1625                 int iContourGroup,sizeContourGroup;
1626                 if (typeContourGroup==3) // contour separete
1627                 {
1628                         sizeContourGroup=lstManConModExp.size();
1629                 } else {  // contour AND OR XOR
1630                         sizeContourGroup=1;
1631                 }
1632
1633                 for( iContourGroup=0 ; iContourGroup<sizeContourGroup ; iContourGroup++ ){
1634                         lstManConModTmp.clear();
1635                         if (typeContourGroup==3) // contour separete
1636                         {
1637                                 lstManConModTmp.push_back( lstManConModExp[iContourGroup]);
1638                         } else {  // contour AND OR XOR
1639                                 lstManConModTmp = lstManConModExp;
1640                         }
1641
1642                         contourextractdata->SetLstManualContourModel( lstManConModTmp );
1643
1644
1645 //                              for (ii=0 ; ii<sizeLstContourThings ; ii++)
1646 //                              {
1647 //                                      ContourThing **contourthing = lstContourThings[ii];
1648 //                                      lstManConMod.push_back( (*contourthing)->getModel() );
1649 //                              }
1650 //                              contourextractdata->SetLstManualContourModel( lstManConMod );
1651
1652                         contourextractdata->CalculateImageResult(); // with actual Z
1653
1654                 } // for  iContourGroup
1655         } // for z
1656
1657         std::string temp = directory + "/" + namefile + "-Value.mhd";
1658
1659         wxString filename;
1660         filename.Printf(_T(temp.c_str()));
1661         //filename.Printf(_T("%s\\%s-Value.mhd",directory.c_str(),namefile.c_str(),z);
1662         /*filename.Printf(_T("%s"),directory.c_str());
1663         filename.append(_T("\\"));
1664         filename.append(_T("%s"),namefile.c_str());
1665         filename.append(_T("-Value.mhd"));*/
1666
1667 // Image Value
1668         vtkMetaImageWriter *writerValueImage = vtkMetaImageWriter::New( );
1669         writerValueImage->SetInput( contourextractdata->GetVtkImageValueResult() );
1670         writerValueImage->SetFileName( (const char *)filename.mb_str() );
1671         writerValueImage->SetFileDimensionality( 3 );
1672         writerValueImage->Write( );
1673
1674 // Image Mask
1675         //              filename.Printf("%s\\%s-Mask.mhd",directory.c_str(),namefile.c_str(),z);
1676         temp = directory + "/" + namefile + "-Mask.mhd";
1677
1678         filename.Printf(_T(temp.c_str()));
1679
1680         /*filename.Printf(_T("%s"),directory.c_str());
1681         filename.append(_T("\\"));
1682         filename.append(_T("%s"),namefile.c_str());
1683         filename.append(_T("-Mask.mhd"));*/
1684
1685         vtkMetaImageWriter *writerMaskImage = vtkMetaImageWriter::New( );
1686         writerMaskImage->SetInput( contourextractdata->GetVtkImageMaskResult() );
1687         writerMaskImage->SetFileName( (const char *)filename.mb_str() );
1688         writerMaskImage->SetFileDimensionality( 3 );
1689         writerMaskImage->Write( );
1690
1691         interfMainPanel::getInstance()->setStringInfoPanel( _T("") );
1692         delete contourextractdata;
1693 }
1694
1695 void wxContourMainFrame::onChangeInstantInstantPanel(std::string name,int value, int minshow, int maxshow){
1696         if( name.compare( _theViewPanel->getVerticalConceptName() ) == 0){
1697                 _theViewPanel->setActualVertical( value);
1698         }
1699         else if( name.compare( _theViewPanel->getHorizontalConceptName() ) == 0|| _instantPanel->getIfConceptCheckedAt( name, 0 ) )     {
1700                 _theViewPanel->setHorizontalConcept( name, minshow, maxshow, minshow, maxshow, value);
1701         }
1702
1703         changeInstant();
1704 }
1705
1706 void wxContourMainFrame::onSnakePressed(){
1707         std::vector<double> vecX; 
1708         std::vector<double> vecY; 
1709         std::vector<double> vecZ; 
1710         _theViewPanel->GetPointsOfActualContour( &vecX , &vecY , &vecZ );       
1711
1712         if (vecX.size()!=0){
1713                 std::vector<int> tempVector;
1714                 _instantPanel->getInstant( tempVector );
1715                 int i,size=vecZ.size();
1716                 int actualSlice = tempVector[1];
1717                 for ( i=0 ; i<size ; i++ )
1718                 {
1719                         vecZ[i] = actualSlice;
1720                 } // for
1721
1722                 wxDialog* dialog = new wxDialog(this, -1, wxString(_T("Snake")));
1723                 wxPanel* panel = new wxPanel(dialog,-1); 
1724                 wxStaticText* sttext = new wxStaticText(panel, -1, wxString(_T("Panel para snake")));
1725                 dialog->ShowModal();
1726         } // if 
1727
1728 }