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 void wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
104 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
105 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
107 int type = GBLACKBOX;
109 //Create the MVC Objects
111 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
112 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
113 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
115 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
117 //Prepares the initial model
118 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height
121 double yy = windowHeight-y;
123 //z value is not important yet, because it is only used a parallel projection
126 _baseView->TransCoordScreenToWorld(xx,yy,zz);
127 model->setInicPoint(xx,yy,zz);
130 std::stringstream stream;
134 stream << "Box0" << _numBoxes;
138 stream << "Box" << _numBoxes;
140 std::string arraystring = stream.str();
142 model->setBBTKName(arraystring);
143 model->setBBTKType(boxType);
144 model->setBBTKPackage(packageName);
146 model->addObserver(view);
147 model->addObserver(this);
149 //Iterate and create the input ports
150 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
151 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
154 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
156 BlackBoxInputDescriptor *desc = itInput->second;
157 createGInputPort(GINPUTPORT,i,model,desc);
161 //Iterate and create the output ports
162 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
163 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
166 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
168 BlackBoxOutputDescriptor *desc = itOutput->second;
169 createGOutputPort(GOUTPUTPORT,i,model,desc);
174 //Associates the view with the correspondent renderer and the model.
175 //(NOTE: Refresh is only made by the view)
176 view->setModel(model);
177 view->setBaseView(_baseView);
178 view->initVtkObjects();
180 //Associates the controller with the correspondent model and view
181 controller->setModelAndView(model,view);
183 //Resgiter change to the observers of the actual model
184 model->notifyObservers(_idManager);
186 //Register the controller of the new object
187 registerController((InteractorStyleMaracas*) controller);
189 //Add the object to the objects list
190 int newId = _controllers.size();
191 controller->setId(newId);
192 _controllers[newId] = controller;
196 //=========================================================================
198 void wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
200 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
201 blackBox->addInputPort((GPortModel*)portController->getModel());
204 //=========================================================================
206 void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
208 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
209 blackBox->addOutputPort((GPortModel*)portController->getModel());
212 //=========================================================================
214 GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBlackBoxModel *blackBox)
218 //Create the MVC Objects
219 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
220 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
221 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
223 model->registerInBox(blackBox,portType, posInBox);
224 blackBox->addOutputPort(model);
226 model->setBBTKType(bbtkType);
227 model->setBBTKName(bbtkName);
229 model->addObserver(view);
230 model->addObserver(this);
232 //Associates the view with the correspondent renderer and the model.
233 //(NOTE: Refresh is only made by the view)
234 view->setModel(model);
235 view->setBaseView(_baseView);
236 view->initVtkObjects();
238 //Associates the controller with the correspondent model and view
239 controller->setModelAndView(model,view);
241 model->notifyObservers(_idManager);
243 //Register the controller of the new object
244 registerController((InteractorStyleMaracas*) controller);
246 int newId = _controllers.size();
247 controller->setId(newId);
248 _controllers[newId] = controller;
250 return (GPortController*)controller;
253 //=========================================================================
255 void wxVtkSceneManager::createGConnector(GPortModel* startPort)
257 int type = GCONNECTOR;
259 manualConnectorContourController* manContourControl = new manualConnectorContourController();
260 manualConnectorContourView* manContourView = new manualConnectorContourView();
261 manualContourModel* manContourModel = new manualContourModel();
263 GConnectorController* connectorcontroller = new GConnectorController();
264 GConnectorModel* connectorModel = new GConnectorModel();
265 GConnectorView* connectorView = new GConnectorView();
266 connectorModel->setGObjectType(type);
268 manContourModel->SetCloseContour(false);
269 connectorModel->setStartPort(startPort);
271 manContourView->SetModel( manContourModel );
272 manContourView->SetWxVtkBaseView( _baseView );
273 manContourView->SetRange( 0.5 );
274 manContourView->SetZ( 900 );
276 manContourView->SetColorNormalContour(0, 0, 1);
277 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
278 manContourView->SetColorSelectContour(1, 0.8, 0);
279 manContourView->SetWidthLine(1);
281 manContourControl->SetModelView( manContourModel , manContourView );
283 manContourControl->CreateNewManualContour();
285 manContourView->RefreshContour();
289 connectorModel->getInicPoint(x,y,z);
291 manContourControl->SetState(1);
292 manContourModel->SetCloseContour(false);
294 manContourModel->AddPoint(x,y,z);
295 manContourView->AddPoint();
297 manContourModel->AddPoint(x,y,z);
298 manContourView->AddPoint();
300 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
301 manContourControl->_bakIdPoint=bak;
302 manContourView->Refresh();
304 manContourControl->SetMoving( false );
306 registerController((InteractorStyleMaracas*) connectorcontroller);
308 connectorcontroller->setManualContourController(manContourControl);
309 connectorModel->setManualContourModel(manContourModel);
310 connectorView->setManualContourView(manContourView);
311 connectorView->setModel(connectorModel);
312 connectorcontroller->setModelAndView(connectorModel,connectorView);
314 int newId = _controllers.size();
315 connectorcontroller->setId(newId);
316 _controllers[newId] = connectorcontroller;
320 //=========================================================================
322 void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
324 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
325 baseViewControlManager->AddInteractorStyleMaracas( param );
328 //=========================================================================
330 void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
332 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
333 baseViewControlManager->RemoveInteractorStyleMaracas( param );
336 //=========================================================================
338 vtkRenderer* wxVtkSceneManager::getRenderer()
340 return _baseView->GetRenderer();
343 //=========================================================================
345 vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
347 return _baseView->GetRenWin();
349 //=========================================================================
352 void wxVtkSceneManager::update(int idController,int command)
354 if(command != NO_COMMAND)
356 if(command == ADD_TO_SELECTED)
358 GObjectController* cont = _controllers[idController];
361 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
363 int id = _selectedObjects[i];
371 int id = idController;
372 _selectedObjects.push_back(id);
376 else if(command == INIT_CREATION_CONTOUR)
378 _worldState = INIT_CREATION_CONTOUR;
379 GObjectController* cont = _controllers[idController];
380 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
383 // The last one is the controller of the connector
384 for(int i=0;i<_controllers.size();i++)
386 GObjectController* cont = _controllers[i];
387 if(cont->getGObjectType() == GPORT )
389 GPortModel* port = (GPortModel*)cont->getModel();
390 if(port->getPortType()==GINPUTPORT)
392 cont->SetActive(true);
396 cont->getView()->setState(NOTHING_HAPPENS);
397 cont->getModel()->notifyObservers(_idManager);
398 cont->SetActive(false);
403 cont->getView()->setState(NOTHING_HAPPENS);
404 cont->getModel()->notifyObservers(_idManager);
405 cont->SetActive(false);
409 _selectedObjects.clear();
411 createGConnector(startOutputPort);
414 else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
416 _worldState = NOTHING_HAPPENS;
417 int id = _controllers.size()-1;
418 GObjectController* cont = _controllers[id];
419 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
421 GObjectController* finPort = _controllers[idController];
422 if(finPort->getGObjectType() == GPORT)
424 GPortModel* modelPort = (GPortModel*)finPort->getModel();
425 modelContour->setEndPort(modelPort);
428 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();
429 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
432 for(int i=0;i<_controllers.size();i++)
434 GObjectController* cont = _controllers[i];
435 if(cont->getView()!=NULL)
437 cont->getView()->setState(NOTHING_HAPPENS);
438 cont->getModel()->notifyObservers(_idManager);
440 cont->SetActive(true);
447 //=========================================================================
449 bool wxVtkSceneManager::OnMouseMove()
452 wxVTKRenderWindowInteractor *wxVTKiren;
453 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
454 wxVTKiren->GetEventPosition(X,Y);
457 if(_worldState == DRAG_OBJECTS)
459 for (int i=0; i<_selectedObjects.size(); i++)
461 int id = _selectedObjects[i];
462 GObjectController* cont = _controllers[id];
465 cont->getView()->setStartDragging(true);
467 cont->getView()->moveObject(X,Y);
468 cont->getView()->setState(DRAG);
469 cont->getModel()->notifyObservers(_idManager);
472 std::map<int, GObjectController*>::iterator it;
474 for(it = _controllers.begin(); it != _controllers.end(); ++it)
476 GObjectController *desc = it->second;
477 if(desc->getGObjectType()==GCONNECTOR)
479 GConnectorView* vconn = (GConnectorView*)desc->getView();
480 vconn->updateStartEndPoints();
484 _startDragging=false;
487 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
489 std::map<int, GObjectController*>::iterator it;
491 for(it = _controllers.begin(); it != _controllers.end(); ++it)
493 GObjectController *desc = it->second;
494 int type = desc->getGObjectType();
495 int state = desc->getView()->getState();
497 if(state == HIGHLIGHTED){
499 updateStatusBar(desc->getStatusText());
502 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
503 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
515 //=========================================================================
517 bool wxVtkSceneManager::OnLeftButtonDown()
520 if(_selectedObjects.size()!=0)
522 _worldState = DRAG_OBJECTS;
523 _startDragging = true;
525 for (int i = 0; i < _selectedObjects.size(); i++)
527 int id = _selectedObjects[i];
528 GObjectController* cont = _controllers[id];
529 cont->getView()->setState(DRAG);
530 cont->getModel()->notifyObservers(_idManager);
539 //=========================================================================
541 bool wxVtkSceneManager::OnLeftButtonUp()
543 if(_worldState == DRAG_OBJECTS)
545 _worldState = NOTHING_HAPPENS;
547 for (int i = 0; i < _selectedObjects.size(); i++)
549 int id = _selectedObjects[i];
550 GObjectController* cont = _controllers[id];
551 cont->getView()->setState(SELECTED);
552 cont->getModel()->notifyObservers(_idManager);
558 //=========================================================================
560 bool wxVtkSceneManager::OnRightButtonUp()
562 for (int i = 0; i < _selectedObjects.size(); i++)
564 int id = _selectedObjects[i];
565 GObjectController* cont = _controllers[id];
566 cont->SetActive(true);
567 cont->getView()->setState(NOTHING_HAPPENS);
568 cont->getModel()->notifyObservers(_idManager);
571 _selectedObjects.clear();
576 //=========================================================================
578 bool wxVtkSceneManager::OnLeftDClick()
581 wxVTKRenderWindowInteractor *wxVTKiren;
582 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
583 wxVTKiren->GetEventPosition(X,Y);
585 std::map<int, GObjectController*>::iterator it;
587 bool clickOnObject = false;
589 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
591 GObjectController *cont = it->second;
592 int type = cont->getGObjectType();
594 if(cont->getView()->isPointInside(X,Y))
598 for (int i=0; i<_selectedObjects.size(); i++)
600 int id = _selectedObjects[i];
601 GObjectController* control = _controllers[id];
602 control->getView()->setState(NOTHING_HAPPENS);
604 _selectedObjects.clear();
606 GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
607 _parent->editBlackBox(bbmodel);
609 clickOnObject = true;
613 if(clickOnObject==false)
615 _parent->editDiagramParameters(this);
621 //=========================================================================
623 bool wxVtkSceneManager::OnChar()
625 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
627 // KeyCode 127 : Delete Key
628 // KeyCode 8 : Backspace Key
629 if(keyCode == 8 || keyCode == 127)
631 if(_selectedObjects.size()>0)
633 for(int i=0;i<_selectedObjects.size();i++)
635 int id = _selectedObjects[i];
638 _selectedObjects.clear();
645 //=========================================================================
647 void wxVtkSceneManager::removeObject(int id)
649 GObjectController *control = _controllers[id];
650 std::vector<int> controllersToRemove;
652 if(control->getGObjectType()==GBLACKBOX)
654 GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
655 std::vector<GPortModel*>inputs = bbmod->getInputPorts();
657 for(int i = 0;i<inputs.size();i++)
659 controllersToRemove.push_back(inputs[i]->getObjectId());
662 std::vector<GPortModel*>outputs = bbmod->getOutputPorts();
664 for(int i = 0;i<outputs.size();i++)
666 controllersToRemove.push_back(outputs[i]->getObjectId());
669 controllersToRemove.push_back(control->getId());
672 for(int i = 0;i<controllersToRemove.size();i++)
674 int id = controllersToRemove[i];
675 GObjectController *cont = _controllers[id];
676 cont->removeFromScene();
677 unregisterController((InteractorStyleMaracas*)cont);
678 _controllers.erase(id);
684 //=========================================================================
686 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
688 _parent->displayBlackBoxInfo(packageName,boxName);
691 //=========================================================================
693 void wxVtkSceneManager::updateStatusBar(std::string textStatus)
695 _parent->updateStatusBar(textStatus);
698 //=========================================================================
700 std::string wxVtkSceneManager::getDiagramScript()
702 bool existsExec=false;
704 std::vector<std::string> packages;
705 std::vector<int> boxes;
706 std::vector<int> connections;
707 std::vector<int> execBoxes;
709 std::map<int, GObjectController*>::iterator it;
711 for(it = _controllers.begin(); it != _controllers.end(); ++it)
713 GObjectController *desc = it->second;
714 int type = desc->getGObjectType();
718 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
720 std::string pkg = mod->getBBTKPackage();
721 bool existsPkg = false;
722 for(int t = 0;t<packages.size() && existsPkg == false;t++)
731 packages.push_back(pkg);
735 boxes.push_back(it->first);
736 if(mod->isExecutable())
738 execBoxes.push_back(it->first);
742 else if(type==GCONNECTOR)
744 connections.push_back(it->first);
748 std::string script = "";
749 script+="# BBTK GEditor Script\n";
750 script+="# ----------------------\n";
754 for(i = 0; i<packages.size();i++)
761 script+="load std\n";
763 for(i = 0; i<boxes.size();i++)
767 GObjectController *control = _controllers[id];
768 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
770 script+=model->getBBTKType();
772 script+=model->getBBTKName();
775 std::vector<GPortModel*> inputs = model->getInputPorts();
776 for(int j = 0; j<inputs.size();j++)
778 GPortModel* inputPort = inputs[j];
779 if(inputPort->isValueSet())
782 script+=model->getBBTKName();
784 script+=inputPort->getBBTKName();
786 script+=inputPort->getValue();
793 for(i = 0; i<connections.size();i++)
796 int id = connections[i];
797 GObjectController *control = _controllers[id];
798 GConnectorModel *model = (GConnectorModel*)control->getModel();
800 //Start Connection info
801 GPortModel *start = model->getStartPort();
802 script+=start->getParentBox()->getBBTKName();
804 script+=start->getBBTKName();
808 //End Connection info
809 GPortModel *end = model->getEndPort();
810 script+=end->getParentBox()->getBBTKName();
812 script+=end->getBBTKName();
817 for(i = 0; i<execBoxes.size();i++)
820 int id = execBoxes[i];
821 GObjectController *control = _controllers[id];
822 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
824 script+=model->getBBTKName();
833 //=========================================================================
835 } // EO namespace bbtk