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

Revision 312, 8.9 KB checked in by mattausch, 19 years ago (diff)

bsp split exporter added

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// tolerance value for side relation
130#define SIDE_TOLERANCE 0.002f // TODO: Test different values
131#define SIDE_TOLERANCE_SQRD 0.000004f
132#define AREA_LIMIT 0.0001f
133
134#ifndef DEL_PTR
135#define DEL_PTR(ptr) do {if (ptr) {        \
136                           delete (ptr);   \
137                                                   (ptr) = 0;}}    \
138                     while (0)
139#endif
140// Clears a container (i.e., a vector of pointers) and deletes the pointers
141#ifndef CLEAR_CONTAINER
142#define CLEAR_CONTAINER(co) do { while (!(co).empty()) {   \
143                                                           DEL_PTR((co).back());     \
144                                                                   (co).pop_back();}}      \
145                                                        while (0)
146#endif
147
148inline
149int signum(const Real a, const Real thresh = TRASH)
150{
151  if (a>thresh)
152    return 1;
153  else
154    if (a<-thresh)
155      return -1;
156  return 0;
157}
158
159inline double Absd(const double a)
160{
161  return (a >= 0.0) ? a : -a;
162}
163
164inline float Abs(const float a)
165{
166  return (a >= 0.0f) ? a : -a;
167}
168
169// =======================================================
170// Comparing things
171//struct Limits {
172//  const Real thresh=TRASH;
173//  const Real small=0.1;
174//};
175
176template <class T>
177bool
178ClipValue(T &v, const T m, const T M)
179{
180  if (v<m) {
181    v = m;
182    return true;
183  }
184  if (v>M) {
185    v = M;
186    return true;
187  }
188
189  return false;
190}
191
192
193inline int eq(Real a, Real b, Real t=TRASH)
194{
195  return Abs(a-b)<t;
196}
197
198inline int leq(Real a,Real b,Real t=TRASH)
199{
200  return a - b < t;
201}
202
203inline int geq(Real a,Real b,Real t=TRASH)
204{
205  return t > b - a;
206}
207
208inline int le(Real a,Real b,Real t=TRASH)
209{
210  return !geq(a,b,t);
211}
212
213inline int ge(Real a,Real b,Real t=TRASH)
214{
215  return !leq(a,b,t);
216}
217
218// ========================================================
219
220// -------------------------------------------------------------------
221// Indents to a given stream by the number of spaces specified.
222// This routine is located in main.cpp, for lack of a better place.
223// -------------------------------------------------------------------
224void indent(ostream &app, int ind);
225
226// ---------------------------------------------------------
227// RandomValue
228//      Returns a random Realing-point value between the two
229//      values.  Range is inclusive; the function should
230//      occasionally return exactly a or b.
231// ---------------------------------------------------------
232inline Real
233RandomValue(Real a, Real b)
234{
235  Real range = (Real) Abs(a - b);
236  return ((Real)rand() / RAND_MAX) * range + ((a < b) ? a : b);
237}
238
239
240
241inline Real sqr(Real a)
242{
243  return a*a;
244}
245
246template <class T>
247void Swap(T &a,T &b)
248{
249  T c;                 
250  c = b;
251  b = a;
252  a = c;
253}
254
255template <class T>
256int eq(T &a, T &b, T &c, T &d) {
257  return a == b && c==d && b==c;
258}
259
260template <class T>
261T Min(T a,T b)
262{
263  return a<b ? a : b;
264}
265
266template <class T>
267T Max(T a,T b)
268{
269  return a>b ? a : b;
270}
271
272
273Real Random(Real max);
274int  Random(int max);
275void Randomize();
276void
277Randomize(const unsigned int seed);
278
279
280void GetKey(char *s=NULL);
281
282inline Real Deg2Rad(const Real a)
283{
284   return a*(PI/180.0f);
285}
286
287inline Real Rad2Deg(const Real a) {
288  return a*(180.0f/PI);
289}
290 
291void InitTiming();
292long GetTime();
293long GetRealTime();
294Real TimeDiff(long t1,long t2);
295char *TimeString();
296
297
298// manipulator
299inline ostream &DEBUGINFO( ostream &s ) {
300  return s<<"FILE "<<__FILE__<<",LINE "<<__LINE__<<endl;
301}
302
303#define DINFO __FILE__<<":"<<__LINE__
304
305
306class CGlobals {
307public:
308
309  static int Special;
310
311};
312
313// -------------------------------------------------------------------
314// Limits.
315//  This class encapsulates all the concessions to Realing-point
316//  error made by ray tracers.
317// -------------------------------------------------------------------
318
319class Limits {
320public:
321  // This is the number used to reject too-close intersections.
322  // The default value is 1.0.
323  static Real Threshold;
324
325  // This is a "small" number.  Less than this number is assumed to
326  // be 0, when such things matter.
327  // The default value is 0.1.
328  static Real Small;
329
330  // This is an impractically "large" number, used for intersection
331  // parameters out to infinity (e.g. the span resulting from an
332  // FindAllIntersections operation on a plane).
333  // The default value is 100000.
334  static Real Infinity;
335};
336
337// ---------------------------------------------------------
338// EpsilonEqual(x,y)
339//   Returns if two values are equal or not (by epsilon)
340// ---------------------------------------------------------
341inline int
342EpsilonEqual(const Real &x, const Real &y)
343{
344  return fabs(x-y) < Limits::Small;
345}
346
347// ---------------------------------------------------------
348// EpsilonEqual(x)
349//   Returns if a value is zero (+/- epsilon)
350// ---------------------------------------------------------
351inline int
352EpsilonEqual(const Real &x)
353{
354  return fabs(x) < Limits::Small;
355}
356
357// ---------------------------------------------------------
358// EpsilonEqual(x,y,epsilon)
359//   Returns if two values are equal or not by a given epsilon
360// ---------------------------------------------------------
361inline int
362EpsilonEqual(const Real &x, const Real &y, const Real &epsilon)
363{
364  return fabs(x-y) < epsilon;
365}
366
367// ---------------------------------------------------------
368// InRange
369//      Returns nonzero if min <= candidate <= max.
370// ---------------------------------------------------------
371template<class T>
372inline int
373InRange(T min, T max, T candidate)
374{
375  return (candidate >= min) && (candidate <= max);
376}
377
378// --------------------------------------------------------------
379// string function with new operator
380
381inline char*
382StrDup(char *src) {
383  char *p;
384  for (p = src; *p++; );
385  char *dest = new char[p-src];
386  for ( p = dest;(*p++ = *src++) != 0; );
387  return dest;
388}
389
390
391inline char *
392StrToLower(char *src) {
393  char *p;
394  for (p = src; *p; p++)
395    *p = tolower(*p);
396  return src;
397}
398
399// return l = log2(a) if a is a power of two else -ceillog2(a)
400inline int
401GetLog2(int a)
402{
403  int i, x;
404  i = 0;
405  x = 1;
406
407  while (x < a) {
408    i++;
409    x <<= 1;
410  }
411
412  return (x==a) ? i: -i;
413}
414
415
416// return ceil(log2(a)) even if a is not a power of two
417// Example:
418// GetCeilLog2(15) = 4
419// GetCeilLog2(16) = 4
420// GetCeilLog2(17) = 5
421inline int
422GetCeilLog2(int a)
423{
424  int i, x;
425  if (a < 0)
426    return -1;
427
428  i = 0;
429  x = 1;
430
431  while (x < a) {
432    i++;
433    x <<= 1;
434  }
435
436  return i;
437}
438
439
440char *
441GetAbsPath(char *path);
442
443char *
444strdup(char *a);
445
446bool
447FileExists(char *filename);
448
449#define DEBUG_LEVEL 5
450//#define DEBUG_LEVEL 1000
451//#define DEBUG_LEVEL 50000
452 
453// debug stream
454extern ofstream Debug;
455
456
457bool
458CreateDir(char *dir);
459
460char *
461GetPath(const char *s);
462
463#endif
464
465
466
467
468
469
470
471
472
473
474
Note: See TracBrowser for help on using the repository browser.