Subversion Repositories gelsvn

Rev

Rev 196 | Rev 248 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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