39 double static constexpr half = double(0.5);
40 double static constexpr MIN_ANGLE = double(4.84813681e-6);
48 Rotation(
double a,
double b,
double c,
double d)
59 _v[0] = double(in[0]);
60 _v[1] = double(in[1]);
61 _v[2] = double(in[2]);
62 _v[3] = double(in[3]);
65 static inline Rotation Identity(
void)
70 Rotation Inverse(
void)
const
72 return Rotation(_v[0], -_v[1], -_v[2], -_v[3]);
78 double n = sqrt(_v[0] * _v[0] + _v[1] * _v[1] + _v[2] * _v[2] + _v[3] * _v[3]);
85 for (
int i = 0; i < 4; i++)
95 const double theta = rotVec.
GetNorm2();
96 if (theta < MIN_ANGLE)
106 return Rotation(cos(angle * half), sin(angle * half) * unitVector.x, sin(angle * half) * unitVector.y,
107 sin(angle * half) * unitVector.
z);
111 static Rotation
FromEulerYPR(
const double &y,
const double &p,
const double &r)
114 rot[0] = cos(half * y) * cos(half * p) * cos(half * r) + sin(half * y) * sin(half * p) * sin(half * r);
115 rot[1] = cos(half * y) * cos(half * p) * sin(half * r) - sin(half * y) * sin(half * p) * cos(half * r);
116 rot[2] = cos(half * y) * sin(half * p) * cos(half * r) + sin(half * y) * cos(half * p) * sin(half * r);
117 rot[3] = sin(half * y) * cos(half * p) * cos(half * r) - cos(half * y) * sin(half * p) * sin(half * r);
126 return Rotation::Identity();
127 double a0 = sqrt(1 - tmp);
128 return Rotation(a0, in.x, in.y, in.
z);
146 double c0 = r1[0] * _v[0] - r1[1] * _v[1] - r1[2] * _v[2] - r1[3] * _v[3];
147 double c1 = r1[1] * _v[0] + r1[0] * _v[1] + r1[3] * _v[2] - r1[2] * _v[3];
148 double c2 = r1[2] * _v[0] - r1[3] * _v[1] + r1[0] * _v[2] + r1[1] * _v[3];
149 double c3 = r1[3] * _v[0] + r1[2] * _v[1] - r1[1] * _v[2] + r1[0] * _v[3];
151 return Rotation(c0, c1, c2, c3);
168 const double angle = asin(norm) * 2;
169 if (angle < MIN_ANGLE)
171 return Vec3(0, 0, 0);
174 return n * (angle / norm);
182 return Vec3(_v[1], _v[2], _v[3]);
184 return Vec3(-_v[1], -_v[2], -_v[3]);
190 y = atan2(
double(2.0) * _v[1] * _v[2] +
double(2.0) * _v[0] * _v[3],
191 _v[1] * _v[1] + _v[0] * _v[0] - _v[3] * _v[3] - _v[2] * _v[2]);
192 p = -asin(
double(2.0) * _v[1] * _v[3] -
double(2.0) * _v[0] * _v[2]);
193 r = atan2(
double(2.0) * _v[2] * _v[3] +
double(2.0) * _v[0] * _v[1],
194 _v[3] * _v[3] - _v[2] * _v[2] - _v[1] * _v[1] + _v[0] * _v[0]);
210 for (
int i = 0; i < 3; i++)
212 for (
int j = 0; j < 3; j++)
214 printf(
"%.3f\t", (
double)R[i * 3 + j]);
220 double &operator[](
unsigned const i)
224 const double &operator[](
unsigned const i)
const
237 const double r0 = _v[0] * _v[0];
238 const double r1 = _v[1] * _v[1];
239 const double r2 = _v[2] * _v[2];
240 const double r3 = _v[3] * _v[3];
242 R[0] = r0 + r1 - r2 - r3;
243 R[1] = 2 * _v[1] * _v[2] - 2 * _v[0] * _v[3];
244 R[2] = 2 * _v[1] * _v[3] + 2 * _v[0] * _v[2];
246 R[3] = 2 * _v[1] * _v[2] + 2 * _v[0] * _v[3];
247 R[4] = r0 - r1 + r2 - r3;
248 R[5] = 2 * _v[2] * _v[3] - 2 * _v[0] * _v[1];
250 R[6] = 2 * _v[1] * _v[3] - 2 * _v[0] * _v[2];
251 R[7] = 2 * _v[2] * _v[3] + 2 * _v[0] * _v[1];
252 R[8] = r0 - r1 - r2 + r3;
261 Vec3 ret(R[0] * in.x + R[1] * in.y + R[2] * in.
z, R[3] * in.x + R[4] * in.y + R[5] * in.
z,
262 R[6] * in.x + R[7] * in.y + R[8] * in.
z);
267 Vec3 RotateBackwards(
const Vec3 &in)
const
272 Vec3 ret(R[0] * in.x + R[1] * in.y + R[2] * in.z, R[3] * in.x + R[4] * in.y + R[5] * in.z,
273 R[6] * in.x + R[7] * in.y + R[8] * in.z);
Vec3 ToEulerYPR(void) const
Returns the Euler angles yaw, pitch, and roll representing this rotation.
Rotation operator*(const Rotation &r1) const
Rotation multiplication: r2*r1, corresponds to a rotation r1 followed by rotation r2.
void GetRotationMatrix(double R[9]) const
Rotation(double a, double b, double c, double d)
Constructor given a unit quaternion.
static Rotation FromRotationVector(const Vec3 rotVec)
Rotation(Rotation const &in)
Copy constructor.
Vec3 operator*(const Vec3 &vec) const
Rotate a vector forward.
Vec3 ToRotationVector(void) const
static Rotation FromEulerYPR(const double &y, const double &p, const double &r)
Returns a Rotation object given Euler angles yaw, pitch, and roll.
static Rotation FromVectorPartOfQuaternion(const Vec3 in)
Returns a Rotation object given the three elements of the vector part of a unit quaternion.
static Rotation FromAxisAngle(const Vec3 &unitVector, const double &angle)
void Normalise(void)
This makes sure the quaternion is a unit quaternion.
void PrintRotationMatrix(void)
For debugging.
Vec3 ToVectorPartOfQuaternion(void) const
Returns the vector part of the quaternion representing this rotation.
void ToEulerYPR(double &y, double &p, double &r) const
Returns the Euler angles yaw, pitch, and roll representing this rotation.
3D vector class with common vector operations.
double GetNorm2Squared(void) const
Calculate the Euclidean norm of the vector, squared (= sum of squared elements).
double GetNorm2(void) const
Calculate the Euclidean norm of the vector.
double z
the three components of the vector