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 | |
---|
26 | #define USE_HAVRAN_RAYCASTER |
---|
27 | |
---|
28 | namespace GtpVisibilityPreprocessor { |
---|
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.14159265358979323846 |
---|
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 | //ostream& operator<<(ostream &s,const BaseC &c); |
---|
129 | |
---|
130 | typedef float Real; |
---|
131 | typedef unsigned char byte; |
---|
132 | |
---|
133 | //#ifndef FALSE |
---|
134 | //#define FALSE 0 |
---|
135 | //#define TRUE !0 |
---|
136 | //#endif |
---|
137 | |
---|
138 | #ifndef __GNUG__ |
---|
139 | // typedef int bool; |
---|
140 | #endif |
---|
141 | |
---|
142 | #ifndef getch |
---|
143 | #define getch() getchar() |
---|
144 | #endif |
---|
145 | |
---|
146 | |
---|
147 | #define TRASH 1.0e-5 |
---|
148 | |
---|
149 | #ifndef PI |
---|
150 | #define PI 3.14159265358979323846f |
---|
151 | #endif |
---|
152 | |
---|
153 | #define MIN_FLOAT -1e30f |
---|
154 | #define MAX_FLOAT 1e30f |
---|
155 | |
---|
156 | |
---|
157 | // tolerance value for polygon area |
---|
158 | #define AREA_LIMIT 0.0001f |
---|
159 | |
---|
160 | #ifndef DEL_PTR |
---|
161 | #define DEL_PTR(ptr) do {if (ptr) { \ |
---|
162 | delete (ptr); \ |
---|
163 | (ptr) = 0;}} \ |
---|
164 | while (0) |
---|
165 | #endif |
---|
166 | // Clears a container (i.e., a vector of pointers) and deletes the pointers |
---|
167 | #if 0 |
---|
168 | #ifndef CLEAR_CONTAINER |
---|
169 | #define CLEAR_CONTAINER(co) do { while (!(co).empty()) { \ |
---|
170 | DEL_PTR((co).back()); \ |
---|
171 | (co).pop_back();}} \ |
---|
172 | while (0) |
---|
173 | #endif |
---|
174 | |
---|
175 | #else |
---|
176 | |
---|
177 | #ifndef CLEAR_CONTAINER |
---|
178 | #define CLEAR_CONTAINER(co) do { for (int _i = 0; _i < (int)(co).size(); ++ _i) { \ |
---|
179 | DEL_PTR((co)[_i]);} \ |
---|
180 | (co).clear(); } \ |
---|
181 | while (0) |
---|
182 | #endif |
---|
183 | |
---|
184 | #endif |
---|
185 | |
---|
186 | |
---|
187 | inline |
---|
188 | int signum(const Real a, const Real thresh = TRASH) |
---|
189 | { |
---|
190 | if (a>thresh) |
---|
191 | return 1; |
---|
192 | else |
---|
193 | if (a<-thresh) |
---|
194 | return -1; |
---|
195 | return 0; |
---|
196 | } |
---|
197 | |
---|
198 | inline double Absd(const double a) |
---|
199 | { |
---|
200 | return (a >= 0.0) ? a : -a; |
---|
201 | } |
---|
202 | |
---|
203 | inline float Abs(const float a) |
---|
204 | { |
---|
205 | return (a >= 0.0f) ? a : -a; |
---|
206 | } |
---|
207 | |
---|
208 | // ======================================================= |
---|
209 | // Comparing things |
---|
210 | //struct Limits { |
---|
211 | // const Real thresh=TRASH; |
---|
212 | // const Real small=0.1; |
---|
213 | //}; |
---|
214 | |
---|
215 | template <class T> |
---|
216 | bool |
---|
217 | ClipValue(T &v, const T m, const T M) |
---|
218 | { |
---|
219 | if (v<m) { |
---|
220 | v = m; |
---|
221 | return true; |
---|
222 | } |
---|
223 | if (v>M) { |
---|
224 | v = M; |
---|
225 | return true; |
---|
226 | } |
---|
227 | |
---|
228 | return false; |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | inline int eq(Real a, Real b, Real t=TRASH) |
---|
233 | { |
---|
234 | return Abs(a-b)<t; |
---|
235 | } |
---|
236 | |
---|
237 | inline int leq(Real a,Real b,Real t=TRASH) |
---|
238 | { |
---|
239 | return a - b < t; |
---|
240 | } |
---|
241 | |
---|
242 | inline int geq(Real a,Real b,Real t=TRASH) |
---|
243 | { |
---|
244 | return t > b - a; |
---|
245 | } |
---|
246 | |
---|
247 | inline int le(Real a,Real b,Real t=TRASH) |
---|
248 | { |
---|
249 | return !geq(a,b,t); |
---|
250 | } |
---|
251 | |
---|
252 | inline int ge(Real a,Real b,Real t=TRASH) |
---|
253 | { |
---|
254 | return !leq(a,b,t); |
---|
255 | } |
---|
256 | |
---|
257 | // ======================================================== |
---|
258 | |
---|
259 | // ------------------------------------------------------------------- |
---|
260 | // Indents to a given stream by the number of spaces specified. |
---|
261 | // This routine is located in main.cpp, for lack of a better place. |
---|
262 | // ------------------------------------------------------------------- |
---|
263 | void indent(std::ostream &app, int ind); |
---|
264 | |
---|
265 | // --------------------------------------------------------- |
---|
266 | // RandomValue |
---|
267 | // Returns a random Realing-point value between the two |
---|
268 | // values. Range is inclusive; the function should |
---|
269 | // occasionally return exactly a or b. |
---|
270 | // --------------------------------------------------------- |
---|
271 | inline Real |
---|
272 | RandomValue(Real a, Real b) |
---|
273 | { |
---|
274 | Real range = (Real) Abs(a - b); |
---|
275 | return ((Real)rand() / RAND_MAX) * range + ((a < b) ? a : b); |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | /*inline int |
---|
280 | RandomValue(int a, int b) |
---|
281 | { |
---|
282 | int range = abs(a - b); |
---|
283 | return (rand() * range) / RAND_MAX + ((a < b) ? a : b); |
---|
284 | }*/ |
---|
285 | |
---|
286 | inline Real sqr(Real a) |
---|
287 | { |
---|
288 | return a*a; |
---|
289 | } |
---|
290 | |
---|
291 | |
---|
292 | template <class T> |
---|
293 | void Swap(T &a,T &b) |
---|
294 | { |
---|
295 | T c; |
---|
296 | c = b; |
---|
297 | b = a; |
---|
298 | a = c; |
---|
299 | } |
---|
300 | |
---|
301 | template <class T> |
---|
302 | int eq(T &a, T &b, T &c, T &d) { |
---|
303 | return a == b && c==d && b==c; |
---|
304 | } |
---|
305 | |
---|
306 | template <class T> |
---|
307 | T Min(T a,T b) |
---|
308 | { |
---|
309 | return a<b ? a : b; |
---|
310 | } |
---|
311 | |
---|
312 | template <class T> |
---|
313 | T Max(T a,T b) |
---|
314 | { |
---|
315 | return a>b ? a : b; |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | Real Random(Real max); |
---|
320 | int Random(int max); |
---|
321 | void Randomize(); |
---|
322 | void |
---|
323 | Randomize(const unsigned int seed); |
---|
324 | |
---|
325 | |
---|
326 | void GetKey(char *s=NULL); |
---|
327 | |
---|
328 | inline Real Deg2Rad(const Real a) |
---|
329 | { |
---|
330 | return a*(PI/180.0f); |
---|
331 | } |
---|
332 | |
---|
333 | inline Real Rad2Deg(const Real a) { |
---|
334 | return a*(180.0f/PI); |
---|
335 | } |
---|
336 | |
---|
337 | void InitTiming(); |
---|
338 | long GetTime(); |
---|
339 | long GetRealTime(); |
---|
340 | Real TimeDiff(long t1, long t2); |
---|
341 | char *TimeString(); |
---|
342 | |
---|
343 | |
---|
344 | // manipulator |
---|
345 | inline std::ostream &DEBUGINFO( std::ostream &s ) { |
---|
346 | return s<<"FILE "<<__FILE__<<",LINE "<<__LINE__<<std::endl; |
---|
347 | } |
---|
348 | |
---|
349 | #define DINFO __FILE__<<":"<<__LINE__ |
---|
350 | |
---|
351 | |
---|
352 | class CGlobals { |
---|
353 | public: |
---|
354 | |
---|
355 | static int Special; |
---|
356 | |
---|
357 | }; |
---|
358 | |
---|
359 | // ------------------------------------------------------------------- |
---|
360 | // Limits. |
---|
361 | // This class encapsulates all the concessions to Realing-point |
---|
362 | // error made by ray tracers. |
---|
363 | // ------------------------------------------------------------------- |
---|
364 | |
---|
365 | class Limits { |
---|
366 | public: |
---|
367 | // This is the number used to reject too-close intersections. |
---|
368 | // The default value is 1.0. |
---|
369 | static Real Threshold; |
---|
370 | |
---|
371 | // This is a "small" number. Less than this number is assumed to |
---|
372 | // be 0, when such things matter. |
---|
373 | // The default value is 0.1. |
---|
374 | static Real Small; |
---|
375 | |
---|
376 | // This is an impractically "large" number, used for intersection |
---|
377 | // parameters out to infinity (e.g. the span resulting from an |
---|
378 | // FindAllIntersections operation on a plane). |
---|
379 | // The default value is 100000. |
---|
380 | static Real Infinity; |
---|
381 | }; |
---|
382 | |
---|
383 | // --------------------------------------------------------- |
---|
384 | // EpsilonEqual(x,y) |
---|
385 | // Returns if two values are equal or not (by epsilon) |
---|
386 | // --------------------------------------------------------- |
---|
387 | inline int |
---|
388 | EpsilonEqual(const Real &x, const Real &y) |
---|
389 | { |
---|
390 | return fabs(x-y) < Limits::Small; |
---|
391 | } |
---|
392 | |
---|
393 | // --------------------------------------------------------- |
---|
394 | // EpsilonEqual(x) |
---|
395 | // Returns if a value is zero (+/- epsilon) |
---|
396 | // --------------------------------------------------------- |
---|
397 | inline int |
---|
398 | EpsilonEqual(const Real &x) |
---|
399 | { |
---|
400 | return fabs(x) < Limits::Small; |
---|
401 | } |
---|
402 | |
---|
403 | // --------------------------------------------------------- |
---|
404 | // EpsilonEqual(x,y,epsilon) |
---|
405 | // Returns if two values are equal or not by a given epsilon |
---|
406 | // --------------------------------------------------------- |
---|
407 | inline int |
---|
408 | EpsilonEqual(const Real &x, const Real &y, const Real &epsilon) |
---|
409 | { |
---|
410 | return fabs(x-y) < epsilon; |
---|
411 | } |
---|
412 | |
---|
413 | // --------------------------------------------------------- |
---|
414 | // InRange |
---|
415 | // Returns nonzero if min <= candidate <= max. |
---|
416 | // --------------------------------------------------------- |
---|
417 | template<class T> |
---|
418 | inline int |
---|
419 | InRange(T min, T max, T candidate) |
---|
420 | { |
---|
421 | return (candidate >= min) && (candidate <= max); |
---|
422 | } |
---|
423 | |
---|
424 | // -------------------------------------------------------------- |
---|
425 | // string function with new operator |
---|
426 | |
---|
427 | inline char* |
---|
428 | StrDup(char *src) { |
---|
429 | char *p; |
---|
430 | for (p = src; *p++; ); |
---|
431 | char *dest = new char[p-src]; |
---|
432 | for ( p = dest;(*p++ = *src++) != 0; ); |
---|
433 | return dest; |
---|
434 | } |
---|
435 | |
---|
436 | |
---|
437 | inline char * |
---|
438 | StrToLower(char *src) { |
---|
439 | char *p; |
---|
440 | for (p = src; *p; p++) |
---|
441 | *p = tolower(*p); |
---|
442 | return src; |
---|
443 | } |
---|
444 | |
---|
445 | // return l = log2(a) if a is a power of two else -ceillog2(a) |
---|
446 | inline int |
---|
447 | GetLog2(int a) |
---|
448 | { |
---|
449 | int i, x; |
---|
450 | i = 0; |
---|
451 | x = 1; |
---|
452 | |
---|
453 | while (x < a) { |
---|
454 | i++; |
---|
455 | x <<= 1; |
---|
456 | } |
---|
457 | |
---|
458 | return (x==a) ? i: -i; |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | // return ceil(log2(a)) even if a is not a power of two |
---|
463 | // Example: |
---|
464 | // GetCeilLog2(15) = 4 |
---|
465 | // GetCeilLog2(16) = 4 |
---|
466 | // GetCeilLog2(17) = 5 |
---|
467 | inline int |
---|
468 | GetCeilLog2(int a) |
---|
469 | { |
---|
470 | int i, x; |
---|
471 | if (a < 0) |
---|
472 | return -1; |
---|
473 | |
---|
474 | i = 0; |
---|
475 | x = 1; |
---|
476 | |
---|
477 | while (x < a) { |
---|
478 | i++; |
---|
479 | x <<= 1; |
---|
480 | } |
---|
481 | |
---|
482 | return i; |
---|
483 | } |
---|
484 | |
---|
485 | |
---|
486 | char * |
---|
487 | GetAbsPath(char *path); |
---|
488 | |
---|
489 | char * |
---|
490 | strdup(char *a); |
---|
491 | |
---|
492 | std::string |
---|
493 | ReplaceSuffix(const std::string &filename, const std::string &a, const std::string &b); |
---|
494 | |
---|
495 | |
---|
496 | int |
---|
497 | SplitFilenames(const std::string &str, std::vector<std::string> &filenames); |
---|
498 | |
---|
499 | bool |
---|
500 | FileExists(char *filename); |
---|
501 | |
---|
502 | //#define GTP_DEBUG 1 |
---|
503 | |
---|
504 | #define DEBUG_LEVEL 5 |
---|
505 | //#define DEBUG_LEVEL 1000 |
---|
506 | //#define DEBUG_LEVEL 50000 |
---|
507 | |
---|
508 | // debug stream |
---|
509 | extern std::ofstream Debug; |
---|
510 | |
---|
511 | |
---|
512 | bool |
---|
513 | CreateDir(char *dir); |
---|
514 | |
---|
515 | char * |
---|
516 | GetPath(const char *s); |
---|
517 | |
---|
518 | char * |
---|
519 | StripPath(const char *s); |
---|
520 | } |
---|
521 | |
---|
522 | |
---|
523 | |
---|
524 | #if USE_GZLIB |
---|
525 | // type of out put and input streams |
---|
526 | #define OUT_STREAM ogzstream |
---|
527 | #define IN_STREAM igzstream |
---|
528 | #else |
---|
529 | #define OUT_STREAM ofstream |
---|
530 | #define IN_STREAM ifstream |
---|
531 | #endif |
---|
532 | |
---|
533 | /** view cell id belonging to empty view space. |
---|
534 | */ |
---|
535 | #define OUT_OF_BOUNDS_ID -1 |
---|
536 | |
---|
537 | #endif |
---|
538 | |
---|
539 | |
---|
540 | |
---|
541 | |
---|
542 | |
---|
543 | |
---|
544 | |
---|
545 | |
---|
546 | |
---|
547 | |
---|