Subversion Repositories gelsvn

Rev

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

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