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

Revision 261, 8.5 KB checked in by mattausch, 19 years ago (diff)

added viewcell loader

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