Blame view

legacy/rtsQuaternion.h 2.94 KB
f1402849   dmayerich   renewed commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  #include "rtsMatrix4x4.h"

  

  #ifndef RTS_QUATERNION_H

  #define RTS_QUATERNION_H

  

  template<typename T>

  class rtsQuaternion

  {

  public:

  	T w;

  	T x;

  	T y;

  	T z;

  

  	void normalize();

  	void CreateRotation(T theta, T axis_x, T axis_y, T axis_z);

  	rtsQuaternion<T> operator*(rtsQuaternion<T> &rhs);

  	matrix4x4<T> toMatrix();

  	

  

  	rtsQuaternion();

  	rtsQuaternion(T w, T x, T y, T z);

  

  };

  

  template<typename T>

  void rtsQuaternion<T>::normalize()

  {

  	double length=sqrt(w*w + x*x + y*y + z*z);

  	w=w/length;

  	x=x/length;

  	y=y/length;

  	z=z/length;

  }

  

  template<typename T>

  void rtsQuaternion<T>::CreateRotation(T theta, T axis_x, T axis_y, T axis_z)

  {

  	//assign the given Euler rotation to this quaternion

  	w = cos(theta/2.0);

  	x = axis_x*sin(theta/2.0);

  	y = axis_y*sin(theta/2.0);

  	z = axis_z*sin(theta/2.0);

  

  

  }

  

  template<typename T>

  rtsQuaternion<T> rtsQuaternion<T>::operator *(rtsQuaternion<T> &param)

  {

  	float A, B, C, D, E, F, G, H;

  

  

  	A = (w + x)*(param.w + param.x);

  	B = (z - y)*(param.y - param.z);

  	C = (w - x)*(param.y + param.z); 

  	D = (y + z)*(param.w - param.x);

  	E = (x + z)*(param.x + param.y);

  	F = (x - z)*(param.x - param.y);

  	G = (w + y)*(param.w - param.z);

  	H = (w - y)*(param.w + param.z);

  

  	rtsQuaternion<T> result;

  	result.w = B + (-E - F + G + H) /2;

  	result.x = A - (E + F + G + H)/2; 

  	result.y = C + (E - F + G - H)/2; 

  	result.z = D + (E - F - G + H)/2;

  

  	return result;

  }

  

  template<typename T>

  matrix4x4<T> rtsQuaternion<T>::toMatrix()

  {

  	matrix4x4<T> result;

  

  

      double wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;

  

  

      // calculate coefficients

      x2 = x + x; y2 = y + y;

      z2 = z + z;

      xx = x * x2; xy = x * y2; xz = x * z2;

      yy = y * y2; yz = y * z2; zz = z * z2;

      wx = w * x2; wy = w * y2; wz = w * z2;

  

  	result(0, 0) = 1.0 - (yy + zz);

  	result(0, 1) = xy - wz;

      //m[0][0] = 1.0 - (yy + zz); m[1][0] = xy - wz;

  

  	result(0, 2) = xz + wy;

  	result(0, 3) = 0.0;

      //m[2][0] = xz + wy; m[3][0] = 0.0;

  

  	result(1, 0) = xy + wz;

  	result(1, 1) = 1.0 - (xx + zz);

      //m[0][1] = xy + wz; m[1][1] = 1.0 - (xx + zz);

  

  	result(1, 2) = yz - wx;

  	result(1, 3) = 0.0;

      //m[2][1] = yz - wx; m[3][1] = 0.0;

  

  	result(2, 0) = xz - wy;

  	result(2, 1) = yz + wx;

      //m[0][2] = xz - wy; m[1][2] = yz + wx;

  

  	result(2, 2) = 1.0 - (xx + yy);

  	result(3, 2) = 0.0;

      //m[2][2] = 1.0 - (xx + yy); m[3][2] = 0.0;

  

  	result(3, 0) = 0.0;

  	result(3, 1) = 0.0;

  	result(3, 2) = 0.0;

  	result(3, 3) = 1.0;

      //m[0][3] = 0; m[1][3] = 0;

      //m[2][3] = 0; m[3][3] = 1;

  	/*

  	double* orientationmatrix=(double*)m;

  	char c;

  

  

  	double* result=new double[16];

  	double* array=(double*)m;

  	for(int i=0; i<16; i++)

  		result[i]=array[i];

  	*/

  

  	return result;

  }

  

  template<typename T>

  rtsQuaternion<T>::rtsQuaternion()

  {

  	w=0.0; x=0.0; y=0.0; z=0.0;

  }

  

  template<typename T>

  rtsQuaternion<T>::rtsQuaternion(T c, T i, T j, T k)

  {

  	w=c;  x=i;  y=j;  z=k;

  }

  

  #endif