]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
*** empty log message ***
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsWxGUIEditorGraphic / wxVtkSceneManager.cxx
1 /*=========================================================================                                                                               
2 Program:   bbtk
3 Module:    $RCSfile$
4 Language:  C++
5 Date:      $Date$
6 Version:   $Revision$
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32 *  \file 
33 *  \brief Class bbtk::wxVtkSceneManager . 
34 */
35
36
37 #include "wxVtkSceneManager.h"
38
39 namespace bbtk
40 {
41
42
43         //=========================================================================
44         wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView,int idManager)
45         {
46                 _parent = parent;
47                 _numBoxes=0;
48                 _idManager=idManager;
49                 _baseView=baseView;
50                 _startDragging=false;
51                 _isComplexBox=false;
52
53                 if( _baseView!=NULL )
54                 {
55 //EED02JUIN2010 
56                         printf ("RaC-EED 21-06-2010 %p wxVtkSceneManager::wxVtkSceneManager If you comment this line, the drag and drop functionnality is not initialized.\n" , this );
57                         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);                      
58
59                         configureBaseView();
60                         _worldState=NOTHING_HAPPENS;
61                         registerController(this);
62                         
63                 }
64                 _idConnectionInCreation=-1;
65                 _contLastId=0;
66
67         }
68
69         //=========================================================================
70
71         void wxVtkSceneManager::disconnectDrop()
72         {
73                 printf ("EED %p ~wxVtkSceneManager::disconnectDrop()\n" , this );
74 //EED02JUIN2010         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
75 //      _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget( new tmpClasswxTextDropTarget() );
76         }
77
78         //=========================================================================
79
80         wxVtkSceneManager::~wxVtkSceneManager()
81         {
82                 printf ("EED %p ~wxVtkSceneManager()\n" , this );
83                 disconnectDrop();
84         }
85  
86         //=========================================================================
87
88         void wxVtkSceneManager::configureBaseView()
89         {
90                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
91
92                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
93
94                 // Important to activate the 2D interaction system
95                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
96                 interactorstylebaseview->SetInteractor ( iren );
97                 iren->SetInteractorStyle(interactorstylebaseview);
98                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
99                 
100                 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
101                 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
102                 
103                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
104                 _baseView->GetRenderer()->GradientBackgroundOff();
105                 _baseView->Refresh();
106         }
107
108         //=========================================================================
109
110         int wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
111         {
112                 _worldState = NOTHING_HAPPENS;
113                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
114                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
115
116                 int type = GBLACKBOX;
117
118                 //Create the MVC Objects
119                 
120                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
121                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
122                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
123                 
124                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
125                 
126                 //Prepares the initial model
127                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
128
129                 double xx = x;
130                 double yy = windowHeight-y;
131                 
132                 //z value is not important yet, because it is only used a parallel projection
133                 double zz = 900;
134
135                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
136                 model->setInicPoint(xx,yy,zz);
137                 
138                 _numBoxes++;
139                 std::stringstream stream;
140                 
141                 if(_numBoxes<10)
142                 {
143                         stream << "Box0" << _numBoxes;
144                 }
145                 else
146                 {
147                         stream << "Box" << _numBoxes;
148                 }
149                 std::string arraystring = stream.str();
150
151                 model->setBBTKName(arraystring);
152                 model->setBBTKType(boxType);
153                 model->setBBTKPackage(packageName);
154                                 
155                 model->addObserver(view);
156                 model->addObserver(this);
157
158                 //Iterate and create the input ports
159                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
160                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
161
162                 int i=0;
163                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
164                 {
165                         BlackBoxInputDescriptor *desc = itInput->second;
166                         createGInputPort(GINPUTPORT,i,model,desc);
167                         i++;
168                 }
169
170                 //Iterate and create the output ports
171                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
172                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
173
174                 i=0;
175                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
176                 {
177                         BlackBoxOutputDescriptor *desc = itOutput->second;
178                         createGOutputPort(GOUTPUTPORT,i,model,desc);
179                         i++;
180                 }
181
182
183                 //Associates the view with the correspondent renderer and the  model.
184                 //(NOTE: Refresh is only made by the view)
185                 view->setModel(model);
186                 view->setBaseView(_baseView);
187                 view->initVtkObjects();
188                 
189                 //Associates the controller with the correspondent model and view
190                 controller->setModelAndView(model,view);
191
192                 //Resgiter change to the observers of the actual model
193                 model->notifyObservers(_idManager);
194                 
195                 int newId = addObjectController(controller);
196
197                 return newId;
198
199         }
200
201         //=========================================================================
202
203         int wxVtkSceneManager::createGComplexBoxInputPort(std::string inputName)
204         {
205                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
206                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
207                 
208                 int type = GCOMPLEXINPUTPORT;
209
210                 //Create the MVC Objects
211                 
212                 GComplexBoxPortModel *model = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
213                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
214                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
215                                 
216                 //Prepares the initial model 
217
218                 double xx = 5;
219                 double yy = windowHeight-5;
220                 
221                 //z value is not important yet, because it is only used a parallel projection
222                 double zz = 900;
223
224                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
225                 model->setInicPoint(xx,yy,zz);
226                                 
227                 model->setBBTKName(inputName);
228                 model->setBBTKType("ComplexInputPort");
229                 model->setComplexPortType(type);
230                                 
231                 model->addObserver(view);
232                 model->addObserver(this);
233                 
234                 //create the output port                
235                 GPortController* portController = createGPort(GOUTPUTPORT,inputName,"ComplexInputPort",0,model);
236                 model->addOutputPort((GPortModel*)portController->getModel());
237                 
238                 //Associates the view with the correspondent renderer and the  model.
239                 //(NOTE: Refresh is only made by the view)
240                 view->setModel(model);
241                 view->setBaseView(_baseView);
242                 view->initVtkObjects();
243                 
244                 //Associates the controller with the correspondent model and view
245                 controller->setModelAndView(model,view);
246
247                 //Resgiter change to the observers of the actual model
248                 model->notifyObservers(_idManager);
249                 
250                 int newId = addObjectController(controller);
251                 return newId;
252         }
253
254         //=========================================================================
255         
256         int wxVtkSceneManager::createGComplexBoxOutputPort(std::string outputName)
257         {
258                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
259                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
260
261                 int type = GCOMPLEXOUTPUTPORT;
262
263                 //Create the MVC Objects
264                 
265                 GComplexBoxPortModel *model = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
266                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
267                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
268                                 
269                 //Prepares the initial model 
270
271                 double xx = 5;
272                 double yy = windowHeight-5;
273                 
274                 //z value is not important yet, because it is only used a parallel projection
275                 double zz = 900;
276
277                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
278                 model->setInicPoint(xx,yy,zz);
279                                 
280                 model->setBBTKName(outputName);
281                 model->setBBTKType("ComplexOutputPort");
282                 model->setComplexPortType(type);
283                                 
284                 model->addObserver(view);
285                 model->addObserver(this);
286                 
287                 //create the output port                
288                 GPortController* portController = createGPort(GINPUTPORT,outputName,"ComplexInputPort",0,model);
289                 model->addInputPort((GPortModel*)portController->getModel());
290                 
291                 //Associates the view with the correspondent renderer and the  model.
292                 //(NOTE: Refresh is only made by the view)
293                 view->setModel(model);
294                 view->setBaseView(_baseView);
295                 view->initVtkObjects();
296                 
297                 //Associates the controller with the correspondent model and view
298                 controller->setModelAndView(model,view);
299
300                 //Resgiter change to the observers of the actual model
301                 model->notifyObservers(_idManager);
302                 
303                 int newId = addObjectController(controller);
304                 return newId;
305         }
306
307         //=========================================================================
308
309         int wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxInputDescriptor *desc)
310         {
311                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
312                 blackBox->addInputPort((GPortModel*)portController->getModel());
313                 return portController->getId();
314         }
315
316         //=========================================================================
317
318         int wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
319         {
320                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
321                 blackBox->addOutputPort((GPortModel*)portController->getModel());
322                 return portController->getId();
323         }
324
325         //=========================================================================
326
327         GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBoxModel *blackBox)
328         {
329                 int type = GPORT;
330
331                 //Create the MVC Objects
332                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
333                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
334                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
335
336                 model->registerInBox(blackBox,portType, posInBox);
337                 
338                 model->setBBTKType(bbtkType);
339                 model->setBBTKName(bbtkName);
340
341                 model->addObserver(view);
342                 model->addObserver(this);
343
344                 //Associates the view with the correspondent renderer and the  model.
345                 //(NOTE: Refresh is only made by the view)
346                 view->setModel(model);
347                 view->setBaseView(_baseView);
348                 view->initVtkObjects();
349                 
350                 //Associates the controller with the correspondent model and view
351                 controller->setModelAndView(model,view);
352
353                 model->notifyObservers(_idManager);
354
355                 int newId = addObjectController(controller);
356
357                 return (GPortController*)controller;
358         }
359
360         //=========================================================================
361
362         int wxVtkSceneManager::createGConnector(GPortModel* startPort)
363         {
364                 int type = GCONNECTOR;
365
366                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
367                 manualConnectorContourView* manContourView      = new manualConnectorContourView();
368                 manualContourModel* manContourModel     = new manualContourModel();
369
370                 GConnectorController* connectorcontroller = new GConnectorController();                         
371                 GConnectorModel* connectorModel = new GConnectorModel();
372                 vtkGConnectorView* connectorView = new vtkGConnectorView();
373                 connectorModel->setGObjectType(type);
374
375                 manContourModel->SetCloseContour(false);
376                 connectorModel->setStartPort(startPort);
377
378                 manContourView->SetModel( manContourModel );
379                 manContourView->SetWxVtkBaseView( _baseView );
380                 manContourView->SetRange( 0.5 );
381                 manContourView->SetZ( 900 );
382
383                 manContourView->SetColorNormalContour(0, 0, 1);
384                 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
385                 manContourView->SetColorSelectContour(1, 0.8, 0);
386                 manContourView->SetWidthLine(1);
387                 manContourView->SetShowText(false);
388
389                 manContourControl->SetModelView( manContourModel , manContourView );
390                 
391                 manContourControl->CreateNewManualContour();
392
393                 manContourView->RefreshContour();
394
395
396                 double x,y,z;
397                 connectorModel->getInicPoint(x,y,z);
398
399                 manContourControl->SetState(1);
400                 manContourModel->SetCloseContour(false);
401                 
402                 manContourModel->AddPoint(x,y,z);
403                 manContourView->AddPoint();
404
405                 manContourModel->AddPoint(x,y,z);
406                 manContourView->AddPoint();
407
408                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
409                 manContourControl->_bakIdPoint=bak;
410                 manContourView->Refresh();
411
412                 manContourControl->SetMoving( false );
413
414                 connectorcontroller->setModelAndView(connectorModel,connectorView);
415
416                 int newId = addObjectController(connectorcontroller);
417
418                 connectorcontroller->setManualContourController(manContourControl);             
419                 connectorModel->setManualContourModel(manContourModel);
420                 connectorView->setManualContourView(manContourView);
421                 connectorView->setModel(connectorModel);
422                 connectorView->setBaseView(_baseView);
423                 
424
425                 connectorModel->addObserver(connectorView);
426                 connectorModel->addObserver(this);
427
428                 return newId;
429         }
430
431         //=========================================================================
432
433         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
434         {
435                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
436                 baseViewControlManager->AddInteractorStyleMaracas( param );
437         }
438
439         //=========================================================================
440
441         void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
442         {
443                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
444                 baseViewControlManager->RemoveInteractorStyleMaracas( param );
445         }
446
447         //=========================================================================
448
449         vtkRenderer* wxVtkSceneManager::getRenderer()
450         {
451                 return _baseView->GetRenderer();
452         }
453
454         //=========================================================================
455
456         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
457         {
458                 return _baseView->GetRenWin();
459         }
460         //=========================================================================
461
462                 
463         void wxVtkSceneManager::update(int idController,int command)
464         {
465                 if(command != NO_COMMAND)
466                 {
467                         if(command == ADD_TO_SELECTED)
468                         {
469                                 GObjectController* cont = _controllers[idController];
470
471                                 bool foundID=false;
472                                 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
473                                 {
474                                         int id = _selectedObjects[i];
475                                         if(id==idController)
476                                         {
477                                                 foundID = true;
478                                         }
479                                 }
480                                 if(!foundID)
481                                 {
482                                         int id = idController;
483                                         _selectedObjects.push_back(id);
484                                 }
485
486                         }
487                         else if(command == INIT_CREATION_CONTOUR)
488                         {       
489                                 _worldState = INIT_CREATION_CONTOUR;
490                                 GObjectController* cont = _controllers[idController];
491                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
492                                 
493
494                                 // The last one is the controller of the connector
495                                 std::map<int, GObjectController*>::iterator it2;
496
497                                 for(it2 = _controllers.begin(); it2 != _controllers.end(); ++it2)
498                                 {
499                                         GObjectController *cont = it2->second;
500                                         if(cont->getGObjectType() == GPORT )
501                                         {
502                                                 GPortModel* port = (GPortModel*)cont->getModel();
503                                                 if(port->getPortType()==GINPUTPORT)
504                                                 {
505                                                         cont->SetActive(true);
506                                                 }
507                                                 else
508                                                 {
509                                                         cont->getView()->setState(NOTHING_HAPPENS);
510                                                         cont->getModel()->notifyObservers(_idManager);
511                                                         cont->SetActive(false);
512                                                 }
513                                         }
514                                         else
515                                         {
516                                                 cont->getView()->setState(NOTHING_HAPPENS);
517                                                 cont->getModel()->notifyObservers(_idManager);
518                                                 cont->SetActive(false);
519                                         }                               
520                                 }
521
522                                 _selectedObjects.clear();
523
524                                 _idConnectionInCreation=createGConnector(startOutputPort);
525
526                         }
527                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
528                         {                               
529                                 _worldState = NOTHING_HAPPENS;
530                                 //int id = _controllers.size()-1;
531                                 GObjectController* cont = _controllers[_idConnectionInCreation];                        
532                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
533
534                                 GObjectController* finPort = _controllers[idController];
535                                 if(finPort->getGObjectType() == GPORT)
536                                 {
537                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
538                                         modelContour->setEndPort(modelPort);
539                                 }                       
540
541                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
542                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
543                                 connView->Refresh();
544
545                                 std::map<int, GObjectController*>::iterator it2;
546
547                                 for(it2 = _controllers.begin(); it2 != _controllers.end(); ++it2)
548                                 {
549                                         GObjectController *cont = it2->second;
550                                         if(cont->getView()!=NULL)
551                                         {
552                                                 cont->getView()->setState(NOTHING_HAPPENS);
553                                                 cont->getModel()->notifyObservers(_idManager);
554                                         }
555                                         cont->SetActive(true);                                                          
556                                 }
557                         }
558                         
559                 }
560         }
561
562         //=========================================================================
563
564         bool wxVtkSceneManager::OnMouseMove()
565         {
566                 int X,Y;
567                 wxVTKRenderWindowInteractor *wxVTKiren;
568                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
569                 wxVTKiren->GetEventPosition(X,Y);
570         
571                 
572                 if(_worldState == DRAG_OBJECTS)
573                 {                       
574                         for (int i=0; i<_selectedObjects.size(); i++)
575                         {
576                                 int id = _selectedObjects[i];
577                                 GObjectController* cont = _controllers[id];
578                                 if(_startDragging)
579                                 {
580                                         cont->getView()->setStartDragging(true);
581                                 }
582                                 cont->getView()->moveObject(X,Y);
583                                 cont->getView()->setState(DRAG);
584                                 cont->getModel()->notifyObservers(_idManager);
585                         }
586
587                         std::map<int, GObjectController*>::iterator it;
588
589                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
590                         {
591                                 GObjectController *desc = it->second;
592                                 if(desc->getGObjectType()==GCONNECTOR)
593                                 {
594                                         vtkGConnectorView* vconn = (vtkGConnectorView*)desc->getView();
595                                         vconn->updateStartEndPoints();
596                                 }
597                         }
598                         
599                         _startDragging=false;
600
601                 }
602                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
603                 {
604                         std::map<int, GObjectController*>::iterator it;
605
606                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
607                         {
608                                 GObjectController *desc = it->second;
609                                 int type = desc->getGObjectType();
610                                 int state = desc->getView()->getState();
611                                 
612                                 if(state == HIGHLIGHTED){
613
614                                         updateStatusBar(desc->getStatusText());
615                                         if(type==GBLACKBOX)
616                                         {
617                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
618                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
619                                         }
620                                 }
621                         }
622                 }
623                 
624                 return true;
625         }
626
627         //=========================================================================
628         
629         bool wxVtkSceneManager::OnLeftButtonDown()
630         {
631                 if(_worldState==INIT_CREATION_CONTOUR)
632                 {
633                         bool isOverPort=false;
634                         std::map<int, GObjectController*>::iterator it;
635                         for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
636                         {
637                                 GObjectController *desc = it->second;
638                                 if(desc->getGObjectType()==GPORT)
639                                 {
640                                         GPortModel* portmod=(GPortModel*)desc->getModel();
641                                         vtkGObjectView* portView=desc->getView();
642                                         if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
643                                         {
644                                                 isOverPort=true;
645                                         }
646                                 }
647                         }
648                         
649                         if(isOverPort==false)
650                         {
651                                 _worldState=NOTHING_HAPPENS;
652                                 //int lastId = _controllers.size()-1;
653                                 GConnectorController *connector = (GConnectorController*)_controllers[_controllers.size()-1];
654                                 connector->removeFromScene();
655                                 unregisterController(connector);
656                                 _controllers.erase(_controllers.size()-1);                      
657
658                                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
659                                 {
660                                         GObjectController *desc = it->second;
661                                         desc->SetActive(true);
662                                         desc->getView()->setState(NOTHING_HAPPENS);
663                                         desc->getModel()->notifyObservers(_idManager);
664                                 }
665                         }
666                 }
667                 
668                 if(_selectedObjects.size()!=0)
669                 {
670                         _worldState = DRAG_OBJECTS;
671                         _startDragging = true;
672
673                         for (int i = 0; i < _selectedObjects.size(); i++)
674                         {
675                                 int id = _selectedObjects[i];
676                                 GObjectController* cont = _controllers[id];
677                                 cont->getView()->setState(DRAG);
678                                 cont->getModel()->notifyObservers(_idManager);
679                         }
680                 }
681                 
682                 
683                 
684                 return true;
685         }
686
687         //=========================================================================
688         
689         bool wxVtkSceneManager::OnLeftButtonUp()
690         {
691                 if(_worldState == DRAG_OBJECTS)
692                 {
693                         _worldState = NOTHING_HAPPENS;
694
695                         for (int i = 0; i < _selectedObjects.size(); i++)
696                         {
697                                 int id = _selectedObjects[i];
698                                 GObjectController* cont = _controllers[id];
699                                 cont->getView()->setState(SELECTED);
700                                 cont->getModel()->notifyObservers(_idManager);
701                         }
702                 }
703                 return true;
704         }
705
706         //=========================================================================
707
708         bool wxVtkSceneManager::OnRightButtonUp()
709         {
710                 if(_worldState==INIT_CREATION_CONTOUR)
711                 {
712                         _worldState=NOTHING_HAPPENS;
713                         //int lastId = _controllers.size()-1;
714                         GConnectorController *connector = (GConnectorController*)_controllers[_idConnectionInCreation];
715                         connector->removeFromScene();
716                         unregisterController(connector);
717                         _controllers.erase(_idConnectionInCreation);                    
718
719                         std::map<int, GObjectController*>::iterator it;
720                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
721                         {
722                                 GObjectController *desc = it->second;
723                                 desc->SetActive(true);
724                                 desc->getView()->setState(NOTHING_HAPPENS);
725                                 desc->getModel()->notifyObservers(_idManager);
726                         }
727                 }
728
729                         
730                 for (int i = 0; i < _selectedObjects.size(); i++)
731                 {
732                         int id = _selectedObjects[i];
733                         GObjectController* cont = _controllers[id];
734                         cont->SetActive(true);
735                         cont->getView()->setState(NOTHING_HAPPENS);
736                         cont->getModel()->notifyObservers(_idManager);
737                 }
738
739                 _selectedObjects.clear();
740
741                 return true;
742         }
743
744         //=========================================================================
745
746         bool wxVtkSceneManager::OnLeftDClick()
747         {
748                 int X,Y;
749                 wxVTKRenderWindowInteractor *wxVTKiren;
750                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
751                 wxVTKiren->GetEventPosition(X,Y);
752
753                 std::map<int, GObjectController*>::iterator it;
754
755                 bool clickOnObject = false;
756
757                 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
758                 {
759                         GObjectController *cont = it->second;
760                         int type = cont->getGObjectType();
761                         
762                         if(cont->getView()->isPointInside(X,Y))
763                         {
764                                 if(type==GBLACKBOX)
765                                 {
766                                         for (int i=0; i<_selectedObjects.size(); i++)
767                                         {
768                                                 int id = _selectedObjects[i];
769                                                 GObjectController* control = _controllers[id];
770                                                 control->getView()->setState(NOTHING_HAPPENS);
771                                         }
772                                         _selectedObjects.clear();
773
774                                         GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
775                                         _parent->editBlackBox(bbmodel);
776                                 }
777                                 clickOnObject = true;                   
778                         }
779                 }
780
781                 if(clickOnObject==false)
782                 {
783                         //_parent->editDiagramParameters(this);
784                 }
785
786                 return true;
787         }
788         
789         //=========================================================================
790
791         bool wxVtkSceneManager::OnChar()
792         {       
793                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
794                 
795                 // KeyCode 127 : Delete Key
796                 // KeyCode 8 : Backspace Key
797                 if(keyCode == 8 || keyCode == 127)
798                 {
799                         if(_selectedObjects.size()>0)
800                         {
801                                 for(int i=0;i<_selectedObjects.size();i++)
802                                 {
803                                         int id = _selectedObjects[i];
804                                         deleteObject(id);
805                                 }
806                                 _selectedObjects.clear();
807                         }
808                 }
809
810                 return true;
811         }
812
813         //=========================================================================
814
815         void wxVtkSceneManager::deleteObject(int id)
816         {
817                 GObjectController *control = _controllers[id];
818                 std::vector<int> controllersToRemove;
819
820                 if(control->getGObjectType()==GBLACKBOX || control->getGObjectType()==GCOMPLEXINPUTPORT || control->getGObjectType()==GCOMPLEXOUTPUTPORT)
821                 {
822                         GBoxModel *bbmod = (GBoxModel*)control->getModel();
823                         std::vector<GPortModel*> inputs = bbmod->getInputPorts();
824                         
825                         bool boxConnected = false;
826
827                         // Add box input controllers to be removed
828                         for(int i = 0;i<inputs.size();i++)
829                         {
830                                 controllersToRemove.push_back(inputs[i]->getObjectId());
831                                 if(inputs[i]->isConnected())
832                                 {
833                                         boxConnected = true;
834                                 }
835                         }
836
837                         std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
838
839                         // Add box output controllers to be removed
840                         for(int i = 0;i<outputs.size();i++)
841                         {
842                                 controllersToRemove.push_back(outputs[i]->getObjectId());
843                                 if(outputs[i]->isConnected())
844                                 {
845                                         boxConnected = true;
846                                 }
847                         }
848
849                         // Add connection controllers to be removed
850                         std::map<int, GObjectController*>::iterator it;
851                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
852                         {
853                                 GObjectController *cont = it->second;
854                                 int type = cont->getGObjectType();
855                                 if(type==GCONNECTOR)
856                                 {
857                                         GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
858                                         if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
859                                         {
860                                                 controllersToRemove.push_back(conMod->getObjectId());
861                                         }
862                                         if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
863                                         {
864                                                 controllersToRemove.push_back(conMod->getObjectId());
865                                         }
866                                 }
867                         }
868
869                         // Add box controller to be removed
870                         controllersToRemove.push_back(bbmod->getObjectId());
871                 }
872                 else if(control->getGObjectType()==GCONNECTOR)
873                 {                       
874                         GConnectorModel *conMod = (GConnectorModel*)control->getModel();
875                         controllersToRemove.push_back(conMod->getObjectId());
876                 }
877
878                 for(int i = 0;i<controllersToRemove.size();i++)
879                 {
880                         int id = controllersToRemove[i];
881                         GObjectController *cont = _controllers[id];
882                         if(cont!=NULL)
883                         {
884                                 cont->removeFromScene();
885                                 unregisterController((InteractorStyleMaracas*)cont);                    
886                                 _controllers.erase(id);
887                         }
888                 }
889
890
891         }
892
893         //=========================================================================
894
895         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
896         {
897                 _parent->displayBlackBoxInfo(packageName,boxName);
898         }
899
900         //=========================================================================
901
902         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
903         {
904                 _parent->updateStatusBar(textStatus);
905         }
906
907         //=========================================================================
908
909         std::string wxVtkSceneManager::getDiagramBBS()
910         {
911                 bool existsExec=false;
912
913                 std::vector<std::string> packages;
914                 std::vector<int> boxes;
915                 std::vector<int> connections;
916                 std::vector<int> execBoxes;
917
918                 std::map<int, GObjectController*>::iterator it;
919
920                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
921                 {
922                         GObjectController *desc = it->second;
923                         int type = desc->getGObjectType();
924
925                         if(type==GBLACKBOX)
926                         {
927                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
928                                 
929                                 std::string pkg = mod->getBBTKPackage();
930                                 bool existsPkg = false;
931                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
932                                 {
933                                         if(packages[t]==pkg)
934                                         {
935                                                 existsPkg=true;
936                                         }
937                                 }
938                                 if(!existsPkg)
939                                 {
940                                         packages.push_back(pkg);
941                                 }
942
943
944                                 boxes.push_back(it->first);
945                                 if(mod->isExecutable())
946                                 {
947                                         execBoxes.push_back(it->first);
948                                         existsExec=true;
949                                 }
950                         }
951                         else if(type==GCONNECTOR)
952                         {
953                                 connections.push_back(it->first);
954                         }
955                 }
956
957                 std::string script = "";
958                 script+="# BBTK GEditor Script\n";
959                 script+="# ----------------------\n";
960                 if(existsExec)
961                 {
962                         script+="include std\n"; // EED
963                         script+="include itkvtk\n"; // EED
964                         int i;
965                         for(i = 0; i<packages.size();i++)
966                         {
967                                 script+="include ";
968                                 script+=packages[i];
969                                 script+="\n";
970                         }
971
972                         // script+="include std\n"; // JPR
973
974                         for(i = 0; i<boxes.size();i++)
975                         {
976                                 script+="new ";
977                                 int id = boxes[i];
978                                 GObjectController *control = _controllers[id];
979                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
980
981                                 script+=model->getBBTKType();
982                                 script+=" ";
983                                 script+=model->getBBTKName();
984                                 script+="\n";
985
986                                 std::vector<GPortModel*> inputs = model->getInputPorts();
987                                 for(int j = 0; j<inputs.size();j++)
988                                 {
989                                         GPortModel* inputPort = inputs[j];
990                                         if(inputPort->isValueSet())
991                                         {
992                                                 script+="set ";
993                                                 script+=model->getBBTKName();
994                                                 script+=".";
995                                                 script+=inputPort->getBBTKName();
996                                                 script+=" ";
997                                                 script+=inputPort->getValue();
998                                                 script+="\n";
999                                         }
1000                                 }
1001
1002                         }
1003
1004                         for(i = 0; i<connections.size();i++)
1005                         {
1006                                 script+="connect ";
1007                                 int id = connections[i];
1008                                 GObjectController *control = _controllers[id];
1009                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
1010
1011                                 //Start Connection info
1012                                 GPortModel *start = model->getStartPort();
1013                                 script+=start->getParentBox()->getBBTKName();
1014                                 script+=".";
1015                                 script+=start->getBBTKName();
1016
1017                                 script+=" ";
1018
1019                                 //End Connection info
1020                                 GPortModel *end = model->getEndPort();
1021                                 script+=end->getParentBox()->getBBTKName();
1022                                 script+=".";
1023                                 script+=end->getBBTKName();
1024
1025                                 script+="\n";
1026                         }
1027
1028                         for(i = 0; i<execBoxes.size();i++)
1029                         {
1030                                 script+="exec ";
1031                                 int id = execBoxes[i];
1032                                 GObjectController *control = _controllers[id];
1033                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1034
1035                                 script+=model->getBBTKName();
1036                                 script+="\n";
1037                         }
1038
1039                 }
1040
1041                 return script;
1042         }
1043
1044         //=========================================================================
1045
1046         std::string wxVtkSceneManager::saveComplexBoxBBS(std::string cbName,std::string cbAuthor,std::string cbCategory,std::string cbDescription)
1047         {
1048
1049                 std::vector<std::string> packages;
1050                 std::vector<int> boxes;
1051                 std::vector<int> connections;
1052                 std::vector<int> execBoxes;
1053
1054                 std::map<int, GObjectController*>::iterator it;
1055
1056                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1057                 {
1058                         GObjectController *desc = it->second;
1059                         int type = desc->getGObjectType();
1060
1061                         if(type==GBLACKBOX)
1062                         {
1063                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
1064                                 
1065                                 std::string pkg = mod->getBBTKPackage();
1066                                 bool existsPkg = false;
1067                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
1068                                 {
1069                                         if(packages[t]==pkg)
1070                                         {
1071                                                 existsPkg=true;
1072                                         }
1073                                 }
1074                                 if(!existsPkg)
1075                                 {
1076                                         packages.push_back(pkg);
1077                                 }
1078
1079
1080                                 boxes.push_back(it->first);
1081                                 if(mod->isExecutable())
1082                                 {
1083                                         execBoxes.push_back(it->first);
1084                                 }
1085                         }
1086                         else if(type==GCONNECTOR)
1087                         {
1088                                 connections.push_back(it->first);
1089                         }
1090                 }
1091
1092                 std::string script = "";
1093
1094                 script+="include std\n";    // EED
1095                 script+="include itkvtk\n"; // EED
1096                 int i;
1097                 for(i = 0; i<packages.size();i++)
1098                 {
1099                         script+="include ";
1100                         script+=packages[i];
1101                         script+="\n";
1102                 }
1103
1104                 // Definition of a complex box
1105                 script+="define ";
1106                 script+=cbName;
1107                 script+="\n";
1108
1109                 script+="author \" ";
1110                 script+=cbAuthor;
1111                 script+="\"\n";
1112
1113                 script+="description \" ";
1114                 script+=cbDescription;
1115                 script+="\"\n";
1116
1117                 // Create boxes
1118                 for(i = 0; i<boxes.size();i++)
1119                 {
1120                         script+="new ";
1121                         int id = boxes[i];
1122                         GObjectController *control = _controllers[id];
1123                         GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1124
1125                         script+=model->getBBTKType();
1126                         script+=" ";
1127                         script+=model->getBBTKName();
1128                         script+="\n";
1129
1130                                 std::vector<GPortModel*> inputs = model->getInputPorts();
1131                                 for(int j = 0; j<inputs.size();j++)
1132                                 {
1133                                         GPortModel* inputPort = inputs[j];
1134                                         if(inputPort->isValueSet())
1135                                         {
1136                                                 script+="set ";
1137                                                 script+=model->getBBTKName();
1138                                                 script+=".";
1139                                                 script+=inputPort->getBBTKName();
1140                                                 script+=" ";
1141                                                 script+=inputPort->getValue();
1142                                                 script+="\n";
1143                                         }
1144                                 }
1145
1146                         }
1147
1148                         // Create connections in the script. If the connection is made with a complex port, it is created the input or output
1149
1150                         std::string complexInputs="";
1151                         std::string complexOutputs="";
1152
1153                         for(i = 0; i<connections.size();i++)
1154                         {
1155                                 int id = connections[i];
1156                                 GObjectController *control = _controllers[id];
1157                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
1158
1159                                 //Connection info
1160                                 GPortModel *start = model->getStartPort();
1161                                 GBoxModel *startBox =start->getParentBox();
1162
1163                                 GPortModel *end = model->getEndPort();
1164                                 GBoxModel *endBox =end->getParentBox();
1165
1166                                 if(startBox->getGObjectType()==GCOMPLEXINPUTPORT)
1167                                 {
1168                                         complexInputs+="input ";
1169                                         complexInputs+=startBox->getBBTKName();
1170
1171                                         complexInputs+=" ";
1172                                         complexInputs+=endBox->getBBTKName();
1173                                         complexInputs+=".";
1174                                         complexInputs+=end->getBBTKName();
1175
1176                                         complexInputs+=" ";
1177                                         complexInputs+="\" \"";
1178
1179                                         complexInputs+="\n";
1180                                 }
1181                                 else if(endBox->getGObjectType()==GCOMPLEXOUTPUTPORT)
1182                                 {
1183                                         complexOutputs+="output ";
1184                                         complexOutputs+=endBox->getBBTKName();
1185
1186                                         complexOutputs+=" ";
1187                                         complexOutputs+=startBox->getBBTKName();
1188                                         complexOutputs+=".";
1189                                         complexOutputs+=start->getBBTKName();
1190
1191                                         complexOutputs+=" ";
1192                                         complexOutputs+="\" \"";
1193
1194                                         complexOutputs+="\n";
1195                                 }
1196                                 else
1197                                 {
1198                                         script+="connect ";
1199                                         script+=startBox->getBBTKName();
1200                                         script+=".";
1201                                         script+=start->getBBTKName();
1202
1203                                         script+=" ";
1204
1205                                         //End Connection info
1206                                         script+=endBox->getBBTKName();
1207                                         script+=".";
1208                                         script+=end->getBBTKName();
1209
1210                                         script+="\n";
1211                                 }
1212                         }
1213
1214                         for(i = 0; i<execBoxes.size();i++)
1215                         {
1216                                 script+="exec ";
1217                                 int id = execBoxes[i];
1218                                 GObjectController *control = _controllers[id];
1219                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1220
1221                                 script+=model->getBBTKName();
1222                                 script+="\n";
1223                         }
1224
1225                         script+="\n";
1226                         script+="# Complex input ports\n";
1227                         script+=complexInputs;
1228
1229                         script+="\n";
1230                         script+="# Complex output ports\n";
1231                         script+=complexOutputs;
1232
1233                         script+="\n";
1234                         script+="endefine";
1235                         script+="\n";           
1236
1237                 return script;
1238         }
1239
1240         //=========================================================================
1241
1242         void wxVtkSceneManager::deleteAllBoxes()
1243         {               
1244                 std::map<int, GObjectController*>::iterator it;
1245                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1246                 {
1247                         GObjectController *cont = it->second;           
1248                         cont->removeFromScene();
1249                         unregisterController((InteractorStyleMaracas*)cont);
1250                 }
1251                 _selectedObjects.clear();
1252                 _controllers.clear();   
1253                 refreshScene();
1254         }
1255
1256         //=========================================================================
1257
1258         void wxVtkSceneManager::refreshScene()
1259         {
1260                 _baseView->RefreshView();
1261         }
1262
1263         //=========================================================================
1264         
1265         void wxVtkSceneManager::centerView()
1266         {
1267                 double temp[3];
1268                 _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1269                 _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0,0,temp[2]);
1270                 _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1271                 _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0,0,temp[2]);
1272                 _baseView->RefreshView();
1273
1274         }
1275
1276         //=========================================================================
1277
1278         void wxVtkSceneManager::saveDiagram(std::string &content)
1279         {
1280                 char buffer [50];
1281
1282                 //Print info IF COMPLEX BOX
1283                 content+="COMPLEXBOX:";
1284                 if(_isComplexBox)
1285                 {
1286                         content+="TRUE\n";
1287
1288                         //Print info complex input ports
1289                         std::vector<int> inputs = getComplexInputPorts();
1290                         int insize = inputs.size();
1291                         content+="COMPLEXINPUTS:";
1292                         sprintf (buffer, "%d", insize);
1293                         content+=buffer;
1294                         content+="\n";
1295                         
1296                         for(int i = 0;i<insize;i++)
1297                         {
1298                                 int id = inputs[i];
1299                                 GObjectController *cont = _controllers[id];
1300                                 cont->getModel()->save(content);
1301                         }
1302
1303                         //Print info complex output ports
1304                         std::vector<int> outputs = getComplexOutputPorts();
1305                         int outsize = outputs.size();
1306                         content+="COMPLEXOUTPUTS:";
1307                         sprintf (buffer, "%d", outsize);
1308                         content+=buffer;
1309                         content+="\n";
1310                         
1311                         for(int i = 0;i<outsize;i++)
1312                         {
1313                                 int id = outputs[i];
1314                                 GObjectController *cont = _controllers[id];
1315                                 cont->getModel()->save(content);
1316                         }
1317                 } // _isComplexBox
1318                 else
1319                 {
1320                         content+="FALSE\n";
1321                 }
1322
1323                 //Print boxes
1324                 std::vector<int> boxes = getBlackBoxes();
1325                 int bsize = boxes.size();
1326                 content+="BOXES:";
1327                 sprintf (buffer, "%d", bsize);
1328                 content+=buffer;
1329                 content+="\n";
1330                 
1331                 for(int i = 0;i<bsize;i++)
1332                 {
1333                         int id = boxes[i];
1334                         GObjectController *cont = _controllers[id];
1335                         cont->getModel()->save(content);
1336                 }
1337
1338                 //Print connections
1339                 std::vector<int> connections = getConnections();
1340                 int csize = connections.size();
1341                 content+="CONNECTIONS:";
1342                 sprintf (buffer, "%d", csize);
1343                 content+=buffer;
1344                 content+="\n";
1345                 
1346                 for(int i = 0;i<csize;i++)
1347                 {
1348                         int id = connections[i];
1349                         GObjectController *cont = _controllers[id];
1350                         cont->getModel()->save(content);
1351                 }
1352
1353         }
1354
1355         //=========================================================================
1356
1357         void wxVtkSceneManager::loadDiagram(ifstream &inputStream)
1358         {
1359
1360                 std::string line="";    
1361                 char delims[] = ":";
1362                 char *result = NULL;
1363         getline(inputStream,line);
1364
1365                 bool start = false;
1366                 while ( !inputStream.eof() ) 
1367                 {
1368                         if(line=="" || line[0]=='#')
1369                         {
1370                                 getline(inputStream,line);
1371                         }
1372                         else if(line=="APP_START")
1373                         {
1374                                 start = true;
1375                                 break;
1376                         }
1377                 }
1378
1379                 if(start)
1380                 {
1381
1382                         //----------
1383                         getline(inputStream,line);//COMPLEX_BOX:TRUE|FALSE
1384                         char complex[30];
1385                         strcpy( complex, line.c_str() );
1386                         result = strtok( complex, delims );
1387                         result = strtok( NULL, delims );
1388                         std::string isComplexBox(result);
1389
1390                         if(isComplexBox=="TRUE")
1391                         {
1392                                 _isComplexBox=true;
1393
1394                                 //-----------------------
1395                                 //- COMPLEX INPUT PORTS
1396                                 //-----------------------
1397                                 getline(inputStream,line);//COMPLEXINPUTS:num
1398                                 char inputs[30];
1399                                 strcpy( inputs, line.c_str() );
1400                                 result = strtok( inputs, delims );
1401                                 result = strtok( NULL, delims );
1402                                                 
1403                                 int numInputs;
1404                                 std::istringstream inps(result);
1405                                 inps >> numInputs;
1406                                 
1407                                 for(int i = 0;i<numInputs;i++)
1408                                 {
1409                                         //----------
1410                                         getline(inputStream,line);//COMPLEX_PORT
1411                                         getline(inputStream,line);//name
1412                                         std::string inputPortName(line);
1413
1414                                         //----------
1415                                         getline(inputStream,line);//xInic:yInic:zInic
1416                                         char coord[80];
1417                                         strcpy( coord, line.c_str() );
1418                                         result = strtok( coord, delims );//xInic
1419                                         std::string xInic(result);
1420                                         result = strtok( NULL, delims );//yInic
1421                                         std::string yInic(result);
1422                                         result = strtok( NULL, delims );//zInic
1423                                         std::string zInic(result);
1424
1425                                         double xIn, yIn, zIn;
1426                                         std::istringstream xSt(xInic);
1427                                         xSt >> xIn;
1428                                         std::istringstream ySt(yInic);
1429                                         ySt >> yIn;
1430                                         std::istringstream zSt(zInic);
1431                                         zSt >> zIn;
1432
1433                                         getline(inputStream,line);//FIN_COMPLEX_PORT
1434
1435                                         int idInputPort = createGComplexBoxInputPort(inputPortName);
1436                                         GObjectController *cont = _controllers[idInputPort];
1437                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
1438                                         cbmod->setInicPoint(xIn,yIn,zIn);
1439                                         cbmod->notifyObservers(_idManager);
1440                                 } // for input complex box
1441
1442
1443                                 //-----------------------
1444                                 //- COMPLEX OUTPUT PORTS
1445                                 //-----------------------
1446
1447                                 getline(inputStream,line);//COMPLEXOUTPUTS:num
1448                                 char outputs[30];
1449                                 strcpy( outputs, line.c_str() );
1450                                 result = strtok( outputs, delims );
1451                                 result = strtok( NULL, delims );
1452                                                 
1453                                 int numOutputs;
1454                                 std::istringstream outps(result);
1455                                 outps >> numOutputs;
1456                                 
1457                                 for(int i = 0;i<numOutputs;i++)
1458                                 {
1459                                         //----------
1460                                         getline(inputStream,line);//COMPLEX_PORT
1461                                         getline(inputStream,line);//name
1462                                         std::string outputPortName(line);
1463
1464                                         //----------
1465                                         getline(inputStream,line);//xInic:yInic:zInic
1466                                         char coord[80];
1467                                         strcpy( coord, line.c_str() );
1468                                         result = strtok( coord, delims );//xInic
1469                                         std::string xInic(result);
1470                                         result = strtok( NULL, delims );//yInic
1471                                         std::string yInic(result);
1472                                         result = strtok( NULL, delims );//zInic
1473                                         std::string zInic(result);
1474
1475                                         double xIn, yIn, zIn;
1476                                         std::istringstream xSt(xInic);
1477                                         xSt >> xIn;
1478                                         std::istringstream ySt(yInic);
1479                                         ySt >> yIn;
1480                                         std::istringstream zSt(zInic);
1481                                         zSt >> zIn;
1482
1483                                         getline(inputStream,line);//FIN_COMPLEX_PORT
1484
1485                                         int idOutputPort = createGComplexBoxOutputPort(outputPortName);
1486                                         GObjectController *cont = _controllers[idOutputPort];
1487                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
1488                                         cbmod->setInicPoint(xIn,yIn,zIn);
1489                                         cbmod->notifyObservers(_idManager);
1490                                 } // for output complex box
1491
1492                         } // complex box
1493
1494                         //----------
1495                         getline(inputStream,line);//BOXES:num
1496                         char boxes[9];
1497                         strcpy( boxes, line.c_str() );
1498                         result = strtok( boxes, delims );
1499                         result = strtok( NULL, delims );
1500                                         
1501                         int numBoxes;
1502                         std::istringstream is(result);
1503                         is >> numBoxes;
1504                         
1505                         for(int i = 0;i<numBoxes;i++)
1506                         {
1507                                 //----------
1508                                 getline(inputStream,line);//BOX
1509                                 getline(inputStream,line);//package:type:name
1510                                 char box[150];
1511                                 strcpy( box, line.c_str() );
1512                                 result = strtok( box, delims );//package
1513                                 std::string package(result);
1514                                 result = strtok( NULL, delims );//type
1515                                 std::string type(result);
1516                                 result = strtok( NULL, delims );//name
1517                                 std::string name(result);
1518
1519                                 getline(inputStream,line);//ISEXEC:TRUE|FALSE
1520                                 char exec[15];
1521                                 strcpy( exec, line.c_str() );
1522                                 result = strtok( exec, delims );//ISEXEC                                
1523                                 result = strtok( NULL, delims );//TRUE|FALSE
1524                                 std::string isExec(result);
1525
1526                                 //----------
1527                                 getline(inputStream,line);//xInic:yInic:zInic
1528                                 char coord[80];
1529                                 strcpy( coord, line.c_str() );
1530                                 result = strtok( coord, delims );//xInic
1531                                 std::string xInic(result);
1532                                 result = strtok( NULL, delims );//yInic
1533                                 std::string yInic(result);
1534                                 result = strtok( NULL, delims );//zInic
1535                                 std::string zInic(result);
1536
1537                                 double xIn, yIn, zIn;
1538                                 std::istringstream xSt(xInic);
1539                                 xSt >> xIn;
1540                                 std::istringstream ySt(yInic);
1541                                 ySt >> yIn;
1542                                 std::istringstream zSt(zInic);
1543                                 zSt >> zIn;
1544                                                 
1545                                 //----------
1546                                 getline(inputStream,line);//xEnd:yEnd:zEnd
1547                                 strcpy( coord, line.c_str() );
1548                                 result = strtok( coord, delims );//xEnd
1549                                 std::string xEnd(result);
1550                                 result = strtok( NULL, delims );//yEnd
1551                                 std::string yEnd(result);
1552                                 result = strtok( NULL, delims );//zEnd
1553                                 std::string zEnd(result);
1554
1555                                 double xEn, yEn, zEn;
1556                                 std::istringstream xEt(xEnd);
1557                                 xEt >> xEn;
1558                                 std::istringstream yEt(yEnd);
1559                                 yEt >> yEn;
1560                                 std::istringstream zEt(zEnd);
1561                                 zEt >> zEn;
1562
1563                                 int idBox = createGBlackBox(0,0,package,type);
1564                                 GObjectController *cont = _controllers[idBox];
1565                                 GBlackBoxModel *bbmod = (GBlackBoxModel*)cont->getModel();
1566                                 bbmod->setBBTKName(name);
1567                                 bbmod->setInicPoint(xIn,yIn,zIn);
1568                                 bbmod->setFinalPoint(xEn,yEn,zEn);
1569                                 if(isExec=="TRUE")
1570                                 {
1571                                         bbmod->setExecutable(true);
1572                                 }
1573                                 else if(isExec=="FALSE")
1574                                 {
1575                                         bbmod->setExecutable(false);
1576                                 }
1577                                                         
1578                                 //----------
1579                                 getline(inputStream,line);//PORT o FIN_BOX
1580                                 std::string port=line.substr(0,4);
1581                                 while(port=="PORT")
1582                                 {
1583                                         getline(inputStream,line);//name:value
1584                                         char poort[150];
1585                                         strcpy( poort, line.c_str() );
1586                                         result = strtok( poort, delims );//name
1587                                         std::string name(result);
1588                                         result = strtok( NULL, delims );//value
1589                                         std::string value(result);
1590
1591                                         bbmod->setValueToInput(name,value);                                     
1592
1593                                         getline(inputStream,line);//PORT o FIN_BOX
1594                                         port=line.substr(0,4);
1595                                 } // while              
1596
1597                                 bbmod->notifyObservers(_idManager);
1598                         } // boxes
1599
1600                         /// CONNECTIONS
1601                         //----------
1602                         getline(inputStream,line);//CONNECTIONS:num
1603                         char conns[30];
1604                         strcpy( conns, line.c_str() );
1605                         result = strtok( conns, delims );
1606                         result = strtok( NULL, delims );
1607                                         
1608                         int numConns;
1609                         std::istringstream isCons(result);
1610                         isCons >> numConns;
1611                                         
1612                         for(int i = 0;i<numConns;i++)
1613                         {
1614                                 //----------
1615                                 getline(inputStream,line);//CONNECTION
1616                                 getline(inputStream,line);//Startbox.PortName:EndBox.PortName
1617
1618                                 char connec[200];
1619                                 strcpy( connec, line.c_str() );
1620                                 result = strtok( connec, delims );
1621                                 std::string nameStartBox(result);
1622                                 result = strtok( NULL, delims );
1623                                 std::string nameStartPort(result);
1624                                 result = strtok( NULL, delims );
1625                                 std::string nameEndBox(result);
1626                                 result = strtok( NULL, delims );
1627                                 std::string nameEndPort(result);
1628
1629                                 std::vector<int> lstB = getBlackBoxes();
1630                                 
1631                                 GPortModel *startP=NULL;
1632                                 GPortModel *endP=NULL;                          
1633                                 for(int j = 0;j<lstB.size();j++)
1634                                 {
1635                                         int idB = lstB[j];
1636                                         GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1637                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1638                                         {                                               
1639                                                 startP = bbMod->getOutputPort(nameStartPort);
1640                                         }
1641                                         else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1642                                         {
1643                                                 endP = bbMod->getInputPort(nameEndPort);
1644                                         }
1645                                 } // for
1646
1647                                 if(_isComplexBox)
1648                                 {
1649                                         if(startP==NULL)
1650                                         {
1651                                                 std::vector<int> lstInputs = getComplexInputPorts();
1652                                                 for(int j = 0;j<lstInputs.size();j++)
1653                                                 {
1654                                                         int idB = lstInputs[j];
1655                                                         GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
1656                                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1657                                                         {                                               
1658                                                                 startP = cbMod->getOutputPort(nameStartPort);
1659                                                         }
1660                                                 }
1661                                         }
1662
1663                                         if(endP==NULL)
1664                                         {
1665                                                 std::vector<int> lstOutputs = getComplexOutputPorts();
1666                                                 for(int j = 0;j<lstOutputs.size();j++)
1667                                                 {
1668                                                         int idB = lstOutputs[j];
1669                                                         GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
1670                                                         if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1671                                                         {                                               
1672                                                                 endP = cbMod->getInputPort(nameEndPort);
1673                                                         }
1674                                                 }
1675                                         }
1676                                 } // complex box
1677                                                                 
1678                                 int idCon = createGConnector(startP);
1679                                 _worldState = NOTHING_HAPPENS;
1680                                 GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
1681
1682                                 GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1683                                 vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1684                                 tempp->endContourCreation();
1685                                 conMod->setEndPort(endP);                                       
1686                                 conView->updateStartEndPoints();
1687
1688                         } // for numConns
1689         } // start
1690
1691         }
1692
1693         //=========================================================================
1694
1695         std::vector<int> wxVtkSceneManager::getBlackBoxes()
1696         {
1697                 std::vector<int> vect;
1698                 std::map<int, GObjectController*>::iterator it;
1699                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1700                 {
1701                         GObjectController *cont = it->second;           
1702                         if(cont->getGObjectType()==GBLACKBOX)
1703                         {
1704                                 vect.push_back(cont->getId());
1705                         }
1706                 }
1707                 return vect;
1708         }
1709
1710         //=========================================================================
1711
1712         std::vector<int> wxVtkSceneManager::getComplexInputPorts()
1713         {
1714                 std::vector<int> vect;
1715                 std::map<int, GObjectController*>::iterator it;
1716                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1717                 {
1718                         GObjectController *cont = it->second;           
1719                         if(cont->getGObjectType()==GCOMPLEXINPUTPORT)
1720                         {
1721                                 vect.push_back(cont->getId());
1722                         }
1723                 }
1724                 return vect;
1725         }
1726
1727         //=========================================================================
1728
1729         std::vector<int> wxVtkSceneManager::getComplexOutputPorts()
1730         {
1731                 std::vector<int> vect;
1732                 std::map<int, GObjectController*>::iterator it;
1733                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1734                 {
1735                         GObjectController *cont = it->second;           
1736                         if(cont->getGObjectType()==GCOMPLEXOUTPUTPORT)
1737                         {
1738                                 vect.push_back(cont->getId());
1739                         }
1740                 }
1741                 return vect;
1742         }
1743
1744         //=========================================================================
1745
1746         std::vector<int> wxVtkSceneManager::getConnections()
1747         {
1748                 std::vector<int> vect;
1749                 std::map<int, GObjectController*>::iterator it;
1750                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1751                 {
1752                         GObjectController *cont = it->second;           
1753                         if(cont->getGObjectType()==GCONNECTOR)
1754                         {
1755                                 vect.push_back(cont->getId());
1756                         }
1757                 }
1758                 return vect;
1759         }
1760
1761         //=========================================================================
1762         
1763         bool wxVtkSceneManager::isComplexBox()
1764         {
1765                 return _isComplexBox;
1766         }
1767
1768         //=========================================================================
1769
1770         void wxVtkSceneManager::setComplexBox(bool val)
1771         {
1772                 _isComplexBox=val;
1773         }
1774  
1775         //=========================================================================
1776
1777         int wxVtkSceneManager::addObjectController(GObjectController* objController)
1778         {
1779                 //Register the controller of the new object
1780                 registerController((InteractorStyleMaracas*) objController);
1781
1782                 //Add the object to the objects list 
1783                 int newId = _contLastId;//_controllers.size();
1784                 objController->setId(newId);
1785                 _controllers[newId] = objController;
1786                 _contLastId++;
1787                 return newId;
1788         }
1789
1790         //=========================================================================
1791
1792         int wxVtkSceneManager::getNumSelectedObjects()
1793         {
1794                 return _selectedObjects.size();
1795         }
1796
1797         //=========================================================================
1798
1799         std::map<int,GObjectController*> wxVtkSceneManager::getSelectedObjects()
1800         {
1801                 std::map<int,GObjectController*> mapSelected;
1802
1803                 std::map<int, GObjectController*>::iterator it;
1804                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1805                 {
1806                         GObjectController *cont = it->second;           
1807                         if(cont->getGObjectType()==GBLACKBOX && cont->getView()->getState()==SELECTED)
1808                         {
1809                                 mapSelected[cont->getId()]=cont;
1810                         }
1811                 }
1812
1813                 std::map<int, GObjectController*>::iterator it2;
1814                 for(it2 = _controllers.begin(); it2 != _controllers.end(); ++it2)
1815                 {
1816                         GObjectController *cont = it2->second;          
1817                         if(cont->getGObjectType()==GCONNECTOR)
1818                         {
1819                                 GConnectorModel* cmod = (GConnectorModel*)cont->getModel();
1820                                 GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
1821                                 GBoxModel* startPortParentBox = cmod->getStartPort()->getParentBox();
1822
1823                                 std::map<int, GObjectController*>::iterator iterOBJ1 = mapSelected.find(startPortParentBox->getObjectId());
1824                                 std::map<int, GObjectController*>::iterator iterOBJ2 = mapSelected.find(endPortParentBox->getObjectId());
1825                                 
1826                                 if(iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end())
1827                                 {                                       
1828                                         int ID = cont->getId();
1829                                         mapSelected[ID]=cont;
1830                                 }
1831                                 
1832                         }
1833                 }
1834                 return mapSelected;
1835         }
1836
1837         //=========================================================================
1838
1839         void wxVtkSceneManager::addObjects(std::map<int,GObjectController*> objectsMap)
1840         {
1841                 
1842                 std::map<int,int> oldIdNewIdBoxes;
1843                 std::vector<int> connections;
1844
1845                 std::map<int, GObjectController*>::iterator it;         
1846                 for(it = objectsMap.begin(); it != objectsMap.end(); ++it)
1847                 {
1848                         GObjectController *cont = it->second;
1849                         int type = cont->getGObjectType();
1850
1851                         if(type==GBLACKBOX)
1852                         {
1853                                 // Copy black box
1854                                 double xInic, yInic,zInic;
1855                                 GBlackBoxModel* copyBox = (GBlackBoxModel*)cont->getModel();
1856                                 copyBox->getInicPoint(xInic,yInic,zInic);
1857                                 int idBox = createGBlackBox(0,0,copyBox->getBBTKPackage(),copyBox->getBBTKType());
1858
1859                                 int idcB = copyBox->getObjectId();
1860                                 oldIdNewIdBoxes[idcB]=idBox;
1861                                 cont = _controllers[idBox];                                                             
1862                                 GBlackBoxModel* newbox  = (GBlackBoxModel*)cont->getModel();
1863                                 newbox->setInicPoint(xInic,yInic,zInic);                                                        
1864                                 int num = newbox->getNumInputPorts();
1865                                 for(int j=0;j<num;j++)
1866                                 {
1867                                         newbox->setValueToInputPort(j,copyBox->getValueInputPort(j));
1868                                 }
1869                                 newbox->notifyObservers(_idManager);
1870                         }
1871                         else if(type==GCONNECTOR)
1872                         {
1873                                 int idCon = cont->getId();
1874                                 connections.push_back(idCon);
1875                         }
1876                         
1877                 }
1878                 
1879                 for(int i = 0 ;i<connections.size();i++)
1880                 {
1881                         int objId = connections[i];
1882                         GObjectController *cont = objectsMap[objId];                    
1883                         GConnectorModel* connectModel = (GConnectorModel*)cont->getModel();
1884                         
1885                         GPortModel* startPort = connectModel->getStartPort();
1886                         int startPortIndex = startPort->getPosInBox();
1887                         GPortModel* endPort = connectModel->getEndPort();
1888                         int endPortIndex = endPort->getPosInBox();                      
1889
1890                         GBlackBoxModel* startPortParentBox = (GBlackBoxModel*)startPort->getParentBox();
1891                         GBlackBoxModel* endPortParentBox = (GBlackBoxModel*)endPort->getParentBox();
1892                         
1893                         int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
1894                         int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
1895
1896                         GBlackBoxModel* newStartBox = (GBlackBoxModel*)_controllers[idNewStartBox]->getModel();
1897                         GBlackBoxModel* newEndBox = (GBlackBoxModel*)_controllers[idNewEndBox]->getModel();
1898
1899                         GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
1900                         GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
1901
1902                         // Creates connection 
1903                         int idCon = createGConnector(newStartPort);
1904                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
1905                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1906                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1907                         tempp->endContourCreation();
1908                         conMod->setEndPort(newEndPort);                                 
1909                         conView->updateStartEndPoints();
1910                 }
1911
1912                 std::map<int, int>::iterator itIds;             
1913                 for(itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds)
1914                 {
1915                         int idOld = itIds->first;
1916                         int idNew = itIds->second;
1917
1918                         GBlackBoxModel* oldBox = (GBlackBoxModel*)objectsMap[idOld]->getModel();
1919                         GBlackBoxModel* newBox = (GBlackBoxModel*)_controllers[idNew]->getModel();
1920
1921                         std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
1922                         std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
1923                         std::vector<int> newInputConnections = newBox->getConnectedInputs();
1924                         std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
1925
1926                         for(int k = 0; k<oldInputConnections.size();k++)
1927                         {
1928                                 bool exist=false;
1929                                 int toCreate=-1;
1930                                 for(int l = 0; l<newInputConnections.size() && !exist;l++)
1931                                 {
1932                                         if(oldInputConnections[k]==newInputConnections[l])
1933                                         {
1934                                                 exist=true;
1935                                         }
1936                                 }
1937
1938                                 if(exist==false)
1939                                 {
1940                                         //Create complex input 
1941                                         int posInBox = oldInputConnections[k];
1942                                         GPortModel* inputPort = oldBox->getInputPort(posInBox);
1943                                         std::string inputPortName = inputPort->getBBTKName();
1944                                         int idInputPort = createGComplexBoxInputPort(inputPortName);
1945                                         GObjectController *cont = _controllers[idInputPort];
1946                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
1947                                         double xIn,yIn,zIn;
1948                                         inputPort->getInicPoint(xIn,yIn,zIn);
1949                                         yIn+=20;
1950                                         cbmod->setInicPoint(xIn,yIn,zIn);
1951                                         cbmod->notifyObservers(_idManager);
1952                                                                                                                 
1953                                         GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
1954
1955                                         // Creates connection 
1956                                         int idCon = createGConnector(cbmod->getOutputPort(0));
1957                                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
1958                                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1959                                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1960                                         tempp->endContourCreation();
1961                                         conMod->setEndPort(inputPortEnd);                                       
1962                                         conView->updateStartEndPoints();
1963
1964                                 }
1965                                         
1966                         }
1967                         
1968
1969                         for(int k = 0; k<oldOutputConnections.size();k++)
1970                         {
1971                                 bool exist=false;
1972                                 int toCreate=-1;
1973                                 for(int l = 0; l<newOutputConnections.size() && !exist;l++)
1974                                 {
1975                                         if(oldOutputConnections[k]==newOutputConnections[l])
1976                                         {
1977                                                 exist=true;
1978                                         }
1979                                 }
1980
1981                                 if(exist==false)
1982                                 {
1983                                         //Create complex output 
1984                                         int posInBox = oldOutputConnections[k];
1985                                         GPortModel* outputPort = oldBox->getOutputPort(posInBox);
1986                                         std::string outputPortName = outputPort->getBBTKName();
1987                                         int idOutputPort = createGComplexBoxOutputPort(outputPortName);
1988                                         GObjectController *cont = _controllers[idOutputPort];
1989                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
1990                                         double xIn,yIn,zIn;
1991                                         outputPort->getInicPoint(xIn,yIn,zIn);
1992                                         yIn-=20;
1993                                         cbmod->setInicPoint(xIn,yIn,zIn);
1994                                         cbmod->notifyObservers(_idManager);
1995                                                                                                                 
1996                                         GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
1997
1998                                         // Creates connection 
1999                                         int idCon = createGConnector(outputPortEnd);
2000                                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
2001                                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2002                                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2003                                         tempp->endContourCreation();
2004                                         conMod->setEndPort(cbmod->getInputPort(0));                                     
2005                                         conView->updateStartEndPoints();
2006
2007                                 }
2008                                         
2009                         }
2010
2011                 }
2012
2013
2014         }
2015
2016         //=========================================================================     
2017
2018 }  // EO namespace bbtk
2019
2020 // EOF
2021