Plane.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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( void );
  55. idPlane( float a, float b, float c, float d );
  56. idPlane( const idVec3 &normal, const float dist );
  57. float operator[]( int index ) const;
  58. float & operator[]( int index );
  59. idPlane operator-() const; // flips plane
  60. idPlane & operator=( const idVec3 &v ); // sets normal and sets idPlane::d to zero
  61. idPlane operator+( const idPlane &p ) const; // add plane equations
  62. idPlane operator-( const idPlane &p ) const; // subtract plane equations
  63. idPlane & operator*=( const idMat3 &m ); // Normal() *= m
  64. bool Compare( const idPlane &p ) const; // exact compare, no epsilon
  65. bool Compare( const idPlane &p, const float epsilon ) const; // compare with epsilon
  66. bool Compare( const idPlane &p, const float normalEps, const float distEps ) const; // compare with epsilon
  67. bool operator==( const idPlane &p ) const; // exact compare, no epsilon
  68. bool operator!=( const idPlane &p ) const; // exact compare, no epsilon
  69. void Zero( void ); // zero plane
  70. void SetNormal( const idVec3 &normal ); // sets the normal
  71. const idVec3 & Normal( void ) const; // reference to const normal
  72. idVec3 & Normal( void ); // reference to normal
  73. float Normalize( bool fixDegenerate = true ); // only normalizes the plane normal, does not adjust d
  74. bool FixDegenerateNormal( void ); // fix degenerate normal
  75. bool FixDegeneracies( float distEpsilon ); // fix degenerate normal and dist
  76. float Dist( void ) const; // returns: -d
  77. void SetDist( const float dist ); // sets: d = -dist
  78. int Type( void ) const; // returns plane type
  79. bool FromPoints( const idVec3 &p1, const idVec3 &p2, const idVec3 &p3, bool fixDegenerate = true );
  80. bool FromVecs( const idVec3 &dir1, const idVec3 &dir2, const idVec3 &p, bool fixDegenerate = true );
  81. void FitThroughPoint( const idVec3 &p ); // assumes normal is valid
  82. bool HeightFit( const idVec3 *points, const int numPoints );
  83. idPlane Translate( const idVec3 &translation ) const;
  84. idPlane & TranslateSelf( const idVec3 &translation );
  85. idPlane Rotate( const idVec3 &origin, const idMat3 &axis ) const;
  86. idPlane & RotateSelf( const idVec3 &origin, const idMat3 &axis );
  87. float Distance( const idVec3 &v ) const;
  88. int Side( const idVec3 &v, const float epsilon = 0.0f ) const;
  89. bool LineIntersection( const idVec3 &start, const idVec3 &end ) const;
  90. // intersection point is start + dir * scale
  91. bool RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale ) const;
  92. bool PlaneIntersection( const idPlane &plane, idVec3 &start, idVec3 &dir ) const;
  93. int GetDimension( void ) const;
  94. const idVec4 & ToVec4( void ) const;
  95. idVec4 & ToVec4( void );
  96. const float * ToFloatPtr( void ) const;
  97. float * ToFloatPtr( void );
  98. const char * ToString( int precision = 2 ) const;
  99. private:
  100. float a;
  101. float b;
  102. float c;
  103. float d;
  104. };
  105. extern idPlane plane_origin;
  106. #define plane_zero plane_origin
  107. ID_INLINE idPlane::idPlane( void ) {
  108. }
  109. ID_INLINE idPlane::idPlane( float a, float b, float c, float d ) {
  110. this->a = a;
  111. this->b = b;
  112. this->c = c;
  113. this->d = d;
  114. }
  115. ID_INLINE idPlane::idPlane( const idVec3 &normal, const float dist ) {
  116. this->a = normal.x;
  117. this->b = normal.y;
  118. this->c = normal.z;
  119. this->d = -dist;
  120. }
  121. ID_INLINE float idPlane::operator[]( int index ) const {
  122. return ( &a )[ index ];
  123. }
  124. ID_INLINE float& idPlane::operator[]( int index ) {
  125. return ( &a )[ index ];
  126. }
  127. ID_INLINE idPlane idPlane::operator-() const {
  128. return idPlane( -a, -b, -c, -d );
  129. }
  130. ID_INLINE idPlane &idPlane::operator=( const idVec3 &v ) {
  131. a = v.x;
  132. b = v.y;
  133. c = v.z;
  134. d = 0;
  135. return *this;
  136. }
  137. ID_INLINE idPlane idPlane::operator+( const idPlane &p ) const {
  138. return idPlane( a + p.a, b + p.b, c + p.c, d + p.d );
  139. }
  140. ID_INLINE idPlane idPlane::operator-( const idPlane &p ) const {
  141. return idPlane( a - p.a, b - p.b, c - p.c, d - p.d );
  142. }
  143. ID_INLINE idPlane &idPlane::operator*=( const idMat3 &m ) {
  144. Normal() *= m;
  145. return *this;
  146. }
  147. ID_INLINE bool idPlane::Compare( const idPlane &p ) const {
  148. return ( a == p.a && b == p.b && c == p.c && d == p.d );
  149. }
  150. ID_INLINE bool idPlane::Compare( const idPlane &p, const float epsilon ) const {
  151. if ( idMath::Fabs( a - p.a ) > epsilon ) {
  152. return false;
  153. }
  154. if ( idMath::Fabs( b - p.b ) > epsilon ) {
  155. return false;
  156. }
  157. if ( idMath::Fabs( c - p.c ) > epsilon ) {
  158. return false;
  159. }
  160. if ( idMath::Fabs( d - p.d ) > epsilon ) {
  161. return false;
  162. }
  163. return true;
  164. }
  165. ID_INLINE bool idPlane::Compare( const idPlane &p, const float normalEps, const float distEps ) const {
  166. if ( idMath::Fabs( d - p.d ) > distEps ) {
  167. return false;
  168. }
  169. if ( !Normal().Compare( p.Normal(), normalEps ) ) {
  170. return false;
  171. }
  172. return true;
  173. }
  174. ID_INLINE bool idPlane::operator==( const idPlane &p ) const {
  175. return Compare( p );
  176. }
  177. ID_INLINE bool idPlane::operator!=( const idPlane &p ) const {
  178. return !Compare( p );
  179. }
  180. ID_INLINE void idPlane::Zero( void ) {
  181. a = b = c = d = 0.0f;
  182. }
  183. ID_INLINE void idPlane::SetNormal( const idVec3 &normal ) {
  184. a = normal.x;
  185. b = normal.y;
  186. c = normal.z;
  187. }
  188. ID_INLINE const idVec3 &idPlane::Normal( void ) const {
  189. return *reinterpret_cast<const idVec3 *>(&a);
  190. }
  191. ID_INLINE idVec3 &idPlane::Normal( void ) {
  192. return *reinterpret_cast<idVec3 *>(&a);
  193. }
  194. ID_INLINE float idPlane::Normalize( bool fixDegenerate ) {
  195. float length = reinterpret_cast<idVec3 *>(&a)->Normalize();
  196. if ( fixDegenerate ) {
  197. FixDegenerateNormal();
  198. }
  199. return length;
  200. }
  201. ID_INLINE bool idPlane::FixDegenerateNormal( void ) {
  202. return Normal().FixDegenerateNormal();
  203. }
  204. ID_INLINE bool idPlane::FixDegeneracies( float distEpsilon ) {
  205. bool fixedNormal = FixDegenerateNormal();
  206. // only fix dist if the normal was degenerate
  207. if ( fixedNormal ) {
  208. if ( idMath::Fabs( d - idMath::Rint( d ) ) < distEpsilon ) {
  209. d = idMath::Rint( d );
  210. }
  211. }
  212. return fixedNormal;
  213. }
  214. ID_INLINE float idPlane::Dist( void ) const {
  215. return -d;
  216. }
  217. ID_INLINE void idPlane::SetDist( const float dist ) {
  218. d = -dist;
  219. }
  220. ID_INLINE bool idPlane::FromPoints( const idVec3 &p1, const idVec3 &p2, const idVec3 &p3, bool fixDegenerate ) {
  221. Normal() = (p1 - p2).Cross( p3 - p2 );
  222. if ( Normalize( fixDegenerate ) == 0.0f ) {
  223. return false;
  224. }
  225. d = -( Normal() * p2 );
  226. return true;
  227. }
  228. ID_INLINE bool idPlane::FromVecs( const idVec3 &dir1, const idVec3 &dir2, const idVec3 &p, bool fixDegenerate ) {
  229. Normal() = dir1.Cross( dir2 );
  230. if ( Normalize( fixDegenerate ) == 0.0f ) {
  231. return false;
  232. }
  233. d = -( Normal() * p );
  234. return true;
  235. }
  236. ID_INLINE void idPlane::FitThroughPoint( const idVec3 &p ) {
  237. d = -( Normal() * p );
  238. }
  239. ID_INLINE idPlane idPlane::Translate( const idVec3 &translation ) const {
  240. return idPlane( a, b, c, d - translation * Normal() );
  241. }
  242. ID_INLINE idPlane &idPlane::TranslateSelf( const idVec3 &translation ) {
  243. d -= translation * Normal();
  244. return *this;
  245. }
  246. ID_INLINE idPlane idPlane::Rotate( const idVec3 &origin, const idMat3 &axis ) const {
  247. idPlane p;
  248. p.Normal() = Normal() * axis;
  249. p.d = d + origin * Normal() - origin * p.Normal();
  250. return p;
  251. }
  252. ID_INLINE idPlane &idPlane::RotateSelf( const idVec3 &origin, const idMat3 &axis ) {
  253. d += origin * Normal();
  254. Normal() *= axis;
  255. d -= origin * Normal();
  256. return *this;
  257. }
  258. ID_INLINE float idPlane::Distance( const idVec3 &v ) const {
  259. return a * v.x + b * v.y + c * v.z + d;
  260. }
  261. ID_INLINE int idPlane::Side( const idVec3 &v, const float epsilon ) const {
  262. float dist = Distance( v );
  263. if ( dist > epsilon ) {
  264. return PLANESIDE_FRONT;
  265. }
  266. else if ( dist < -epsilon ) {
  267. return PLANESIDE_BACK;
  268. }
  269. else {
  270. return PLANESIDE_ON;
  271. }
  272. }
  273. ID_INLINE bool idPlane::LineIntersection( const idVec3 &start, const idVec3 &end ) const {
  274. float d1, d2, fraction;
  275. d1 = Normal() * start + d;
  276. d2 = Normal() * end + d;
  277. if ( d1 == d2 ) {
  278. return false;
  279. }
  280. if ( d1 > 0.0f && d2 > 0.0f ) {
  281. return false;
  282. }
  283. if ( d1 < 0.0f && d2 < 0.0f ) {
  284. return false;
  285. }
  286. fraction = ( d1 / ( d1 - d2 ) );
  287. return ( fraction >= 0.0f && fraction <= 1.0f );
  288. }
  289. ID_INLINE bool idPlane::RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale ) const {
  290. float d1, d2;
  291. d1 = Normal() * start + d;
  292. d2 = Normal() * dir;
  293. if ( d2 == 0.0f ) {
  294. return false;
  295. }
  296. scale = -( d1 / d2 );
  297. return true;
  298. }
  299. ID_INLINE int idPlane::GetDimension( void ) const {
  300. return 4;
  301. }
  302. ID_INLINE const idVec4 &idPlane::ToVec4( void ) const {
  303. return *reinterpret_cast<const idVec4 *>(&a);
  304. }
  305. ID_INLINE idVec4 &idPlane::ToVec4( void ) {
  306. return *reinterpret_cast<idVec4 *>(&a);
  307. }
  308. ID_INLINE const float *idPlane::ToFloatPtr( void ) const {
  309. return reinterpret_cast<const float *>(&a);
  310. }
  311. ID_INLINE float *idPlane::ToFloatPtr( void ) {
  312. return reinterpret_cast<float *>(&a);
  313. }
  314. #endif /* !__MATH_PLANE_H__ */