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