GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/CGLA/TableTrigonometry.h
00001 #ifndef __CGLA_TABLETRIGONOMETRY_H__
00002 #define __CGLA_TABLETRIGONOMETRY_H__
00003 
00004 #include <vector>
00005 #include "CGLA.h"
00006 
00007 namespace CGLA {
00008 
00009         namespace TableTrigonometry
00010         {
00011                 typedef unsigned short int Angle;
00012 
00013                 const Angle  ANGLE_MAX = USHRT_MAX>>2;
00014                 const float ANGLE_FACTOR = float(ANGLE_MAX) / M_PI_2;
00015         
00016                 class CosTable
00017                 {
00018                         std::vector<float> tab;
00019 
00020                 public:
00021                 
00022                         CosTable(): tab(ANGLE_MAX+1)
00023                         {
00024                                 for(int i=0;i<ANGLE_MAX+1; i++)
00025                                         tab[i] = cos(i/ANGLE_FACTOR);
00026                         }
00027                 
00028                         float operator[](int i) const {return tab[i];}
00029                 };
00030 
00031                 const CosTable& COS_TABLE();
00032 
00033                 inline float angle2float(Angle theta)
00034                 {
00035                         static float MPI2 = (float)(M_PI * 2);
00036                         switch(theta & 3)
00037                                 {
00038                                 case 0: return   (theta>>2)/ANGLE_FACTOR;
00039                                 case 1: return M_PI - (theta>>2)/ANGLE_FACTOR;
00040                                 case 2: return M_PI + (theta>>2)/ANGLE_FACTOR;
00041                                 case 3: return MPI2 - (theta>>2)/ANGLE_FACTOR;
00042                                 }
00043                         return 0;       
00044                 }
00045 
00046                 inline float t_cos(Angle theta)
00047                 {
00048                         switch(theta & 3)
00049                                 {
00050                                 case 0: return   COS_TABLE()[ theta>>2 ];
00051                                 case 1: return - COS_TABLE()[ theta>>2 ];
00052                                 case 2: return - COS_TABLE()[ theta>>2 ];
00053                                 case 3: return   COS_TABLE()[ theta>>2 ];
00054                                 }
00055                         return 0;
00056                 }
00057 
00058                 inline float t_sin(Angle theta)
00059                 {
00060                         switch(theta & 3)
00061                                 {
00062                                 case 0: return   COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
00063                                 case 1: return   COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
00064                                 case 2: return - COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
00065                                 case 3: return - COS_TABLE()[ ANGLE_MAX - (theta>>2) ];
00066                                 }
00067                         return 0;
00068                 }
00069 
00070                 inline Angle t_atan(float x, float y)
00071                 {
00072                         Angle theta = Angle( acos(fabs(x)/sqrt(x*x+y*y)) * ANGLE_FACTOR );
00073                         Angle key = (x>=0 ? (y>=0 ? 0 : 3) : (y>=0 ? 1 : 2));
00074                         return (theta<<2) | key;
00075                 }
00076 
00077         }
00078 
00079 
00080 }
00081 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations