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

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