1 // Created on: 2010-05-11
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2010-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 // Note: implementation is based on free samples from
17 // http://www.gamedev.ru/code/articles/?id=4215&page=3
18 // and maths found in Wikipedia and elsewhere
20 #include <gp_Quaternion.hxx>
24 //=======================================================================
27 //=======================================================================
29 Standard_Boolean gp_Quaternion::IsEqual (const gp_Quaternion& theOther) const
31 if (this == &theOther)
33 return Abs (x - theOther.x) <= gp::Resolution() &&
34 Abs (y - theOther.y) <= gp::Resolution() &&
35 Abs (z - theOther.z) <= gp::Resolution() &&
36 Abs (w - theOther.w) <= gp::Resolution();
39 //=======================================================================
40 //function : SetRotation
42 //=======================================================================
44 void gp_Quaternion::SetRotation (const gp_Vec& theVecFrom, const gp_Vec& theVecTo)
46 gp_Vec aVecCross (theVecFrom.Crossed (theVecTo));
47 Set (aVecCross.X(), aVecCross.Y(), aVecCross.Z(), theVecFrom.Dot (theVecTo));
48 Normalize(); // if "from" or "to" not unit, normalize quat
49 w += 1.0; // reducing angle to halfangle
50 if (w <= gp::Resolution()) // angle close to PI
52 if ((theVecFrom.Z() * theVecFrom.Z()) > (theVecFrom.X() * theVecFrom.X()))
53 Set ( 0.0, theVecFrom.Z(), -theVecFrom.Y(), w); // theVecFrom * gp_Vec(1,0,0)
55 Set (theVecFrom.Y(), -theVecFrom.X(), 0.0, w); // theVecFrom * gp_Vec(0,0,1)
60 //=======================================================================
61 //function : SetRotation
63 //=======================================================================
65 void gp_Quaternion::SetRotation (const gp_Vec& theVecFrom, const gp_Vec& theVecTo, const gp_Vec& theHelpCrossVec)
67 gp_Vec aVecCross (theVecFrom.Crossed (theVecTo));
68 Set (aVecCross.X(), aVecCross.Y(), aVecCross.Z(), theVecFrom.Dot (theVecTo));
69 Normalize(); // if "from" or "to" not unit, normalize quat
70 w += 1.0; // reducing angle to halfangle
71 if (w <= gp::Resolution()) // angle close to PI
73 gp_Vec theAxis = theVecFrom.Crossed (theHelpCrossVec);
74 Set (theAxis.X(), theAxis.Y(), theAxis.Z(), w);
79 //=======================================================================
80 //function : SetVectorAndAngle
82 //=======================================================================
84 void gp_Quaternion::SetVectorAndAngle (const gp_Vec& theAxis, const Standard_Real theAngle)
86 gp_Vec anAxis = theAxis.Normalized();
87 Standard_Real anAngleHalf = 0.5 * theAngle;
88 Standard_Real sin_a = Sin (anAngleHalf);
89 Set (anAxis.X() * sin_a, anAxis.Y() * sin_a, anAxis.Z() * sin_a, Cos (anAngleHalf));
92 //=======================================================================
93 //function : GetVectorAndAngle
95 //=======================================================================
97 void gp_Quaternion::GetVectorAndAngle (gp_Vec& theAxis, Standard_Real& theAngle) const
99 Standard_Real vl = Sqrt (x * x + y * y + z * z);
100 if (vl > gp::Resolution())
102 Standard_Real ivl = 1.0 / vl;
103 theAxis.SetCoord (x * ivl, y * ivl, z * ivl);
106 theAngle = 2.0 * ATan2 (-vl, -w); // [-PI, 0]
110 theAngle = 2.0 * ATan2 ( vl, w); // [ 0, PI]
115 theAxis.SetCoord (0.0, 0.0, 1.0);
120 //=======================================================================
121 //function : SetMatrix
123 //=======================================================================
125 void gp_Quaternion::SetMatrix (const gp_Mat& theMat)
127 Standard_Real tr = theMat (1, 1) + theMat (2, 2) + theMat(3, 3); // trace of martix
129 { // if trace positive than "w" is biggest component
130 Set (theMat (3, 2) - theMat (2, 3),
131 theMat (1, 3) - theMat (3, 1),
132 theMat (2, 1) - theMat (1, 2),
134 Scale (0.5 / Sqrt (w)); // "w" contain the "norm * 4"
136 else if ((theMat (1, 1) > theMat (2, 2)) && (theMat (1, 1) > theMat (3, 3)))
137 { // Some of vector components is bigger
138 Set (1.0 + theMat (1, 1) - theMat (2, 2) - theMat (3, 3),
139 theMat (1, 2) + theMat (2, 1),
140 theMat (1, 3) + theMat (3, 1),
141 theMat (3, 2) - theMat (2, 3));
142 Scale (0.5 / Sqrt (x));
144 else if (theMat (2, 2) > theMat (3, 3))
146 Set (theMat (1, 2) + theMat (2, 1),
147 1.0 + theMat (2, 2) - theMat (1, 1) - theMat (3, 3),
148 theMat (2, 3) + theMat (3, 2),
149 theMat (1, 3) - theMat (3, 1));
150 Scale (0.5 / Sqrt (y));
154 Set (theMat (1, 3) + theMat (3, 1),
155 theMat (2, 3) + theMat (3, 2),
156 1.0 + theMat (3, 3) - theMat (1, 1) - theMat (2, 2),
157 theMat (2, 1) - theMat (1, 2));
158 Scale (0.5 / Sqrt (z));
162 //=======================================================================
163 //function : GetMatrix
165 //=======================================================================
167 gp_Mat gp_Quaternion::GetMatrix () const
169 Standard_Real wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
170 Standard_Real s = 2.0 / SquareNorm();
171 x2 = x * s; y2 = y * s; z2 = z * s;
172 xx = x * x2; xy = x * y2; xz = x * z2;
173 yy = y * y2; yz = y * z2; zz = z * z2;
174 wx = w * x2; wy = w * y2; wz = w * z2;
178 aMat (1, 1) = 1.0 - (yy + zz);
179 aMat (1, 2) = xy - wz;
180 aMat (1, 3) = xz + wy;
182 aMat (2, 1) = xy + wz;
183 aMat (2, 2) = 1.0 - (xx + zz);
184 aMat (2, 3) = yz - wx;
186 aMat (3, 1) = xz - wy;
187 aMat (3, 2) = yz + wx;
188 aMat (3, 3) = 1.0 - (xx + yy);
189 // 1 division 16 multiplications 15 addidtions 12 variables
194 namespace { // anonymous namespace
196 //=======================================================================
197 //function : translateEulerSequence
199 // Code supporting conversion between quaternion and generalized
200 // Euler angles (sequence of three rotations) is based on
201 // algorithm by Ken Shoemake, published in Graphics Gems IV, p. 222-22
202 // http://tog.acm.org/resources/GraphicsGems/gemsiv/euler_angle/EulerAngles.c
203 //=======================================================================
205 struct gp_EulerSequence_Parameters
207 Standard_Integer i; // first rotation axis
208 Standard_Integer j; // next axis of rotation
209 Standard_Integer k; // third axis
210 Standard_Boolean isOdd; // true if order of two first rotation axes is odd permutation, e.g. XZ
211 Standard_Boolean isTwoAxes; // true if third rotation is about the same axis as first
212 Standard_Boolean isExtrinsic; // true if rotations are made around fixed axes
214 gp_EulerSequence_Parameters (Standard_Integer theAx1,
215 Standard_Boolean theisOdd,
216 Standard_Boolean theisTwoAxes,
217 Standard_Boolean theisExtrinsic)
219 j(1 + (theAx1 + (theisOdd ? 1 : 0)) % 3),
220 k(1 + (theAx1 + (theisOdd ? 0 : 1)) % 3),
222 isTwoAxes(theisTwoAxes),
223 isExtrinsic(theisExtrinsic)
227 gp_EulerSequence_Parameters translateEulerSequence (const gp_EulerSequence theSeq)
229 typedef gp_EulerSequence_Parameters Params;
230 const Standard_Boolean F = Standard_False;
231 const Standard_Boolean T = Standard_True;
235 case gp_Extrinsic_XYZ: return Params (1, F, F, T);
236 case gp_Extrinsic_XZY: return Params (1, T, F, T);
237 case gp_Extrinsic_YZX: return Params (2, F, F, T);
238 case gp_Extrinsic_YXZ: return Params (2, T, F, T);
239 case gp_Extrinsic_ZXY: return Params (3, F, F, T);
240 case gp_Extrinsic_ZYX: return Params (3, T, F, T);
242 // Conversion of intrinsic angles is made by the same code as for extrinsic,
243 // using equivalence rule: intrinsic rotation is equivalent to extrinsic
244 // rotation by the same angles but with inverted order of elemental rotations.
245 // Swapping of angles (Alpha <-> Gamma) is done inside conversion procedure;
246 // sequence of axes is inverted by setting appropriate parameters here.
247 // Note that proper Euler angles (last block below) are symmetric for sequence of axes.
248 case gp_Intrinsic_XYZ: return Params (3, T, F, F);
249 case gp_Intrinsic_XZY: return Params (2, F, F, F);
250 case gp_Intrinsic_YZX: return Params (1, T, F, F);
251 case gp_Intrinsic_YXZ: return Params (3, F, F, F);
252 case gp_Intrinsic_ZXY: return Params (2, T, F, F);
253 case gp_Intrinsic_ZYX: return Params (1, F, F, F);
255 case gp_Extrinsic_XYX: return Params (1, F, T, T);
256 case gp_Extrinsic_XZX: return Params (1, T, T, T);
257 case gp_Extrinsic_YZY: return Params (2, F, T, T);
258 case gp_Extrinsic_YXY: return Params (2, T, T, T);
259 case gp_Extrinsic_ZXZ: return Params (3, F, T, T);
260 case gp_Extrinsic_ZYZ: return Params (3, T, T, T);
262 case gp_Intrinsic_XYX: return Params (1, F, T, F);
263 case gp_Intrinsic_XZX: return Params (1, T, T, F);
264 case gp_Intrinsic_YZY: return Params (2, F, T, F);
265 case gp_Intrinsic_YXY: return Params (2, T, T, F);
266 case gp_Intrinsic_ZXZ: return Params (3, F, T, F);
267 case gp_Intrinsic_ZYZ: return Params (3, T, T, F);
270 case gp_EulerAngles : return Params (3, F, T, F); // = Intrinsic_ZXZ
271 case gp_YawPitchRoll: return Params (1, F, F, F); // = Intrinsic_ZYX
275 } // anonymous namespace
277 //=======================================================================
278 //function : SetEulerAngles
280 //=======================================================================
282 void gp_Quaternion::SetEulerAngles (const gp_EulerSequence theOrder,
283 const Standard_Real theAlpha,
284 const Standard_Real theBeta,
285 const Standard_Real theGamma)
287 gp_EulerSequence_Parameters o = translateEulerSequence (theOrder);
289 Standard_Real a = theAlpha, b = theBeta, c = theGamma;
290 if ( ! o.isExtrinsic )
298 Standard_Real ti = 0.5 * a;
299 Standard_Real tj = 0.5 * b;
300 Standard_Real th = 0.5 * c;
301 Standard_Real ci = Cos (ti);
302 Standard_Real cj = Cos (tj);
303 Standard_Real ch = Cos (th);
304 Standard_Real si = Sin (ti);
305 Standard_Real sj = Sin (tj);
306 Standard_Real sh = Sin (th);
307 Standard_Real cc = ci * ch;
308 Standard_Real cs = ci * sh;
309 Standard_Real sc = si * ch;
310 Standard_Real ss = si * sh;
312 Standard_Real values[4]; // w, x, y, z
315 values[o.i] = cj * (cs + sc);
316 values[o.j] = sj * (cc + ss);
317 values[o.k] = sj * (cs - sc);
318 values[0] = cj * (cc - ss);
322 values[o.i] = cj * sc - sj * cs;
323 values[o.j] = cj * ss + sj * cc;
324 values[o.k] = cj * cs - sj * sc;
325 values[0] = cj * cc + sj * ss;
328 values[o.j] = -values[o.j];
336 //=======================================================================
337 //function : GetEulerAngles
339 //=======================================================================
341 void gp_Quaternion::GetEulerAngles (const gp_EulerSequence theOrder,
342 Standard_Real& theAlpha,
343 Standard_Real& theBeta,
344 Standard_Real& theGamma) const
346 gp_Mat M = GetMatrix();
348 gp_EulerSequence_Parameters o = translateEulerSequence (theOrder);
351 double sy = sqrt (M(o.i, o.j) * M(o.i, o.j) + M(o.i, o.k) * M(o.i, o.k));
352 if (sy > 16 * DBL_EPSILON)
354 theAlpha = ATan2 (M(o.i, o.j), M(o.i, o.k));
355 theGamma = ATan2 (M(o.j, o.i), -M(o.k, o.i));
359 theAlpha = ATan2 (-M(o.j, o.k), M(o.j, o.j));
362 theBeta = ATan2 (sy, M(o.i, o.i));
366 double cy = sqrt (M(o.i, o.i) * M(o.i, o.i) + M(o.j, o.i) * M(o.j, o.i));
367 if (cy > 16 * DBL_EPSILON)
369 theAlpha = ATan2 (M(o.k, o.j), M(o.k, o.k));
370 theGamma = ATan2 (M(o.j, o.i), M(o.i, o.i));
374 theAlpha = ATan2 (-M(o.j, o.k), M(o.j, o.j));
377 theBeta = ATan2 (-M(o.k, o.i), cy);
381 theAlpha = -theAlpha;
383 theGamma = -theGamma;
385 if ( ! o.isExtrinsic )
387 Standard_Real aFirst = theAlpha;
393 //=======================================================================
394 //function : StabilizeLength
396 //=======================================================================
398 void gp_Quaternion::StabilizeLength()
400 Standard_Real cs = Abs (x) + Abs (y) + Abs (z) + Abs (w);
403 x /= cs; y /= cs; z /= cs; w /= cs;
411 //=======================================================================
412 //function : Normalize
414 //=======================================================================
416 void gp_Quaternion::Normalize()
418 Standard_Real aMagn = Norm();
419 if (aMagn < gp::Resolution())
427 //=======================================================================
428 //function : Normalize
430 //=======================================================================
432 Standard_Real gp_Quaternion::GetRotationAngle() const
436 return 2.0 * ATan2 (-Sqrt (x * x + y * y + z * z), -w);
440 return 2.0 * ATan2 ( Sqrt (x * x + y * y + z * z), w);
444 //=======================================================================
445 //function : Multiply
447 //=======================================================================
449 gp_Vec gp_Quaternion::Multiply (const gp_Vec& theVec) const
451 gp_Quaternion theQ (theVec.X() * w + theVec.Z() * y - theVec.Y() * z,
452 theVec.Y() * w + theVec.X() * z - theVec.Z() * x,
453 theVec.Z() * w + theVec.Y() * x - theVec.X() * y,
454 theVec.X() * x + theVec.Y() * y + theVec.Z() * z);
455 return gp_Vec (w * theQ.x + x * theQ.w + y * theQ.z - z * theQ.y,
456 w * theQ.y + y * theQ.w + z * theQ.x - x * theQ.z,
457 w * theQ.z + z * theQ.w + x * theQ.y - y * theQ.x) * (1.0 / SquareNorm());