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 boxName)
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, boxName);
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(boxName);
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,posinBox,blackBox);
203 //=========================================================================
205 void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
207 createGPort(portType,posinBox,blackBox);
210 //=========================================================================
212 void wxVtkSceneManager::createGPort(int portType, 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->addObserver(view);
225 model->addObserver(this);
227 //Associates the view with the correspondent renderer and the model.
228 //(NOTE: Refresh is only made by the view)
229 view->setModel(model);
230 view->setBaseView(_baseView);
231 view->initVtkObjects();
233 //Associates the controller with the correspondent model and view
234 controller->setModelAndView(model,view);
236 model->notifyObservers(_idManager);
238 //Register the controller of the new object
239 registerController((InteractorStyleMaracas*) controller);
241 int newId = _controllers.size();
242 controller->setId(newId);
243 _controllers[newId] = controller;
246 //=========================================================================
248 void wxVtkSceneManager::createGConnector(GPortModel* startPort)
250 int type = GCONNECTOR;
252 manualConnectorContourController* manContourControl = new manualConnectorContourController();
253 manualConnectorContourView* manContourView = new manualConnectorContourView();
254 manualContourModel* manContourModel = new manualContourModel();
256 GConnectorController* connectorcontroller = new GConnectorController();
257 GConnectorModel* connectorModel = new GConnectorModel();
258 GConnectorView* connectorView = new GConnectorView();
259 connectorModel->setGObjectType(type);
261 manContourModel->SetCloseContour(false);
262 connectorModel->setStartPort(startPort);
264 manContourView->SetModel( manContourModel );
265 manContourView->SetWxVtkBaseView( _baseView );
266 manContourView->SetRange( 0.5 );
267 manContourView->SetZ( 900 );
269 manContourView->SetColorNormalContour(0, 0, 1);
270 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
271 manContourView->SetColorSelectContour(1, 0.8, 0);
272 manContourView->SetWidthLine(1);
274 manContourControl->SetModelView( manContourModel , manContourView );
276 manContourControl->CreateNewManualContour();
278 manContourView->RefreshContour();
282 connectorModel->getInicPoint(x,y,z);
284 manContourControl->SetState(1);
285 manContourModel->SetCloseContour(false);
287 manContourModel->AddPoint(x,y,z);
288 manContourView->AddPoint();
290 manContourModel->AddPoint(x,y,z);
291 manContourView->AddPoint();
293 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
294 manContourControl->_bakIdPoint=bak;
295 manContourView->Refresh();
297 manContourControl->SetMoving( false );
299 registerController((InteractorStyleMaracas*) connectorcontroller);
301 connectorcontroller->setManualContourController(manContourControl);
302 connectorModel->setManualContourModel(manContourModel);
303 connectorView->setManualContourView(manContourView);
304 connectorView->setModel(connectorModel);
305 connectorcontroller->setModelAndView(connectorModel,connectorView);
307 int newId = _controllers.size();
308 connectorcontroller->setId(newId);
309 _controllers[newId] = connectorcontroller;
313 //=========================================================================
315 void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
317 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
318 baseViewControlManager->AddInteractorStyleMaracas( param );
321 //=========================================================================
323 vtkRenderer* wxVtkSceneManager::getRenderer()
325 return _baseView->GetRenderer();
328 //=========================================================================
330 vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
332 return _baseView->GetRenWin();
334 //=========================================================================
336 bool wxVtkSceneManager::OnMouseMove()
339 wxVTKRenderWindowInteractor *wxVTKiren;
340 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
341 wxVTKiren->GetEventPosition(X,Y);
344 if(_worldState == DRAG_OBJECTS)
346 for (int i=0; i<_selectedObjects.size(); i++)
348 int id = _selectedObjects[i];
349 GObjectController* cont = _controllers[id];
352 cont->getView()->setStartDragging(true);
354 cont->getView()->moveObject(X,Y);
355 cont->getView()->setState(DRAG);
356 cont->getModel()->notifyObservers(_idManager);
359 std::map<int, GObjectController*>::iterator it;
361 for(it = _controllers.begin(); it != _controllers.end(); ++it)
363 GObjectController *desc = it->second;
364 if(desc->getGObjectType()==GCONNECTOR)
366 GConnectorView* vconn = (GConnectorView*)desc->getView();
367 vconn->updateStartEndPoints();
371 _startDragging=false;
374 else if(_worldState == NOTHING_HAPPENS)
376 std::map<int, GObjectController*>::iterator it;
378 for(it = _controllers.begin(); it != _controllers.end(); ++it)
380 GObjectController *desc = it->second;
381 if(desc->getGObjectType()==GBLACKBOX && desc->getView()->getState() == HIGHLIGHTED)
383 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
384 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
393 //=========================================================================
395 void wxVtkSceneManager::update(int idController,int command)
397 if(command != NO_COMMAND)
399 if(command == ADD_TO_SELECTED)
401 GObjectController* cont = _controllers[idController];
404 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
406 int id = _selectedObjects[i];
414 int id = idController;
415 _selectedObjects.push_back(id);
419 else if(command == INIT_CREATION_CONTOUR)
421 _worldState = INIT_CREATION_CONTOUR;
422 GObjectController* cont = _controllers[idController];
423 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
426 // The last one is the controller of the connector
427 for(int i=0;i<_controllers.size();i++)
429 GObjectController* cont = _controllers[i];
430 if(cont->getGObjectType() == GPORT )
432 GPortModel* port = (GPortModel*)cont->getModel();
433 if(port->getPortType()==GINPUTPORT)
435 cont->SetActive(true);
439 cont->getView()->setState(NOTHING_HAPPENS);
440 cont->getModel()->notifyObservers(_idManager);
441 cont->SetActive(false);
446 cont->getView()->setState(NOTHING_HAPPENS);
447 cont->getModel()->notifyObservers(_idManager);
448 cont->SetActive(false);
452 _selectedObjects.clear();
454 createGConnector(startOutputPort);
457 else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
459 _worldState = NOTHING_HAPPENS;
460 int id = _controllers.size()-1;
461 GObjectController* cont = _controllers[id];
462 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
464 GObjectController* finPort = _controllers[idController];
465 if(finPort->getGObjectType() == GPORT)
467 GPortModel* modelPort = (GPortModel*)finPort->getModel();
468 modelContour->setEndPort(modelPort);
471 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();
472 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
475 for(int i=0;i<_controllers.size();i++)
477 GObjectController* cont = _controllers[i];
478 if(cont->getView()!=NULL)
480 cont->getView()->setState(NOTHING_HAPPENS);
481 cont->getModel()->notifyObservers(_idManager);
483 cont->SetActive(true);
489 //=========================================================================
492 bool wxVtkSceneManager::OnLeftButtonDown()
495 if(_selectedObjects.size()!=0)
497 _worldState = DRAG_OBJECTS;
498 _startDragging = true;
500 for (int i = 0; i < _selectedObjects.size(); i++)
502 int id = _selectedObjects[i];
503 GObjectController* cont = _controllers[id];
504 cont->getView()->setState(DRAG);
505 cont->getModel()->notifyObservers(_idManager);
514 //=========================================================================
516 bool wxVtkSceneManager::OnLeftButtonUp()
518 if(_worldState == DRAG_OBJECTS)
520 _worldState = NOTHING_HAPPENS;
522 for (int i = 0; i < _selectedObjects.size(); i++)
524 int id = _selectedObjects[i];
525 GObjectController* cont = _controllers[id];
526 cont->getView()->setState(SELECTED);
527 cont->getModel()->notifyObservers(_idManager);
533 //=========================================================================
535 bool wxVtkSceneManager::OnRightButtonUp()
537 for (int i = 0; i < _selectedObjects.size(); i++)
539 int id = _selectedObjects[i];
540 GObjectController* cont = _controllers[id];
541 cont->SetActive(true);
542 cont->getView()->setState(NOTHING_HAPPENS);
543 cont->getModel()->notifyObservers(_idManager);
546 _selectedObjects.clear();
551 //=========================================================================
553 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
555 _parent->displayBlackBoxInfo(packageName,boxName);
558 } // EO namespace bbtk