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