Subversion Repositories gelsvn

Rev

Rev 197 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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