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

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