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);
225 model->setBBTKType(bbtkType);
226 model->setBBTKName(bbtkName);
228 model->addObserver(view);
229 model->addObserver(this);
231 //Associates the view with the correspondent renderer and the model.
232 //(NOTE: Refresh is only made by the view)
233 view->setModel(model);
234 view->setBaseView(_baseView);
235 view->initVtkObjects();
237 //Associates the controller with the correspondent model and view
238 controller->setModelAndView(model,view);
240 model->notifyObservers(_idManager);
242 //Register the controller of the new object
243 registerController((InteractorStyleMaracas*) controller);
245 int newId = _controllers.size();
246 controller->setId(newId);
247 _controllers[newId] = controller;
249 return (GPortController*)controller;
252 //=========================================================================
254 void wxVtkSceneManager::createGConnector(GPortModel* startPort)
256 int type = GCONNECTOR;
258 manualConnectorContourController* manContourControl = new manualConnectorContourController();
259 manualConnectorContourView* manContourView = new manualConnectorContourView();
260 manualContourModel* manContourModel = new manualContourModel();
262 GConnectorController* connectorcontroller = new GConnectorController();
263 GConnectorModel* connectorModel = new GConnectorModel();
264 GConnectorView* connectorView = new GConnectorView();
265 connectorModel->setGObjectType(type);
267 manContourModel->SetCloseContour(false);
268 connectorModel->setStartPort(startPort);
270 manContourView->SetModel( manContourModel );
271 manContourView->SetWxVtkBaseView( _baseView );
272 manContourView->SetRange( 0.5 );
273 manContourView->SetZ( 900 );
275 manContourView->SetColorNormalContour(0, 0, 1);
276 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
277 manContourView->SetColorSelectContour(1, 0.8, 0);
278 manContourView->SetWidthLine(1);
280 manContourControl->SetModelView( manContourModel , manContourView );
282 manContourControl->CreateNewManualContour();
284 manContourView->RefreshContour();
288 connectorModel->getInicPoint(x,y,z);
290 manContourControl->SetState(1);
291 manContourModel->SetCloseContour(false);
293 manContourModel->AddPoint(x,y,z);
294 manContourView->AddPoint();
296 manContourModel->AddPoint(x,y,z);
297 manContourView->AddPoint();
299 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
300 manContourControl->_bakIdPoint=bak;
301 manContourView->Refresh();
303 manContourControl->SetMoving( false );
305 registerController((InteractorStyleMaracas*) connectorcontroller);
307 connectorcontroller->setManualContourController(manContourControl);
308 connectorModel->setManualContourModel(manContourModel);
309 connectorView->setManualContourView(manContourView);
310 connectorView->setModel(connectorModel);
311 connectorcontroller->setModelAndView(connectorModel,connectorView);
313 int newId = _controllers.size();
314 connectorcontroller->setId(newId);
315 _controllers[newId] = connectorcontroller;
319 //=========================================================================
321 void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
323 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
324 baseViewControlManager->AddInteractorStyleMaracas( param );
327 //=========================================================================
329 void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
331 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
332 baseViewControlManager->RemoveInteractorStyleMaracas( param );
335 //=========================================================================
337 vtkRenderer* wxVtkSceneManager::getRenderer()
339 return _baseView->GetRenderer();
342 //=========================================================================
344 vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
346 return _baseView->GetRenWin();
348 //=========================================================================
351 void wxVtkSceneManager::update(int idController,int command)
353 if(command != NO_COMMAND)
355 if(command == ADD_TO_SELECTED)
357 GObjectController* cont = _controllers[idController];
360 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
362 int id = _selectedObjects[i];
370 int id = idController;
371 _selectedObjects.push_back(id);
375 else if(command == INIT_CREATION_CONTOUR)
377 _worldState = INIT_CREATION_CONTOUR;
378 GObjectController* cont = _controllers[idController];
379 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
382 // The last one is the controller of the connector
383 for(int i=0;i<_controllers.size();i++)
385 GObjectController* cont = _controllers[i];
386 if(cont->getGObjectType() == GPORT )
388 GPortModel* port = (GPortModel*)cont->getModel();
389 if(port->getPortType()==GINPUTPORT)
391 cont->SetActive(true);
395 cont->getView()->setState(NOTHING_HAPPENS);
396 cont->getModel()->notifyObservers(_idManager);
397 cont->SetActive(false);
402 cont->getView()->setState(NOTHING_HAPPENS);
403 cont->getModel()->notifyObservers(_idManager);
404 cont->SetActive(false);
408 _selectedObjects.clear();
410 createGConnector(startOutputPort);
413 else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
415 _worldState = NOTHING_HAPPENS;
416 int id = _controllers.size()-1;
417 GObjectController* cont = _controllers[id];
418 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
420 GObjectController* finPort = _controllers[idController];
421 if(finPort->getGObjectType() == GPORT)
423 GPortModel* modelPort = (GPortModel*)finPort->getModel();
424 modelContour->setEndPort(modelPort);
427 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();
428 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
431 for(int i=0;i<_controllers.size();i++)
433 GObjectController* cont = _controllers[i];
434 if(cont->getView()!=NULL)
436 cont->getView()->setState(NOTHING_HAPPENS);
437 cont->getModel()->notifyObservers(_idManager);
439 cont->SetActive(true);
446 //=========================================================================
448 bool wxVtkSceneManager::OnMouseMove()
451 wxVTKRenderWindowInteractor *wxVTKiren;
452 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
453 wxVTKiren->GetEventPosition(X,Y);
456 if(_worldState == DRAG_OBJECTS)
458 for (int i=0; i<_selectedObjects.size(); i++)
460 int id = _selectedObjects[i];
461 GObjectController* cont = _controllers[id];
464 cont->getView()->setStartDragging(true);
466 cont->getView()->moveObject(X,Y);
467 cont->getView()->setState(DRAG);
468 cont->getModel()->notifyObservers(_idManager);
471 std::map<int, GObjectController*>::iterator it;
473 for(it = _controllers.begin(); it != _controllers.end(); ++it)
475 GObjectController *desc = it->second;
476 if(desc->getGObjectType()==GCONNECTOR)
478 GConnectorView* vconn = (GConnectorView*)desc->getView();
479 vconn->updateStartEndPoints();
483 _startDragging=false;
486 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
488 std::map<int, GObjectController*>::iterator it;
490 for(it = _controllers.begin(); it != _controllers.end(); ++it)
492 GObjectController *desc = it->second;
493 int type = desc->getGObjectType();
494 int state = desc->getView()->getState();
496 if(state == HIGHLIGHTED){
498 updateStatusBar(desc->getStatusText());
501 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
502 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
514 //=========================================================================
516 bool wxVtkSceneManager::OnLeftButtonDown()
519 if(_selectedObjects.size()!=0)
521 _worldState = DRAG_OBJECTS;
522 _startDragging = true;
524 for (int i = 0; i < _selectedObjects.size(); i++)
526 int id = _selectedObjects[i];
527 GObjectController* cont = _controllers[id];
528 cont->getView()->setState(DRAG);
529 cont->getModel()->notifyObservers(_idManager);
538 //=========================================================================
540 bool wxVtkSceneManager::OnLeftButtonUp()
542 if(_worldState == DRAG_OBJECTS)
544 _worldState = NOTHING_HAPPENS;
546 for (int i = 0; i < _selectedObjects.size(); i++)
548 int id = _selectedObjects[i];
549 GObjectController* cont = _controllers[id];
550 cont->getView()->setState(SELECTED);
551 cont->getModel()->notifyObservers(_idManager);
557 //=========================================================================
559 bool wxVtkSceneManager::OnRightButtonUp()
561 for (int i = 0; i < _selectedObjects.size(); i++)
563 int id = _selectedObjects[i];
564 GObjectController* cont = _controllers[id];
565 cont->SetActive(true);
566 cont->getView()->setState(NOTHING_HAPPENS);
567 cont->getModel()->notifyObservers(_idManager);
570 _selectedObjects.clear();
575 //=========================================================================
577 bool wxVtkSceneManager::OnLeftDClick()
580 wxVTKRenderWindowInteractor *wxVTKiren;
581 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
582 wxVTKiren->GetEventPosition(X,Y);
584 std::map<int, GObjectController*>::iterator it;
586 bool clickOnObject = false;
588 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
590 GObjectController *cont = it->second;
591 int type = cont->getGObjectType();
593 if(cont->getView()->isPointInside(X,Y))
597 for (int i=0; i<_selectedObjects.size(); i++)
599 int id = _selectedObjects[i];
600 GObjectController* control = _controllers[id];
601 control->getView()->setState(NOTHING_HAPPENS);
603 _selectedObjects.clear();
605 GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
606 _parent->editBlackBox(bbmodel);
608 clickOnObject = true;
612 if(clickOnObject==false)
614 _parent->editDiagramParameters(this);
620 //=========================================================================
622 bool wxVtkSceneManager::OnChar()
624 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
626 // KeyCode 127 : Delete Key
627 // KeyCode 8 : Backspace Key
628 if(keyCode == 8 || keyCode == 127)
630 if(_selectedObjects.size()>0)
632 for(int i=0;i<_selectedObjects.size();i++)
634 int id = _selectedObjects[i];
637 _selectedObjects.clear();
644 //=========================================================================
646 void wxVtkSceneManager::removeObject(int id)
648 GObjectController *control = _controllers[id];
649 std::vector<int> controllersToRemove;
651 if(control->getGObjectType()==GBLACKBOX)
653 GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
654 std::vector<GPortModel*> inputs = bbmod->getInputPorts();
656 for(int i = 0;i<inputs.size();i++)
658 controllersToRemove.push_back(inputs[i]->getObjectId());
661 std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
663 for(int i = 0;i<outputs.size();i++)
665 controllersToRemove.push_back(outputs[i]->getObjectId());
668 controllersToRemove.push_back(bbmod->getObjectId());
671 for(int i = 0;i<controllersToRemove.size();i++)
673 int id = controllersToRemove[i];
674 GObjectController *cont = _controllers[id];
675 cont->removeFromScene();
676 unregisterController((InteractorStyleMaracas*)cont);
677 _controllers.erase(id);
683 //=========================================================================
685 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
687 _parent->displayBlackBoxInfo(packageName,boxName);
690 //=========================================================================
692 void wxVtkSceneManager::updateStatusBar(std::string textStatus)
694 _parent->updateStatusBar(textStatus);
697 //=========================================================================
699 std::string wxVtkSceneManager::getDiagramScript()
701 bool existsExec=false;
703 std::vector<std::string> packages;
704 std::vector<int> boxes;
705 std::vector<int> connections;
706 std::vector<int> execBoxes;
708 std::map<int, GObjectController*>::iterator it;
710 for(it = _controllers.begin(); it != _controllers.end(); ++it)
712 GObjectController *desc = it->second;
713 int type = desc->getGObjectType();
717 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
719 std::string pkg = mod->getBBTKPackage();
720 bool existsPkg = false;
721 for(int t = 0;t<packages.size() && existsPkg == false;t++)
730 packages.push_back(pkg);
734 boxes.push_back(it->first);
735 if(mod->isExecutable())
737 execBoxes.push_back(it->first);
741 else if(type==GCONNECTOR)
743 connections.push_back(it->first);
747 std::string script = "";
748 script+="# BBTK GEditor Script\n";
749 script+="# ----------------------\n";
753 for(i = 0; i<packages.size();i++)
760 script+="include std\n";
762 for(i = 0; i<boxes.size();i++)
766 GObjectController *control = _controllers[id];
767 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
769 script+=model->getBBTKType();
771 script+=model->getBBTKName();
774 std::vector<GPortModel*> inputs = model->getInputPorts();
775 for(int j = 0; j<inputs.size();j++)
777 GPortModel* inputPort = inputs[j];
778 if(inputPort->isValueSet())
781 script+=model->getBBTKName();
783 script+=inputPort->getBBTKName();
785 script+=inputPort->getValue();
792 for(i = 0; i<connections.size();i++)
795 int id = connections[i];
796 GObjectController *control = _controllers[id];
797 GConnectorModel *model = (GConnectorModel*)control->getModel();
799 //Start Connection info
800 GPortModel *start = model->getStartPort();
801 script+=start->getParentBox()->getBBTKName();
803 script+=start->getBBTKName();
807 //End Connection info
808 GPortModel *end = model->getEndPort();
809 script+=end->getParentBox()->getBBTKName();
811 script+=end->getBBTKName();
816 for(i = 0; i<execBoxes.size();i++)
819 int id = execBoxes[i];
820 GObjectController *control = _controllers[id];
821 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
823 script+=model->getBBTKName();
832 //=========================================================================
834 } // EO namespace bbtk