v3_rnd.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. ** Command & Conquer Renegade(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.h $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 7/09/99 9:49a $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef V3_RND_H
  39. #define V3_RND_H
  40. #include "always.h"
  41. #include "vector3.h"
  42. #include "random.h"
  43. #include <limits.h>
  44. /*
  45. ** Vector3Randomizer is an abstract class for generating random Vector3s.
  46. ** Examples: generating vectors in a sphere, cylinder, etc.
  47. ** This file contains several concrete derived classes; others may be defined
  48. ** in future.
  49. ** A possible future extension to this class would be to add a method to
  50. ** efficiently get an array of random points.
  51. */
  52. class Vector3Randomizer {
  53. public:
  54. enum
  55. {
  56. CLASSID_UNKNOWN = 0xFFFFFFFF,
  57. CLASSID_SOLIDBOX = 0,
  58. CLASSID_SOLIDSPHERE,
  59. CLASSID_HOLLOWSPHERE,
  60. CLASSID_SOLIDCYLINDER,
  61. CLASSID_MAXKNOWN,
  62. CLASSID_LAST = 0x0000FFFF
  63. };
  64. virtual ~Vector3Randomizer(void) { }
  65. // RTTI identifiction
  66. virtual unsigned int Class_ID (void) const = 0;
  67. // Return a random vector
  68. virtual void Get_Vector(Vector3 &vector) = 0;
  69. // Get the maximum component possible for generated vectors
  70. virtual float Get_Maximum_Extent(void) = 0;
  71. // Scale all vectors produced in future
  72. virtual void Scale(float scale) = 0;
  73. // Clone the randomizer
  74. virtual Vector3Randomizer * Clone(void) const = 0;
  75. protected:
  76. // Derived classes should have protected copy CTors so users use the Clone() function
  77. // Utility functions
  78. float Get_Random_Float_Minus1_To_1() { return Randomizer * OOIntMax; }
  79. float Get_Random_Float_0_To_1() { return ((unsigned int)Randomizer) * OOUIntMax; }
  80. static const float OOIntMax;
  81. static const float OOUIntMax;
  82. static Random3Class Randomizer;
  83. private:
  84. // Derived classes should have a private dummy assignment operator to block usage
  85. };
  86. /*
  87. ** Vector3SolidBoxRandomizer is a randomizer for generating points uniformly distributed inside a
  88. ** box which is centered on the origin.
  89. */
  90. class Vector3SolidBoxRandomizer : public Vector3Randomizer {
  91. public:
  92. Vector3SolidBoxRandomizer(const Vector3 & extents);
  93. virtual unsigned int Class_ID (void) const { return CLASSID_SOLIDBOX; }
  94. virtual const Vector3 & Get_Extents (void) const { return Extents; }
  95. virtual void Get_Vector(Vector3 &vector);
  96. virtual float Get_Maximum_Extent(void);
  97. virtual void Scale(float scale);
  98. virtual Vector3Randomizer * Clone(void) const { return new Vector3SolidBoxRandomizer(*this); }
  99. protected:
  100. // Derived classes should have protected copy CTors so users use the Clone() function
  101. Vector3SolidBoxRandomizer(const Vector3SolidBoxRandomizer &src) : Extents(src.Extents) { }
  102. private:
  103. // Derived classes should have a private dummy assignment operator to block usage
  104. Vector3SolidBoxRandomizer & Vector3SolidBoxRandomizer::operator = (const Vector3SolidBoxRandomizer &that) { that; return *this; }
  105. Vector3 Extents;
  106. };
  107. /*
  108. ** Vector3SolidSphereRandomizer is a randomizer for generating points uniformly distributed inside
  109. ** a sphere which is centered on the origin.
  110. */
  111. class Vector3SolidSphereRandomizer : public Vector3Randomizer {
  112. public:
  113. Vector3SolidSphereRandomizer(float radius);
  114. virtual unsigned int Class_ID (void) const { return CLASSID_SOLIDSPHERE; }
  115. virtual float Get_Radius (void) const { return Radius; }
  116. virtual void Get_Vector(Vector3 &vector);
  117. virtual float Get_Maximum_Extent(void);
  118. virtual void Scale(float scale);
  119. virtual Vector3Randomizer * Clone(void) const { return new Vector3SolidSphereRandomizer(*this); }
  120. protected:
  121. // Derived classes should have protected copy CTors so users use the Clone() function
  122. Vector3SolidSphereRandomizer(const Vector3SolidSphereRandomizer &src) : Radius(src.Radius) { }
  123. private:
  124. // Derived classes should have a private dummy assignment operator to block usage
  125. Vector3SolidSphereRandomizer & Vector3SolidSphereRandomizer::operator = (const Vector3SolidSphereRandomizer &that) { that; return *this; }
  126. float Radius;
  127. };
  128. /*
  129. ** Vector3HollowSphereRandomizer is a randomizer for generating points uniformly distributed on the
  130. ** surface of a sphere which is centered on the origin.
  131. */
  132. class Vector3HollowSphereRandomizer : public Vector3Randomizer {
  133. public:
  134. Vector3HollowSphereRandomizer(float radius);
  135. virtual unsigned int Class_ID (void) const { return CLASSID_HOLLOWSPHERE; }
  136. virtual float Get_Radius (void) const { return Radius; }
  137. virtual void Get_Vector(Vector3 &vector);
  138. virtual float Get_Maximum_Extent(void);
  139. virtual void Scale(float scale);
  140. virtual Vector3Randomizer * Clone(void) const { return new Vector3HollowSphereRandomizer(*this); }
  141. protected:
  142. // Derived classes should have protected copy CTors so users use the Clone() function
  143. Vector3HollowSphereRandomizer(const Vector3HollowSphereRandomizer &src) : Radius(src.Radius) { }
  144. private:
  145. // Derived classes should have a private dummy assignment operator to block usage
  146. Vector3HollowSphereRandomizer & Vector3HollowSphereRandomizer::operator = (const Vector3HollowSphereRandomizer &that) { that; return *this; }
  147. float Radius;
  148. };
  149. /*
  150. ** Vector3SolidCylinderRandomizer is a randomizer for generating points uniformly distributed
  151. ** inside a cylinder which is centered on the origin (set extent to 0 for a disk).
  152. */
  153. class Vector3SolidCylinderRandomizer : public Vector3Randomizer {
  154. public:
  155. Vector3SolidCylinderRandomizer(float extent, float radius);
  156. virtual unsigned int Class_ID (void) const { return CLASSID_SOLIDCYLINDER; }
  157. virtual float Get_Radius (void) const { return Radius; }
  158. virtual float Get_Height (void) const { return Extent; }
  159. virtual void Get_Vector(Vector3 &vector);
  160. virtual float Get_Maximum_Extent(void);
  161. virtual void Scale(float scale);
  162. virtual Vector3Randomizer * Clone(void) const { return new Vector3SolidCylinderRandomizer(*this); }
  163. protected:
  164. // Derived classes should have protected copy CTors so users use the Clone() function
  165. Vector3SolidCylinderRandomizer(const Vector3SolidCylinderRandomizer &src) : Extent(src.Extent), Radius(src.Radius) { }
  166. private:
  167. // Derived classes should have a private dummy assignment operator to block usage
  168. Vector3SolidCylinderRandomizer & Vector3SolidCylinderRandomizer::operator = (const Vector3SolidCylinderRandomizer &that) { that; return *this; }
  169. float Extent;
  170. float Radius;
  171. };
  172. #endif