Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 bj 1
#ifndef __CGLA_TABLETRIGONOMETRY_H__
2
#define __CGLA_TABLETRIGONOMETRY_H__
3
 
4
#include <vector>
5
#include "CGLA.h"
6
 
7
namespace CGLA {
8
 
9
	namespace TableTrigonometry
10
	{
11
		typedef unsigned short int Angle;
12
 
13
		const Angle  ANGLE_MAX = USHRT_MAX>>2;
14
		const float ANGLE_FACTOR = float(ANGLE_MAX) / M_PI_2;
15
 
16
		class CosTable
17
		{
18
			std::vector<float> tab;
19
 
20
		public:
21
 
22
			CosTable(): tab(ANGLE_MAX+1)
23
			{
24
				for(int i=0;i<ANGLE_MAX+1; i++)
25
					tab[i] = cos(i/ANGLE_FACTOR);
26
			}
27
 
28
			float operator[](int i) const {return tab[i];}
29
		};
30
 
31
		const CosTable& COS_TABLE();
32
 
33
		inline float angle2float(Angle theta)
34
		{
35
			static float MPI2 = (float)(M_PI * 2);
36
			switch(theta & 3)
37
				{
38
				case 0: return   (theta>>2)/ANGLE_FACTOR;
39
				case 1: return M_PI - (theta>>2)/ANGLE_FACTOR;
40
				case 2: return M_PI + (theta>>2)/ANGLE_FACTOR;
41
				case 3: return MPI2 - (theta>>2)/ANGLE_FACTOR;
42
				}
43
			return 0;	
44
		}
45
 
46
		inline float t_cos(Angle theta)
47
		{
48
			switch(theta & 3)
49
				{
50
				case 0: return   COS_TABLE()[ theta>>2 ];
51
				case 1: return - COS_TABLE()[ theta>>2 ];
52
				case 2: return - COS_TABLE()[ theta>>2 ];
53
				case 3: return   COS_TABLE()[ theta>>2 ];
54
				}
55
			return 0;
56
		}
57
 
58
		inline float t_sin(Angle theta)
59
		{
60
			switch(theta & 3)
61
				{
62
				case 0: return   COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
63
				case 1: return   COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
64
				case 2: return - COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
65
				case 3: return - COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
66
				}
67
			return 0;
68
		}
69
 
70
		inline Angle t_atan(float x, float y)
71
		{
72
			Angle theta = Angle( acos(fabs(x)/sqrt(x*x+y*y)) * ANGLE_FACTOR );
73
			Angle key = (x>=0 ? (y>=0 ? 0 : 3) : (y>=0 ? 1 : 2));
74
			return (theta<<2) | key;
75
		}
76
 
77
	}
78
 
79
 
80
}
81
#endif