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

Revision 2726, 10.2 KB checked in by mattausch, 17 years ago (diff)

worked on gvs efficiency

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