1 #include <cpExtensions/QT/RendererWidget.h>
3 #ifdef cpExtensions_QT4
5 #include <vtkAxesActor.h>
6 #include <vtkInteractorStyle.h>
7 #include <vtkOrientationMarkerWidget.h>
8 #include <vtkRenderer.h>
9 #include <vtkRenderWindow.h>
11 // -------------------------------------------------------------------------
12 cpExtensions::QT::RendererWidget::
13 RendererWidget( QWidget* parent, Qt::WindowFlags f )
14 : Superclass( parent, f )
16 this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( );
17 this->GetRenderWindow( )->AddRenderer( this->m_Renderer );
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( );
28 this->SetQuadrant( 0 );
31 // -------------------------------------------------------------------------
32 cpExtensions::QT::RendererWidget::
37 // -------------------------------------------------------------------------
38 int cpExtensions::QT::RendererWidget::
41 return( this->m_Quadrant );
44 // -------------------------------------------------------------------------
45 void cpExtensions::QT::RendererWidget::
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 );
59 // -------------------------------------------------------------------------
60 vtkRenderer* cpExtensions::QT::RendererWidget::
63 return( this->m_Renderer );
66 // -------------------------------------------------------------------------
67 const vtkRenderer* cpExtensions::QT::RendererWidget::
70 return( this->m_Renderer );
73 // -------------------------------------------------------------------------
74 vtkInteractorStyle* cpExtensions::QT::RendererWidget::
78 dynamic_cast< vtkInteractorStyle* >(
79 this->GetInteractor( )->GetInteractorStyle( )
84 // -------------------------------------------------------------------------
85 const vtkInteractorStyle* cpExtensions::QT::RendererWidget::
88 // Ugly, but necessary :-(
89 Self* self = const_cast< Self* >( this );
92 dynamic_cast< const vtkInteractorStyle* >(
93 self->GetInteractor( )->GetInteractorStyle( )
100 // -------------------------------------------------------------------------
101 void cpExtensions::QT::RendererWidget::
102 SetStyle( vtkInteractorStyle* style )
104 this->GetInteractor( )->SetInteractorStyle( style );
107 // -------------------------------------------------------------------------
108 vtkCamera* cpExtensions::QT::RendererWidget::
111 return( this->m_Renderer->GetActiveCamera( ) );
114 // -------------------------------------------------------------------------
115 const vtkCamera* cpExtensions::QT::RendererWidget::
116 GetActiveCamera( ) const
118 return( this->m_Renderer->GetActiveCamera( ) );
121 // -------------------------------------------------------------------------
122 void cpExtensions::QT::RendererWidget::
123 AddViewProp( vtkProp* prop, const std::string& name )
127 if( this->m_ViewProps.find( name ) == this->m_ViewProps.end( ) )
129 vtkSmartPointer< vtkPropCollection > coll =
130 vtkSmartPointer< vtkPropCollection >::New( );
131 coll->AddItem( prop );
132 this->m_ViewProps[ name ] = coll;
133 this->m_Renderer->AddViewProp( prop );
140 // -------------------------------------------------------------------------
141 void cpExtensions::QT::RendererWidget::
142 AddViewProps( vtkPropCollection* props, const std::string& name )
146 if( this->m_ViewProps.find( name ) == this->m_ViewProps.end( ) )
148 this->m_ViewProps[ name ] = props;
149 props->InitTraversal( );
150 while( vtkProp* p = props->GetNextProp( ) )
151 this->m_Renderer->AddViewProp( p );
158 // -------------------------------------------------------------------------
159 void cpExtensions::QT::RendererWidget::
160 AddAuxViewProp( vtkProp* prop, const std::string& name )
164 if( this->m_ViewProps.find( name ) != this->m_ViewProps.end( ) )
166 auto a = this->m_AuxViewProps.find( name );
167 vtkSmartPointer< vtkPropCollection > coll;
168 if( a == this->m_AuxViewProps.end( ) )
170 coll = vtkSmartPointer< vtkPropCollection >::New( );
171 this->m_AuxViewProps[ name ] = coll;
175 coll->AddItem( prop );
176 this->m_Renderer->AddViewProp( prop );
183 // -------------------------------------------------------------------------
184 void cpExtensions::QT::RendererWidget::
185 AddAuxViewProps( vtkPropCollection* props, const std::string& name )
187 props->InitTraversal( );
188 while( auto p = props->GetNextProp( ) )
189 this->AddAuxViewProp( p, name );
192 // -------------------------------------------------------------------------
193 vtkPropCollection* cpExtensions::QT::RendererWidget::
194 GetViewProps( const std::string& name )
196 auto i = this->m_ViewProps.find( name );
197 if( i != this->m_ViewProps.end( ) )
203 // -------------------------------------------------------------------------
204 const vtkPropCollection* cpExtensions::QT::RendererWidget::
205 GetViewProps( const std::string& name ) const
207 auto i = this->m_ViewProps.find( name );
208 if( i != this->m_ViewProps.end( ) )
214 // -------------------------------------------------------------------------
215 vtkPropCollection* cpExtensions::QT::RendererWidget::
216 GetAuxViewProps( const std::string& name )
218 auto i = this->m_AuxViewProps.find( name );
219 if( i != this->m_AuxViewProps.end( ) )
225 // -------------------------------------------------------------------------
226 const vtkPropCollection* cpExtensions::QT::RendererWidget::
227 GetAuxViewProps( const std::string& name ) const
229 auto i = this->m_AuxViewProps.find( name );
230 if( i != this->m_AuxViewProps.end( ) )
236 // -------------------------------------------------------------------------
237 void cpExtensions::QT::RendererWidget::
238 RemoveViewProps( const std::string& name )
240 auto i = this->m_ViewProps.find( name );
241 if( i != this->m_ViewProps.end( ) )
243 i->second->InitTraversal( );
244 while( auto p = i->second->GetNextProp( ) )
245 this->m_Renderer->RemoveViewProp( p );
246 this->m_ViewProps.erase( i );
248 i = this->m_AuxViewProps.find( name );
249 if( i != this->m_AuxViewProps.end( ) )
251 i->second->InitTraversal( );
252 while( auto p = i->second->GetNextProp( ) )
253 this->m_Renderer->RemoveViewProp( p );
254 this->m_AuxViewProps.erase( i );
261 // -------------------------------------------------------------------------
262 void cpExtensions::QT::RendererWidget::
265 this->m_Renderer->RemoveAllViewProps( );
266 this->m_ViewProps.clear( );
267 this->m_AuxViewProps.clear( );
270 // -------------------------------------------------------------------------
271 void cpExtensions::QT::RendererWidget::
272 HideViewProps( const std::string& name )
274 auto i = this->m_ViewProps.find( name );
275 if( i != this->m_ViewProps.end( ) )
277 i->second->InitTraversal( );
278 while( auto p = i->second->GetNextProp( ) )
281 i = this->m_AuxViewProps.find( name );
282 if( i != this->m_AuxViewProps.end( ) )
284 i->second->InitTraversal( );
285 while( auto p = i->second->GetNextProp( ) )
293 // -------------------------------------------------------------------------
294 void cpExtensions::QT::RendererWidget::
295 ShowViewProps( const std::string& name )
297 auto i = this->m_ViewProps.find( name );
298 if( i != this->m_ViewProps.end( ) )
300 i->second->InitTraversal( );
301 while( auto p = i->second->GetNextProp( ) )
304 i = this->m_AuxViewProps.find( name );
305 if( i != this->m_AuxViewProps.end( ) )
307 i->second->InitTraversal( );
308 while( auto p = i->second->GetNextProp( ) )
318 // -------------------------------------------------------------------------
319 const cpExtensions::QT::RendererWidget::
320 TNamedActors& cpExtensions::QT::RendererWidget::
321 GetNamedActors( ) const
323 return( this->m_NamedActors );
326 // -------------------------------------------------------------------------
327 cpExtensions::QT::RendererWidget::
328 TProps& cpExtensions::QT::RendererWidget::
329 GetViewProps( const std::string& name )
332 auto i = this->m_NamedActors.find( name );
333 if( i == this->m_NamedActors.end( ) )
342 // -------------------------------------------------------------------------
343 const cpExtensions::QT::RendererWidget::
344 TProps& cpExtensions::QT::RendererWidget::
345 GetViewProps( const std::string& name ) const
347 static const TProps empty = NULL;
348 auto i = this->m_NamedActors.find( name );
349 if( i == this->m_NamedActors.end( ) )
355 // -------------------------------------------------------------------------
356 void cpExtensions::QT::RendererWidget::
357 AddViewProp( vtkProp* prop, const std::string& name )
359 if( this->m_NamedActors.find( name ) == this->m_NamedActors.end( ) )
361 TProps coll = TProps::New( );
362 coll->AddItem( prop );
363 this->m_NamedActors[ name ] = coll;
364 this->m_Renderer->AddViewProp( prop );
369 // -------------------------------------------------------------------------
370 void cpExtensions::QT::RendererWidget::
371 AddViewProps( vtkPropCollection* coll, const std::string& name )
373 if( this->m_NamedActors.find( name ) == this->m_NamedActors.end( ) )
375 this->m_NamedActors[ name ] = coll;
376 coll->InitTraversal( );
377 while( vtkProp* prop = coll->GetNextProp( ) )
378 this->m_Renderer->AddViewProp( prop );
383 // -------------------------------------------------------------------------
384 void cpExtensions::QT::RendererWidget::
387 this->m_Renderer->RemoveAllViewProps( );
388 this->m_NamedActors.clear( );
391 // -------------------------------------------------------------------------
392 void cpExtensions::QT::RendererWidget::
393 RemoveViewProps( const std::string& name )
395 auto a = this->m_NamedActors.find( name );
396 if( a != this->m_NamedActors.end( ) )
398 a->second->InitTraversal( );
399 while( vtkProp* prop = a->second->GetNextProp( ) )
400 this->m_Renderer->RemoveViewProp( prop );
401 this->m_NamedActors.erase( a );
406 // -------------------------------------------------------------------------
407 void cpExtensions::QT::RendererWidget::
408 HideViewProps( const std::string& name )
410 auto a = this->m_NamedActors.find( name );
411 if( a != this->m_NamedActors.end( ) )
413 a->second->InitTraversal( );
414 while( vtkProp* prop = a->second->GetNextProp( ) )
415 prop->VisibilityOff( );
420 // -------------------------------------------------------------------------
421 void cpExtensions::QT::RendererWidget::
422 ShowViewProps( const std::string& name )
424 auto a = this->m_NamedActors.find( name );
425 if( a != this->m_NamedActors.end( ) )
427 a->second->InitTraversal( );
428 while( vtkProp* prop = a->second->GetNextProp( ) )
429 prop->VisibilityOn( );
435 // -------------------------------------------------------------------------
436 void cpExtensions::QT::RendererWidget::
440 double bounds[ 6 ] = { 0, 1, 0, 1, 0, 1 };
444 auto props = this->m_ViewProps.begin( );
445 props != this->m_ViewProps.end( );
449 props->second->InitTraversal( );
451 auto prop = dynamic_cast< vtkProp3D* >( props->second->GetNextProp( ) )
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 ];
465 prop->GetBounds( bounds );
471 this->m_Renderer->ResetCamera( );
474 // -------------------------------------------------------------------------
475 void cpExtensions::QT::RendererWidget::
478 this->GetRenderWindow( )->Render( );
481 #endif // cpExtensions_QT4