Subversion Repositories gelsvn

Rev

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

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