v3_rnd.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : G *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/v3_rnd.cpp $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 7/09/99 9:49a $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "v3_rnd.h"
  36. #include "vector2.h"
  37. const float Vector3Randomizer::OOIntMax = 1.0f / (float)INT_MAX;
  38. const float Vector3Randomizer::OOUIntMax = 1.0f / (float)UINT_MAX;
  39. Random3Class Vector3Randomizer::Randomizer;
  40. Vector3SolidBoxRandomizer::Vector3SolidBoxRandomizer(const Vector3 & extents)
  41. {
  42. Extents.X = MAX(extents.X, 0.0f);
  43. Extents.Y = MAX(extents.Y, 0.0f);
  44. Extents.Z = MAX(extents.Z, 0.0f);
  45. }
  46. void Vector3SolidBoxRandomizer::Get_Vector(Vector3 &vector)
  47. {
  48. vector.X = Get_Random_Float_Minus1_To_1() * Extents.X;
  49. vector.Y = Get_Random_Float_Minus1_To_1() * Extents.Y;
  50. vector.Z = Get_Random_Float_Minus1_To_1() * Extents.Z;
  51. }
  52. float Vector3SolidBoxRandomizer::Get_Maximum_Extent(void)
  53. {
  54. float max = MAX(Extents.X, Extents.Y);
  55. max = MAX(max, Extents.Z);
  56. return max;
  57. }
  58. void Vector3SolidBoxRandomizer::Scale(float scale)
  59. {
  60. scale = MAX(scale, 0.0f);
  61. Extents.X *= scale;
  62. Extents.Y *= scale;
  63. Extents.Z *= scale;
  64. }
  65. Vector3SolidSphereRandomizer::Vector3SolidSphereRandomizer(float radius)
  66. {
  67. Radius = MAX(radius, 0.0f);
  68. }
  69. void Vector3SolidSphereRandomizer::Get_Vector(Vector3 &vector)
  70. {
  71. // Generate vectors in a cube and discard the ones not in a sphere
  72. float rad_squared = Radius * Radius;
  73. for (;;) {
  74. vector.X = Get_Random_Float_Minus1_To_1() * Radius;
  75. vector.Y = Get_Random_Float_Minus1_To_1() * Radius;
  76. vector.Z = Get_Random_Float_Minus1_To_1() * Radius;
  77. if (vector.Length2() <= rad_squared) break;
  78. }
  79. }
  80. float Vector3SolidSphereRandomizer::Get_Maximum_Extent(void)
  81. {
  82. return Radius;
  83. }
  84. void Vector3SolidSphereRandomizer::Scale(float scale)
  85. {
  86. scale = MAX(scale, 0.0f);
  87. Radius *= scale;
  88. }
  89. Vector3HollowSphereRandomizer::Vector3HollowSphereRandomizer(float radius)
  90. {
  91. Radius = MAX(radius, 0.0f);
  92. }
  93. void Vector3HollowSphereRandomizer::Get_Vector(Vector3 &vector)
  94. {
  95. // Generate vectors in a 2x2x2 origin-centered cube, discard the ones not in a unit-radius
  96. // sphere and scale the result to Radius.
  97. float v_l2;
  98. for (;;) {
  99. vector.X = Get_Random_Float_Minus1_To_1();
  100. vector.Y = Get_Random_Float_Minus1_To_1();
  101. vector.Z = Get_Random_Float_Minus1_To_1();
  102. v_l2 = vector.Length2();
  103. if (v_l2 <= 1.0f && v_l2 > 0.0f) break;
  104. }
  105. float scale = Radius * WWMath::Inv_Sqrt(v_l2);
  106. vector.X *= scale;
  107. vector.Y *= scale;
  108. vector.Z *= scale;
  109. }
  110. float Vector3HollowSphereRandomizer::Get_Maximum_Extent(void)
  111. {
  112. return Radius;
  113. }
  114. void Vector3HollowSphereRandomizer::Scale(float scale)
  115. {
  116. scale = MAX(scale, 0.0f);
  117. Radius *= scale;
  118. }
  119. Vector3SolidCylinderRandomizer::Vector3SolidCylinderRandomizer(float extent, float radius)
  120. {
  121. Extent = MAX(extent, 0.0f);
  122. Radius = MAX(radius, 0.0f);
  123. }
  124. void Vector3SolidCylinderRandomizer::Get_Vector(Vector3 &vector)
  125. {
  126. vector.X = Get_Random_Float_Minus1_To_1() * Extent;
  127. // Generate 2D vectors in a square and discard the ones not in a circle
  128. Vector2 vec2;
  129. float rad_squared = Radius * Radius;
  130. for (;;) {
  131. vec2.X = Get_Random_Float_Minus1_To_1() * Radius;
  132. vec2.Y = Get_Random_Float_Minus1_To_1() * Radius;
  133. if (vec2.Length2() <= rad_squared) break;
  134. }
  135. vector.Y = vec2.X;
  136. vector.Z = vec2.Y;
  137. }
  138. float Vector3SolidCylinderRandomizer::Get_Maximum_Extent(void)
  139. {
  140. return MAX(Extent, Radius);
  141. }
  142. void Vector3SolidCylinderRandomizer::Scale(float scale)
  143. {
  144. scale = MAX(scale, 0.0f);
  145. Extent *= scale;
  146. Radius *= scale;
  147. }