plane.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /* $Header: /Commando/Code/wwmath/plane.h 16 5/05/01 5:48p Jani_p $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Voxel Technology *
  24. * *
  25. * File Name : PLANE.H *
  26. * *
  27. * Programmer : Greg Hjelstrom *
  28. * *
  29. * Start Date : 03/17/97 *
  30. * *
  31. * Last Update : March 17, 1997 [GH] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #if defined(_MSC_VER)
  37. #pragma once
  38. #endif
  39. #ifndef PLANE_H
  40. #define PLANE_H
  41. #include "always.h"
  42. #include "vector3.h"
  43. #include "sphere.h"
  44. /*
  45. ** PlaneClass
  46. **
  47. ** 3D-planes. This class uses the Normal+Distance description of a plane.
  48. ** The relationship for all points (p) on the plane is given by:
  49. **
  50. ** N.X * p.X + N.Y * p.Y + N.Z * p.Z = D
  51. **
  52. ** BEWARE, if you are used to the Ax + By + Cz + D = 0 description, the
  53. ** sign of the D value is inverted.
  54. */
  55. class PlaneClass
  56. {
  57. public:
  58. enum { FRONT = 0, BACK, ON };
  59. Vector3 N; // Normal of the plane
  60. float D; // Distance along the normal from the origin
  61. PlaneClass(void) : N(0.0f,0.0f,1.0f), D(0.0f) { }
  62. /*
  63. ** Plane initialization:
  64. ** a,b,c,d - explicitly set the four coefficients (note the sign of d!)
  65. ** normal,dist - explicitly set the normal and distance
  66. ** normal,point - compute plane with normal, containing point
  67. ** p1,p2,p3 - compute plane containing three points
  68. */
  69. PlaneClass(float nx,float ny,float nz,float dist);
  70. PlaneClass(const Vector3 & normal,float dist);
  71. PlaneClass(const Vector3 & normal,const Vector3 & point);
  72. PlaneClass(const Vector3 & point1,const Vector3 & point2,const Vector3 & point3);
  73. inline void Set(float a,float b,float c,float d);
  74. inline void Set(const Vector3 & normal,float dist);
  75. inline void Set(const Vector3 & normal,const Vector3 & point);
  76. inline void Set(const Vector3 & point1,const Vector3 & point2,const Vector3 & point3);
  77. bool Compute_Intersection(const Vector3 & p0,const Vector3 & p1,float * set_t) const;
  78. bool In_Front(const Vector3 & point) const;
  79. bool In_Front(const SphereClass & sphere) const;
  80. bool In_Front_Or_Intersecting(const SphereClass & sphere) const;
  81. static void Intersect_Planes(const PlaneClass & a, const PlaneClass & b, Vector3 *line_dir, Vector3 *line_point);
  82. };
  83. inline PlaneClass::PlaneClass(float nx,float ny,float nz,float dist)
  84. {
  85. Set(nx,ny,nz,dist);
  86. }
  87. inline PlaneClass::PlaneClass(const Vector3 & normal,float dist)
  88. {
  89. Set(normal,dist);
  90. }
  91. inline PlaneClass::PlaneClass(const Vector3 & normal,const Vector3 & point)
  92. {
  93. Set(normal,point);
  94. }
  95. inline PlaneClass::PlaneClass(const Vector3 & point1, const Vector3 & point2, const Vector3 & point3)
  96. {
  97. Set(point1,point2,point3);
  98. }
  99. inline void PlaneClass::Set(float a,float b,float c,float d)
  100. {
  101. N.X = a;
  102. N.Y = b;
  103. N.Z = c;
  104. D = d;
  105. }
  106. inline void PlaneClass::Set(const Vector3 & normal,float dist)
  107. {
  108. N = normal;
  109. D = dist;
  110. }
  111. inline void PlaneClass::Set(const Vector3 & normal,const Vector3 & point)
  112. {
  113. N = normal;
  114. D = Vector3::Dot_Product(normal , point);
  115. }
  116. inline void PlaneClass::Set(const Vector3 & point1, const Vector3 & point2, const Vector3 & point3)
  117. {
  118. #ifdef ALLOW_TEMPORARIES
  119. N = Vector3::Cross_Product((point2 - point1), (point3 - point1));
  120. #else
  121. Vector3::Cross_Product((point2 - point1), (point3 - point1), &N);
  122. #endif
  123. if (N != Vector3(0.0f, 0.0f, 0.0f)) {
  124. // Points are not colinear. Normalize N and calculate D.
  125. N.Normalize();
  126. D = Vector3::Dot_Product(N, point1);
  127. } else {
  128. // They are colinear - return default plane (constructors can't fail).
  129. N = Vector3(0.0f, 0.0f, 1.0f);
  130. D = 0.0f;
  131. }
  132. }
  133. inline bool PlaneClass::Compute_Intersection(const Vector3 & p0,const Vector3 & p1,float * set_t) const
  134. {
  135. float num,den;
  136. den = Vector3::Dot_Product(N,p1-p0);
  137. /*
  138. ** If the denominator is zero, the ray is parallel to the plane
  139. */
  140. if (den == 0.0f) {
  141. return false;
  142. }
  143. num = -(Vector3::Dot_Product(N,p0) - D);
  144. *set_t = num/den;
  145. /*
  146. ** If t is not between 0 and 1, the line containing the segment intersects
  147. ** the plane but the segment does not
  148. */
  149. if ((*set_t < 0.0f) || (*set_t > 1.0f)) {
  150. return false;
  151. }
  152. return true;
  153. }
  154. inline bool PlaneClass::In_Front(const Vector3 & point) const
  155. {
  156. float dist = Vector3::Dot_Product(point,N);
  157. return (dist > D);
  158. }
  159. // This function returns true if the sphere is in front of the plane.
  160. inline bool PlaneClass::In_Front(const SphereClass & sphere) const
  161. {
  162. float dist = Vector3::Dot_Product(sphere.Center,N);
  163. return ((dist - D) >= sphere.Radius);
  164. }
  165. // This function will return 1 if any part of the sphere is in front of the plane.
  166. // (i.e. if the sphere is entirely in front of the plane or if it intersects the plane).
  167. inline bool PlaneClass::In_Front_Or_Intersecting(const SphereClass & sphere) const
  168. {
  169. float dist = Vector3::Dot_Product(sphere.Center , N);
  170. return ((D - dist) < sphere.Radius);
  171. }
  172. inline void PlaneClass::Intersect_Planes(const PlaneClass & a, const PlaneClass & b, Vector3 *line_dir, Vector3 *line_point)
  173. {
  174. // Method used is from "plane-to-plane intersection", Graphics Gems III, pp. 233-235.
  175. // Find line of intersection. First find direction vector of line:
  176. Vector3::Cross_Product(a.N, b.N, line_dir);
  177. // Now find point on line. How we do it depends on what the largest coordinate of the
  178. // direction vector is.
  179. Vector3 abs_dir = *line_dir;
  180. abs_dir.Update_Max(-abs_dir);
  181. if (abs_dir.X > abs_dir.Y) {
  182. if (abs_dir.X > abs_dir.Z) {
  183. // X largest
  184. float ool = 1.0f / line_dir->X;
  185. line_point->Y = (b.N.Z * a.D - a.N.Z * b.D) * ool;
  186. line_point->Z = (a.N.Y * b.D - b.N.Y * a.D) * ool;
  187. line_point->X = 0.0f;
  188. } else {
  189. // Z largest
  190. float ool = 1.0f / line_dir->Z;
  191. line_point->X = (b.N.Y * a.D - a.N.Y * b.D) * ool;
  192. line_point->Y = (a.N.X * b.D - b.N.X * a.D) * ool;
  193. line_point->Z = 0.0f;
  194. }
  195. } else {
  196. if (abs_dir.Y > abs_dir.Z) {
  197. // Y largest
  198. float ool = 1.0f / line_dir->Y;
  199. line_point->Z = (b.N.X * a.D - a.N.X * b.D) * ool;
  200. line_point->X = (a.N.Z * b.D - b.N.Z * a.D) * ool;
  201. line_point->Y = 0.0f;
  202. } else {
  203. // Z largest
  204. float ool = 1.0f / line_dir->Z;
  205. line_point->X = (b.N.Y * a.D - a.N.Y * b.D) * ool;
  206. line_point->Y = (a.N.X * b.D - b.N.X * a.D) * ool;
  207. line_point->Z = 0.0f;
  208. }
  209. }
  210. // Normalize direction vector (we do it here because we needed the non-normalized version to
  211. // find the point).
  212. line_dir->Normalize();
  213. }
  214. #endif /*PLANE_H*/