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

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