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

Revision 1201, 9.9 KB checked in by mattausch, 18 years ago (diff)

added loader for osp trees

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
23using namespace std;
24
25
26
27namespace GtpVisibilityPreprocessor {
28
29
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.14159265358979323846
56#define MAXFLOAT    3.40282347e+38F
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
120//ostream& operator<<(ostream &s,const BaseC &c);
121
122typedef float Real;
123typedef unsigned char byte;
124
125//#ifndef FALSE
126//#define FALSE 0
127//#define TRUE !0
128//#endif
129
130#ifndef __GNUG__
131// typedef int bool;
132#endif
133
134#ifndef getch
135#define getch() getchar()
136#endif
137
138
139#define TRASH 1.0e-5
140
141#ifndef PI
142#define PI    3.14159265358979323846f
143#endif
144
145#define MIN_FLOAT -1e30f
146#define MAX_FLOAT  1e30f
147
148
149// tolerance value for polygon area
150#define AREA_LIMIT 0.0001f
151
152#ifndef DEL_PTR
153#define DEL_PTR(ptr) do {if (ptr) {        \
154                           delete (ptr);   \
155                                                   (ptr) = 0;}}    \
156                     while (0)
157#endif
158// Clears a container (i.e., a vector of pointers) and deletes the pointers
159#if 0
160#ifndef CLEAR_CONTAINER
161#define CLEAR_CONTAINER(co) do { while (!(co).empty()) {   \
162                                                           DEL_PTR((co).back());     \
163                                                                   (co).pop_back();}}      \
164                                                        while (0)
165#endif
166
167#else
168
169#ifndef CLEAR_CONTAINER
170#define CLEAR_CONTAINER(co) do { for (int _i = 0; _i < (int)(co).size(); ++ _i) { \
171        DEL_PTR((co)[_i]);} \
172        (co).clear(); } \
173while (0)
174#endif
175
176#endif
177
178
179inline
180int 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(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
264RandomValue(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
272
273inline Real sqr(Real a)
274{
275  return a*a;
276}
277
278template <class T>
279void Swap(T &a,T &b)
280{
281  T c;                 
282  c = b;
283  b = a;
284  a = c;
285}
286
287template <class T>
288int eq(T &a, T &b, T &c, T &d) {
289  return a == b && c==d && b==c;
290}
291
292template <class T>
293T Min(T a,T b)
294{
295  return a<b ? a : b;
296}
297
298template <class T>
299T Max(T a,T b)
300{
301  return a>b ? a : b;
302}
303
304
305Real Random(Real max);
306int  Random(int max);
307void Randomize();
308void
309Randomize(const unsigned int seed);
310
311
312void GetKey(char *s=NULL);
313
314inline Real Deg2Rad(const Real a)
315{
316   return a*(PI/180.0f);
317}
318
319inline Real Rad2Deg(const Real a) {
320  return a*(180.0f/PI);
321}
322 
323void InitTiming();
324long GetTime();
325long GetRealTime();
326Real TimeDiff(long t1,long t2);
327char *TimeString();
328
329
330// manipulator
331inline ostream &DEBUGINFO( ostream &s ) {
332  return s<<"FILE "<<__FILE__<<",LINE "<<__LINE__<<endl;
333}
334
335#define DINFO __FILE__<<":"<<__LINE__
336
337
338class CGlobals {
339public:
340
341  static int Special;
342
343};
344
345// -------------------------------------------------------------------
346// Limits.
347//  This class encapsulates all the concessions to Realing-point
348//  error made by ray tracers.
349// -------------------------------------------------------------------
350
351class Limits {
352public:
353  // This is the number used to reject too-close intersections.
354  // The default value is 1.0.
355  static Real Threshold;
356
357  // This is a "small" number.  Less than this number is assumed to
358  // be 0, when such things matter.
359  // The default value is 0.1.
360  static Real Small;
361
362  // This is an impractically "large" number, used for intersection
363  // parameters out to infinity (e.g. the span resulting from an
364  // FindAllIntersections operation on a plane).
365  // The default value is 100000.
366  static Real Infinity;
367};
368
369// ---------------------------------------------------------
370// EpsilonEqual(x,y)
371//   Returns if two values are equal or not (by epsilon)
372// ---------------------------------------------------------
373inline int
374EpsilonEqual(const Real &x, const Real &y)
375{
376  return fabs(x-y) < Limits::Small;
377}
378
379// ---------------------------------------------------------
380// EpsilonEqual(x)
381//   Returns if a value is zero (+/- epsilon)
382// ---------------------------------------------------------
383inline int
384EpsilonEqual(const Real &x)
385{
386  return fabs(x) < Limits::Small;
387}
388
389// ---------------------------------------------------------
390// EpsilonEqual(x,y,epsilon)
391//   Returns if two values are equal or not by a given epsilon
392// ---------------------------------------------------------
393inline int
394EpsilonEqual(const Real &x, const Real &y, const Real &epsilon)
395{
396  return fabs(x-y) < epsilon;
397}
398
399// ---------------------------------------------------------
400// InRange
401//      Returns nonzero if min <= candidate <= max.
402// ---------------------------------------------------------
403template<class T>
404inline int
405InRange(T min, T max, T candidate)
406{
407  return (candidate >= min) && (candidate <= max);
408}
409
410// --------------------------------------------------------------
411// string function with new operator
412
413inline char*
414StrDup(char *src) {
415  char *p;
416  for (p = src; *p++; );
417  char *dest = new char[p-src];
418  for ( p = dest;(*p++ = *src++) != 0; );
419  return dest;
420}
421
422
423inline char *
424StrToLower(char *src) {
425  char *p;
426  for (p = src; *p; p++)
427    *p = tolower(*p);
428  return src;
429}
430
431// return l = log2(a) if a is a power of two else -ceillog2(a)
432inline int
433GetLog2(int a)
434{
435  int i, x;
436  i = 0;
437  x = 1;
438
439  while (x < a) {
440    i++;
441    x <<= 1;
442  }
443
444  return (x==a) ? i: -i;
445}
446
447
448// return ceil(log2(a)) even if a is not a power of two
449// Example:
450// GetCeilLog2(15) = 4
451// GetCeilLog2(16) = 4
452// GetCeilLog2(17) = 5
453inline int
454GetCeilLog2(int a)
455{
456  int i, x;
457  if (a < 0)
458    return -1;
459
460  i = 0;
461  x = 1;
462
463  while (x < a) {
464    i++;
465    x <<= 1;
466  }
467
468  return i;
469}
470
471
472char *
473GetAbsPath(char *path);
474
475char *
476strdup(char *a);
477
478bool
479FileExists(char *filename);
480
481#define DEBUG_LEVEL 5
482//#define DEBUG_LEVEL 1000
483//#define DEBUG_LEVEL 50000
484 
485// debug stream
486extern ofstream Debug;
487
488
489bool
490CreateDir(char *dir);
491
492char *
493GetPath(const char *s);
494
495}
496
497
498#if USE_GZLIB
499        // type of out put and input streams
500        #define OUT_STREAM ogzstream
501        #define IN_STREAM igzstream
502#else
503        #define OUT_STREAM ofstream
504        #define IN_STREAM ifstream
505#endif
506
507#endif
508
509
510
511
512
513
514
515
516
517
Note: See TracBrowser for help on using the repository browser.