]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/QT/RendererWidget.cxx
5d44fcdad65aac0ebb7e96da5ee9011641e723c5
[cpPlugins.git] / lib / cpExtensions / QT / RendererWidget.cxx
1 #include <cpExtensions/QT/RendererWidget.h>
2
3 #ifdef cpExtensions_QT4
4
5 #include <vtkAxesActor.h>
6 #include <vtkInteractorStyle.h>
7 #include <vtkOrientationMarkerWidget.h>
8 #include <vtkRenderer.h>
9 #include <vtkRenderWindow.h>
10
11 // -------------------------------------------------------------------------
12 cpExtensions::QT::RendererWidget::
13 RendererWidget( QWidget* parent, Qt::WindowFlags f )
14   : Superclass( parent, f )
15 {
16   this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( );
17   this->GetRenderWindow( )->AddRenderer( this->m_Renderer );
18
19   vtkAxesActor* axes = vtkAxesActor::New( );
20   axes->AxisLabelsOff( );
21   this->m_Marker = vtkSmartPointer< vtkOrientationMarkerWidget >::New( );
22   this->m_Marker->SetOutlineColor( 1, 1, 1 );
23   this->m_Marker->SetOrientationMarker( axes );
24   this->m_Marker->SetInteractor( this->GetRenderWindow( )->GetInteractor( ) );
25   this->m_Marker->EnabledOn( );
26   this->m_Marker->InteractiveOff( );
27   axes->Delete( );
28   this->SetQuadrant( 0 );
29 }
30
31 // -------------------------------------------------------------------------
32 cpExtensions::QT::RendererWidget::
33 ~RendererWidget( )
34 {
35 }
36
37 // -------------------------------------------------------------------------
38 int cpExtensions::QT::RendererWidget::
39 GetQuadrant( ) const
40 {
41   return( this->m_Quadrant );
42 }
43
44 // -------------------------------------------------------------------------
45 void cpExtensions::QT::RendererWidget::
46 SetQuadrant( int q )
47 {
48   this->m_Quadrant = ( q - 1 ) % 4;
49   if( this->m_Quadrant == 0 )
50     this->m_Marker->SetViewport( 0.85, 0.00, 1.00, 0.15 );
51   else if( this->m_Quadrant == 1 )
52     this->m_Marker->SetViewport( 0.00, 0.00, 0.15, 0.15 );
53   else if( this->m_Quadrant == 2 )
54     this->m_Marker->SetViewport( 0.00, 0.85, 0.15, 1.00 );
55   else if( this->m_Quadrant == 3 )
56     this->m_Marker->SetViewport( 0.85, 0.85, 1.00, 1.00 );
57 }
58
59 // -------------------------------------------------------------------------
60 vtkRenderer* cpExtensions::QT::RendererWidget::
61 GetRenderer( )
62 {
63   return( this->m_Renderer );
64 }
65
66 // -------------------------------------------------------------------------
67 const vtkRenderer* cpExtensions::QT::RendererWidget::
68 GetRenderer( ) const
69 {
70   return( this->m_Renderer );
71 }
72
73 // -------------------------------------------------------------------------
74 vtkInteractorStyle* cpExtensions::QT::RendererWidget::
75 GetStyle( )
76 {
77   return(
78     dynamic_cast< vtkInteractorStyle* >(
79       this->GetInteractor( )->GetInteractorStyle( )
80       )
81     );
82 }
83
84 // -------------------------------------------------------------------------
85 const vtkInteractorStyle* cpExtensions::QT::RendererWidget::
86 GetStyle( ) const
87 {
88   // Ugly, but necessary :-(
89   Self* self = const_cast< Self* >( this );
90   if( self != NULL )
91     return(
92       dynamic_cast< const vtkInteractorStyle* >(
93         self->GetInteractor( )->GetInteractorStyle( )
94         )
95       );
96   else
97     return( NULL );
98 }
99
100 // -------------------------------------------------------------------------
101 void cpExtensions::QT::RendererWidget::
102 SetStyle( vtkInteractorStyle* style )
103 {
104   this->GetInteractor( )->SetInteractorStyle( style );
105 }
106
107 // -------------------------------------------------------------------------
108 vtkCamera* cpExtensions::QT::RendererWidget::
109 GetActiveCamera( )
110 {
111   return( this->m_Renderer->GetActiveCamera( ) );
112 }
113
114 // -------------------------------------------------------------------------
115 const vtkCamera* cpExtensions::QT::RendererWidget::
116 GetActiveCamera( ) const
117 {
118   return( this->m_Renderer->GetActiveCamera( ) );
119 }
120
121 // -------------------------------------------------------------------------
122 void cpExtensions::QT::RendererWidget::
123 AddViewProp( vtkProp* prop, const std::string& name )
124 {
125   if( prop != NULL )
126   {
127     if( this->m_ViewProps.find( name ) == this->m_ViewProps.end( ) )
128     {
129       vtkSmartPointer< vtkPropCollection > coll =
130         vtkSmartPointer< vtkPropCollection >::New( );
131       coll->AddItem( prop );
132       this->m_ViewProps[ name ] = coll;
133       this->m_Renderer->AddViewProp( prop );
134
135     } // fi
136
137   } // fi
138 }
139
140 // -------------------------------------------------------------------------
141 void cpExtensions::QT::RendererWidget::
142 AddViewProps( vtkPropCollection* props, const std::string& name )
143 {
144   if( props != NULL )
145   {
146     if( this->m_ViewProps.find( name ) == this->m_ViewProps.end( ) )
147     {
148       this->m_ViewProps[ name ] = props;
149       props->InitTraversal( );
150       while( vtkProp* p = props->GetNextProp( ) )
151         this->m_Renderer->AddViewProp( p );
152
153     } // fi
154
155   } // fi
156 }
157
158 // -------------------------------------------------------------------------
159 void cpExtensions::QT::RendererWidget::
160 AddAuxViewProp( vtkProp* prop, const std::string& name )
161 {
162   if( prop != NULL )
163   {
164     if( this->m_ViewProps.find( name ) != this->m_ViewProps.end( ) )
165     {
166       auto a = this->m_AuxViewProps.find( name );
167       vtkSmartPointer< vtkPropCollection > coll;
168       if( a == this->m_AuxViewProps.end( ) )
169       {
170         coll = vtkSmartPointer< vtkPropCollection >::New( );
171         this->m_AuxViewProps[ name ] = coll;
172       }
173       else
174         coll = a->second;
175       coll->AddItem( prop );
176       this->m_Renderer->AddViewProp( prop );
177
178     } // fi
179
180   } // fi
181 }
182
183 // -------------------------------------------------------------------------
184 void cpExtensions::QT::RendererWidget::
185 AddAuxViewProps( vtkPropCollection* props, const std::string& name )
186 {
187   props->InitTraversal( );
188   while( auto p = props->GetNextProp( ) )
189     this->AddAuxViewProp( p, name );
190 }
191
192 // -------------------------------------------------------------------------
193 vtkPropCollection* cpExtensions::QT::RendererWidget::
194 GetViewProps( const std::string& name )
195 {
196   auto i = this->m_ViewProps.find( name );
197   if( i != this->m_ViewProps.end( ) )
198     return( i->second );
199   else
200     return( NULL );
201 }
202
203 // -------------------------------------------------------------------------
204 const vtkPropCollection* cpExtensions::QT::RendererWidget::
205 GetViewProps( const std::string& name ) const
206 {
207   auto i = this->m_ViewProps.find( name );
208   if( i != this->m_ViewProps.end( ) )
209     return( i->second );
210   else
211     return( NULL );
212 }
213
214 // -------------------------------------------------------------------------
215 vtkPropCollection* cpExtensions::QT::RendererWidget::
216 GetAuxViewProps( const std::string& name )
217 {
218   auto i = this->m_AuxViewProps.find( name );
219   if( i != this->m_AuxViewProps.end( ) )
220     return( i->second );
221   else
222     return( NULL );
223 }
224
225 // -------------------------------------------------------------------------
226 const vtkPropCollection* cpExtensions::QT::RendererWidget::
227 GetAuxViewProps( const std::string& name ) const
228 {
229   auto i = this->m_AuxViewProps.find( name );
230   if( i != this->m_AuxViewProps.end( ) )
231     return( i->second );
232   else
233     return( NULL );
234 }
235
236 // -------------------------------------------------------------------------
237 void cpExtensions::QT::RendererWidget::
238 RemoveViewProps( const std::string& name )
239 {
240   auto i = this->m_ViewProps.find( name );
241   if( i != this->m_ViewProps.end( ) )
242   {
243     i->second->InitTraversal( );
244     while( auto p = i->second->GetNextProp( ) )
245       this->m_Renderer->RemoveViewProp( p );
246     this->m_ViewProps.erase( i );
247
248     i = this->m_AuxViewProps.find( name );
249     if( i != this->m_AuxViewProps.end( ) )
250     {
251       i->second->InitTraversal( );
252       while( auto p = i->second->GetNextProp( ) )
253         this->m_Renderer->RemoveViewProp( p );
254       this->m_AuxViewProps.erase( i );
255
256     } // fi
257
258   } // fi
259 }
260
261 // -------------------------------------------------------------------------
262 void cpExtensions::QT::RendererWidget::
263 RemoveViewProps( )
264 {
265   this->m_Renderer->RemoveAllViewProps( );
266   this->m_ViewProps.clear( );
267   this->m_AuxViewProps.clear( );
268 }
269
270 // -------------------------------------------------------------------------
271 void cpExtensions::QT::RendererWidget::
272 HideViewProps( const std::string& name )
273 {
274   auto i = this->m_ViewProps.find( name );
275   if( i != this->m_ViewProps.end( ) )
276   {
277     i->second->InitTraversal( );
278     while( auto p = i->second->GetNextProp( ) )
279       p->VisibilityOff( );
280
281     i = this->m_AuxViewProps.find( name );
282     if( i != this->m_AuxViewProps.end( ) )
283     {
284       i->second->InitTraversal( );
285       while( auto p = i->second->GetNextProp( ) )
286         p->VisibilityOff( );
287
288     } // fi
289
290   } // fi
291 }
292
293 // -------------------------------------------------------------------------
294 void cpExtensions::QT::RendererWidget::
295 ShowViewProps( const std::string& name )
296 {
297   auto i = this->m_ViewProps.find( name );
298   if( i != this->m_ViewProps.end( ) )
299   {
300     i->second->InitTraversal( );
301     while( auto p = i->second->GetNextProp( ) )
302       p->VisibilityOn( );
303
304     i = this->m_AuxViewProps.find( name );
305     if( i != this->m_AuxViewProps.end( ) )
306     {
307       i->second->InitTraversal( );
308       while( auto p = i->second->GetNextProp( ) )
309         p->VisibilityOn( );
310
311     } // fi
312
313   } // fi
314 }
315
316
317 /*
318 // -------------------------------------------------------------------------
319 const cpExtensions::QT::RendererWidget::
320 TNamedActors& cpExtensions::QT::RendererWidget::
321 GetNamedActors( ) const
322 {
323   return( this->m_NamedActors );
324 }
325
326 // -------------------------------------------------------------------------
327 cpExtensions::QT::RendererWidget::
328 TProps& cpExtensions::QT::RendererWidget::
329 GetViewProps( const std::string& name )
330 {
331   static TProps empty;
332   auto i = this->m_NamedActors.find( name );
333   if( i == this->m_NamedActors.end( ) )
334   {
335     empty = NULL;
336     return( empty );
337   }
338   else
339     return( i->second );
340 }
341
342 // -------------------------------------------------------------------------
343 const cpExtensions::QT::RendererWidget::
344 TProps& cpExtensions::QT::RendererWidget::
345 GetViewProps( const std::string& name ) const
346 {
347   static const TProps empty = NULL;
348   auto i = this->m_NamedActors.find( name );
349   if( i == this->m_NamedActors.end( ) )
350     return( empty );
351   else
352     return( i->second );
353 }
354
355 // -------------------------------------------------------------------------
356 void cpExtensions::QT::RendererWidget::
357 AddViewProp( vtkProp* prop, const std::string& name )
358 {
359   if( this->m_NamedActors.find( name ) == this->m_NamedActors.end( ) )
360   {
361     TProps coll = TProps::New( );
362     coll->AddItem( prop );
363     this->m_NamedActors[ name ] = coll;
364     this->m_Renderer->AddViewProp( prop );
365
366   } // fi
367 }
368
369 // -------------------------------------------------------------------------
370 void cpExtensions::QT::RendererWidget::
371 AddViewProps( vtkPropCollection* coll, const std::string& name )
372 {
373   if( this->m_NamedActors.find( name ) == this->m_NamedActors.end( ) )
374   {
375     this->m_NamedActors[ name ] = coll;
376     coll->InitTraversal( );
377     while( vtkProp* prop = coll->GetNextProp( ) )
378       this->m_Renderer->AddViewProp( prop );
379
380   } // fi
381 }
382
383 // -------------------------------------------------------------------------
384 void cpExtensions::QT::RendererWidget::
385 RemoveViewProps( )
386 {
387   this->m_Renderer->RemoveAllViewProps( );
388   this->m_NamedActors.clear( );
389 }
390
391 // -------------------------------------------------------------------------
392 void cpExtensions::QT::RendererWidget::
393 RemoveViewProps( const std::string& name )
394 {
395   auto a = this->m_NamedActors.find( name );
396   if( a != this->m_NamedActors.end( ) )
397   {
398     a->second->InitTraversal( );
399     while( vtkProp* prop = a->second->GetNextProp( ) )
400       this->m_Renderer->RemoveViewProp( prop );
401     this->m_NamedActors.erase( a );
402
403   } // fi
404 }
405
406 // -------------------------------------------------------------------------
407 void cpExtensions::QT::RendererWidget::
408 HideViewProps( const std::string& name )
409 {
410   auto a = this->m_NamedActors.find( name );
411   if( a != this->m_NamedActors.end( ) )
412   {
413     a->second->InitTraversal( );
414     while( vtkProp* prop = a->second->GetNextProp( ) )
415       prop->VisibilityOff( );
416
417   } // fi
418 }
419
420 // -------------------------------------------------------------------------
421 void cpExtensions::QT::RendererWidget::
422 ShowViewProps( const std::string& name )
423 {
424   auto a = this->m_NamedActors.find( name );
425   if( a != this->m_NamedActors.end( ) )
426   {
427     a->second->InitTraversal( );
428     while( vtkProp* prop = a->second->GetNextProp( ) )
429       prop->VisibilityOn( );
430
431   } // fi
432 }
433 */
434
435 // -------------------------------------------------------------------------
436 void cpExtensions::QT::RendererWidget::
437 ResetCamera( )
438 {
439   /* TODO
440      double bounds[ 6 ] = { 0, 1, 0, 1, 0, 1 };
441      double b[ 6 ];
442      bool start = true;
443      for(
444      auto props = this->m_ViewProps.begin( );
445      props != this->m_ViewProps.end( );
446      ++props
447      )
448      {
449      props->second->InitTraversal( );
450      while(
451      auto prop = dynamic_cast< vtkProp3D* >( props->second->GetNextProp( ) )
452      )
453      {
454      if( !start )
455      {
456      prop->GetBounds( b );
457      bounds[ 0 ] = ( b[ 0 ] < bounds[ 0 ] )? b[ 0 ]: bounds[ 0 ];
458      bounds[ 1 ] = ( bounds[ 1 ] < b[ 1 ] )? b[ 1 ]: bounds[ 1 ];
459      bounds[ 2 ] = ( b[ 2 ] < bounds[ 2 ] )? b[ 2 ]: bounds[ 2 ];
460      bounds[ 3 ] = ( bounds[ 3 ] < b[ 3 ] )? b[ 3 ]: bounds[ 3 ];
461      bounds[ 4 ] = ( b[ 4 ] < bounds[ 4 ] )? b[ 4 ]: bounds[ 4 ];
462      bounds[ 5 ] = ( bounds[ 5 ] < b[ 5 ] )? b[ 5 ]: bounds[ 5 ];
463      }
464      else
465      prop->GetBounds( bounds );
466
467      } // elihw
468
469      } // rof
470   */
471   this->m_Renderer->ResetCamera( );
472 }
473
474 // -------------------------------------------------------------------------
475 void cpExtensions::QT::RendererWidget::
476 Render( )
477 {
478   this->GetRenderWindow( )->Render( );
479 }
480
481 #endif // cpExtensions_QT4
482
483 // eof - $RCSfile$