]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
8592cc116e55f4abbc6d408bc4d4a7fa7fa82714
[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         void wxVtkSceneManager::disconnectDrop()
139         {
140                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
141         }
142         //=========================================================================
143         wxVtkSceneManager::~wxVtkSceneManager()
144         {
145         }
146         //=========================================================================
147
148         void wxVtkSceneManager::configureBaseView()
149         {
150                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
151
152                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
153
154                 // Important to activate the 2D interaction system
155                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
156                 interactorstylebaseview->SetInteractor ( iren );
157                 iren->SetInteractorStyle(interactorstylebaseview);
158                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
159                 
160                 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
161                 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,900);
162                 
163
164                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
165                 _baseView->GetRenderer()->GradientBackgroundOff();
166                 _baseView->Refresh();
167         }
168
169         //=========================================================================
170
171         void wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxName)
172         {
173
174                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
175                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
176
177                 int type = GBLACKBOX;
178
179                 //Create the MVC Objects
180                 
181                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
182                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
183                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
184                 
185                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxName);
186                 
187                 //Prepares the initial model
188                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
189
190                 double xx = x;
191                 double yy =  windowHeight-y;
192                 
193                 //z value is not important yet, because it is only used a parallel projection
194                 double zz = 900;
195
196                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
197                 model->setInicPoint(xx,yy,zz);
198                 
199                 int cantObjects = _objects.size();
200                 cantObjects++;
201                 std::stringstream stream;
202                 stream << "Box:Algo " << cantObjects;
203                 std::string arraystring = stream.str();
204
205                 model->setBBTKName(arraystring);
206                 model->setBBTKType(boxName);
207                                 
208                 model->addObserver(view);
209                 model->addObserver(this);
210
211                 //Iterate and create the input ports
212                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
213                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
214
215                 int i=0;
216                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
217                 {
218                         BlackBoxInputDescriptor *desc = itInput->second;
219                         createGInputPort(desc,model,i);
220                         i++;
221                 }
222
223                 //Iterate and create the output ports
224                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
225                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
226
227                 i=0;
228                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
229                 {
230                         BlackBoxOutputDescriptor *desc = itOutput->second;
231                         createGOutputPort(desc,model,i);
232                         i++;
233                 }
234
235
236                 //Associates the view with the correspondent renderer and the  model.
237                 //(NOTE: Refresh is only made by the view)
238                 view->setModel(model);
239                 view->setBaseView(_baseView);
240                 view->initVtkObjects();
241                 
242                 //Associates the controller with the correspondent model and view
243                 controller->setModelAndView(model,view);
244
245                 //Resgiter change to the observers of the actual model
246                 model->setChanged();
247                 model->notifyObservers();
248                 
249                 //Register the controller of the new object
250                 registerController((InteractorStyleMaracas*) controller);
251
252                 //Add the object to the objects list (only boxes and connectors)
253                 _objects.push_back(model);
254
255         }
256
257         //=========================================================================
258
259         void wxVtkSceneManager::createGOutputPort(BlackBoxOutputDescriptor *desc,GBlackBoxModel *blackBox, int pos)
260         {
261                 int type = GPORT;
262
263                 //Create the MVC Objects
264                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
265                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
266                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
267
268                 model->registerInBox(blackBox,GOUTPUTPORT, pos);
269                 blackBox->addOutputPort(model);
270                 
271                 model->addObserver(view);
272                 model->addObserver(this);
273
274                 //Associates the view with the correspondent renderer and the  model.
275                 //(NOTE: Refresh is only made by the view)
276                 view->setModel(model);
277                 view->setBaseView(_baseView);
278                 view->initVtkObjects();
279                 
280                 //Associates the controller with the correspondent model and view
281                 controller->setModelAndView(model,view);
282
283                 //Register the controller of the new object
284                 registerController((InteractorStyleMaracas*) controller);
285         }
286
287         //=========================================================================
288
289         void wxVtkSceneManager::createGInputPort(BlackBoxInputDescriptor *desc,GBlackBoxModel *blackBox, int pos)
290         {
291                 int type = GPORT;
292
293                 //Create the MVC Objects
294                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
295                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
296                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
297
298                 model->registerInBox(blackBox,GINPUTPORT,pos);
299                 blackBox->addInputPort(model);
300
301                 model->addObserver(view);
302                 model->addObserver(this);
303
304                 //Associates the view with the correspondent renderer and the  model.
305                 //(NOTE: Refresh is only made by the view)
306                 view->setModel(model);
307                 view->setBaseView(_baseView);
308                 view->initVtkObjects();
309                 
310                 //Associates the controller with the correspondent model and view
311                 controller->setModelAndView(model,view);
312
313                 //Register the controller of the new object
314                 registerController((InteractorStyleMaracas*) controller);
315         }
316
317         //=========================================================================
318
319         void wxVtkSceneManager::createGConnector(GPortModel* startPort)
320         {
321                 manualContourControler* manContourControl       = new manualContourControler();
322                 GConnectorView* manViewerContour        = new GConnectorView();
323                 manualContourModel* manContourModel     = new manualContourModel();
324                 manContourModel->SetCloseContour(false);
325
326                 manViewerContour->SetModel( manContourModel );
327                 manViewerContour->SetWxVtkBaseView( _baseView );
328                 manViewerContour->SetRange( 0.5 );
329                 manViewerContour->SetZ( 900 );
330
331                 manViewerContour->SetColorNormalContour(0, 0, 1);
332                 manViewerContour->SetColorEditContour(0.5, 0.5, 0.5);
333                 manViewerContour->SetColorSelectContour(1, 0.8, 0);
334                 manViewerContour->SetWidthLine(1);
335
336                 manContourControl->SetModelView( manContourModel , manViewerContour );
337                 manContourControl->Configure();
338                 int i,sizeLstPoints = manContourModel->GetSizeLstPoints();
339
340                 for ( i=0; i<sizeLstPoints; i++ )
341                 {
342                         manViewerContour->AddPoint();
343                 }
344
345                 manContourControl->CreateNewManualContour();
346
347                 manViewerContour->RefreshContour();
348
349                 registerController((InteractorStyleMaracas*) manContourControl);
350
351         }
352
353         //=========================================================================
354
355         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
356         {
357                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
358                 baseViewControlManager->AddInteractorStyleMaracas( param );
359         }
360
361         //=========================================================================
362
363         vtkRenderer* wxVtkSceneManager::getRenderer()
364         {
365                 return _baseView->GetRenderer();
366         }
367
368         //=========================================================================
369
370         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
371         {
372                 return _baseView->GetRenWin();
373         }
374         //=========================================================================
375
376         bool wxVtkSceneManager::OnMouseMove()
377         {
378                 return true;
379         }
380
381         //=========================================================================
382         
383         void wxVtkSceneManager::update(int command)
384         {
385                 
386                 if(command==INIT_CREATION_CONTOUR)
387                 {
388                         for(int i = 0; i<_objects.size();i++)
389                         {
390                                 if(_objects[i]->getGObjectType() == GBLACKBOX)
391                                 {
392                                         
393                                         GPortModel* startOutputPort=((GBlackBoxModel*)_objects[i])->getStartOutputPort();
394                                         createGConnector(startOutputPort);
395
396                                 }
397                                 else
398                                 {
399                                         // The others must not react to events
400                                 }
401                         }
402                 }
403         }
404
405         //=========================================================================
406
407 }  // EO namespace bbtk
408
409 // EOF
410