Pluecker.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_PLUECKER_H__
  21. #define __MATH_PLUECKER_H__
  22. /*
  23. ===============================================================================
  24. Pluecker coordinate
  25. ===============================================================================
  26. */
  27. class idPluecker {
  28. public:
  29. idPluecker( void );
  30. explicit idPluecker( const float *a );
  31. explicit idPluecker( const idVec3 &start, const idVec3 &end );
  32. explicit idPluecker( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 );
  33. float operator[]( const int index ) const;
  34. float & operator[]( const int index );
  35. idPluecker operator-() const; // flips the direction
  36. idPluecker operator*( const float a ) const;
  37. idPluecker operator/( const float a ) const;
  38. float operator*( const idPluecker &a ) const; // permuted inner product
  39. idPluecker operator-( const idPluecker &a ) const;
  40. idPluecker operator+( const idPluecker &a ) const;
  41. idPluecker & operator*=( const float a );
  42. idPluecker & operator/=( const float a );
  43. idPluecker & operator+=( const idPluecker &a );
  44. idPluecker & operator-=( const idPluecker &a );
  45. bool Compare( const idPluecker &a ) const; // exact compare, no epsilon
  46. bool Compare( const idPluecker &a, const float epsilon ) const; // compare with epsilon
  47. bool operator==( const idPluecker &a ) const; // exact compare, no epsilon
  48. bool operator!=( const idPluecker &a ) const; // exact compare, no epsilon
  49. void Set( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 );
  50. void Zero( void );
  51. void FromLine( const idVec3 &start, const idVec3 &end ); // pluecker from line
  52. void FromRay( const idVec3 &start, const idVec3 &dir ); // pluecker from ray
  53. bool FromPlanes( const idPlane &p1, const idPlane &p2 ); // pluecker from intersection of planes
  54. bool ToLine( idVec3 &start, idVec3 &end ) const; // pluecker to line
  55. bool ToRay( idVec3 &start, idVec3 &dir ) const; // pluecker to ray
  56. void ToDir( idVec3 &dir ) const; // pluecker to direction
  57. float PermutedInnerProduct( const idPluecker &a ) const; // pluecker permuted inner product
  58. float Distance3DSqr( const idPluecker &a ) const; // pluecker line distance
  59. float Length( void ) const; // pluecker length
  60. float LengthSqr( void ) const; // pluecker squared length
  61. idPluecker Normalize( void ) const; // pluecker normalize
  62. float NormalizeSelf( void ); // pluecker normalize
  63. int GetDimension( void ) const;
  64. const float * ToFloatPtr( void ) const;
  65. float * ToFloatPtr( void );
  66. const char * ToString( int precision = 2 ) const;
  67. private:
  68. float p[6];
  69. };
  70. extern idPluecker pluecker_origin;
  71. #define pluecker_zero pluecker_origin
  72. ID_INLINE idPluecker::idPluecker( void ) {
  73. }
  74. ID_INLINE idPluecker::idPluecker( const float *a ) {
  75. memcpy( p, a, 6 * sizeof( float ) );
  76. }
  77. ID_INLINE idPluecker::idPluecker( const idVec3 &start, const idVec3 &end ) {
  78. FromLine( start, end );
  79. }
  80. ID_INLINE idPluecker::idPluecker( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 ) {
  81. p[0] = a1;
  82. p[1] = a2;
  83. p[2] = a3;
  84. p[3] = a4;
  85. p[4] = a5;
  86. p[5] = a6;
  87. }
  88. ID_INLINE idPluecker idPluecker::operator-() const {
  89. return idPluecker( -p[0], -p[1], -p[2], -p[3], -p[4], -p[5] );
  90. }
  91. ID_INLINE float idPluecker::operator[]( const int index ) const {
  92. return p[index];
  93. }
  94. ID_INLINE float &idPluecker::operator[]( const int index ) {
  95. return p[index];
  96. }
  97. ID_INLINE idPluecker idPluecker::operator*( const float a ) const {
  98. return idPluecker( p[0]*a, p[1]*a, p[2]*a, p[3]*a, p[4]*a, p[5]*a );
  99. }
  100. ID_INLINE float idPluecker::operator*( const idPluecker &a ) const {
  101. return p[0] * a.p[4] + p[1] * a.p[5] + p[2] * a.p[3] + p[4] * a.p[0] + p[5] * a.p[1] + p[3] * a.p[2];
  102. }
  103. ID_INLINE idPluecker idPluecker::operator/( const float a ) const {
  104. float inva;
  105. assert( a != 0.0f );
  106. inva = 1.0f / a;
  107. return idPluecker( p[0]*inva, p[1]*inva, p[2]*inva, p[3]*inva, p[4]*inva, p[5]*inva );
  108. }
  109. ID_INLINE idPluecker idPluecker::operator+( const idPluecker &a ) const {
  110. return idPluecker( p[0] + a[0], p[1] + a[1], p[2] + a[2], p[3] + a[3], p[4] + a[4], p[5] + a[5] );
  111. }
  112. ID_INLINE idPluecker idPluecker::operator-( const idPluecker &a ) const {
  113. return idPluecker( p[0] - a[0], p[1] - a[1], p[2] - a[2], p[3] - a[3], p[4] - a[4], p[5] - a[5] );
  114. }
  115. ID_INLINE idPluecker &idPluecker::operator*=( const float a ) {
  116. p[0] *= a;
  117. p[1] *= a;
  118. p[2] *= a;
  119. p[3] *= a;
  120. p[4] *= a;
  121. p[5] *= a;
  122. return *this;
  123. }
  124. ID_INLINE idPluecker &idPluecker::operator/=( const float a ) {
  125. float inva;
  126. assert( a != 0.0f );
  127. inva = 1.0f / a;
  128. p[0] *= inva;
  129. p[1] *= inva;
  130. p[2] *= inva;
  131. p[3] *= inva;
  132. p[4] *= inva;
  133. p[5] *= inva;
  134. return *this;
  135. }
  136. ID_INLINE idPluecker &idPluecker::operator+=( const idPluecker &a ) {
  137. p[0] += a[0];
  138. p[1] += a[1];
  139. p[2] += a[2];
  140. p[3] += a[3];
  141. p[4] += a[4];
  142. p[5] += a[5];
  143. return *this;
  144. }
  145. ID_INLINE idPluecker &idPluecker::operator-=( const idPluecker &a ) {
  146. p[0] -= a[0];
  147. p[1] -= a[1];
  148. p[2] -= a[2];
  149. p[3] -= a[3];
  150. p[4] -= a[4];
  151. p[5] -= a[5];
  152. return *this;
  153. }
  154. ID_INLINE bool idPluecker::Compare( const idPluecker &a ) const {
  155. return ( ( p[0] == a[0] ) && ( p[1] == a[1] ) && ( p[2] == a[2] ) &&
  156. ( p[3] == a[3] ) && ( p[4] == a[4] ) && ( p[5] == a[5] ) );
  157. }
  158. ID_INLINE bool idPluecker::Compare( const idPluecker &a, const float epsilon ) const {
  159. if ( idMath::Fabs( p[0] - a[0] ) > epsilon ) {
  160. return false;
  161. }
  162. if ( idMath::Fabs( p[1] - a[1] ) > epsilon ) {
  163. return false;
  164. }
  165. if ( idMath::Fabs( p[2] - a[2] ) > epsilon ) {
  166. return false;
  167. }
  168. if ( idMath::Fabs( p[3] - a[3] ) > epsilon ) {
  169. return false;
  170. }
  171. if ( idMath::Fabs( p[4] - a[4] ) > epsilon ) {
  172. return false;
  173. }
  174. if ( idMath::Fabs( p[5] - a[5] ) > epsilon ) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. ID_INLINE bool idPluecker::operator==( const idPluecker &a ) const {
  180. return Compare( a );
  181. }
  182. ID_INLINE bool idPluecker::operator!=( const idPluecker &a ) const {
  183. return !Compare( a );
  184. }
  185. ID_INLINE void idPluecker::Set( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 ) {
  186. p[0] = a1;
  187. p[1] = a2;
  188. p[2] = a3;
  189. p[3] = a4;
  190. p[4] = a5;
  191. p[5] = a6;
  192. }
  193. ID_INLINE void idPluecker::Zero( void ) {
  194. p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = 0.0f;
  195. }
  196. ID_INLINE void idPluecker::FromLine( const idVec3 &start, const idVec3 &end ) {
  197. p[0] = start[0] * end[1] - end[0] * start[1];
  198. p[1] = start[0] * end[2] - end[0] * start[2];
  199. p[2] = start[0] - end[0];
  200. p[3] = start[1] * end[2] - end[1] * start[2];
  201. p[4] = start[2] - end[2];
  202. p[5] = end[1] - start[1];
  203. }
  204. ID_INLINE void idPluecker::FromRay( const idVec3 &start, const idVec3 &dir ) {
  205. p[0] = start[0] * dir[1] - dir[0] * start[1];
  206. p[1] = start[0] * dir[2] - dir[0] * start[2];
  207. p[2] = -dir[0];
  208. p[3] = start[1] * dir[2] - dir[1] * start[2];
  209. p[4] = -dir[2];
  210. p[5] = dir[1];
  211. }
  212. ID_INLINE bool idPluecker::ToLine( idVec3 &start, idVec3 &end ) const {
  213. idVec3 dir1, dir2;
  214. float d;
  215. dir1[0] = p[3];
  216. dir1[1] = -p[1];
  217. dir1[2] = p[0];
  218. dir2[0] = -p[2];
  219. dir2[1] = p[5];
  220. dir2[2] = -p[4];
  221. d = dir2 * dir2;
  222. if ( d == 0.0f ) {
  223. return false; // pluecker coordinate does not represent a line
  224. }
  225. start = dir2.Cross(dir1) * (1.0f / d);
  226. end = start + dir2;
  227. return true;
  228. }
  229. ID_INLINE bool idPluecker::ToRay( idVec3 &start, idVec3 &dir ) const {
  230. idVec3 dir1;
  231. float d;
  232. dir1[0] = p[3];
  233. dir1[1] = -p[1];
  234. dir1[2] = p[0];
  235. dir[0] = -p[2];
  236. dir[1] = p[5];
  237. dir[2] = -p[4];
  238. d = dir * dir;
  239. if ( d == 0.0f ) {
  240. return false; // pluecker coordinate does not represent a line
  241. }
  242. start = dir.Cross(dir1) * (1.0f / d);
  243. return true;
  244. }
  245. ID_INLINE void idPluecker::ToDir( idVec3 &dir ) const {
  246. dir[0] = -p[2];
  247. dir[1] = p[5];
  248. dir[2] = -p[4];
  249. }
  250. ID_INLINE float idPluecker::PermutedInnerProduct( const idPluecker &a ) const {
  251. return p[0] * a.p[4] + p[1] * a.p[5] + p[2] * a.p[3] + p[4] * a.p[0] + p[5] * a.p[1] + p[3] * a.p[2];
  252. }
  253. ID_INLINE float idPluecker::Length( void ) const {
  254. return ( float )idMath::Sqrt( p[5] * p[5] + p[4] * p[4] + p[2] * p[2] );
  255. }
  256. ID_INLINE float idPluecker::LengthSqr( void ) const {
  257. return ( p[5] * p[5] + p[4] * p[4] + p[2] * p[2] );
  258. }
  259. ID_INLINE float idPluecker::NormalizeSelf( void ) {
  260. float l, d;
  261. l = LengthSqr();
  262. if ( l == 0.0f ) {
  263. return l; // pluecker coordinate does not represent a line
  264. }
  265. d = idMath::InvSqrt( l );
  266. p[0] *= d;
  267. p[1] *= d;
  268. p[2] *= d;
  269. p[3] *= d;
  270. p[4] *= d;
  271. p[5] *= d;
  272. return d * l;
  273. }
  274. ID_INLINE idPluecker idPluecker::Normalize( void ) const {
  275. float d;
  276. d = LengthSqr();
  277. if ( d == 0.0f ) {
  278. return *this; // pluecker coordinate does not represent a line
  279. }
  280. d = idMath::InvSqrt( d );
  281. return idPluecker( p[0]*d, p[1]*d, p[2]*d, p[3]*d, p[4]*d, p[5]*d );
  282. }
  283. ID_INLINE int idPluecker::GetDimension( void ) const {
  284. return 6;
  285. }
  286. ID_INLINE const float *idPluecker::ToFloatPtr( void ) const {
  287. return p;
  288. }
  289. ID_INLINE float *idPluecker::ToFloatPtr( void ) {
  290. return p;
  291. }
  292. #endif /* !__MATH_PLUECKER_H__ */