openModeller  Version 1.5.0
Utilities.h
Go to the documentation of this file.
1 /* **************************************
2  * GARP Modeling Package
3  *
4  * **************************************
5  *
6  * Copyright (c), The Center for Research, University of Kansas, 2385 Irving Hill Road, Lawrence, KS 66044-4755, USA.
7  * Copyright (C), David R.B. Stockwell of Symbiotik Pty. Ltd.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the license that is distributed with the software.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * license.txt file included with this software for more details.
16  */
17 
18 // Utilities.h: interface for the GarpUtil class.
19 //
21 
22 #ifndef __UTILITIES_H__
23 #define __UTILITIES_H__
24 
26 
27 #include <math.h>
28 #include <time.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <string>
33 using std::string;
34 
35 #ifndef __GARPWIN
36 typedef unsigned char BYTE;
37 #else
38 #include <windows.h>
39 #endif
40 
41 // Comparison of two performance values
42 #define Maxflag 1
43 #define BETTER(X,Y) (Maxflag ? (X) > (Y) : (X) < (Y))
44 
45 const int CHAR_ARRAY_SIZE = 256;
46 const int FILE_CHUNK = 1024;
47 
48 const double EPS = 0.000001;
49 const int MASK = 2147483647;
50 const int PRIME = 65539;
51 const double SCALE = 0.4656612875e-9;
52 
53 const int MAX_ENV_LAYERS = 256;
54 const int MAX_PRESENCE_POINTS = 100000;
55 const int MAX_RULES = 256;
56 
57 const int MIN_SCALED_VALUE = 1;
58 const int MAX_SCALED_VALUE = 254;
60 
62 
63 const int MIN_SIG_NO = 4;
64 
65 const BYTE MASK_VALUE = 255;
66 const BYTE MISSING_VALUE = 254;
67 const BYTE PRESENCE = 1;
68 const BYTE ABSENCE = 0;
69 
70 // used in random number generator below
71 extern unsigned long Seed; // seed for random number generator
72 extern unsigned long OrigSeed; // original value for random seed
73 
74 // macros for bitmap header generation
75 typedef unsigned long DWORD;
76 typedef int BOOL;
77 typedef unsigned char BYTE;
78 typedef unsigned short WORD;
79 
80 #define MAX_BYTE 255
81 
82 // these defines are already present in windef.h
83 #ifndef WIN32
84 #define MAKEWORD(a, b) ((WORD)(((BYTE)(a)) | ((WORD)((BYTE)(b))) << 8))
85 #define MAKELONG(a, b) ((LONG)(((WORD)(a)) | ((DWORD)((WORD)(b))) << 16))
86 #define LOWORD(l) ((WORD)(l))
87 #define HIWORD(l) ((WORD)(((DWORD)(l) >> 16) & 0xFFFF))
88 #define LOBYTE(w) ((BYTE)(w))
89 #define HIBYTE(w) ((BYTE)(((WORD)(w) >> 8) & 0xFF))
90 #endif
91 
92 /*
93  * Generic definitions like this should be avoided as, at any time, may
94  * conflict with some system or other included header.
95  */
96 #if defined(WIN32) || defined(__APPLE__)
97 #ifndef MAX
98 #define MAX(a, b) ( (a > b)? a : b )
99 #endif
100 #ifndef MIN
101 #define MIN(a, b) ( (a < b)? a : b )
102 #endif
103 #endif
104 
105 int getFileSize(char * strFilename);
106 
107 // ===========================================================================
108 // Declaration and Implementation of class GarpException
109 // ===========================================================================
111 {
112 public:
113 
114  GarpException(int code, string message) :
115  AlgorithmException( message ),
116  intCode( code ),
117  strMessage( message)
118  { }
119 
120  ~GarpException() throw() {}
121 
122  void getError(int& code, string& message)
123  {
124  code = intCode;
125  message = strMessage;
126  }
127 
128 private:
129 
130  int intCode;
131  string strMessage;
132 };
133 
134 // ===========================================================================
135 // Declaration of class GarpUtil
136 // ===========================================================================
137 class GarpUtil
138 {
139 
140 public:
141  GarpUtil() {};
142 
143  // ------------------------------------------------
144  static void randomize(unsigned long iOrigSeedProvided = 0)
145  {
146  time_t clock;
147 
148  if (iOrigSeedProvided == 0)
149  {
150  time(&clock);
151  OrigSeed = (unsigned long)clock;
152  }
153  else
154  {
155  OrigSeed = iOrigSeedProvided;
156  }
157 
158  Seed = OrigSeed;
159  }
160 
161  // ==================================================
162  // Method: bool between(x, y, z)
163  // --------------------------------------------------
164  // Purpose: Check if a number is in between two others
165  // Output: Return 1 if x is between y and z
166  // Return 0 otherwise
167  // --------------------------------------------------
168  static bool between(double x, double y, double z)
169  {
170  if (y < z && x > y && x < z)
171  return true;
172 
173  else if (y > z && x < y && x > z)
174  return true;
175 
176  else
177  return false;
178  }
179 
180  // ==================================================
181  // Method: bool notBetween(x, y, z)
182  // --------------------------------------------------
183  // Purpose: Check if a number is in not between two others
184  // Output: Return 1 if x is between y and z
185  // Return 0 otherwise
186  // --------------------------------------------------
187  inline static int notBetween(double x, double y, double z)
188  {
189  if (y <= z && x >= y && x <= z)
190  return 0;
191 
192  else if (y >= z && x <= y && x >= z)
193  return 0;
194 
195  else
196  return 1;
197  }
198 
199  // --------------------------------------------------
200  inline static bool equalEps(double x, double y, double eps)
201  {
202  return (fabs(x - y) < eps);
203  }
204 
205  // --------------------------------------------------
206  inline static double random()
207  {
208  double r;
209 
210  Seed = (Seed * PRIME) & MASK;
211  r = Seed * SCALE;
212  return r;
213  }
214 
215  // --------------------------------------------------
216  inline static int randint(int low, int high)
217  {
218  int p;
219  double r;
220 
221  r = random();
222  p = (int) (low + (high - low) * r);
223 
224  return p;
225  }
226 
227  // ------------------------------------------------
228  static int isInArray(long array[], int n, int value)
229  {
230  int i;
231 
232  // position array is sorted!
233  for (i = 0; i < n; i++)
234  if (array[i] == value)
235  // value found
236  return 1;
237 
238  else if (array[i] > value)
239  // cannot be found anymore (array is sorted)
240  return 0;
241 
242  return 0;
243  }
244 
245  // ------------------------------------------------
246  static int membership(BYTE rval1, BYTE rval2, BYTE value)
247  {
248  if (rval1 == 0 && rval2 == 255)
249  return 255;
250  else if (value < rval1 || value > rval2 )
251  return 0;
252  else
253  return 1;
254  }
255 
256 };
257 
258 // ===========================================================================
259 // Declaration and Implementation of class BioclimHistogram
260 // ===========================================================================
262 {
263 public:
265 
267  { reset(); }
268 
269  void reset()
270  {
271  // reset values
272  for (int k = 0; k < 2; k++)
273  for (int i = 0; i < MAX_ENV_LAYERS; i++)
274  for (int j = 0; j < 256; j++)
275  matrix[k][i][j] = (BYTE) 0;
276  }
277 };
278 
279 // ===========================================================================
280 // ===========================================================================
281 #endif
const BYTE MASK_VALUE
Definition: Utilities.h:65
BYTE matrix[2][MAX_ENV_LAYERS][256]
Definition: Utilities.h:264
const int MAX_SCALED_VALUE
Definition: Utilities.h:58
#define eps
const int CHAR_ARRAY_SIZE
Definition: Utilities.h:45
static int randint(int low, int high)
Definition: Utilities.h:216
static int notBetween(double x, double y, double z)
Definition: Utilities.h:187
int getFileSize(char *strFilename)
Definition: Utilities.cpp:39
static bool equalEps(double x, double y, double eps)
Definition: Utilities.h:200
static int membership(BYTE rval1, BYTE rval2, BYTE value)
Definition: Utilities.h:246
static int isInArray(long array[], int n, int value)
Definition: Utilities.h:228
const int MAX_PRESENCE_POINTS
Definition: Utilities.h:54
unsigned short WORD
Definition: Utilities.h:78
const BYTE MISSING_VALUE
Definition: Utilities.h:66
unsigned long DWORD
Definition: Utilities.h:75
string strMessage
Definition: Utilities.h:131
unsigned char BYTE
Definition: Utilities.h:36
static void randomize(unsigned long iOrigSeedProvided=0)
Definition: Utilities.h:144
void getError(int &code, string &message)
Definition: Utilities.h:122
unsigned long Seed
Definition: Utilities.cpp:20
const int MASK
Definition: Utilities.h:49
unsigned long OrigSeed
Definition: Utilities.cpp:21
static double random()
Definition: Utilities.h:206
const int MAX_RULES
Definition: Utilities.h:55
const int SCALED_VALUE_RANGE
Definition: Utilities.h:59
const int MAX_ENV_LAYERS
Definition: Utilities.h:53
const BYTE PRESENCE
Definition: Utilities.h:67
GarpUtil()
Definition: Utilities.h:141
const double EPS
Definition: Utilities.h:48
static bool between(double x, double y, double z)
Definition: Utilities.h:168
const int MIN_SCALED_VALUE
Definition: Utilities.h:57
const BYTE ABSENCE
Definition: Utilities.h:68
const int PRIME
Definition: Utilities.h:50
int BOOL
Definition: Utilities.h:76
const int MAX_MUTATION_TEMPERATURE
Definition: Utilities.h:61
const int MIN_SIG_NO
Definition: Utilities.h:63
GarpException(int code, string message)
Definition: Utilities.h:114
const int FILE_CHUNK
Definition: Utilities.h:46
const double SCALE
Definition: Utilities.h:51