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