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 createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
203 //=========================================================================
205 void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
207 createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
210 //=========================================================================
212 void wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBlackBoxModel *blackBox)
216 //Create the MVC Objects
217 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
218 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
219 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
221 model->registerInBox(blackBox,portType, posInBox);
222 blackBox->addOutputPort(model);
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;
249 //=========================================================================
251 void wxVtkSceneManager::createGConnector(GPortModel* startPort)
253 int type = GCONNECTOR;
255 manualConnectorContourController* manContourControl = new manualConnectorContourController();
256 manualConnectorContourView* manContourView = new manualConnectorContourView();
257 manualContourModel* manContourModel = new manualContourModel();
259 GConnectorController* connectorcontroller = new GConnectorController();
260 GConnectorModel* connectorModel = new GConnectorModel();
261 GConnectorView* connectorView = new GConnectorView();
262 connectorModel->setGObjectType(type);
264 manContourModel->SetCloseContour(false);
265 connectorModel->setStartPort(startPort);
267 manContourView->SetModel( manContourModel );
268 manContourView->SetWxVtkBaseView( _baseView );
269 manContourView->SetRange( 0.5 );
270 manContourView->SetZ( 900 );
272 manContourView->SetColorNormalContour(0, 0, 1);
273 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
274 manContourView->SetColorSelectContour(1, 0.8, 0);
275 manContourView->SetWidthLine(1);
277 manContourControl->SetModelView( manContourModel , manContourView );
279 manContourControl->CreateNewManualContour();
281 manContourView->RefreshContour();
285 connectorModel->getInicPoint(x,y,z);
287 manContourControl->SetState(1);
288 manContourModel->SetCloseContour(false);
290 manContourModel->AddPoint(x,y,z);
291 manContourView->AddPoint();
293 manContourModel->AddPoint(x,y,z);
294 manContourView->AddPoint();
296 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
297 manContourControl->_bakIdPoint=bak;
298 manContourView->Refresh();
300 manContourControl->SetMoving( false );
302 registerController((InteractorStyleMaracas*) connectorcontroller);
304 connectorcontroller->setManualContourController(manContourControl);
305 connectorModel->setManualContourModel(manContourModel);
306 connectorView->setManualContourView(manContourView);
307 connectorView->setModel(connectorModel);
308 connectorcontroller->setModelAndView(connectorModel,connectorView);
310 int newId = _controllers.size();
311 connectorcontroller->setId(newId);
312 _controllers[newId] = connectorcontroller;
316 //=========================================================================
318 void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
320 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
321 baseViewControlManager->AddInteractorStyleMaracas( param );
324 //=========================================================================
326 void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
328 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
329 baseViewControlManager->RemoveInteractorStyleMaracas( param );
332 //=========================================================================
334 vtkRenderer* wxVtkSceneManager::getRenderer()
336 return _baseView->GetRenderer();
339 //=========================================================================
341 vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
343 return _baseView->GetRenWin();
345 //=========================================================================
348 void wxVtkSceneManager::update(int idController,int command)
350 if(command != NO_COMMAND)
352 if(command == ADD_TO_SELECTED)
354 GObjectController* cont = _controllers[idController];
357 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
359 int id = _selectedObjects[i];
367 int id = idController;
368 _selectedObjects.push_back(id);
372 else if(command == INIT_CREATION_CONTOUR)
374 _worldState = INIT_CREATION_CONTOUR;
375 GObjectController* cont = _controllers[idController];
376 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
379 // The last one is the controller of the connector
380 for(int i=0;i<_controllers.size();i++)
382 GObjectController* cont = _controllers[i];
383 if(cont->getGObjectType() == GPORT )
385 GPortModel* port = (GPortModel*)cont->getModel();
386 if(port->getPortType()==GINPUTPORT)
388 cont->SetActive(true);
392 cont->getView()->setState(NOTHING_HAPPENS);
393 cont->getModel()->notifyObservers(_idManager);
394 cont->SetActive(false);
399 cont->getView()->setState(NOTHING_HAPPENS);
400 cont->getModel()->notifyObservers(_idManager);
401 cont->SetActive(false);
405 _selectedObjects.clear();
407 createGConnector(startOutputPort);
410 else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
412 _worldState = NOTHING_HAPPENS;
413 int id = _controllers.size()-1;
414 GObjectController* cont = _controllers[id];
415 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
417 GObjectController* finPort = _controllers[idController];
418 if(finPort->getGObjectType() == GPORT)
420 GPortModel* modelPort = (GPortModel*)finPort->getModel();
421 modelContour->setEndPort(modelPort);
424 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();
425 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
428 for(int i=0;i<_controllers.size();i++)
430 GObjectController* cont = _controllers[i];
431 if(cont->getView()!=NULL)
433 cont->getView()->setState(NOTHING_HAPPENS);
434 cont->getModel()->notifyObservers(_idManager);
436 cont->SetActive(true);
442 //=========================================================================
444 bool wxVtkSceneManager::OnMouseMove()
447 wxVTKRenderWindowInteractor *wxVTKiren;
448 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
449 wxVTKiren->GetEventPosition(X,Y);
452 if(_worldState == DRAG_OBJECTS)
454 for (int i=0; i<_selectedObjects.size(); i++)
456 int id = _selectedObjects[i];
457 GObjectController* cont = _controllers[id];
460 cont->getView()->setStartDragging(true);
462 cont->getView()->moveObject(X,Y);
463 cont->getView()->setState(DRAG);
464 cont->getModel()->notifyObservers(_idManager);
467 std::map<int, GObjectController*>::iterator it;
469 for(it = _controllers.begin(); it != _controllers.end(); ++it)
471 GObjectController *desc = it->second;
472 if(desc->getGObjectType()==GCONNECTOR)
474 GConnectorView* vconn = (GConnectorView*)desc->getView();
475 vconn->updateStartEndPoints();
479 _startDragging=false;
482 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
484 std::map<int, GObjectController*>::iterator it;
486 for(it = _controllers.begin(); it != _controllers.end(); ++it)
488 GObjectController *desc = it->second;
489 int type = desc->getGObjectType();
490 int state = desc->getView()->getState();
492 if(state == HIGHLIGHTED){
494 updateStatusBar(desc->getStatusText());
497 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
498 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
510 //=========================================================================
512 bool wxVtkSceneManager::OnLeftButtonDown()
515 if(_selectedObjects.size()!=0)
517 _worldState = DRAG_OBJECTS;
518 _startDragging = true;
520 for (int i = 0; i < _selectedObjects.size(); i++)
522 int id = _selectedObjects[i];
523 GObjectController* cont = _controllers[id];
524 cont->getView()->setState(DRAG);
525 cont->getModel()->notifyObservers(_idManager);
534 //=========================================================================
536 bool wxVtkSceneManager::OnLeftButtonUp()
538 if(_worldState == DRAG_OBJECTS)
540 _worldState = NOTHING_HAPPENS;
542 for (int i = 0; i < _selectedObjects.size(); i++)
544 int id = _selectedObjects[i];
545 GObjectController* cont = _controllers[id];
546 cont->getView()->setState(SELECTED);
547 cont->getModel()->notifyObservers(_idManager);
553 //=========================================================================
555 bool wxVtkSceneManager::OnRightButtonUp()
557 for (int i = 0; i < _selectedObjects.size(); i++)
559 int id = _selectedObjects[i];
560 GObjectController* cont = _controllers[id];
561 cont->SetActive(true);
562 cont->getView()->setState(NOTHING_HAPPENS);
563 cont->getModel()->notifyObservers(_idManager);
566 _selectedObjects.clear();
571 //=========================================================================
573 bool wxVtkSceneManager::OnChar()
575 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
577 // KeyCode 127 : Delete Key
578 // KeyCode 8 : Backspace Key
579 if(keyCode == 8 || keyCode == 127)
581 if(_selectedObjects.size()>0)
583 for(int i=0;i<_selectedObjects.size();i++)
585 int id = _selectedObjects[i];
588 _selectedObjects.clear();
595 //=========================================================================
597 void wxVtkSceneManager::removeObject(int id)
599 GObjectController *control = _controllers[id];
600 std::vector<int> controllersToRemove;
602 if(control->getGObjectType()==GBLACKBOX)
604 GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
605 std::vector<GPortModel*>inputs = bbmod->getInputPorts();
607 for(int i = 0;i<inputs.size();i++)
609 controllersToRemove.push_back(inputs[i]->getObjectId());
612 std::vector<GPortModel*>outputs = bbmod->getOutputPorts();
614 for(int i = 0;i<outputs.size();i++)
616 controllersToRemove.push_back(outputs[i]->getObjectId());
619 controllersToRemove.push_back(control->getId());
622 for(int i = 0;i<controllersToRemove.size();i++)
624 int id = controllersToRemove[i];
625 GObjectController *cont = _controllers[id];
626 cont->removeFromScene();
627 unregisterController((InteractorStyleMaracas*)cont);
628 _controllers.erase(id);
634 //=========================================================================
636 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
638 _parent->displayBlackBoxInfo(packageName,boxName);
641 //=========================================================================
643 void wxVtkSceneManager::updateStatusBar(std::string textStatus)
645 _parent->updateStatusBar(textStatus);
648 //=========================================================================
650 std::string wxVtkSceneManager::getDiagramScript()
652 bool existsExec=false;
654 std::vector<std::string> packages;
655 std::vector<int> boxes;
656 std::vector<int> connections;
657 std::vector<int> execBoxes;
659 std::map<int, GObjectController*>::iterator it;
661 for(it = _controllers.begin(); it != _controllers.end(); ++it)
663 GObjectController *desc = it->second;
664 int type = desc->getGObjectType();
668 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
670 std::string pkg = mod->getBBTKPackage();
671 bool existsPkg = false;
672 for(int t = 0;t<packages.size() && existsPkg == false;t++)
681 packages.push_back(pkg);
685 boxes.push_back(it->first);
686 if(mod->isExecutable())
688 execBoxes.push_back(it->first);
692 else if(type==GCONNECTOR)
694 connections.push_back(it->first);
698 std::string script = "";
699 script+="# BBTK GEditor Script\n";
700 script+="# ----------------------\n";
704 for(i = 0; i<packages.size();i++)
711 for(i = 0; i<boxes.size();i++)
715 GObjectController *control = _controllers[id];
716 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
718 script+=model->getBBTKType();
720 script+=model->getBBTKName();
724 for(i = 0; i<connections.size();i++)
727 int id = connections[i];
728 GObjectController *control = _controllers[id];
729 GConnectorModel *model = (GConnectorModel*)control->getModel();
731 //Start Connection info
732 GPortModel *start = model->getStartPort();
733 script+=start->getParentBox()->getBBTKName();
735 script+=start->getBBTKName();
739 //End Connection info
740 GPortModel *end = model->getEndPort();
741 script+=end->getParentBox()->getBBTKName();
743 script+=end->getBBTKName();
748 for(i = 0; i<execBoxes.size();i++)
751 int id = execBoxes[i];
752 GObjectController *control = _controllers[id];
753 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
755 script+=model->getBBTKName();
764 //=========================================================================
766 } // EO namespace bbtk