 |
NumCpp
2.1.0
A C++ implementation of the Python Numpy library
|
Go to the documentation of this file.
89 Quaternion(
double inI,
double inJ,
double inK,
double inS) noexcept :
90 components_{ inI, inJ, inK, inS }
104 components_{ 0.0, 0.0, 0.0, 0.0 }
106 if (inArray.size() == 3)
109 eulerToQuat(inArray[0], inArray[1], inArray[2]);
111 else if (inArray.size() == 4)
117 else if (inArray.size() == 9)
140 const double halfAngle = inAngle / 2.0;
141 const double sinHalfAngle =
std::sin(halfAngle);
143 components_[0] = normAxis.
x * sinHalfAngle;
144 components_[1] = normAxis.
y * sinHalfAngle;
145 components_[2] = normAxis.
z * sinHalfAngle;
146 components_[3] =
std::cos(halfAngle);
180 eyeTimesScalar.
zeros();
181 eyeTimesScalar(0, 0) = inQuat2.
s();
182 eyeTimesScalar(1, 1) = inQuat2.
s();
183 eyeTimesScalar(2, 2) = inQuat2.
s();
185 NdArray<double> epsilonHat = linalg::hat<double>(inQuat2.
i(), inQuat2.
j(), inQuat2.
k());
187 q.
put(
Slice(0, 3),
Slice(0, 3), eyeTimesScalar + epsilonHat);
188 q(3, 0) = -inQuat2.
i();
189 q(3, 1) = -inQuat2.
j();
190 q(3, 2) = -inQuat2.
k();
220 return {-
i(), -
j(), -
k(),
s()};
230 double i() const noexcept
232 return components_[0];
267 double j() const noexcept
269 return components_[1];
279 double k() const noexcept
281 return components_[2];
296 if (inPercent < 0.0 || inPercent > 1.0)
310 const double oneMinus = 1.0 - inPercent;
311 std::array<double, 4> newComponents{};
314 inQuat2.components_.begin(), newComponents.begin(),
315 [inPercent, oneMinus](
double component1,
double component2) ->
double
317 return oneMinus * component1 + inPercent * component2;
320 return {newComponents[0], newComponents[1], newComponents[2], newComponents[3]};
334 return nlerp(*
this, inQuat2, inPercent);
345 return std::asin(2 * (
s() *
j() -
k() *
i()));
365 return std::atan2(2 * (
s() *
i() +
j() *
k()),
380 if (inVector.
size() != 3)
385 return *
this * inVector;
399 return *
this * inVec3;
409 double s() const noexcept
411 return components_[3];
426 if (inPercent < 0 || inPercent > 1)
446 if (dotProduct < 0.0)
452 constexpr
double DOT_THRESHOLD = 0.9995;
453 if (dotProduct > DOT_THRESHOLD) {
456 return nlerp(inQuat1, inQuat2, inPercent);
459 dotProduct =
clip(dotProduct, -1.0, 1.0);
460 const double theta0 = std::acos(dotProduct);
461 const double theta = theta0 * inPercent;
481 return slerp(*
this, inQuat2, inPercent);
510 const double q0 =
i();
511 const double q1 =
j();
512 const double q2 =
k();
513 const double q3 =
s();
520 dcm(0, 0) = q3sqr + q0sqr - q1sqr - q2sqr;
521 dcm(0, 1) = 2 * (q0 * q1 - q3 * q2);
522 dcm(0, 2) = 2 * (q0 * q2 + q3 * q1);
523 dcm(1, 0) = 2 * (q0 * q1 + q3 * q2);
524 dcm(1, 1) = q3sqr + q1sqr - q0sqr - q2sqr;
525 dcm(1, 2) = 2 * (q1 * q2 - q3 * q0);
526 dcm(2, 0) = 2 * (q0 * q2 - q3 * q1);
527 dcm(2, 1) = 2 * (q1 * q2 + q3 * q0);
528 dcm(2, 2) = q3sqr + q2sqr - q0sqr - q1sqr;
542 auto componentsCopy = components_;
557 const Vec3 eulerAxis = { 1.0, 0.0, 0.0 };
567 double yaw() const noexcept
569 return std::atan2(2 * (
s() *
k() +
i() *
j()),
584 const Vec3 eulerAxis = { 0.0, 1.0, 0.0 };
599 const Vec3 eulerAxis = { 0.0, 0.0, 1.0 };
614 const auto comparitor = [](
double value1,
double value2) noexcept ->
bool
620 inRhs.components_.begin(), comparitor);
634 return !(*
this == inRhs);
649 inRhs.components_.begin(), components_.begin(), std::plus<double>());
682 inRhs.components_.begin(), components_.begin(), std::minus<double>());
726 double q0 = inRhs.
s() *
i();
727 q0 += inRhs.i() *
s();
728 q0 -= inRhs.j() *
k();
729 q0 += inRhs.k() *
j();
731 double q1 = inRhs.s() *
j();
732 q1 += inRhs.i() *
k();
733 q1 += inRhs.j() *
s();
734 q1 -= inRhs.k() *
i();
736 double q2 = inRhs.s() *
k();
737 q2 -= inRhs.i() *
j();
738 q2 += inRhs.j() *
i();
739 q2 += inRhs.k() *
s();
741 double q3 = inRhs.s() *
s();
742 q3 -= inRhs.i() *
i();
743 q3 -= inRhs.j() *
j();
744 q3 -= inRhs.k() *
k();
769 [&inScalar](
double& component)
771 component *= inScalar;
819 if (inVec.
size() != 3)
824 const auto p =
Quaternion(inVec[0], inVec[1], inVec[2], 0.0);
825 const auto pPrime = *
this * p * this->
inverse();
885 inOStream << inQuat.
str();
891 std::array<double, 4> components_{ {0.0, 0.0, 0.0, 1.0} };
897 void normalize() noexcept
899 double sumOfSquares = 0.0;
901 [&sumOfSquares](
double component) noexcept ->
void
903 sumOfSquares += utils::sqr(component);
908 [&
norm](
double& component) noexcept ->
void
922 void eulerToQuat(
double roll,
double pitch,
double yaw) noexcept
924 const double halfPhi =
roll / 2.0;
925 const double halfTheta =
pitch / 2.0;
926 const double halfPsi =
yaw / 2.0;
947 void dcmToQuat(
const NdArray<double>& dcm)
949 const Shape inShape = dcm.shape();
950 if (!(inShape.rows == 3 && inShape.cols == 3))
955 NdArray<double> checks(1, 4);
956 checks[0] = 1 + dcm(0, 0) + dcm(1, 1) + dcm(2, 2);
957 checks[1] = 1 + dcm(0, 0) - dcm(1, 1) - dcm(2, 2);
958 checks[2] = 1 - dcm(0, 0) + dcm(1, 1) - dcm(2, 2);
959 checks[3] = 1 - dcm(0, 0) - dcm(1, 1) + dcm(2, 2);
967 components_[3] = 0.5 *
std::sqrt(1 + dcm(0, 0) + dcm(1, 1) + dcm(2, 2));
968 components_[0] = (dcm(2, 1) - dcm(1, 2)) / (4 * components_[3]);
969 components_[1] = (dcm(0, 2) - dcm(2, 0)) / (4 * components_[3]);
970 components_[2] = (dcm(1, 0) - dcm(0, 1)) / (4 * components_[3]);
976 components_[0] = 0.5 *
std::sqrt(1 + dcm(0, 0) - dcm(1, 1) - dcm(2, 2));
977 components_[1] = (dcm(1, 0) + dcm(0, 1)) / (4 * components_[0]);
978 components_[2] = (dcm(2, 0) + dcm(0, 2)) / (4 * components_[0]);
979 components_[3] = (dcm(2, 1) - dcm(1, 2)) / (4 * components_[0]);
985 components_[1] = 0.5 *
std::sqrt(1 - dcm(0, 0) + dcm(1, 1) - dcm(2, 2));
986 components_[0] = (dcm(1, 0) + dcm(0, 1)) / (4 * components_[1]);
987 components_[2] = (dcm(2, 1) + dcm(1, 2)) / (4 * components_[1]);
988 components_[3] = (dcm(0, 2) - dcm(2, 0)) / (4 * components_[1]);
994 components_[2] = 0.5 *
std::sqrt(1 - dcm(0, 0) - dcm(1, 1) + dcm(2, 2));
995 components_[0] = (dcm(2, 0) + dcm(0, 2)) / (4 * components_[2]);
996 components_[1] = (dcm(2, 1) + dcm(1, 2)) / (4 * components_[2]);
997 components_[3] = (dcm(1, 0) - dcm(0, 1)) / (4 * components_[2]);
Quaternion & operator+=(const Quaternion &inRhs) noexcept
Definition: Quaternion.hpp:646
value_type item() const
Definition: NdArrayCore.hpp:2958
bool operator!=(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:632
Quaternion(double inI, double inJ, double inK, double inS) noexcept
Definition: Quaternion.hpp:89
Vec3 operator*(const Vec3 &inVec3) const
Definition: Quaternion.hpp:841
static Quaternion slerp(const Quaternion &inQuat1, const Quaternion &inQuat2, double inPercent)
Definition: Quaternion.hpp:424
Holds a unit quaternion.
Definition: Quaternion.hpp:58
double y
Definition: Vec3.hpp:55
Quaternion & operator-=(const Quaternion &inRhs) noexcept
Definition: Quaternion.hpp:679
NdArray< dtype > dot(const NdArray< dtype > &inOtherArray) const
Definition: NdArrayCore.hpp:2644
static Quaternion identity() noexcept
Definition: Quaternion.hpp:242
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:51
double pitch() const noexcept
Definition: Quaternion.hpp:343
Quaternion & operator*=(const Quaternion &inRhs) noexcept
Definition: Quaternion.hpp:724
NdArray< double > toDCM() const
Definition: Quaternion.hpp:506
double s() const noexcept
Definition: Quaternion.hpp:409
dtype clip(dtype inValue, dtype inMinValue, dtype inMaxValue)
Definition: clip.hpp:51
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:53
static Quaternion nlerp(const Quaternion &inQuat1, const Quaternion &inQuat2, double inPercent)
Definition: Quaternion.hpp:294
std::string num2str(dtype inNumber)
Definition: num2str.hpp:47
double yaw() const noexcept
Definition: Quaternion.hpp:567
Quaternion operator/(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:869
Quaternion inverse() const noexcept
Definition: Quaternion.hpp:254
static Quaternion xRotation(double inAngle) noexcept
Definition: Quaternion.hpp:555
double z
Definition: Vec3.hpp:56
double roll() const noexcept
Definition: Quaternion.hpp:363
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4608
auto cos(dtype inValue) noexcept
Definition: cos.hpp:52
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:703
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:214
std::uint32_t uint32
Definition: Types.hpp:41
static Quaternion zRotation(double inAngle) noexcept
Definition: Quaternion.hpp:597
Quaternion operator-(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:698
Quaternion operator*(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:788
NdArray< dtype > & put(int32 inIndex, value_type inValue)
Definition: NdArrayCore.hpp:3685
Quaternion(const NdArray< double > &inArray)
Definition: Quaternion.hpp:103
NdArray< double > toNdArray() const
Definition: Quaternion.hpp:540
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:96
size_type size() const noexcept
Definition: NdArrayCore.hpp:4326
NdArray< double > angularVelocity(const Quaternion &inQuat2, double inTime) const
Definition: Quaternion.hpp:206
bool operator==(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:612
void print() const
Definition: Quaternion.hpp:352
Vec3 rotate(const Vec3 &inVec3) const
Definition: Quaternion.hpp:397
auto sin(dtype inValue) noexcept
Definition: sin.hpp:52
NdArray< dtype > & zeros() noexcept
Definition: NdArrayCore.hpp:4626
double k() const noexcept
Definition: Quaternion.hpp:279
static Quaternion yRotation(double inAngle) noexcept
Definition: Quaternion.hpp:582
Definition: Coordinate.hpp:45
friend std::ostream & operator<<(std::ostream &inOStream, const Quaternion &inQuat)
Definition: Quaternion.hpp:883
double j() const noexcept
Definition: Quaternion.hpp:267
Quaternion(const Vec3 &inAxis, double inAngle) noexcept
Definition: Quaternion.hpp:135
Quaternion & operator*=(double inScalar) noexcept
Definition: Quaternion.hpp:766
Quaternion operator*(double inScalar) const noexcept
Definition: Quaternion.hpp:803
NdArray< double > rotate(const NdArray< double > &inVector) const
Definition: Quaternion.hpp:378
NdArray< double > toNdArray() const
Definition: Vec3.hpp:324
std::string str() const
Definition: Quaternion.hpp:491
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
double x
Definition: Vec3.hpp:54
double i() const noexcept
Definition: Quaternion.hpp:230
Quaternion operator+(const Quaternion &inRhs) const noexcept
Definition: Quaternion.hpp:665
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:45
Quaternion slerp(const Quaternion &inQuat2, double inPercent) const
Definition: Quaternion.hpp:479
Quaternion(double roll, double pitch, double yaw) noexcept
Definition: Quaternion.hpp:75
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition: StlAlgorithms.hpp:136
Quaternion conjugate() const noexcept
Definition: Quaternion.hpp:218
A Class for slicing into NdArrays.
Definition: Slice.hpp:44
Holds a 3D vector.
Definition: Vec3.hpp:50
Vec3 normalize() const noexcept
Definition: Vec3.hpp:276
Quaternion & operator/=(const Quaternion &inRhs) noexcept
Definition: Quaternion.hpp:855
NdArray< double > norm(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: norm.hpp:53
NdArray< uint32 > argmax(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: argmax.hpp:48
Quaternion(const NdArray< double > &inAxis, double inAngle)
Definition: Quaternion.hpp:156
static NdArray< double > angularVelocity(const Quaternion &inQuat1, const Quaternion &inQuat2, double inTime)
Definition: Quaternion.hpp:171
Quaternion operator-() const noexcept
Definition: Quaternion.hpp:710
NdArray< double > operator*(const NdArray< double > &inVec) const
Definition: Quaternion.hpp:817
Quaternion nlerp(const Quaternion &inQuat2, double inPercent) const
Definition: Quaternion.hpp:332