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