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