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