Subversion Repositories gelsvn

Rev

Rev 195 | Rev 197 | 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
 
55 jab 62
#ifdef WIN32
196 jrf 63
  inline bool isnan(double x) { return _isnan(x);}
55 jab 64
#else
178 bj 65
	inline bool isnan(double x) { return std::isnan(x);}
55 jab 66
#endif
67
 
12 jab 68
  template<class Scalar>
69
  Scalar s_min(Scalar a, Scalar b)
70
  {
71
    return a<b ? a : b;
72
  }
2 bj 73
 
12 jab 74
  template<class Scalar>
75
  Scalar s_max(Scalar a, Scalar b)
76
  {
77
    return a>b ? a : b;
78
  }
2 bj 79
 
12 jab 80
  ///Template for a function that squares the argument.
81
  template <class Scalar>
82
  inline Scalar sqr(Scalar x) {///
83
    return x*x;}
2 bj 84
 
12 jab 85
  /// Scalaremplate for a function that returns the cube of the argument.
86
  template <class Scalar>
87
  inline Scalar qbe(Scalar x) {///
88
    return x*x*x;}
2 bj 89
 
12 jab 90
  template <class Scalar>
91
  inline bool is_zero(Scalar x)	{return (x > -MINUTE && x < MINUTE);}
2 bj 92
 
12 jab 93
  template <class Scalar>
94
  inline bool is_tiny(Scalar x)	{return (x > -TINY && x < TINY);}
2 bj 95
 
12 jab 96
  /** What power of 2 ?. if x is the argument, find the largest 
97
      y so that 2^y <= x */
45 jab 98
  inline int two_to_what_power(unsigned int x) 
12 jab 99
  {
100
    if (x<1) 
101
      return -1;
102
    int i = 0;
103
    while (x != 1) {x>>=1;i++;}
104
    return i;
105
  }
2 bj 106
 
107
#ifdef __sgi
12 jab 108
  inline int round(float x) {return int(rint(x));}
2 bj 109
#else
12 jab 110
  inline int round(float x) {return int(x+0.5);}
2 bj 111
#endif
112
 
12 jab 113
  template<class T>
114
  inline T sign(T x) {return x>=T(0) ? 1 : -1;}
2 bj 115
 
116
 
12 jab 117
  template<class T>
45 jab 118
  inline T int_pow(T x, unsigned int k) 
12 jab 119
  {
120
    T y = static_cast<T>(1);
45 jab 121
    for(unsigned int i=0;i<k;++i)
12 jab 122
      y *= x;
123
    return y;
124
  }
2 bj 125
 
39 bj 126
	/** raw_assign takes a CGLA vector, matrix or whatever has a get() function
127
			as its first argument and a raw pointer to a (presumed scalar) entity 
128
			as the second argument. the contents dereferenced by the pointer is 
129
			copied to the entity given as first argument. */
12 jab 130
  template<class T, class S>
131
  void raw_assign(T& a,  const S* b)
132
  {
133
    memcpy(static_cast<void*>(a.get()),static_cast<const void*>(b),sizeof(T));
134
  }
2 bj 135
 
136
}
137
 
138
#endif