]> Creatis software - bbtk.git/blob - packages/wxvtk/src/bbwxvtkViewer3D.cxx
1.6
[bbtk.git] / packages / wxvtk / src / bbwxvtkViewer3D.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbwxvtkViewer3D.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/03/07 13:18:43 $
7   Version:   $Revision: 1.20 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 /**
19  *  \file 
20  *  \brief 
21  */
22
23
24 #ifdef _USE_WXWIDGETS_
25
26
27 #include "bbwxvtkViewer3D.h"
28 #include "bbwxvtkPackage.h"
29
30 #include "vtkCamera.h"
31
32
33
34 namespace bbwxvtk
35 {
36   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wxvtk,Viewer3D);
37   
38   //-------------------------------------------------------------------------
39   Viewer3DWidget::Viewer3DWidget(Viewer3D* box, wxWindow *parent, bool stereo)
40     : 
41     wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
42     mBox(box)
43   {
44     wxPanel *panel = this;
45
46     wxvtkrenderwindowinteractor = new wxVTKRenderWindowInteractor(panel,-1);
47     wxvtkrenderwindowinteractor->UseCaptureMouseOn();
48
49    renderer     = vtkRenderer::New();
50    renderwindow = vtkRenderWindow::New();
51
52    renderwindow->AddRenderer(renderer);
53
54    if (stereo==true){
55       renderwindow->SetStereoCapableWindow(1);
56       //renderwindow->SetStereoTypeToCrystalEyes();  
57       renderwindow->SetStereoTypeToRedBlue();
58       renderwindow->SetStereoRender(1);
59    }
60
61    wxvtkrenderwindowinteractor->SetRenderWindow(renderwindow);
62    
63    interactorstyle = vtkInteractorStyleTrackballCamera ::New();
64    interactorstyle->SetInteractor (  wxvtkrenderwindowinteractor );
65    wxvtkrenderwindowinteractor->SetInteractorStyle( interactorstyle );
66    
67    int x1=0,x2=200,y1=0,y2=200,z1=0,z2=200;
68    double spx=1,spy=1,spz=1;
69    vtkCamera *camera = renderer->GetActiveCamera();
70    camera->SetViewUp ( spx*0, -spy*1, spz*0);
71    camera->SetPosition( spx*(x1+x2)/2, spy*(y1+y2)/2, -spz*10000000); 
72    camera->SetFocalPoint        ( spx*(x1+x2)/2 , spy*(y1+y2)/2 , spz*0); 
73    //           camera->SetParallelScale(  spx*(x2-x1)/2.0 );
74    renderer->ResetCamera(x1,x2,y1,y2,z1,z2);
75    
76    renderer->SetBackground(0.1, 0.1, 0.2);
77   
78    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
79    sizer->Add( wxvtkrenderwindowinteractor , 1, wxEXPAND, 0);
80    panel->SetSizer(sizer);
81    panel->SetAutoLayout(true);
82    panel->Layout();
83   
84   }
85   //-------------------------------------------------------------------------
86
87   //-------------------------------------------------------------------------
88   Viewer3DWidget::~Viewer3DWidget()
89   {
90     mBox->DisableObservers();
91     mBox->bbSetOutputWidget(NULL);
92     renderwindow->Delete();
93     renderer->Delete();
94     interactorstyle->Delete();
95     delete wxvtkrenderwindowinteractor;
96   }
97   //-------------------------------------------------------------------------
98   
99   //-------------------------------------------------------------------------
100   void Viewer3DWidget::Update()
101   {
102     //    wxvtkrenderwindowinteractor->Update();
103     wxvtkrenderwindowinteractor->Render();
104     renderer->ResetCameraClippingRange();
105   }
106   //-------------------------------------------------------------------------
107
108   //-------------------------------------------------------------------------
109   vtkRenderer *Viewer3DWidget::GetRenderer()
110   {
111      return renderer;
112   }
113   //-------------------------------------------------------------------------
114
115   //-------------------------------------------------------------------------
116   wxVTKRenderWindowInteractor * Viewer3DWidget::GetInteractor()
117   {
118     return wxvtkrenderwindowinteractor;
119   }
120   //-------------------------------------------------------------------------
121
122 void Viewer3DWidget::Refresh(bool eraseBackground , const wxRect* rect )
123 {
124 //  wxPanel::Refresh(false,rect);
125 }
126
127
128
129   //--------------------------------------------------------------------------
130   //-------------------------------------------------------------------------
131   //--------------------------------------------------------------------------
132   //--------------------------------------------------------------------------
133
134
135   BBTK_BLACK_BOX_IMPLEMENTATION(Viewer3D,bbtk::WxBlackBox);
136
137         
138         //-----------------------------------------------------------------     
139         void Viewer3D::bbUserSetDefaultValues()
140         { 
141                 firsttime=true;
142                 bbSetInputIn1(NULL);
143                 bbSetInputIn2(NULL);
144                 bbSetInputIn3(NULL);
145                 bbSetInputIn4(NULL);
146                 bbSetInputIn5(NULL);
147                 bbSetInputObs1(NULL);
148                 bbSetInputObs2(NULL);
149                 bbSetInputObs3(NULL);
150                 bbSetInputObs4(NULL);
151                 bbSetInputObs5(NULL);
152                 bbSetInputStereo(false);
153                 bbSetOutputRenderer( NULL );
154                 bbSetOutputInteractor( NULL );
155                 bbSetOutputWidget(NULL);
156         }
157         
158         //-----------------------------------------------------------------     
159         void Viewer3D::bbUserInitializeProcessing()
160         {
161         }
162         
163         //-----------------------------------------------------------------     
164         void Viewer3D::bbUserFinalizeProcessing()
165         {  
166           DisableObservers();
167         }
168
169         void Viewer3D::DisableObservers()
170         {  
171           Viewer3DWidget* w = (Viewer3DWidget*)bbGetOutputWidget();
172           if (w!=NULL)
173             { 
174               vtkRenderWindowInteractor *interactor = w->GetInteractor();
175               if (interactor!=NULL)
176                 {
177                   if (bbGetInputObs1()!=NULL) 
178                     {
179                       if (bbGetInputObs1()->GetInteractor()!=NULL)
180                         bbGetInputObs1()->EnabledOff();
181                     }
182                   if (bbGetInputObs2()!=NULL) 
183                     {
184                       if (bbGetInputObs2()->GetInteractor()!=NULL)
185                   //bbGetInputObs2()->SetInteractor(NULL);
186                       bbGetInputObs2()->EnabledOff();
187                     }
188                   if (bbGetInputObs3()!=NULL) 
189                     {
190                      if (bbGetInputObs3()->GetInteractor()!=NULL)
191                    //bbGetInputObs3()->SetInteractor(NULL);
192                       bbGetInputObs3()->EnabledOff();
193                     }
194                   if (bbGetInputObs4()!=NULL) 
195                     {
196                      if (bbGetInputObs4()->GetInteractor()!=NULL)
197                    //bbGetInputObs4()->SetInteractor(NULL);
198                       bbGetInputObs4()->EnabledOff();
199                     }
200                   if (bbGetInputObs5()!=NULL) 
201                     {
202                    if (bbGetInputObs5()->GetInteractor()!=NULL)
203                      //bbGetInputObs5()->SetInteractor(NULL);
204                       bbGetInputObs5()->EnabledOff();
205                     }
206                 }       
207             }
208         }
209         
210         //--------------------------------------------------------------------------
211   void Viewer3D::Process() 
212   { 
213           
214     if (!bbIsShown()) return;
215 //EED     
216 //        Viewer3DWidget* w = (Viewer3DWidget*)bbGetOutputWidget();
217 //        if (w)   w->Update();
218
219           
220           
221     Viewer3DWidget* w = (Viewer3DWidget*)bbGetOutputWidget();
222           if ((firsttime==true) && (w!=NULL)){ 
223                   firsttime=false;
224                   
225                   vtkRenderWindowInteractor *interactor = w->GetInteractor();
226                   if (interactor!=NULL)
227                   {
228
229                           if (bbGetInputObs1()!=NULL) 
230                           {
231                                   bbGetInputObs1()->SetInteractor(interactor);
232                                   bbGetInputObs1()->EnabledOn();
233                           }
234                           if (bbGetInputObs2()!=NULL) 
235                           {
236                                   bbGetInputObs2()->SetInteractor(interactor);
237                                   bbGetInputObs2()->EnabledOn();
238                           }
239                           if (bbGetInputObs3()!=NULL) 
240                           {
241                                   bbGetInputObs3()->SetInteractor(interactor);
242                                   bbGetInputObs3()->EnabledOn();
243                           }
244                           if (bbGetInputObs4()!=NULL) 
245                           {
246                                   bbGetInputObs4()->SetInteractor(interactor);
247                                   bbGetInputObs4()->EnabledOn();
248                           }
249                           if (bbGetInputObs5()!=NULL) 
250                           {
251                                   bbGetInputObs5()->SetInteractor(interactor);
252                                   bbGetInputObs5()->EnabledOn();
253                           }
254                           bbSetOutputInteractor(  w->GetInteractor() );
255                   } // interactor
256         
257                   vtkRenderer *renderer = w->GetRenderer();
258                   if (renderer!=NULL)
259                   {
260                           bbSetOutputRenderer( renderer );
261                           
262                           /*if (bbGetInputIn1()!=NULL) {renderer->AddActor( bbGetInputIn1() ); }
263                           if (bbGetInputIn2()!=NULL) {renderer->AddActor( bbGetInputIn2() ); }
264                           if (bbGetInputIn3()!=NULL) {renderer->AddActor( bbGetInputIn3() ); }
265                           if (bbGetInputIn4()!=NULL) {renderer->AddActor( bbGetInputIn4() ); }
266                           if (bbGetInputIn5()!=NULL) {renderer->AddActor( bbGetInputIn5() ); }
267                           renderer->ResetCamera();
268                           double bounds[6];
269                           renderer->ComputeVisiblePropBounds(bounds);
270                           renderer->ResetCameraClippingRange(bounds);*/
271                           
272                   } // renderer
273                   
274                                   
275           } // w && firsttime
276
277           if(bbGetOutputRenderer()!=NULL){
278                    vtkRenderer *renderer = w->GetRenderer();
279                   if (bbGetInputIn1()!=NULL) {renderer->AddActor( bbGetInputIn1() ); }
280                   if (bbGetInputIn2()!=NULL) {renderer->AddActor( bbGetInputIn2() ); }
281                   if (bbGetInputIn3()!=NULL) {renderer->AddActor( bbGetInputIn3() ); }
282                   if (bbGetInputIn4()!=NULL) {renderer->AddActor( bbGetInputIn4() ); }
283                   if (bbGetInputIn5()!=NULL) {renderer->AddActor( bbGetInputIn5() ); }
284                   renderer->ResetCamera();
285                   double bounds[6];
286                   renderer->ComputeVisiblePropBounds(bounds);
287                   renderer->ResetCameraClippingRange(bounds);
288
289           }
290
291           if (w!=NULL)   w->Update();
292
293   }
294   
295
296   /**
297    * \brief  Create Widget . 
298    *
299    *
300    */ 
301   void Viewer3D::CreateWidget(wxWindow* parent)
302   {
303  
304     bbtkDebugMessageInc("Core",9,"Viewer3D::CreateWidget()"<<std::endl);
305
306     Viewer3DWidget* w = new Viewer3DWidget(this,
307                                            parent,
308                                            //bbGetWxParent(),
309                                            bbGetInputStereo());
310     //EED  w->Update();
311     //...
312           
313     bbSetOutputWidget(w);
314     
315     bbtkDebugDecTab("Core",9);
316     
317   }
318
319   // This callback is necessary to get actual processing of the view 
320   // when window is shown
321   void  Viewer3D::OnShowWidget()
322   {
323     Process();
324         //    Viewer3DWidget* w = (Viewer3DWidget*)bbGetOutputWidget();
325     //    if (w) w->Update();
326   }
327
328
329 }//namespace bbwxvtk
330
331 #endif
332