source: trunk/VUT/GtpVisibilityPreprocessor/src/common.h @ 235

Revision 235, 8.4 KB checked in by mattausch, 19 years ago (diff)

added some bsp stuff

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