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

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