doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Pluecker.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 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.
23 
24 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.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __MATH_PLUECKER_H__
30 #define __MATH_PLUECKER_H__
31 
32 /*
33 ===============================================================================
34 
35  Pluecker coordinate
36 
37 ===============================================================================
38 */
39 
40 class idPluecker {
41 public:
42  idPluecker( void );
43  explicit idPluecker( const float *a );
44  explicit idPluecker( const idVec3 &start, const idVec3 &end );
45  explicit idPluecker( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 );
46 
47  float operator[]( const int index ) const;
48  float & operator[]( const int index );
49  idPluecker operator-() const; // flips the direction
50  idPluecker operator*( const float a ) const;
51  idPluecker operator/( const float a ) const;
52  float operator*( const idPluecker &a ) const; // permuted inner product
53  idPluecker operator-( const idPluecker &a ) const;
54  idPluecker operator+( const idPluecker &a ) const;
55  idPluecker & operator*=( const float a );
56  idPluecker & operator/=( const float a );
57  idPluecker & operator+=( const idPluecker &a );
58  idPluecker & operator-=( const idPluecker &a );
59 
60  bool Compare( const idPluecker &a ) const; // exact compare, no epsilon
61  bool Compare( const idPluecker &a, const float epsilon ) const; // compare with epsilon
62  bool operator==( const idPluecker &a ) const; // exact compare, no epsilon
63  bool operator!=( const idPluecker &a ) const; // exact compare, no epsilon
64 
65  void Set( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 );
66  void Zero( void );
67 
68  void FromLine( const idVec3 &start, const idVec3 &end ); // pluecker from line
69  void FromRay( const idVec3 &start, const idVec3 &dir ); // pluecker from ray
70  bool FromPlanes( const idPlane &p1, const idPlane &p2 ); // pluecker from intersection of planes
71  bool ToLine( idVec3 &start, idVec3 &end ) const; // pluecker to line
72  bool ToRay( idVec3 &start, idVec3 &dir ) const; // pluecker to ray
73  void ToDir( idVec3 &dir ) const; // pluecker to direction
74  float PermutedInnerProduct( const idPluecker &a ) const; // pluecker permuted inner product
75  float Distance3DSqr( const idPluecker &a ) const; // pluecker line distance
76 
77  float Length( void ) const; // pluecker length
78  float LengthSqr( void ) const; // pluecker squared length
79  idPluecker Normalize( void ) const; // pluecker normalize
80  float NormalizeSelf( void ); // pluecker normalize
81 
82  int GetDimension( void ) const;
83 
84  const float * ToFloatPtr( void ) const;
85  float * ToFloatPtr( void );
86  const char * ToString( int precision = 2 ) const;
87 
88 private:
89  float p[6];
90 };
91 
93 #define pluecker_zero pluecker_origin
94 
95 ID_INLINE idPluecker::idPluecker( void ) {
96 }
97 
98 ID_INLINE idPluecker::idPluecker( const float *a ) {
99  memcpy( p, a, 6 * sizeof( float ) );
100 }
101 
102 ID_INLINE idPluecker::idPluecker( const idVec3 &start, const idVec3 &end ) {
103  FromLine( start, end );
104 }
105 
106 ID_INLINE idPluecker::idPluecker( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 ) {
107  p[0] = a1;
108  p[1] = a2;
109  p[2] = a3;
110  p[3] = a4;
111  p[4] = a5;
112  p[5] = a6;
113 }
114 
116  return idPluecker( -p[0], -p[1], -p[2], -p[3], -p[4], -p[5] );
117 }
118 
119 ID_INLINE float idPluecker::operator[]( const int index ) const {
120  return p[index];
121 }
122 
123 ID_INLINE float &idPluecker::operator[]( const int index ) {
124  return p[index];
125 }
126 
127 ID_INLINE idPluecker idPluecker::operator*( const float a ) const {
128  return idPluecker( p[0]*a, p[1]*a, p[2]*a, p[3]*a, p[4]*a, p[5]*a );
129 }
130 
131 ID_INLINE float idPluecker::operator*( const idPluecker &a ) const {
132  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];
133 }
134 
135 ID_INLINE idPluecker idPluecker::operator/( const float a ) const {
136  float inva;
137 
138  assert( a != 0.0f );
139  inva = 1.0f / a;
140  return idPluecker( p[0]*inva, p[1]*inva, p[2]*inva, p[3]*inva, p[4]*inva, p[5]*inva );
141 }
142 
143 ID_INLINE idPluecker idPluecker::operator+( const idPluecker &a ) const {
144  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] );
145 }
146 
147 ID_INLINE idPluecker idPluecker::operator-( const idPluecker &a ) const {
148  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] );
149 }
150 
151 ID_INLINE idPluecker &idPluecker::operator*=( const float a ) {
152  p[0] *= a;
153  p[1] *= a;
154  p[2] *= a;
155  p[3] *= a;
156  p[4] *= a;
157  p[5] *= a;
158  return *this;
159 }
160 
161 ID_INLINE idPluecker &idPluecker::operator/=( const float a ) {
162  float inva;
163 
164  assert( a != 0.0f );
165  inva = 1.0f / a;
166  p[0] *= inva;
167  p[1] *= inva;
168  p[2] *= inva;
169  p[3] *= inva;
170  p[4] *= inva;
171  p[5] *= inva;
172  return *this;
173 }
174 
176  p[0] += a[0];
177  p[1] += a[1];
178  p[2] += a[2];
179  p[3] += a[3];
180  p[4] += a[4];
181  p[5] += a[5];
182  return *this;
183 }
184 
186  p[0] -= a[0];
187  p[1] -= a[1];
188  p[2] -= a[2];
189  p[3] -= a[3];
190  p[4] -= a[4];
191  p[5] -= a[5];
192  return *this;
193 }
194 
195 ID_INLINE bool idPluecker::Compare( const idPluecker &a ) const {
196  return ( ( p[0] == a[0] ) && ( p[1] == a[1] ) && ( p[2] == a[2] ) &&
197  ( p[3] == a[3] ) && ( p[4] == a[4] ) && ( p[5] == a[5] ) );
198 }
199 
200 ID_INLINE bool idPluecker::Compare( const idPluecker &a, const float epsilon ) const {
201  if ( idMath::Fabs( p[0] - a[0] ) > epsilon ) {
202  return false;
203  }
204 
205  if ( idMath::Fabs( p[1] - a[1] ) > epsilon ) {
206  return false;
207  }
208 
209  if ( idMath::Fabs( p[2] - a[2] ) > epsilon ) {
210  return false;
211  }
212 
213  if ( idMath::Fabs( p[3] - a[3] ) > epsilon ) {
214  return false;
215  }
216 
217  if ( idMath::Fabs( p[4] - a[4] ) > epsilon ) {
218  return false;
219  }
220 
221  if ( idMath::Fabs( p[5] - a[5] ) > epsilon ) {
222  return false;
223  }
224 
225  return true;
226 }
227 
228 ID_INLINE bool idPluecker::operator==( const idPluecker &a ) const {
229  return Compare( a );
230 }
231 
232 ID_INLINE bool idPluecker::operator!=( const idPluecker &a ) const {
233  return !Compare( a );
234 }
235 
236 ID_INLINE void idPluecker::Set( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 ) {
237  p[0] = a1;
238  p[1] = a2;
239  p[2] = a3;
240  p[3] = a4;
241  p[4] = a5;
242  p[5] = a6;
243 }
244 
245 ID_INLINE void idPluecker::Zero( void ) {
246  p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = 0.0f;
247 }
248 
249 ID_INLINE void idPluecker::FromLine( const idVec3 &start, const idVec3 &end ) {
250  p[0] = start[0] * end[1] - end[0] * start[1];
251  p[1] = start[0] * end[2] - end[0] * start[2];
252  p[2] = start[0] - end[0];
253  p[3] = start[1] * end[2] - end[1] * start[2];
254  p[4] = start[2] - end[2];
255  p[5] = end[1] - start[1];
256 }
257 
258 ID_INLINE void idPluecker::FromRay( const idVec3 &start, const idVec3 &dir ) {
259  p[0] = start[0] * dir[1] - dir[0] * start[1];
260  p[1] = start[0] * dir[2] - dir[0] * start[2];
261  p[2] = -dir[0];
262  p[3] = start[1] * dir[2] - dir[1] * start[2];
263  p[4] = -dir[2];
264  p[5] = dir[1];
265 }
266 
267 ID_INLINE bool idPluecker::ToLine( idVec3 &start, idVec3 &end ) const {
268  idVec3 dir1, dir2;
269  float d;
270 
271  dir1[0] = p[3];
272  dir1[1] = -p[1];
273  dir1[2] = p[0];
274 
275  dir2[0] = -p[2];
276  dir2[1] = p[5];
277  dir2[2] = -p[4];
278 
279  d = dir2 * dir2;
280  if ( d == 0.0f ) {
281  return false; // pluecker coordinate does not represent a line
282  }
283 
284  start = dir2.Cross(dir1) * (1.0f / d);
285  end = start + dir2;
286  return true;
287 }
288 
289 ID_INLINE bool idPluecker::ToRay( idVec3 &start, idVec3 &dir ) const {
290  idVec3 dir1;
291  float d;
292 
293  dir1[0] = p[3];
294  dir1[1] = -p[1];
295  dir1[2] = p[0];
296 
297  dir[0] = -p[2];
298  dir[1] = p[5];
299  dir[2] = -p[4];
300 
301  d = dir * dir;
302  if ( d == 0.0f ) {
303  return false; // pluecker coordinate does not represent a line
304  }
305 
306  start = dir.Cross(dir1) * (1.0f / d);
307  return true;
308 }
309 
310 ID_INLINE void idPluecker::ToDir( idVec3 &dir ) const {
311  dir[0] = -p[2];
312  dir[1] = p[5];
313  dir[2] = -p[4];
314 }
315 
316 ID_INLINE float idPluecker::PermutedInnerProduct( const idPluecker &a ) const {
317  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];
318 }
319 
320 ID_INLINE float idPluecker::Length( void ) const {
321  return ( float )idMath::Sqrt( p[5] * p[5] + p[4] * p[4] + p[2] * p[2] );
322 }
323 
324 ID_INLINE float idPluecker::LengthSqr( void ) const {
325  return ( p[5] * p[5] + p[4] * p[4] + p[2] * p[2] );
326 }
327 
328 ID_INLINE float idPluecker::NormalizeSelf( void ) {
329  float l, d;
330 
331  l = LengthSqr();
332  if ( l == 0.0f ) {
333  return l; // pluecker coordinate does not represent a line
334  }
335  d = idMath::InvSqrt( l );
336  p[0] *= d;
337  p[1] *= d;
338  p[2] *= d;
339  p[3] *= d;
340  p[4] *= d;
341  p[5] *= d;
342  return d * l;
343 }
344 
345 ID_INLINE idPluecker idPluecker::Normalize( void ) const {
346  float d;
347 
348  d = LengthSqr();
349  if ( d == 0.0f ) {
350  return *this; // pluecker coordinate does not represent a line
351  }
352  d = idMath::InvSqrt( d );
353  return idPluecker( p[0]*d, p[1]*d, p[2]*d, p[3]*d, p[4]*d, p[5]*d );
354 }
355 
356 ID_INLINE int idPluecker::GetDimension( void ) const {
357  return 6;
358 }
359 
360 ID_INLINE const float *idPluecker::ToFloatPtr( void ) const {
361  return p;
362 }
363 
364 ID_INLINE float *idPluecker::ToFloatPtr( void ) {
365  return p;
366 }
367 
368 #endif /* !__MATH_PLUECKER_H__ */
bool operator==(const idPluecker &a) const
Definition: Pluecker.h:228
assert(prefInfo.fullscreenBtn)
bool ToRay(idVec3 &start, idVec3 &dir) const
Definition: Pluecker.h:289
void ToDir(idVec3 &dir) const
Definition: Pluecker.h:310
idPluecker Normalize(void) const
Definition: Pluecker.h:345
idPluecker & operator+=(const idPluecker &a)
Definition: Pluecker.h:175
void Set(const float a1, const float a2, const float a3, const float a4, const float a5, const float a6)
Definition: Pluecker.h:236
Definition: Vector.h:316
static float Sqrt(float x)
Definition: Math.h:302
int GetDimension(void) const
Definition: Pluecker.h:356
idPluecker operator-() const
Definition: Pluecker.h:115
idVec3 Cross(const idVec3 &a) const
Definition: Vector.h:619
idPluecker operator*(const float a) const
Definition: Pluecker.h:127
const char * ToString(int precision=2) const
Definition: Pluecker.cpp:84
void FromLine(const idVec3 &start, const idVec3 &end)
Definition: Pluecker.h:249
list l
Definition: prepare.py:17
bool FromPlanes(const idPlane &p1, const idPlane &p2)
Definition: Pluecker.cpp:41
float operator[](const int index) const
Definition: Pluecker.h:119
GLuint index
Definition: glext.h:3476
GLuint GLuint end
Definition: glext.h:2845
bool Compare(const idPluecker &a) const
Definition: Pluecker.h:195
static float Fabs(float f)
Definition: Math.h:779
Definition: Plane.h:71
float PermutedInnerProduct(const idPluecker &a) const
Definition: Pluecker.h:316
float NormalizeSelf(void)
Definition: Pluecker.h:328
idPluecker operator+(const idPluecker &a) const
Definition: Pluecker.h:143
static float InvSqrt(float x)
Definition: Math.h:268
void FromRay(const idVec3 &start, const idVec3 &dir)
Definition: Pluecker.h:258
idPluecker & operator-=(const idPluecker &a)
Definition: Pluecker.h:185
GLubyte GLubyte GLubyte a
Definition: glext.h:4662
idPluecker(void)
Definition: Pluecker.h:95
const float * ToFloatPtr(void) const
Definition: Pluecker.h:360
float Length(void) const
Definition: Pluecker.h:320
bool operator!=(const idPluecker &a) const
Definition: Pluecker.h:232
idPluecker operator/(const float a) const
Definition: Pluecker.h:135
tuple f
Definition: idal.py:89
idPluecker & operator/=(const float a)
Definition: Pluecker.h:161
idPluecker & operator*=(const float a)
Definition: Pluecker.h:151
idPluecker pluecker_origin
bool ToLine(idVec3 &start, idVec3 &end) const
Definition: Pluecker.h:267
float p[6]
Definition: Pluecker.h:89
float LengthSqr(void) const
Definition: Pluecker.h:324
float Distance3DSqr(const idPluecker &a) const
Definition: Pluecker.cpp:62
GLfloat GLfloat p
Definition: glext.h:4674
GLuint start
Definition: glext.h:2845
void Zero(void)
Definition: Pluecker.h:245