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