home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_09 / cordic.hpp < prev    next >
C/C++ Source or Header  |  1993-02-28  |  3KB  |  76 lines

  1. //CORDIC.HPP  a header file for the CORDIC class which provides integer
  2. //                    sine and cosine approximations for graphics processing.
  3. //                    These routines are a C++ adaptation of routines written in C in
  4. //                    "The CORDIC Method for Faster sin and cos Calculations"
  5. //                    by Michael Bertrand, in _The C User's Journal_, Vol. 10,
  6. //                    #11, pp 57 - 64.
  7.  
  8. //                    Principal changes from the C implementation are: making a lot of
  9. //                    global int's into private static members, and establishing
  10. //                    a predefined instance of the cordic object: cord.
  11.  
  12. //                    The work is done by the cordic::sinCos() member functions.
  13. //                    The results are returned as integers between 0 and 16K,
  14. //                    and the long input is in CORDIC Angle units which divide
  15. //                    the circle into 64K parts, instead of degrees or radians.
  16.  
  17. //                    The version which uses an unsigned long as an input is the
  18. //                    fastest, but the inputs must be 0 <= theta <= 64K.
  19. //                    The version which takes a long as an input is the next
  20. //                    fastest:  it moves the input theta into the above range,
  21. //                    then calls the unsigned long version to do the work.  The
  22. //                    float version is the slowest:  it takes a radian input and
  23. //                    converts it to CAU's before calling the second version to
  24. //                    massage the value for the first function to work on.
  25.  
  26. //                    The sine cosine values can be normalized, but the real value
  27. //                    of these approximations is best found by designing routines
  28. //                    to use the results as they are, to improve speed.  For the
  29. //                    same reason, it is better to use the unsigned long value
  30. //                    as input to the routines, to avoid the time overhead
  31. //                    associated with changing from radians to CAU's.
  32.  
  33. #ifndef CORDIC_HPP
  34. #define CORDIC_HPP
  35.  
  36. #include <math.h>
  37.  
  38.  
  39. enum {Quad1, Quad2, Quad3, Quad4, NBits = 14};
  40.  
  41. enum {cordbase = 16384};
  42.  
  43.  
  44. class cordic
  45. {
  46. private:
  47.     static int ArcTan[NBits];      //array of integer arcTan's
  48.     static int xInit;              //length of initial vector
  49.     static int instance;                 //previous instance of CORDIC?
  50.     static long HalfBase;         //an eighth of a circle in CAU's
  51.     static long Quad2Boundary;    //half a circle in CAU's
  52.     static long Quad3Boundary;    //3/4 circle in CAU's
  53.     static long CordicBase;       //a quarter of a circle in CAU's
  54.  
  55. public:
  56.     cordic();
  57.     ~cordic();
  58.  
  59.     long cordicbase(void);            //provides the value of CordicBase to users
  60.  
  61.     //these routines should be used by placing the value of the angle for
  62.     //which the sine and/or cosine is to be evaluated into theta, and
  63.     //looking for the return values in the sin and cos variables.
  64.     //The unsigned long version will accept only positive angle values
  65.     //less than 64K, while the long version accepts any long input and
  66.     //the radian version accepts most reasonable float inputs
  67.     void sinCos(unsigned long theta, int &sin, int &cos); //input in CAU's
  68.     void sinCos(long theta, int &sin, int &cos);
  69.     void sinCos(float theta, int &sin, int &cos);   // input in Radians
  70.  
  71. };// Class CORDIC
  72.  
  73. extern cordic cord;        //predefined instance
  74.  
  75. #endif //CORDIC_HPP
  76.