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 vtkRenderer* wxVtkSceneManager::getRenderer()
328 return _baseView->GetRenderer();
331 //=========================================================================
333 vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
335 return _baseView->GetRenWin();
337 //=========================================================================
340 void wxVtkSceneManager::update(int idController,int command)
342 if(command != NO_COMMAND)
344 if(command == ADD_TO_SELECTED)
346 GObjectController* cont = _controllers[idController];
349 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
351 int id = _selectedObjects[i];
359 int id = idController;
360 _selectedObjects.push_back(id);
364 else if(command == INIT_CREATION_CONTOUR)
366 _worldState = INIT_CREATION_CONTOUR;
367 GObjectController* cont = _controllers[idController];
368 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
371 // The last one is the controller of the connector
372 for(int i=0;i<_controllers.size();i++)
374 GObjectController* cont = _controllers[i];
375 if(cont->getGObjectType() == GPORT )
377 GPortModel* port = (GPortModel*)cont->getModel();
378 if(port->getPortType()==GINPUTPORT)
380 cont->SetActive(true);
384 cont->getView()->setState(NOTHING_HAPPENS);
385 cont->getModel()->notifyObservers(_idManager);
386 cont->SetActive(false);
391 cont->getView()->setState(NOTHING_HAPPENS);
392 cont->getModel()->notifyObservers(_idManager);
393 cont->SetActive(false);
397 _selectedObjects.clear();
399 createGConnector(startOutputPort);
402 else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
404 _worldState = NOTHING_HAPPENS;
405 int id = _controllers.size()-1;
406 GObjectController* cont = _controllers[id];
407 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
409 GObjectController* finPort = _controllers[idController];
410 if(finPort->getGObjectType() == GPORT)
412 GPortModel* modelPort = (GPortModel*)finPort->getModel();
413 modelContour->setEndPort(modelPort);
416 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();
417 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
420 for(int i=0;i<_controllers.size();i++)
422 GObjectController* cont = _controllers[i];
423 if(cont->getView()!=NULL)
425 cont->getView()->setState(NOTHING_HAPPENS);
426 cont->getModel()->notifyObservers(_idManager);
428 cont->SetActive(true);
434 //=========================================================================
436 bool wxVtkSceneManager::OnMouseMove()
439 wxVTKRenderWindowInteractor *wxVTKiren;
440 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
441 wxVTKiren->GetEventPosition(X,Y);
444 if(_worldState == DRAG_OBJECTS)
446 for (int i=0; i<_selectedObjects.size(); i++)
448 int id = _selectedObjects[i];
449 GObjectController* cont = _controllers[id];
452 cont->getView()->setStartDragging(true);
454 cont->getView()->moveObject(X,Y);
455 cont->getView()->setState(DRAG);
456 cont->getModel()->notifyObservers(_idManager);
459 std::map<int, GObjectController*>::iterator it;
461 for(it = _controllers.begin(); it != _controllers.end(); ++it)
463 GObjectController *desc = it->second;
464 if(desc->getGObjectType()==GCONNECTOR)
466 GConnectorView* vconn = (GConnectorView*)desc->getView();
467 vconn->updateStartEndPoints();
471 _startDragging=false;
474 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
476 std::map<int, GObjectController*>::iterator it;
478 for(it = _controllers.begin(); it != _controllers.end(); ++it)
480 GObjectController *desc = it->second;
481 int type = desc->getGObjectType();
482 int state = desc->getView()->getState();
484 if(state == HIGHLIGHTED){
486 updateStatusBar(desc->getStatusText());
489 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
490 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
502 //=========================================================================
504 bool wxVtkSceneManager::OnLeftButtonDown()
507 if(_selectedObjects.size()!=0)
509 _worldState = DRAG_OBJECTS;
510 _startDragging = true;
512 for (int i = 0; i < _selectedObjects.size(); i++)
514 int id = _selectedObjects[i];
515 GObjectController* cont = _controllers[id];
516 cont->getView()->setState(DRAG);
517 cont->getModel()->notifyObservers(_idManager);
526 //=========================================================================
528 bool wxVtkSceneManager::OnLeftButtonUp()
530 if(_worldState == DRAG_OBJECTS)
532 _worldState = NOTHING_HAPPENS;
534 for (int i = 0; i < _selectedObjects.size(); i++)
536 int id = _selectedObjects[i];
537 GObjectController* cont = _controllers[id];
538 cont->getView()->setState(SELECTED);
539 cont->getModel()->notifyObservers(_idManager);
545 //=========================================================================
547 bool wxVtkSceneManager::OnRightButtonUp()
549 for (int i = 0; i < _selectedObjects.size(); i++)
551 int id = _selectedObjects[i];
552 GObjectController* cont = _controllers[id];
553 cont->SetActive(true);
554 cont->getView()->setState(NOTHING_HAPPENS);
555 cont->getModel()->notifyObservers(_idManager);
558 _selectedObjects.clear();
563 //=========================================================================
565 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
567 _parent->displayBlackBoxInfo(packageName,boxName);
570 //=========================================================================
572 void wxVtkSceneManager::updateStatusBar(std::string textStatus)
574 _parent->updateStatusBar(textStatus);
577 //=========================================================================
579 } // EO namespace bbtk