Subversion Repositories gelsvn

Rev

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

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