Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Quaternion Struct Reference

#include <quaternion.h>

Public Member Functions

 Quaternion ()
 
 Quaternion (float s, Vector v)
 
Quaternion Conjugate () const
 
void netswap ()
 
float Magnitude () const
 
Quaternion operator* (const Quaternion &rval) const
 
Quaternionoperator*= (const Quaternion &rval)
 
QuaternionNormalize ()
 
void to_matrix (Matrix &mat) const
 

Static Public Member Functions

static Quaternion from_vectors (const Vector &v1, const Vector &v2, const Vector &v3)
 
static Quaternion from_axis_angle (const Vector &axis, float angle)
 

Public Attributes

float s
 
Vector v
 

Detailed Description

Definition at line 7 of file quaternion.h.

Constructor & Destructor Documentation

Quaternion::Quaternion ( )
inline

Definition at line 11 of file quaternion.h.

Referenced by Conjugate(), from_axis_angle(), from_vectors(), and operator*().

11  : s( 0 )
12  , v( 0, 0, 0 ) {}
Quaternion::Quaternion ( float  s,
Vector  v 
)
inline

Definition at line 13 of file quaternion.h.

References s, and v.

14  {
15  this->s = s;
16  this->v = v;
17  }

Member Function Documentation

Quaternion Quaternion::Conjugate ( ) const
inline

Definition at line 19 of file quaternion.h.

References Quaternion(), s, v, and Vector.

Referenced by HoldPositionWithRespectTo(), Transformation::InvertOrientationRevPos(), and PhysicsSystem::Rotate().

20  {
21  return Quaternion( s, Vector( -v.i, -v.j, -v.k ) );
22  }
Quaternion Quaternion::from_axis_angle ( const Vector axis,
float  angle 
)
static

Definition at line 87 of file quaternion.cpp.

References UniverseUtil::cos(), Quaternion(), UniverseUtil::sin(), and Vector.

88 {
89  float sin_a = sin( angle/2 );
90  float cos_a = cos( angle/2 );
91  return Quaternion( cos_a, Vector( axis.i*sin_a,
92  axis.j*sin_a,
93  axis.k*sin_a ) );
94 }
Quaternion Quaternion::from_vectors ( const Vector v1,
const Vector v2,
const Vector v3 
)
static

Definition at line 3 of file quaternion.cpp.

References max(), Quaternion(), S, Vector, AiXml::X, AiXml::Y, and AiXml::Z.

Referenced by AddMounts(), aim_assist(), aim_assist_debug(), Transformation::from_matrix(), LocationSelect::SetOrientation(), Beam::SetOrientation(), and Unit::SetOrientation().

4 {
5  float T = v1.i+v2.j+v3.k+1, S, W, X, Y, Z;
6  if (T > 0) {
7  S = 0.5/sqrtf( T );
8  W = 0.25/S;
9  X = (v3.j-v2.k)*S;
10  Y = (v1.k-v3.i)*S;
11  Z = (v2.i-v1.j)*S;
12  } else {
13  int max = (v1.i > v2.j) ? 1 : 2;
14  if (max == 1)
15  max = (v1.i > v3.k) ? 1 : 3;
16  else
17  max = (v2.j > v3.k) ? 2 : 3;
18  switch (max)
19  {
20  case 1:
21  //column 0
22  S = sqrtf( ( v2.i-(v2.j+v3.k) )+1 );
23  X = S*.5;
24  S = .5/S;
25  W = (v3.j-v2.k)*S;
26  Y = (v2.i+v1.j)*S;
27  Z = (v3.i+v1.k)*S;
28  break;
29  case 2:
30  //column 1
31  S = sqrtf( ( v3.j-(v3.k+v1.i) )+1 );
32  Y = 0.5*S;
33  S = .5/S;
34  W = (v1.k-v3.i);
35  Z = (v3.j+v2.k);
36  X = (v1.j+v2.i);
37  break;
38  case 3:
39  //column 2
40  S = sqrtf( ( v1.k-(v1.i+v2.j) )+1 );
41  Z = 0.5*S;
42  S = .5/S;
43  W = (v2.i-v1.j);
44  X = (v1.k+v3.i);
45  Y = (v2.k+v3.j);
46  break;
47  }
48 #if 0
49  DEPRECATED
50  /*
51  * switch(max) {
52  * case 1:
53  * //column 0
54  * S = QSQRT( (v1.j - (v2.j + v3.k ))+1);
55  * Y = 0.5 * S;
56  * S = .5 / S;
57  * Z = (v1.j + v2.i ) * S;
58  * X = (v1.k + v3.i ) * S;
59  * W = (v2.k - v3.j ) * S;
60  *
61  * break;
62  * case 2:
63  * //column 1
64  * S = QSQRT( v2.k - (v3.k + v1.i )+1);
65  * Y = 0.5 * S;
66  * S = .5 / S;
67  * Z = (v3.j + v2.k ) * S;
68  * X = (v2.i + v1.j ) * S;
69  * W = (v3.i - v1.k ) * S;
70  * break;
71  * case 3:
72  * //column 2
73  * S = QSQRT( v3.i - (v1.i + v2.j )+1);
74  * Z = 0.5 * S;
75  * S = .5 / S;
76  * X = (v1.k + v3.i ) * S;
77  * Y = (v3.j + v2.k ) * S;
78  * W = (v1.j - v2.i ) * S;
79  * break;
80  * }
81  */
82 #endif
83  }
84  return Quaternion( W, Vector( X, Y, Z ) );
85 }
float Quaternion::Magnitude ( ) const
inline

