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