Plane.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __MATH_PLANE_H__
  21. #define __MATH_PLANE_H__
  22. /*
  23. ===============================================================================
  24. 3D plane with equation: a * x + b * y + c * z + d = 0
  25. ===============================================================================
  26. */
  27. class idVec3;
  28. class idMat3;
  29. #define ON_EPSILON 0.1f
  30. #define DEGENERATE_DIST_EPSILON 1e-4f
  31. #define SIDE_FRONT 0
  32. #define SIDE_BACK 1
  33. #define SIDE_ON 2
  34. #define SIDE_CROSS 3
  35. // plane sides
  36. #define PLANESIDE_FRONT 0
  37. #define PLANESIDE_BACK 1
  38. #define PLANESIDE_ON 2
  39. #define PLANESIDE_CROSS 3
  40. // plane types
  41. #define PLANETYPE_X 0
  42. #define PLANETYPE_Y 1
  43. #define PLANETYPE_Z 2
  44. #define PLANETYPE_NEGX 3
  45. #define PLANETYPE_NEGY 4
  46. #define PLANETYPE_NEGZ 5
  47. #define PLANETYPE_TRUEAXIAL 6 // all types < 6 are true axial planes
  48. #define PLANETYPE_ZEROX 6
  49. #define PLANETYPE_ZEROY 7
  50. #define PLANETYPE_ZEROZ 8
  51. #define PLANETYPE_NONAXIAL 9
  52. class idPlane {
  53. public:
  54. idPlane();
  55. explicit idPlane( float a, float b, float c, float d );
  56. explicit idPlane( const idVec3 &normal, const float dist );
  57. explicit idPlane( const idVec3 & v0, const idVec3 & v1, const idVec3 & v2, bool fixDegenerate = false );
  58. float operator[]( int index ) const;
  59. float & operator[]( int index );
  60. idPlane operator-() const; // flips plane
  61. idPlane & operator=( const idVec3 &v ); // sets normal and sets idPlane::d to zero
  62. idPlane operator+( const idPlane &p ) const; // add plane equations
  63. idPlane operator-( const idPlane &p ) const; // subtract plane equations
  64. idPlane operator*( const float s ) const; // scale plane
  65. idPlane & operator*=( const idMat3 &m ); // Normal() *= m
  66. bool Compare( const idPlane &p ) const; // exact compare, no epsilon
  67. bool Compare( const idPlane &p, const float epsilon ) const; // compare with epsilon
  68. bool Compare( const idPlane &p, const float normalEps, const float distEps ) const; // compare with epsilon
  69. bool operator==( const idPlane &p ) const; // exact compare, no epsilon
  70. bool operator!=( const idPlane &p ) const; // exact compare, no epsilon
  71. void Zero(); // zero plane
  72. void SetNormal( const idVec3 &normal ); // sets the normal
  73. const idVec3 & Normal() const; // reference to const normal
  74. idVec3 & Normal(); // reference to normal
  75. float Normalize( bool fixDegenerate = true ); // only normalizes the plane normal, does not adjust d
  76. bool FixDegenerateNormal(); // fix degenerate normal
  77. bool FixDegeneracies( float distEpsilon ); // fix degenerate normal and dist
  78. float Dist() const; // returns: -d
  79. void SetDist( const float dist ); // sets: d = -dist
  80. int Type() const; // returns plane type
  81. bool FromPoints( const idVec3 &p1, const idVec3 &p2, const idVec3 &p3, bool fixDegenerate = true );
  82. bool FromVecs( const idVec3 &dir1, const idVec3 &dir2, const idVec3 &p, bool fixDegenerate = true );
  83. void FitThroughPoint( const idVec3 &p ); // assumes normal is valid
  84. bool HeightFit( const idVec3 *points, const int numPoints );
  85. idPlane Translate( const idVec3 &translation ) const;
  86. idPlane & TranslateSelf( const idVec3 &translation );
  87. idPlane Rotate( const idVec3 &origin, const idMat3 &axis ) const;
  88. idPlane & RotateSelf( const idVec3 &origin, const idMat3 &axis );
  89. float Distance( const idVec3 &v ) const;
  90. int Side( const idVec3 &v, const float epsilon = 0.0f ) const;
  91. bool LineIntersection( const idVec3 &start, const idVec3 &end ) const;
  92. // intersection point is start + dir * scale
  93. bool RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale ) const;
  94. bool PlaneIntersection( const idPlane &plane, idVec3 &start, idVec3 &dir ) const;
  95. int GetDimension() const;
  96. const idVec4 & ToVec4() const;
  97. idVec4 & ToVec4();
  98. const float * ToFloatPtr() const;
  99. float * ToFloatPtr();
  100. const char * ToString( int precision = 2 ) const;
  101. private:
  102. float a;
  103. float b;
  104. float c;
  105. float d;
  106. };
  107. extern idPlane plane_origin;
  108. #define plane_zero plane_origin
  109. ID_INLINE idPlane::idPlane() {
  110. }
  111. ID_INLINE idPlane::idPlane( float a, float b, float c, float d ) {
  112. this->a = a;
  113. this->b = b;
  114. this->c = c;
  115. this->d = d;
  116. }
  117. ID_INLINE idPlane::idPlane( const idVec3 &normal, const float dist ) {
  118. this->a = normal.x;
  119. this->b = normal.y;
  120. this->c = normal.z;
  121. this->d = -dist;
  122. }
  123. ID_INLINE idPlane::idPlane( const idVec3 & v0, const idVec3 & v1, const idVec3 & v2, bool fixDegenerate ) {
  124. FromPoints( v0, v1, v2, fixDegenerate );
  125. }
  126. ID_INLINE float idPlane::operator[]( int index ) const {
  127. return ( &a )[ index ];
  128. }
  129. ID_INLINE float& idPlane::operator[]( int index ) {
  130. return ( &a )[ index ];
  131. }
  132. ID_INLINE idPlane idPlane::operator-() const {
  133. return idPlane( -a, -b, -c, -d );
  134. }
  135. ID_INLINE idPlane &idPlane::operator=( const idVec3 &v ) {
  136. a = v.x;
  137. b = v.y;
  138. c = v.z;
  139. d = 0;
  140. return *this;
  141. }
  142. ID_INLINE idPlane idPlane::operator+( const idPlane &p ) const {
  143. return idPlane( a + p.a, b + p.b, c + p.c, d + p.d );
  144. }
  145. ID_INLINE idPlane idPlane::operator-( const idPlane &p ) const {
  146. return idPlane( a - p.a, b - p.b, c - p.c, d - p.d );
  147. }
  148. ID_INLINE idPlane idPlane::operator*( const float s ) const {
  149. return idPlane( a * s, b * s, c * s, d * s );
  150. }
  151. ID_INLINE idPlane &idPlane::operator*=( const idMat3 &m ) {
  152. Normal() *= m;
  153. return *this;
  154. }
  155. ID_INLINE bool idPlane::Compare( const idPlane &p ) const {
  156. return ( a == p.a && b == p.b && c == p.c && d == p.d );
  157. }
  158. ID_INLINE bool idPlane::Compare( const idPlane &p, const float epsilon ) const {
  159. if ( idMath::Fabs( a - p.a ) > epsilon ) {
  160. return false;
  161. }
  162. if ( idMath::Fabs( b - p.b ) > epsilon ) {
  163. return false;
  164. }
  165. if ( idMath::Fabs( c - p.c ) > epsilon ) {
  166. return false;
  167. }
  168. if ( idMath::Fabs( d - p.d ) > epsilon ) {
  169. return false;
  170. }
  171. return true;
  172. }
  173. ID_INLINE bool idPlane::Compare( const idPlane &p, const float normalEps, const float distEps ) const {
  174. if ( idMath::Fabs( d - p.d ) > distEps ) {
  175. return false;
  176. }
  177. if ( !Normal().Compare( p.Normal(), normalEps ) ) {
  178. return false;
  179. }
  180. return true;
  181. }
  182. ID_INLINE bool idPlane::operator==( const idPlane &p ) const {
  183. return Compare( p );
  184. }
  185. ID_INLINE bool idPlane::operator!=( const idPlane &p ) const {
  186. return !Compare( p );
  187. }
  188. ID_INLINE void idPlane::Zero() {
  189. a = b = c = d = 0.0f;
  190. }
  191. ID_INLINE void idPlane::SetNormal( const idVec3 &normal ) {
  192. a = normal.x;
  193. b = normal.y;
  194. c = normal.z;
  195. }
  196. ID_INLINE const idVec3 &idPlane::Normal() const {
  197. return *reinterpret_cast<const idVec3 *>(&a);
  198. }
  199. ID_INLINE idVec3 &idPlane::Normal() {
  200. return *reinterpret_cast<idVec3 *>(&a);
  201. }
  202. ID_INLINE float idPlane::Normalize( bool fixDegenerate ) {
  203. float length = reinterpret_cast<idVec3 *>(&a)->Normalize();
  204. if ( fixDegenerate ) {
  205. FixDegenerateNormal();
  206. }
  207. return length;
  208. }
  209. ID_INLINE bool idPlane::FixDegenerateNormal() {
  210. return Normal().FixDegenerateNormal();
  211. }
  212. ID_INLINE bool idPlane::FixDegeneracies( float distEpsilon ) {
  213. bool fixedNormal = FixDegenerateNormal();
  214. // only fix dist if the normal was degenerate
  215. if ( fixedNormal ) {
  216. if ( idMath::Fabs( d - idMath::Rint( d ) ) < distEpsilon ) {
  217. d = idMath::Rint( d );
  218. }
  219. }
  220. return fixedNormal;
  221. }
  222. ID_INLINE float idPlane::Dist() const {
  223. return -d;
  224. }
  225. ID_INLINE void idPlane::SetDist( const float dist ) {
  226. d = -dist;
  227. }
  228. ID_INLINE bool idPlane::FromPoints( const idVec3 &p1, const idVec3 &p2, const idVec3 &p3, bool fixDegenerate ) {
  229. Normal() = (p1 - p2).Cross( p3 - p2 );
  230. if ( Normalize( fixDegenerate ) == 0.0f ) {
  231. return false;
  232. }
  233. d = -( Normal() * p2 );
  234. return true;
  235. }
  236. ID_INLINE bool idPlane::FromVecs( const idVec3 &dir1, const idVec3 &dir2, const idVec3 &p, bool fixDegenerate ) {
  237. Normal() = dir1.Cross( dir2 );
  238. if ( Normalize( fixDegenerate ) == 0.0f ) {
  239. return false;
  240. }
  241. d = -( Normal() * p );
  242. return true;
  243. }
  244. ID_INLINE void idPlane::FitThroughPoint( const idVec3 &p ) {
  245. d = -( Normal() * p );
  246. }
  247. ID_INLINE idPlane idPlane::Translate( const idVec3 &translation ) const {
  248. return idPlane( a, b, c, d - translation * Normal() );
  249. }
  250. ID_INLINE idPlane &idPlane::TranslateSelf( const idVec3 &translation ) {
  251. d -= translation * Normal();
  252. return *this;
  253. }
  254. ID_INLINE idPlane idPlane::Rotate( const idVec3 &origin, const idMat3 &axis ) const {
  255. idPlane p;
  256. p.Normal() = Normal() * axis;
  257. p.d = d + origin * Normal() - origin * p.Normal();
  258. return p;
  259. }
  260. ID_INLINE idPlane &idPlane::RotateSelf( const idVec3 &origin, const idMat3 &axis ) {
  261. d += origin * Normal();
  262. Normal() *= axis;
  263. d -= origin * Normal();
  264. return *this;
  265. }
  266. ID_INLINE float idPlane::Distance( const idVec3 &v ) const {
  267. return a * v.x + b * v.y + c * v.z + d;
  268. }
  269. ID_INLINE int idPlane::Side( const idVec3 &v, const float epsilon ) const {
  270. float dist = Distance( v );
  271. if ( dist > epsilon ) {
  272. return PLANESIDE_FRONT;
  273. }
  274. else if ( dist < -epsilon ) {
  275. return PLANESIDE_BACK;
  276. }
  277. else {
  278. return PLANESIDE_ON;
  279. }
  280. }
  281. ID_INLINE bool idPlane::LineIntersection( const idVec3 &start, const idVec3 &end ) const {
  282. float d1, d2, fraction;
  283. d1 = Normal() * start + d;
  284. d2 = Normal() * end + d;
  285. if ( d1 == d2 ) {
  286. return false;
  287. }
  288. if ( d1 > 0.0f && d2 > 0.0f ) {
  289. return false;
  290. }
  291. if ( d1 < 0.0f && d2 < 0.0f ) {
  292. return false;
  293. }
  294. fraction = ( d1 / ( d1 - d2 ) );
  295. return ( fraction >= 0.0f && fraction <= 1.0f );
  296. }
  297. ID_INLINE bool idPlane::RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale ) const {
  298. float d1, d2;
  299. d1 = Normal() * start + d;
  300. d2 = Normal() * dir;
  301. if ( d2 == 0.0f ) {
  302. return false;
  303. }
  304. scale = -( d1 / d2 );
  305. return true;
  306. }
  307. ID_INLINE int idPlane::GetDimension() const {
  308. return 4;
  309. }
  310. ID_INLINE const idVec4 &idPlane::ToVec4() const {
  311. return *reinterpret_cast<const idVec4 *>(&a);
  312. }
  313. ID_INLINE idVec4 &idPlane::ToVec4() {
  314. return *reinterpret_cast<idVec4 *>(&a);
  315. }
  316. ID_INLINE const float *idPlane::ToFloatPtr() const {
  317. return reinterpret_cast<const float *>(&a);
  318. }
  319. ID_INLINE float *idPlane::ToFloatPtr() {
  320. return reinterpret_cast<float *>(&a);
  321. }
  322. #endif /* !__MATH_PLANE_H__ */