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

Revision 2756, 8.9 KB checked in by mattausch, 17 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 Randomize(unsigned int seed);
290
291
292void GetKey(char *s=NULL);
293
294inline Real Deg2Rad(const Real a)
295{
296   return a * (M_PI / 180.0f);
297}
298
299inline Real Rad2Deg(const Real a) {
300  return a * (180.0f/M_PI);
301}
302 
303void InitTiming();
304long GetTime();
305long GetRealTime();
306Real TimeDiff(long t1, long t2);
307char *TimeString();
308
309
310
311/** Limits.
312    This class encapsulates all the concessions to Realing-point
313        error made by ray tracers.
314*/
315class Limits
316{
317public:
318        // This is the number used to reject too-close intersections.
319        // The default value is 1.0.
320        static Real Threshold;
321
322        // This is a "small" number.  Less than this number is assumed to
323        // be 0, when such things matter.
324        // The default value is 0.1.
325        static Real Small;
326
327        // This is an impractically "large" number, used for intersection
328        // parameters out to infinity (e.g. the span resulting from an
329        // FindAllIntersections operation on a plane).
330        // The default value is 100000.
331        static Real Infinity;
332};
333
334// ---------------------------------------------------------
335// EpsilonEqual(x,y)
336//   Returns if two values are equal or not (by epsilon)
337// ---------------------------------------------------------
338inline int
339EpsilonEqual(const Real &x, const Real &y)
340{
341  return fabs(x-y) < Limits::Small;
342}
343
344// ---------------------------------------------------------
345// EpsilonEqual(x)
346//   Returns if a value is zero (+/- epsilon)
347// ---------------------------------------------------------
348inline int
349EpsilonEqual(const Real &x)
350{
351  return fabs(x) < Limits::Small;
352}
353
354// ---------------------------------------------------------
355// EpsilonEqual(x,y,epsilon)
356//   Returns if two values are equal or not by a given epsilon
357// ---------------------------------------------------------
358inline int
359EpsilonEqual(const Real &x, const Real &y, const Real &epsilon)
360{
361  return fabs(x-y) < epsilon;
362}
363
364// ---------------------------------------------------------
365// InRange
366//      Returns nonzero if min <= candidate <= max.
367// ---------------------------------------------------------
368template<class T>
369inline int
370InRange(T min, T max, T candidate)
371{
372  return (candidate >= min) && (candidate <= max);
373}
374
375// --------------------------------------------------------------
376// string function with new operator
377
378inline char*
379StrDup(char *src) {
380  char *p;
381  for (p = src; *p++; );
382  char *dest = new char[p-src];
383  for ( p = dest;(*p++ = *src++) != 0; );
384  return dest;
385}
386
387
388inline char *
389StrToLower(char *src) {
390  char *p;
391  for (p = src; *p; p++)
392    *p = tolower(*p);
393  return src;
394}
395
396// return l = log2(a) if a is a power of two else -ceillog2(a)
397inline int
398GetLog2(int a)
399{
400  int i, x;
401  i = 0;
402  x = 1;
403
404  while (x < a) {
405    i++;
406    x <<= 1;
407  }
408
409  return (x==a) ? i: -i;
410}
411
412
413// return ceil(log2(a)) even if a is not a power of two
414// Example:
415// GetCeilLog2(15) = 4
416// GetCeilLog2(16) = 4
417// GetCeilLog2(17) = 5
418inline int
419GetCeilLog2(int a)
420{
421  int i, x;
422  if (a < 0)
423    return -1;
424
425  i = 0;
426  x = 1;
427
428  while (x < a) {
429    i++;
430    x <<= 1;
431  }
432
433  return i;
434}
435
436
437char *
438GetAbsPath(char *path);
439
440char *
441strdup(char *a);
442
443std::string
444ReplaceSuffix(const std::string &filename, const std::string &a, const std::string &b);
445
446
447int
448SplitFilenames(const std::string &str, std::vector<std::string> &filenames);
449
450bool
451FileExists(char *filename);
452
453bool
454CreateDir(char *dir);
455
456char *
457GetPath(const char *s);
458
459char *
460StripPath(const char *s);
461
462
463
464///////////
465//-- typedefs
466
467typedef std::vector<BvhNode *> BvhNodeContainer;
468typedef std::vector<SceneEntity *> SceneEntityContainer;
469typedef std::vector<BvhLeaf *> BvhLeafContainer;
470typedef std::vector<Triangle3> TriangleContainer;
471
472typedef std::queue<OcclusionQuery *> QueryQueue;
473
474static std::ofstream Debug("debug.log");
475
476}
477
478#endif
479
480
481
482
483
484
485
486
Note: See TracBrowser for help on using the repository browser.