Subversion Repositories gelsvn

Rev

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

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