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