Subversion Repositories gelsvn

Rev

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

Rev 196 Rev 197
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
#ifdef WIN32
-
 
63
  inline bool isnan(double x) { return _isnan(x);}
62
  inline bool isnan(double x) { return x != x; }
64
#else
-
 
65
	inline bool isnan(double x) { return std::isnan(x);}
-
 
66
#endif
-
 
67
 
63
 
68
  template<class Scalar>
64
  template<class Scalar>
69
  Scalar s_min(Scalar a, Scalar b)
65
  Scalar s_min(Scalar a, Scalar b)
70
  {
66
  {
71
    return a<b ? a : b;
67
    return a<b ? a : b;
72
  }
68
  }
73
 
69
 
74
  template<class Scalar>
70
  template<class Scalar>
75
  Scalar s_max(Scalar a, Scalar b)
71
  Scalar s_max(Scalar a, Scalar b)
76
  {
72
  {
77
    return a>b ? a : b;
73
    return a>b ? a : b;
78
  }
74
  }
79
 
75
 
80
  ///Template for a function that squares the argument.
76
  ///Template for a function that squares the argument.
81
  template <class Scalar>
77
  template <class Scalar>
82
  inline Scalar sqr(Scalar x) {///
78
  inline Scalar sqr(Scalar x) {///
83
    return x*x;}
79
    return x*x;}
84
	
80
	
85
  /// Scalaremplate for a function that returns the cube of the argument.
81
  /// Scalaremplate for a function that returns the cube of the argument.
86
  template <class Scalar>
82
  template <class Scalar>
87
  inline Scalar qbe(Scalar x) {///
83
  inline Scalar qbe(Scalar x) {///
88
    return x*x*x;}
84
    return x*x*x;}
89
 
85
 
90
  template <class Scalar>
86
  template <class Scalar>
91
  inline bool is_zero(Scalar x)	{return (x > -MINUTE && x < MINUTE);}
87
  inline bool is_zero(Scalar x)	{return (x > -MINUTE && x < MINUTE);}
92
 
88
 
93
  template <class Scalar>
89
  template <class Scalar>
94
  inline bool is_tiny(Scalar x)	{return (x > -TINY && x < TINY);}
90
  inline bool is_tiny(Scalar x)	{return (x > -TINY && x < TINY);}
95
 
91
 
96
  /** 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 
97
      y so that 2^y <= x */
93
      y so that 2^y <= x */
98
  inline int two_to_what_power(unsigned int x) 
94
  inline int two_to_what_power(unsigned int x) 
99
  {
95
  {
100
    if (x<1) 
96
    if (x<1) 
101
      return -1;
97
      return -1;
102
    int i = 0;
98
    int i = 0;
103
    while (x != 1) {x>>=1;i++;}
99
    while (x != 1) {x>>=1;i++;}
104
    return i;
100
    return i;
105
  }
101
  }
106
 
102
 
107
#ifdef __sgi
103
#ifdef __sgi
108
  inline int round(float x) {return int(rint(x));}
104
  inline int round(float x) {return int(rint(x));}
109
#else
105
#else
110
  inline int round(float x) {return int(x+0.5);}
106
  inline int round(float x) {return int(x+0.5);}
111
#endif
107
#endif
112
 
108
 
113
  template<class T>
109
  template<class T>
114
  inline T sign(T x) {return x>=T(0) ? 1 : -1;}
110
  inline T sign(T x) {return x>=T(0) ? 1 : -1;}
115
 
111
 
116
 
112
 
117
  template<class T>
113
  template<class T>
118
  inline T int_pow(T x, unsigned int k) 
114
  inline T int_pow(T x, unsigned int k) 
119
  {
115
  {
120
    T y = static_cast<T>(1);
116
    T y = static_cast<T>(1);
121
    for(unsigned int i=0;i<k;++i)
117
    for(unsigned int i=0;i<k;++i)
122
      y *= x;
118
      y *= x;
123
    return y;
119
    return y;
124
  }
120
  }
125
 
121
 
126
	/** 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
127
			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 
128
			as the second argument. the contents dereferenced by the pointer is 
124
			as the second argument. the contents dereferenced by the pointer is 
129
			copied to the entity given as first argument. */
125
			copied to the entity given as first argument. */
130
  template<class T, class S>
126
  template<class T, class S>
131
  void raw_assign(T& a,  const S* b)
127
  void raw_assign(T& a,  const S* b)
132
  {
128
  {
133
    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));
134
  }
130
  }
135
	
131
	
136
}
132
}
137
 
133
 
138
#endif
134
#endif
139
 
135