]> Creatis software - cpPlugins.git/blobdiff - plugins/VTKWidgets/LineWidget.cxx
yet another refactoring
[cpPlugins.git] / plugins / VTKWidgets / LineWidget.cxx
diff --git a/plugins/VTKWidgets/LineWidget.cxx b/plugins/VTKWidgets/LineWidget.cxx
new file mode 100644 (file)
index 0000000..24928e6
--- /dev/null
@@ -0,0 +1,173 @@
+#include <VTKWidgets/LineWidget.h>
+#include <cpInstances/DataObjects/Image.h>
+#include <cpInstances/DataObjects/Mesh.h>
+#include <cpExtensions/Visualization/WindowLevelImageActor.h>
+
+#include <vtkLineWidget2.h>
+#include <vtkImageData.h>
+#include <vtkImageMapper3D.h>
+#include <vtkInteractorObserver.h>
+#include <vtkLineRepresentation.h>
+#include <vtkMapper.h>
+#include <vtkPolyData.h>
+#include <vtkRenderer.h>
+
+// -------------------------------------------------------------------------
+void cpPluginsVTKWidgets::LineWidget::
+Clear( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsVTKWidgets::LineWidget::
+SetEnabled( bool v )
+{
+  auto wdg = this->GetVTK< vtkLineWidget2 >( );
+  if( wdg != NULL )
+  {
+    wdg->SetEnabled( v );
+    wdg->GetInteractor( )->Render( );
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+bool cpPluginsVTKWidgets::LineWidget::
+GetEnabled( ) const
+{
+  auto wdg = this->GetVTK< const vtkLineWidget2 >( );
+  if( wdg != NULL )
+  {
+    vtkLineWidget2* w = const_cast< vtkLineWidget2* >( wdg );
+    return( w->GetEnabled( ) != 0 );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsVTKWidgets::LineWidget::
+LineWidget( )
+  : Superclass( )
+{
+  typedef cpPlugins::Pipeline::DataObject _TData;
+  typedef cpInstances::DataObjects::Mesh       _TMesh;
+
+  this->_ConfigureInput< _TData >( "Input", false, false );
+  this->_ConfigureOutput< _TMesh >( "Output" );
+
+  // Create output data
+  auto line = this->_CreateVTK< vtkPolyData >( );
+  line->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
+  line->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
+  line->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
+  line->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
+  line->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
+  this->GetOutput( "Output" )->SetVTK( line );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsVTKWidgets::LineWidget::
+~LineWidget( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsVTKWidgets::LineWidget::
+_GenerateData( )
+{
+  if( this->m_Interactors.size( ) == 0 )
+    this->_Error( "Give at least one valid interactor." );
+
+  auto image = this->GetInputData< vtkImageData >( "Input" );
+  if( image != NULL ) this->_GD_Image( image );
+  else this->_Error( "Do not know how to create this widget." );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsVTKWidgets::LineWidget::
+TValidProps cpPluginsVTKWidgets::LineWidget::
+_GetValidActors( vtkObject* source )
+{
+  TValidProps valid_props;
+  auto iIt = this->m_Interactors.begin( );
+  for( ; iIt != this->m_Interactors.end( ); ++iIt )
+  {
+    auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
+    if( r != NULL )
+    {
+      auto props = r->GetViewProps( );
+      if( props != NULL )
+      {
+        props->InitTraversal( );
+        while( vtkProp* prop = props->GetNextProp( ) )
+        {
+          auto image_actor = dynamic_cast< vtkImageSlice* >( prop );
+          auto mesh_actor = dynamic_cast< vtkActor* >( prop );
+          vtkObject* input = NULL;
+          if( image_actor != NULL )
+          {
+            auto mapper = image_actor->GetMapper( );
+            if( mapper != NULL )
+              input = mapper->GetInput( );
+          }
+          else if( mesh_actor != NULL )
+          {
+            auto mapper = mesh_actor->GetMapper( );
+            if( mapper != NULL )
+              input = mapper->GetInput( );
+
+          } // fi
+          if( input != NULL )
+            valid_props[ *iIt ].insert( prop );
+
+        } // elihw
+
+      } // fi
+
+    } // fi
+
+  } // rof
+  return( valid_props );
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsVTKWidgets::LineWidget::
+_GD_Image( vtkImageData* image )
+{
+  auto valid_props = this->_GetValidActors( image );
+  if( valid_props.size( ) == 0 )
+    this->_Error( "Given image does not have a valid associated actor." );
+
+  vtkSmartPointer< vtkLineWidget2 > wdg = this->GetVTK< vtkLineWidget2 >( );
+  if( wdg.GetPointer( ) == NULL )
+  {
+    auto vIt = valid_props.begin( );
+    auto actor = dynamic_cast< vtkImageSlice* >( *( vIt->second.begin( ) ) );
+
+    double bnds[ 6 ];
+    actor->GetBounds( bnds );
+    double p0[ 3 ], p1[ 3 ];
+    p0[ 0 ] = bnds[ 0 ];
+    p0[ 1 ] = bnds[ 2 ];
+    p0[ 2 ] = bnds[ 4 ];
+    p1[ 0 ] = bnds[ 1 ];
+    p1[ 1 ] = bnds[ 3 ];
+    p1[ 2 ] = bnds[ 5 ];
+
+    auto rep = vtkSmartPointer< vtkLineRepresentation >::New( );
+    rep->SetPoint1WorldPosition( p0 );
+    rep->SetPoint2WorldPosition( p1 );
+    rep->PlaceWidget( bnds );
+
+    wdg = this->_CreateVTK< vtkLineWidget2 >( );
+    wdg->SetInteractor( valid_props.begin( )->first );
+    wdg->SetRepresentation( rep );
+    wdg->EnabledOn( );
+  }
+  else
+    dynamic_cast< vtkLineRepresentation* >( wdg->GetRepresentation( ) )->
+      GetPolyData( this->GetOutputData< vtkPolyData >( "Output" ) );
+}
+
+// eof - $RCSfile$