]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
78ece891bb13c29bcdf5cd1ba7c2fc752074978e
[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 boxName)
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, boxName);
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(boxName);
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,posinBox,blackBox);
201         }
202
203         //=========================================================================
204
205         void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
206         {
207                 createGPort(portType,posinBox,blackBox);
208         }
209
210         //=========================================================================
211
212         void wxVtkSceneManager::createGPort(int portType, 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->addObserver(view);
225                 model->addObserver(this);
226
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();
232                 
233                 //Associates the controller with the correspondent model and view
234                 controller->setModelAndView(model,view);
235
236                 model->notifyObservers(_idManager);
237
238                 //Register the controller of the new object
239                 registerController((InteractorStyleMaracas*) controller);
240                 
241                 int newId = _controllers.size();
242                 controller->setId(newId);
243                 _controllers[newId] = controller;
244         }
245
246         //=========================================================================
247
248         void wxVtkSceneManager::createGConnector(GPortModel* startPort)
249         {
250                 int type = GCONNECTOR;
251
252                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
253                 manualConnectorContourView* manContourView      = new manualConnectorContourView();
254                 manualContourModel* manContourModel     = new manualContourModel();
255
256                 GConnectorController* connectorcontroller = new GConnectorController();                         
257                 GConnectorModel* connectorModel = new GConnectorModel();
258                 GConnectorView* connectorView = new GConnectorView();
259                 connectorModel->setGObjectType(type);
260
261                 manContourModel->SetCloseContour(false);
262                 connectorModel->setStartPort(startPort);
263
264                 manContourView->SetModel( manContourModel );
265                 manContourView->SetWxVtkBaseView( _baseView );
266                 manContourView->SetRange( 0.5 );
267                 manContourView->SetZ( 900 );
268
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);
273
274                 manContourControl->SetModelView( manContourModel , manContourView );
275                 
276                 manContourControl->CreateNewManualContour();
277
278                 manContourView->RefreshContour();
279
280
281                 double x,y,z;
282                 connectorModel->getInicPoint(x,y,z);
283
284                 manContourControl->SetState(1);
285                 manContourModel->SetCloseContour(false);
286                 
287                 manContourModel->AddPoint(x,y,z);
288                 manContourView->AddPoint();
289
290                 manContourModel->AddPoint(x,y,z);
291                 manContourView->AddPoint();
292
293                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
294                 manContourControl->_bakIdPoint=bak;
295                 manContourView->Refresh();
296
297                 manContourControl->SetMoving( false );
298
299                 registerController((InteractorStyleMaracas*) connectorcontroller);
300
301                 connectorcontroller->setManualContourController(manContourControl);             
302                 connectorModel->setManualContourModel(manContourModel);
303                 connectorView->setManualContourView(manContourView);
304                 connectorView->setModel(connectorModel);
305                 connectorcontroller->setModelAndView(connectorModel,connectorView);
306
307                 int newId = _controllers.size();
308                 connectorcontroller->setId(newId);
309                 _controllers[newId] = connectorcontroller;
310
311         }
312
313         //=========================================================================
314
315         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
316         {
317                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
318                 baseViewControlManager->AddInteractorStyleMaracas( param );
319         }
320
321         //=========================================================================
322
323         vtkRenderer* wxVtkSceneManager::getRenderer()
324         {
325                 return _baseView->GetRenderer();
326         }
327
328         //=========================================================================
329
330         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
331         {
332                 return _baseView->GetRenWin();
333         }
334         //=========================================================================
335
336         bool wxVtkSceneManager::OnMouseMove()
337         {
338                 int X,Y;
339                 wxVTKRenderWindowInteractor *wxVTKiren;
340                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
341                 wxVTKiren->GetEventPosition(X,Y);
342         
343                 
344                 if(_worldState == DRAG_OBJECTS)
345                 {                       
346                         for (int i=0; i<_selectedObjects.size(); i++)
347                         {
348                                 int id = _selectedObjects[i];
349                                 GObjectController* cont = _controllers[id];
350                                 if(_startDragging)
351                                 {
352                                         cont->getView()->setStartDragging(true);
353                                 }
354                                 cont->getView()->moveObject(X,Y);
355                                 cont->getView()->setState(DRAG);
356                                 cont->getModel()->notifyObservers(_idManager);
357                         }
358
359                         std::map<int, GObjectController*>::iterator it;
360
361                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
362                         {
363                                 GObjectController *desc = it->second;
364                                 if(desc->getGObjectType()==GCONNECTOR)
365                                 {
366                                         GConnectorView* vconn = (GConnectorView*)desc->getView();
367                                         vconn->updateStartEndPoints();
368                                 }
369                         }
370                         
371                         _startDragging=false;
372
373                 }
374                 else if(_worldState == NOTHING_HAPPENS)
375                 {
376                         std::map<int, GObjectController*>::iterator it;
377
378                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
379                         {
380                                 GObjectController *desc = it->second;
381                                 if(desc->getGObjectType()==GBLACKBOX && desc->getView()->getState() == HIGHLIGHTED)
382                                 {
383                                         GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
384                                         _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
385                                 }
386                         }
387                 }
388
389
390                 return true;
391         }
392
393         //=========================================================================
394         
395         void wxVtkSceneManager::update(int idController,int command)
396         {
397                 if(command != NO_COMMAND)
398                 {
399                         if(command == ADD_TO_SELECTED)
400                         {
401                                 GObjectController* cont = _controllers[idController];
402
403                                 bool foundID=false;
404                                 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
405                                 {
406                                         int id = _selectedObjects[i];
407                                         if(id==idController)
408                                         {
409                                                 foundID = true;
410                                         }
411                                 }
412                                 if(!foundID)
413                                 {
414                                         int id = idController;
415                                         _selectedObjects.push_back(id);
416                                 }
417
418                         }
419                         else if(command == INIT_CREATION_CONTOUR)
420                         {       
421                                 _worldState = INIT_CREATION_CONTOUR;
422                                 GObjectController* cont = _controllers[idController];
423                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
424                                 
425
426                                 // The last one is the controller of the connector
427                                 for(int i=0;i<_controllers.size();i++)
428                                 {
429                                         GObjectController* cont = _controllers[i];
430                                         if(cont->getGObjectType() == GPORT )
431                                         {
432                                                 GPortModel* port = (GPortModel*)cont->getModel();
433                                                 if(port->getPortType()==GINPUTPORT)
434                                                 {
435                                                         cont->SetActive(true);
436                                                 }
437                                                 else
438                                                 {
439                                                         cont->getView()->setState(NOTHING_HAPPENS);
440                                                         cont->getModel()->notifyObservers(_idManager);
441                                                         cont->SetActive(false);
442                                                 }
443                                         }
444                                         else
445                                         {
446                                                 cont->getView()->setState(NOTHING_HAPPENS);
447                                                 cont->getModel()->notifyObservers(_idManager);
448                                                 cont->SetActive(false);
449                                         }                               
450                                 }
451
452                                 _selectedObjects.clear();
453
454                                 createGConnector(startOutputPort);
455
456                         }
457                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
458                         {                               
459                                 _worldState = NOTHING_HAPPENS;
460                                 int id = _controllers.size()-1;
461                                 GObjectController* cont = _controllers[id];                     
462                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
463
464                                 GObjectController* finPort = _controllers[idController];
465                                 if(finPort->getGObjectType() == GPORT)
466                                 {
467                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
468                                         modelContour->setEndPort(modelPort);
469                                 }                       
470
471                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
472                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
473                                 connView->Refresh();
474
475                                 for(int i=0;i<_controllers.size();i++)
476                                 {
477                                         GObjectController* cont = _controllers[i];
478                                         if(cont->getView()!=NULL)
479                                         {
480                                                 cont->getView()->setState(NOTHING_HAPPENS);
481                                                 cont->getModel()->notifyObservers(_idManager);
482                                         }
483                                         cont->SetActive(true);                                                          
484                                 }
485                         }
486                 }
487         }
488
489         //=========================================================================
490
491         
492         bool wxVtkSceneManager::OnLeftButtonDown()
493         {
494                 
495                 if(_selectedObjects.size()!=0)
496                 {
497                         _worldState = DRAG_OBJECTS;
498                         _startDragging = true;
499
500                         for (int i = 0; i < _selectedObjects.size(); i++)
501                         {
502                                 int id = _selectedObjects[i];
503                                 GObjectController* cont = _controllers[id];
504                                 cont->getView()->setState(DRAG);
505                                 cont->getModel()->notifyObservers(_idManager);
506                         }
507                 }
508                 
509                 
510                 
511                 return true;
512         }
513
514         //=========================================================================
515         
516         bool wxVtkSceneManager::OnLeftButtonUp()
517         {
518                 if(_worldState == DRAG_OBJECTS)
519                 {
520                         _worldState = NOTHING_HAPPENS;
521
522                         for (int i = 0; i < _selectedObjects.size(); i++)
523                         {
524                                 int id = _selectedObjects[i];
525                                 GObjectController* cont = _controllers[id];
526                                 cont->getView()->setState(SELECTED);
527                                 cont->getModel()->notifyObservers(_idManager);
528                         }
529                 }
530                 return true;
531         }
532
533         //=========================================================================
534
535         bool wxVtkSceneManager::OnRightButtonUp()
536         {
537                 for (int i = 0; i < _selectedObjects.size(); i++)
538                 {
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);
544                 }
545
546                 _selectedObjects.clear();
547
548                 return true;
549         }
550
551         //=========================================================================
552
553         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
554         {
555                 _parent->displayBlackBoxInfo(packageName,boxName);
556         }
557
558 }  // EO namespace bbtk
559
560 // EOF
561