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