LOST 0.0.1
LOST: Open-source Star Tracker
Loading...
Searching...
No Matches
attitude-utils.hpp
Go to the documentation of this file.
1#ifndef ATTITUDE_UTILS_H
2#define ATTITUDE_UTILS_H
3
4#include <ostream>
5
6#include "decimal.hpp"
8
9namespace lost {
10
11// At first, I wanted to have two separate Attitude classes, one storing Euler angles and converting
12// to Quaterinon, and another storing as Quaternion and converting to Euler. But abstract classes
13// make everything more annoying, because you need vectors of pointers...ugh!
14
16struct Vec2 {
19
20 decimal Magnitude() const;
21 decimal MagnitudeSq() const;
22
23 Vec2 Normalize() const;
24
25 decimal operator*(const Vec2 &) const;
26 Vec2 operator*(const decimal &) const;
27 Vec2 operator-(const Vec2 &) const;
28 Vec2 operator+(const Vec2 &) const;
29
30 friend std::ostream &operator<<(std::ostream &output, const Vec2 &vec);
31};
32
33class Mat3; // define above so we can use in Vec3 class
34
36class Vec3 {
37public:
41
42 Vec3(){};
43 Vec3(decimal x, decimal y, decimal z) : x(x), y(y), z(z){};
44
45 decimal Magnitude() const;
46 decimal MagnitudeSq() const;
47 Vec3 Normalize() const;
48
49 decimal operator*(const Vec3 &) const;
50 Vec3 operator*(const decimal &) const;
51 Vec3 operator*(const Mat3 &) const;
52 Vec3 operator-(const Vec3 &) const;
53 Vec3 operator+(const Vec3 &) const;
54 Vec3 CrossProduct(const Vec3 &) const;
55 Mat3 OuterProduct(const Vec3 &) const;
56
57 friend std::ostream &operator<<(std::ostream &output, const Vec3 &vec);
58};
59
61class Mat3 {
62public:
64
65 decimal At(int i, int j) const;
66 Mat3 operator+(const Mat3 &) const;
67 Mat3 operator*(const Mat3 &) const;
68 Vec3 operator*(const Vec3 &) const;
69 Mat3 operator*(const decimal &) const;
70 Mat3 Transpose() const;
71 Vec3 Column(int) const;
72 Vec3 Row(int) const;
73 decimal Trace() const;
74 decimal Det() const;
75 Mat3 Inverse() const;
76};
77
78extern const Mat3 kIdentityMat3;
79
80void SerializeVec3(SerializeContext *, const Vec3 &);
82
83decimal Distance(const Vec2 &, const Vec2 &);
84decimal Distance(const Vec3 &, const Vec3 &);
85
103
106public:
107 Quaternion() = default;
108 explicit Quaternion(const Vec3 &);
109 Quaternion(const Vec3 &, decimal);
110
113
114 Quaternion operator*(const Quaternion &other) const;
115 Quaternion Conjugate() const;
116 Vec3 Vector() const;
117 void SetVector(const Vec3 &);
118 Vec3 Rotate(const Vec3 &) const;
119 decimal Angle() const;
122 decimal SmallestAngle() const;
123 void SetAngle(decimal);
124 EulerAngles ToSpherical() const;
125 bool IsUnit(decimal tolerance) const;
126 Quaternion Canonicalize() const;
127
132};
133
134//
145class Attitude {
146public:
148 Attitude() = default;
149 explicit Attitude(const Quaternion &); // NOLINT
150 explicit Attitude(const Mat3 &dcm);
151
153 Mat3 GetDCM() const;
154 EulerAngles ToSpherical() const;
155 Vec3 Rotate(const Vec3 &) const;
156 bool IsKnown() const;
157
158private:
159 enum class AttitudeType {
160 UnknownType,
161 QuaternionType,
162 DCMType,
163 };
164
165 AttitudeType type;
166 Quaternion quaternion;
167 Mat3 dcm; // direction cosine matrix
168};
169
170Mat3 QuaternionToDCM(const Quaternion &);
171Quaternion DCMToQuaternion(const Mat3 &);
172
176Quaternion SphericalToQuaternion(decimal ra, decimal dec, decimal roll);
177
180void SpatialToSpherical(const Vec3 &, decimal *ra, decimal *de);
182decimal Angle(const Vec3 &, const Vec3 &);
184decimal AngleUnit(const Vec3 &, const Vec3 &);
185
193
194// TODO: quaternion and euler angle conversion, conversion between ascension/declination to rec9tu
195
196} // namespace lost
197
198#endif
EulerAngles ToSpherical() const
Get the euler angles from the attitude, converting from whatever format is stored.
Vec3 Rotate(const Vec3 &) const
Convert a vector from the reference frame to the body frame.
Mat3 GetDCM() const
Get the rotation matrix (direction cosine matrix) representing the attitude, converting from whatever...
Attitude()=default
constructs unknown attitude:
Quaternion GetQuaternion() const
Get the quaternion representing the attitude, converting from whatever format is stored.
bool IsKnown() const
A "human-readable" way to represent a 3d rotation or orientation.
EulerAngles(decimal ra, decimal de, decimal roll)
decimal roll
How far we roll counterclockwise. Roll is performed last (after yaw and pitch).
decimal de
Declination. How far we pitch up (or down if negative). Pitch is performed second,...
decimal ra
Right ascension. How far we yaw left. Yaw is performed first.
3x3 vector with decimaling point components
Mat3 Inverse() const
Inverse of a matrix.
Vec3 Column(int) const
Get the column at index j.
Vec3 Row(int) const
Get the row at index i.
decimal Det() const
Determinant of a matrix.
Mat3 Transpose() const
Transpose of a matrix.
decimal Trace() const
Trace of a matrix.
decimal At(int i, int j) const
Access the i,j-th element of the matrix.
Mat3 operator+(const Mat3 &) const
Normal matrix addition.
decimal x[9]
Mat3 operator*(const Mat3 &) const
Naive matrix multiplication.
A quaternion is a common way to represent a 3d rotation.
Quaternion Canonicalize() const
Ensure that the quaternion's real part is nonnegative.
Vec3 Vector() const
The vector formed by imaginary components of the quaternion. The axis of the represented rotation.
decimal Angle() const
How many radians the rotation represented by this quaternion has.
EulerAngles ToSpherical() const
Vec3 Rotate(const Vec3 &) const
Rotate a 3d vector according to the rotation represented by the quaternion.
Quaternion(decimal real, decimal i, decimal j, decimal k)
Quaternion Conjugate() const
Effectively computes a quaternion representing the inverse rotation of the original.
void SetVector(const Vec3 &)
Set imaginary components.
Quaternion()=default
Quaternion operator*(const Quaternion &other) const
Multiply two quaternions using the usual definition of quaternion multiplication (effectively compose...
void SetAngle(decimal)
bool IsUnit(decimal tolerance) const
Whether the quaternion is a unit quaternion. All quaternions representing rotations should be units.
decimal SmallestAngle() const
Returns the smallest angle that can be used to represent the rotation represented by the quaternion.
Three dimensional vector with decimaling point components.
Vec3 Normalize() const
Create a vector pointing in the same direction with magnitude 1.
Vec3(decimal x, decimal y, decimal z)
decimal MagnitudeSq() const
The square of the magnitude.
Mat3 OuterProduct(const Vec3 &) const
The outer product of two vectors.
decimal operator*(const Vec3 &) const
Dot product.
Vec3 operator+(const Vec3 &) const
Vec3 CrossProduct(const Vec3 &) const
Usual vector cross product.
decimal Magnitude() const
friend std::ostream & operator<<(std::ostream &output, const Vec3 &vec)
Vec3 operator-(const Vec3 &) const
Usual vector subtraction.
double decimal
Definition decimal.hpp:8
LOST starting point.
Mat3 QuaternionToDCM(const Quaternion &quat)
Convert a quaternion to a rotation matrix (Direction Cosine Matrix).
decimal RadToArcSec(decimal rad)
decimal ArcSecToRad(decimal arcSec)
void SpatialToSpherical(const Vec3 &vec, decimal *ra, decimal *de)
Convert from a 3d point on the unit sphere to right ascension & declination.
decimal Distance(const Vec2 &, const Vec2 &)
decimal AngleUnit(const Vec3 &vec1, const Vec3 &vec2)
Calculate the inner angle, in radians, between two /unit/ vectors.
decimal DegToRad(decimal deg)
Quaternion SphericalToQuaternion(decimal ra, decimal dec, decimal roll)
Return a quaternion that will reorient the coordinate axes so that the x-axis points at the given rig...
Vec3 SphericalToSpatial(decimal ra, decimal de)
Convert from right ascension & declination to a 3d point on the unit sphere.
decimal RadToDeg(decimal rad)
const Mat3 kIdentityMat3
3x3 identity matrix
Quaternion DCMToQuaternion(const Mat3 &dcm)
Convert a rotation matrix (Direction Cosine Matrix) to a quaternion representing the same rotation.
decimal Angle(const Vec3 &vec1, const Vec3 &vec2)
Calculate the inner angle, in radians, between two vectors.
decimal DecimalModulo(decimal x, decimal mod)
Given a decimal, find it "modulo" another decimal, in the true mathematical sense (not remainder).
void SerializeVec3(SerializeContext *ser, const Vec3 &vec)
Serialize a Vec3 to buffer. Takes up space according to SerializeLengthVec3.
Vec3 DeserializeVec3(DeserializeContext *des)
A two dimensional vector with decimaling point components.
Vec2 Normalize() const
decimal MagnitudeSq() const
The square of the magnitude.
Vec2 operator-(const Vec2 &) const
Usual vector subtraction.
decimal operator*(const Vec2 &) const
Vec2 operator+(const Vec2 &) const
Usual vector addition.
decimal Magnitude() const
friend std::ostream & operator<<(std::ostream &output, const Vec2 &vec)