Vectors and rotation matrices
Almost every coordinate transform in the library works the same way. It turns a position on the sky into a 3D unit vector, rotates that vector into another frame, and turns it back into two angles. Precession, nutation and frame bias are each a rotation matrix. Applying several of them in a row means multiplying the matrices.
celestial_core::matrix provides the two types behind this: Vector3 for directions
and positions, and RotationMatrix3 for the rotations. Here is the whole loop:
use celestial_core::matrix::{RotationMatrix3, Vector3};
let mut frame = RotationMatrix3::identity();
frame.rotate_z(0.1);
let star = Vector3::from_spherical(1.2, 0.3); // RA, Dec in radians
let (ra, dec) = (frame * star).to_spherical();
All angles in this module are plain f64 radians. Use Angle::radians() to get them
from an Angle.
Vector3
A 3D Cartesian vector with public x, y and z fields.
Create a vector
use celestial_core::matrix::Vector3;
let v = Vector3::new(1.0, 2.0, 3.0);
let x = Vector3::x_axis(); // [1, 0, 0]
let y = Vector3::y_axis(); // [0, 1, 0]
let z = Vector3::z_axis(); // [0, 0, 1]
let zero = Vector3::zeros();
let v = Vector3::from_array([1.0, 2.0, 3.0]);
let arr: [f64; 3] = v.to_array();
Convert to and from spherical coordinates
from_spherical(ra, dec) returns the unit vector pointing at that RA and Dec:
use celestial_core::constants::HALF_PI;
use celestial_core::matrix::Vector3;
Vector3::from_spherical(0.0, 0.0); // [1, 0, 0]
Vector3::from_spherical(HALF_PI, 0.0); // [6.123233995736766e-17, 1, 0]
Vector3::from_spherical(0.0, HALF_PI); // [6.123233995736766e-17, 0, 1], the north pole
The tiny x components are real. HALF_PI is slightly less than the true π/2, so its cosine
is not quite zero.
to_spherical goes the other way and returns (theta, phi):
thetais the angle from +X toward +Y, like RA, in(-π, π].phiis the elevation above the XY plane, like Dec, in[-π/2, π/2].
use celestial_core::matrix::Vector3;
let (theta, phi) = Vector3::new(0.0, 0.0, 1.0).to_spherical();
// theta = 0.0, phi = π/2
The vector doesn’t need unit length, because only its direction matters. The zero
vector returns (0.0, 0.0). The names follow the astronomy convention, where phi is
elevation. Physics texts often swap the two names.
Length and normalization
use celestial_core::matrix::Vector3;
let v = Vector3::new(3.0, 4.0, 0.0);
v.magnitude(); // 5.0
v.magnitude_squared(); // 25.0, skips the square root
let unit = v.normalize()?; // [0.6000000000000001, 0.8, 0.0]
normalize returns an error for a zero-length or non-finite vector. It multiplies by
1/|v| rather than dividing, as ERFA’s pn does, which is why x lands one bit above
0.6.
Dot and cross products
For unit vectors, the dot product is the cosine of the angle between them. The cross product gives the axis perpendicular to both, by the right-hand rule.
use celestial_core::matrix::Vector3;
let a = Vector3::x_axis();
let b = Vector3::y_axis();
a.dot(&b); // 0.0
a.cross(&b); // [0, 0, 1], the z axis
Vector3::new(1.0, 2.0, 3.0).dot(&Vector3::new(4.0, 5.0, 6.0)); // 32.0
Read and write components
Read and write the fields directly. get and set take an index instead, and return an
error for any index outside 0 to 2.
use celestial_core::matrix::Vector3;
let mut v = Vector3::new(1.0, 2.0, 3.0);
v.x = 10.0;
v.get(0)?; // 10.0
v.set(1, 20.0)?;
v.get(3); // Err: index out of bounds
Arithmetic
use celestial_core::matrix::Vector3;
let a = Vector3::new(1.0, 2.0, 3.0);
let b = Vector3::new(4.0, 5.0, 6.0);
a + b; // [5, 7, 9]
b - a; // [3, 3, 3]
a * 2.0; // [2, 4, 6]
3.0 * a; // [3, 6, 9]
a / 2.0; // [0.5, 1, 1.5]
-a; // [-1, -2, -3]
let mut v = Vector3::new(10.0, 20.0, 30.0);
v /= 2.0; // [5, 10, 15]
Display a vector
Display prints nine decimal places:
use celestial_core::matrix::Vector3;
println!("{}", Vector3::new(1.0, -2.5, 3.0));
// Vector3(1.000000000, -2.500000000, 3.000000000)
RotationMatrix3
A 3x3 rotation matrix stored row-major. The elements are private, and every
constructor either builds a rotation or checks that its input is one. Read the elements
with elements().
Create a matrix
Start from the identity and add rotations, or pass the elements in:
use celestial_core::matrix::RotationMatrix3;
let m = RotationMatrix3::identity();
let m = RotationMatrix3::from_array([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
])?;
from_array checks its input. It returns an error unless every element is finite and
the matrix is a proper rotation. That means M * M^T within 1e-12 of the identity and a
determinant within 1e-12 of +1. A scaling matrix such as diag(2, 1, 1) fails.
Deserializing with serde runs the same check.
There is no per-element setter, for the same reason.
Build a rotation
rotate_x, rotate_y and rotate_z each rotate the matrix in place about one axis.
Each call replaces the matrix self with R_axis(angle) * self, so later calls apply
after earlier ones:
use celestial_core::matrix::RotationMatrix3;
let mut m = RotationMatrix3::identity();
m.rotate_z(0.1); // m = Rz
m.rotate_y(-0.05); // m = Ry * Rz
m.rotate_x(0.02); // m = Rx * Ry * Rz
Rotations follow the ERFA convention. A positive angle rotates the coordinate frame counterclockwise, seen from the positive axis looking toward the origin. Because the frame turns and the vector stays put, the vector’s coordinates move the opposite way:
use celestial_core::constants::HALF_PI;
use celestial_core::matrix::RotationMatrix3;
let mut m = RotationMatrix3::identity();
m.rotate_z(HALF_PI);
m.apply_to_vector([1.0, 0.0, 0.0]); // [6.123233995736766e-17, -1.0, 0.0]
Apply a matrix to a vector
apply_to_vector works on [f64; 3]. The * operator works on Vector3, with the
matrix by value or by reference.
use celestial_core::matrix::{RotationMatrix3, Vector3};
let mut m = RotationMatrix3::identity();
m.rotate_z(0.5);
let out: [f64; 3] = m.apply_to_vector([1.0, 0.0, 0.0]);
let v = Vector3::new(1.0, 0.0, 0.0);
let out: Vector3 = m * v;
let out: Vector3 = &m * v;
Compose rotations
Multiplying two matrices composes their rotations. The rightmost matrix acts first.
use celestial_core::matrix::RotationMatrix3;
let mut rx = RotationMatrix3::identity();
rx.rotate_x(0.1);
let mut rz = RotationMatrix3::identity();
rz.rotate_z(0.2);
// Apply rx, then rz
let combined = rz * rx;
let combined = rz.multiply(&rx); // same result
* accepts every mix of owned and borrowed operands: m * m, m * &m, &m * m and
&m * &m.
Transform RA and Dec in one call
transform_spherical converts to a unit vector, applies the matrix and converts back:
use celestial_core::constants::QUARTER_PI;
use celestial_core::matrix::RotationMatrix3;
let mut m = RotationMatrix3::identity();
m.rotate_z(QUARTER_PI);
let (ra, dec) = m.transform_spherical(0.0, 0.0);
// ra = -0.7853981633974483 (-45°), dec = 0.0
Input and output are in radians. Output RA is in (-π, π] and output Dec is in
[-π/2, π/2]. Wrap the RA with wrap_0_2pi if you need it positive.
Invert a rotation
The inverse of a rotation matrix is its transpose. transpose() only swaps elements,
so it adds no rounding error:
use celestial_core::matrix::RotationMatrix3;
let mut m = RotationMatrix3::identity();
m.rotate_z(0.5);
m.rotate_x(0.3);
let rotated = m.apply_to_vector([1.0, 2.0, 3.0]);
let restored = m.transpose().apply_to_vector(rotated);
// restored = [1.0, 2.0, 2.9999999999999996]
The round trip lands within a bit of the start because each apply_to_vector rounds.
Check and compare matrices
use celestial_core::matrix::RotationMatrix3;
let mut m = RotationMatrix3::identity();
m.rotate_z(0.5);
m.is_rotation_matrix(1e-14); // true: orthogonal with determinant +1, within the tolerance
m.determinant(); // 1.0
let a = RotationMatrix3::identity();
a.max_difference(&m); // largest absolute element-wise difference
Read the elements
use celestial_core::matrix::RotationMatrix3;
let m = RotationMatrix3::identity();
let elems: &[[f64; 3]; 3] = m.elements();
elems[0][0]; // 1.0
Display a matrix
Display prints the matrix over several lines with nine decimal places:
use celestial_core::matrix::RotationMatrix3;
println!("{}", RotationMatrix3::identity());
// RotationMatrix3:
// [ 1.000000000 0.000000000 0.000000000]
// [ 0.000000000 1.000000000 0.000000000]
// [ 0.000000000 0.000000000 1.000000000]