openModeller
Version 1.4.0
|
00001 /* ************************************** 00002 * GARP Modeling Package 00003 * 00004 * ************************************** 00005 * 00006 * Copyright (c), The Center for Research, University of Kansas, 2385 Irving Hill Road, Lawrence, KS 66044-4755, USA. 00007 * Copyright (C), David R.B. Stockwell of Symbiotik Pty. Ltd. 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the license that is distributed with the software. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * license.txt file included with this software for more details. 00016 */ 00017 00018 // Utilities.h: interface for the GarpUtil class. 00019 // 00021 00022 #ifndef __UTILITIES_H__ 00023 #define __UTILITIES_H__ 00024 00025 #include <openmodeller/Exceptions.hh> 00026 00027 #include <math.h> 00028 #include <time.h> 00029 #include <stdio.h> 00030 #include <stdlib.h> 00031 #include <string.h> 00032 #include <string> 00033 using std::string; 00034 00035 #ifndef __GARPWIN 00036 typedef unsigned char BYTE; 00037 #else 00038 #include <windows.h> 00039 #endif 00040 00041 // Comparison of two performance values 00042 #define Maxflag 1 00043 #define BETTER(X,Y) (Maxflag ? (X) > (Y) : (X) < (Y)) 00044 00045 const int CHAR_ARRAY_SIZE = 256; 00046 const int FILE_CHUNK = 1024; 00047 00048 const double EPS = 0.000001; 00049 const int MASK = 2147483647; 00050 const int PRIME = 65539; 00051 const double SCALE = 0.4656612875e-9; 00052 00053 const int MAX_ENV_LAYERS = 256; 00054 const int MAX_PRESENCE_POINTS = 100000; 00055 const int MAX_RULES = 256; 00056 00057 const int MIN_SCALED_VALUE = 1; 00058 const int MAX_SCALED_VALUE = 254; 00059 const int SCALED_VALUE_RANGE = MAX_SCALED_VALUE - MIN_SCALED_VALUE; 00060 00061 const int MAX_MUTATION_TEMPERATURE = (int) SCALED_VALUE_RANGE; 00062 00063 const int MIN_SIG_NO = 4; 00064 00065 const BYTE MASK_VALUE = 255; 00066 const BYTE MISSING_VALUE = 254; 00067 const BYTE PRESENCE = 1; 00068 const BYTE ABSENCE = 0; 00069 00070 // used in random number generator below 00071 extern unsigned long Seed; // seed for random number generator 00072 extern unsigned long OrigSeed; // original value for random seed 00073 00074 // macros for bitmap header generation 00075 typedef unsigned long DWORD; 00076 typedef int BOOL; 00077 typedef unsigned char BYTE; 00078 typedef unsigned short WORD; 00079 00080 #define MAX_BYTE 255 00081 00082 // these defines are already present in windef.h 00083 #ifndef WIN32 00084 #define MAKEWORD(a, b) ((WORD)(((BYTE)(a)) | ((WORD)((BYTE)(b))) << 8)) 00085 #define MAKELONG(a, b) ((LONG)(((WORD)(a)) | ((DWORD)((WORD)(b))) << 16)) 00086 #define LOWORD(l) ((WORD)(l)) 00087 #define HIWORD(l) ((WORD)(((DWORD)(l) >> 16) & 0xFFFF)) 00088 #define LOBYTE(w) ((BYTE)(w)) 00089 #define HIBYTE(w) ((BYTE)(((WORD)(w) >> 8) & 0xFF)) 00090 #endif 00091 00092 /* 00093 * Generic definitions like this should be avoided as, at any time, may 00094 * conflict with some system or other included header. 00095 */ 00096 #if defined(WIN32) || defined(__APPLE__) 00097 #ifndef MAX 00098 #define MAX(a, b) ( (a > b)? a : b ) 00099 #endif 00100 #ifndef MIN 00101 #define MIN(a, b) ( (a < b)? a : b ) 00102 #endif 00103 #endif 00104 00105 int getFileSize(char * strFilename); 00106 00107 // =========================================================================== 00108 // Declaration and Implementation of class GarpException 00109 // =========================================================================== 00110 class GarpException : public AlgorithmException 00111 { 00112 public: 00113 00114 GarpException(int code, string message) : 00115 AlgorithmException( message ), 00116 intCode( code ), 00117 strMessage( message) 00118 { } 00119 00120 ~GarpException() throw() {} 00121 00122 void getError(int& code, string& message) 00123 { 00124 code = intCode; 00125 message = strMessage; 00126 } 00127 00128 private: 00129 00130 int intCode; 00131 string strMessage; 00132 }; 00133 00134 // =========================================================================== 00135 // Declaration of class GarpUtil 00136 // =========================================================================== 00137 class GarpUtil 00138 { 00139 00140 public: 00141 GarpUtil() {}; 00142 00143 // ------------------------------------------------ 00144 static void randomize(unsigned long iOrigSeedProvided = 0) 00145 { 00146 time_t clock; 00147 00148 if (iOrigSeedProvided == 0) 00149 { 00150 time(&clock); 00151 OrigSeed = (unsigned long)clock; 00152 } 00153 else 00154 { 00155 OrigSeed = iOrigSeedProvided; 00156 } 00157 00158 Seed = OrigSeed; 00159 } 00160 00161 // ================================================== 00162 // Method: bool between(x, y, z) 00163 // -------------------------------------------------- 00164 // Purpose: Check if a number is in between two others 00165 // Output: Return 1 if x is between y and z 00166 // Return 0 otherwise 00167 // -------------------------------------------------- 00168 static bool between(double x, double y, double z) 00169 { 00170 if (y < z && x > y && x < z) 00171 return true; 00172 00173 else if (y > z && x < y && x > z) 00174 return true; 00175 00176 else 00177 return false; 00178 } 00179 00180 // ================================================== 00181 // Method: bool notBetween(x, y, z) 00182 // -------------------------------------------------- 00183 // Purpose: Check if a number is in not between two others 00184 // Output: Return 1 if x is between y and z 00185 // Return 0 otherwise 00186 // -------------------------------------------------- 00187 inline static int notBetween(double x, double y, double z) 00188 { 00189 if (y <= z && x >= y && x <= z) 00190 return 0; 00191 00192 else if (y >= z && x <= y && x >= z) 00193 return 0; 00194 00195 else 00196 return 1; 00197 } 00198 00199 // -------------------------------------------------- 00200 inline static bool equalEps(double x, double y, double eps) 00201 { 00202 return (fabs(x - y) < eps); 00203 } 00204 00205 // -------------------------------------------------- 00206 inline static double random() 00207 { 00208 double r; 00209 00210 Seed = (Seed * PRIME) & MASK; 00211 r = Seed * SCALE; 00212 return r; 00213 } 00214 00215 // -------------------------------------------------- 00216 inline static int randint(int low, int high) 00217 { 00218 int p; 00219 double r; 00220 00221 r = random(); 00222 p = (int) (low + (high - low) * r); 00223 00224 return p; 00225 } 00226 00227 // ------------------------------------------------ 00228 static int isInArray(long array[], int n, int value) 00229 { 00230 int i; 00231 00232 // position array is sorted! 00233 for (i = 0; i < n; i++) 00234 if (array[i] == value) 00235 // value found 00236 return 1; 00237 00238 else if (array[i] > value) 00239 // cannot be found anymore (array is sorted) 00240 return 0; 00241 00242 return 0; 00243 } 00244 00245 // ------------------------------------------------ 00246 static int membership(BYTE rval1, BYTE rval2, BYTE value) 00247 { 00248 if (rval1 == 0 && rval2 == 255) 00249 return 255; 00250 else if (value < rval1 || value > rval2 ) 00251 return 0; 00252 else 00253 return 1; 00254 } 00255 00256 }; 00257 00258 // =========================================================================== 00259 // Declaration and Implementation of class BioclimHistogram 00260 // =========================================================================== 00261 class BioclimHistogram 00262 { 00263 public: 00264 BYTE matrix[2][MAX_ENV_LAYERS][256]; 00265 00266 BioclimHistogram() 00267 { reset(); } 00268 00269 void reset() 00270 { 00271 // reset values 00272 for (int k = 0; k < 2; k++) 00273 for (int i = 0; i < MAX_ENV_LAYERS; i++) 00274 for (int j = 0; j < 256; j++) 00275 matrix[k][i][j] = (BYTE) 0; 00276 } 00277 }; 00278 00279 // =========================================================================== 00280 // =========================================================================== 00281 #endif