1 /*=========================================================================
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
33 * \brief Class bbtk::wxVtkSceneManager .
37 #include "wxVtkSceneManager.h"
43 //=========================================================================
44 wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView,int idManager)
55 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);
57 _worldState=NOTHING_HAPPENS;
58 registerController(this);
64 //=========================================================================
66 void wxVtkSceneManager::disconnectDrop()
68 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
71 //=========================================================================
73 wxVtkSceneManager::~wxVtkSceneManager()
77 //=========================================================================
79 void wxVtkSceneManager::configureBaseView()
81 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
83 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
85 // Important to activate the 2D interaction system
86 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
87 interactorstylebaseview->SetInteractor ( iren );
88 iren->SetInteractorStyle(interactorstylebaseview);
89 interactorstylebaseview->SetwxVtkBaseView(_baseView);
91 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
92 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
94 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
95 _baseView->GetRenderer()->GradientBackgroundOff();
99 //=========================================================================
101 int wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
103 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
104 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
106 int type = GBLACKBOX;
108 //Create the MVC Objects
110 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
111 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
112 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
114 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
116 //Prepares the initial model
117 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height
120 double yy = windowHeight-y;
122 //z value is not important yet, because it is only used a parallel projection
125 _baseView->TransCoordScreenToWorld(xx,yy,zz);
126 model->setInicPoint(xx,yy,zz);
129 std::stringstream stream;
133 stream << "Box0" << _numBoxes;
137 stream << "Box" << _numBoxes;
139 std::string arraystring = stream.str();
141 model->setBBTKName(arraystring);
142 model->setBBTKType(boxType);
143 model->setBBTKPackage(packageName);
145 model->addObserver(view);
146 model->addObserver(this);
148 //Iterate and create the input ports
149 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
150 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
153 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
155 BlackBoxInputDescriptor *desc = itInput->second;
156 createGInputPort(GINPUTPORT,i,model,desc);
160 //Iterate and create the output ports
161 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
162 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
165 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
167 BlackBoxOutputDescriptor *desc = itOutput->second;
168 createGOutputPort(GOUTPUTPORT,i,model,desc);
173 //Associates the view with the correspondent renderer and the model.
174 //(NOTE: Refresh is only made by the view)
175 view->setModel(model);
176 view->setBaseView(_baseView);
177 view->initVtkObjects();
179 //Associates the controller with the correspondent model and view
180 controller->setModelAndView(model,view);
182 //Resgiter change to the observers of the actual model
183 model->notifyObservers(_idManager);
185 //Register the controller of the new object
186 registerController((InteractorStyleMaracas*) controller);
188 //Add the object to the objects list
189 int newId = _controllers.size();
190 controller->setId(newId);
191 _controllers[newId] = controller;
196 //=========================================================================
198 int wxVtkSceneManager::createGComplexBoxInputPort(std::string inputName)
200 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
201 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
203 int type = GCOMPLEXINPUTPORT;
205 //Create the MVC Objects
207 GComplexBoxPortModel *model = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
208 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
209 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
211 //Prepares the initial model
214 double yy = windowHeight-5;
216 //z value is not important yet, because it is only used a parallel projection
219 _baseView->TransCoordScreenToWorld(xx,yy,zz);
220 model->setInicPoint(xx,yy,zz);
222 model->setBBTKName(inputName);
223 model->setBBTKType("ComplexInputPort");
224 model->setComplexPortType(type);
226 model->addObserver(view);
227 model->addObserver(this);
229 //create the output port
230 GPortController* portController = createGPort(GOUTPUTPORT,inputName,"ComplexInputPort",0,model);
231 model->addOutputPort((GPortModel*)portController->getModel());
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();
239 //Associates the controller with the correspondent model and view
240 controller->setModelAndView(model,view);
242 //Resgiter change to the observers of the actual model
243 model->notifyObservers(_idManager);
245 //Register the controller of the new object
246 registerController((InteractorStyleMaracas*) controller);
248 //Add the object to the objects list
249 int newId = _controllers.size();
250 controller->setId(newId);
251 _controllers[newId] = controller;
255 //=========================================================================
257 int wxVtkSceneManager::createGComplexBoxOutputPort(std::string outputName)
259 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
260 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
262 int type = GCOMPLEXOUTPUTPORT;
264 //Create the MVC Objects
266 GComplexBoxPortModel *model = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
267 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
268 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
270 //Prepares the initial model
273 double yy = windowHeight-5;
275 //z value is not important yet, because it is only used a parallel projection
278 _baseView->TransCoordScreenToWorld(xx,yy,zz);
279 model->setInicPoint(xx,yy,zz);
281 model->setBBTKName(outputName);
282 model->setBBTKType("ComplexOutputPort");
283 model->setComplexPortType(type);
285 model->addObserver(view);
286 model->addObserver(this);
288 //create the output port
289 GPortController* portController = createGPort(GINPUTPORT,outputName,"ComplexInputPort",0,model);
290 model->addInputPort((GPortModel*)portController->getModel());
292 //Associates the view with the correspondent renderer and the model.
293 //(NOTE: Refresh is only made by the view)
294 view->setModel(model);
295 view->setBaseView(_baseView);
296 view->initVtkObjects();
298 //Associates the controller with the correspondent model and view
299 controller->setModelAndView(model,view);
301 //Resgiter change to the observers of the actual model
302 model->notifyObservers(_idManager);
304 //Register the controller of the new object
305 registerController((InteractorStyleMaracas*) controller);
307 //Add the object to the objects list
308 int newId = _controllers.size();
309 controller->setId(newId);
310 _controllers[newId] = controller;
314 //=========================================================================
316 int wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxInputDescriptor *desc)
318 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
319 blackBox->addInputPort((GPortModel*)portController->getModel());
320 return portController->getId();
323 //=========================================================================
325 int wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
327 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
328 blackBox->addOutputPort((GPortModel*)portController->getModel());
329 return portController->getId();
332 //=========================================================================
334 GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBoxModel *blackBox)
338 //Create the MVC Objects
339 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
340 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
341 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
343 model->registerInBox(blackBox,portType, posInBox);
345 model->setBBTKType(bbtkType);
346 model->setBBTKName(bbtkName);
348 model->addObserver(view);
349 model->addObserver(this);
351 //Associates the view with the correspondent renderer and the model.
352 //(NOTE: Refresh is only made by the view)
353 view->setModel(model);
354 view->setBaseView(_baseView);
355 view->initVtkObjects();
357 //Associates the controller with the correspondent model and view
358 controller->setModelAndView(model,view);
360 model->notifyObservers(_idManager);
362 //Register the controller of the new object
363 registerController((InteractorStyleMaracas*) controller);
365 int newId = _controllers.size();
366 controller->setId(newId);
367 _controllers[newId] = controller;
369 return (GPortController*)controller;
372 //=========================================================================
374 int wxVtkSceneManager::createGConnector(GPortModel* startPort)
376 int type = GCONNECTOR;
378 manualConnectorContourController* manContourControl = new manualConnectorContourController();
379 manualConnectorContourView* manContourView = new manualConnectorContourView();
380 manualContourModel* manContourModel = new manualContourModel();
382 GConnectorController* connectorcontroller = new GConnectorController();
383 GConnectorModel* connectorModel = new GConnectorModel();
384 vtkGConnectorView* connectorView = new vtkGConnectorView();
385 connectorModel->setGObjectType(type);
387 manContourModel->SetCloseContour(false);
388 connectorModel->setStartPort(startPort);
390 manContourView->SetModel( manContourModel );
391 manContourView->SetWxVtkBaseView( _baseView );
392 manContourView->SetRange( 0.5 );
393 manContourView->SetZ( 900 );
395 manContourView->SetColorNormalContour(0, 0, 1);
396 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
397 manContourView->SetColorSelectContour(1, 0.8, 0);
398 manContourView->SetWidthLine(1);
399 manContourView->SetShowText(false);
401 manContourControl->SetModelView( manContourModel , manContourView );
403 manContourControl->CreateNewManualContour();
405 manContourView->RefreshContour();
409 connectorModel->getInicPoint(x,y,z);
411 manContourControl->SetState(1);
412 manContourModel->SetCloseContour(false);
414 manContourModel->AddPoint(x,y,z);
415 manContourView->AddPoint();
417 manContourModel->AddPoint(x,y,z);
418 manContourView->AddPoint();
420 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
421 manContourControl->_bakIdPoint=bak;
422 manContourView->Refresh();
424 manContourControl->SetMoving( false );
426 registerController((InteractorStyleMaracas*) connectorcontroller);
428 connectorcontroller->setManualContourController(manContourControl);
429 connectorModel->setManualContourModel(manContourModel);
430 connectorView->setManualContourView(manContourView);
431 connectorView->setModel(connectorModel);
432 connectorView->setBaseView(_baseView);
433 connectorcontroller->setModelAndView(connectorModel,connectorView);
435 connectorModel->addObserver(connectorView);
436 connectorModel->addObserver(this);
438 int newId = _controllers.size();
439 connectorcontroller->setId(newId);
440 _controllers[newId] = connectorcontroller;
445 //=========================================================================
447 void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
449 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
450 baseViewControlManager->AddInteractorStyleMaracas( param );
453 //=========================================================================
455 void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
457 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
458 baseViewControlManager->RemoveInteractorStyleMaracas( param );
461 //=========================================================================
463 vtkRenderer* wxVtkSceneManager::getRenderer()
465 return _baseView->GetRenderer();
468 //=========================================================================
470 vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
472 return _baseView->GetRenWin();
474 //=========================================================================
477 void wxVtkSceneManager::update(int idController,int command)
479 if(command != NO_COMMAND)
481 if(command == ADD_TO_SELECTED)
483 GObjectController* cont = _controllers[idController];
486 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
488 int id = _selectedObjects[i];
496 int id = idController;
497 _selectedObjects.push_back(id);
501 else if(command == INIT_CREATION_CONTOUR)
503 _worldState = INIT_CREATION_CONTOUR;
504 GObjectController* cont = _controllers[idController];
505 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
508 // The last one is the controller of the connector
509 for(int i=0;i<_controllers.size();i++)
511 GObjectController* cont = _controllers[i];
512 if(cont->getGObjectType() == GPORT )
514 GPortModel* port = (GPortModel*)cont->getModel();
515 if(port->getPortType()==GINPUTPORT)
517 cont->SetActive(true);
521 cont->getView()->setState(NOTHING_HAPPENS);
522 cont->getModel()->notifyObservers(_idManager);
523 cont->SetActive(false);
528 cont->getView()->setState(NOTHING_HAPPENS);
529 cont->getModel()->notifyObservers(_idManager);
530 cont->SetActive(false);
534 _selectedObjects.clear();
536 createGConnector(startOutputPort);
539 else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
541 _worldState = NOTHING_HAPPENS;
542 int id = _controllers.size()-1;
543 GObjectController* cont = _controllers[id];
544 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
546 GObjectController* finPort = _controllers[idController];
547 if(finPort->getGObjectType() == GPORT)
549 GPortModel* modelPort = (GPortModel*)finPort->getModel();
550 modelContour->setEndPort(modelPort);
553 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();
554 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
557 for(int i=0;i<_controllers.size();i++)
559 GObjectController* cont = _controllers[i];
560 if(cont->getView()!=NULL)
562 cont->getView()->setState(NOTHING_HAPPENS);
563 cont->getModel()->notifyObservers(_idManager);
565 cont->SetActive(true);
572 //=========================================================================
574 bool wxVtkSceneManager::OnMouseMove()
577 wxVTKRenderWindowInteractor *wxVTKiren;
578 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
579 wxVTKiren->GetEventPosition(X,Y);
582 if(_worldState == DRAG_OBJECTS)
584 for (int i=0; i<_selectedObjects.size(); i++)
586 int id = _selectedObjects[i];
587 GObjectController* cont = _controllers[id];
590 cont->getView()->setStartDragging(true);
592 cont->getView()->moveObject(X,Y);
593 cont->getView()->setState(DRAG);
594 cont->getModel()->notifyObservers(_idManager);
597 std::map<int, GObjectController*>::iterator it;
599 for(it = _controllers.begin(); it != _controllers.end(); ++it)
601 GObjectController *desc = it->second;
602 if(desc->getGObjectType()==GCONNECTOR)
604 vtkGConnectorView* vconn = (vtkGConnectorView*)desc->getView();
605 vconn->updateStartEndPoints();
609 _startDragging=false;
612 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
614 std::map<int, GObjectController*>::iterator it;
616 for(it = _controllers.begin(); it != _controllers.end(); ++it)
618 GObjectController *desc = it->second;
619 int type = desc->getGObjectType();
620 int state = desc->getView()->getState();
622 if(state == HIGHLIGHTED){
624 updateStatusBar(desc->getStatusText());
627 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
628 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
640 //=========================================================================
642 bool wxVtkSceneManager::OnLeftButtonDown()
644 if(_worldState==INIT_CREATION_CONTOUR)
646 bool isOverPort=false;
647 std::map<int, GObjectController*>::iterator it;
648 for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
650 GObjectController *desc = it->second;
651 if(desc->getGObjectType()==GPORT)
653 GPortModel* portmod=(GPortModel*)desc->getModel();
654 vtkGObjectView* portView=desc->getView();
655 if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
662 if(isOverPort==false)
664 _worldState=NOTHING_HAPPENS;
665 int lastId = _controllers.size()-1;
666 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
667 connector->removeFromScene();
668 unregisterController(connector);
669 _controllers.erase(lastId);
671 for(it = _controllers.begin(); it != _controllers.end(); ++it)
673 GObjectController *desc = it->second;
674 desc->SetActive(true);
675 desc->getView()->setState(NOTHING_HAPPENS);
676 desc->getModel()->notifyObservers(_idManager);
681 if(_selectedObjects.size()!=0)
683 _worldState = DRAG_OBJECTS;
684 _startDragging = true;
686 for (int i = 0; i < _selectedObjects.size(); i++)
688 int id = _selectedObjects[i];
689 GObjectController* cont = _controllers[id];
690 cont->getView()->setState(DRAG);
691 cont->getModel()->notifyObservers(_idManager);
700 //=========================================================================
702 bool wxVtkSceneManager::OnLeftButtonUp()
704 if(_worldState == DRAG_OBJECTS)
706 _worldState = NOTHING_HAPPENS;
708 for (int i = 0; i < _selectedObjects.size(); i++)
710 int id = _selectedObjects[i];
711 GObjectController* cont = _controllers[id];
712 cont->getView()->setState(SELECTED);
713 cont->getModel()->notifyObservers(_idManager);
719 //=========================================================================
721 bool wxVtkSceneManager::OnRightButtonUp()
723 if(_worldState==INIT_CREATION_CONTOUR)
725 _worldState=NOTHING_HAPPENS;
726 int lastId = _controllers.size()-1;
727 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
728 connector->removeFromScene();
729 unregisterController(connector);
730 _controllers.erase(lastId);
732 std::map<int, GObjectController*>::iterator it;
733 for(it = _controllers.begin(); it != _controllers.end(); ++it)
735 GObjectController *desc = it->second;
736 desc->SetActive(true);
737 desc->getView()->setState(NOTHING_HAPPENS);
738 desc->getModel()->notifyObservers(_idManager);
743 for (int i = 0; i < _selectedObjects.size(); i++)
745 int id = _selectedObjects[i];
746 GObjectController* cont = _controllers[id];
747 cont->SetActive(true);
748 cont->getView()->setState(NOTHING_HAPPENS);
749 cont->getModel()->notifyObservers(_idManager);
752 _selectedObjects.clear();
757 //=========================================================================
759 bool wxVtkSceneManager::OnLeftDClick()
762 wxVTKRenderWindowInteractor *wxVTKiren;
763 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
764 wxVTKiren->GetEventPosition(X,Y);
766 std::map<int, GObjectController*>::iterator it;
768 bool clickOnObject = false;
770 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
772 GObjectController *cont = it->second;
773 int type = cont->getGObjectType();
775 if(cont->getView()->isPointInside(X,Y))
779 for (int i=0; i<_selectedObjects.size(); i++)
781 int id = _selectedObjects[i];
782 GObjectController* control = _controllers[id];
783 control->getView()->setState(NOTHING_HAPPENS);
785 _selectedObjects.clear();
787 GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
788 _parent->editBlackBox(bbmodel);
790 clickOnObject = true;
794 if(clickOnObject==false)
796 //_parent->editDiagramParameters(this);
802 //=========================================================================
804 bool wxVtkSceneManager::OnChar()
806 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
808 // KeyCode 127 : Delete Key
809 // KeyCode 8 : Backspace Key
810 if(keyCode == 8 || keyCode == 127)
812 if(_selectedObjects.size()>0)
814 for(int i=0;i<_selectedObjects.size();i++)
816 int id = _selectedObjects[i];
819 _selectedObjects.clear();
826 //=========================================================================
828 void wxVtkSceneManager::deleteObject(int id)
830 GObjectController *control = _controllers[id];
831 std::vector<int> controllersToRemove;
833 if(control->getGObjectType()==GBLACKBOX)
835 GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
836 std::vector<GPortModel*> inputs = bbmod->getInputPorts();
838 bool boxConnected = false;
840 // Add box input controllers to be removed
841 for(int i = 0;i<inputs.size();i++)
843 controllersToRemove.push_back(inputs[i]->getObjectId());
844 if(inputs[i]->isConnected())
850 std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
852 // Add box output controllers to be removed
853 for(int i = 0;i<outputs.size();i++)
855 controllersToRemove.push_back(outputs[i]->getObjectId());
856 if(outputs[i]->isConnected())
862 // Add connection controllers to be removed
863 std::map<int, GObjectController*>::iterator it;
864 for(it = _controllers.begin(); it != _controllers.end(); ++it)
866 GObjectController *cont = it->second;
867 int type = cont->getGObjectType();
870 GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
871 if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
873 controllersToRemove.push_back(conMod->getObjectId());
875 if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
877 controllersToRemove.push_back(conMod->getObjectId());
882 // Add box controller to be removed
883 controllersToRemove.push_back(bbmod->getObjectId());
885 else if(control->getGObjectType()==GCONNECTOR)
887 GConnectorModel *conMod = (GConnectorModel*)control->getModel();
888 controllersToRemove.push_back(conMod->getObjectId());
891 for(int i = 0;i<controllersToRemove.size();i++)
893 int id = controllersToRemove[i];
894 GObjectController *cont = _controllers[id];
895 cont->removeFromScene();
896 unregisterController((InteractorStyleMaracas*)cont);
897 _controllers.erase(id);
903 //=========================================================================
905 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
907 _parent->displayBlackBoxInfo(packageName,boxName);
910 //=========================================================================
912 void wxVtkSceneManager::updateStatusBar(std::string textStatus)
914 _parent->updateStatusBar(textStatus);
917 //=========================================================================
919 std::string wxVtkSceneManager::getDiagramBBS()
921 bool existsExec=false;
923 std::vector<std::string> packages;
924 std::vector<int> boxes;
925 std::vector<int> connections;
926 std::vector<int> execBoxes;
928 std::map<int, GObjectController*>::iterator it;
930 for(it = _controllers.begin(); it != _controllers.end(); ++it)
932 GObjectController *desc = it->second;
933 int type = desc->getGObjectType();
937 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
939 std::string pkg = mod->getBBTKPackage();
940 bool existsPkg = false;
941 for(int t = 0;t<packages.size() && existsPkg == false;t++)
950 packages.push_back(pkg);
954 boxes.push_back(it->first);
955 if(mod->isExecutable())
957 execBoxes.push_back(it->first);
961 else if(type==GCONNECTOR)
963 connections.push_back(it->first);
967 std::string script = "";
968 script+="# BBTK GEditor Script\n";
969 script+="# ----------------------\n";
973 for(i = 0; i<packages.size();i++)
980 script+="include std\n";
982 for(i = 0; i<boxes.size();i++)
986 GObjectController *control = _controllers[id];
987 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
989 script+=model->getBBTKType();
991 script+=model->getBBTKName();
994 std::vector<GPortModel*> inputs = model->getInputPorts();
995 for(int j = 0; j<inputs.size();j++)
997 GPortModel* inputPort = inputs[j];
998 if(inputPort->isValueSet())
1001 script+=model->getBBTKName();
1003 script+=inputPort->getBBTKName();
1005 script+=inputPort->getValue();
1012 for(i = 0; i<connections.size();i++)
1015 int id = connections[i];
1016 GObjectController *control = _controllers[id];
1017 GConnectorModel *model = (GConnectorModel*)control->getModel();
1019 //Start Connection info
1020 GPortModel *start = model->getStartPort();
1021 script+=start->getParentBox()->getBBTKName();
1023 script+=start->getBBTKName();
1027 //End Connection info
1028 GPortModel *end = model->getEndPort();
1029 script+=end->getParentBox()->getBBTKName();
1031 script+=end->getBBTKName();
1036 for(i = 0; i<execBoxes.size();i++)
1039 int id = execBoxes[i];
1040 GObjectController *control = _controllers[id];
1041 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1043 script+=model->getBBTKName();
1052 //=========================================================================
1054 std::string wxVtkSceneManager::saveComplexBoxBBS(std::string cbName,std::string cbAuthor,std::string cbCategory,std::string cbDescription)
1057 std::vector<std::string> packages;
1058 std::vector<int> boxes;
1059 std::vector<int> connections;
1060 std::vector<int> execBoxes;
1062 std::map<int, GObjectController*>::iterator it;
1064 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1066 GObjectController *desc = it->second;
1067 int type = desc->getGObjectType();
1071 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
1073 std::string pkg = mod->getBBTKPackage();
1074 bool existsPkg = false;
1075 for(int t = 0;t<packages.size() && existsPkg == false;t++)
1077 if(packages[t]==pkg)
1084 packages.push_back(pkg);
1088 boxes.push_back(it->first);
1089 if(mod->isExecutable())
1091 execBoxes.push_back(it->first);
1094 else if(type==GCONNECTOR)
1096 connections.push_back(it->first);
1100 std::string script = "";
1102 for(i = 0; i<packages.size();i++)
1105 script+=packages[i];
1109 script+="include std\n";
1111 // Definition of a complex box
1116 script+="author \" ";
1120 script+="description \" ";
1121 script+=cbDescription;
1125 for(i = 0; i<boxes.size();i++)
1129 GObjectController *control = _controllers[id];
1130 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1132 script+=model->getBBTKType();
1134 script+=model->getBBTKName();
1137 std::vector<GPortModel*> inputs = model->getInputPorts();
1138 for(int j = 0; j<inputs.size();j++)
1140 GPortModel* inputPort = inputs[j];
1141 if(inputPort->isValueSet())
1144 script+=model->getBBTKName();
1146 script+=inputPort->getBBTKName();
1148 script+=inputPort->getValue();
1155 // Create connections in the script. If the connection is made with a complex port, it is created the input or output
1157 std::string complexInputs="";
1158 std::string complexOutputs="";
1160 for(i = 0; i<connections.size();i++)
1162 int id = connections[i];
1163 GObjectController *control = _controllers[id];
1164 GConnectorModel *model = (GConnectorModel*)control->getModel();
1167 GPortModel *start = model->getStartPort();
1168 GBoxModel *startBox =start->getParentBox();
1170 GPortModel *end = model->getEndPort();
1171 GBoxModel *endBox =end->getParentBox();
1173 if(startBox->getGObjectType()==GCOMPLEXINPUTPORT)
1175 complexInputs+="input ";
1176 complexInputs+=startBox->getBBTKName();
1179 complexInputs+=endBox->getBBTKName();
1181 complexInputs+=end->getBBTKName();
1184 complexInputs+="\" \"";
1186 complexInputs+="\n";
1188 else if(endBox->getGObjectType()==GCOMPLEXOUTPUTPORT)
1190 complexOutputs+="output ";
1191 complexOutputs+=endBox->getBBTKName();
1193 complexOutputs+=" ";
1194 complexOutputs+=startBox->getBBTKName();
1195 complexOutputs+=".";
1196 complexOutputs+=start->getBBTKName();
1198 complexOutputs+=" ";
1199 complexOutputs+="\" \"";
1201 complexOutputs+="\n";
1206 script+=startBox->getBBTKName();
1208 script+=start->getBBTKName();
1212 //End Connection info
1213 script+=endBox->getBBTKName();
1215 script+=end->getBBTKName();
1221 for(i = 0; i<execBoxes.size();i++)
1224 int id = execBoxes[i];
1225 GObjectController *control = _controllers[id];
1226 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1228 script+=model->getBBTKName();
1233 script+="# Complex input ports\n";
1234 script+=complexInputs;
1237 script+="# Complex output ports\n";
1238 script+=complexOutputs;
1247 //=========================================================================
1249 void wxVtkSceneManager::deleteAllBoxes()
1251 std::map<int, GObjectController*>::iterator it;
1252 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1254 GObjectController *cont = it->second;
1255 cont->removeFromScene();
1256 unregisterController((InteractorStyleMaracas*)cont);
1258 _selectedObjects.clear();
1259 _controllers.clear();
1263 //=========================================================================
1265 void wxVtkSceneManager::refreshScene()
1267 _baseView->RefreshView();
1270 //=========================================================================
1272 void wxVtkSceneManager::centerView()
1275 _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1276 _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0,0,temp[2]);
1277 _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1278 _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0,0,temp[2]);
1279 _baseView->RefreshView();
1283 //=========================================================================
1285 void wxVtkSceneManager::saveDiagram(std::string &content)
1289 std::vector<int> boxes = getBoxes();
1290 int bsize = boxes.size();
1292 sprintf (buffer, "%d", bsize);
1296 for(int i = 0;i<bsize;i++)
1299 GObjectController *cont = _controllers[id];
1300 cont->getModel()->save(content);
1304 std::vector<int> connections = getConnections();
1305 int csize = connections.size();
1306 content+="CONNECTIONS:";
1307 sprintf (buffer, "%d", csize);
1311 for(int i = 0;i<csize;i++)
1313 int id = connections[i];
1314 GObjectController *cont = _controllers[id];
1315 cont->getModel()->save(content);
1320 //=========================================================================
1322 void wxVtkSceneManager::loadDiagram(ifstream &inputStream)
1325 std::string line="";
1326 char delims[] = ":";
1327 char *result = NULL;
1328 getline(inputStream,line);
1331 while ( !inputStream.eof() )
1333 if(line=="" || line[0]=='#')
1335 getline(inputStream,line);
1337 else if(line=="APP_START")
1347 getline(inputStream,line);//BOXES:num
1349 strcpy( boxes, line.c_str() );
1350 result = strtok( boxes, delims );
1351 result = strtok( NULL, delims );
1354 std::istringstream is(result);
1357 for(int i = 0;i<numBoxes;i++)
1360 getline(inputStream,line);//BOX
1361 getline(inputStream,line);//package:type:name
1363 strcpy( box, line.c_str() );
1364 result = strtok( box, delims );//package
1365 std::string package(result);
1366 result = strtok( NULL, delims );//type
1367 std::string type(result);
1368 result = strtok( NULL, delims );//name
1369 std::string name(result);
1371 getline(inputStream,line);//ISEXEC:TRUE|FALSE
1373 strcpy( exec, line.c_str() );
1374 result = strtok( exec, delims );//ISEXEC
1375 result = strtok( NULL, delims );//TRUE|FALSE
1376 std::string isExec(result);
1379 getline(inputStream,line);//xInic:yInic:zInic
1381 strcpy( coord, line.c_str() );
1382 result = strtok( coord, delims );//xInic
1383 std::string xInic(result);
1384 result = strtok( NULL, delims );//yInic
1385 std::string yInic(result);
1386 result = strtok( NULL, delims );//zInic
1387 std::string zInic(result);
1389 double xIn, yIn, zIn;
1390 std::istringstream xSt(xInic);
1392 std::istringstream ySt(yInic);
1394 std::istringstream zSt(zInic);
1398 getline(inputStream,line);//xEnd:yEnd:zEnd
1399 strcpy( coord, line.c_str() );
1400 result = strtok( coord, delims );//xEnd
1401 std::string xEnd(result);
1402 result = strtok( NULL, delims );//yEnd
1403 std::string yEnd(result);
1404 result = strtok( NULL, delims );//zEnd
1405 std::string zEnd(result);
1407 double xEn, yEn, zEn;
1408 std::istringstream xEt(xEnd);
1410 std::istringstream yEt(yEnd);
1412 std::istringstream zEt(zEnd);
1415 int idBox = createGBlackBox(0,0,package,type);
1416 GObjectController *cont = _controllers[idBox];
1417 GBlackBoxModel *bbmod = (GBlackBoxModel*)cont->getModel();
1418 bbmod->setBBTKName(name);
1419 bbmod->setInicPoint(xIn,yIn,zIn);
1420 bbmod->setFinalPoint(xEn,yEn,zEn);
1423 bbmod->setExecutable(true);
1425 else if(isExec=="FALSE")
1427 bbmod->setExecutable(false);
1431 getline(inputStream,line);//PORT o FIN_BOX
1432 std::string port=line.substr(0,4);
1435 getline(inputStream,line);//name:value
1437 strcpy( poort, line.c_str() );
1438 result = strtok( poort, delims );//name
1439 std::string name(result);
1440 result = strtok( NULL, delims );//value
1441 std::string value(result);
1443 bbmod->setValueToInput(name,value);
1445 getline(inputStream,line);//PORT o FIN_BOX
1446 port=line.substr(0,4);
1449 bbmod->notifyObservers(_idManager);
1454 getline(inputStream,line);//CONNECTIONS:num
1456 strcpy( conns, line.c_str() );
1457 result = strtok( conns, delims );
1458 result = strtok( NULL, delims );
1461 std::istringstream isCons(result);
1464 for(int i = 0;i<numConns;i++)
1467 getline(inputStream,line);//CONNECTION
1468 getline(inputStream,line);//Startbox.PortName:EndBox.PortName
1471 strcpy( connec, line.c_str() );
1472 result = strtok( connec, delims );
1473 std::string nameStartBox(result);
1474 result = strtok( NULL, delims );
1475 std::string nameStartPort(result);
1476 result = strtok( NULL, delims );
1477 std::string nameEndBox(result);
1478 result = strtok( NULL, delims );
1479 std::string nameEndPort(result);
1481 std::vector<int> lstB = getBoxes();
1483 GPortModel *startP=NULL;
1484 GPortModel *endP=NULL;
1485 for(int j = 0;j<lstB.size();j++)
1488 GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1489 if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1491 startP = bbMod->getOutputPort(nameStartPort);
1493 else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1495 endP = bbMod->getInputPort(nameEndPort);
1499 int idCon = createGConnector(startP);
1500 _worldState = NOTHING_HAPPENS;
1501 GConnectorController *tempp = (GConnectorController*)_controllers[idCon];
1503 GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1504 vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1505 tempp->endContourCreation();
1506 conMod->setEndPort(endP);
1507 conView->updateStartEndPoints();
1514 //=========================================================================
1516 std::vector<int> wxVtkSceneManager::getBoxes()
1518 std::vector<int> vect;
1519 std::map<int, GObjectController*>::iterator it;
1520 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1522 GObjectController *cont = it->second;
1523 if(cont->getGObjectType()==GBLACKBOX)
1525 vect.push_back(cont->getId());
1531 //=========================================================================
1533 std::vector<int> wxVtkSceneManager::getConnections()
1535 std::vector<int> vect;
1536 std::map<int, GObjectController*>::iterator it;
1537 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1539 GObjectController *cont = it->second;
1540 if(cont->getGObjectType()==GCONNECTOR)
1542 vect.push_back(cont->getId());
1548 //=========================================================================
1550 bool wxVtkSceneManager::isComplexBox()
1552 return _isComplexBox;
1555 //=========================================================================
1557 void wxVtkSceneManager::setComplexBox(bool val)
1562 //=========================================================================
1564 } // EO namespace bbtk