Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.8 KiB

  1. // File: VecMath.cpp
  2. // Author: Michael Marr (mikemarr)
  3. //
  4. // History:
  5. // -@- 8/1/95 (mikemarr) added FloatEquals
  6. #include "StdAfx.h"
  7. #include "VecMath.h"
  8. // Function: FloatEquals
  9. // Peform a "fuzzy" compare of two floating point numbers. This relies
  10. // on the IEEE bit representation of floating point numbers.
  11. int
  12. FloatEquals(float x1, float x2)
  13. {
  14. #define EXPMASK 0x7f800000
  15. #define BITSOFPRECISION 19
  16. #define MANTBITS 23
  17. #define EXPOFFSET (BITSOFPRECISION<<MANTBITS)
  18. #define ZEROEPS 3.8e-6F
  19. #define TINYEPS 1.E-35F
  20. if (x1 == x2) return 1; // quick out on exact match
  21. float flEps;
  22. if ((x1 == 0.0f) || (x2 == 0.0f)) {
  23. flEps = ZEROEPS;
  24. } else {
  25. float maxX;
  26. if (x1 > x2)
  27. maxX = x1;
  28. else
  29. maxX = x2;
  30. // grab the exponent of the larger number
  31. unsigned int uExp = (*((unsigned int *) &maxX) & EXPMASK);
  32. if (uExp < EXPOFFSET)
  33. flEps = TINYEPS;
  34. else {
  35. uExp -= EXPOFFSET;
  36. flEps = *((float *) &uExp);
  37. }
  38. }
  39. return (((x1 + flEps) >= x2) && ((x1 - flEps) <= x2));
  40. }
  41. #include <math.h>
  42. float
  43. Vector2::Norm() const
  44. {
  45. return (float) sqrt(NormSquared());
  46. }
  47. float
  48. CoVector2::Norm() const
  49. {
  50. return (float) sqrt(NormSquared());
  51. }
  52. float
  53. Vector3::Norm() const
  54. {
  55. return (float) sqrt(NormSquared());
  56. }
  57. float
  58. CoVector3::Norm() const
  59. {
  60. return (float) sqrt(NormSquared());
  61. }
  62. // Function: Rotate
  63. // rotate the vector counterclockwise around the given axis by theta radians
  64. // Preconditions:
  65. // axis must be UNIT LENGTH
  66. void Vector3::Rotate(const Vector3 &vAxis, float fTheta)
  67. {
  68. float fCosTheta = float(cos(fTheta)), fSinTheta = float(sin(fTheta));
  69. *this *= fCosTheta;
  70. *this += (vAxis * (Dot(*this, vAxis) * (1.f - fCosTheta)));
  71. *this += (Cross(*this, vAxis) * fSinTheta);
  72. }