]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
4e337be93c3d97cc460fee84c9377324d2457934
[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->setChanged();
184                 model->notifyObservers();
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();
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* manViewerContour    = 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                 manViewerContour->SetModel( manContourModel );
265                 manViewerContour->SetWxVtkBaseView( _baseView );
266                 manViewerContour->SetRange( 0.5 );
267                 manViewerContour->SetZ( 900 );
268
269                 manViewerContour->SetColorNormalContour(0, 0, 1);
270                 manViewerContour->SetColorEditContour(0.5, 0.5, 0.5);
271                 manViewerContour->SetColorSelectContour(1, 0.8, 0);
272                 manViewerContour->SetWidthLine(1);
273
274                 manContourControl->SetModelView( manContourModel , manViewerContour );
275                 
276                 manContourControl->CreateNewManualContour();
277
278                 manViewerContour->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                 manViewerContour->AddPoint();
289
290                 manContourModel->AddPoint(x,y,z);
291                 manViewerContour->AddPoint();
292
293                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
294                 manContourControl->_bakIdPoint=bak;
295                 manViewerContour->Refresh();
296
297                 manContourControl->SetMoving( false );
298
299                 registerController((InteractorStyleMaracas*) connectorcontroller);
300
301                 connectorcontroller->setManualContourController(manContourControl);             
302                 connectorModel->setManualContourModel(manContourModel);
303                 connectorcontroller->setModelAndView(connectorModel,NULL);
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                         if(cont->getGObjectType() == GPORT)
348                         {
349                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
350                                 createGConnector(startOutputPort);
351                         }
352                         // The last one is the controller of the connector
353                         for(int i=0;i<_controllers.size()-1;i++)
354                         {
355                                 GObjectController* cont = _controllers[i];
356                                 if(cont->getGObjectType() == GPORT )
357                                 {
358                                         GPortModel* port = (GPortModel*)cont->getModel();
359                                         if(port->getPortType()==GINPUTPORT)
360                                         {
361                                                 cont->SetActive(true);
362                                         }
363                                         else
364                                         {
365                                                 cont->getView()->setState(NOTHING_HAPPENS);
366                                                 cont->SetActive(false);
367                                         }
368                                 }
369                                 else
370                                 {
371                                         cont->getView()->setState(NOTHING_HAPPENS);
372                                         cont->SetActive(false);
373                                 }                               
374                         }
375                         _worldState = CREATING_CONTOUR;
376                 }
377                 else if(command == FIN_CREATION_CONTOUR && _worldState == CREATING_CONTOUR)
378                 {
379                         _worldState = NOTHING_HAPPENS;
380                         int id = _controllers.size()-1;
381                         GObjectController* cont = _controllers[id];                     
382                         GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
383
384                         GObjectController* finPort = _controllers[idController];
385                         if(cont->getGObjectType() == GPORT)
386                         {
387                                 GPortModel* modelPort = (GPortModel*)finPort->getModel();
388                                 modelContour->setEndPort(modelPort);
389                         }                       
390
391                         manualContourControler* manCont = ((GConnectorController*)cont)->getManualContourController();
392                         
393                         //manCont->SetCompleteCreation( true );
394                         //manCont->SetKeyBoardMoving( false );
395                         //manCont->GetManualContourModel()->SetCloseContour(false);
396                         //manCont->SetEditable( false );
397                         //manCont->SetPosibleToMove( false );
398                         //manCont->SetState(0);
399
400                         for(int i=0;i<_controllers.size();i++)
401                         {
402                                 GObjectController* cont = _controllers[i];
403                                 if(cont->getView()!=NULL)
404                                 {
405                                         cont->getView()->setState(NOTHING_HAPPENS);
406                                 }
407                                 cont->SetActive(true);                                                          
408                         }
409                 }
410         }
411
412         //=========================================================================
413
414 }  // EO namespace bbtk
415
416 // EOF
417