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