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)
54 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);
56 _worldState=NOTHING_HAPPENS;
57 registerController(this);
63 //=========================================================================
65 void wxVtkSceneManager::disconnectDrop()
67 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
70 //=========================================================================
72 wxVtkSceneManager::~wxVtkSceneManager()
76 //=========================================================================
78 void wxVtkSceneManager::configureBaseView()
80 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
82 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
84 // Important to activate the 2D interaction system
85 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
86 interactorstylebaseview->SetInteractor ( iren );
87 iren->SetInteractorStyle(interactorstylebaseview);
88 interactorstylebaseview->SetwxVtkBaseView(_baseView);
90 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
91 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
93 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
94 _baseView->GetRenderer()->GradientBackgroundOff();
98 //=========================================================================
100 void 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;
195 //=========================================================================
197 void wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
199 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
200 blackBox->addInputPort((GPortModel*)portController->getModel());
203 //=========================================================================
205 void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
207 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
208 blackBox->addOutputPort((GPortModel*)portController->getModel());
211 //=========================================================================
213 GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBlackBoxModel *blackBox)
217 //Create the MVC Objects
218 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
219 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
220 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
222 model->registerInBox(blackBox,portType, posInBox);
224 model->setBBTKType(bbtkType);
225 model->setBBTKName(bbtkName);
227 model->addObserver(view);
228 model->addObserver(this);
230 //Associates the view with the correspondent renderer and the model.
231 //(NOTE: Refresh is only made by the view)
232 view->setModel(model);
233 view->setBaseView(_baseView);
234 view->initVtkObjects();
236 //Associates the controller with the correspondent model and view
237 controller->setModelAndView(model,view);
239 model->notifyObservers(_idManager);
241 //Register the controller of the new object
242 registerController((InteractorStyleMaracas*) controller);
244 int newId = _controllers.size();
245 controller->setId(newId);
246 _controllers[newId] = controller;
248 return (GPortController*)controller;
251 //=========================================================================
253 void wxVtkSceneManager::createGConnector(GPortModel* startPort)
255 int type = GCONNECTOR;
257 manualConnectorContourController* manContourControl = new manualConnectorContourController();
258 manualConnectorContourView* manContourView = new manualConnectorContourView();
259 manualContourModel* manContourModel = new manualContourModel();
261 GConnectorController* connectorcontroller = new GConnectorController();
262 GConnectorModel* connectorModel = new GConnectorModel();
263 GConnectorView* connectorView = new GConnectorView();
264 connectorModel->setGObjectType(type);
266 manContourModel->SetCloseContour(false);
267 connectorModel->setStartPort(startPort);
269 manContourView->SetModel( manContourModel );
270 manContourView->SetWxVtkBaseView( _baseView );
271 manContourView->SetRange( 0.5 );
272 manContourView->SetZ( 900 );
274 manContourView->SetColorNormalContour(0, 0, 1);
275 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
276 manContourView->SetColorSelectContour(1, 0.8, 0);
277 manContourView->SetWidthLine(1);
279 manContourControl->SetModelView( manContourModel , manContourView );
281 manContourControl->CreateNewManualContour();
283 manContourView->RefreshContour();
287 connectorModel->getInicPoint(x,y,z);
289 manContourControl->SetState(1);
290 manContourModel->SetCloseContour(false);
292 manContourModel->AddPoint(x,y,z);
293 manContourView->AddPoint();
295 manContourModel->AddPoint(x,y,z);
296 manContourView->AddPoint();
298 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
299 manContourControl->_bakIdPoint=bak;
300 manContourView->Refresh();
302 manContourControl->SetMoving( false );
304 registerController((InteractorStyleMaracas*) connectorcontroller);
306 connectorcontroller->setManualContourController(manContourControl);
307 connectorModel->setManualContourModel(manContourModel);
308 connectorView->setManualContourView(manContourView);
309 connectorView->setModel(connectorModel);
310 connectorcontroller->setModelAndView(connectorModel,connectorView);
312 int newId = _controllers.size();
313 connectorcontroller->setId(newId);
314 _controllers[newId] = connectorcontroller;
318 //=========================================================================
320 void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
322 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
323 baseViewControlManager->AddInteractorStyleMaracas( param );
326 //=========================================================================
328 void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
330 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
331 baseViewControlManager->RemoveInteractorStyleMaracas( param );
334 //=========================================================================
336 vtkRenderer* wxVtkSceneManager::getRenderer()
338 return _baseView->GetRenderer();
341 //=========================================================================
343 vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
345 return _baseView->GetRenWin();
347 //=========================================================================
350 void wxVtkSceneManager::update(int idController,int command)
352 if(command != NO_COMMAND)
354 if(command == ADD_TO_SELECTED)
356 GObjectController* cont = _controllers[idController];
359 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
361 int id = _selectedObjects[i];
369 int id = idController;
370 _selectedObjects.push_back(id);
374 else if(command == INIT_CREATION_CONTOUR)
376 _worldState = INIT_CREATION_CONTOUR;
377 GObjectController* cont = _controllers[idController];
378 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
381 // The last one is the controller of the connector
382 for(int i=0;i<_controllers.size();i++)
384 GObjectController* cont = _controllers[i];
385 if(cont->getGObjectType() == GPORT )
387 GPortModel* port = (GPortModel*)cont->getModel();
388 if(port->getPortType()==GINPUTPORT)
390 cont->SetActive(true);
394 cont->getView()->setState(NOTHING_HAPPENS);
395 cont->getModel()->notifyObservers(_idManager);
396 cont->SetActive(false);
401 cont->getView()->setState(NOTHING_HAPPENS);
402 cont->getModel()->notifyObservers(_idManager);
403 cont->SetActive(false);
407 _selectedObjects.clear();
409 createGConnector(startOutputPort);
412 else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
414 _worldState = NOTHING_HAPPENS;
415 int id = _controllers.size()-1;
416 GObjectController* cont = _controllers[id];
417 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
419 GObjectController* finPort = _controllers[idController];
420 if(finPort->getGObjectType() == GPORT)
422 GPortModel* modelPort = (GPortModel*)finPort->getModel();
423 modelContour->setEndPort(modelPort);
426 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();
427 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
430 for(int i=0;i<_controllers.size();i++)
432 GObjectController* cont = _controllers[i];
433 if(cont->getView()!=NULL)
435 cont->getView()->setState(NOTHING_HAPPENS);
436 cont->getModel()->notifyObservers(_idManager);
438 cont->SetActive(true);
445 //=========================================================================
447 bool wxVtkSceneManager::OnMouseMove()
450 wxVTKRenderWindowInteractor *wxVTKiren;
451 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
452 wxVTKiren->GetEventPosition(X,Y);
455 if(_worldState == DRAG_OBJECTS)
457 for (int i=0; i<_selectedObjects.size(); i++)
459 int id = _selectedObjects[i];
460 GObjectController* cont = _controllers[id];
463 cont->getView()->setStartDragging(true);
465 cont->getView()->moveObject(X,Y);
466 cont->getView()->setState(DRAG);
467 cont->getModel()->notifyObservers(_idManager);
470 std::map<int, GObjectController*>::iterator it;
472 for(it = _controllers.begin(); it != _controllers.end(); ++it)
474 GObjectController *desc = it->second;
475 if(desc->getGObjectType()==GCONNECTOR)
477 GConnectorView* vconn = (GConnectorView*)desc->getView();
478 vconn->updateStartEndPoints();
482 _startDragging=false;
485 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
487 std::map<int, GObjectController*>::iterator it;
489 for(it = _controllers.begin(); it != _controllers.end(); ++it)
491 GObjectController *desc = it->second;
492 int type = desc->getGObjectType();
493 int state = desc->getView()->getState();
495 if(state == HIGHLIGHTED){
497 updateStatusBar(desc->getStatusText());
500 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
501 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
513 //=========================================================================
515 bool wxVtkSceneManager::OnLeftButtonDown()
517 if(_worldState==INIT_CREATION_CONTOUR)
519 bool isOverPort=false;
520 std::map<int, GObjectController*>::iterator it;
521 for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
523 GObjectController *desc = it->second;
524 if(desc->getGObjectType()==GPORT)
526 GPortModel* portmod=(GPortModel*)desc->getModel();
527 vtkGObjectView* portView=desc->getView();
528 if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
535 if(isOverPort==false)
537 _worldState=NOTHING_HAPPENS;
538 int lastId = _controllers.size()-1;
539 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
540 connector->removeFromScene();
541 unregisterController(connector);
542 _controllers.erase(lastId);
544 for(it = _controllers.begin(); it != _controllers.end(); ++it)
546 GObjectController *desc = it->second;
547 desc->SetActive(true);
548 desc->getView()->setState(NOTHING_HAPPENS);
549 desc->getModel()->notifyObservers(_idManager);
554 if(_selectedObjects.size()!=0)
556 _worldState = DRAG_OBJECTS;
557 _startDragging = true;
559 for (int i = 0; i < _selectedObjects.size(); i++)
561 int id = _selectedObjects[i];
562 GObjectController* cont = _controllers[id];
563 cont->getView()->setState(DRAG);
564 cont->getModel()->notifyObservers(_idManager);
573 //=========================================================================
575 bool wxVtkSceneManager::OnLeftButtonUp()
577 if(_worldState == DRAG_OBJECTS)
579 _worldState = NOTHING_HAPPENS;
581 for (int i = 0; i < _selectedObjects.size(); i++)
583 int id = _selectedObjects[i];
584 GObjectController* cont = _controllers[id];
585 cont->getView()->setState(SELECTED);
586 cont->getModel()->notifyObservers(_idManager);
592 //=========================================================================
594 bool wxVtkSceneManager::OnRightButtonUp()
596 if(_worldState==INIT_CREATION_CONTOUR)
598 _worldState=NOTHING_HAPPENS;
599 int lastId = _controllers.size()-1;
600 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
601 connector->removeFromScene();
602 unregisterController(connector);
603 _controllers.erase(lastId);
605 std::map<int, GObjectController*>::iterator it;
606 for(it = _controllers.begin(); it != _controllers.end(); ++it)
608 GObjectController *desc = it->second;
609 desc->SetActive(true);
610 desc->getView()->setState(NOTHING_HAPPENS);
611 desc->getModel()->notifyObservers(_idManager);
616 for (int i = 0; i < _selectedObjects.size(); i++)
618 int id = _selectedObjects[i];
619 GObjectController* cont = _controllers[id];
620 cont->SetActive(true);
621 cont->getView()->setState(NOTHING_HAPPENS);
622 cont->getModel()->notifyObservers(_idManager);
625 _selectedObjects.clear();
630 //=========================================================================
632 bool wxVtkSceneManager::OnLeftDClick()
635 wxVTKRenderWindowInteractor *wxVTKiren;
636 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
637 wxVTKiren->GetEventPosition(X,Y);
639 std::map<int, GObjectController*>::iterator it;
641 bool clickOnObject = false;
643 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
645 GObjectController *cont = it->second;
646 int type = cont->getGObjectType();
650 if(cont->getView()->isPointInside(X,Y))
652 for (int i=0; i<_selectedObjects.size(); i++)
654 int id = _selectedObjects[i];
655 GObjectController* control = _controllers[id];
656 control->getView()->setState(NOTHING_HAPPENS);
658 _selectedObjects.clear();
660 GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
661 _parent->editBlackBox(bbmodel);
663 clickOnObject = true;
667 if(clickOnObject==false)
669 _parent->editDiagramParameters(this);
675 //=========================================================================
677 bool wxVtkSceneManager::OnChar()
679 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
681 // KeyCode 127 : Delete Key
682 // KeyCode 8 : Backspace Key
683 if(keyCode == 8 || keyCode == 127)
685 if(_selectedObjects.size()>0)
687 for(int i=0;i<_selectedObjects.size();i++)
689 int id = _selectedObjects[i];
692 _selectedObjects.clear();
699 //=========================================================================
701 void wxVtkSceneManager::removeObject(int id)
703 GObjectController *control = _controllers[id];
704 std::vector<int> controllersToRemove;
706 if(control->getGObjectType()==GBLACKBOX)
708 GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
709 std::vector<GPortModel*> inputs = bbmod->getInputPorts();
711 bool boxConnected = false;
713 // Add box input controllers to be removed
714 for(int i = 0;i<inputs.size();i++)
716 controllersToRemove.push_back(inputs[i]->getObjectId());
717 if(inputs[i]->isConnected())
723 std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
725 // Add box output controllers to be removed
726 for(int i = 0;i<outputs.size();i++)
728 controllersToRemove.push_back(outputs[i]->getObjectId());
729 if(outputs[i]->isConnected())
735 // Add connection controllers to be removed
736 std::map<int, GObjectController*>::iterator it;
737 for(it = _controllers.begin(); it != _controllers.end(); ++it)
739 GObjectController *cont = it->second;
740 int type = cont->getGObjectType();
743 GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
744 if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
746 controllersToRemove.push_back(conMod->getObjectId());
748 if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
750 controllersToRemove.push_back(conMod->getObjectId());
755 // Add box controller to be removed
756 controllersToRemove.push_back(bbmod->getObjectId());
759 for(int i = 0;i<controllersToRemove.size();i++)
761 int id = controllersToRemove[i];
762 GObjectController *cont = _controllers[id];
763 cont->removeFromScene();
764 unregisterController((InteractorStyleMaracas*)cont);
765 _controllers.erase(id);
771 //=========================================================================
773 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
775 _parent->displayBlackBoxInfo(packageName,boxName);
778 //=========================================================================
780 void wxVtkSceneManager::updateStatusBar(std::string textStatus)
782 _parent->updateStatusBar(textStatus);
785 //=========================================================================
787 std::string wxVtkSceneManager::getDiagramScript()
789 bool existsExec=false;
791 std::vector<std::string> packages;
792 std::vector<int> boxes;
793 std::vector<int> connections;
794 std::vector<int> execBoxes;
796 std::map<int, GObjectController*>::iterator it;
798 for(it = _controllers.begin(); it != _controllers.end(); ++it)
800 GObjectController *desc = it->second;
801 int type = desc->getGObjectType();
805 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
807 std::string pkg = mod->getBBTKPackage();
808 bool existsPkg = false;
809 for(int t = 0;t<packages.size() && existsPkg == false;t++)
818 packages.push_back(pkg);
822 boxes.push_back(it->first);
823 if(mod->isExecutable())
825 execBoxes.push_back(it->first);
829 else if(type==GCONNECTOR)
831 connections.push_back(it->first);
835 std::string script = "";
836 script+="# BBTK GEditor Script\n";
837 script+="# ----------------------\n";
841 for(i = 0; i<packages.size();i++)
848 script+="include std\n";
850 for(i = 0; i<boxes.size();i++)
854 GObjectController *control = _controllers[id];
855 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
857 script+=model->getBBTKType();
859 script+=model->getBBTKName();
862 std::vector<GPortModel*> inputs = model->getInputPorts();
863 for(int j = 0; j<inputs.size();j++)
865 GPortModel* inputPort = inputs[j];
866 if(inputPort->isValueSet())
869 script+=model->getBBTKName();
871 script+=inputPort->getBBTKName();
873 script+=inputPort->getValue();
880 for(i = 0; i<connections.size();i++)
883 int id = connections[i];
884 GObjectController *control = _controllers[id];
885 GConnectorModel *model = (GConnectorModel*)control->getModel();
887 //Start Connection info
888 GPortModel *start = model->getStartPort();
889 script+=start->getParentBox()->getBBTKName();
891 script+=start->getBBTKName();
895 //End Connection info
896 GPortModel *end = model->getEndPort();
897 script+=end->getParentBox()->getBBTKName();
899 script+=end->getBBTKName();
904 for(i = 0; i<execBoxes.size();i++)
907 int id = execBoxes[i];
908 GObjectController *control = _controllers[id];
909 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
911 script+=model->getBBTKName();
920 //=========================================================================
922 } // EO namespace bbtk