Subversion Repositories gelsvn

Rev

Rev 10 | Rev 21 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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