/** 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 #include #include #include namespace CHCDemoEngine { struct Triangle3; class BvhNode; class BvhLeaf; class OcclusionQuery; class SceneEntity; #if defined(_MSC_VER) // use perftimer only on msvc #define USE_PERFTIMER // define __SSE__ macro as it is not defined under MSVC #define __SSE__ #endif // 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.14159265358979323846F #define M_E 2.71828182845904523536F #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 typedef float Real; typedef unsigned char byte; #ifndef getch #define getch() getchar() #endif #define TRASH 1.0e-5 // delete a pointer and set to NULL #ifndef DEL_PTR #define DEL_PTR(ptr) do {if (ptr) { \ delete (ptr); \ (ptr) = 0;}} \ while (0) #endif // delete a pointer and set to NULL #ifndef DEL_ARRAY_PTR #define DEL_ARRAY_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 #ifndef CLEAR_CONTAINER #define CLEAR_CONTAINER(co) do { for (size_t _i = 0; _i < (int)(co).size(); ++ _i) { \ DEL_PTR((co)[_i]);} \ (co).clear(); } \ while (0) #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(unsigned int seed); void GetKey(char *s=NULL); inline Real Deg2Rad(const Real a) { return a * (M_PI / 180.0f); } inline Real Rad2Deg(const Real a) { return a * (180.0f/M_PI); } void InitTiming(); long GetTime(); long GetRealTime(); Real TimeDiff(long t1, long t2); char *TimeString(); /** Limits. This class encapsulates all the concessions to Realing-point error made by ray tracers. */ class Limits { public: // This is the number used to reject too-close intersections. // The default value is 1.0. static Real Threshold; // This is a "small" number. Less than this number is assumed to // be 0, when such things matter. // The default value is 0.1. static Real Small; // This is an impractically "large" number, used for intersection // parameters out to infinity (e.g. the span resulting from an // FindAllIntersections operation on a plane). // The default value is 100000. static Real Infinity; }; // --------------------------------------------------------- // EpsilonEqual(x,y) // Returns if two values are equal or not (by epsilon) // --------------------------------------------------------- inline int EpsilonEqual(const Real &x, const Real &y) { return fabs(x-y) < Limits::Small; } // --------------------------------------------------------- // EpsilonEqual(x) // Returns if a value is zero (+/- epsilon) // --------------------------------------------------------- inline int EpsilonEqual(const Real &x) { return fabs(x) < Limits::Small; } // --------------------------------------------------------- // EpsilonEqual(x,y,epsilon) // Returns if two values are equal or not by a given epsilon // --------------------------------------------------------- inline int EpsilonEqual(const Real &x, const Real &y, const Real &epsilon) { return fabs(x-y) < epsilon; } // --------------------------------------------------------- // InRange // Returns nonzero if min <= candidate <= max. // --------------------------------------------------------- template 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); std::string ReplaceSuffix(const std::string &filename, const std::string &a, const std::string &b); int SplitFilenames(const std::string &str, std::vector &filenames); bool FileExists(char *filename); bool CreateDir(char *dir); char * GetPath(const char *s); char * StripPath(const char *s); ////// //-- model directory static std::string model_path("data/city/model/"); /////////// //-- typedefs typedef std::vector BvhNodeContainer; typedef std::vector SceneEntityContainer; typedef std::vector BvhLeafContainer; typedef std::vector TriangleContainer; typedef std::vector QueryContainer; typedef std::queue QueryQueue; typedef std::queue BvhNodeQueue; static std::ofstream Debug("debug.log"); #define INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES 1000 class Shape; typedef std::vector ShapeContainer; struct LODLevel; typedef std::vector LODLevelContainer; } #endif