]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
a95d229934a5fdb81b3d3ccd6b6bdcd654c30fa3
[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 id)
45         {
46                 _id=id;
47                 _baseView=baseView;
48                 if( _baseView!=NULL )
49                 {
50                         
51                         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(parent);
52                         registerController(this);
53                         configureBaseView();
54
55                         ///// ******* TO ERASE *******
56                         //JUST TO TEST
57                         //              
58                         /*
59                         vtkConeSource *cone = vtkConeSource::New();
60         
61                    cone->SetResolution(10);
62                         
63                    vtkPolyDataMapper *map = vtkPolyDataMapper::New();
64                    map->SetInput(cone->GetOutput());
65                         
66                    vtkActor *act = vtkActor::New();
67                         
68                    act->SetMapper(map);
69                         
70                    vtkPoints *_pts = vtkPoints::New();
71                         _pts->SetNumberOfPoints(4);
72
73                         _pts->SetPoint(0, -100  , -100  , 900 );
74                         _pts->SetPoint(1,  100  , -100  , 900 );
75                         _pts->SetPoint(2,  100  ,  100  , 900 );
76                         _pts->SetPoint(3, -100  ,  100  , 900 );
77         
78                         vtkCellArray *lines = vtkCellArray::New();
79                         lines->InsertNextCell(5);
80                         lines->InsertCellPoint(0);
81                         lines->InsertCellPoint(1);
82                         lines->InsertCellPoint(2);
83                         lines->InsertCellPoint(3);
84                         lines->InsertCellPoint(0);
85
86                         vtkPolyData *_pd = vtkPolyData::New();
87                         _pd->SetPoints( _pts );
88                         _pd->SetLines( lines );
89
90                         vtkActor *_pointVtkActor        =       vtkActor::New();
91                         vtkPolyDataMapper* _bboxMapper          =       vtkPolyDataMapper::New();
92
93                         _bboxMapper->SetInput(_pd);
94                         _pointVtkActor->SetMapper(_bboxMapper);
95
96                         getRenderer()->AddActor(_pointVtkActor);
97
98                         
99                         
100                                         
101                    vtkPoints *_pts2 = vtkPoints::New();
102                         _pts2->SetNumberOfPoints(4);
103
104                         _pts2->SetPoint(0, -105 , -100  , -50 );
105                         _pts2->SetPoint(1,  100 , -100  , -50 );
106                         _pts2->SetPoint(2,  100 ,  100  , -50 );
107                         _pts2->SetPoint(3, -105 ,  100  , -50 );
108         
109                         vtkCellArray *lines2 = vtkCellArray::New();
110                         lines2->InsertNextCell(5);
111                         lines2->InsertCellPoint(0);
112                         lines2->InsertCellPoint(1);
113                         lines2->InsertCellPoint(2);
114                         lines2->InsertCellPoint(3);
115                         lines2->InsertCellPoint(0);
116
117                         vtkPolyData *_pd2 = vtkPolyData::New();
118                         _pd2->SetPoints( _pts2 );
119                         _pd2->SetLines( lines2 );
120
121                         vtkActor *_pointVtkActor2       =       vtkActor::New();
122                         vtkPolyDataMapper* _bboxMapper2         =       vtkPolyDataMapper::New();
123
124                         _bboxMapper2->SetInput(_pd2);
125                         _pointVtkActor2->SetMapper(_bboxMapper2);
126
127                         getRenderer()->AddActor(_pointVtkActor2);
128
129                         */
130                         // ******* TO ERASE *******
131                         /////////////////////
132                         
133                 }
134         }
135
136
137         //=========================================================================
138
139         void wxVtkSceneManager::disconnectDrop()
140         {
141                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
142         }
143
144         //=========================================================================
145
146         wxVtkSceneManager::~wxVtkSceneManager()
147         {
148         }
149
150         //=========================================================================
151
152         void wxVtkSceneManager::configureBaseView()
153         {
154                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
155
156                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
157
158                 // Important to activate the 2D interaction system
159                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
160                 interactorstylebaseview->SetInteractor ( iren );
161                 iren->SetInteractorStyle(interactorstylebaseview);
162                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
163                 
164                 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
165                 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
166                 
167                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
168                 _baseView->GetRenderer()->GradientBackgroundOff();
169                 _baseView->Refresh();
170         }
171
172         //=========================================================================
173
174         void wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxName)
175         {
176
177                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
178                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
179
180                 int type = GBLACKBOX;
181
182                 //Create the MVC Objects
183                 
184                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
185                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
186                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
187                 
188                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxName);
189                 
190                 //Prepares the initial model
191                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
192
193                 double xx = x;
194                 double yy =  windowHeight-y;
195                 
196                 //z value is not important yet, because it is only used a parallel projection
197                 double zz = 900;
198
199                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
200                 model->setInicPoint(xx,yy,zz);
201                 
202                 int cantObjects = _objects.size();
203                 cantObjects++;
204                 std::stringstream stream;
205                 
206                 if(cantObjects<10)
207                 {
208                         stream << "Box0" << cantObjects;
209                 }
210                 else
211                 {
212                         stream << "Box" << cantObjects;
213                 }
214                 std::string arraystring = stream.str();
215
216                 model->setBBTKName(arraystring);
217                 model->setBBTKType(boxName);
218                                 
219                 model->addObserver(view);
220                 model->addObserver(this);
221
222                 //Iterate and create the input ports
223                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
224                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
225
226                 int i=0;
227                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
228                 {
229                         BlackBoxInputDescriptor *desc = itInput->second;
230                         createGInputPort(GINPUTPORT,i,model,desc);
231                         i++;
232                 }
233
234                 //Iterate and create the output ports
235                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
236                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
237
238                 i=0;
239                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
240                 {
241                         BlackBoxOutputDescriptor *desc = itOutput->second;
242                         createGOutputPort(GOUTPUTPORT,i,model,desc);
243                         i++;
244                 }
245
246
247                 //Associates the view with the correspondent renderer and the  model.
248                 //(NOTE: Refresh is only made by the view)
249                 view->setModel(model);
250                 view->setBaseView(_baseView);
251                 view->initVtkObjects();
252                 
253                 //Associates the controller with the correspondent model and view
254                 controller->setModelAndView(model,view);
255
256                 //Resgiter change to the observers of the actual model
257                 model->setChanged();
258                 model->notifyObservers();
259                 
260                 //Register the controller of the new object
261                 registerController((InteractorStyleMaracas*) controller);
262
263                 //Add the object to the objects list 
264                 _objects.push_back(controller);
265
266                 int newId = _controllers.size();
267                 controller->setId(newId);
268                 _controllers[newId] = controller;
269
270         }
271
272         //=========================================================================
273
274         void wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
275         {
276                 createGPort(portType,posinBox,blackBox);
277         }
278
279         //=========================================================================
280
281         void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
282         {
283                 createGPort(portType,posinBox,blackBox);
284         }
285
286         //=========================================================================
287
288         void wxVtkSceneManager::createGPort(int portType, int posInBox,GBlackBoxModel *blackBox)
289         {
290                 int type = GPORT;
291
292                 //Create the MVC Objects
293                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
294                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
295                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
296
297                 model->registerInBox(blackBox,portType, posInBox);
298                 blackBox->addOutputPort(model);
299                 
300                 model->addObserver(view);
301                 model->addObserver(this);
302
303                 //Associates the view with the correspondent renderer and the  model.
304                 //(NOTE: Refresh is only made by the view)
305                 view->setModel(model);
306                 view->setBaseView(_baseView);
307                 view->initVtkObjects();
308                 
309                 //Associates the controller with the correspondent model and view
310                 controller->setModelAndView(model,view);
311
312                 model->notifyObservers();
313
314                 //Register the controller of the new object
315                 registerController((InteractorStyleMaracas*) controller);
316                 
317                 int newId = _controllers.size();
318                 controller->setId(newId);
319                 _controllers[newId] = controller;
320         }
321
322         //=========================================================================
323
324         void wxVtkSceneManager::createGConnector(GPortModel* startPort)
325         {
326                 manualContourControler* manContourControl       = new manualContourControler();
327                 GConnectorView* manViewerContour        = new GConnectorView();
328                 manualContourModel* manContourModel     = new manualContourModel();
329                 manContourModel->SetCloseContour(false);
330
331                 manViewerContour->SetModel( manContourModel );
332                 manViewerContour->SetWxVtkBaseView( _baseView );
333                 manViewerContour->SetRange( 0.5 );
334                 manViewerContour->SetZ( 900 );
335
336                 manViewerContour->SetColorNormalContour(0, 0, 1);
337                 manViewerContour->SetColorEditContour(0.5, 0.5, 0.5);
338                 manViewerContour->SetColorSelectContour(1, 0.8, 0);
339                 manViewerContour->SetWidthLine(1);
340
341                 manContourControl->SetModelView( manContourModel , manViewerContour );
342                 manContourControl->Configure();
343                 int i,sizeLstPoints = manContourModel->GetSizeLstPoints();
344
345                 for ( i=0; i<sizeLstPoints; i++ )
346                 {
347                         manViewerContour->AddPoint();
348                 }
349
350                 manContourControl->CreateNewManualContour();
351
352                 manViewerContour->RefreshContour();
353
354
355                 double x,y,z;
356                 startPort->getCenter(x,y,z);
357
358                 manContourControl->SetState(1);
359                 manContourModel->SetCloseContour(false);
360                 
361                 manContourModel->AddPoint(x,y,z);
362                 manViewerContour->AddPoint();
363
364                 manContourModel->AddPoint(x,y,z);
365                 manViewerContour->AddPoint();
366
367                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
368                 manContourControl->_bakIdPoint=bak;
369                 manViewerContour->Refresh();
370
371                 manContourControl->SetMoving( false );
372
373                 registerController((InteractorStyleMaracas*) manContourControl);
374
375         }
376
377         //=========================================================================
378
379         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
380         {
381                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
382                 baseViewControlManager->AddInteractorStyleMaracas( param );
383         }
384
385         //=========================================================================
386
387         vtkRenderer* wxVtkSceneManager::getRenderer()
388         {
389                 return _baseView->GetRenderer();
390         }
391
392         //=========================================================================
393
394         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
395         {
396                 return _baseView->GetRenWin();
397         }
398         //=========================================================================
399
400         bool wxVtkSceneManager::OnMouseMove()
401         {
402                 return true;
403         }
404
405         //=========================================================================
406         
407         void wxVtkSceneManager::update(int idController,int command)
408         {
409                 
410                 if(command==INIT_CREATION_CONTOUR)
411                 {       
412                         GObjectController* cont = _controllers[idController];
413                         if(cont->getGObjectType() == GPORT)
414                         {
415                                 GPortController* controller = (GPortController*)cont;
416                                 vtkGPortView* view = (vtkGPortView*)controller->getView();
417
418                                 if(view->getState() == CREATING_CONTOUR)
419                                 {
420                                         //pre: The port in this state is an output port                                 
421                                         GPortModel* startOutputPort = (GPortModel*)controller->getModel();
422                                         createGConnector(startOutputPort);
423                                 }
424                         }
425                         
426                 }
427         }
428
429         //=========================================================================
430
431 }  // EO namespace bbtk
432
433 // EOF
434