source: GTP/trunk/Lib/Vis/Preprocessing/src/common.h @ 2176

Revision 2176, 10.2 KB checked in by mattausch, 18 years ago (diff)

removed using namespace std from .h

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