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

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