openModeller  Version 1.4.0
om_niche.cpp
Go to the documentation of this file.
00001 
00027 #include <openmodeller/om.hh>
00028 
00029 #include "getopts/getopts.h"
00030 
00031 #include "om_cmd_utils.hh"
00032 
00033 #include "graph/graphic.hh"
00034 
00035 #include <string.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 
00039 #include <stdexcept>
00040 
00041 #include <string>
00042 using std::string;
00043 
00044 float _zoom;
00045 int   _redraw    = 1;
00046 int   _last_draw = 0;
00047 int   _scale = 1;
00048 
00049 GColor _bg( 0, 140, 150 );
00050 
00051 int   _nmap;
00052 Map **_maps;
00053 
00054 OccurrencesPtr _presences;
00055 OccurrencesPtr _absences;
00056 
00057 OpenModeller _om;
00058 GImage  *_cnv;
00059 GImage  *_pm;
00060 GGraph *_graph;
00061 
00062 void draw();
00063 void draw_niche( GGraph *graph );
00064 void draw_occur( GGraph *graph, const OccurrencesPtr& oc, GColor color );
00065 
00066 
00067 
00068 /**************************************************************/
00069 /**************************** main ****************************/
00070 
00071 int
00072 main( int argc, char **argv )
00073 {
00074   Options opts;
00075   int option;
00076 
00077   // command-line parameters (short name, long name, description, take args)
00078   opts.addOption( "" , "log-level", "Set the log level (debug, warn, info, error)", true );
00079   opts.addOption( "v", "version"  , "Display version info"                        , false );
00080   opts.addOption( "o", "model"    , "File with serialized model"                  , true );
00081   opts.addOption( "s", "scale"    , "Scale factor for model output"               , true );
00082 
00083   std::string log_level("info");
00084   std::string model_file;
00085 
00086   if ( ! opts.parse( argc, argv ) ) {
00087 
00088     opts.showHelp( argv[0] ); 
00089     exit(0);
00090   }
00091 
00092   // Set up any related external resources
00093   setupExternalResources();
00094 
00095   OpenModeller om;
00096 
00097   while ( ( option = opts.cycle() ) >= 0 ) {
00098 
00099     switch ( option ) {
00100 
00101       case 0:
00102         log_level = opts.getArgs( option );
00103         break;
00104       case 1:
00105         printf( "om_niche %s\n", om.getVersion().c_str() );
00106         printf("This is free software; see the source for copying conditions. There is NO\n");
00107         printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
00108         exit(0);
00109         break;
00110       case 2:
00111         model_file = opts.getArgs( option );
00112         break;
00113       case 3:
00114         _scale = atoi( opts.getArgs( option ).c_str() );
00115         break;
00116       default:
00117         break;
00118     }
00119   }
00120 
00121   // Check parameters
00122 
00123   if ( model_file.empty() ) {
00124 
00125     printf( "Please specify a model file\n");
00126     exit(-1);
00127   }
00128 
00129   // Log stuff
00130 
00131   Log::Level level_code = getLogLevel( log_level );
00132 
00133   Log::instance()->setLevel( level_code );
00134 
00135   try {
00136 
00137     AlgorithmFactory::searchDefaultDirs();
00138 
00139     ConfigurationPtr input = Configuration::readXml( model_file.c_str() );
00140 
00141     _om.setModelConfiguration( input );
00142 
00143     EnvironmentPtr env = _om.getEnvironment();
00144 
00145     // Normalize environment if necessary
00146     // IMPORTANT: this must be done before calling "getExtremes" below, 
00147     // otherwise min/max won't be normalized.
00148     
00149     AlgorithmPtr alg = _om.getAlgorithm();
00150 
00151     alg->setNormalization( env );
00152 
00153     // Get environment info
00154     int nmap = env->numLayers();
00155 
00156     Sample min;
00157     Sample max;
00158 
00159     env->getExtremes( &min, &max );
00160 
00161     if ( nmap < 2 ) {
00162 
00163       Log::instance()->error( "Need more than one environmental variable!\n" );
00164       exit(2);
00165     }
00166     else if ( nmap > 2 ) {
00167 
00168       Log::instance()->error( "Maximum number of environmental variables (2) exceeded!\n" );
00169       exit(3);
00170     }
00171 
00172     // Occurrences
00173     SamplerPtr samp = _om.getSampler();
00174 
00175     _presences = samp->getPresences();
00176     _absences = samp->getAbsences();
00177 
00178     // Instantiate graphical window
00179     int dimx = 256;
00180     int dimy = 256;
00181     GFrame *frame = createFrame( "openModeller Niche Viewer", 1, dimx, dimy );
00182     _cnv = frame->newCanvas( 0, 0, dimx, dimy );
00183     _pm  = frame->newPixmap( _cnv, dimx, dimy );
00184 
00185     // Drawing area
00186     _graph = new GGraph( _pm );
00187     _graph->scale( min[0], min[1], max[0], max[1] );
00188     _graph->background( _bg );
00189     _graph->clear();
00190 
00191     // Zoom in and out with mouse buttons
00192     _zoom = 2.0;
00193 
00194     frame->funcShow( draw );
00195 
00196     frame->exec();
00197 
00198     delete _graph;
00199     delete frame;
00200   }
00201   catch ( std::exception& e ) {
00202 
00203     Log::instance()->error( "Exception occurred: %s\n", e.what() );
00204   }
00205   catch ( ... ) {
00206 
00207     Log::instance()->error( "Unknown Error occurred\n" );
00208   }
00209 
00210   return 0;
00211 }
00212 
00213 
00214 /**************************************************************/
00215 
00216 /************/
00217 /*** draw ***/
00218 void
00219 draw()
00220 {
00221   if ( _redraw ) {
00222 
00223     draw_niche( _graph );
00224     draw_occur( _graph, _presences, GColor::Green );
00225     draw_occur( _graph, _absences, GColor::Red );
00226 
00227     _redraw = 0;
00228   }
00229 
00230   _cnv->put( _graph );
00231 }
00232 
00233 
00234 /******************/
00235 /*** draw niche ***/
00236 void
00237 draw_niche( GGraph *graph )
00238 {
00239   Scalar xmin  = graph->minX();
00240   Scalar ymin  = graph->minY();
00241   Scalar xmax  = graph->maxX();
00242   Scalar ymax  = graph->maxY();
00243   Scalar xstep = graph->stepX();
00244   Scalar ystep = graph->stepY();
00245 
00246   int i = 0;
00247   Scalar amb[2];
00248   Scalar *x = amb;
00249   Scalar *y = amb + 1;
00250 
00251   for ( *y = ymin; *y < ymax; *y += ystep ) {
00252 
00253     for ( *x = xmin; *x < xmax; *x += xstep ) {
00254 
00255       GColor color = GColor::Blue;
00256       color.scale( _scale*_om.getValue( amb ) );
00257       graph->pixel( float(*x), float(*y), color );
00258     }
00259 
00260     if ( ! ( ++i % 10 ) ) {
00261 
00262   _cnv->put( graph );
00263     }
00264   }
00265 }
00266 
00267 
00268 /******************/
00269 /*** draw occur ***/
00270 void
00271 draw_occur( GGraph *graph, const OccurrencesPtr& occurs, GColor color )
00272 {
00273   if ( occurs && occurs->numOccurrences() ) {
00274 
00275     // Draw each set of occurrences.
00276     EnvironmentPtr env = _om.getEnvironment();
00277     Sample amb;
00278     OccurrencesImpl::const_iterator oc = occurs->begin();
00279 
00280     for ( ; oc != occurs->end(); ++oc ) {
00281 
00282       amb = env->get( (*oc)->x(), (*oc)->y() );
00283 
00284       if ( amb.size() >= 2 ) {
00285 
00286         graph->markAxe( amb[0], amb[1], 1, color );
00287       }
00288     }
00289   }
00290 }
00291