gForceSDK
Quaternion.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017, OYMotion Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28  * DAMAGE.
29  *
30  */
38 #pragma once
39 
40 #include <cmath>
41 #include <sstream>
42 
43 namespace gf
44 {
45 
50  class Euler
51  {
52  public:
55  Euler() {}
56 
59  Euler(float pitch, float roll, float yaw)
60  : mPitch(pitch)
61  , mRoll(roll)
62  , mYaw(yaw)
63  {
64  }
65 
69  float pitch() const { return mPitch; }
70 
74  float roll() const { return mRoll; }
75 
79  float yaw() const { return mYaw; }
80 
84  std::string toString() const
85  {
86  std::ostringstream stm;
87  stm << "pitch: " << mPitch << ", roll: " << mRoll << ", yaw: " << mYaw;
88  return stm.str();
89  }
90 
91  private:
92  float mPitch = 0;
93  float mRoll = 0;
94  float mYaw = 0;
95  };
96 
102  {
103  public:
107 
110  Quaternion(float w, float x, float y, float z)
111  : mW(w)
112  , mX(x)
113  , mY(y)
114  , mZ(z)
115  {
116  }
117 
121  float w() const { return mW; }
122 
126  float x() const { return mX; }
127 
131  float y() const { return mY; }
132 
136  float z() const { return mZ; }
137 
141  std::string toString() const
142  {
143  std::ostringstream stm;
144  stm << "w: " << mW << ", x: " << mX << ", y: " << mY << ", z: " << mZ;
145  return stm.str();
146  }
147 
148  public:
152  Euler toEuler() const
153  {
154  static const float PI = 3.14159265f;
155  float pitch = 0, roll = 0, yaw = 0;
156  double test = mY*mZ + mX*mW;
157  if (std::abs(test) > 0.4999f){
158  int symbol = (test > 0.4999f) ? 1 : -1;
159  yaw = symbol * 2 * std::atan2f(mY, mW) * 180 / PI;
160  pitch = symbol * 90.f;
161  roll = 0.f;
162  return Euler(pitch, roll, yaw);
163  }
164  yaw = std::atan2f((2 * mZ*mW - 2 * mX*mY), (1 - 2 * mX*mX - 2 * mZ*mZ)) * 180 / PI;
165  pitch = (float)std::asin(2 * test) * 180 / PI;
166  roll = std::atan2f((2 * mY*mW - 2 * mX*mZ), (1 - 2 * mX*mX - 2 * mY*mY)) * 180 / PI;
167  return Euler(pitch, roll, yaw);
168  }
169 
170  private:
171  float mW = 1;
172  float mX = 0;
173  float mY = 0;
174  float mZ = 0;
175  };
176 
177 } // namespace gf
Quaternion()
Default constructor.
Definition: Quaternion.h:106
std::string toString() const
Get a human-readable value.
Definition: Quaternion.h:141
Data type of quaternion.
Definition: Quaternion.h:101
Euler()
Default constructor.
Definition: Quaternion.h:55
Euler(float pitch, float roll, float yaw)
Constructor with 3 elements.
Definition: Quaternion.h:59
Euler toEuler() const
Convert quaternion to Eulerian angle.
Definition: Quaternion.h:152
float roll() const
Get roll.
Definition: Quaternion.h:74
float x() const
Get X.
Definition: Quaternion.h:126
float pitch() const
Get pitch.
Definition: Quaternion.h:69
The namespace in which all of the gForceSDK project definitions are contained.
Definition: Device.h:42
float w() const
Get W.
Definition: Quaternion.h:121
Data type of Eulerian angle.
Definition: Quaternion.h:50
std::string toString() const
Get a human-readable value.
Definition: Quaternion.h:84
float yaw() const
Get yaw.
Definition: Quaternion.h:79
Quaternion(float w, float x, float y, float z)
Constructor with 4 elements.
Definition: Quaternion.h:110
float y() const
Get Y.
Definition: Quaternion.h:131
float z() const
Get Z.
Definition: Quaternion.h:136