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

Revision 574, 9.4 KB checked in by mattausch, 18 years ago (diff)

finished function for view cell construction
removed bsp rays from vspbspmanager

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