88 Quaternion(
double inI,
double inJ,
double inK,
double inS) noexcept :
89 components_{ inI, inJ, inK, inS }
103 components_{ 0.0, 0.0, 0.0, 0.0 }
105 if (inArray.
size() == 3)
108 eulerToQuat(inArray[0], inArray[1], inArray[2]);
110 else if (inArray.
size() == 4)
116 else if (inArray.
size() == 9)
139 const double halfAngle = inAngle / 2.0;
140 const double sinHalfAngle =
std::sin(halfAngle);
142 components_[0] = normAxis.
x * sinHalfAngle;
143 components_[1] = normAxis.
y * sinHalfAngle;
144 components_[2] = normAxis.
z * sinHalfAngle;
145 components_[3] =
std::cos(halfAngle);
179 eyeTimesScalar.
zeros();
180 eyeTimesScalar(0, 0) = inQuat2.
s();
181 eyeTimesScalar(1, 1) = inQuat2.
s();
182 eyeTimesScalar(2, 2) = inQuat2.
s();
184 NdArray<double> epsilonHat = linalg::hat<double>(inQuat2.
i(), inQuat2.
j(), inQuat2.
k());
186 q.
put(
Slice(0, 3),
Slice(0, 3), eyeTimesScalar + epsilonHat);
187 q(3, 0) = -inQuat2.
i();
188 q(3, 1) = -inQuat2.
j();
189 q(3, 2) = -inQuat2.
k();
217 return { -
i(), -
j(), -
k(),
s() };
226 double i() const noexcept
228 return components_[0];
260 double j() const noexcept
262 return components_[1];
271 double k() const noexcept
273 return components_[2];
287 if (inPercent < 0.0 || inPercent > 1.0)
301 const double oneMinus = 1.0 - inPercent;
302 std::array<double, 4> newComponents{};
305 inQuat1.components_.end(),
306 inQuat2.components_.begin(),
307 newComponents.begin(),
308 [inPercent, oneMinus](
double component1,
double component2) ->
double
309 { return oneMinus * component1 + inPercent * component2; });
311 return { newComponents[0], newComponents[1], newComponents[2], newComponents[3] };
324 return nlerp(*
this, inQuat2, inPercent);
335 return std::asin(2 * (
s() *
j() -
k() *
i()));
367 if (inVector.
size() != 3)
372 return *
this * inVector;
384 return *
this * inVec3;
393 double s() const noexcept
395 return components_[3];
409 if (inPercent < 0 || inPercent > 1)
429 if (dotProduct < 0.0)
435 constexpr
double DOT_THRESHOLD = 0.9995;
436 if (dotProduct > DOT_THRESHOLD)
440 return nlerp(inQuat1, inQuat2, inPercent);
443 dotProduct =
clip(dotProduct, -1.0, 1.0);
444 const double theta0 = std::acos(dotProduct);
445 const double theta = theta0 * inPercent;
465 return slerp(*
this, inQuat2, inPercent);
492 const double q0 =
i();
493 const double q1 =
j();
494 const double q2 =
k();
495 const double q3 =
s();
502 dcm(0, 0) = q3sqr + q0sqr - q1sqr - q2sqr;
503 dcm(0, 1) = 2 * (q0 * q1 - q3 * q2);
504 dcm(0, 2) = 2 * (q0 * q2 + q3 * q1);
505 dcm(1, 0) = 2 * (q0 * q1 + q3 * q2);
506 dcm(1, 1) = q3sqr + q1sqr - q0sqr - q2sqr;
507 dcm(1, 2) = 2 * (q1 * q2 - q3 * q0);
508 dcm(2, 0) = 2 * (q0 * q2 - q3 * q1);
509 dcm(2, 1) = 2 * (q1 * q2 + q3 * q0);
510 dcm(2, 2) = q3sqr + q2sqr - q0sqr - q1sqr;
523 auto componentsCopy = components_;
536 const Vec3 eulerAxis = { 1.0, 0.0, 0.0 };
546 double yaw() const noexcept
560 const Vec3 eulerAxis = { 0.0, 1.0, 0.0 };
573 const Vec3 eulerAxis = { 0.0, 0.0, 1.0 };
586 const auto comparitor = [](
double value1,
double value2) noexcept ->
bool
591 inRhs.components_.begin(),
604 return !(*
this == inRhs);
618 inRhs.components_.begin(),
620 std::plus<double>());
650 inRhs.components_.begin(),
652 std::minus<double>());
691 double q0 = inRhs.
s() *
i();
692 q0 += inRhs.i() *
s();
693 q0 -= inRhs.j() *
k();
694 q0 += inRhs.k() *
j();
696 double q1 = inRhs.s() *
j();
697 q1 += inRhs.i() *
k();
698 q1 += inRhs.j() *
s();
699 q1 -= inRhs.k() *
i();
701 double q2 = inRhs.s() *
k();
702 q2 -= inRhs.i() *
j();
703 q2 += inRhs.j() *
i();
704 q2 += inRhs.k() *
s();
706 double q3 = inRhs.s() *
s();
707 q3 -= inRhs.i() *
i();
708 q3 -= inRhs.j() *
j();
709 q3 -= inRhs.k() *
k();
733 [inScalar](
double& component) { component *= inScalar; });
774 if (inVec.
size() != 3)
779 const auto p =
Quaternion(inVec[0], inVec[1], inVec[2], 0.0);
780 const auto pPrime = *
this * p * this->
inverse();
833 inOStream << inQuat.
str();
839 std::array<double, 4> components_{ { 0.0, 0.0, 0.0, 1.0 } };
845 void normalize() noexcept
847 double sumOfSquares = 0.0;
850 [&sumOfSquares](
double component) noexcept ->
void
851 { sumOfSquares += utils::sqr(component); });
856 [
norm](
double& component) noexcept ->
void { component /= norm; });
867 void eulerToQuat(
double roll,
double pitch,
double yaw) noexcept
869 const double halfPhi =
roll / 2.0;
870 const double halfTheta =
pitch / 2.0;
871 const double halfPsi =
yaw / 2.0;
892 void dcmToQuat(
const NdArray<double>& dcm)
894 const Shape inShape = dcm.shape();
895 if (!(inShape.rows == 3 && inShape.cols == 3))
900 NdArray<double> checks(1, 4);
901 checks[0] = 1 + dcm(0, 0) + dcm(1, 1) + dcm(2, 2);
902 checks[1] = 1 + dcm(0, 0) - dcm(1, 1) - dcm(2, 2);
903 checks[2] = 1 - dcm(0, 0) + dcm(1, 1) - dcm(2, 2);
904 checks[3] = 1 - dcm(0, 0) - dcm(1, 1) + dcm(2, 2);
912 components_[3] = 0.5 *
std::sqrt(1 + dcm(0, 0) + dcm(1, 1) + dcm(2, 2));
913 components_[0] = (dcm(2, 1) - dcm(1, 2)) / (4 * components_[3]);
914 components_[1] = (dcm(0, 2) - dcm(2, 0)) / (4 * components_[3]);
915 components_[2] = (dcm(1, 0) - dcm(0, 1)) / (4 * components_[3]);
921 components_[0] = 0.5 *
std::sqrt(1 + dcm(0, 0) - dcm(1, 1) - dcm(2, 2));
922 components_[1] = (dcm(1, 0) + dcm(0, 1)) / (4 * components_[0]);
923 components_[2] = (dcm(2, 0) + dcm(0, 2)) / (4 * components_[0]);
924 components_[3] = (dcm(2, 1) - dcm(1, 2)) / (4 * components_[0]);
930 components_[1] = 0.5 *
std::sqrt(1 - dcm(0, 0) + dcm(1, 1) - dcm(2, 2));
931 components_[0] = (dcm(1, 0) + dcm(0, 1)) / (4 * components_[1]);
932 components_[2] = (dcm(2, 1) + dcm(1, 2)) / (4 * components_[1]);
933 components_[3] = (dcm(0, 2) - dcm(2, 0)) / (4 * components_[1]);
939 components_[2] = 0.5 *
std::sqrt(1 - dcm(0, 0) - dcm(1, 1) + dcm(2, 2));
940 components_[0] = (dcm(2, 0) + dcm(0, 2)) / (4 * components_[2]);
941 components_[1] = (dcm(2, 1) + dcm(1, 2)) / (4 * components_[2]);
942 components_[3] = (dcm(1, 0) - dcm(0, 1)) / (4 * components_[2]);
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
NdArray< dtype > & put(int32 inIndex, value_type inValue)
Definition: NdArrayCore.hpp:3663
size_type size() const noexcept
Definition: NdArrayCore.hpp:4289
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1221
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4650
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1529
value_type item() const
Definition: NdArrayCore.hpp:2921
NdArray< dtype > & zeros() noexcept
Definition: NdArrayCore.hpp:4668
NdArray< dtype > dot(const NdArray< dtype > &inOtherArray) const
Definition: NdArrayCore.hpp:2623
A Class for slicing into NdArrays.
Definition: Slice.hpp:44
Holds a 3D vector.
Definition: Vec3.hpp:50
double z
Definition: Vec3.hpp:55
NdArray< double > toNdArray() const
Definition: Vec3.hpp:324
Vec3 normalize() const noexcept
Definition: Vec3.hpp:276
double x
Definition: Vec3.hpp:53
double y
Definition: Vec3.hpp:54
Holds a unit quaternion.
Definition: Quaternion.hpp:58
double s() const noexcept
Definition: Quaternion.hpp:393
std::string str() const
Definition: Quaternion.hpp:474
NdArray< double > operator*(const NdArray< double > &inVec) const
Definition: Quaternion.hpp:772
NdArray< double > angularVelocity(const Quaternion &inQuat2, double inTime) const
Definition: Quaternion.hpp:204
Quaternion & operator*=(double inScalar) noexcept
Definition: Quaternion.hpp:729
double roll() const noexcept
Definition: Quaternion.hpp:353
NdArray< double > rotate(const NdArray< double > &inVector) const
Definition: Quaternion.hpp:365
static Quaternion xRotation(double inAngle) noexcept
Definition: Quaternion.hpp:534
static Quaternion nlerp(const Quaternion &inQuat1, const Quaternion &inQuat2, double inPercent)
Definition: Quaternion.hpp:285
Vec3 rotate(const Vec3 &inVec3) const
Definition: Quaternion.hpp:382
Quaternion(double inI, double inJ, double inK, double inS) noexcept
Definition: Quaternion.hpp:88
Quaternion operator-() const noexcept
Definition: Quaternion.hpp:677
NdArray< double > toNdArray() const
Definition: Quaternion.hpp:521
Quaternion operator+(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:634
double i() const noexcept
Definition: Quaternion.hpp:226
double yaw() const noexcept
Definition: Quaternion.hpp:546
double pitch() const noexcept
Definition: Quaternion.hpp:333
Quaternion & operator-=(const Quaternion &inRhs) noexcept
Definition: Quaternion.hpp:646
Quaternion slerp(const Quaternion &inQuat2, double inPercent) const
Definition: Quaternion.hpp:463
friend std::ostream & operator<<(std::ostream &inOStream, const Quaternion &inQuat)
Definition: Quaternion.hpp:831
static Quaternion slerp(const Quaternion &inQuat1, const Quaternion &inQuat2, double inPercent)
Definition: Quaternion.hpp:407
static NdArray< double > angularVelocity(const Quaternion &inQuat1, const Quaternion &inQuat2, double inTime)
Definition: Quaternion.hpp:170
void print() const
Definition: Quaternion.hpp:342
Quaternion(const NdArray< double > &inAxis, double inAngle)
Definition: Quaternion.hpp:155
bool operator==(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:584
Quaternion & operator/=(const Quaternion &inRhs) noexcept
Definition: Quaternion.hpp:806
Quaternion(double roll, double pitch, double yaw) noexcept
Definition: Quaternion.hpp:74
Vec3 operator*(const Vec3 &inVec3) const
Definition: Quaternion.hpp:794
Quaternion inverse() const noexcept
Definition: Quaternion.hpp:248
double k() const noexcept
Definition: Quaternion.hpp:271
NdArray< double > toDCM() const
Definition: Quaternion.hpp:488
Quaternion & operator*=(const Quaternion &inRhs) noexcept
Definition: Quaternion.hpp:689
static Quaternion zRotation(double inAngle) noexcept
Definition: Quaternion.hpp:571
Quaternion operator/(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:818
Quaternion nlerp(const Quaternion &inQuat2, double inPercent) const
Definition: Quaternion.hpp:322
static Quaternion yRotation(double inAngle) noexcept
Definition: Quaternion.hpp:558
Quaternion(const Vec3 &inAxis, double inAngle) noexcept
Definition: Quaternion.hpp:134
double j() const noexcept
Definition: Quaternion.hpp:260
Quaternion operator*(double inScalar) const noexcept
Definition: Quaternion.hpp:760
Quaternion operator-(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:666
Quaternion operator*(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:747
bool operator!=(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:602
Quaternion(const NdArray< double > &inArray)
Definition: Quaternion.hpp:102
Quaternion conjugate() const noexcept
Definition: Quaternion.hpp:215
static Quaternion identity() noexcept
Definition: Quaternion.hpp:237
Quaternion & operator+=(const Quaternion &inRhs) noexcept
Definition: Quaternion.hpp:614
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:784
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition: StlAlgorithms.hpp:142
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:99
std::string num2str(dtype inNumber)
Definition: num2str.hpp:46
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:51
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:44
Definition: Coordinate.hpp:45
NdArray< uint32 > argmax(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: argmax.hpp:46
auto sin(dtype inValue) noexcept
Definition: sin.hpp:49
dtype clip(dtype inValue, dtype inMinValue, dtype inMaxValue)
Definition: clip.hpp:50
auto cos(dtype inValue) noexcept
Definition: cos.hpp:49
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48
NdArray< double > norm(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: norm.hpp:51
std::uint32_t uint32
Definition: Types.hpp:40