mathfunc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #ifndef MATHFUNC_H
  5. #define MATHFUNC_H
  6. #ifndef STUFF_STUFF_HPP
  7. #include <stuff/stuff.hpp>
  8. #endif
  9. // DESCRIBES A COORDINATE SYSTEM, OR FRAME OR REFERENCE
  10. class frameOfRef
  11. {
  12. public:
  13. Stuff::Vector3D i;
  14. Stuff::Vector3D j;
  15. Stuff::Vector3D k;
  16. frameOfRef(void);
  17. frameOfRef (frameOfRef &copy)
  18. {
  19. *this = copy;
  20. }
  21. frameOfRef & set_i (Stuff::Vector3D &new_i)
  22. {
  23. i = new_i;
  24. return *this;
  25. }
  26. frameOfRef & set_j (Stuff::Vector3D &new_j)
  27. {
  28. j = new_j;
  29. return *this;
  30. }
  31. frameOfRef & set_k (Stuff::Vector3D &new_k)
  32. {
  33. k = new_k;
  34. return *this;
  35. }
  36. frameOfRef & operator =(frameOfRef &fr);
  37. frameOfRef & orthonormalize ()
  38. {
  39. return orthonormalize_on_yaxis();
  40. }
  41. frameOfRef & orthonormalize_on_xaxis ();
  42. frameOfRef & orthonormalize_on_yaxis ();
  43. frameOfRef & orthonormalize_on_zaxis ();
  44. frameOfRef & reset_to_world_frame ();
  45. frameOfRef & rotate_about_i (float &angle);
  46. frameOfRef & rotate_about_j (float &angle);
  47. frameOfRef & rotate_about_k (float &angle);
  48. frameOfRef & rotate_about_i_orthonormal (float &angle);
  49. frameOfRef & rotate_about_j_orthonormal (float &angle);
  50. frameOfRef & rotate_about_k_orthonormal (float &angle);
  51. frameOfRef & rotate(float &i_angle,
  52. float &j_angle,
  53. float &k_angle);
  54. frameOfRef & rotate(Stuff::Vector3D &rotation_vector);
  55. frameOfRef & trans_to_frame (frameOfRef &new_frame);
  56. frameOfRef & trans_from_frame (frameOfRef &old_frame);
  57. void trans_to_frame (Stuff::Vector3D &vector)
  58. {
  59. float vx = vector.x;
  60. float vy = vector.y;
  61. float vz = vector.z;
  62. vector.x = i.x*vx + i.y*vy + i.z*vz;
  63. vector.y = j.x*vx + j.y*vy + j.z*vz;
  64. vector.z = k.x*vx + k.y*vy + k.z*vz;
  65. }
  66. };
  67. void Rotate (Stuff::Vector2DOf<float> &vec, float angle);
  68. void Rotate (Stuff::Vector3D &vec, float angle);
  69. void RotateLight (Stuff::Vector3D &vec, float angle);
  70. void OppRotate (Stuff::Vector3D &vec, float angle);
  71. float distance_from (Stuff::Vector3D &v1, Stuff::Vector3D &v2);
  72. float my_acos (float val);
  73. float angle_from (Stuff::Vector2DOf<float>&v1, Stuff::Vector2DOf<float> &v2);
  74. float angle_from (Stuff::Vector3D &v1, Stuff::Vector3D &v2);
  75. float world_angle_between (Stuff::Vector3D &v1, Stuff::Vector3D &v2);
  76. Stuff::Vector3D relativePositionToPoint (Stuff::Vector3D point, float angle, float distance, unsigned long flags);
  77. long RandomNumber (long range);
  78. long SignedRandomNumber (long range);
  79. bool RollDice (long percent);
  80. _inline long float2long (float val)
  81. {
  82. //_ftol TRUNCS not rounds. Processor wants to round. Surely there is some flag to not have this happen?
  83. // There is but BOY is it slow. We will try Andy's Magical formula instead.
  84. // Doesn't work either. Major bug in Intel's FPU.
  85. // Will simply call long here now to insure working ok and address later.
  86. long result = long(val);
  87. return result;
  88. #if 0
  89. float point5 = 0.49f;
  90. if (val < 0.0f)
  91. {
  92. __asm
  93. {
  94. fld val
  95. fadd point5
  96. fistp result
  97. }
  98. }
  99. else
  100. {
  101. __asm
  102. {
  103. fld val
  104. fsub point5
  105. fistp result
  106. }
  107. }
  108. return result;
  109. if (val > 0.0f)
  110. val -= 0.49999999f;
  111. else
  112. val += 0.49999999f;
  113. val += 3 << 22;
  114. return ((*(long*)&val)&0x007fffff) - 0x00400000;
  115. #endif
  116. }
  117. //---------------------------------------------------------------------------
  118. _inline float mc2_atan2 (float f1, float f2)
  119. {
  120. //Return atan of f1/f2;
  121. float result = 1.570796f;
  122. //f2 is always assumed positive here!!!
  123. if (f2 > Stuff::SMALL)
  124. {
  125. __asm
  126. {
  127. fld f1
  128. fld f2
  129. fpatan
  130. fabs
  131. fstp result
  132. }
  133. }
  134. return result;
  135. }
  136. //---------------------------------------------------------------------------
  137. _inline float fmax (float f1, float f2)
  138. {
  139. if (f1 > f2)
  140. return f1;
  141. else
  142. return f2;
  143. }
  144. //---------------------------------------------------------------------------
  145. _inline float fmin (float f1, float f2)
  146. {
  147. if (f1 < f2)
  148. return f1;
  149. else
  150. return f2;
  151. }
  152. //---------------------------------------------------------------------------
  153. _inline float sign (float f1)
  154. {
  155. if (f1 < 0.0f)
  156. return -1.0f;
  157. return 0.0f;
  158. }
  159. #endif