QuickMath.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. #include <math.h>
  19. #include "QuickMath.h"
  20. #include "FixedPoint.h"
  21. double SINQ[csNumRotSteps],COSQ[csNumRotSteps];
  22. float fSINQ[csNumRotSteps],fCOSQ[csNumRotSteps];
  23. short ATANQ[60];
  24. short SQRTQ[MAX_FAST_SQRT];
  25. short rspATan(short sDeltaY,short sDeltaX)
  26. {
  27. short sDelX,sDelY,sDeg; // absolute versions
  28. if (sDeltaX < 0) sDelX = -sDeltaX;
  29. else sDelX = sDeltaX;
  30. if (sDeltaY < 0) sDelY = -sDeltaY;
  31. else sDelY = sDeltaY;
  32. if (sDelY <= sDelX)
  33. if (sDelX != 0)
  34. sDeg = ATANQ[
  35. long(0.5 + (rspRadToDeg * sDelY) / sDelX)];
  36. else
  37. sDeg = 90;
  38. else
  39. if (sDelY) sDeg = 90 - ATANQ[
  40. long(0.5 + (rspRadToDeg * sDelX) / sDelY)];
  41. else sDeg = 90;
  42. // Keep in bounds
  43. if (sDeltaX < 0) sDeg = 180 - sDeg;
  44. if (sDeltaY < 0) sDeg = 360 - sDeg;
  45. if (sDeg == 360) sDeg = 0;
  46. return sDeg;
  47. }
  48. short rspATan(double dVal)
  49. {
  50. short sDeg; // absolute versions
  51. double dAbsVal;
  52. if (dVal < 0) dAbsVal = -dVal;
  53. else dAbsVal = dVal;
  54. if (dAbsVal <= 1.0)
  55. sDeg = ATANQ[
  56. long(0.5 + rspRadToDeg * dAbsVal)];
  57. else
  58. sDeg = 90 - ATANQ[
  59. long(0.5 + rspRadToDeg / dAbsVal)];
  60. // Keep in bounds
  61. if (dVal < 0.0) sDeg = 360 - sDeg;
  62. if (sDeg == 360) sDeg = 0;
  63. return sDeg;
  64. }
  65. void InitTrig()
  66. {
  67. short i;
  68. double rad;
  69. const double cdStepsToRad =
  70. rspPI * 2.0 / double(csNumRotSteps);
  71. for (i=0;i<csNumRotSteps;i++)
  72. {
  73. rad = (double)i * cdStepsToRad;
  74. SINQ[i] = (double)sin(rad);
  75. COSQ[i] = (double)cos(rad);
  76. fSINQ[i] = (float) SINQ[i];
  77. fCOSQ[i] = (float) COSQ[i];
  78. }
  79. // Set up Arctan 45 degrees:
  80. for (i=0;i<58;i++)
  81. {
  82. ATANQ[i] = short(0.5 + atan( rspDegToRad * i ) *
  83. rspRadToDeg);
  84. }
  85. long l;
  86. for (l=0; l < MAX_FAST_SQRT; l++)
  87. {
  88. SQRTQ[l] = short(sqrt(double(l)));
  89. }
  90. TRACE("QTRIG initialized!\n");
  91. RQuickTrigFP dummy;
  92. }