]> Creatis software - cpPlugins.git/blob - appli/examples/example_MarchingCubes.cxx
...
[cpPlugins.git] / appli / examples / example_MarchingCubes.cxx
1 #include <cstdlib>
2 #include <iostream>
3 #include <string>
4
5 #include <cpPlugins/Interface/Plugins.h>
6
7 // -------------------------------------------------------------------------
8 typedef cpPlugins::Interface::Plugins TPlugins;
9
10 // -------------------------------------------------------------------------
11 int main( int argc, char* argv[] )
12 {
13   if( argc < 4 )
14   {
15     std::cerr
16       << "Usage: " << argv[ 0 ]
17       << " plugins_file"
18       << " input_image threshold output_image" << std::endl;
19     return( 1 );
20
21   } // fi
22
23   // Load plugins
24   cpPlugins::Interface::Plugins plugins;
25   if( !plugins.LoadPluginsPath( argv[ 1 ] ) )
26   {
27     std::cerr << "Failed to load plugins." << std::endl;
28     return( 1 );
29
30   } // fi
31
32   // Associate filenames
33   std::vector< std::string > fnames;
34   for( int i = 2; i < argc - 2; ++i )
35     fnames.push_back( argv[ i ] );
36   
37   // Read image
38   std::string filter = "cpPlugins::BasicFilters::MarchingCubes";
39   try
40   {
41     std::string name = plugins.ReadImage( fnames, "" );
42
43     if( !( plugins.ActivateFilter( filter ) ) )
44     {
45       std::cerr << "Filter \"" << filter << "\" not found." << std::endl;
46       return( 1 );
47
48     } // fi
49
50     // Connect IO objects
51     TPlugins::TStringContainer inputs, outputs;
52     plugins.GetActiveFilterInputsNames( inputs );
53     plugins.GetActiveFilterOutputsNames( outputs );
54
55     plugins.ConnectInputInActiveFilter( name, *( inputs.begin( ) ) );
56     //plugins.SetOutputNameInActiveFilter( "output_mesh", *( outputs.begin( ) ) );
57
58     // Configure filter
59     TPlugins::TParameters* params = plugins.GetActiveFilterParameters( );
60     params->AddToRealList( "Thresholds", std::atof( argv[ argc - 2 ] ) );
61
62     // Execute filter
63     TPlugins::TStringContainer generated_outputs;
64     if( !( plugins.UpdateActiveFilter( generated_outputs, name ) ) )
65     {
66       std::cerr << "Error using filter \"" << filter << "\"." << std::endl;
67       return( 1 );
68
69     } // fi
70
71     // Save mesh
72     std::string outmesh_name = *( generated_outputs.begin( ) );
73     if( !( plugins.WriteDataObject( argv[ argc - 1 ], outmesh_name ) ) )
74     {
75       std::cerr
76         << "Error writing result into \"" << argv[ argc - 1 ]
77         << "\"" << std::endl;
78       return( 1 );
79
80     } // fi
81   }
82   catch( std::exception& err )
83   {
84     std::cerr << err.what( ) << std::endl;
85     return( 1 );
86
87   } // yrt
88   return( 0 );
89 }
90
91 // eof - $RCSfile$