Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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