]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
f1f1fed1e047a0ab667d5f178a6c9842c147b749
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsKernelEditorGraphic / wxVtkSceneManager.cxx
1 /*=========================================================================                                                                               
2 Program:   bbtk
3 Module:    $RCSfile$
4 Language:  C++
5 Date:      $Date$
6 Version:   $Revision$
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
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.
20 *
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
25 *  liability. 
26 *
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 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32 *  \file 
33 *  \brief Class bbtk::wxVtkSceneManager . 
34 */
35
36
37 #include "wxVtkSceneManager.h"
38
39 namespace bbtk
40 {
41
42
43         //=========================================================================
44         wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView,int idManager)
45         {
46                 _parent = parent;
47                 _numBoxes=0;
48                 _idManager=idManager;
49                 _baseView=baseView;
50                 _idLastController=0;
51                 _startDragging=false;
52
53                 if( _baseView!=NULL )
54                 {
55                         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);                      
56                         configureBaseView();
57                         _worldState=NOTHING_HAPPENS;
58                         registerController(this);
59                         
60                 }
61
62         }
63
64         //=========================================================================
65
66         void wxVtkSceneManager::disconnectDrop()
67         {
68                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
69         }
70
71         //=========================================================================
72
73         wxVtkSceneManager::~wxVtkSceneManager()
74         {
75         }
76
77         //=========================================================================
78
79         void wxVtkSceneManager::configureBaseView()
80         {
81                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
82
83                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
84
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);
90                 
91                 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
92                 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
93                 
94                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
95                 _baseView->GetRenderer()->GradientBackgroundOff();
96                 _baseView->Refresh();
97         }
98
99         //=========================================================================
100
101         void wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
102         {
103
104                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
105                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
106
107                 int type = GBLACKBOX;
108
109                 //Create the MVC Objects
110                 
111                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
112                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
113                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
114                 
115                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
116                 
117                 //Prepares the initial model
118                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
119
120                 double xx = x;
121                 double yy =  windowHeight-y;
122                 
123                 //z value is not important yet, because it is only used a parallel projection
124                 double zz = 900;
125
126                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
127                 model->setInicPoint(xx,yy,zz);
128                 
129                 _numBoxes++;
130                 std::stringstream stream;
131                 
132                 if(_numBoxes<10)
133                 {
134                         stream << "Box0" << _numBoxes;
135                 }
136                 else
137                 {
138                         stream << "Box" << _numBoxes;
139                 }
140                 std::string arraystring = stream.str();
141
142                 model->setBBTKName(arraystring);
143                 model->setBBTKType(boxType);
144                 model->setBBTKPackage(packageName);
145                                 
146                 model->addObserver(view);
147                 model->addObserver(this);
148
149                 //Iterate and create the input ports
150                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
151                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
152
153                 int i=0;
154                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
155                 {
156                         BlackBoxInputDescriptor *desc = itInput->second;
157                         createGInputPort(GINPUTPORT,i,model,desc);
158                         i++;
159                 }
160
161                 //Iterate and create the output ports
162                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
163                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
164
165                 i=0;
166                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
167                 {
168                         BlackBoxOutputDescriptor *desc = itOutput->second;
169                         createGOutputPort(GOUTPUTPORT,i,model,desc);
170                         i++;
171                 }
172
173
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();
179                 
180                 //Associates the controller with the correspondent model and view
181                 controller->setModelAndView(model,view);
182
183                 //Resgiter change to the observers of the actual model
184                 model->notifyObservers(_idManager);
185                 
186                 //Register the controller of the new object
187                 registerController((InteractorStyleMaracas*) controller);
188
189                 //Add the object to the objects list 
190                 int newId = _controllers.size();
191                 controller->setId(newId);
192                 _controllers[newId] = controller;
193
194         }
195
196         //=========================================================================
197
198         void wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
199         {
200                 createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
201         }
202
203         //=========================================================================
204
205         void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
206         {
207                 createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
208         }
209
210         //=========================================================================
211
212         void wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBlackBoxModel *blackBox)
213         {
214                 int type = GPORT;
215
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);
220
221                 model->registerInBox(blackBox,portType, posInBox);
222                 blackBox->addOutputPort(model);
223                 
224                 model->setBBTKType(bbtkType);
225                 model->setBBTKName(bbtkName);
226
227                 model->addObserver(view);
228                 model->addObserver(this);
229
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();
235                 
236                 //Associates the controller with the correspondent model and view
237                 controller->setModelAndView(model,view);
238
239                 model->notifyObservers(_idManager);
240
241                 //Register the controller of the new object
242                 registerController((InteractorStyleMaracas*) controller);
243                 
244                 int newId = _controllers.size();
245                 controller->setId(newId);
246                 _controllers[newId] = controller;
247         }
248
249         //=========================================================================
250
251         void wxVtkSceneManager::createGConnector(GPortModel* startPort)
252         {
253                 int type = GCONNECTOR;
254
255                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
256                 manualConnectorContourView* manContourView      = new manualConnectorContourView();
257                 manualContourModel* manContourModel     = new manualContourModel();
258
259                 GConnectorController* connectorcontroller = new GConnectorController();                         
260                 GConnectorModel* connectorModel = new GConnectorModel();
261                 GConnectorView* connectorView = new GConnectorView();
262                 connectorModel->setGObjectType(type);
263
264                 manContourModel->SetCloseContour(false);
265                 connectorModel->setStartPort(startPort);
266
267                 manContourView->SetModel( manContourModel );
268                 manContourView->SetWxVtkBaseView( _baseView );
269                 manContourView->SetRange( 0.5 );
270                 manContourView->SetZ( 900 );
271
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);
276
277                 manContourControl->SetModelView( manContourModel , manContourView );
278                 
279                 manContourControl->CreateNewManualContour();
280
281                 manContourView->RefreshContour();
282
283
284                 double x,y,z;
285                 connectorModel->getInicPoint(x,y,z);
286
287                 manContourControl->SetState(1);
288                 manContourModel->SetCloseContour(false);
289                 
290                 manContourModel->AddPoint(x,y,z);
291                 manContourView->AddPoint();
292
293                 manContourModel->AddPoint(x,y,z);
294                 manContourView->AddPoint();
295
296                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
297                 manContourControl->_bakIdPoint=bak;
298                 manContourView->Refresh();
299
300                 manContourControl->SetMoving( false );
301
302                 registerController((InteractorStyleMaracas*) connectorcontroller);
303
304                 connectorcontroller->setManualContourController(manContourControl);             
305                 connectorModel->setManualContourModel(manContourModel);
306                 connectorView->setManualContourView(manContourView);
307                 connectorView->setModel(connectorModel);
308                 connectorcontroller->setModelAndView(connectorModel,connectorView);
309
310                 int newId = _controllers.size();
311                 connectorcontroller->setId(newId);
312                 _controllers[newId] = connectorcontroller;
313
314         }
315
316         //=========================================================================
317
318         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
319         {
320                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
321                 baseViewControlManager->AddInteractorStyleMaracas( param );
322         }
323
324         //=========================================================================
325
326         vtkRenderer* wxVtkSceneManager::getRenderer()
327         {
328                 return _baseView->GetRenderer();
329         }
330
331         //=========================================================================
332
333         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
334         {
335                 return _baseView->GetRenWin();
336         }
337         //=========================================================================
338
339                 
340         void wxVtkSceneManager::update(int idController,int command)
341         {
342                 if(command != NO_COMMAND)
343                 {
344                         if(command == ADD_TO_SELECTED)
345                         {
346                                 GObjectController* cont = _controllers[idController];
347
348                                 bool foundID=false;
349                                 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
350                                 {
351                                         int id = _selectedObjects[i];
352                                         if(id==idController)
353                                         {
354                                                 foundID = true;
355                                         }
356                                 }
357                                 if(!foundID)
358                                 {
359                                         int id = idController;
360                                         _selectedObjects.push_back(id);
361                                 }
362
363                         }
364                         else if(command == INIT_CREATION_CONTOUR)
365                         {       
366                                 _worldState = INIT_CREATION_CONTOUR;
367                                 GObjectController* cont = _controllers[idController];
368                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
369                                 
370
371                                 // The last one is the controller of the connector
372                                 for(int i=0;i<_controllers.size();i++)
373                                 {
374                                         GObjectController* cont = _controllers[i];
375                                         if(cont->getGObjectType() == GPORT )
376                                         {
377                                                 GPortModel* port = (GPortModel*)cont->getModel();
378                                                 if(port->getPortType()==GINPUTPORT)
379                                                 {
380                                                         cont->SetActive(true);
381                                                 }
382                                                 else
383                                                 {
384                                                         cont->getView()->setState(NOTHING_HAPPENS);
385                                                         cont->getModel()->notifyObservers(_idManager);
386                                                         cont->SetActive(false);
387                                                 }
388                                         }
389                                         else
390                                         {
391                                                 cont->getView()->setState(NOTHING_HAPPENS);
392                                                 cont->getModel()->notifyObservers(_idManager);
393                                                 cont->SetActive(false);
394                                         }                               
395                                 }
396
397                                 _selectedObjects.clear();
398
399                                 createGConnector(startOutputPort);
400
401                         }
402                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
403                         {                               
404                                 _worldState = NOTHING_HAPPENS;
405                                 int id = _controllers.size()-1;
406                                 GObjectController* cont = _controllers[id];                     
407                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
408
409                                 GObjectController* finPort = _controllers[idController];
410                                 if(finPort->getGObjectType() == GPORT)
411                                 {
412                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
413                                         modelContour->setEndPort(modelPort);
414                                 }                       
415
416                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
417                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
418                                 connView->Refresh();
419
420                                 for(int i=0;i<_controllers.size();i++)
421                                 {
422                                         GObjectController* cont = _controllers[i];
423                                         if(cont->getView()!=NULL)
424                                         {
425                                                 cont->getView()->setState(NOTHING_HAPPENS);
426                                                 cont->getModel()->notifyObservers(_idManager);
427                                         }
428                                         cont->SetActive(true);                                                          
429                                 }
430                         }
431                 }
432         }
433
434         //=========================================================================
435
436         bool wxVtkSceneManager::OnMouseMove()
437         {
438                 int X,Y;
439                 wxVTKRenderWindowInteractor *wxVTKiren;
440                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
441                 wxVTKiren->GetEventPosition(X,Y);
442         
443                 
444                 if(_worldState == DRAG_OBJECTS)
445                 {                       
446                         for (int i=0; i<_selectedObjects.size(); i++)
447                         {
448                                 int id = _selectedObjects[i];
449                                 GObjectController* cont = _controllers[id];
450                                 if(_startDragging)
451                                 {
452                                         cont->getView()->setStartDragging(true);
453                                 }
454                                 cont->getView()->moveObject(X,Y);
455                                 cont->getView()->setState(DRAG);
456                                 cont->getModel()->notifyObservers(_idManager);
457                         }
458
459                         std::map<int, GObjectController*>::iterator it;
460
461                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
462                         {
463                                 GObjectController *desc = it->second;
464                                 if(desc->getGObjectType()==GCONNECTOR)
465                                 {
466                                         GConnectorView* vconn = (GConnectorView*)desc->getView();
467                                         vconn->updateStartEndPoints();
468                                 }
469                         }
470                         
471                         _startDragging=false;
472
473                 }
474                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
475                 {
476                         std::map<int, GObjectController*>::iterator it;
477
478                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
479                         {
480                                 GObjectController *desc = it->second;
481                                 int type = desc->getGObjectType();
482                                 int state = desc->getView()->getState();
483                                 
484                                 if(state == HIGHLIGHTED){
485
486                                         updateStatusBar(desc->getStatusText());
487                                         if(type==GBLACKBOX)
488                                         {
489                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
490                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
491                                         }
492                                 }
493                         }
494                 }
495                 
496                 
497
498
499                 return true;
500         }
501
502         //=========================================================================
503         
504         bool wxVtkSceneManager::OnLeftButtonDown()
505         {
506                 
507                 if(_selectedObjects.size()!=0)
508                 {
509                         _worldState = DRAG_OBJECTS;
510                         _startDragging = true;
511
512                         for (int i = 0; i < _selectedObjects.size(); i++)
513                         {
514                                 int id = _selectedObjects[i];
515                                 GObjectController* cont = _controllers[id];
516                                 cont->getView()->setState(DRAG);
517                                 cont->getModel()->notifyObservers(_idManager);
518                         }
519                 }
520                 
521                 
522                 
523                 return true;
524         }
525
526         //=========================================================================
527         
528         bool wxVtkSceneManager::OnLeftButtonUp()
529         {
530                 if(_worldState == DRAG_OBJECTS)
531                 {
532                         _worldState = NOTHING_HAPPENS;
533
534                         for (int i = 0; i < _selectedObjects.size(); i++)
535                         {
536                                 int id = _selectedObjects[i];
537                                 GObjectController* cont = _controllers[id];
538                                 cont->getView()->setState(SELECTED);
539                                 cont->getModel()->notifyObservers(_idManager);
540                         }
541                 }
542                 return true;
543         }
544
545         //=========================================================================
546
547         bool wxVtkSceneManager::OnRightButtonUp()
548         {
549                 for (int i = 0; i < _selectedObjects.size(); i++)
550                 {
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);
556                 }
557
558                 _selectedObjects.clear();
559
560                 return true;
561         }
562
563         //=========================================================================
564
565         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
566         {
567                 _parent->displayBlackBoxInfo(packageName,boxName);
568         }
569
570         //=========================================================================
571
572         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
573         {
574                 _parent->updateStatusBar(textStatus);
575         }
576
577         //=========================================================================
578
579 }  // EO namespace bbtk
580
581 // EOF
582