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