]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
daa6327c157cceea4e7cd481750fa60fa7654e82
[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(wxDropTarget *parent, wxVtk3DBaseView *baseView,int idManager)
45         {
46                 _numBoxes=0;
47                 _idManager=idManager;
48                 _baseView=baseView;
49                 _idLastController=0;
50
51                 if( _baseView!=NULL )
52                 {
53                         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(parent);
54                         registerController(this);
55                         configureBaseView();
56                         _worldState=NOTHING_HAPPENS;
57                         
58                 }
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                                 
145                 model->addObserver(view);
146                 model->addObserver(this);
147
148                 //Iterate and create the input ports
149                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
150                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
151
152                 int i=0;
153                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
154                 {
155                         BlackBoxInputDescriptor *desc = itInput->second;
156                         createGInputPort(GINPUTPORT,i,model,desc);
157                         i++;
158                 }
159
160                 //Iterate and create the output ports
161                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
162                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
163
164                 i=0;
165                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
166                 {
167                         BlackBoxOutputDescriptor *desc = itOutput->second;
168                         createGOutputPort(GOUTPUTPORT,i,model,desc);
169                         i++;
170                 }
171
172
173                 //Associates the view with the correspondent renderer and the  model.
174                 //(NOTE: Refresh is only made by the view)
175                 view->setModel(model);
176                 view->setBaseView(_baseView);
177                 view->initVtkObjects();
178                 
179                 //Associates the controller with the correspondent model and view
180                 controller->setModelAndView(model,view);
181
182                 //Resgiter change to the observers of the actual model
183                 model->notifyObservers(_idManager);
184                 
185                 //Register the controller of the new object
186                 registerController((InteractorStyleMaracas*) controller);
187
188                 //Add the object to the objects list 
189                 int newId = _controllers.size();
190                 controller->setId(newId);
191                 _controllers[newId] = controller;
192
193         }
194
195         //=========================================================================
196
197         void wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
198         {
199                 createGPort(portType,posinBox,blackBox);
200         }
201
202         //=========================================================================
203
204         void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
205         {
206                 createGPort(portType,posinBox,blackBox);
207         }
208
209         //=========================================================================
210
211         void wxVtkSceneManager::createGPort(int portType, int posInBox,GBlackBoxModel *blackBox)
212         {
213                 int type = GPORT;
214
215                 //Create the MVC Objects
216                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
217                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
218                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
219
220                 model->registerInBox(blackBox,portType, posInBox);
221                 blackBox->addOutputPort(model);
222                 
223                 model->addObserver(view);
224                 model->addObserver(this);
225
226                 //Associates the view with the correspondent renderer and the  model.
227                 //(NOTE: Refresh is only made by the view)
228                 view->setModel(model);
229                 view->setBaseView(_baseView);
230                 view->initVtkObjects();
231                 
232                 //Associates the controller with the correspondent model and view
233                 controller->setModelAndView(model,view);
234
235                 model->notifyObservers(_idManager);
236
237                 //Register the controller of the new object
238                 registerController((InteractorStyleMaracas*) controller);
239                 
240                 int newId = _controllers.size();
241                 controller->setId(newId);
242                 _controllers[newId] = controller;
243         }
244
245         //=========================================================================
246
247         void wxVtkSceneManager::createGConnector(GPortModel* startPort)
248         {
249                 int type = GCONNECTOR;
250
251                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
252                 manualConnectorContourView* manContourView      = new manualConnectorContourView();
253                 manualContourModel* manContourModel     = new manualContourModel();
254
255                 GConnectorController* connectorcontroller = new GConnectorController();                         
256                 GConnectorModel* connectorModel = new GConnectorModel();
257                 GConnectorView* connectorView = new GConnectorView();
258                 connectorModel->setGObjectType(type);
259
260                 manContourModel->SetCloseContour(false);
261                 connectorModel->setStartPort(startPort);
262
263                 manContourView->SetModel( manContourModel );
264                 manContourView->SetWxVtkBaseView( _baseView );
265                 manContourView->SetRange( 0.5 );
266                 manContourView->SetZ( 900 );
267
268                 manContourView->SetColorNormalContour(0, 0, 1);
269                 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
270                 manContourView->SetColorSelectContour(1, 0.8, 0);
271                 manContourView->SetWidthLine(1);
272
273                 manContourControl->SetModelView( manContourModel , manContourView );
274                 
275                 manContourControl->CreateNewManualContour();
276
277                 manContourView->RefreshContour();
278
279
280                 double x,y,z;
281                 connectorModel->getInicPoint(x,y,z);
282
283                 manContourControl->SetState(1);
284                 manContourModel->SetCloseContour(false);
285                 
286                 manContourModel->AddPoint(x,y,z);
287                 manContourView->AddPoint();
288
289                 manContourModel->AddPoint(x,y,z);
290                 manContourView->AddPoint();
291
292                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
293                 manContourControl->_bakIdPoint=bak;
294                 manContourView->Refresh();
295
296                 manContourControl->SetMoving( false );
297
298                 registerController((InteractorStyleMaracas*) connectorcontroller);
299
300                 connectorcontroller->setManualContourController(manContourControl);             
301                 connectorModel->setManualContourModel(manContourModel);
302                 connectorView->setManualContourView(manContourView);
303                 connectorcontroller->setModelAndView(connectorModel,connectorView);
304
305                 int newId = _controllers.size();
306                 connectorcontroller->setId(newId);
307                 _controllers[newId] = connectorcontroller;
308
309         }
310
311         //=========================================================================
312
313         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
314         {
315                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
316                 baseViewControlManager->AddInteractorStyleMaracas( param );
317         }
318
319         //=========================================================================
320
321         vtkRenderer* wxVtkSceneManager::getRenderer()
322         {
323                 return _baseView->GetRenderer();
324         }
325
326         //=========================================================================
327
328         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
329         {
330                 return _baseView->GetRenWin();
331         }
332         //=========================================================================
333
334         bool wxVtkSceneManager::OnMouseMove()
335         {
336                 return true;
337         }
338
339         //=========================================================================
340         
341         void wxVtkSceneManager::update(int idController,int command)
342         {
343                 
344                 if(command == INIT_CREATION_CONTOUR)
345                 {       
346                         GObjectController* cont = _controllers[idController];
347                         GPortModel* startOutputPort = (GPortModel*)cont->getModel();
348                         createGConnector(startOutputPort);
349
350                         // The last one is the controller of the connector
351                         for(int i=0;i<_controllers.size()-1;i++)
352                         {
353                                 GObjectController* cont = _controllers[i];
354                                 if(cont->getGObjectType() == GPORT )
355                                 {
356                                         GPortModel* port = (GPortModel*)cont->getModel();
357                                         if(port->getPortType()==GINPUTPORT)
358                                         {
359                                                 cont->SetActive(true);
360                                         }
361                                         else
362                                         {
363                                                 cont->getView()->setState(NOTHING_HAPPENS);
364                                                 cont->SetActive(false);
365                                         }
366                                 }
367                                 else
368                                 {
369                                         cont->getView()->setState(NOTHING_HAPPENS);
370                                         cont->SetActive(false);
371                                 }                               
372                         }
373                         _worldState = CREATING_CONTOUR;
374                 }
375                 else if(command == FIN_CREATION_CONTOUR && _worldState == CREATING_CONTOUR)
376                 {
377                         
378                         _worldState = NOTHING_HAPPENS;
379                         int id = _controllers.size()-1;
380                         GObjectController* cont = _controllers[id];                     
381                         GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
382
383                         GObjectController* finPort = _controllers[idController];
384                         if(finPort->getGObjectType() == GPORT)
385                         {
386                                 GPortModel* modelPort = (GPortModel*)finPort->getModel();
387                                 modelContour->setEndPort(modelPort);
388                         }                       
389
390                         manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
391                         manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
392                         connView->Refresh();
393
394                         for(int i=0;i<_controllers.size();i++)
395                         {
396                                 GObjectController* cont = _controllers[i];
397                                 if(cont->getView()!=NULL)
398                                 {
399                                         cont->getView()->setState(NOTHING_HAPPENS);
400                                 }
401                                 cont->SetActive(true);                                                          
402                         }
403                 }
404         }
405
406         //=========================================================================
407
408 }  // EO namespace bbtk
409
410 // EOF
411