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 <memory>
5#include <vector>
6
8#include "decimal.hpp"
9
10namespace lost {
11
12// At first, I wanted to have two separate Attitude classes, one storing Euler angles and converting
13// to Quaterinon, and another storing as Quaternion and converting to Euler. But abstract classes
14// make everything more annoying, because you need vectors of pointers...ugh!
15
17struct Vec2 {
20
21 decimal Magnitude() const;
22 decimal MagnitudeSq() const;
23
24 Vec2 Normalize() const;
25
26 decimal operator*(const Vec2 &) const;
27 Vec2 operator*(const decimal &) const;
28 Vec2 operator-(const Vec2 &) const;
29 Vec2 operator+(const Vec2 &) const;
30};
31
32class Mat3; // define above so we can use in Vec3 class
33
35class Vec3 {
36public:
40
41 decimal Magnitude() const;
42 decimal MagnitudeSq() const;
43 Vec3 Normalize() const;
44
45 decimal operator*(const Vec3 &) const;
46 Vec3 operator*(const decimal &) const;
47 Vec3 operator*(const Mat3 &) const;
48 Vec3 operator-(const Vec3 &) const;
49 Vec3 CrossProduct(const Vec3 &) const;
50 Mat3 OuterProduct(const Vec3 &) const;
51};
52
54class Mat3 {
55public:
57
58 decimal At(int i, int j) const;
59 Mat3 operator+(const Mat3 &) const;
60 Mat3 operator*(const Mat3 &) const;
61 Vec3 operator*(const Vec3 &) const;
62 Mat3 operator*(const decimal &) const;
63 Mat3 Transpose() const;
64 Vec3 Column(int) const;
65 Vec3 Row(int) const;
66 decimal Trace() const;
67 decimal Det() const;
68 Mat3 Inverse() const;
69};
70
71extern const Mat3 kIdentityMat3;
72
73void SerializeVec3(SerializeContext *, const Vec3 &);
75
76decimal Distance(const Vec2 &, const Vec2 &);
77decimal Distance(const Vec3 &, const Vec3 &);
78
96
99public:
100 Quaternion() = default;
101 explicit Quaternion(const Vec3 &);
102 Quaternion(const Vec3 &, decimal);
103
106
107 Quaternion operator*(const Quaternion &other) const;
108 Quaternion Conjugate() const;
109 Vec3 Vector() const;
110 void SetVector(const Vec3 &);
111 Vec3 Rotate(const Vec3 &) const;
112 decimal Angle() const;
115 decimal SmallestAngle() const;
116 void SetAngle(decimal);
117 EulerAngles ToSpherical() const;
118 bool IsUnit(decimal tolerance) const;
119 Quaternion Canonicalize() const;
120
125};
126
127//
138class Attitude {
139public:
141 Attitude() = default;
142 explicit Attitude(const Quaternion &); // NOLINT
143 explicit Attitude(const Mat3 &dcm);
144
146 Mat3 GetDCM() const;
147 EulerAngles ToSpherical() const;
148 Vec3 Rotate(const Vec3 &) const;
149 bool IsKnown() const;
150
151private:
152 enum class AttitudeType {
153 UnknownType,
154 QuaternionType,
155 DCMType,
156 };
157
158 AttitudeType type;
159 Quaternion quaternion;
160 Mat3 dcm; // direction cosine matrix
161};
162
163Mat3 QuaternionToDCM(const Quaternion &);
164Quaternion DCMToQuaternion(const Mat3 &);
165
169Quaternion SphericalToQuaternion(decimal ra, decimal dec, decimal roll);
170
173void SpatialToSpherical(const Vec3 &, decimal *ra, decimal *de);
175decimal Angle(const Vec3 &, const Vec3 &);
177decimal AngleUnit(const Vec3 &, const Vec3 &);
178
186
187// TODO: quaternion and euler angle conversion, conversion between ascension/declination to rec9tu
188
189}
190
191#endif
The attitude (orientation) of a spacecraft.
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.
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 CrossProduct(const Vec3 &) const
Usual vector cross product.
decimal Magnitude() const
Vec3 operator-(const Vec3 &) const
Usual vector subtraction.
double decimal
Definition decimal.hpp:11
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 SerializePrimitive(SerializeContext *ser, const T &val)
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