Subversion Repositories gelsvn

Rev

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

Rev 248 Rev 277
1
// $Id: CGLA.h 248 2006-06-30 10:54:06Z bj $
1
// $Id: CGLA.h 277 2006-08-29 09:14:45Z 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
		inline double sqrt3()
53
		inline double sqrt3()
54
		{
54
		{
55
				static const double sqrt3_val = sqrt(3.0);
55
				static const double sqrt3_val = sqrt(3.0);
56
				return sqrt3_val;
56
				return sqrt3_val;
57
		}
57
		}
58
 
58
 
59
#define SQRT3 sqrt3()
59
#define SQRT3 sqrt3()
60
 
60
 
61
  /// Useful enum that represents coordiante axes.
61
  /// Useful enum that represents coordiante axes.
62
  enum Axis {XAXIS=0,YAXIS=1,ZAXIS=2};
62
  enum Axis {XAXIS=0,YAXIS=1,ZAXIS=2};
63
 
63
 
64
  inline bool isnan(double x) { return x != x; }
64
  inline bool isnan(double x) { return x != x; }
65
 
65
 
66
  template<class Scalar>
66
  template<class Scalar>
67
  Scalar s_min(Scalar a, Scalar b)
67
  Scalar s_min(Scalar a, Scalar b)
68
  {
68
  {
69
    return a<b ? a : b;
69
    return a<b ? a : b;
70
  }
70
  }
71
 
71
 
72
  template<class Scalar>
72
  template<class Scalar>
73
  Scalar s_max(Scalar a, Scalar b)
73
  Scalar s_max(Scalar a, Scalar b)
74
  {
74
  {
75
    return a>b ? a : b;
75
    return a>b ? a : b;
76
  }
76
  }
77
 
77
 
78
  ///Template for a function that squares the argument.
78
  ///Template for a function that squares the argument.
79
  template <class Scalar>
79
  template <class Scalar>
80
  inline Scalar sqr(Scalar x) {///
80
  inline Scalar sqr(Scalar x) {///
81
    return x*x;}
81
    return x*x;}
82
	
82
	
83
  /// Scalaremplate for a function that returns the cube of the argument.
83
  /// Scalaremplate for a function that returns the cube of the argument.
84
  template <class Scalar>
84
  template <class Scalar>
85
  inline Scalar qbe(Scalar x) {///
85
  inline Scalar qbe(Scalar x) {///
86
    return x*x*x;}
86
    return x*x*x;}
87
 
87
 
88
  template <class Scalar>
88
  template <class Scalar>
89
  inline bool is_zero(Scalar x)	{return (x > -MINUTE && x < MINUTE);}
89
  inline bool is_zero(Scalar x)	{return (x > -MINUTE && x < MINUTE);}
90
 
90
 
91
  template <class Scalar>
91
  template <class Scalar>
92
  inline bool is_tiny(Scalar x)	{return (x > -TINY && x < TINY);}
92
  inline bool is_tiny(Scalar x)	{return (x > -TINY && x < TINY);}
93
 
93
 
94
  /** What power of 2 ?. if x is the argument, find the largest 
94
  /** What power of 2 ?. if x is the argument, find the largest 
95
      y so that 2^y <= x */
95
      y so that 2^y <= x */
96
  inline int two_to_what_power(unsigned int x) 
96
  inline int two_to_what_power(unsigned int x) 
97
  {
97
  {
98
    if (x<1) 
98
    if (x<1) 
99
      return -1;
99
      return -1;
100
    int i = 0;
100
    int i = 0;
101
    while (x != 1) {x>>=1;i++;}
101
    while (x != 1) {x>>=1;i++;}
102
    return i;
102
    return i;
103
  }
103
  }
104
 
104
 
105
#ifdef __sgi
105
#ifdef __sgi
106
  inline int round(float x) {return int(rint(x));}
106
  inline int round(float x) {return int(rint(x));}
107
#else
107
#else
108
  inline int round(float x) {return int(x+0.5);}
108
  inline int round(float x) {return int(x+0.5);}
109
#endif
109
#endif
110
 
110
 
111
  template<class T>
111
  template<class T>
112
  inline T sign(T x) {return x>=T(0) ? 1 : -1;}
112
  inline T sign(T x) {return x>=T(0) ? 1 : -1;}
113
 
113
 
114
 
-
 
-
 
114
  /// Integer power function with O(log(n)) complexity
115
  template<class T>
115
  template<class T>
116
  inline T int_pow(T x, unsigned int k) 
116
  inline T int_pow(T a, unsigned int n)
117
  {
117
  {
118
    T y = static_cast<T>(1);
118
    T result = static_cast<T>(1);
119
    for(unsigned int i=0;i<k;++i)
119
    for(; n > 0; n >>= 1)
-
 
120
    {
-
 
121
      if(n & 1) result = result*a;
120
      y *= x;
122
      a *= a;
-
 
123
    }
121
    return y;
124
    return result;
122
  }
125
  }
123
 
126
 
124
	/** raw_assign takes a CGLA vector, matrix or whatever has a get() function
127
	/** raw_assign takes a CGLA vector, matrix or whatever has a get() function
125
			as its first argument and a raw pointer to a (presumed scalar) entity 
128
			as its first argument and a raw pointer to a (presumed scalar) entity 
126
			as the second argument. the contents dereferenced by the pointer is 
129
			as the second argument. the contents dereferenced by the pointer is 
127
			copied to the entity given as first argument. */
130
			copied to the entity given as first argument. */
128
  template<class T, class S>
131
  template<class T, class S>
129
  void raw_assign(T& a,  const S* b)
132
  void raw_assign(T& a,  const S* b)
130
  {
133
  {
131
    memcpy(static_cast<void*>(a.get()),static_cast<const void*>(b),sizeof(T));
134
    memcpy(static_cast<void*>(a.get()),static_cast<const void*>(b),sizeof(T));
132
  }
135
  }
133
	
136
	
134
}
137
}
135
 
138
 
136
#endif
139
#endif
137
 
140