Subversion Repositories gelsvn

Rev

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

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