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

Revision 865, 9.6 KB checked in by mattausch, 19 years ago (diff)

fixed uint bug

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 // $$$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#ifdef sgi
79#define OUT_BIN_MODE ios::out
80#define IN_BIN_MODE ios::in
81#else // sgi
82#if defined(__WATCOMC__) || defined(_MSC_VER)
83#define OUT_BIN_MODE ios::out | ios::binary
84#define IN_BIN_MODE ios::in | ios::binary
85#else
86#define OUT_BIN_MODE ios::out | ios::bin
87#define IN_BIN_MODE ios::in | ios::bin
88#endif // __WATCOMC_
89#endif // sgi
90
91
92//  #ifndef HAS_BOOL
93//  //enum bool {
94//  //  false = 0,
95//  //  true
96//  //};
97//  #define bool int
98//  #define false 0
99//  #define true  1
100//  #endif // HAS_BOOL
101
102typedef unsigned long dword;
103
104#ifndef NULL
105#define NULL (void *)0
106#endif // NULL
107
108//ostream& operator<<(ostream &s,const BaseC &c);
109
110typedef float Real;
111typedef unsigned char byte;
112
113//#ifndef FALSE
114//#define FALSE 0
115//#define TRUE !0
116//#endif
117
118#ifndef __GNUG__
119// typedef int bool;
120#endif
121
122#ifndef getch
123#define getch() getchar()
124#endif
125
126
127#define TRASH 1.0e-5
128
129#ifndef PI
130#define PI    3.14159265358979323846f
131#endif
132
133#define MIN_FLOAT -1e30f
134#define MAX_FLOAT  1e30f
135
136
137// tolerance value for polygon area
138#define AREA_LIMIT 0.0001f
139
140#ifndef DEL_PTR
141#define DEL_PTR(ptr) do {if (ptr) {        \
142                           delete (ptr);   \
143                                                   (ptr) = 0;}}    \
144                     while (0)
145#endif
146// Clears a container (i.e., a vector of pointers) and deletes the pointers
147#if 0
148#ifndef CLEAR_CONTAINER
149#define CLEAR_CONTAINER(co) do { while (!(co).empty()) {   \
150                                                           DEL_PTR((co).back());     \
151                                                                   (co).pop_back();}}      \
152                                                        while (0)
153#endif
154
155#else
156
157#ifndef CLEAR_CONTAINER
158#define CLEAR_CONTAINER(co) do { for (int _i = 0; _i < (int)(co).size(); ++ _i) { \
159        DEL_PTR((co)[_i]);} \
160        (co).clear(); } \
161while (0)
162#endif
163
164#endif
165
166
167inline
168int signum(const Real a, const Real thresh = TRASH)
169{
170  if (a>thresh)
171    return 1;
172  else
173    if (a<-thresh)
174      return -1;
175  return 0;
176}
177
178inline double Absd(const double a)
179{
180  return (a >= 0.0) ? a : -a;
181}
182
183inline float Abs(const float a)
184{
185  return (a >= 0.0f) ? a : -a;
186}
187
188// =======================================================
189// Comparing things
190//struct Limits {
191//  const Real thresh=TRASH;
192//  const Real small=0.1;
193//};
194
195template <class T>
196bool
197ClipValue(T &v, const T m, const T M)
198{
199  if (v<m) {
200    v = m;
201    return true;
202  }
203  if (v>M) {
204    v = M;
205    return true;
206  }
207
208  return false;
209}
210
211
212inline int eq(Real a, Real b, Real t=TRASH)
213{
214  return Abs(a-b)<t;
215}
216
217inline int leq(Real a,Real b,Real t=TRASH)
218{
219  return a - b < t;
220}
221
222inline int geq(Real a,Real b,Real t=TRASH)
223{
224  return t > b - a;
225}
226
227inline int le(Real a,Real b,Real t=TRASH)
228{
229  return !geq(a,b,t);
230}
231
232inline int ge(Real a,Real b,Real t=TRASH)
233{
234  return !leq(a,b,t);
235}
236
237// ========================================================
238
239// -------------------------------------------------------------------
240// Indents to a given stream by the number of spaces specified.
241// This routine is located in main.cpp, for lack of a better place.
242// -------------------------------------------------------------------
243void indent(ostream &app, int ind);
244
245// ---------------------------------------------------------
246// RandomValue
247//      Returns a random Realing-point value between the two
248//      values.  Range is inclusive; the function should
249//      occasionally return exactly a or b.
250// ---------------------------------------------------------
251inline Real
252RandomValue(Real a, Real b)
253{
254  Real range = (Real) Abs(a - b);
255  return ((Real)rand() / RAND_MAX) * range + ((a < b) ? a : b);
256}
257
258
259
260
261inline Real sqr(Real a)
262{
263  return a*a;
264}
265
266template <class T>
267void Swap(T &a,T &b)
268{
269  T c;                 
270  c = b;
271  b = a;
272  a = c;
273}
274
275template <class T>
276int eq(T &a, T &b, T &c, T &d) {
277  return a == b && c==d && b==c;
278}
279
280template <class T>
281T Min(T a,T b)
282{
283  return a<b ? a : b;
284}
285
286template <class T>
287T Max(T a,T b)
288{
289  return a>b ? a : b;
290}
291
292
293Real Random(Real max);
294int  Random(int max);
295void Randomize();
296void
297Randomize(const unsigned int seed);
298
299
300void GetKey(char *s=NULL);
301
302inline Real Deg2Rad(const Real a)
303{
304   return a*(PI/180.0f);
305}
306
307inline Real Rad2Deg(const Real a) {
308  return a*(180.0f/PI);
309}
310 
311void InitTiming();
312long GetTime();
313long GetRealTime();
314Real TimeDiff(long t1,long t2);
315char *TimeString();
316
317
318// manipulator
319inline ostream &DEBUGINFO( ostream &s ) {
320  return s<<"FILE "<<__FILE__<<",LINE "<<__LINE__<<endl;
321}
322
323#define DINFO __FILE__<<":"<<__LINE__
324
325
326class CGlobals {
327public:
328
329  static int Special;
330
331};
332
333// -------------------------------------------------------------------
334// Limits.
335//  This class encapsulates all the concessions to Realing-point
336//  error made by ray tracers.
337// -------------------------------------------------------------------
338
339class Limits {
340public:
341  // This is the number used to reject too-close intersections.
342  // The default value is 1.0.
343  static Real Threshold;
344
345  // This is a "small" number.  Less than this number is assumed to
346  // be 0, when such things matter.
347  // The default value is 0.1.
348  static Real Small;
349
350  // This is an impractically "large" number, used for intersection
351  // parameters out to infinity (e.g. the span resulting from an
352  // FindAllIntersections operation on a plane).
353  // The default value is 100000.
354  static Real Infinity;
355};
356
357// ---------------------------------------------------------
358// EpsilonEqual(x,y)
359//   Returns if two values are equal or not (by epsilon)
360// ---------------------------------------------------------
361inline int
362EpsilonEqual(const Real &x, const Real &y)
363{
364  return fabs(x-y) < Limits::Small;
365}
366
367// ---------------------------------------------------------
368// EpsilonEqual(x)
369//   Returns if a value is zero (+/- epsilon)
370// ---------------------------------------------------------
371inline int
372EpsilonEqual(const Real &x)
373{
374  return fabs(x) < Limits::Small;
375}
376
377// ---------------------------------------------------------
378// EpsilonEqual(x,y,epsilon)
379//   Returns if two values are equal or not by a given epsilon
380// ---------------------------------------------------------
381inline int
382EpsilonEqual(const Real &x, const Real &y, const Real &epsilon)
383{
384  return fabs(x-y) < epsilon;
385}
386
387// ---------------------------------------------------------
388// InRange
389//      Returns nonzero if min <= candidate <= max.
390// ---------------------------------------------------------
391template<class T>
392inline int
393InRange(T min, T max, T candidate)
394{
395  return (candidate >= min) && (candidate <= max);
396}
397
398// --------------------------------------------------------------
399// string function with new operator
400
401inline char*
402StrDup(char *src) {
403  char *p;
404  for (p = src; *p++; );
405  char *dest = new char[p-src];
406  for ( p = dest;(*p++ = *src++) != 0; );
407  return dest;
408}
409
410
411inline char *
412StrToLower(char *src) {
413  char *p;
414  for (p = src; *p; p++)
415    *p = tolower(*p);
416  return src;
417}
418
419// return l = log2(a) if a is a power of two else -ceillog2(a)
420inline int
421GetLog2(int a)
422{
423  int i, x;
424  i = 0;
425  x = 1;
426
427  while (x < a) {
428    i++;
429    x <<= 1;
430  }
431
432  return (x==a) ? i: -i;
433}
434
435
436// return ceil(log2(a)) even if a is not a power of two
437// Example:
438// GetCeilLog2(15) = 4
439// GetCeilLog2(16) = 4
440// GetCeilLog2(17) = 5
441inline int
442GetCeilLog2(int a)
443{
444  int i, x;
445  if (a < 0)
446    return -1;
447
448  i = 0;
449  x = 1;
450
451  while (x < a) {
452    i++;
453    x <<= 1;
454  }
455
456  return i;
457}
458
459
460char *
461GetAbsPath(char *path);
462
463char *
464strdup(char *a);
465
466bool
467FileExists(char *filename);
468
469#define DEBUG_LEVEL 5
470//#define DEBUG_LEVEL 1000
471//#define DEBUG_LEVEL 50000
472 
473// debug stream
474extern ofstream Debug;
475
476
477bool
478CreateDir(char *dir);
479
480char *
481GetPath(const char *s);
482
483}
484
485#endif
486
487
488
489
490
491
492
493
494
495
496
Note: See TracBrowser for help on using the repository browser.