[2751] | 1 | /** This file contains various macros, templates and constants for the ERS system. |
---|
| 2 | @author Jiri Bittner |
---|
[2746] | 3 | */ |
---|
| 4 | |
---|
| 5 | #ifndef __COMMON_H |
---|
| 6 | #define __COMMON_H |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | #include <math.h> |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include <iostream> |
---|
| 12 | #include <fstream> |
---|
| 13 | #include <string> |
---|
| 14 | #include <limits.h> |
---|
| 15 | #include <vector> |
---|
| 16 | |
---|
| 17 | |
---|
[2751] | 18 | namespace CHCDemo |
---|
| 19 | { |
---|
[2746] | 20 | |
---|
[2751] | 21 | struct Triangle3; |
---|
[2752] | 22 | class BvhNode; |
---|
| 23 | class BvhLeaf; |
---|
| 24 | |
---|
| 25 | |
---|
[2746] | 26 | #if defined(_MSC_VER) |
---|
| 27 | // use perftimer only on msvc |
---|
| 28 | #define USE_PERFTIMER |
---|
| 29 | |
---|
| 30 | // define __SSE__ macro as it is not defined under MSVC |
---|
| 31 | #define __SSE__ |
---|
| 32 | #endif |
---|
| 33 | |
---|
| 34 | // This constant should be used for the length of the array char for filenames |
---|
| 35 | // etc., for example: char filename[MaxStringLength] |
---|
| 36 | const int MaxStringLength = 256; |
---|
| 37 | |
---|
| 38 | #if defined(_MSC_VER) |
---|
| 39 | #pragma warning(disable:4018) |
---|
| 40 | #pragma warning(disable:4800) |
---|
| 41 | //#pragma warning(disable:4244) |
---|
| 42 | |
---|
| 43 | #if 0 // Note matt: comment this out because conflicts with definition in qt library!! |
---|
| 44 | typedef unsigned int uint; |
---|
| 45 | #endif |
---|
| 46 | typedef unsigned short ushort; |
---|
| 47 | typedef unsigned char uchar; |
---|
| 48 | typedef unsigned long ulong; |
---|
| 49 | #endif |
---|
| 50 | |
---|
| 51 | #if defined(__GNUC__) || defined(_MSC_VER) |
---|
| 52 | //#define DIRCAT '.' |
---|
| 53 | #endif |
---|
| 54 | |
---|
| 55 | #if !defined(__WATCOMC__) && !defined(__CYGWIN32__) && !defined(_MSC_VER) |
---|
| 56 | #include <values.h> |
---|
| 57 | #else // __WATCOMC__ |
---|
[2751] | 58 | #define M_PI 3.14159265358979323846F |
---|
[2746] | 59 | #define MAXFLOAT 3.40282347e+37F |
---|
| 60 | #endif // __WATCOMC__ |
---|
| 61 | |
---|
| 62 | // some compilers do not define the bool yet, but it was declared by ANSI |
---|
| 63 | #if !defined(__WATCOMC__) && !defined(_MSC_VER) |
---|
| 64 | #if defined (__GNUC__) || (_BOOL) |
---|
| 65 | //#error "HAS BOOL defined" |
---|
| 66 | #define HAS_BOOL |
---|
| 67 | #endif |
---|
| 68 | #else // __WATCOMC__ |
---|
| 69 | #if (__WATCOMC__ > 1060) |
---|
| 70 | //#error "Watcom HAS BOOL defined" |
---|
| 71 | #define HAS_BOOL |
---|
| 72 | #endif |
---|
| 73 | #endif // __WATCOMC__ |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | #if defined(__WATCOMC__) || defined(_MSC_VER) |
---|
| 77 | #define strcasecmp stricmp |
---|
| 78 | #define strncasecmp strnicmp |
---|
| 79 | #endif // __WATCOMC__ |
---|
| 80 | |
---|
| 81 | // matt |
---|
| 82 | |
---|
| 83 | #define USE_GZLIB 1 |
---|
| 84 | |
---|
| 85 | #if USE_GZLIB |
---|
| 86 | |
---|
| 87 | #define OUT_BIN_MODE ios::out |
---|
| 88 | #define IN_BIN_MODE ios::in |
---|
| 89 | |
---|
| 90 | #else |
---|
| 91 | |
---|
| 92 | #ifdef sgi |
---|
| 93 | #define OUT_BIN_MODE ios::out |
---|
| 94 | #define IN_BIN_MODE ios::in |
---|
| 95 | #else // sgi |
---|
| 96 | #if defined(__WATCOMC__) || defined(_MSC_VER) |
---|
| 97 | #define OUT_BIN_MODE ios::out | ios::binary |
---|
| 98 | #define IN_BIN_MODE ios::in | ios::binary |
---|
| 99 | #else |
---|
| 100 | #define OUT_BIN_MODE ios::out | ios::bin |
---|
| 101 | #define IN_BIN_MODE ios::in | ios::bin |
---|
| 102 | #endif // __WATCOMC_ |
---|
| 103 | #endif // sgi |
---|
| 104 | |
---|
| 105 | #endif |
---|
| 106 | |
---|
| 107 | // #ifndef HAS_BOOL |
---|
| 108 | // //enum bool { |
---|
| 109 | // // false = 0, |
---|
| 110 | // // true |
---|
| 111 | // //}; |
---|
| 112 | // #define bool int |
---|
| 113 | // #define false 0 |
---|
| 114 | // #define true 1 |
---|
| 115 | // #endif // HAS_BOOL |
---|
| 116 | |
---|
| 117 | typedef unsigned long dword; |
---|
| 118 | |
---|
| 119 | #ifndef NULL |
---|
| 120 | #define NULL (void *)0 |
---|
| 121 | #endif // NULL |
---|
| 122 | |
---|
| 123 | typedef float Real; |
---|
| 124 | typedef unsigned char byte; |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | #ifndef getch |
---|
| 128 | #define getch() getchar() |
---|
| 129 | #endif |
---|
| 130 | |
---|
| 131 | #define TRASH 1.0e-5 |
---|
| 132 | |
---|
[2751] | 133 | // delete a pointer and set to NULL |
---|
[2746] | 134 | #ifndef DEL_PTR |
---|
| 135 | #define DEL_PTR(ptr) do {if (ptr) { \ |
---|
| 136 | delete (ptr); \ |
---|
| 137 | (ptr) = 0;}} \ |
---|
| 138 | while (0) |
---|
| 139 | #endif |
---|
| 140 | |
---|
| 141 | |
---|
[2751] | 142 | // Clears a container (i.e., a vector of pointers) and deletes the pointers |
---|
[2746] | 143 | #ifndef CLEAR_CONTAINER |
---|
[2751] | 144 | #define CLEAR_CONTAINER(co) do { for (size_t _i = 0; _i < (int)(co).size(); ++ _i) { \ |
---|
[2746] | 145 | DEL_PTR((co)[_i]);} \ |
---|
| 146 | (co).clear(); } \ |
---|
| 147 | while (0) |
---|
| 148 | #endif |
---|
| 149 | |
---|
| 150 | |
---|
[2751] | 151 | inline int signum(const Real a, const Real thresh = TRASH) |
---|
[2746] | 152 | { |
---|
| 153 | if (a>thresh) |
---|
| 154 | return 1; |
---|
| 155 | else |
---|
| 156 | if (a<-thresh) |
---|
| 157 | return -1; |
---|
| 158 | return 0; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | inline double Absd(const double a) |
---|
| 162 | { |
---|
| 163 | return (a >= 0.0) ? a : -a; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | inline float Abs(const float a) |
---|
| 167 | { |
---|
| 168 | return (a >= 0.0f) ? a : -a; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | // ======================================================= |
---|
| 172 | // Comparing things |
---|
| 173 | //struct Limits { |
---|
| 174 | // const Real thresh=TRASH; |
---|
| 175 | // const Real small=0.1; |
---|
| 176 | //}; |
---|
| 177 | |
---|
| 178 | template <class T> |
---|
| 179 | bool |
---|
| 180 | ClipValue(T &v, const T m, const T M) |
---|
| 181 | { |
---|
| 182 | if (v<m) { |
---|
| 183 | v = m; |
---|
| 184 | return true; |
---|
| 185 | } |
---|
| 186 | if (v>M) { |
---|
| 187 | v = M; |
---|
| 188 | return true; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | return false; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | inline int eq(Real a, Real b, Real t=TRASH) |
---|
| 196 | { |
---|
| 197 | return Abs(a-b)<t; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | inline int leq(Real a,Real b,Real t=TRASH) |
---|
| 201 | { |
---|
| 202 | return a - b < t; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | inline int geq(Real a,Real b,Real t=TRASH) |
---|
| 206 | { |
---|
| 207 | return t > b - a; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | inline int le(Real a,Real b,Real t=TRASH) |
---|
| 211 | { |
---|
| 212 | return !geq(a,b,t); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | inline int ge(Real a,Real b,Real t=TRASH) |
---|
| 216 | { |
---|
| 217 | return !leq(a,b,t); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | // ======================================================== |
---|
| 221 | |
---|
| 222 | // ------------------------------------------------------------------- |
---|
| 223 | // Indents to a given stream by the number of spaces specified. |
---|
| 224 | // This routine is located in main.cpp, for lack of a better place. |
---|
| 225 | // ------------------------------------------------------------------- |
---|
| 226 | void indent(std::ostream &app, int ind); |
---|
| 227 | |
---|
| 228 | // --------------------------------------------------------- |
---|
| 229 | // RandomValue |
---|
| 230 | // Returns a random Realing-point value between the two |
---|
| 231 | // values. Range is inclusive; the function should |
---|
| 232 | // occasionally return exactly a or b. |
---|
| 233 | // --------------------------------------------------------- |
---|
| 234 | inline Real RandomValue(Real a, Real b) |
---|
| 235 | { |
---|
| 236 | Real range = (Real) Abs(a - b); |
---|
| 237 | return ((Real)rand() / RAND_MAX) * range + ((a < b) ? a : b); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | /*inline int |
---|
| 242 | RandomValue(int a, int b) |
---|
| 243 | { |
---|
| 244 | int range = abs(a - b); |
---|
| 245 | return (rand() * range) / RAND_MAX + ((a < b) ? a : b); |
---|
| 246 | }*/ |
---|
| 247 | |
---|
| 248 | inline Real sqr(Real a) |
---|
| 249 | { |
---|
| 250 | return a*a; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | template <class T> |
---|
| 255 | void Swap(T &a,T &b) |
---|
| 256 | { |
---|
| 257 | T c; |
---|
| 258 | c = b; |
---|
| 259 | b = a; |
---|
| 260 | a = c; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | template <class T> |
---|
| 264 | int eq(T &a, T &b, T &c, T &d) { |
---|
| 265 | return a == b && c==d && b==c; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | template <class T> |
---|
| 269 | T Min(T a,T b) |
---|
| 270 | { |
---|
| 271 | return a<b ? a : b; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | template <class T> |
---|
| 275 | T Max(T a,T b) |
---|
| 276 | { |
---|
| 277 | return a>b ? a : b; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | Real Random(Real max); |
---|
| 282 | int Random(int max); |
---|
| 283 | void Randomize(); |
---|
| 284 | void |
---|
| 285 | Randomize(const unsigned int seed); |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | void GetKey(char *s=NULL); |
---|
| 289 | |
---|
| 290 | inline Real Deg2Rad(const Real a) |
---|
| 291 | { |
---|
[2751] | 292 | return a * (M_PI / 180.0f); |
---|
[2746] | 293 | } |
---|
| 294 | |
---|
| 295 | inline Real Rad2Deg(const Real a) { |
---|
[2751] | 296 | return a * (180.0f/M_PI); |
---|
[2746] | 297 | } |
---|
| 298 | |
---|
| 299 | void InitTiming(); |
---|
| 300 | long GetTime(); |
---|
| 301 | long GetRealTime(); |
---|
| 302 | Real TimeDiff(long t1, long t2); |
---|
| 303 | char *TimeString(); |
---|
| 304 | |
---|
| 305 | |
---|
| 306 | |
---|
[2751] | 307 | /** Limits. |
---|
| 308 | This class encapsulates all the concessions to Realing-point |
---|
| 309 | error made by ray tracers. |
---|
| 310 | */ |
---|
| 311 | class Limits |
---|
| 312 | { |
---|
[2746] | 313 | public: |
---|
[2751] | 314 | // This is the number used to reject too-close intersections. |
---|
| 315 | // The default value is 1.0. |
---|
| 316 | static Real Threshold; |
---|
[2746] | 317 | |
---|
[2751] | 318 | // This is a "small" number. Less than this number is assumed to |
---|
| 319 | // be 0, when such things matter. |
---|
| 320 | // The default value is 0.1. |
---|
| 321 | static Real Small; |
---|
[2746] | 322 | |
---|
[2751] | 323 | // This is an impractically "large" number, used for intersection |
---|
| 324 | // parameters out to infinity (e.g. the span resulting from an |
---|
| 325 | // FindAllIntersections operation on a plane). |
---|
| 326 | // The default value is 100000. |
---|
| 327 | static Real Infinity; |
---|
[2746] | 328 | }; |
---|
| 329 | |
---|
| 330 | // --------------------------------------------------------- |
---|
| 331 | // EpsilonEqual(x,y) |
---|
| 332 | // Returns if two values are equal or not (by epsilon) |
---|
| 333 | // --------------------------------------------------------- |
---|
| 334 | inline int |
---|
| 335 | EpsilonEqual(const Real &x, const Real &y) |
---|
| 336 | { |
---|
| 337 | return fabs(x-y) < Limits::Small; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | // --------------------------------------------------------- |
---|
| 341 | // EpsilonEqual(x) |
---|
| 342 | // Returns if a value is zero (+/- epsilon) |
---|
| 343 | // --------------------------------------------------------- |
---|
| 344 | inline int |
---|
| 345 | EpsilonEqual(const Real &x) |
---|
| 346 | { |
---|
| 347 | return fabs(x) < Limits::Small; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | // --------------------------------------------------------- |
---|
| 351 | // EpsilonEqual(x,y,epsilon) |
---|
| 352 | // Returns if two values are equal or not by a given epsilon |
---|
| 353 | // --------------------------------------------------------- |
---|
| 354 | inline int |
---|
| 355 | EpsilonEqual(const Real &x, const Real &y, const Real &epsilon) |
---|
| 356 | { |
---|
| 357 | return fabs(x-y) < epsilon; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | // --------------------------------------------------------- |
---|
| 361 | // InRange |
---|
| 362 | // Returns nonzero if min <= candidate <= max. |
---|
| 363 | // --------------------------------------------------------- |
---|
| 364 | template<class T> |
---|
| 365 | inline int |
---|
| 366 | InRange(T min, T max, T candidate) |
---|
| 367 | { |
---|
| 368 | return (candidate >= min) && (candidate <= max); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | // -------------------------------------------------------------- |
---|
| 372 | // string function with new operator |
---|
| 373 | |
---|
| 374 | inline char* |
---|
| 375 | StrDup(char *src) { |
---|
| 376 | char *p; |
---|
| 377 | for (p = src; *p++; ); |
---|
| 378 | char *dest = new char[p-src]; |
---|
| 379 | for ( p = dest;(*p++ = *src++) != 0; ); |
---|
| 380 | return dest; |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | |
---|
| 384 | inline char * |
---|
| 385 | StrToLower(char *src) { |
---|
| 386 | char *p; |
---|
| 387 | for (p = src; *p; p++) |
---|
| 388 | *p = tolower(*p); |
---|
| 389 | return src; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | // return l = log2(a) if a is a power of two else -ceillog2(a) |
---|
| 393 | inline int |
---|
| 394 | GetLog2(int a) |
---|
| 395 | { |
---|
| 396 | int i, x; |
---|
| 397 | i = 0; |
---|
| 398 | x = 1; |
---|
| 399 | |
---|
| 400 | while (x < a) { |
---|
| 401 | i++; |
---|
| 402 | x <<= 1; |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | return (x==a) ? i: -i; |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | |
---|
| 409 | // return ceil(log2(a)) even if a is not a power of two |
---|
| 410 | // Example: |
---|
| 411 | // GetCeilLog2(15) = 4 |
---|
| 412 | // GetCeilLog2(16) = 4 |
---|
| 413 | // GetCeilLog2(17) = 5 |
---|
| 414 | inline int |
---|
| 415 | GetCeilLog2(int a) |
---|
| 416 | { |
---|
| 417 | int i, x; |
---|
| 418 | if (a < 0) |
---|
| 419 | return -1; |
---|
| 420 | |
---|
| 421 | i = 0; |
---|
| 422 | x = 1; |
---|
| 423 | |
---|
| 424 | while (x < a) { |
---|
| 425 | i++; |
---|
| 426 | x <<= 1; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | return i; |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | |
---|
| 433 | char * |
---|
| 434 | GetAbsPath(char *path); |
---|
| 435 | |
---|
| 436 | char * |
---|
| 437 | strdup(char *a); |
---|
| 438 | |
---|
| 439 | std::string |
---|
| 440 | ReplaceSuffix(const std::string &filename, const std::string &a, const std::string &b); |
---|
| 441 | |
---|
| 442 | |
---|
| 443 | int |
---|
| 444 | SplitFilenames(const std::string &str, std::vector<std::string> &filenames); |
---|
| 445 | |
---|
| 446 | bool |
---|
| 447 | FileExists(char *filename); |
---|
| 448 | |
---|
| 449 | bool |
---|
| 450 | CreateDir(char *dir); |
---|
| 451 | |
---|
| 452 | char * |
---|
| 453 | GetPath(const char *s); |
---|
| 454 | |
---|
| 455 | char * |
---|
| 456 | StripPath(const char *s); |
---|
| 457 | |
---|
| 458 | |
---|
[2752] | 459 | |
---|
| 460 | /////////// |
---|
| 461 | //-- typedefs |
---|
| 462 | |
---|
| 463 | typedef std::vector<BvhNode *> BvhNodeContainer; |
---|
| 464 | typedef std::vector<BvhLeaf *> BvhLeafContainer; |
---|
[2751] | 465 | typedef std::vector<Triangle3> TriangleContainer; |
---|
[2746] | 466 | |
---|
[2752] | 467 | |
---|
[2751] | 468 | } |
---|
| 469 | |
---|
[2746] | 470 | #endif |
---|
| 471 | |
---|
| 472 | |
---|
| 473 | |
---|
| 474 | |
---|
| 475 | |
---|
| 476 | |
---|
| 477 | |
---|
| 478 | |
---|