source: GTP/trunk/App/Demos/Vis/CHC_revisited/common.h @ 2755

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