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