Jamoma API  0.6.0.a19
OrientationDataspace.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup foundationDataspaceLib
4  *
5  * @brief The #OrientationDataspace converts between different untis describing orientation.
6  *
7  * @details The neutral unit of the orientation dataspace is _quarternian_. @n
8  * @n
9  * The unit conventinos are based on SpinCalc for Matlab: http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-orientation-quaternions-and-euler-vectors
10  *
11  * @authors Nils Peters, Trond Lossius, Tim Place, ...
12  *
13  * @copyright Copyright © 2011 by Nils Peters @n
14  * This code is licensed under the terms of the "New BSD License" @n
15  * http://creativecommons.org/licenses/BSD/
16  */
17 
18 
19 #include "OrientationDataspace.h"
20 
21 
22 #define thisTTClass QuaternionUnit
23 #define thisTTClassName "unit.quaternion"
24 #define thisTTClassTags "dataspace.unit, orientation"
25 
27 TTDataspaceUnit(arguments)
28 {;}
29 
30 QuaternionUnit::~QuaternionUnit(){;}
31 
33 {
34  //output.resize(4);
35  output = input;
36 }
37 
38 
40 {
41  //output.resize(4);
42  output = input;
43 }
44 
45 
46 #undef thisTTClass
47 #undef thisTTClassName
48 #undef thisTTClassTags
49 
50 /***********************************************************************************************/
51 
52 #define thisTTClass EulerUnit
53 #define thisTTClassName "unit.euler"
54 #define thisTTClassTags "dataspace.unit, orientation"
55 
57 TTDataspaceUnit(arguments)
58 {;}
59 
60 EulerUnit::~EulerUnit(){;}
61 //yaw pitch roll
62 void EulerUnit::convertToNeutral(const TTValue& input, TTValue& output)
63 {
64  TTFloat64 yaw, pitch, roll;
65 
66  yaw = input[0];
67  yaw = yaw * kTTDegreesToRadians * -0.5;
68  pitch = input[1];
69  pitch = pitch * kTTDegreesToRadians * 0.5;
70  roll = input[2];
71  roll = roll * kTTDegreesToRadians * 0.5;
72 
73 
74  TTFloat64 sinYaw(sin(yaw));
75  TTFloat64 cosYaw(cos(yaw));
76  TTFloat64 sinPitch(sin(pitch));
77  TTFloat64 cosPitch(cos(pitch));
78  TTFloat64 sinRoll(sin(roll));
79  TTFloat64 cosRoll(cos(roll));
80  TTFloat64 cosPitchCosRoll(cosPitch*cosRoll);
81  TTFloat64 sinPitchSinRoll(sinPitch*sinRoll);
82 
83  output.resize(4);
84  output[0] = cosYaw * sinPitch * cosRoll - sinYaw * cosPitch * sinRoll; //X
85  output[1] = cosYaw * cosPitch * sinRoll + sinYaw * sinPitch * cosRoll; //Y
86  output[2] = sinYaw * cosPitchCosRoll + cosYaw * sinPitchSinRoll; //Z
87  output[3] = cosYaw * cosPitchCosRoll - sinYaw * sinPitchSinRoll; //W
88 
89 
90 
91 
92 }
93 
94 void EulerUnit::convertFromNeutral(const TTValue& input, TTValue& output)
95 {
96  TTFloat64 W, X, Y, Z;
97 
98  X = input[0];
99  Y = input[1];
100  Z = input[2];
101  W = input[3];
102 
103  output.resize(3);
104  output[0] = kTTRadiansToDegrees * atan2(-2*(Z*W-X*Y), W*W - X*X + Y*Y - Z*Z); //yaw
105  output[1] = kTTRadiansToDegrees * asin(2*(W*X + Y*Z)); //pitch
106  output[2] = kTTRadiansToDegrees * atan2(2*(W*Y + X*Z), W*W - X*X - Y*Y + Z*Z); //roll
107 }
108 
109 
110 #undef thisTTClass
111 #undef thisTTClassName
112 #undef thisTTClassTags
113 
114 /***********************************************************************************************/
115 
116 #define thisTTClass AxisUnit
117 #define thisTTClassName "unit.axis"
118 #define thisTTClassTags "dataspace.unit, orientation"
119 
121 TTDataspaceUnit(arguments)
122 {;}
123 
124 AxisUnit::~AxisUnit(){;}
125 
126 void AxisUnit::convertToNeutral(const TTValue& input, TTValue& output)
127 {
128  TTFloat64 angle, x, y, z;
129  TTFloat64 sinAngle, n;
130 
131 
132  x = input[0];
133  y = input[1];
134  z = input[2];
135  angle = input[3];
136  angle = angle * kTTDegreesToRadians * 0.5;
137  sinAngle = sin(angle);
138 
139  //vector normalization:
140  n = sqrt(x*x + y*y + z*z);
141 
142  if (n > 0.0)
143  n = 1.0/n;
144 
145  /* x = x * n;
146  y = y * n;
147  z = z * n; */
148 
149  output.resize(4);
150  output[0] = x * n * sinAngle; //X
151  output[1] = y * n * sinAngle; //Y
152  output[2] = z * n * sinAngle; //Z
153  output[3] = cos(angle); //W
154 }
155 
156 void AxisUnit::convertFromNeutral(const TTValue& input, TTValue& output)
157 {
158  TTFloat64 X, Y, Z, W;
159 
160  X = input[0];
161  Y = input[1];
162  Z = input[2];
163  W = input[3];
164 
165 
166  TTFloat64 sin_a = sqrt( 1.0 - W * W );
167 
168  output.resize(4);
169  output[3] = kTTRadiansToDegrees * 2.0 * atan2(sin_a, W); //angle
170 
171  if ( fabs( sin_a ) < 0.0005 )
172  sin_a = 1.0;
173  else sin_a = 1.0/sin_a;
174 
175  output[0] = X * sin_a; //x
176  output[1] = Y * sin_a; //y
177  output[2] = Z * sin_a; //z
178 }
179 
180 
181 #undef thisTTClass
182 #undef thisTTClassName
183 #undef thisTTClassTags
184 
185 /***********************************************************************************************/
186 
187 #define thisTTClass OrientationDataspace
188 #define thisTTClassName "dataspace.orientation"
189 #define thisTTClassTags "foundationDataspaceLib, dataspace, orientation"
190 
192 {
193  // Create one of each kind of unit, and cache them in a hash
194  registerUnit(TT("unit.euler"), TT("euler"));
195  registerUnit(TT("unit.euler"), TT("ypr"));
196  registerUnit(TT("unit.axis"), TT("axis"));
197  registerUnit(TT("unit.axis"), TT("xyza"));
198  registerUnit(TT("unit.quaternion"), TT("quaternion"));
199  registerUnit(TT("unit.quaternion"), TT("quat"));
200 
201  // Set our neutral unit (the unit through which all conversions are made)
202  neutralUnit = TT("quaternion");
203 
204  // Now that the cache is created, we can create a set of default units
205  setInputUnit(neutralUnit);
206  setOutputUnit(neutralUnit);
207 }
208 
209 
210 OrientationDataspace::~OrientationDataspace()
211 {
212  ;
213 }
214 
215 #undef thisTTClass
216 #undef thisTTClassName
217 #undef thisTTClassTags
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from axis unit to neutral unit.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to Euler.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTFOUNDATION_EXPORT const TTFloat64 kTTRadiansToDegrees
Factor constant for converting radians to degrees.
Definition: TTBase.cpp:32
Specification for the base class of each DataspaceUnit.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to axis unit.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from Euler to neutral unit.
TTFOUNDATION_EXPORT const TTFloat64 kTTDegreesToRadians
Factor constant for converting degrees to radians.
Definition: TTBase.cpp:33
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to quarternian.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from quarternian to neutral unit.
TT_OBJECT_CONSTRUCTOR
Constructor macro.
void resize(size_type n)
Change the number of elements.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
The OrientationDataspace converts between different untis describing orientation. ...