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