Subversion Repositories gelsvn

Rev

Rev 2 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 595
-
 
1
/* ----------------------------------------------------------------------- *
-
 
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
-
 
3
 * Copyright (C) the authors and DTU Informatics
-
 
4
 * For license and list of authors, see ../../doc/intro.pdf
-
 
5
 * ----------------------------------------------------------------------- */
-
 
6
 
-
 
7
/** @file TableTrigonometry.h
-
 
8
 * @brief Tabulated trigonometry - not certain this is useful.
-
 
9
 */
-
 
10
 
1
#ifndef __CGLA_TABLETRIGONOMETRY_H__
11
#ifndef __CGLA_TABLETRIGONOMETRY_H__
2
#define __CGLA_TABLETRIGONOMETRY_H__
12
#define __CGLA_TABLETRIGONOMETRY_H__
3
 
13
 
4
#include <vector>
14
#include <vector>
5
#include "CGLA.h"
15
#include "CGLA.h"
6
 
16
 
7
namespace CGLA {
17
namespace CGLA {
8
 
18
 
9
	namespace TableTrigonometry
19
	namespace TableTrigonometry
10
	{
20
	{
11
		typedef unsigned short int Angle;
21
		typedef unsigned short int Angle;
12
 
22
 
13
		const Angle  ANGLE_MAX = USHRT_MAX>>2;
23
		const Angle  ANGLE_MAX = USHRT_MAX>>2;
14
		const float ANGLE_FACTOR = float(ANGLE_MAX) / M_PI_2;
24
		const float ANGLE_FACTOR = float(ANGLE_MAX) / M_PI_2;
15
	
25
	
16
		class CosTable
26
		class CosTable
17
		{
27
		{
18
			std::vector<float> tab;
28
			std::vector<float> tab;
19
 
29
 
20
		public:
30
		public:
21
		
31
		
22
			CosTable(): tab(ANGLE_MAX+1)
32
			CosTable(): tab(ANGLE_MAX+1)
23
			{
33
			{
24
				for(int i=0;i<ANGLE_MAX+1; i++)
34
				for(int i=0;i<ANGLE_MAX+1; i++)
25
					tab[i] = cos(i/ANGLE_FACTOR);
35
					tab[i] = cos(i/ANGLE_FACTOR);
26
			}
36
			}
27
		
37
		
28
			float operator[](int i) const {return tab[i];}
38
			float operator[](int i) const {return tab[i];}
29
		};
39
		};
30
 
40
 
31
		const CosTable& COS_TABLE();
41
		const CosTable& COS_TABLE();
32
 
42
 
33
		inline float angle2float(Angle theta)
43
		inline float angle2float(Angle theta)
34
		{
44
		{
35
			static float MPI2 = (float)(M_PI * 2);
45
			static float MPI2 = (float)(M_PI * 2);
36
			switch(theta & 3)
46
			switch(theta & 3)
37
				{
47
				{
38
				case 0: return   (theta>>2)/ANGLE_FACTOR;
48
				case 0: return   (theta>>2)/ANGLE_FACTOR;
39
				case 1: return M_PI - (theta>>2)/ANGLE_FACTOR;
49
				case 1: return M_PI - (theta>>2)/ANGLE_FACTOR;
40
				case 2: return M_PI + (theta>>2)/ANGLE_FACTOR;
50
				case 2: return M_PI + (theta>>2)/ANGLE_FACTOR;
41
				case 3: return MPI2 - (theta>>2)/ANGLE_FACTOR;
51
				case 3: return MPI2 - (theta>>2)/ANGLE_FACTOR;
42
				}
52
				}
43
			return 0;	
53
			return 0;	
44
		}
54
		}
45
 
55
 
46
		inline float t_cos(Angle theta)
56
		inline float t_cos(Angle theta)
47
		{
57
		{
48
			switch(theta & 3)
58
			switch(theta & 3)
49
				{
59
				{
50
				case 0: return   COS_TABLE()[ theta>>2 ];
60
				case 0: return   COS_TABLE()[ theta>>2 ];
51
				case 1: return - COS_TABLE()[ theta>>2 ];
61
				case 1: return - COS_TABLE()[ theta>>2 ];
52
				case 2: return - COS_TABLE()[ theta>>2 ];
62
				case 2: return - COS_TABLE()[ theta>>2 ];
53
				case 3: return   COS_TABLE()[ theta>>2 ];
63
				case 3: return   COS_TABLE()[ theta>>2 ];
54
				}
64
				}
55
			return 0;
65
			return 0;
56
		}
66
		}
57
 
67
 
58
		inline float t_sin(Angle theta)
68
		inline float t_sin(Angle theta)
59
		{
69
		{
60
			switch(theta & 3)
70
			switch(theta & 3)
61
				{
71
				{
62
				case 0: return   COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
72
				case 0: return   COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
63
				case 1: return   COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
73
				case 1: return   COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
64
				case 2: return - COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
74
				case 2: return - COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
65
				case 3: return - COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
75
				case 3: return - COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
66
				}
76
				}
67
			return 0;
77
			return 0;
68
		}
78
		}
69
 
79
 
70
		inline Angle t_atan(float x, float y)
80
		inline Angle t_atan(float x, float y)
71
		{
81
		{
72
			Angle theta = Angle( acos(fabs(x)/sqrt(x*x+y*y)) * ANGLE_FACTOR );
82
			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));
83
			Angle key = (x>=0 ? (y>=0 ? 0 : 3) : (y>=0 ? 1 : 2));
74
			return (theta<<2) | key;
84
			return (theta<<2) | key;
75
		}
85
		}
76
 
86
 
77
	}
87
	}
78
 
88
 
79
 
89
 
80
}
90
}
81
#endif
91
#endif
82
 
92