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

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