// ================================================================ // $Id: common.h,v 1.1 2004/02/16 14:46:00 bittner Exp $ // **************************************************************** // /** \file common.h Common defines for the ERS system. This file contains various macros, templates and constants for the ERS system. @author Jiri Bittner */ #ifndef __COMMON_H #define __COMMON_H #include #include #include #include #include namespace GtpVisibilityPreprocessor { // This constant should be used for the length of the array char for filenames // etc., for example: char filename[MaxStringLength] const int MaxStringLength = 256; #if defined(_MSC_VER) #pragma warning(disable:4018) #pragma warning(disable:4800) //#pragma warning(disable:4244) #if 0 // Note matt: comment this out because conflicts with definition in qt library!! typedef unsigned int uint; #endif typedef unsigned short ushort; typedef unsigned char uchar; typedef unsigned long ulong; #endif #if defined(__GNUC__) || defined(_MSC_VER) #define DIRCAT '.' #endif #if !defined(__WATCOMC__) && !defined(__CYGWIN32__) && !defined(_MSC_VER) #include #else // __WATCOMC__ #define M_PI 3.14159265358979323846 #define MAXFLOAT 3.40282347e+37F #endif // __WATCOMC__ // some compilers do not define the bool yet, but it was declared by ANSI #if !defined(__WATCOMC__) && !defined(_MSC_VER) #if defined (__GNUC__) || (_BOOL) //#error "HAS BOOL defined" #define HAS_BOOL #endif #else // __WATCOMC__ #if (__WATCOMC__ > 1060) //#error "Watcom HAS BOOL defined" #define HAS_BOOL #endif #endif // __WATCOMC__ #if defined(__WATCOMC__) || defined(_MSC_VER) #define strcasecmp stricmp #define strncasecmp strnicmp #endif // __WATCOMC__ // matt #define USE_GZLIB 1 #if USE_GZLIB #define OUT_BIN_MODE ios::out #define IN_BIN_MODE ios::in #else #ifdef sgi #define OUT_BIN_MODE ios::out #define IN_BIN_MODE ios::in #else // sgi #if defined(__WATCOMC__) || defined(_MSC_VER) #define OUT_BIN_MODE ios::out | ios::binary #define IN_BIN_MODE ios::in | ios::binary #else #define OUT_BIN_MODE ios::out | ios::bin #define IN_BIN_MODE ios::in | ios::bin #endif // __WATCOMC_ #endif // sgi #endif // #ifndef HAS_BOOL // //enum bool { // // false = 0, // // true // //}; // #define bool int // #define false 0 // #define true 1 // #endif // HAS_BOOL typedef unsigned long dword; #ifndef NULL #define NULL (void *)0 #endif // NULL //ostream& operator<<(ostream &s,const BaseC &c); typedef float Real; typedef unsigned char byte; //#ifndef FALSE //#define FALSE 0 //#define TRUE !0 //#endif #ifndef __GNUG__ // typedef int bool; #endif #ifndef getch #define getch() getchar() #endif #define TRASH 1.0e-5 #ifndef PI #define PI 3.14159265358979323846f #endif #define MIN_FLOAT -1e30f #define MAX_FLOAT 1e30f // tolerance value for polygon area #define AREA_LIMIT 0.0001f #ifndef DEL_PTR #define DEL_PTR(ptr) do {if (ptr) { \ delete (ptr); \ (ptr) = 0;}} \ while (0) #endif // Clears a container (i.e., a vector of pointers) and deletes the pointers #if 0 #ifndef CLEAR_CONTAINER #define CLEAR_CONTAINER(co) do { while (!(co).empty()) { \ DEL_PTR((co).back()); \ (co).pop_back();}} \ while (0) #endif #else #ifndef CLEAR_CONTAINER #define CLEAR_CONTAINER(co) do { for (int _i = 0; _i < (int)(co).size(); ++ _i) { \ DEL_PTR((co)[_i]);} \ (co).clear(); } \ while (0) #endif #endif inline int signum(const Real a, const Real thresh = TRASH) { if (a>thresh) return 1; else if (a<-thresh) return -1; return 0; } inline double Absd(const double a) { return (a >= 0.0) ? a : -a; } inline float Abs(const float a) { return (a >= 0.0f) ? a : -a; } // ======================================================= // Comparing things //struct Limits { // const Real thresh=TRASH; // const Real small=0.1; //}; template bool ClipValue(T &v, const T m, const T M) { if (vM) { v = M; return true; } return false; } inline int eq(Real a, Real b, Real t=TRASH) { return Abs(a-b) b - a; } inline int le(Real a,Real b,Real t=TRASH) { return !geq(a,b,t); } inline int ge(Real a,Real b,Real t=TRASH) { return !leq(a,b,t); } // ======================================================== // ------------------------------------------------------------------- // Indents to a given stream by the number of spaces specified. // This routine is located in main.cpp, for lack of a better place. // ------------------------------------------------------------------- void indent(std::ostream &app, int ind); // --------------------------------------------------------- // RandomValue // Returns a random Realing-point value between the two // values. Range is inclusive; the function should // occasionally return exactly a or b. // --------------------------------------------------------- inline Real RandomValue(Real a, Real b) { Real range = (Real) Abs(a - b); return ((Real)rand() / RAND_MAX) * range + ((a < b) ? a : b); } /*inline int RandomValue(int a, int b) { int range = abs(a - b); return (rand() * range) / RAND_MAX + ((a < b) ? a : b); }*/ inline Real sqr(Real a) { return a*a; } template void Swap(T &a,T &b) { T c; c = b; b = a; a = c; } template int eq(T &a, T &b, T &c, T &d) { return a == b && c==d && b==c; } template T Min(T a,T b) { return a T Max(T a,T b) { return a>b ? a : b; } Real Random(Real max); int Random(int max); void Randomize(); void Randomize(const unsigned int seed); void GetKey(char *s=NULL); inline Real Deg2Rad(const Real a) { return a*(PI/180.0f); } inline Real Rad2Deg(const Real a) { return a*(180.0f/PI); } void InitTiming(); long GetTime(); long GetRealTime(); Real TimeDiff(long t1, long t2); char *TimeString(); // manipulator inline std::ostream &DEBUGINFO( std::ostream &s ) { return s<<"FILE "<<__FILE__<<",LINE "<<__LINE__< inline int InRange(T min, T max, T candidate) { return (candidate >= min) && (candidate <= max); } // -------------------------------------------------------------- // string function with new operator inline char* StrDup(char *src) { char *p; for (p = src; *p++; ); char *dest = new char[p-src]; for ( p = dest;(*p++ = *src++) != 0; ); return dest; } inline char * StrToLower(char *src) { char *p; for (p = src; *p; p++) *p = tolower(*p); return src; } // return l = log2(a) if a is a power of two else -ceillog2(a) inline int GetLog2(int a) { int i, x; i = 0; x = 1; while (x < a) { i++; x <<= 1; } return (x==a) ? i: -i; } // return ceil(log2(a)) even if a is not a power of two // Example: // GetCeilLog2(15) = 4 // GetCeilLog2(16) = 4 // GetCeilLog2(17) = 5 inline int GetCeilLog2(int a) { int i, x; if (a < 0) return -1; i = 0; x = 1; while (x < a) { i++; x <<= 1; } return i; } char * GetAbsPath(char *path); char * strdup(char *a); bool FileExists(char *filename); //#define GTP_DEBUG 1 #define DEBUG_LEVEL 5 //#define DEBUG_LEVEL 1000 //#define DEBUG_LEVEL 50000 // debug stream extern std::ofstream Debug; bool CreateDir(char *dir); char * GetPath(const char *s); char * StripPath(const char *s); } #if USE_GZLIB // type of out put and input streams #define OUT_STREAM ogzstream #define IN_STREAM igzstream #else #define OUT_STREAM ofstream #define IN_STREAM ifstream #endif /** view cell id belonging to empty view space. */ #define OUT_OF_BOUNDS_ID -1 #endif