openModeller  Version 1.5.0
om_niche.cpp
Go to the documentation of this file.
1 
27 #include <openmodeller/om.hh>
28 
29 #include "getopts/getopts.h"
30 
31 #include "om_cmd_utils.hh"
32 
33 #include "graph/graphic.hh"
34 
35 #include <string.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #include <stdexcept>
40 
41 #include <string>
42 using std::string;
43 
44 float _zoom;
45 int _redraw = 1;
46 int _last_draw = 0;
47 int _scale = 1;
48 
49 GColor _bg( 0, 140, 150 );
50 
51 int _nmap;
53 
56 
58 GImage *_cnv;
59 GImage *_pm;
60 GGraph *_graph;
61 
62 void draw();
63 void draw_niche( GGraph *graph );
64 void draw_occur( GGraph *graph, const OccurrencesPtr& oc, GColor color );
65 
66 
67 
68 /**************************************************************/
69 /**************************** main ****************************/
70 
71 int
72 main( int argc, char **argv )
73 {
74  Options opts;
75  int option;
76 
77  // command-line parameters (short name, long name, description, take args)
78  opts.addOption( "" , "log-level", "Set the log level (debug, warn, info, error)", true );
79  opts.addOption( "v", "version" , "Display version info" , false );
80  opts.addOption( "o", "model" , "File with serialized model" , true );
81  opts.addOption( "s", "scale" , "Scale factor for model output" , true );
82 
83  std::string log_level("info");
84  std::string model_file;
85 
86  if ( ! opts.parse( argc, argv ) ) {
87 
88  opts.showHelp( argv[0] );
89  exit(0);
90  }
91 
92  // Set up any related external resources
94 
95  OpenModeller om;
96 
97  while ( ( option = opts.cycle() ) >= 0 ) {
98 
99  switch ( option ) {
100 
101  case 0:
102  log_level = opts.getArgs( option );
103  break;
104  case 1:
105  printf( "om_niche %s\n", om.getVersion().c_str() );
106  printf("This is free software; see the source for copying conditions. There is NO\n");
107  printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
108  exit(0);
109  break;
110  case 2:
111  model_file = opts.getArgs( option );
112  break;
113  case 3:
114  _scale = atoi( opts.getArgs( option ).c_str() );
115  break;
116  default:
117  break;
118  }
119  }
120 
121  // Check parameters
122 
123  if ( model_file.empty() ) {
124 
125  printf( "Please specify a model file\n");
126  exit(-1);
127  }
128 
129  // Log stuff
130 
131  Log::Level level_code = getLogLevel( log_level );
132 
133  Log::instance()->setLevel( level_code );
134 
135  try {
136 
138 
139  ConfigurationPtr input = Configuration::readXml( model_file.c_str() );
140 
141  _om.setModelConfiguration( input );
142 
143  EnvironmentPtr env = _om.getEnvironment();
144 
145  // Normalize environment if necessary
146  // IMPORTANT: this must be done before calling "getExtremes" below,
147  // otherwise min/max won't be normalized.
148 
149  AlgorithmPtr alg = _om.getAlgorithm();
150 
151  alg->setNormalization( env );
152 
153  // Get environment info
154  int nmap = env->numLayers();
155 
156  Sample min;
157  Sample max;
158 
159  env->getExtremes( &min, &max );
160 
161  if ( nmap < 2 ) {
162 
163  Log::instance()->error( "Need more than one environmental variable!\n" );
164  exit(2);
165  }
166  else if ( nmap > 2 ) {
167 
168  Log::instance()->error( "Maximum number of environmental variables (2) exceeded!\n" );
169  exit(3);
170  }
171 
172  // Occurrences
173  SamplerPtr samp = _om.getSampler();
174 
175  _presences = samp->getPresences();
176  _absences = samp->getAbsences();
177 
178  // Instantiate graphical window
179  int dimx = 256;
180  int dimy = 256;
181  GFrame *frame = createFrame( "openModeller Niche Viewer", 1, dimx, dimy );
182  _cnv = frame->newCanvas( 0, 0, dimx, dimy );
183  _pm = frame->newPixmap( _cnv, dimx, dimy );
184 
185  // Drawing area
186  _graph = new GGraph( _pm );
187  _graph->scale( min[0], min[1], max[0], max[1] );
188  _graph->background( _bg );
189  _graph->clear();
190 
191  // Zoom in and out with mouse buttons
192  _zoom = 2.0;
193 
194  frame->funcShow( draw );
195 
196  frame->exec();
197 
198  delete _graph;
199  delete frame;
200  }
201  catch ( std::exception& e ) {
202 
203  Log::instance()->error( "Exception occurred: %s\n", e.what() );
204  }
205  catch ( ... ) {
206 
207  Log::instance()->error( "Unknown Error occurred\n" );
208  }
209 
210  return 0;
211 }
212 
213 
214 /**************************************************************/
215 
216 /************/
217 /*** draw ***/
218 void
220 {
221  if ( _redraw ) {
222 
223  draw_niche( _graph );
224  draw_occur( _graph, _presences, GColor::Green );
225  draw_occur( _graph, _absences, GColor::Red );
226 
227  _redraw = 0;
228  }
229 
230  _cnv->put( _graph );
231 }
232 
233 
234 /******************/
235 /*** draw niche ***/
236 void
237 draw_niche( GGraph *graph )
238 {
239  Scalar xmin = graph->minX();
240  Scalar ymin = graph->minY();
241  Scalar xmax = graph->maxX();
242  Scalar ymax = graph->maxY();
243  Scalar xstep = graph->stepX();
244  Scalar ystep = graph->stepY();
245 
246  int i = 0;
247  Scalar amb[2];
248  Scalar *x = amb;
249  Scalar *y = amb + 1;
250 
251  for ( *y = ymin; *y < ymax; *y += ystep ) {
252 
253  for ( *x = xmin; *x < xmax; *x += xstep ) {
254 
255  GColor color = GColor::Blue;
256  color.scale( _scale*_om.getValue( amb ) );
257  graph->pixel( float(*x), float(*y), color );
258  }
259 
260  if ( ! ( ++i % 10 ) ) {
261 
262  _cnv->put( graph );
263  }
264  }
265 }
266 
267 
268 /******************/
269 /*** draw occur ***/
270 void
271 draw_occur( GGraph *graph, const OccurrencesPtr& occurs, GColor color )
272 {
273  if ( occurs && occurs->numOccurrences() ) {
274 
275  // Draw each set of occurrences.
276  EnvironmentPtr env = _om.getEnvironment();
277  Sample amb;
278  OccurrencesImpl::const_iterator oc = occurs->begin();
279 
280  for ( ; oc != occurs->end(); ++oc ) {
281 
282  amb = env->get( (*oc)->x(), (*oc)->y() );
283 
284  if ( amb.size() >= 2 ) {
285 
286  graph->markAxe( amb[0], amb[1], 1, color );
287  }
288  }
289  }
290 }
291 
static ConfigurationPtr readXml(char const *filename)
int _last_draw
Definition: om_niche.cpp:46
double Scalar
Type of map values.
Definition: om_defs.hh:39
void draw_niche(GGraph *graph)
Definition: om_niche.cpp:237
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
Log::Level getLogLevel(std::string level)
GImage * _cnv
Definition: om_niche.cpp:58
int _nmap
Definition: om_niche.cpp:51
Level
Definition: Log.hh:54
void setLevel(Level level)
Definition: Log.hh:107
EnvironmentPtr getEnvironment()
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
OccurrencesPtr _presences
Definition: om_niche.cpp:54
OccurrencesPtr _absences
Definition: om_niche.cpp:55
void setupExternalResources()
Definition: os_specific.cpp:95
AlgorithmPtr getAlgorithm()
int main(int argc, char **argv)
Definition: om_niche.cpp:72
Scalar getValue(const ConstEnvironmentPtr &env, Coord x, Coord y)
float _zoom
Definition: om_niche.cpp:44
static int searchDefaultDirs()
Definition: Map.hh:49
int _redraw
Definition: om_niche.cpp:45
int _scale
Definition: om_niche.cpp:47
std::size_t size() const
Definition: Sample.hh:70
std::string getVersion()
GColor _bg(0, 140, 150)
void draw_occur(GGraph *graph, const OccurrencesPtr &oc, GColor color)
Definition: om_niche.cpp:271
const SamplerPtr & getSampler() const
OpenModeller _om
Definition: om_niche.cpp:57
GGraph * _graph
Definition: om_niche.cpp:60
GImage * _pm
Definition: om_niche.cpp:59
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
Map ** _maps
Definition: om_niche.cpp:52
void draw()
Definition: om_niche.cpp:219
int min(int v1, int v2)
Definition: rules_base.cpp:56
void setModelConfiguration(const ConstConfigurationPtr &)
Definition: Sample.hh:25