sphere.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 : wwmath *
  23. * *
  24. * $Archive:: /Commando/Code/WWMath/sphere.h $*
  25. * *
  26. * Author:: Greg_h *
  27. * *
  28. * $Modtime:: 3/29/01 10:37a $*
  29. * *
  30. * $Revision:: 18 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * SphereClass::SphereClass -- constructor *
  35. * SphereClass::Init -- assign a new center and radius to this sphere *
  36. * SphereClass::Re_Center -- move the center, update radius to enclose old sphere *
  37. * SphereClass::Add_Sphere -- expands 'this' sphere to enclose the given sphere *
  38. * SphereClass::Transform -- transforms this sphere *
  39. * SphereClass::Volume -- returns the volume of this sphere *
  40. * SphereClass::operator+= -- 'Add' a sphere to this one *
  41. * SphereClass::operator *= -- transform this sphere by the given radius *
  42. * Spheres_Intersect -- test whether two spheres intersect *
  43. * Add_Spheres -- Add two spheres together, creating sphere which encloses both *
  44. * operator + -- Add two spheres together, creating a sphere which encloses both *
  45. * Transform Sphere -- transform a sphere *
  46. * Transform_Sphere -- transform a sphere *
  47. * operator * -- Transform a sphere *
  48. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  49. #if defined(_MSC_VER)
  50. #pragma once
  51. #endif
  52. #ifndef SPHERE_H
  53. #define SPHERE_H
  54. #include "always.h"
  55. #include "vector3.h"
  56. #include "matrix3d.h"
  57. /////////////////////////////////////////////////////////////////////
  58. // SphereClass
  59. //
  60. // Center - center of the sphere.
  61. // Radius - radius of the sphere
  62. //
  63. /////////////////////////////////////////////////////////////////////
  64. class SphereClass
  65. {
  66. public:
  67. inline SphereClass(void) { };
  68. inline SphereClass(const Vector3 & center,float radius) { Init(center,radius); }
  69. inline SphereClass(const Vector3 & center,const SphereClass & s0);
  70. inline SphereClass(const Vector3 *Position, const int VertCount);
  71. inline void Init(const Vector3 & pos,float radius);
  72. inline void Re_Center(const Vector3 & center);
  73. inline void Add_Sphere(const SphereClass & s);
  74. inline void Transform(const Matrix3D & tm);
  75. inline float Volume(void) const;
  76. inline SphereClass & operator += (const SphereClass & s);
  77. inline SphereClass & operator *= (const Matrix3D & m);
  78. Vector3 Center;
  79. float Radius;
  80. };
  81. /***********************************************************************************************
  82. * SphereClass::SphereClass -- constructor *
  83. * *
  84. * INPUT: *
  85. * *
  86. * OUTPUT: *
  87. * *
  88. * WARNINGS: *
  89. * *
  90. * HISTORY: *
  91. * 8/12/98 GTH : Created. *
  92. *=============================================================================================*/
  93. inline SphereClass::SphereClass(const Vector3 & center,const SphereClass & s0)
  94. {
  95. float dist = (s0.Center - center).Length();
  96. Center = center;
  97. Radius = s0.Radius + dist;
  98. }
  99. inline SphereClass::SphereClass(const Vector3 *Position,const int VertCount)
  100. {
  101. int i;
  102. double dx,dy,dz;
  103. // bounding sphere
  104. // Using the algorithm described in Graphics Gems I page 301.
  105. // This algorithm supposedly generates a bounding sphere within
  106. // 5% of the optimal one but is much faster and simpler to implement.
  107. Vector3 xmin(Position[0].X,Position[0].Y,Position[0].Z);
  108. Vector3 xmax(Position[0].X,Position[0].Y,Position[0].Z);
  109. Vector3 ymin(Position[0].X,Position[0].Y,Position[0].Z);
  110. Vector3 ymax(Position[0].X,Position[0].Y,Position[0].Z);
  111. Vector3 zmin(Position[0].X,Position[0].Y,Position[0].Z);
  112. Vector3 zmax(Position[0].X,Position[0].Y,Position[0].Z);
  113. // FIRST PASS:
  114. // finding the 6 minima and maxima points
  115. for (i=1; i<VertCount; i++) {
  116. if (Position[i].X < xmin.X) {
  117. xmin.X = Position[i].X; xmin.Y = Position[i].Y; xmin.Z = Position[i].Z;
  118. }
  119. if (Position[i].X > xmax.X) {
  120. xmax.X = Position[i].X; xmax.Y = Position[i].Y; xmax.Z = Position[i].Z;
  121. }
  122. if (Position[i].Y < ymin.Y) {
  123. ymin.X = Position[i].X; ymin.Y = Position[i].Y; ymin.Z = Position[i].Z;
  124. }
  125. if (Position[i].Y > ymax.Y) {
  126. ymax.X = Position[i].X; ymax.Y = Position[i].Y; ymax.Z = Position[i].Z;
  127. }
  128. if (Position[i].Z < zmin.Z) {
  129. zmin.X = Position[i].X; zmin.Y = Position[i].Y; zmin.Z = Position[i].Z;
  130. }
  131. if (Position[i].Z > zmax.Z) {
  132. zmax.X = Position[i].X; zmax.Y = Position[i].Y; zmax.Z = Position[i].Z;
  133. }
  134. }
  135. // xspan = distance between the 2 points xmin and xmax squared.
  136. // same goes for yspan and zspan.
  137. dx = xmax.X - xmin.X;
  138. dy = xmax.Y - xmin.Y;
  139. dz = xmax.Z - xmin.Z;
  140. double xspan = dx*dx + dy*dy + dz*dz;
  141. dx = ymax.X - ymin.X;
  142. dy = ymax.Y - ymin.Y;
  143. dz = ymax.Z - ymin.Z;
  144. double yspan = dx*dx + dy*dy + dz*dz;
  145. dx = zmax.X - zmin.X;
  146. dy = zmax.Y - zmin.Y;
  147. dz = zmax.Z - zmin.Z;
  148. double zspan = dx*dx + dy*dy + dz*dz;
  149. // Set points dia1 and dia2 to the maximally separated pair
  150. // This will be the diameter of the initial sphere
  151. Vector3 dia1 = xmin;
  152. Vector3 dia2 = xmax;
  153. double maxspan = xspan;
  154. if (yspan > maxspan) {
  155. maxspan = yspan;
  156. dia1 = ymin;
  157. dia2 = ymax;
  158. }
  159. if (zspan > maxspan) {
  160. maxspan = zspan;
  161. dia1 = zmin;
  162. dia2 = zmax;
  163. }
  164. // Compute initial center and radius and radius squared
  165. Vector3 center;
  166. center.X = (dia1.X + dia2.X) / 2.0f;
  167. center.Y = (dia1.Y + dia2.Y) / 2.0f;
  168. center.Z = (dia1.Z + dia2.Z) / 2.0f;
  169. dx = dia2.X - center.X;
  170. dy = dia2.Y - center.Y;
  171. dz = dia2.Z - center.Z;
  172. double radsqr = dx*dx + dy*dy + dz*dz;
  173. double radius = sqrt(radsqr);
  174. // SECOND PASS:
  175. // Increment current sphere if any points fall outside of it.
  176. for (i=0; i<VertCount; i++) {
  177. dx = Position[i].X - center.X;
  178. dy = Position[i].Y - center.Y;
  179. dz = Position[i].Z - center.Z;
  180. double testrad2 = dx*dx + dy*dy + dz*dz;
  181. if (testrad2 > radsqr) {
  182. // this point was outside the old sphere, compute a new
  183. // center point and radius which contains this point
  184. double testrad = sqrt(testrad2);
  185. // adjust center and radius
  186. radius = (radius + testrad) / 2.0;
  187. radsqr = radius * radius;
  188. double oldtonew = testrad - radius;
  189. center.X = (radius * center.X + oldtonew * Position[i].X) / testrad;
  190. center.Y = (radius * center.Y + oldtonew * Position[i].Y) / testrad;
  191. center.Z = (radius * center.Z + oldtonew * Position[i].Z) / testrad;
  192. }
  193. }
  194. Center = center;
  195. Radius = radius;
  196. }
  197. /***********************************************************************************************
  198. * SphereClass::Init -- assign a new center and radius to this sphere *
  199. * *
  200. * INPUT: *
  201. * *
  202. * OUTPUT: *
  203. * *
  204. * WARNINGS: *
  205. * *
  206. * HISTORY: *
  207. * 8/12/98 GTH : Created. *
  208. *=============================================================================================*/
  209. inline void SphereClass::Init(const Vector3 & pos,float radius)
  210. {
  211. Center = pos;
  212. Radius = radius;
  213. }
  214. /***********************************************************************************************
  215. * SphereClass::Re_Center -- move the center, update radius to enclose old sphere *
  216. * *
  217. * INPUT: *
  218. * *
  219. * OUTPUT: *
  220. * *
  221. * WARNINGS: *
  222. * *
  223. * HISTORY: *
  224. * 8/12/98 GTH : Created. *
  225. *=============================================================================================*/
  226. inline void SphereClass::Re_Center(const Vector3 & center)
  227. {
  228. float dist = (Center - center).Length();
  229. Center = center;
  230. Radius += dist;
  231. }
  232. /***********************************************************************************************
  233. * SphereClass::Add_Sphere -- expands 'this' sphere to enclose the given sphere *
  234. * *
  235. * INPUT: *
  236. * *
  237. * OUTPUT: *
  238. * *
  239. * WARNINGS: *
  240. * *
  241. * HISTORY: *
  242. * 8/12/98 GTH : Created. *
  243. *=============================================================================================*/
  244. inline void SphereClass::Add_Sphere(const SphereClass & s)
  245. {
  246. if (s.Radius == 0.0f) return;
  247. float dist = (s.Center - Center).Length();
  248. if (dist == 0.0f) {
  249. Radius = (Radius > s.Radius) ? Radius : s.Radius;
  250. return;
  251. }
  252. float rnew = (dist + Radius + s.Radius) / 2.0f;
  253. // If rnew is smaller than either of the two sphere radii (it can't be
  254. // smaller than both of them), this means that the smaller sphere is
  255. // completely inside the larger, and the result of adding the two is
  256. // simply the larger sphere. If rnew isn't less than either of them, it is
  257. // the new radius - calculate the new center.
  258. if (rnew < Radius) {
  259. // The existing sphere is the result - do nothing.
  260. } else {
  261. if (rnew < s.Radius) {
  262. // The new sphere is the result:
  263. Init(s.Center, s.Radius);
  264. } else {
  265. // Neither sphere is completely inside the other, so rnew is the new
  266. // radius - calculate the new center
  267. float lerp = (rnew - Radius) / dist;
  268. Vector3 center = (s.Center - Center) * lerp + Center;
  269. Init(center, rnew);
  270. }
  271. }
  272. }
  273. /***********************************************************************************************
  274. * SphereClass::Transform -- transforms this sphere *
  275. * *
  276. * INPUT: *
  277. * *
  278. * OUTPUT: *
  279. * *
  280. * WARNINGS: *
  281. * *
  282. * HISTORY: *
  283. * 3/12/99 GTH : Created. *
  284. *=============================================================================================*/
  285. inline void SphereClass::Transform(const Matrix3D & tm)
  286. {
  287. // warning, assumes Orthogonal matrix
  288. Center = tm * Center;
  289. }
  290. /***********************************************************************************************
  291. * SphereClass::Volume -- returns the volume of this sphere *
  292. * *
  293. * INPUT: *
  294. * *
  295. * OUTPUT: *
  296. * *
  297. * WARNINGS: *
  298. * *
  299. * HISTORY: *
  300. * 3/22/99 GTH : Created. *
  301. *=============================================================================================*/
  302. inline float SphereClass::Volume(void) const
  303. {
  304. return (4.0 / 3.0) * WWMATH_PI * (Radius * Radius * Radius);
  305. }
  306. /***********************************************************************************************
  307. * SphereClass::operator+= -- 'Add' a sphere to this one *
  308. * *
  309. * Expands 'this' sphere to also enclose the given sphere *
  310. * *
  311. * INPUT: *
  312. * *
  313. * OUTPUT: *
  314. * *
  315. * WARNINGS: *
  316. * *
  317. * HISTORY: *
  318. * 8/12/98 GTH : Created. *
  319. *=============================================================================================*/
  320. inline SphereClass & SphereClass::operator += (const SphereClass & s)
  321. {
  322. Add_Sphere(s);
  323. return *this;
  324. }
  325. /***********************************************************************************************
  326. * SphereClass::operator *= -- transform this sphere by the given radius *
  327. * *
  328. * INPUT: *
  329. * *
  330. * OUTPUT: *
  331. * *
  332. * WARNINGS: *
  333. * *
  334. * HISTORY: *
  335. * 8/12/98 GTH : Created. *
  336. *=============================================================================================*/
  337. inline SphereClass & SphereClass::operator *= (const Matrix3D & m)
  338. {
  339. Init(m * Center, Radius);
  340. return *this;
  341. }
  342. /***********************************************************************************************
  343. * Spheres_Intersect -- test whether two spheres intersect *
  344. * *
  345. * INPUT: *
  346. * *
  347. * OUTPUT: *
  348. * *
  349. * WARNINGS: *
  350. * *
  351. * HISTORY: *
  352. * 8/12/98 GTH : Created. *
  353. *=============================================================================================*/
  354. inline bool Spheres_Intersect(const SphereClass & s0,const SphereClass & s1)
  355. {
  356. Vector3 delta = s0.Center - s1.Center;
  357. float dist2 = delta*delta;
  358. if (dist2 < (s0.Radius + s1.Radius) * (s0.Radius + s1.Radius)) {
  359. return true;
  360. } else {
  361. return false;
  362. }
  363. }
  364. /***********************************************************************************************
  365. * Add_Spheres -- Add two spheres together, creating sphere which encloses both *
  366. * *
  367. * INPUT: *
  368. * *
  369. * OUTPUT: *
  370. * *
  371. * WARNINGS: *
  372. * *
  373. * HISTORY: *
  374. * 8/12/98 GTH : Created. *
  375. *=============================================================================================*/
  376. inline SphereClass Add_Spheres(const SphereClass & s0, const SphereClass & s1)
  377. {
  378. if (s0.Radius == 0.0f) {
  379. return s1;
  380. } else {
  381. SphereClass result(s0);
  382. result.Add_Sphere(s1);
  383. return result;
  384. }
  385. }
  386. /***********************************************************************************************
  387. * operator + -- Add two spheres together, creating a sphere which encloses both *
  388. * *
  389. * INPUT: *
  390. * *
  391. * OUTPUT: *
  392. * *
  393. * WARNINGS: *
  394. * *
  395. * HISTORY: *
  396. * 8/12/98 GTH : Created. *
  397. *=============================================================================================*/
  398. inline SphereClass operator + (const SphereClass & s0,const SphereClass & s1)
  399. {
  400. return Add_Spheres(s0,s1);
  401. }
  402. /***********************************************************************************************
  403. * Transform Sphere -- transform a sphere *
  404. * *
  405. * INPUT: *
  406. * *
  407. * OUTPUT: *
  408. * *
  409. * WARNINGS: *
  410. * *
  411. * HISTORY: *
  412. * 8/12/98 GTH : Created. *
  413. *=============================================================================================*/
  414. inline SphereClass Transform_Sphere(const Matrix3D & m, const SphereClass & s)
  415. {
  416. // Warning, assumes Orthogonal matrix
  417. return SphereClass(m*s.Center,s.Radius);
  418. }
  419. /***********************************************************************************************
  420. * Transform_Sphere -- transform a sphere *
  421. * *
  422. * INPUT: *
  423. * *
  424. * OUTPUT: *
  425. * *
  426. * WARNINGS: *
  427. * *
  428. * HISTORY: *
  429. * 8/12/98 GTH : Created. *
  430. *=============================================================================================*/
  431. inline void Transform_Sphere(const Matrix3D & m, const SphereClass & s,SphereClass & res)
  432. {
  433. // warning, assumes Orthogonal matrix
  434. res.Center = m*s.Center;
  435. res.Radius = s.Radius;
  436. }
  437. /***********************************************************************************************
  438. * operator * -- Transform a sphere *
  439. * *
  440. * INPUT: *
  441. * *
  442. * OUTPUT: *
  443. * *
  444. * WARNINGS: *
  445. * *
  446. * HISTORY: *
  447. * 8/12/98 GTH : Created. *
  448. *=============================================================================================*/
  449. inline SphereClass operator * (const Matrix3D & m, const SphereClass & s)
  450. {
  451. return Transform_Sphere(m,s);
  452. }
  453. #endif