source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h @ 2903

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