Definition at line 30 of file quaternion.h.

References s, and v.

Referenced by Normalize().

31  {
32  return sqrtf( s*s+v.i*v.i+v.j*v.j+v.k*v.k );
33  }
void Quaternion::netswap ( )
inline

Definition at line 24 of file quaternion.h.

References s, v, and VSSwapHostFloatToLittle().

Referenced by Transformation::netswap().

25  {
27  v.netswap();
28  }
Quaternion& Quaternion::Normalize ( )
inline

Definition at line 43 of file quaternion.h.

References Magnitude(), s, and v.

Referenced by linear_interpolate_uncapped().

44  {
45  float mag = Magnitude();
46  v *= 1.0f / mag;
47  s /= mag;
48  return *this;
49  }
Quaternion Quaternion::operator* ( const Quaternion rval) const
inline

Definition at line 34 of file quaternion.h.

References DotProduct(), Quaternion(), s, and v.

35  {
36  return Quaternion( s*rval.s-DotProduct( v, rval.v ),
37  s*rval.v+rval.s*v+v.Cross( rval.v ) );
38  }
Quaternion& Quaternion::operator*= ( const Quaternion rval)
inline

Definition at line 39 of file quaternion.h.

40  {
41  return *this = *this*rval;
42  }
void Quaternion::to_matrix ( Matrix mat) const
inline

Definition at line 53 of file quaternion.h.

References e, float, M, s, and v.

Referenced by aim_assist(), aim_assist_debug(), ApplyQuaternion(), HoldPositionWithRespectTo(), ZoneMgr::isVisible(), Mount::NextMountCloser(), and Transformation::to_matrix().

54  {
55  const float GFXEPSILON = ( (float) 10e-6 );
56  float W = v.i*v.i+v.j*v.j+v.k*v.k+s*s; //norm
57  W = (W < 0+GFXEPSILON && W > 0-GFXEPSILON) ? 0 : 2.0/W;
58 
59  float xw = v.i*W;
60  float yw = v.j*W;
61  float zw = v.k*W;
62 
63  float sx = s*xw;
64  float sy = s*yw;
65  float sz = s*zw;
66 
67  float xx = v.i*xw;
68  float xy = v.i*yw;
69  float xz = v.i*zw;
70 
71  float yy = v.j*yw;
72  float yz = v.j*zw;
73  float zz = v.k*zw;
74 
75 #define M( B, A ) mat.r[B*3+A]
76 
77  M( 0, 0 ) = 1-(yy+zz);
78  M( 1, 0 ) = (xy+sz);
79  M( 2, 0 ) = (xz-sy);
80 
81  M( 0, 1 ) = (xy-sz);
82  M( 1, 1 ) = 1-(xx+zz);
83  M( 2, 1 ) = (yz+sx);
84 
85  M( 0, 2 ) = (xz+sy);
86  M( 1, 2 ) = (yz-sx);
87  M( 2, 2 ) = 1-(xx+yy);
88  //M(3,0) = M(3,1) = M(3,2) = M(0,3) = M(1,3) = M(2,3) = 0;
89  //M(3,3) = 1;
90 
91 #undef M
92 
93  }

Member Data Documentation


The documentation for this struct was generated from the following files: