]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
Move box and refresh position ... ports don't refresh automatically their position...
[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                         
60                         
61                         vtkConeSource *cone = vtkConeSource::New();
62         
63                    cone->SetResolution(10);
64                         
65                    vtkPolyDataMapper *map = vtkPolyDataMapper::New();
66                    map->SetInput(cone->GetOutput());
67                         
68                    vtkActor *act = vtkActor::New();
69                         
70                    act->SetMapper(map);
71                         
72                    vtkPoints *_pts = vtkPoints::New();
73                         _pts->SetNumberOfPoints(4);
74
75                         _pts->SetPoint(0, -1    , -1    , 0 );
76                         _pts->SetPoint(1,  1    , -1    , 0 );
77                         _pts->SetPoint(2,  1    ,  1    , 0 );
78                         _pts->SetPoint(3, -1    ,  1    , 0 );
79         
80                         vtkCellArray *lines = vtkCellArray::New();
81                         lines->InsertNextCell(5);
82                         lines->InsertCellPoint(0);
83                         lines->InsertCellPoint(1);
84                         lines->InsertCellPoint(2);
85                         lines->InsertCellPoint(3);
86                         lines->InsertCellPoint(0);
87
88                         vtkPolyData *_pd = vtkPolyData::New();
89                         _pd->SetPoints( _pts );
90                         _pd->SetLines( lines );
91
92                         vtkActor *_pointVtkActor        =       vtkActor::New();
93                         vtkPolyDataMapper* _bboxMapper          =       vtkPolyDataMapper::New();
94
95                         _bboxMapper->SetInput(_pd);
96                         _pointVtkActor->SetMapper(_bboxMapper);
97
98                         getRenderer()->AddActor(_pointVtkActor);
99
100                         
101                         
102                         // ******* TO ERASE *******
103                         /////////////////////
104                         
105                 }
106         }
107
108         //=========================================================================
109         void wxVtkSceneManager::disconnectDrop()
110         {
111                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
112         }
113         //=========================================================================
114         wxVtkSceneManager::~wxVtkSceneManager()
115         {
116         }
117         //=========================================================================
118
119         void wxVtkSceneManager::configureBaseView()
120         {
121                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
122
123                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
124
125                 // Important to activate the 2D interaction system
126                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
127                 interactorstylebaseview->SetInteractor ( iren );
128                 iren->SetInteractorStyle(interactorstylebaseview);
129                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
130                 
131                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
132                 _baseView->GetRenderer()->GradientBackgroundOff();
133                 _baseView->Refresh();
134
135         }
136
137         //=========================================================================
138
139         void wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxName)
140         {
141
142                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
143                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
144
145                 int type = GBLACKBOX;
146
147                 //Create the MVC Objects
148                 
149                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
150                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
151                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
152                 
153                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxName);
154                 
155                 //Prepares the initial model
156                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
157
158                 double xx = x;
159                 double yy =  windowHeight-y;
160                 
161                 //z value is not important yet, because it is only used a parallel projection
162                 double zz = 0;
163
164                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
165                 model->setInicPoint(xx,yy,zz);
166                 
167                 model->addObserver(view);
168                 model->addObserver(this);
169
170                 //Iterate and create the input ports
171                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
172                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
173
174                 int i=0;
175                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
176                 {
177                         BlackBoxInputDescriptor *desc = itInput->second;
178                         createGInputPort(desc,model,i);
179                         i++;
180                 }
181
182                 //Iterate and create the output ports
183                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
184                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
185
186                 i=0;
187                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
188                 {
189                         BlackBoxOutputDescriptor *desc = itOutput->second;
190                         createGOutputPort(desc,model,i);
191                         i++;
192                 }
193
194
195                 //Associates the view with the correspondent renderer and the  model.
196                 //(NOTE: Refresh is only made by the view)
197                 view->setModel(model);
198                 view->setBaseView(_baseView);
199                 view->initVtkObjects();
200                 
201                 //Associates the controller with the correspondent model and view
202                 controller->setModelAndView(model,view);
203
204                 //Resgiter change to the observers of the actual model
205                 model->setChanged();
206                 model->notifyObservers();
207                 
208                 //Register the controller of the new object
209                 registerController((InteractorStyleMaracas*) controller);
210
211                 //Add the object to the objects list (only boxes and connectors)
212                 _objects.push_back(model);
213
214         }
215
216         //=========================================================================
217
218         void wxVtkSceneManager::createGOutputPort(BlackBoxOutputDescriptor *desc,GBlackBoxModel *blackBox, int pos)
219         {
220                 int type = GPORT;
221
222                 //Create the MVC Objects
223                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
224                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
225                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
226
227                 model->registerInBox(blackBox,GOUTPUTPORT, pos);
228                 blackBox->addOutputPort(model);
229                 
230                 model->addObserver(view);
231                 model->addObserver(this);
232
233                 //Associates the view with the correspondent renderer and the  model.
234                 //(NOTE: Refresh is only made by the view)
235                 view->setModel(model);
236                 view->setBaseView(_baseView);
237                 view->initVtkObjects();
238                 
239                 //Associates the controller with the correspondent model and view
240                 controller->setModelAndView(model,view);
241
242                 //Register the controller of the new object
243                 registerController((InteractorStyleMaracas*) controller);
244         }
245
246         //=========================================================================
247
248         void wxVtkSceneManager::createGInputPort(BlackBoxInputDescriptor *desc,GBlackBoxModel *blackBox, int pos)
249         {
250                 int type = GPORT;
251
252                 //Create the MVC Objects
253                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
254                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
255                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
256
257                 model->registerInBox(blackBox,GINPUTPORT,pos);
258                 blackBox->addInputPort(model);
259
260                 model->addObserver(view);
261                 model->addObserver(this);
262
263                 //Associates the view with the correspondent renderer and the  model.
264                 //(NOTE: Refresh is only made by the view)
265                 view->setModel(model);
266                 view->setBaseView(_baseView);
267                 view->initVtkObjects();
268                 
269                 //Associates the controller with the correspondent model and view
270                 controller->setModelAndView(model,view);
271
272                 //Register the controller of the new object
273                 registerController((InteractorStyleMaracas*) controller);
274         }
275
276         //=========================================================================
277
278         void wxVtkSceneManager::createGConnector(GPortModel* startPort)
279         {
280                 manualContourControler* manContourControl       = new manualContourControler();
281                 manualViewContour* manViewerContour     = new manualViewContour();
282                 manualContourModel* manContourModel     = new manualContourModel();
283
284                 
285                 manViewerContour->SetModel( manContourModel );
286                 manViewerContour->SetWxVtkBaseView( _baseView );
287                 manViewerContour->SetRange( 0.5 );
288                 manViewerContour->SetZ( 900 );
289
290                 manViewerContour->SetColorNormalContour(0, 0, 1);
291                 manViewerContour->SetColorEditContour(0.5, 0.5, 0.5);
292                 manViewerContour->SetColorSelectContour(1, 0.8, 0);
293                 manViewerContour->SetWidthLine(1);
294
295                 manContourControl->SetModelView( manContourModel , manViewerContour );
296                 manContourControl->Configure();
297                 int i,sizeLstPoints = manContourModel->GetSizeLstPoints();
298                 for ( i=0; i<sizeLstPoints; i++ )
299                 {
300                         manViewerContour->AddPoint();
301                 }
302
303                 manContourControl->CreateNewManualContour();
304                 manViewerContour->RefreshContour();                     
305                 registerController((InteractorStyleMaracas*) manContourControl);
306
307         }
308
309         //=========================================================================
310
311         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
312         {
313                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
314                 baseViewControlManager->AddInteractorStyleMaracas( param );
315         }
316
317         //=========================================================================
318
319         vtkRenderer* wxVtkSceneManager::getRenderer()
320         {
321                 return _baseView->GetRenderer();
322         }
323
324         //=========================================================================
325
326         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
327         {
328                 return _baseView->GetRenWin();
329         }
330         //=========================================================================
331
332         bool wxVtkSceneManager::OnMouseMove()
333         {
334                 return true;
335         }
336
337         //=========================================================================
338         
339         void wxVtkSceneManager::update(int command)
340         {
341                 
342                 if(command==INIT_CREATION_CONTOUR)
343                 {
344                         std::cout<<"wxVtkSceneManager::update size:"<<_objects.size()<<std::endl;
345                         for(int i = 0; i<_objects.size();i++)
346                         {
347                                 std::cout<<"wxVtkSceneManager::update type:"<<_objects[i]->getGObjectType()<<std::endl;
348                                 if(_objects[i]->getGObjectType() == GBLACKBOX)
349                                 {
350                                         
351                                         GPortModel* startInputPort=((GBlackBoxModel*)_objects[i])->getStartInputPort();
352                                         std::cout<<"wxVtkSceneManager::update "<<startInputPort<<std::endl;
353                                         createGConnector(startInputPort);
354
355                                 }
356                                 else
357                                 {
358                                         // The others must not react to events
359                                 }
360                         }
361                 }
362         }
363
364         //=========================================================================
365
366 }  // EO namespace bbtk
367
368 // EOF
369