Subversion Repositories gelsvn

Rev

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

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