Subversion Repositories gelsvn

Rev

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

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