Subversion Repositories gelsvn

Rev

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

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