Subversion Repositories gelsvn

Rev

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

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