Subversion Repositories gelsvn

Rev

Rev 277 | Rev 595 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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