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