]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
First prototype works. Basic boxes construction, evenct handling and colors represent...
[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                         vtkConeSource *cone = vtkConeSource::New();
59         
60                    cone->SetResolution(10);
61                         
62                    vtkPolyDataMapper *map = vtkPolyDataMapper::New();
63                    map->SetInput(cone->GetOutput());
64                         
65                    vtkActor *act = vtkActor::New();
66                         
67                    act->SetMapper(map);
68                         
69                    vtkPoints *_pts = vtkPoints::New();
70                         _pts->SetNumberOfPoints(4);
71
72                         _pts->SetPoint(0, -1    , -1    , 0 );
73                         _pts->SetPoint(1,  1    , -1    , 0 );
74                         _pts->SetPoint(2,  1    ,  1    , 0 );
75                         _pts->SetPoint(3, -1    ,  1    , 0 );
76         
77                         vtkCellArray *lines = vtkCellArray::New();
78                         lines->InsertNextCell(5);
79                         lines->InsertCellPoint(0);
80                         lines->InsertCellPoint(1);
81                         lines->InsertCellPoint(2);
82                         lines->InsertCellPoint(3);
83                         lines->InsertCellPoint(0);
84
85                         vtkPolyData *_pd = vtkPolyData::New();
86                         _pd->SetPoints( _pts );
87                         _pd->SetLines( lines );
88
89                         vtkActor *_pointVtkActor        =       vtkActor::New();
90                         vtkPolyDataMapper* _bboxMapper          =       vtkPolyDataMapper::New();
91
92                         _bboxMapper->SetInput(_pd);
93                         _pointVtkActor->SetMapper(_bboxMapper);
94
95                         getRenderer()->AddActor(_pointVtkActor);
96                         
97                         // ******* TO ERASE *******
98                         /////////////////////
99                         
100                 }
101         }
102
103         //=========================================================================
104         void wxVtkSceneManager::disconnectDrop()
105         {
106                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
107         }
108         //=========================================================================
109         wxVtkSceneManager::~wxVtkSceneManager()
110         {
111         }
112         //=========================================================================
113
114         void wxVtkSceneManager::configureBaseView()
115         {
116                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
117
118                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
119
120                 // Important to activate the 2D interaction system
121                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
122                 interactorstylebaseview->SetInteractor ( iren );
123                 iren->SetInteractorStyle(interactorstylebaseview);
124                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
125                 
126                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
127                 _baseView->GetRenderer()->GradientBackgroundOff();
128                 _baseView->Refresh();
129
130         }
131
132         //=========================================================================
133
134         void wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxName)
135         {
136
137                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
138                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
139
140                 int type = GBLACKBOX;
141
142                 //Create the MVC Objects
143                 
144                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
145                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
146                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
147                 
148                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxName);
149                 
150                 //Prepares the initial model
151                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
152
153                 double xx = x;
154                 double yy =  windowHeight-y;
155                 
156                 //z value is not important yet, because it is only used a parallel projection
157                 double zz = 0;
158
159                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
160                 model->setInicPoint(xx,yy,zz);
161                 
162                 model->addObserver(view);
163
164                 //Iterate and create the input ports
165                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
166                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
167
168                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
169                 {
170                         BlackBoxInputDescriptor *desc = itInput->second;
171                         createGInputPort(desc,model);
172                 }
173
174                 //Iterate and create the output ports
175                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
176                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
177
178
179                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
180                 {
181                         BlackBoxOutputDescriptor *desc = itOutput->second;
182                         createGOutputPort(desc,model);
183                         
184                 }
185
186
187                 //Associates the view with the correspondent renderer and the  model.
188                 //(NOTE: Refresh is only made by the view)
189                 view->setModel(model);
190                 view->setBaseView(_baseView);
191                 view->initVtkObjects();
192                 
193                 //Associates the controller with the correspondent model and view
194                 controller->setModelAndView(model,view);
195
196                 //Resgiter change to the observers of the actual model
197                 model->setChanged();
198                 model->notifyObservers();
199                 
200                 //Register the controller of the new object
201                 registerController((InteractorStyleMaracas*) controller);
202
203                 //Add the object to the objects list (only boxes and connectors)
204                 objects.push_back(model);
205
206         }
207
208         //=========================================================================
209
210         void wxVtkSceneManager::createGOutputPort(BlackBoxOutputDescriptor *desc,GBlackBoxModel *blackBox)
211         {
212                 int type = GPORT;
213
214                 //Create the MVC Objects
215                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
216                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
217                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
218
219                 model->registerInBox(blackBox,GOUTPUTPORT);
220                 blackBox->addOutputPort(model);
221                 
222                 //Associates the view with the correspondent renderer and the  model.
223                 //(NOTE: Refresh is only made by the view)
224                 view->setModel(model);
225                 view->setBaseView(_baseView);
226                 view->initVtkObjects();
227                 
228                 //Associates the controller with the correspondent model and view
229                 controller->setModelAndView(model,view);
230
231                 //Register the controller of the new object
232                 registerController((InteractorStyleMaracas*) controller);
233         }
234
235         //=========================================================================
236
237         void wxVtkSceneManager::createGInputPort(BlackBoxInputDescriptor *desc,GBlackBoxModel *blackBox)
238         {
239                 int type = GPORT;
240
241                 //Create the MVC Objects
242                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
243                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
244                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
245
246                 model->registerInBox(blackBox,GINPUTPORT);
247                 blackBox->addInputPort(model);
248
249
250                 //Associates the view with the correspondent renderer and the  model.
251                 //(NOTE: Refresh is only made by the view)
252                 view->setModel(model);
253                 view->setBaseView(_baseView);
254                 view->initVtkObjects();
255                 
256                 //Associates the controller with the correspondent model and view
257                 controller->setModelAndView(model,view);
258
259                 //Register the controller of the new object
260                 registerController((InteractorStyleMaracas*) controller);
261         }
262
263         //=========================================================================
264
265         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
266         {
267                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
268                 baseViewControlManager->AddInteractorStyleMaracas( param );
269         }
270
271         //=========================================================================
272
273         vtkRenderer* wxVtkSceneManager::getRenderer()
274         {
275                 return _baseView->GetRenderer();
276         }
277
278         //=========================================================================
279
280         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
281         {
282                 return _baseView->GetRenWin();
283         }
284         //=========================================================================
285
286         bool wxVtkSceneManager::OnMouseMove()
287         {
288                 return true;
289         }
290         //=========================================================================
291 }  // EO namespace bbtk
292
293 // EOF
294