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

Revision 1757, 10.2 KB checked in by bittner, 18 years ago (diff)

pvs updates

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+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
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/*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// manipulator
337inline ostream &DEBUGINFO( ostream &s ) {
338  return s<<"FILE "<<__FILE__<<",LINE "<<__LINE__<<endl;
339}
340
341#define DINFO __FILE__<<":"<<__LINE__
342
343
344class CGlobals {
345public:
346
347  static int Special;
348
349};
350
351// -------------------------------------------------------------------
352// Limits.
353//  This class encapsulates all the concessions to Realing-point
354//  error made by ray tracers.
355// -------------------------------------------------------------------
356
357class Limits {
358public:
359  // This is the number used to reject too-close intersections.
360  // The default value is 1.0.
361  static Real Threshold;
362
363  // This is a "small" number.  Less than this number is assumed to
364  // be 0, when such things matter.
365  // The default value is 0.1.
366  static Real Small;
367
368  // This is an impractically "large" number, used for intersection
369  // parameters out to infinity (e.g. the span resulting from an
370  // FindAllIntersections operation on a plane).
371  // The default value is 100000.
372  static Real Infinity;
373};
374
375// ---------------------------------------------------------
376// EpsilonEqual(x,y)
377//   Returns if two values are equal or not (by epsilon)
378// ---------------------------------------------------------
379inline int
380EpsilonEqual(const Real &x, const Real &y)
381{
382  return fabs(x-y) < Limits::Small;
383}
384
385// ---------------------------------------------------------
386// EpsilonEqual(x)
387//   Returns if a value is zero (+/- epsilon)
388// ---------------------------------------------------------
389inline int
390EpsilonEqual(const Real &x)
391{
392  return fabs(x) < Limits::Small;
393}
394
395// ---------------------------------------------------------
396// EpsilonEqual(x,y,epsilon)
397//   Returns if two values are equal or not by a given epsilon
398// ---------------------------------------------------------
399inline int
400EpsilonEqual(const Real &x, const Real &y, const Real &epsilon)
401{
402  return fabs(x-y) < epsilon;
403}
404
405// ---------------------------------------------------------
406// InRange
407//      Returns nonzero if min <= candidate <= max.
408// ---------------------------------------------------------
409template<class T>
410inline int
411InRange(T min, T max, T candidate)
412{
413  return (candidate >= min) && (candidate <= max);
414}
415
416// --------------------------------------------------------------
417// string function with new operator
418
419inline char*
420StrDup(char *src) {
421  char *p;
422  for (p = src; *p++; );
423  char *dest = new char[p-src];
424  for ( p = dest;(*p++ = *src++) != 0; );
425  return dest;
426}
427
428
429inline char *
430StrToLower(char *src) {
431  char *p;
432  for (p = src; *p; p++)
433    *p = tolower(*p);
434  return src;
435}
436
437// return l = log2(a) if a is a power of two else -ceillog2(a)
438inline int
439GetLog2(int a)
440{
441  int i, x;
442  i = 0;
443  x = 1;
444
445  while (x < a) {
446    i++;
447    x <<= 1;
448  }
449
450  return (x==a) ? i: -i;
451}
452
453
454// return ceil(log2(a)) even if a is not a power of two
455// Example:
456// GetCeilLog2(15) = 4
457// GetCeilLog2(16) = 4
458// GetCeilLog2(17) = 5
459inline int
460GetCeilLog2(int a)
461{
462  int i, x;
463  if (a < 0)
464    return -1;
465
466  i = 0;
467  x = 1;
468
469  while (x < a) {
470    i++;
471    x <<= 1;
472  }
473
474  return i;
475}
476
477
478char *
479GetAbsPath(char *path);
480
481char *
482strdup(char *a);
483
484bool
485FileExists(char *filename);
486
487//#define GTP_DEBUG 1
488
489#define DEBUG_LEVEL 5
490//#define DEBUG_LEVEL 1000
491//#define DEBUG_LEVEL 50000
492 
493// debug stream
494extern ofstream Debug;
495
496
497bool
498CreateDir(char *dir);
499
500char *
501GetPath(const char *s);
502
503char *
504StripPath(const char *s);
505}
506
507
508#if USE_GZLIB
509        // type of out put and input streams
510        #define OUT_STREAM ogzstream
511        #define IN_STREAM igzstream
512#else
513        #define OUT_STREAM ofstream
514        #define IN_STREAM ifstream
515#endif
516
517/** view cell id belonging to empty view space.
518*/
519#define OUT_OF_BOUNDS_ID -1
520
521#endif
522
523
524
525
526
527
528
529
530
531
Note: See TracBrowser for help on using the repository browser.