Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Quaternion.h « MT « include « solid « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a925f21cd5d47270307fb28e273c5d8648c1a7bf (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * SOLID - Software Library for Interference Detection
 * 
 * Copyright (C) 2001-2003  Dtecta.  All rights reserved.
 *
 * This library may be distributed under the terms of the Q Public License
 * (QPL) as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * This library may be distributed and/or modified under the terms of the
 * GNU General Public License (GPL) version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * This library is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Commercial use or any other use of this library not covered by either 
 * the QPL or the GPL requires an additional license from Dtecta. 
 * Please contact info@dtecta.com for enquiries about the terms of commercial
 * use of this library.
 */

#ifndef QUATERNION_H
#define QUATERNION_H

#if defined (__sgi)
#include <assert.h>
#else
#include <cassert>
#endif

#include "Tuple4.h"
#include "Vector3.h"

namespace MT {

	template <typename Scalar>	
	class Quaternion : public Tuple4<Scalar> {
	public:
		Quaternion() {}
		
		template <typename Scalar2>
		explicit Quaternion(const Scalar2 *v) : Tuple4<Scalar>(v) {}
		
		template <typename Scalar2>
		Quaternion(const Scalar2& x, const Scalar2& y, const Scalar2& z, const Scalar2& w) 
			: Tuple4<Scalar>(x, y, z, w) 
		{}
		
		Quaternion(const Vector3<Scalar>& axis, const Scalar& angle) 
		{ 
			setRotation(axis, angle); 
		}

		template <typename Scalar2>
		Quaternion(const Scalar2& yaw, const Scalar2& pitch, const Scalar2& roll)
		{ 
			setEuler(yaw, pitch, roll); 
		}

		void setRotation(const Vector3<Scalar>& axis, const Scalar& angle)
		{
			Scalar d = axis.length();
			assert(d != Scalar(0.0));
			Scalar s = Scalar_traits<Scalar>::sin(angle * Scalar(0.5)) / d;
			setValue(axis[0] * s, axis[1] * s, axis[2] * s, 
					 Scalar_traits<Scalar>::cos(angle * Scalar(0.5)));
		}

		template <typename Scalar2>
		void setEuler(const Scalar2& yaw, const Scalar2& pitch, const Scalar2& roll)
		{
			Scalar halfYaw = Scalar(yaw) * Scalar(0.5);  
			Scalar halfPitch = Scalar(pitch) * Scalar(0.5);  
			Scalar halfRoll = Scalar(roll) * Scalar(0.5);  
			Scalar cosYaw = Scalar_traits<Scalar>::cos(halfYaw);
			Scalar sinYaw = Scalar_traits<Scalar>::sin(halfYaw);
			Scalar cosPitch = Scalar_traits<Scalar>::cos(halfPitch);
			Scalar sinPitch = Scalar_traits<Scalar>::sin(halfPitch);
			Scalar cosRoll = Scalar_traits<Scalar>::cos(halfRoll);
			Scalar sinRoll = Scalar_traits<Scalar>::sin(halfRoll);
			setValue(cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw,
					 cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw,
					 sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw,
					 cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw);
		}
  
		Quaternion<Scalar>& operator+=(const Quaternion<Scalar>& q)
		{
			this->m_co[0] += q[0]; this->m_co[1] += q[1]; this->m_co[2] += q[2]; this->m_co[3] += q[3];
			return *this;
		}
		
		Quaternion<Scalar>& operator-=(const Quaternion<Scalar>& q) 
		{
			this->m_co[0] -= q[0]; this->m_co[1] -= q[1]; this->m_co[2] -= q[2]; this->m_co[3] -= q[3];
			return *this;
		}

		Quaternion<Scalar>& operator*=(const Scalar& s)
		{
			this->m_co[0] *= s; this->m_co[1] *= s; this->m_co[2] *= s; this->m_co[3] *= s;
			return *this;
		}
		
		Quaternion<Scalar>& operator/=(const Scalar& s) 
		{
			assert(s != Scalar(0.0));
			return *this *= Scalar(1.0) / s;
		}
  
		Quaternion<Scalar>& operator*=(const Quaternion<Scalar>& q)
		{
			setValue(this->m_co[3] * q[0] + this->m_co[0] * q[3] + this->m_co[1] * q[2] - this->m_co[2] * q[1],
					 this->m_co[3] * q[1] + this->m_co[1] * q[3] + this->m_co[2] * q[0] - this->m_co[0] * q[2],
					 this->m_co[3] * q[2] + this->m_co[2] * q[3] + this->m_co[0] * q[1] - this->m_co[1] * q[0],
					 this->m_co[3] * q[3] - this->m_co[0] * q[0] - this->m_co[1] * q[1] - this->m_co[2] * q[2]);
			return *this;
		}
	
		Scalar dot(const Quaternion<Scalar>& q) const
		{
			return this->m_co[0] * q[0] + this->m_co[1] * q[1] + this->m_co[2] * q[2] + this->m_co[3] * q[3];
		}

		Scalar length2() const
		{
			return dot(*this);
		}

		Scalar length() const
		{
			return Scalar_traits<Scalar>::sqrt(length2());
		}

		Quaternion<Scalar>& normalize() 
		{
			return *this /= length();
		}
		
		Quaternion<Scalar> normalized() const 
		{
			return *this / length();
		} 

		Scalar angle(const Quaternion<Scalar>& q) const 
		{
			Scalar s = Scalar_traits<Scalar>::sqrt(length2() * q.length2());
			assert(s != Scalar(0.0));
			return Scalar_traits<Scalar>::acos(dot(q) / s);
		}
   
		Quaternion<Scalar> conjugate() const 
		{
			return Quaternion<Scalar>(-this->m_co[0], -this->m_co[1], -this->m_co[2], this->m_co[3]);
		}

		Quaternion<Scalar> inverse() const
		{
			return conjugate / length2();
		}
		
		Quaternion<Scalar> slerp(const Quaternion<Scalar>& q, const Scalar& t) const
		{
			Scalar theta = angle(q);
			if (theta != Scalar(0.0))
			{
				Scalar d = Scalar(1.0) / Scalar_traits<Scalar>::sin(theta);
				Scalar s0 = Scalar_traits<Scalar>::sin((Scalar(1.0) - t) * theta);
				Scalar s1 = Scalar_traits<Scalar>::sin(t * theta);   
				return Quaternion<Scalar>((this->m_co[0] * s0 + q[0] * s1) * d,
										  (this->m_co[1] * s0 + q[1] * s1) * d,
										  (this->m_co[2] * s0 + q[2] * s1) * d,
										  (this->m_co[3] * s0 + q[3] * s1) * d);
			}
			else
			{
				return *this;
			}
		}

		static Quaternion<Scalar> random() 
		{
			// From: "Uniform Random Rotations", Ken Shoemake, Graphics Gems III, 
            //       pg. 124-132
			Scalar x0 = Scalar_traits<Scalar>::random();
			Scalar r1 = Scalar_traits<Scalar>::sqrt(Scalar(1.0) - x0);
			Scalar r2 = Scalar_traits<Scalar>::sqrt(x0);
			Scalar t1 = Scalar_traits<Scalar>::TwoTimesPi() * Scalar_traits<Scalar>::random();
			Scalar t2 = Scalar_traits<Scalar>::TwoTimesPi() * Scalar_traits<Scalar>::random();
			Scalar c1 = Scalar_traits<Scalar>::cos(t1);
			Scalar s1 = Scalar_traits<Scalar>::sin(t1);
			Scalar c2 = Scalar_traits<Scalar>::cos(t2);
			Scalar s2 = Scalar_traits<Scalar>::sin(t2);
			return Quaternion<Scalar>(s1 * r1, c1 * r1, s2 * r2, c2 * r2);
		}

	};

	template <typename Scalar>
	inline Quaternion<Scalar>
	operator+(const Quaternion<Scalar>& q1, const Quaternion<Scalar>& q2)
	{
		return Quaternion<Scalar>(q1[0] + q2[0], q1[1] + q2[1], q1[2] + q2[2], q1[3] + q2[3]);
	}
	
	template <typename Scalar>
	inline Quaternion<Scalar>
	operator-(const Quaternion<Scalar>& q1, const Quaternion<Scalar>& q2)
	{
		return Quaternion<Scalar>(q1[0] - q2[0], q1[1] - q2[1], q1[2] - q2[2], q1[3] - q2[3]);
	}
	
	template <typename Scalar>
	inline Quaternion<Scalar>
	operator-(const Quaternion<Scalar>& q)
	{
		return Quaternion<Scalar>(-q[0], -q[1], -q[2], -q[3]);
	}

	template <typename Scalar>
	inline Quaternion<Scalar>
	operator*(const Quaternion<Scalar>& q, const Scalar& s)
	{
		return Quaternion<Scalar>(q[0] * s, q[1] * s, q[2] * s, q[3] * s);
	}
	
	template <typename Scalar>
	inline Quaternion<Scalar>
	operator*(const Scalar& s, const Quaternion<Scalar>& q)
	{
		return q * s;
	}
	
	template <typename Scalar>
	inline Quaternion<Scalar>
	operator*(const Quaternion<Scalar>& q1, const Quaternion<Scalar>& q2) {
		return Quaternion<Scalar>(q1[3] * q2[0] + q1[0] * q2[3] + q1[1] * q2[2] - q1[2] * q2[1],
								  q1[3] * q2[1] + q1[1] * q2[3] + q1[2] * q2[0] - q1[0] * q2[2],
								  q1[3] * q2[2] + q1[2] * q2[3] + q1[0] * q2[1] - q1[1] * q2[0],
								  q1[3] * q2[3] - q1[0] * q2[0] - q1[1] * q2[1] - q1[2] * q2[2]); 
	}
	
	template <typename Scalar>
	inline Quaternion<Scalar>
	operator*(const Quaternion<Scalar>& q, const Vector3<Scalar>& w)
	{
		return Quaternion<Scalar>( q[3] * w[0] + q[1] * w[2] - q[2] * w[1],
								   q[3] * w[1] + q[2] * w[0] - q[0] * w[2],
								   q[3] * w[2] + q[0] * w[1] - q[1] * w[0],
								  -q[0] * w[0] - q[1] * w[1] - q[2] * w[2]); 
	}
	
	template <typename Scalar>
	inline Quaternion<Scalar>
	operator*(const Vector3<Scalar>& w, const Quaternion<Scalar>& q)
	{
		return Quaternion<Scalar>( w[0] * q[3] + w[1] * q[2] - w[2] * q[1],
								   w[1] * q[3] + w[2] * q[0] - w[0] * q[2],
								   w[2] * q[3] + w[0] * q[1] - w[1] * q[0],
								  -w[0] * q[0] - w[1] * q[1] - w[2] * q[2]); 
	}
	
	template <typename Scalar>
	inline Scalar 
	dot(const Quaternion<Scalar>& q1, const Quaternion<Scalar>& q2) 
	{ 
		return q1.dot(q2); 
	}

	template <typename Scalar>
	inline Scalar
	length2(const Quaternion<Scalar>& q) 
	{ 
		return q.length2(); 
	}

	template <typename Scalar>
	inline Scalar
	length(const Quaternion<Scalar>& q) 
	{ 
		return q.length(); 
	}

	template <typename Scalar>
	inline Scalar
	angle(const Quaternion<Scalar>& q1, const Quaternion<Scalar>& q2) 
	{ 
		return q1.angle(q2); 
	}

	template <typename Scalar>
	inline Quaternion<Scalar>
	conjugate(const Quaternion<Scalar>& q) 
	{
		return q.conjugate();
	}

	template <typename Scalar>
	inline Quaternion<Scalar>
	inverse(const Quaternion<Scalar>& q) 
	{
		return q.inverse();
	}

	template <typename Scalar>
	inline Quaternion<Scalar>
	slerp(const Quaternion<Scalar>& q1, const Quaternion<Scalar>& q2, const Scalar& t) 
	{
		return q1.slerp(q2, t);
	}
	
}

#endif