25 #ifndef SRC_ORIENTATION_QUATERNION_H_ 26 #define SRC_ORIENTATION_QUATERNION_H_ 38 #include <forward_list> 43 #include "unit/Unit.h" 44 #include "quantity/Scalar.h" 83 class FloatVectorStruct {
131 void set(
float w,
float x,
float y,
float z) {
143 set(src.w, src.x, src.y, src.z);
152 v.x = 2 * ((q.x * q.z) - (q.w * q.y));
153 v.y = 2 * ((q.w * q.x) + (q.y * q.z));
154 v.z = (q.w * q.w) - (q.x * q.x) - (q.y * q.y) + (q.z * q.z);
166 const FloatVectorStruct& gravity, FloatVectorStruct& ypr) {
168 ypr.x = (float) atan2((2 * (q.x * q.y)) - (2 * (q.w * q.z)),
169 (2 * (q.w * q.w)) + (2 * (q.x * q.x)) - 1);
171 ypr.y = (float) atan(
174 (gravity.x * gravity.x)
175 + (gravity.z * gravity.z)));
177 ypr.z = (float) atan(
180 (gravity.y * gravity.y)
181 + (gravity.z * gravity.z)));
189 FloatVectorStruct gravity;
190 getGravity(gravity, *
this);
191 getYawPitchRoll(*
this, gravity, ypr);
199 FloatVectorStruct ypr;
200 getYawPitchRollRadians(ypr);
209 FloatVectorStruct ypr;
210 getYawPitchRollRadians(ypr);
219 FloatVectorStruct ypr;
220 getYawPitchRollRadians(ypr);
240 double cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y
243 if (fabs(cosHalfTheta) >= 1.0) {
251 double halfTheta = acos(cosHalfTheta);
252 double sinHalfTheta = sqrt(1.0 - cosHalfTheta * cosHalfTheta);
255 if (fabs(sinHalfTheta) < 0.001) {
256 out.w = (qa.w * 0.5f + qb.w * 0.5f);
257 out.x = (qa.x * 0.5f + qb.x * 0.5f);
258 out.y = (qa.y * 0.5f + qb.y * 0.5f);
259 out.z = (qa.z * 0.5f + qb.z * 0.5f);
262 float ratioA = (float) (sin((1 - t) * halfTheta) / sinHalfTheta);
263 float ratioB = (float) (sin(t * halfTheta) / sinHalfTheta);
265 out.w = (qa.w * ratioA + qb.w * ratioB);
266 out.x = (qa.x * ratioA + qb.x * ratioB);
267 out.y = (qa.y * ratioA + qb.y * ratioB);
268 out.z = (qa.z * ratioA + qb.z * ratioB);
293 this->divide(dotProduct(*
this, *
this));
311 x = this->w * q.x + this->x * q.w + this->y * q.z - this->z * q.y;
312 y = this->w * q.y + this->y * q.w + this->z * q.x - this->x * q.z;
313 z = this->w * q.z + this->z * q.w + this->x * q.y - this->y * q.x;
314 w = this->w * q.w - this->x * q.x - this->y * q.y - this->z * q.z;
327 this->w = this->w / s;
328 this->x = this->x / s;
329 this->y = this->y / s;
330 this->z = this->z / s;
334 return q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
353 q_diff.
set(qa.w, qa.x, qa.y, qa.z);
405 static void getUnits(vector<IUnit *>& units) {
407 units.push_back(&Quaternion::component_units);
408 units.push_back(&Quaternion::component_units);
409 units.push_back(&Quaternion::component_units);
410 units.push_back(&Quaternion::component_units);
413 bool getPrintableString(vector<string>& printable_string) {
417 bool getContainedQuantities(vector<IQuantity *>& quantities) {
418 quantities.push_back(
new Scalar(w));
419 quantities.push_back(
new Scalar(x));
420 quantities.push_back(
new Scalar(y));
421 quantities.push_back(
new Scalar(z));
425 bool getContainedQuantityNames(vector<string>& quantity_names) {
426 quantity_names.push_back(
"W");
427 quantity_names.push_back(
"X");
428 quantity_names.push_back(
"Y");
429 quantity_names.push_back(
"Z");
Quaternion(float w, float x, float y, float z)
Constructs a Quaternion instance, using the provides w, x, y and z valuese.
Definition: Quaternion.h:117
static void getGravity(FloatVectorStruct &v, const Quaternion &q)
Extracts the gravity vector from the Quaternion.
Definition: Quaternion.h:151
The Quaternion class provides methods to operate on a quaternion.
Definition: Quaternion.h:76
static void getYawPitchRoll(const Quaternion &q, const FloatVectorStruct &gravity, FloatVectorStruct &ypr)
Extracts the yaw, pitch and roll values from the Quaternion.
Definition: Quaternion.h:165
float getX()
Accessor for the Quaternion's X component value.
Definition: Quaternion.h:370
void getYawPitchRollRadians(FloatVectorStruct &ypr)
Extracts the yaw, pitch and roll values from the Quaternion.
Definition: Quaternion.h:188
float getY()
Accessor for the Quaternion's Y component value.
Definition: Quaternion.h:378
void multiply(const Quaternion &q)
Modifies this quaternion (the multiplicand) to be the product of multiplication by a multiplier Quate...
Definition: Quaternion.h:308
Quaternion(const Quaternion &src)
Constructs a Quaternion instance, using values from another Quaternion instance.
Definition: Quaternion.h:106
static void difference(const Quaternion &qa, const Quaternion &qb, Quaternion &q_diff)
Divides two quaternions.
Definition: Quaternion.h:351
void divide(float s)
Modifies a quaternion, scaling it by the provided parameter.
Definition: Quaternion.h:326
float getZ()
Accessor for the Quaternion's Z component value.
Definition: Quaternion.h:386
void conjugate()
Modifies the Quaternion to be its complex conjugate.
Definition: Quaternion.h:279
static void slerp(const Quaternion &qa, const Quaternion &qb, double t, Quaternion &out)
Estimates an intermediate Quaternion given Quaternions representing each end of the path...
Definition: Quaternion.h:237
Definition: IQuantity.h:33
void getRoll(Scalar &roll)
Extracts the roll angle value from the Quaternion.
Definition: Quaternion.h:218
void getYawRadians(Scalar &yaw)
Extracts the yaw angle value from the Quaternion.
Definition: Quaternion.h:198
void getPitch(Scalar &pitch)
Extracts the pitch angle value from the Quaternion.
Definition: Quaternion.h:208
void set(float w, float x, float y, float z)
Modifies the Quaternion by setting the component W, X, Y and Z value.
Definition: Quaternion.h:131
Quaternion()
Constructs a Quaternion instance, using default values for a Unit Quaternion.
Definition: Quaternion.h:97
float getW()
Accessor for the Quaternion's W component value.
Definition: Quaternion.h:362
void inverse()
Modifies the Quaternion to be its inverse (reciprocal).
Definition: Quaternion.h:291