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

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