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

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