Subversion Repositories gelsvn

Rev

Rev 277 | Rev 624 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
248 bj 1
// $Id: CGLA.h 417 2009-01-12 15:40:09Z jrf $
2
 
12 jab 3
#ifndef __CGLA_CGLA_H__
4
#define __CGLA_CGLA_H__
2 bj 5
 
102 bj 6
#if (_MSC_VER >= 1200)
195 jrf 7
#pragma warning (disable: 4244 4800)
102 bj 8
#endif
9
 
2 bj 10
#include <cmath>
66 bj 11
#include <cfloat>
2 bj 12
#include <climits>
13
#include <cassert>
14
#include <algorithm>
66 bj 15
#include <functional>
2 bj 16
 
17
#ifndef M_PI
18
#define M_PI 3.14159265358979323846
19
#define M_PI_2 1.57079632679489661923
20
#endif
21
 
12 jab 22
namespace CGLA 
23
{
129 jab 24
		inline float cgla_nan() 
25
		{
196 jrf 26
				static const float cgla_nan_value = log(-1.0f);
129 jab 27
				return cgla_nan_value;
28
		}
29
 
21 jrf 30
  /** Procedural definition of NAN */  
129 jab 31
#define CGLA_NAN cgla_nan()
21 jrf 32
 
33
  /** NAN is used for initialization of vectors and matrices
34
      in debug mode */
129 jab 35
#define CGLA_INIT_VALUE cgla_nan()
21 jrf 36
 
12 jab 37
  /** Numerical constant representing something large.
38
      value is a bit arbitrary */
39
  const double BIG=10e+30;
2 bj 40
 
12 jab 41
  /** Numerical constant represents something extremely small.
42
      value is a bit arbitrary */
43
  const double MINUTE=10e-30;
2 bj 44
 
12 jab 45
  /** Numerical constant represents something very small.
46
      value is a bit arbitrary */
47
  const double TINY=3e-7;
2 bj 48
 
12 jab 49
  /** Numerical constant represents something small.
50
      value is a bit arbitrary */
51
  const double SMALL=10e-2;
2 bj 52
 
417 jrf 53
  /** The GEL pseudo-random number generator uses
54
      UINT_MAX as RAND_MAX to avoid mod operations. */
55
  const unsigned int GEL_RAND_MAX=UINT_MAX;
56
 
129 jab 57
		inline double sqrt3()
58
		{
59
				static const double sqrt3_val = sqrt(3.0);
60
				return sqrt3_val;
61
		}
2 bj 62
 
129 jab 63
#define SQRT3 sqrt3()
64
 
12 jab 65
  /// Useful enum that represents coordiante axes.
66
  enum Axis {XAXIS=0,YAXIS=1,ZAXIS=2};
2 bj 67
 
197 jrf 68
  inline bool isnan(double x) { return x != x; }
55 jab 69
 
12 jab 70
  template<class Scalar>
71
  Scalar s_min(Scalar a, Scalar b)
72
  {
73
    return a<b ? a : b;
74
  }
2 bj 75
 
12 jab 76
  template<class Scalar>
77
  Scalar s_max(Scalar a, Scalar b)
78
  {
79
    return a>b ? a : b;
80
  }
2 bj 81
 
417 jrf 82
  /// Template for a function that squares the argument.
12 jab 83
  template <class Scalar>
84
  inline Scalar sqr(Scalar x) {///
85
    return x*x;}
2 bj 86
 
417 jrf 87
  /// Scalar template for a function that returns the cube of the argument.
12 jab 88
  template <class Scalar>
89
  inline Scalar qbe(Scalar x) {///
90
    return x*x*x;}
2 bj 91
 
12 jab 92
  template <class Scalar>
93
  inline bool is_zero(Scalar x)	{return (x > -MINUTE && x < MINUTE);}
2 bj 94
 
12 jab 95
  template <class Scalar>
96
  inline bool is_tiny(Scalar x)	{return (x > -TINY && x < TINY);}
2 bj 97
 
12 jab 98
  /** What power of 2 ?. if x is the argument, find the largest 
99
      y so that 2^y <= x */
45 jab 100
  inline int two_to_what_power(unsigned int x) 
12 jab 101
  {
102
    if (x<1) 
103
      return -1;
104
    int i = 0;
105
    while (x != 1) {x>>=1;i++;}
106
    return i;
107
  }
2 bj 108
 
109
#ifdef __sgi
12 jab 110
  inline int round(float x) {return int(rint(x));}
2 bj 111
#else
12 jab 112
  inline int round(float x) {return int(x+0.5);}
2 bj 113
#endif
114
 
12 jab 115
  template<class T>
116
  inline T sign(T x) {return x>=T(0) ? 1 : -1;}
2 bj 117
 
277 jrf 118
  /// Integer power function with O(log(n)) complexity
12 jab 119
  template<class T>
277 jrf 120
  inline T int_pow(T a, unsigned int n)
12 jab 121
  {
277 jrf 122
    T result = static_cast<T>(1);
123
    for(; n > 0; n >>= 1)
124
    {
125
      if(n & 1) result = result*a;
126
      a *= a;
127
    }
128
    return result;
12 jab 129
  }
417 jrf 130
 
131
  /// Function that seeds the GEL pseudo-random number generator
132
  void gel_srand(unsigned int seed);
2 bj 133
 
417 jrf 134
  /** GEL provides a linear congruential pseudo-random number 
135
      generator which is optimized for speed. This version allows 
136
      an integer argument which is useful for grid-based noise
137
      functions. */
138
  unsigned int gel_rand(unsigned int k);
139
 
140
  /** GEL provides a linear congruential pseudo-random number 
141
      generator which is optimized for speed. This means
142
      that GEL_RAND_MAX==UINT_MAX. */
143
  unsigned int gel_rand();
144
 
39 bj 145
	/** raw_assign takes a CGLA vector, matrix or whatever has a get() function
146
			as its first argument and a raw pointer to a (presumed scalar) entity 
147
			as the second argument. the contents dereferenced by the pointer is 
148
			copied to the entity given as first argument. */
12 jab 149
  template<class T, class S>
150
  void raw_assign(T& a,  const S* b)
151
  {
152
    memcpy(static_cast<void*>(a.get()),static_cast<const void*>(b),sizeof(T));
153
  }
2 bj 154
 
155
}
156
 
157
#endif