]> Creatis software - cpPlugins.git/blob - appli/examples/plugins/ImageTracer.cxx
Start contour tracer widget debugging: change interactor style.
[cpPlugins.git] / appli / examples / plugins / ImageTracer.cxx
1 #include <cpExtensions/Config.h>
2
3 #ifdef cpExtensions_QT4
4
5 #include <cpExtensions/QT/SimpleMPRWidget.h>
6 #include <cpExtensions/QT/ImageWidget.h>
7 #include <cpPlugins/Interface/Workspace.h>
8
9 #include <QApplication>
10 #include <QWidget>
11 #include <QMainWindow>
12 #include <QHBoxLayout>
13
14 #include <vtkImageData.h>
15
16
17 #include <vtkImageTracerWidget.h>
18 #include <vtkSmartPointer.h>
19 #include <vtkGlyphSource2D.h>
20 #include <vtkProp.h>
21
22
23 #endif // cpExtensions_QT4
24
25 //----------------------------------------------------------------------------
26 int main( int argc, char* argv[] )
27 {
28 #ifdef cpExtensions_QT4
29
30   // Manage inputs
31   if( argc < 2 )
32   {
33     std::cerr << "Usage: " << argv[ 0 ] << " image_file(s)" << std::endl;
34     return( 1 );
35
36   } // fi
37
38   // Create interface
39   cpPlugins::Interface::Plugins::Pointer interface =
40     cpPlugins::Interface::Plugins::New( );
41   try
42   {
43     interface->LoadEnvironments( );
44     interface->GuessPlugins( );
45   }
46   catch( std::exception& err )
47   {
48     std::cerr << "Error caught: " << err.what( ) << std::endl;
49     return( 1 );
50
51   } // yrt
52
53   // Create workspace
54   typedef cpPlugins::Interface::Workspace _TWorkspace;
55   _TWorkspace::Pointer ws = _TWorkspace::New( );
56   ws->PrintExecutionOn( );
57
58   // Configure reader
59   auto reader = ws->CreateFilter( "IO", "ImageReader", "reader" );
60   auto params = reader->GetParameters( );
61   for( int i = 1; i < argc; ++i )
62     params->AddToOpenFileNameList( "FileNames",  argv[ i ] );
63
64   // Execute reader
65   try
66   {
67     reader->Update( );
68   }
69   catch( std::exception& err )
70   {
71     std::cerr << "Error caugth: " << err.what( ) << std::endl;
72     return( 1 );
73
74   } // yrt
75
76   vtkSmartPointer< vtkImageTracerWidget > itw;
77
78   // Configure viewer
79   auto image = reader->GetOutputData< vtkImageData >( "Output" );
80   auto dim = image->GetDataDimension( );
81   switch( dim )
82   {
83   case 2: case 3:
84   {
85     // Qt initialization
86     QApplication app( argc, argv );
87     QMainWindow wnd;
88     wnd.setGeometry( 0, 0, 1024, 768 );
89
90     // Create main widget
91     QWidget* wdg = NULL;
92     if( dim == 2 )
93     {
94       auto view = new cpExtensions::QT::ImageWidget( &wnd );
95       view->SetImage( image, 2, "image" );
96       view->SetQuadrant( 2 );
97       view->SetImageOpacity( 0.5 );
98       wdg = view;
99
100       itw = vtkSmartPointer< vtkImageTracerWidget >::New( );
101       itw->SetCaptureRadius(1.5);
102       itw->GetGlyphSource()->SetColor(1, 0, 0);
103       itw->GetGlyphSource()->SetScale(3.0);
104       itw->GetGlyphSource()->SetRotationAngle(45.0);
105       itw->GetGlyphSource()->Modified();
106       itw->ProjectToPlaneOn();
107       itw->SetProjectionNormalToZAxes();
108       itw->SetProjectionPosition(
109         (
110           view->GetImageActor( )->GetBounds( )[ 4 ] +
111           view->GetImageActor( )->GetBounds( )[ 5 ]
112           ) / double( 2 )
113         );
114       itw->SetViewProp( view->GetImageActor( ) );
115       itw->SetInputData( image );
116       itw->SetInteractor( view->GetInteractor( ) );
117       itw->PlaceWidget( );
118       itw->SnapToImageOn( );
119       itw->AutoCloseOn( );
120
121       /* TODO
122          vtkSmartPointer< vtkSplineWidget > isw =
123          vtkSmartPointer< vtkSplineWidget >::New( );
124          isw.SetCurrentRenderer(ren2)
125          isw.SetDefaultRenderer(ren2)
126          isw.SetInputConnection(extract.GetOutputPort())
127          isw.SetInteractor(iren)
128          bnds = imageActor2.GetBounds()
129          isw.PlaceWidget(bnds[0], bnds[1], bnds[2], bnds[3], bnds[4], bnds[5])
130          isw.ProjectToPlaneOn()
131          isw.SetProjectionNormalToXAxes()
132          isw.SetProjectionPosition(pos)
133       */
134
135       itw->On( );
136     }
137     else
138     {
139       auto view = new cpExtensions::QT::SimpleMPRWidget( &wnd );
140       view->SetImage( image, "image" );
141       wdg = view;
142
143     } // fi
144
145     // Start application and show data
146     wnd.setCentralWidget( wdg );
147     wnd.show( );
148     return( app.exec( ) );
149   }
150   break;
151   default:
152   {
153     std::cerr << "Unmanaged dimension." << std::endl;
154     return( 1 );
155   }
156   break;
157
158   } // hctiws
159 #else // cpExtensions_QT4
160   return( 0 );
161 #endif // cpExtensions_QT4
162 }
163
164 // eof - $RCSfile$