973c2be1 |
1 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
2 | // |
973c2be1 |
3 | // This file is part of Open CASCADE Technology software library. |
b311480e |
4 | // |
d5f74e42 |
5 | // This library is free software; you can redistribute it and/or modify it under |
6 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
7 | // by the Free Software Foundation, with special exception defined in the file |
8 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
9 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
10 | // |
973c2be1 |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
b311480e |
13 | |
7fd59977 |
14 | #ifndef _gp_QuaternionSLerp_HeaderFile |
15 | #define _gp_QuaternionSLerp_HeaderFile |
16 | |
17 | #include <gp_Quaternion.hxx> |
18 | |
1beb58d7 |
19 | //! Perform Spherical Linear Interpolation of the quaternions, |
20 | //! return unit length quaternion. |
7fd59977 |
21 | class gp_QuaternionSLerp |
22 | { |
1beb58d7 |
23 | public: |
24 | |
25 | //! Compute interpolated quaternion between two quaternions. |
26 | //! @param theStart first quaternion |
27 | //! @param theEnd second quaternion |
28 | //! @param theT normalized interpolation coefficient within 0..1 range, |
29 | //! with 0 pointing to theStart and 1 to theEnd. |
30 | static gp_Quaternion Interpolate (const gp_Quaternion& theQStart, |
31 | const gp_Quaternion& theQEnd, |
32 | Standard_Real theT) |
33 | { |
34 | gp_Quaternion aResult; |
35 | gp_QuaternionSLerp aLerp (theQStart, theQEnd); |
36 | aLerp.Interpolate (theT, aResult); |
37 | return aResult; |
38 | } |
7fd59977 |
39 | |
40 | public: |
41 | |
1beb58d7 |
42 | //! Empty constructor, |
7fd59977 |
43 | gp_QuaternionSLerp() {} |
44 | |
1beb58d7 |
45 | //! Constructor with initialization. |
7fd59977 |
46 | gp_QuaternionSLerp (const gp_Quaternion& theQStart, const gp_Quaternion& theQEnd) |
47 | { |
48 | Init (theQStart, theQEnd); |
49 | } |
50 | |
1beb58d7 |
51 | //! Initialize the tool with Start and End values. |
7fd59977 |
52 | void Init (const gp_Quaternion& theQStart, const gp_Quaternion& theQEnd) |
53 | { |
54 | InitFromUnit (theQStart.Normalized(), theQEnd.Normalized()); |
55 | } |
56 | |
1beb58d7 |
57 | //! Initialize the tool with Start and End unit quaternions. |
7fd59977 |
58 | void InitFromUnit (const gp_Quaternion& theQStart, const gp_Quaternion& theQEnd) |
59 | { |
60 | myQStart = theQStart; |
61 | myQEnd = theQEnd; |
62 | Standard_Real cosOmega = myQStart.Dot (myQEnd); |
63 | if (cosOmega < 0.0) |
64 | { |
65 | cosOmega = -cosOmega; |
66 | myQEnd = -myQEnd; |
67 | } |
68 | if (cosOmega > 0.9999) |
69 | { |
70 | cosOmega = 0.9999; |
71 | } |
72 | myOmega = ACos (cosOmega); |
73 | Standard_Real invSinOmega = (1.0 / Sin (myOmega)); |
74 | myQStart.Scale (invSinOmega); |
75 | myQEnd.Scale (invSinOmega); |
76 | } |
77 | |
78 | //! Set interpolated quaternion for theT position (from 0.0 to 1.0) |
79 | void Interpolate (Standard_Real theT, gp_Quaternion& theResultQ) const |
80 | { |
81 | theResultQ = myQStart * Sin((1.0 - theT) * myOmega) + myQEnd * Sin (theT * myOmega); |
82 | } |
83 | |
84 | private: |
85 | |
86 | gp_Quaternion myQStart; |
87 | gp_Quaternion myQEnd; |
88 | Standard_Real myOmega; |
89 | |
90 | }; |
91 | |
92 | #endif //_gp_QuaternionSLerp_HeaderFile |